Search in sources :

Example 61 with StandardPropertyValue

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

the class FileAuthorizerTest method setup.

@Before
public void setup() throws IOException {
    // primary authorizations
    primaryAuthorizations = new File("target/authorizations/authorizations.xml");
    FileUtils.ensureDirectoryExistAndCanAccess(primaryAuthorizations.getParentFile());
    // primary tenants
    primaryTenants = new File("target/authorizations/users.xml");
    FileUtils.ensureDirectoryExistAndCanAccess(primaryTenants.getParentFile());
    // restore authorizations
    restoreAuthorizations = new File("target/restore/authorizations.xml");
    FileUtils.ensureDirectoryExistAndCanAccess(restoreAuthorizations.getParentFile());
    // restore authorizations
    restoreTenants = new File("target/restore/users.xml");
    FileUtils.ensureDirectoryExistAndCanAccess(restoreTenants.getParentFile());
    flow = new File("src/test/resources/flow.xml.gz");
    FileUtils.ensureDirectoryExistAndCanAccess(flow.getParentFile());
    flowNoPorts = new File("src/test/resources/flow-no-ports.xml.gz");
    FileUtils.ensureDirectoryExistAndCanAccess(flowNoPorts.getParentFile());
    flowWithDns = new File("src/test/resources/flow-with-dns.xml.gz");
    FileUtils.ensureDirectoryExistAndCanAccess(flowWithDns.getParentFile());
    properties = mock(NiFiProperties.class);
    when(properties.getRestoreDirectory()).thenReturn(restoreAuthorizations.getParentFile());
    when(properties.getFlowConfigurationFile()).thenReturn(flow);
    configurationContext = mock(AuthorizerConfigurationContext.class);
    when(configurationContext.getProperty(Mockito.eq(FileAccessPolicyProvider.PROP_AUTHORIZATIONS_FILE))).thenReturn(new StandardPropertyValue(primaryAuthorizations.getPath(), null));
    when(configurationContext.getProperty(Mockito.eq(FileUserGroupProvider.PROP_TENANTS_FILE))).thenReturn(new StandardPropertyValue(primaryTenants.getPath(), null));
    when(configurationContext.getProperties()).then((invocation) -> {
        final Map<String, String> properties = new HashMap<>();
        final PropertyValue authFile = configurationContext.getProperty(FileAccessPolicyProvider.PROP_AUTHORIZATIONS_FILE);
        if (authFile != null) {
            properties.put(FileAccessPolicyProvider.PROP_AUTHORIZATIONS_FILE, authFile.getValue());
        }
        final PropertyValue tenantFile = configurationContext.getProperty(FileUserGroupProvider.PROP_TENANTS_FILE);
        if (tenantFile != null) {
            properties.put(FileUserGroupProvider.PROP_TENANTS_FILE, tenantFile.getValue());
        }
        final PropertyValue legacyAuthFile = configurationContext.getProperty(FileAuthorizer.PROP_LEGACY_AUTHORIZED_USERS_FILE);
        if (legacyAuthFile != null) {
            properties.put(FileAuthorizer.PROP_LEGACY_AUTHORIZED_USERS_FILE, legacyAuthFile.getValue());
        }
        final PropertyValue initialAdmin = configurationContext.getProperty(FileAccessPolicyProvider.PROP_INITIAL_ADMIN_IDENTITY);
        if (initialAdmin != null) {
            properties.put(FileAccessPolicyProvider.PROP_INITIAL_ADMIN_IDENTITY, initialAdmin.getValue());
        }
        int i = 1;
        while (true) {
            final String key = FileAccessPolicyProvider.PROP_NODE_IDENTITY_PREFIX + i++;
            final PropertyValue value = configurationContext.getProperty(key);
            if (value == null) {
                break;
            } else {
                properties.put(key, value.getValue());
            }
        }
        return properties;
    });
    authorizer = new FileAuthorizer();
    authorizer.setNiFiProperties(properties);
    authorizer.initialize(null);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) HashMap(java.util.HashMap) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) PropertyValue(org.apache.nifi.components.PropertyValue) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Matchers.anyString(org.mockito.Matchers.anyString) File(java.io.File) Before(org.junit.Before)

Example 62 with StandardPropertyValue

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

the class FileAuthorizerTest method testOnConfiguredWhenInitialAdminAndLegacyUsersProvided.

@Test(expected = AuthorizerCreationException.class)
public void testOnConfiguredWhenInitialAdminAndLegacyUsersProvided() throws Exception {
    final String adminIdentity = "admin-user";
    when(configurationContext.getProperty(Mockito.eq(FileAccessPolicyProvider.PROP_INITIAL_ADMIN_IDENTITY))).thenReturn(new StandardPropertyValue(adminIdentity, null));
    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);
}
Also used : StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 63 with StandardPropertyValue

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

the class FileAuthorizerTest method testOnConfiguredWhenInitialAdminProvided.

@Test
public void testOnConfiguredWhenInitialAdminProvided() throws Exception {
    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(12, 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;
        }
    }
    assertTrue(foundRootGroupPolicy);
}
Also used : StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 64 with StandardPropertyValue

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

the class FileAuthorizerTest method testOnConfiguredWhenLegacyUsersFileProvidedAndFlowHasNoPorts.

@Test
public void testOnConfiguredWhenLegacyUsersFileProvidedAndFlowHasNoPorts() throws Exception {
    properties = mock(NiFiProperties.class);
    when(properties.getRestoreDirectory()).thenReturn(restoreAuthorizations.getParentFile());
    when(properties.getFlowConfigurationFile()).thenReturn(flowNoPorts);
    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);
    boolean foundDataTransferPolicy = false;
    for (AccessPolicy policy : authorizer.getAccessPolicies()) {
        if (policy.getResource().contains(ResourceType.DataTransfer.name())) {
            foundDataTransferPolicy = true;
            break;
        }
    }
    assertFalse(foundDataTransferPolicy);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Test(org.junit.Test)

Example 65 with StandardPropertyValue

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

the class FileAuthorizerTest method testOnConfiguredWhenNodeIdentitiesProvidedAndUsersAlreadyExist.

@Test
public void testOnConfiguredWhenNodeIdentitiesProvidedAndUsersAlreadyExist() 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, TENANTS_FOR_ADMIN_AND_NODES);
    authorizer.onConfigured(configurationContext);
    assertEquals(3, authorizer.getUsers().size());
    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)

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