Search in sources :

Example 51 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class TestSchemaNamePropertyStrategy method testNameAndBlankVersion.

@Test
public void testNameAndBlankVersion() throws SchemaNotFoundException, IOException {
    final PropertyValue nameValue = new MockPropertyValue("person");
    final PropertyValue branchValue = new MockPropertyValue(null);
    final PropertyValue versionValue = new MockPropertyValue("   ");
    final SchemaNamePropertyStrategy schemaNamePropertyStrategy = new SchemaNamePropertyStrategy(schemaRegistry, nameValue, branchValue, versionValue);
    final SchemaIdentifier expectedSchemaIdentifier = SchemaIdentifier.builder().name(nameValue.getValue()).build();
    when(schemaRegistry.retrieveSchema(argThat(new SchemaIdentifierMatcher(expectedSchemaIdentifier)))).thenReturn(recordSchema);
    final RecordSchema retrievedSchema = schemaNamePropertyStrategy.getSchema(Collections.emptyMap(), null, recordSchema);
    assertNotNull(retrievedSchema);
}
Also used : MockPropertyValue(org.apache.nifi.util.MockPropertyValue) PropertyValue(org.apache.nifi.components.PropertyValue) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 52 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class TestSchemaNamePropertyStrategy method testNameAndNonNumericVersion.

@Test(expected = SchemaNotFoundException.class)
public void testNameAndNonNumericVersion() throws SchemaNotFoundException, IOException {
    final PropertyValue nameValue = new MockPropertyValue("person");
    final PropertyValue branchValue = new MockPropertyValue(null);
    final PropertyValue versionValue = new MockPropertyValue("XYZ");
    final SchemaNamePropertyStrategy schemaNamePropertyStrategy = new SchemaNamePropertyStrategy(schemaRegistry, nameValue, branchValue, versionValue);
    schemaNamePropertyStrategy.getSchema(Collections.emptyMap(), null, recordSchema);
}
Also used : MockPropertyValue(org.apache.nifi.util.MockPropertyValue) PropertyValue(org.apache.nifi.components.PropertyValue) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) Test(org.junit.Test)

Example 53 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class FileAccessPolicyProvider method onConfigured.

@Override
public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
    try {
        final PropertyValue userGroupProviderIdentifier = configurationContext.getProperty(PROP_USER_GROUP_PROVIDER);
        if (!userGroupProviderIdentifier.isSet()) {
            throw new AuthorizerCreationException("The user group provider must be specified.");
        }
        userGroupProvider = userGroupProviderLookup.getUserGroupProvider(userGroupProviderIdentifier.getValue());
        if (userGroupProvider == null) {
            throw new AuthorizerCreationException("Unable to locate user group provider with identifier " + userGroupProviderIdentifier.getValue());
        }
        final PropertyValue authorizationsPath = configurationContext.getProperty(PROP_AUTHORIZATIONS_FILE);
        if (StringUtils.isBlank(authorizationsPath.getValue())) {
            throw new AuthorizerCreationException("The authorizations file must be specified.");
        }
        // get the authorizations file and ensure it exists
        authorizationsFile = new File(authorizationsPath.getValue());
        if (!authorizationsFile.exists()) {
            logger.info("Creating new authorizations file at {}", new Object[] { authorizationsFile.getAbsolutePath() });
            saveAuthorizations(new Authorizations());
        }
        final File authorizationsFileDirectory = authorizationsFile.getAbsoluteFile().getParentFile();
        // the restore directory is optional and may be null
        final File restoreDirectory = properties.getRestoreDirectory();
        if (restoreDirectory != null) {
            // sanity check that restore directory is a directory, creating it if necessary
            FileUtils.ensureDirectoryExistAndCanAccess(restoreDirectory);
            // check that restore directory is not the same as the authorizations directory
            if (authorizationsFileDirectory.getAbsolutePath().equals(restoreDirectory.getAbsolutePath())) {
                throw new AuthorizerCreationException(String.format("Authorizations file directory '%s' is the same as restore directory '%s' ", authorizationsFileDirectory.getAbsolutePath(), restoreDirectory.getAbsolutePath()));
            }
            // the restore copy will have same file name, but reside in a different directory
            restoreAuthorizationsFile = new File(restoreDirectory, authorizationsFile.getName());
            try {
                // sync the primary copy with the restore copy
                FileUtils.syncWithRestore(authorizationsFile, restoreAuthorizationsFile, logger);
            } catch (final IOException | IllegalStateException ioe) {
                throw new AuthorizerCreationException(ioe);
            }
        }
        // extract the identity mappings from nifi.properties if any are provided
        identityMappings = Collections.unmodifiableList(IdentityMappingUtil.getIdentityMappings(properties));
        // get the value of the initial admin identity
        final PropertyValue initialAdminIdentityProp = configurationContext.getProperty(PROP_INITIAL_ADMIN_IDENTITY);
        initialAdminIdentity = initialAdminIdentityProp.isSet() ? IdentityMappingUtil.mapIdentity(initialAdminIdentityProp.getValue(), identityMappings) : null;
        // get the value of the legacy authorized users file
        final PropertyValue legacyAuthorizedUsersProp = configurationContext.getProperty(FileAuthorizer.PROP_LEGACY_AUTHORIZED_USERS_FILE);
        legacyAuthorizedUsersFile = legacyAuthorizedUsersProp.isSet() ? legacyAuthorizedUsersProp.getValue() : null;
        // extract any node identities
        nodeIdentities = new HashSet<>();
        for (Map.Entry<String, String> entry : configurationContext.getProperties().entrySet()) {
            Matcher matcher = NODE_IDENTITY_PATTERN.matcher(entry.getKey());
            if (matcher.matches() && !StringUtils.isBlank(entry.getValue())) {
                nodeIdentities.add(IdentityMappingUtil.mapIdentity(entry.getValue(), identityMappings));
            }
        }
        // load the authorizations
        load();
        // if we've copied the authorizations file to a restore directory synchronize it
        if (restoreAuthorizationsFile != null) {
            FileUtils.copyFile(authorizationsFile, restoreAuthorizationsFile, false, false, logger);
        }
        logger.info(String.format("Authorizations file loaded at %s", new Date().toString()));
    } catch (IOException | AuthorizerCreationException | JAXBException | IllegalStateException | SAXException e) {
        throw new AuthorizerCreationException(e);
    }
}
Also used : Authorizations(org.apache.nifi.authorization.file.generated.Authorizations) Matcher(java.util.regex.Matcher) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) JAXBException(javax.xml.bind.JAXBException) PropertyValue(org.apache.nifi.components.PropertyValue) IOException(java.io.IOException) Date(java.util.Date) SAXException(org.xml.sax.SAXException) File(java.io.File) Map(java.util.Map)

