Search in sources :

Example 56 with StandardPropertyValue

use of org.apache.nifi.attribute.expression.language.StandardPropertyValue in project nifi by apache.

the class FileAuthorizerTest method testOnConfiguredWhenLegacyUsersFileProvided.

@Test
public void testOnConfiguredWhenLegacyUsersFileProvided() throws Exception {
    when(configurationContext.getProperty(Mockito.eq(FileAuthorizer.PROP_LEGACY_AUTHORIZED_USERS_FILE))).thenReturn(new StandardPropertyValue("src/test/resources/authorized-users.xml", null));
    writeFile(primaryAuthorizations, EMPTY_AUTHORIZATIONS_CONCISE);
    writeFile(primaryTenants, EMPTY_TENANTS_CONCISE);
    authorizer.onConfigured(configurationContext);
    // verify all users got created correctly
    final Set<User> users = authorizer.getUsers();
    assertEquals(6, users.size());
    final User user1 = authorizer.getUserByIdentity("user1");
    assertNotNull(user1);
    final User user2 = authorizer.getUserByIdentity("user2");
    assertNotNull(user2);
    final User user3 = authorizer.getUserByIdentity("user3");
    assertNotNull(user3);
    final User user4 = authorizer.getUserByIdentity("user4");
    assertNotNull(user4);
    final User user5 = authorizer.getUserByIdentity("user5");
    assertNotNull(user5);
    final User user6 = authorizer.getUserByIdentity("user6");
    assertNotNull(user6);
    // verify one group got created
    final Set<Group> groups = authorizer.getGroups();
    assertEquals(1, groups.size());
    final Group group1 = groups.iterator().next();
    assertEquals("group1", group1.getName());
    // verify more than one policy got created
    final Set<AccessPolicy> policies = authorizer.getAccessPolicies();
    assertTrue(policies.size() > 0);
    // verify user1's policies
    final Map<String, Set<RequestAction>> user1Policies = getResourceActions(policies, user1);
    assertEquals(4, user1Policies.size());
    assertTrue(user1Policies.containsKey(ResourceType.Flow.getValue()));
    assertEquals(1, user1Policies.get(ResourceType.Flow.getValue()).size());
    assertTrue(user1Policies.get(ResourceType.Flow.getValue()).contains(RequestAction.READ));
    assertTrue(user1Policies.containsKey(ResourceType.ProcessGroup.getValue() + "/" + ROOT_GROUP_ID));
    assertEquals(1, user1Policies.get(ResourceType.ProcessGroup.getValue() + "/" + ROOT_GROUP_ID).size());
    assertTrue(user1Policies.get(ResourceType.ProcessGroup.getValue() + "/" + ROOT_GROUP_ID).contains(RequestAction.READ));
    // verify user2's policies
    final Map<String, Set<RequestAction>> user2Policies = getResourceActions(policies, user2);
    assertEquals(2, user2Policies.size());
    assertTrue(user2Policies.containsKey(ResourceType.Provenance.getValue()));
    assertEquals(1, user2Policies.get(ResourceType.Provenance.getValue()).size());
    assertTrue(user2Policies.get(ResourceType.Provenance.getValue()).contains(RequestAction.READ));
    // verify user3's policies
    final Map<String, Set<RequestAction>> user3Policies = getResourceActions(policies, user3);
    assertEquals(6, user3Policies.size());
    assertTrue(user3Policies.containsKey(ResourceType.Flow.getValue()));
    assertEquals(1, user3Policies.get(ResourceType.Flow.getValue()).size());
    assertTrue(user3Policies.get(ResourceType.Flow.getValue()).contains(RequestAction.READ));
    assertTrue(user3Policies.containsKey(ResourceType.ProcessGroup.getValue() + "/" + ROOT_GROUP_ID));
    assertEquals(2, user3Policies.get(ResourceType.ProcessGroup.getValue() + "/" + ROOT_GROUP_ID).size());
    assertTrue(user3Policies.get(ResourceType.ProcessGroup.getValue() + "/" + ROOT_GROUP_ID).contains(RequestAction.WRITE));
    // verify user4's policies
    final Map<String, Set<RequestAction>> user4Policies = getResourceActions(policies, user4);
    assertEquals(6, user4Policies.size());
    assertTrue(user4Policies.containsKey(ResourceType.Flow.getValue()));
    assertEquals(1, user4Policies.get(ResourceType.Flow.getValue()).size());
    assertTrue(user4Policies.get(ResourceType.Flow.getValue()).contains(RequestAction.READ));
    assertTrue(user4Policies.containsKey(ResourceType.ProcessGroup.getValue() + "/" + ROOT_GROUP_ID));
    assertEquals(1, user4Policies.get(ResourceType.ProcessGroup.getValue() + "/" + ROOT_GROUP_ID).size());
    assertTrue(user4Policies.get(ResourceType.ProcessGroup.getValue() + "/" + ROOT_GROUP_ID).contains(RequestAction.READ));
    assertTrue(user4Policies.containsKey(ResourceType.Tenant.getValue()));
    assertEquals(2, user4Policies.get(ResourceType.Tenant.getValue()).size());
    assertTrue(user4Policies.get(ResourceType.Tenant.getValue()).contains(RequestAction.WRITE));
    assertTrue(user4Policies.containsKey(ResourceType.Policy.getValue()));
    assertEquals(2, user4Policies.get(ResourceType.Policy.getValue()).size());
    assertTrue(user4Policies.get(ResourceType.Policy.getValue()).contains(RequestAction.WRITE));
    // verify user5's policies
    final Map<String, Set<RequestAction>> user5Policies = getResourceActions(policies, user5);
    assertEquals(2, user5Policies.size());
    assertTrue(user5Policies.containsKey(ResourceType.Proxy.getValue()));
    assertEquals(1, user5Policies.get(ResourceType.Proxy.getValue()).size());
    assertTrue(user5Policies.get(ResourceType.Proxy.getValue()).contains(RequestAction.WRITE));
    // verify user6's policies
    final Map<String, Set<RequestAction>> user6Policies = getResourceActions(policies, user6);
    assertEquals(3, user6Policies.size());
    assertTrue(user6Policies.containsKey(ResourceType.SiteToSite.getValue()));
    assertEquals(1, user6Policies.get(ResourceType.SiteToSite.getValue()).size());
    assertTrue(user6Policies.get(ResourceType.SiteToSite.getValue()).contains(RequestAction.READ));
    final Resource inputPortResource = ResourceFactory.getDataTransferResource(ResourceFactory.getComponentResource(ResourceType.InputPort, "2f7d1606-b090-4be7-a592-a5b70fb55531", "TCP Input"));
    final AccessPolicy inputPortPolicy = authorizer.getUsersAndAccessPolicies().getAccessPolicy(inputPortResource.getIdentifier(), RequestAction.WRITE);
    assertNotNull(inputPortPolicy);
    assertEquals(1, inputPortPolicy.getUsers().size());
    assertTrue(inputPortPolicy.getUsers().contains(user6.getIdentifier()));
    assertEquals(1, inputPortPolicy.getGroups().size());
    assertTrue(inputPortPolicy.getGroups().contains(group1.getIdentifier()));
    final Resource outputPortResource = ResourceFactory.getDataTransferResource(ResourceFactory.getComponentResource(ResourceType.OutputPort, "2f7d1606-b090-4be7-a592-a5b70fb55532", "TCP Output"));
    final AccessPolicy outputPortPolicy = authorizer.getUsersAndAccessPolicies().getAccessPolicy(outputPortResource.getIdentifier(), RequestAction.WRITE);
    assertNotNull(outputPortPolicy);
    assertEquals(1, outputPortPolicy.getUsers().size());
    assertTrue(outputPortPolicy.getUsers().contains(user4.getIdentifier()));
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 57 with StandardPropertyValue

use of org.apache.nifi.attribute.expression.language.StandardPropertyValue in project nifi by apache.

the class FileAuthorizerTest method testOnConfiguredWhenInitialAdminProvidedAndNoFlowExists.

@Test
public void testOnConfiguredWhenInitialAdminProvidedAndNoFlowExists() throws Exception {
    // setup NiFi properties to return a file that does not exist
    properties = mock(NiFiProperties.class);
    when(properties.getRestoreDirectory()).thenReturn(restoreAuthorizations.getParentFile());
    when(properties.getFlowConfigurationFile()).thenReturn(new File("src/test/resources/does-not-exist.xml.gz"));
    authorizer.setNiFiProperties(properties);
    final String adminIdentity = "admin-user";
    when(configurationContext.getProperty(Mockito.eq(FileAccessPolicyProvider.PROP_INITIAL_ADMIN_IDENTITY))).thenReturn(new StandardPropertyValue(adminIdentity, null));
    writeFile(primaryAuthorizations, EMPTY_AUTHORIZATIONS_CONCISE);
    writeFile(primaryTenants, EMPTY_TENANTS_CONCISE);
    authorizer.onConfigured(configurationContext);
    final Set<User> users = authorizer.getUsers();
    assertEquals(1, users.size());
    final User adminUser = users.iterator().next();
    assertEquals(adminIdentity, adminUser.getIdentity());
    final Set<AccessPolicy> policies = authorizer.getAccessPolicies();
    assertEquals(8, policies.size());
    final String rootGroupResource = ResourceType.ProcessGroup.getValue() + "/" + ROOT_GROUP_ID;
    boolean foundRootGroupPolicy = false;
    for (AccessPolicy policy : policies) {
        if (policy.getResource().equals(rootGroupResource)) {
            foundRootGroupPolicy = true;
            break;
        }
    }
    assertFalse(foundRootGroupPolicy);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Matchers.anyString(org.mockito.Matchers.anyString) File(java.io.File) Test(org.junit.Test)

Example 58 with StandardPropertyValue

use of org.apache.nifi.attribute.expression.language.StandardPropertyValue in project nifi by apache.

the class FileAuthorizerTest method testOnConfiguredWhenInitialAdminProvidedAndFlowIsNull.

@Test
public void testOnConfiguredWhenInitialAdminProvidedAndFlowIsNull() throws Exception {
    // setup NiFi properties to return a file that does not exist
    properties = mock(NiFiProperties.class);
    when(properties.getRestoreDirectory()).thenReturn(restoreAuthorizations.getParentFile());
    when(properties.getFlowConfigurationFile()).thenReturn(null);
    authorizer.setNiFiProperties(properties);
    final String adminIdentity = "admin-user";
    when(configurationContext.getProperty(Mockito.eq(FileAccessPolicyProvider.PROP_INITIAL_ADMIN_IDENTITY))).thenReturn(new StandardPropertyValue(adminIdentity, null));
    writeFile(primaryAuthorizations, EMPTY_AUTHORIZATIONS_CONCISE);
    writeFile(primaryTenants, EMPTY_TENANTS_CONCISE);
    authorizer.onConfigured(configurationContext);
    final Set<User> users = authorizer.getUsers();
    assertEquals(1, users.size());
    final User adminUser = users.iterator().next();
    assertEquals(adminIdentity, adminUser.getIdentity());
    final Set<AccessPolicy> policies = authorizer.getAccessPolicies();
    assertEquals(8, policies.size());
    final String rootGroupResource = ResourceType.ProcessGroup.getValue() + "/" + ROOT_GROUP_ID;
    boolean foundRootGroupPolicy = false;
    for (AccessPolicy policy : policies) {
        if (policy.getResource().equals(rootGroupResource)) {
            foundRootGroupPolicy = true;
            break;
        }
    }
    assertFalse(foundRootGroupPolicy);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 59 with StandardPropertyValue

use of org.apache.nifi.attribute.expression.language.StandardPropertyValue in project nifi by apache.

the class FileAuthorizerTest method testOnConfiguredWhenNodeIdentitiesProvided.

@Test
public void testOnConfiguredWhenNodeIdentitiesProvided() throws Exception {
    final String adminIdentity = "admin-user";
    final String nodeIdentity1 = "node1";
    final String nodeIdentity2 = "node2";
    when(configurationContext.getProperty(Mockito.eq(FileAccessPolicyProvider.PROP_INITIAL_ADMIN_IDENTITY))).thenReturn(new StandardPropertyValue(adminIdentity, null));
    when(configurationContext.getProperty(Mockito.eq(FileAccessPolicyProvider.PROP_NODE_IDENTITY_PREFIX + "1"))).thenReturn(new StandardPropertyValue(nodeIdentity1, null));
    when(configurationContext.getProperty(Mockito.eq(FileAccessPolicyProvider.PROP_NODE_IDENTITY_PREFIX + "2"))).thenReturn(new StandardPropertyValue(nodeIdentity2, null));
    writeFile(primaryAuthorizations, EMPTY_AUTHORIZATIONS_CONCISE);
    writeFile(primaryTenants, EMPTY_TENANTS_CONCISE);
    authorizer.onConfigured(configurationContext);
    User adminUser = authorizer.getUserByIdentity(adminIdentity);
    assertNotNull(adminUser);
    User nodeUser1 = authorizer.getUserByIdentity(nodeIdentity1);
    assertNotNull(nodeUser1);
    User nodeUser2 = authorizer.getUserByIdentity(nodeIdentity2);
    assertNotNull(nodeUser2);
    AccessPolicy proxyWritePolicy = authorizer.getUsersAndAccessPolicies().getAccessPolicy(ResourceType.Proxy.getValue(), RequestAction.WRITE);
    assertNotNull(proxyWritePolicy);
    assertTrue(proxyWritePolicy.getUsers().contains(nodeUser1.getIdentifier()));
    assertTrue(proxyWritePolicy.getUsers().contains(nodeUser2.getIdentifier()));
}
Also used : StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 60 with StandardPropertyValue

use of org.apache.nifi.attribute.expression.language.StandardPropertyValue in project nifi by apache.

the class FileAuthorizerTest method testOnConfiguredWhenInitialAdminProvidedWithIdentityMapping.

@Test
public void testOnConfiguredWhenInitialAdminProvidedWithIdentityMapping() throws Exception {
    final Properties props = new Properties();
    props.setProperty("nifi.security.identity.mapping.pattern.dn1", "^CN=(.*?), OU=(.*?), O=(.*?), L=(.*?), ST=(.*?), C=(.*?)$");
    props.setProperty("nifi.security.identity.mapping.value.dn1", "$1_$2_$3");
    properties = getNiFiProperties(props);
    when(properties.getRestoreDirectory()).thenReturn(restoreAuthorizations.getParentFile());
    when(properties.getFlowConfigurationFile()).thenReturn(flow);
    authorizer.setNiFiProperties(properties);
    final String adminIdentity = "CN=localhost, OU=Apache NiFi, O=Apache, L=Santa Monica, ST=CA, C=US";
    when(configurationContext.getProperty(Mockito.eq(FileAccessPolicyProvider.PROP_INITIAL_ADMIN_IDENTITY))).thenReturn(new StandardPropertyValue(adminIdentity, null));
    writeFile(primaryAuthorizations, EMPTY_AUTHORIZATIONS_CONCISE);
    writeFile(primaryTenants, EMPTY_TENANTS_CONCISE);
    authorizer.onConfigured(configurationContext);
    final Set<User> users = authorizer.getUsers();
    assertEquals(1, users.size());
    final User adminUser = users.iterator().next();
    assertEquals("localhost_Apache NiFi_Apache", adminUser.getIdentity());
}
Also used : StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Matchers.anyString(org.mockito.Matchers.anyString) Properties(java.util.Properties) NiFiProperties(org.apache.nifi.util.NiFiProperties) Test(org.junit.Test)

Aggregations

StandardPropertyValue (org.apache.nifi.attribute.expression.language.StandardPropertyValue)91 Test (org.junit.Test)78 AuthorizerConfigurationContext (org.apache.nifi.authorization.AuthorizerConfigurationContext)33 Matchers.anyString (org.mockito.Matchers.anyString)30 NiFiProperties (org.apache.nifi.util.NiFiProperties)24 PropertyValue (org.apache.nifi.components.PropertyValue)17 HashMap (java.util.HashMap)16 Properties (java.util.Properties)15 Group (org.apache.nifi.authorization.Group)12 Before (org.junit.Before)12 Set (java.util.Set)10 UserAndGroups (org.apache.nifi.authorization.UserAndGroups)10 AuthorizerCreationException (org.apache.nifi.authorization.exception.AuthorizerCreationException)9 CreateLdapServer (org.apache.directory.server.annotations.CreateLdapServer)8 CreateTransport (org.apache.directory.server.annotations.CreateTransport)8 ApplyLdifFiles (org.apache.directory.server.core.annotations.ApplyLdifFiles)8 CreateDS (org.apache.directory.server.core.annotations.CreateDS)8 CreatePartition (org.apache.directory.server.core.annotations.CreatePartition)8 AbstractLdapTestUnit (org.apache.directory.server.core.integ.AbstractLdapTestUnit)8 FrameworkRunner (org.apache.directory.server.core.integ.FrameworkRunner)8