@Test public void testInheritFingerprint() { User user1 = new User.Builder().identifier("user-id-1").identity("user-1").build(); User user2 = new User.Builder().identifier("user-id-2").identity("user-2").build(); Group group1 = new Group.Builder().identifier("group-id-1").name("group-1").addUser(user1.getIdentifier()).build(); Group group2 = new Group.Builder().identifier("group-id-2").name("group-2").build(); AccessPolicy policy1 = new AccessPolicy.Builder() .identifier("policy-id-1") .resource("resource1") .action(RequestAction.READ) .addUser(user1.getIdentifier()) .addUser(user2.getIdentifier()) .build(); AccessPolicy policy2 = new AccessPolicy.Builder() .identifier("policy-id-2") .resource("resource2") .action(RequestAction.READ) .addGroup(group1.getIdentifier()) .addGroup(group2.getIdentifier()) .addUser(user1.getIdentifier()) .addUser(user2.getIdentifier()) .build(); Set<Group> groups1 = new LinkedHashSet<>(); groups1.add(group1); groups1.add(group2); Set<User> users1 = new LinkedHashSet<>(); users1.add(user1); users1.add(user2); Set<AccessPolicy> policies1 = new LinkedHashSet<>(); policies1.add(policy1); policies1.add(policy2); AbstractPolicyBasedAuthorizer authorizer1 = Mockito.mock(AbstractPolicyBasedAuthorizer.class); when(authorizer1.getGroups()).thenReturn(groups1); when(authorizer1.getUsers()).thenReturn(users1); when(authorizer1.getAccessPolicies()).thenReturn(policies1); final String fingerprint1 = authorizer1.getFingerprint(); AbstractPolicyBasedAuthorizer authorizer2 = new MockPolicyBasedAuthorizer(); authorizer2.inheritFingerprint(fingerprint1); final String fingerprint2 = authorizer2.getFingerprint(); assertEquals(fingerprint1, fingerprint2); assertEquals(authorizer1.getUsers(), authorizer2.getUsers()); assertEquals(authorizer1.getGroups(), authorizer2.getGroups()); assertEquals(authorizer1.getAccessPolicies(), authorizer2.getAccessPolicies()); }
public final void inheritFingerprint(final String fingerprint) throws AuthorizationAccessException { if (fingerprint == null || fingerprint.trim().isEmpty()) { return; } final byte[] fingerprintBytes = fingerprint.getBytes(StandardCharsets.UTF_8); try (final ByteArrayInputStream in = new ByteArrayInputStream(fingerprintBytes)) { final DocumentBuilder docBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder(); final Document document = docBuilder.parse(in); final Element rootElement = document.getDocumentElement(); NodeList userNodes = rootElement.getElementsByTagName(USER_ELEMENT); for (int i=0; i < userNodes.getLength(); i++) { Node userNode = userNodes.item(i); User user = parseUser((Element) userNode); addUser(user); } NodeList groupNodes = rootElement.getElementsByTagName(GROUP_ELEMENT); for (int i=0; i < groupNodes.getLength(); i++) { Node groupNode = groupNodes.item(i); Group group = parseGroup((Element) groupNode); addGroup(group); } NodeList policyNodes = rootElement.getElementsByTagName(POLICY_ELEMENT); for (int i=0; i < policyNodes.getLength(); i++) { Node policyNode = policyNodes.item(i); AccessPolicy policy = parsePolicy((Element) policyNode); addAccessPolicy(policy); } } catch (SAXException | ParserConfigurationException | IOException e) { throw new AuthorizationAccessException("Unable to parse fingerprint", e); } }
AbstractPolicyBasedAuthorizer implements Authorizer { public final void inheritFingerprint(final String fingerprint) throws AuthorizationAccessException { if (fingerprint == null || fingerprint.trim().isEmpty()) { return; } final byte[] fingerprintBytes = fingerprint.getBytes(StandardCharsets.UTF_8); try (final ByteArrayInputStream in = new ByteArrayInputStream(fingerprintBytes)) { final DocumentBuilder docBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder(); final Document document = docBuilder.parse(in); final Element rootElement = document.getDocumentElement(); NodeList userNodes = rootElement.getElementsByTagName(USER_ELEMENT); for (int i=0; i < userNodes.getLength(); i++) { Node userNode = userNodes.item(i); User user = parseUser((Element) userNode); addUser(user); } NodeList groupNodes = rootElement.getElementsByTagName(GROUP_ELEMENT); for (int i=0; i < groupNodes.getLength(); i++) { Node groupNode = groupNodes.item(i); Group group = parseGroup((Element) groupNode); addGroup(group); } NodeList policyNodes = rootElement.getElementsByTagName(POLICY_ELEMENT); for (int i=0; i < policyNodes.getLength(); i++) { Node policyNode = policyNodes.item(i); AccessPolicy policy = parsePolicy((Element) policyNode); addAccessPolicy(policy); } } catch (SAXException | ParserConfigurationException | IOException e) { throw new AuthorizationAccessException("Unable to parse fingerprint", e); } } }
AbstractPolicyBasedAuthorizer implements Authorizer { public final void inheritFingerprint(final String fingerprint) throws AuthorizationAccessException { if (fingerprint == null || fingerprint.trim().isEmpty()) { return; } final byte[] fingerprintBytes = fingerprint.getBytes(StandardCharsets.UTF_8); try (final ByteArrayInputStream in = new ByteArrayInputStream(fingerprintBytes)) { final DocumentBuilder docBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder(); final Document document = docBuilder.parse(in); final Element rootElement = document.getDocumentElement(); NodeList userNodes = rootElement.getElementsByTagName(USER_ELEMENT); for (int i=0; i < userNodes.getLength(); i++) { Node userNode = userNodes.item(i); User user = parseUser((Element) userNode); addUser(user); } NodeList groupNodes = rootElement.getElementsByTagName(GROUP_ELEMENT); for (int i=0; i < groupNodes.getLength(); i++) { Node groupNode = groupNodes.item(i); Group group = parseGroup((Element) groupNode); addGroup(group); } NodeList policyNodes = rootElement.getElementsByTagName(POLICY_ELEMENT); for (int i=0; i < policyNodes.getLength(); i++) { Node policyNode = policyNodes.item(i); AccessPolicy policy = parsePolicy((Element) policyNode); addAccessPolicy(policy); } } catch (SAXException | ParserConfigurationException | IOException e) { throw new AuthorizationAccessException("Unable to parse fingerprint", e); } }  }
AbstractPolicyBasedAuthorizer implements Authorizer { public final void inheritFingerprint(final String fingerprint) throws AuthorizationAccessException { if (fingerprint == null || fingerprint.trim().isEmpty()) { return; } final byte[] fingerprintBytes = fingerprint.getBytes(StandardCharsets.UTF_8); try (final ByteArrayInputStream in = new ByteArrayInputStream(fingerprintBytes)) { final DocumentBuilder docBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder(); final Document document = docBuilder.parse(in); final Element rootElement = document.getDocumentElement(); NodeList userNodes = rootElement.getElementsByTagName(USER_ELEMENT); for (int i=0; i < userNodes.getLength(); i++) { Node userNode = userNodes.item(i); User user = parseUser((Element) userNode); addUser(user); } NodeList groupNodes = rootElement.getElementsByTagName(GROUP_ELEMENT); for (int i=0; i < groupNodes.getLength(); i++) { Node groupNode = groupNodes.item(i); Group group = parseGroup((Element) groupNode); addGroup(group); } NodeList policyNodes = rootElement.getElementsByTagName(POLICY_ELEMENT); for (int i=0; i < policyNodes.getLength(); i++) { Node policyNode = policyNodes.item(i); AccessPolicy policy = parsePolicy((Element) policyNode); addAccessPolicy(policy); } } catch (SAXException | ParserConfigurationException | IOException e) { throw new AuthorizationAccessException("Unable to parse fingerprint", e); } }  @Override final void onConfigured(final AuthorizerConfigurationContext configurationContext); @Override final AuthorizationResult authorize(AuthorizationRequest request); final synchronized Group addGroup(Group group); abstract Group doAddGroup(Group group); abstract Group getGroup(String identifier); final synchronized Group updateGroup(Group group); abstract Group doUpdateGroup(Group group); abstract Group deleteGroup(Group group); abstract Set<Group> getGroups(); final synchronized User addUser(User user); abstract User doAddUser(User user); abstract User getUser(String identifier); abstract User getUserByIdentity(String identity); final synchronized User updateUser(final User user); abstract User doUpdateUser(User user); abstract User deleteUser(User user); abstract Set<User> getUsers(); final synchronized AccessPolicy addAccessPolicy(AccessPolicy accessPolicy); abstract AccessPolicy getAccessPolicy(String identifier); abstract AccessPolicy updateAccessPolicy(AccessPolicy accessPolicy); abstract AccessPolicy deleteAccessPolicy(AccessPolicy policy); abstract Set<AccessPolicy> getAccessPolicies(); abstract UsersAndAccessPolicies getUsersAndAccessPolicies(); final void inheritFingerprint(final String fingerprint); final String getFingerprint(); }
AbstractPolicyBasedAuthorizer implements Authorizer { public final void inheritFingerprint(final String fingerprint) throws AuthorizationAccessException { if (fingerprint == null || fingerprint.trim().isEmpty()) { return; } final byte[] fingerprintBytes = fingerprint.getBytes(StandardCharsets.UTF_8); try (final ByteArrayInputStream in = new ByteArrayInputStream(fingerprintBytes)) { final DocumentBuilder docBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder(); final Document document = docBuilder.parse(in); final Element rootElement = document.getDocumentElement(); NodeList userNodes = rootElement.getElementsByTagName(USER_ELEMENT); for (int i=0; i < userNodes.getLength(); i++) { Node userNode = userNodes.item(i); User user = parseUser((Element) userNode); addUser(user); } NodeList groupNodes = rootElement.getElementsByTagName(GROUP_ELEMENT); for (int i=0; i < groupNodes.getLength(); i++) { Node groupNode = groupNodes.item(i); Group group = parseGroup((Element) groupNode); addGroup(group); } NodeList policyNodes = rootElement.getElementsByTagName(POLICY_ELEMENT); for (int i=0; i < policyNodes.getLength(); i++) { Node policyNode = policyNodes.item(i); AccessPolicy policy = parsePolicy((Element) policyNode); addAccessPolicy(policy); } } catch (SAXException | ParserConfigurationException | IOException e) { throw new AuthorizationAccessException("Unable to parse fingerprint", e); } }  @Override final void onConfigured(final AuthorizerConfigurationContext configurationContext); @Override final AuthorizationResult authorize(AuthorizationRequest request); final synchronized Group addGroup(Group group); abstract Group doAddGroup(Group group); abstract Group getGroup(String identifier); final synchronized Group updateGroup(Group group); abstract Group doUpdateGroup(Group group); abstract Group deleteGroup(Group group); abstract Set<Group> getGroups(); final synchronized User addUser(User user); abstract User doAddUser(User user); abstract User getUser(String identifier); abstract User getUserByIdentity(String identity); final synchronized User updateUser(final User user); abstract User doUpdateUser(User user); abstract User deleteUser(User user); abstract Set<User> getUsers(); final synchronized AccessPolicy addAccessPolicy(AccessPolicy accessPolicy); abstract AccessPolicy getAccessPolicy(String identifier); abstract AccessPolicy updateAccessPolicy(AccessPolicy accessPolicy); abstract AccessPolicy deleteAccessPolicy(AccessPolicy policy); abstract Set<AccessPolicy> getAccessPolicies(); abstract UsersAndAccessPolicies getUsersAndAccessPolicies(); final void inheritFingerprint(final String fingerprint); final String getFingerprint(); static final String EMPTY_FINGERPRINT; }