@Test public void testAddAccessPolicy() throws Exception { writeFile(primaryAuthorizations, EMPTY_AUTHORIZATIONS); writeFile(primaryTenants, EMPTY_TENANTS); authorizer.onConfigured(configurationContext); assertEquals(0, authorizer.getAccessPolicies().size()); final AccessPolicy policy1 = new AccessPolicy.Builder() .identifier("policy-1") .resource("resource-1") .addUser("user-1") .addGroup("group-1") .action(RequestAction.READ) .build(); final AccessPolicy returnedPolicy1 = authorizer.addAccessPolicy(policy1); assertNotNull(returnedPolicy1); assertEquals(policy1.getIdentifier(), returnedPolicy1.getIdentifier()); assertEquals(policy1.getResource(), returnedPolicy1.getResource()); assertEquals(policy1.getUsers(), returnedPolicy1.getUsers()); assertEquals(policy1.getGroups(), returnedPolicy1.getGroups()); assertEquals(policy1.getAction(), returnedPolicy1.getAction()); assertEquals(1, authorizer.getAccessPolicies().size()); final AccessPolicy policy2 = new AccessPolicy.Builder() .identifier("policy-2") .resource("resource-1") .addUser("user-1") .addGroup("group-1") .action(RequestAction.READ) .build(); try { final AccessPolicy returnedPolicy2 = authorizer.addAccessPolicy(policy2); Assert.fail("Should have thrown exception"); } catch (Exception e) { } assertEquals(1, authorizer.getAccessPolicies().size()); }
private void addAccessPolicy(final Authorizations authorizations, final String resource, final String identity, final String action) { Policy foundPolicy = null; for (Policy policy : authorizations.getPolicies().getPolicy()) { if (policy.getResource().equals(resource) && policy.getAction().equals(action)) { foundPolicy = policy; break; } } if (foundPolicy == null) { final String uuidSeed = resource + identity + action; final UUID policyIdentifier = UUID.nameUUIDFromBytes(uuidSeed.getBytes(StandardCharsets.UTF_8)); final AccessPolicy.Builder builder = new AccessPolicy.Builder() .identifier(policyIdentifier.toString()) .resource(resource) .addUser(identity); if (action.equals(READ_CODE)) { builder.action(RequestAction.READ); } else if (action.equals(WRITE_CODE)) { builder.action(RequestAction.WRITE); } else { throw new IllegalStateException("Unknown Policy Action: " + action); } final AccessPolicy accessPolicy = builder.build(); final Policy jaxbPolicy = createJAXBPolicy(accessPolicy); authorizations.getPolicies().getPolicy().add(jaxbPolicy); } else { Policy.User policyUser = new Policy.User(); policyUser.setIdentifier(identity); foundPolicy.getUser().add(policyUser); } }
FileAuthorizer extends AbstractPolicyBasedAuthorizer { private void addAccessPolicy(final Authorizations authorizations, final String resource, final String identity, final String action) { Policy foundPolicy = null; for (Policy policy : authorizations.getPolicies().getPolicy()) { if (policy.getResource().equals(resource) && policy.getAction().equals(action)) { foundPolicy = policy; break; } } if (foundPolicy == null) { final String uuidSeed = resource + identity + action; final UUID policyIdentifier = UUID.nameUUIDFromBytes(uuidSeed.getBytes(StandardCharsets.UTF_8)); final AccessPolicy.Builder builder = new AccessPolicy.Builder() .identifier(policyIdentifier.toString()) .resource(resource) .addUser(identity); if (action.equals(READ_CODE)) { builder.action(RequestAction.READ); } else if (action.equals(WRITE_CODE)) { builder.action(RequestAction.WRITE); } else { throw new IllegalStateException("Unknown Policy Action: " + action); } final AccessPolicy accessPolicy = builder.build(); final Policy jaxbPolicy = createJAXBPolicy(accessPolicy); authorizations.getPolicies().getPolicy().add(jaxbPolicy); } else { Policy.User policyUser = new Policy.User(); policyUser.setIdentifier(identity); foundPolicy.getUser().add(policyUser); } } }
FileAuthorizer extends AbstractPolicyBasedAuthorizer { private void addAccessPolicy(final Authorizations authorizations, final String resource, final String identity, final String action) { Policy foundPolicy = null; for (Policy policy : authorizations.getPolicies().getPolicy()) { if (policy.getResource().equals(resource) && policy.getAction().equals(action)) { foundPolicy = policy; break; } } if (foundPolicy == null) { final String uuidSeed = resource + identity + action; final UUID policyIdentifier = UUID.nameUUIDFromBytes(uuidSeed.getBytes(StandardCharsets.UTF_8)); final AccessPolicy.Builder builder = new AccessPolicy.Builder() .identifier(policyIdentifier.toString()) .resource(resource) .addUser(identity); if (action.equals(READ_CODE)) { builder.action(RequestAction.READ); } else if (action.equals(WRITE_CODE)) { builder.action(RequestAction.WRITE); } else { throw new IllegalStateException("Unknown Policy Action: " + action); } final AccessPolicy accessPolicy = builder.build(); final Policy jaxbPolicy = createJAXBPolicy(accessPolicy); authorizations.getPolicies().getPolicy().add(jaxbPolicy); } else { Policy.User policyUser = new Policy.User(); policyUser.setIdentifier(identity); foundPolicy.getUser().add(policyUser); } }  }
FileAuthorizer extends AbstractPolicyBasedAuthorizer { private void addAccessPolicy(final Authorizations authorizations, final String resource, final String identity, final String action) { Policy foundPolicy = null; for (Policy policy : authorizations.getPolicies().getPolicy()) { if (policy.getResource().equals(resource) && policy.getAction().equals(action)) { foundPolicy = policy; break; } } if (foundPolicy == null) { final String uuidSeed = resource + identity + action; final UUID policyIdentifier = UUID.nameUUIDFromBytes(uuidSeed.getBytes(StandardCharsets.UTF_8)); final AccessPolicy.Builder builder = new AccessPolicy.Builder() .identifier(policyIdentifier.toString()) .resource(resource) .addUser(identity); if (action.equals(READ_CODE)) { builder.action(RequestAction.READ); } else if (action.equals(WRITE_CODE)) { builder.action(RequestAction.WRITE); } else { throw new IllegalStateException("Unknown Policy Action: " + action); } final AccessPolicy accessPolicy = builder.build(); final Policy jaxbPolicy = createJAXBPolicy(accessPolicy); authorizations.getPolicies().getPolicy().add(jaxbPolicy); } else { Policy.User policyUser = new Policy.User(); policyUser.setIdentifier(identity); foundPolicy.getUser().add(policyUser); } }  @Override void initialize(final AuthorizerInitializationContext initializationContext); @Override void doOnConfigured(final AuthorizerConfigurationContext configurationContext); @AuthorizerContext void setNiFiProperties(NiFiProperties properties); @Override void preDestruction(); @Override synchronized Group doAddGroup(Group group); @Override Group getGroup(String identifier); @Override synchronized Group doUpdateGroup(Group group); @Override synchronized Group deleteGroup(Group group); @Override Set<Group> getGroups(); @Override synchronized User doAddUser(final User user); @Override User getUser(final String identifier); @Override User getUserByIdentity(final String identity); @Override synchronized User doUpdateUser(final User user); @Override synchronized User deleteUser(final User user); @Override Set<User> getUsers(); @Override synchronized AccessPolicy doAddAccessPolicy(final AccessPolicy accessPolicy); @Override AccessPolicy getAccessPolicy(final String identifier); @Override synchronized AccessPolicy updateAccessPolicy(final AccessPolicy accessPolicy); @Override synchronized AccessPolicy deleteAccessPolicy(final AccessPolicy accessPolicy); @Override Set<AccessPolicy> getAccessPolicies(); @Override UsersAndAccessPolicies getUsersAndAccessPolicies(); }
FileAuthorizer extends AbstractPolicyBasedAuthorizer { private void addAccessPolicy(final Authorizations authorizations, final String resource, final String identity, final String action) { Policy foundPolicy = null; for (Policy policy : authorizations.getPolicies().getPolicy()) { if (policy.getResource().equals(resource) && policy.getAction().equals(action)) { foundPolicy = policy; break; } } if (foundPolicy == null) { final String uuidSeed = resource + identity + action; final UUID policyIdentifier = UUID.nameUUIDFromBytes(uuidSeed.getBytes(StandardCharsets.UTF_8)); final AccessPolicy.Builder builder = new AccessPolicy.Builder() .identifier(policyIdentifier.toString()) .resource(resource) .addUser(identity); if (action.equals(READ_CODE)) { builder.action(RequestAction.READ); } else if (action.equals(WRITE_CODE)) { builder.action(RequestAction.WRITE); } else { throw new IllegalStateException("Unknown Policy Action: " + action); } final AccessPolicy accessPolicy = builder.build(); final Policy jaxbPolicy = createJAXBPolicy(accessPolicy); authorizations.getPolicies().getPolicy().add(jaxbPolicy); } else { Policy.User policyUser = new Policy.User(); policyUser.setIdentifier(identity); foundPolicy.getUser().add(policyUser); } }  @Override void initialize(final AuthorizerInitializationContext initializationContext); @Override void doOnConfigured(final AuthorizerConfigurationContext configurationContext); @AuthorizerContext void setNiFiProperties(NiFiProperties properties); @Override void preDestruction(); @Override synchronized Group doAddGroup(Group group); @Override Group getGroup(String identifier); @Override synchronized Group doUpdateGroup(Group group); @Override synchronized Group deleteGroup(Group group); @Override Set<Group> getGroups(); @Override synchronized User doAddUser(final User user); @Override User getUser(final String identifier); @Override User getUserByIdentity(final String identity); @Override synchronized User doUpdateUser(final User user); @Override synchronized User deleteUser(final User user); @Override Set<User> getUsers(); @Override synchronized AccessPolicy doAddAccessPolicy(final AccessPolicy accessPolicy); @Override AccessPolicy getAccessPolicy(final String identifier); @Override synchronized AccessPolicy updateAccessPolicy(final AccessPolicy accessPolicy); @Override synchronized AccessPolicy deleteAccessPolicy(final AccessPolicy accessPolicy); @Override Set<AccessPolicy> getAccessPolicies(); @Override UsersAndAccessPolicies getUsersAndAccessPolicies();  }