Example 54 with PropertyValue

use of org.apache.nifi.components.PropertyValue in project nifi by apache.

the class FileAccessPolicyProviderTest 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);
    userGroupProvider = new FileUserGroupProvider();
    userGroupProvider.setNiFiProperties(properties);
    userGroupProvider.initialize(null);
    // this same configuration is being used for both the user group provider and the access policy provider
    configurationContext = mock(AuthorizerConfigurationContext.class);
    when(configurationContext.getProperty(eq(FileAccessPolicyProvider.PROP_AUTHORIZATIONS_FILE))).thenReturn(new StandardPropertyValue(primaryAuthorizations.getPath(), null));
    when(configurationContext.getProperty(eq(FileUserGroupProvider.PROP_TENANTS_FILE))).thenReturn(new StandardPropertyValue(primaryTenants.getPath(), null));
    when(configurationContext.getProperty(eq(FileAccessPolicyProvider.PROP_INITIAL_ADMIN_IDENTITY))).thenReturn(new StandardPropertyValue(null, null));
    when(configurationContext.getProperty(eq(FileAuthorizer.PROP_LEGACY_AUTHORIZED_USERS_FILE))).thenReturn(new StandardPropertyValue(null, null));
    when(configurationContext.getProperty(eq(FileAccessPolicyProvider.PROP_USER_GROUP_PROVIDER))).thenReturn(new StandardPropertyValue("user-group-provider", 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());
            }
        }
        i = 1;
        while (true) {
            final String key = FileUserGroupProvider.PROP_INITIAL_USER_IDENTITY_PREFIX + i++;
            final PropertyValue value = configurationContext.getProperty(key);
            if (value == null) {
                break;
            } else {
                properties.put(key, value.getValue());
            }
        }
        // ensure the initial admin is seeded into the user provider if appropriate
        if (properties.containsKey(FileAccessPolicyProvider.PROP_INITIAL_ADMIN_IDENTITY)) {
            i = 0;
            while (true) {
                final String key = FileUserGroupProvider.PROP_INITIAL_USER_IDENTITY_PREFIX + i++;
                if (!properties.containsKey(key)) {
                    properties.put(key, properties.get(FileAccessPolicyProvider.PROP_INITIAL_ADMIN_IDENTITY));
                    break;
                }
            }
        }
        return properties;
    });
    final AccessPolicyProviderInitializationContext initializationContext = mock(AccessPolicyProviderInitializationContext.class);
    when(initializationContext.getUserGroupProviderLookup()).thenReturn(new UserGroupProviderLookup() {

        @Override
        public UserGroupProvider getUserGroupProvider(String identifier) {
            return userGroupProvider;
        }
    });
    accessPolicyProvider = new FileAccessPolicyProvider();
    accessPolicyProvider.setNiFiProperties(properties);
    accessPolicyProvider.initialize(initializationContext);
}
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 55 with PropertyValue

use of org.apache.nifi.components.PropertyValue 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)

Aggregations

PropertyValue (org.apache.nifi.components.PropertyValue)73 HashMap (java.util.HashMap)29 Test (org.junit.Test)22 StandardPropertyValue (org.apache.nifi.attribute.expression.language.StandardPropertyValue)21 ComponentLog (org.apache.nifi.logging.ComponentLog)18 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)16 IOException (java.io.IOException)15 Map (java.util.Map)13 FlowFile (org.apache.nifi.flowfile.FlowFile)11 ProcessException (org.apache.nifi.processor.exception.ProcessException)11 MockPropertyValue (org.apache.nifi.util.MockPropertyValue)11 ArrayList (java.util.ArrayList)9 SSLContext (javax.net.ssl.SSLContext)7 Relationship (org.apache.nifi.processor.Relationship)7 AuthorizerCreationException (org.apache.nifi.authorization.exception.AuthorizerCreationException)6 File (java.io.File)5 DynamicRelationship (org.apache.nifi.annotation.behavior.DynamicRelationship)5 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)5 SchemaIdentifier (org.apache.nifi.serialization.record.SchemaIdentifier)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5