ClientPermissionLoader { public static AccessEnforcement loadIniFile(final File iniFile) throws IOException { System.out.println("Loading client permissions: " + iniFile.toString()); final Wini ini = new Wini(iniFile); final ConcurrentMap<String, ClientPermissions> permissionMap = new ConcurrentHashMap<String, ClientPermissions>(); final Set<String> knownSecrets = new HashSet<>(); final Collection<Profile.Section> secretSections = ini.values(); for (Profile.Section secretSection : secretSections) { final String secretName = secretSection.getName(); knownSecrets.add(secretName); for (final Entry<String, String> userPermission : secretSection.entrySet()) { final String username = userPermission.getKey(); final String permissions = userPermission.getValue(); final String[] permissionArray = permissions.split(","); permissionMap.putIfAbsent(username, new ClientPermissions()); final ClientPermissions clientPermissions = permissionMap.get(username); for (final String permissionString : permissionArray) { final Permissions permission = Permissions.valueOf(permissionString.trim().toUpperCase()); clientPermissions.addPermission(secretName, permission); } } } return new AccessEnforcement(permissionMap, knownSecrets); }  static AccessEnforcement loadIniFile(final File iniFile);  }
@Test public void testNotFoundSecret() throws IOException, UnauthorizedException, NotFoundException { final AccessEnforcement accessEnforcement = ClientPermissionLoader .loadIniFile(new File("config/client/clients.config")); Assert.assertNotNull(accessEnforcement); try { accessEnforcement.enforceAccess(admin, "no-such-secret", Permissions.GENERATE); Assert.fail("Expected not found exception"); } catch (NotFoundException expected) { } }
@Test public void testAdminPermissions() throws IOException, UnauthorizedException, NotFoundException { final AccessEnforcement accessEnforcement = ClientPermissionLoader .loadIniFile(new File("config/client/clients.config")); Assert.assertNotNull(accessEnforcement); accessEnforcement.enforceAccess(admin, prfSecret, Permissions.GENERATE); accessEnforcement.enforceAccess(admin, prfSecret, Permissions.DELETE); accessEnforcement.enforceAccess(admin, prfSecret, Permissions.ENABLE); accessEnforcement.enforceAccess(admin, prfSecret, Permissions.DISABLE); accessEnforcement.enforceAccess(admin, prfSecret, Permissions.INFO); try { accessEnforcement.enforceAccess(admin, prfSecret, Permissions.EXPONENTIATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(admin, prfSecret, Permissions.STORE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(admin, prfSecret, Permissions.READ); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(admin, prfSecret, Permissions.SIGN); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } accessEnforcement.enforceAccess(admin, mySecret, Permissions.DELETE); accessEnforcement.enforceAccess(admin, mySecret, Permissions.ENABLE); accessEnforcement.enforceAccess(admin, mySecret, Permissions.DISABLE); accessEnforcement.enforceAccess(admin, mySecret, Permissions.INFO); try { accessEnforcement.enforceAccess(admin, mySecret, Permissions.GENERATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(admin, mySecret, Permissions.EXPONENTIATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(admin, mySecret, Permissions.STORE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(admin, mySecret, Permissions.READ); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(admin, mySecret, Permissions.SIGN); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } }
@Test public void testSecOfficerPermissions() throws IOException, UnauthorizedException, NotFoundException { final AccessEnforcement accessEnforcement = ClientPermissionLoader .loadIniFile(new File("config/client/clients.config")); Assert.assertNotNull(accessEnforcement); accessEnforcement.enforceAccess(secOfficer, prfSecret, Permissions.DISABLE); accessEnforcement.enforceAccess(secOfficer, prfSecret, Permissions.INFO); try { accessEnforcement.enforceAccess(secOfficer, prfSecret, Permissions.GENERATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, prfSecret, Permissions.DELETE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, prfSecret, Permissions.ENABLE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, prfSecret, Permissions.EXPONENTIATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, prfSecret, Permissions.STORE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, prfSecret, Permissions.READ); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, prfSecret, Permissions.SIGN); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } accessEnforcement.enforceAccess(secOfficer, mySecret, Permissions.DISABLE); accessEnforcement.enforceAccess(secOfficer, mySecret, Permissions.INFO); try { accessEnforcement.enforceAccess(secOfficer, mySecret, Permissions.GENERATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, mySecret, Permissions.DELETE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, mySecret, Permissions.ENABLE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, mySecret, Permissions.EXPONENTIATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, mySecret, Permissions.STORE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, mySecret, Permissions.READ); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(secOfficer, mySecret, Permissions.SIGN); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } }
@Test public void testExpUserPermissions() throws IOException, UnauthorizedException, NotFoundException { final AccessEnforcement accessEnforcement = ClientPermissionLoader .loadIniFile(new File("config/client/clients.config")); Assert.assertNotNull(accessEnforcement); accessEnforcement.enforceAccess(expUser, prfSecret, Permissions.EXPONENTIATE); accessEnforcement.enforceAccess(expUser, prfSecret, Permissions.INFO); try { accessEnforcement.enforceAccess(expUser, prfSecret, Permissions.GENERATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, prfSecret, Permissions.DELETE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, prfSecret, Permissions.ENABLE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, prfSecret, Permissions.DISABLE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, prfSecret, Permissions.STORE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, prfSecret, Permissions.READ); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, prfSecret, Permissions.SIGN); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, mySecret, Permissions.GENERATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, mySecret, Permissions.DISABLE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, mySecret, Permissions.INFO); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, mySecret, Permissions.DELETE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, mySecret, Permissions.ENABLE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, mySecret, Permissions.EXPONENTIATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, mySecret, Permissions.STORE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, mySecret, Permissions.READ); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(expUser, mySecret, Permissions.SIGN); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } }
@Test public void testStoreUserPermissions() throws IOException, UnauthorizedException, NotFoundException { final AccessEnforcement accessEnforcement = ClientPermissionLoader .loadIniFile(new File("config/client/clients.config")); Assert.assertNotNull(accessEnforcement); accessEnforcement.enforceAccess(storeUser, mySecret, Permissions.STORE); accessEnforcement.enforceAccess(storeUser, mySecret, Permissions.READ); accessEnforcement.enforceAccess(storeUser, mySecret, Permissions.DELETE); accessEnforcement.enforceAccess(storeUser, mySecret, Permissions.INFO); try { accessEnforcement.enforceAccess(storeUser, mySecret, Permissions.GENERATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, mySecret, Permissions.ENABLE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, mySecret, Permissions.DISABLE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, mySecret, Permissions.EXPONENTIATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, mySecret, Permissions.SIGN); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, prfSecret, Permissions.GENERATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, prfSecret, Permissions.DISABLE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, prfSecret, Permissions.INFO); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, prfSecret, Permissions.DELETE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, prfSecret, Permissions.ENABLE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, prfSecret, Permissions.EXPONENTIATE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, prfSecret, Permissions.STORE); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, prfSecret, Permissions.READ); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } try { accessEnforcement.enforceAccess(storeUser, prfSecret, Permissions.SIGN); Assert.fail("Expected unauthorized exception"); } catch (UnauthorizedException expected) { } }