Search in sources :

Example 31 with PropertyValue

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

the class TestSchemaNamePropertyStrategy method testNameAndBranch.

@Test
public void testNameAndBranch() throws SchemaNotFoundException, IOException {
    final PropertyValue nameValue = new MockPropertyValue("person");
    final PropertyValue branchValue = new MockPropertyValue("test");
    final PropertyValue versionValue = new MockPropertyValue(null);
    final SchemaNamePropertyStrategy schemaNamePropertyStrategy = new SchemaNamePropertyStrategy(schemaRegistry, nameValue, branchValue, versionValue);
    final SchemaIdentifier expectedSchemaIdentifier = SchemaIdentifier.builder().name(nameValue.getValue()).branch(branchValue.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 32 with PropertyValue

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

the class TestSchemaNamePropertyStrategy method testNameOnly.

@Test
public void testNameOnly() throws SchemaNotFoundException, IOException {
    final PropertyValue nameValue = new MockPropertyValue("person");
    final PropertyValue branchValue = new MockPropertyValue(null);
    final PropertyValue versionValue = new MockPropertyValue(null);
    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 33 with PropertyValue

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

the class TestSchemaNamePropertyStrategy method testNameAndBlankBranch.

@Test
public void testNameAndBlankBranch() throws SchemaNotFoundException, IOException {
    final PropertyValue nameValue = new MockPropertyValue("person");
    final PropertyValue branchValue = new MockPropertyValue("  ");
    final PropertyValue versionValue = new MockPropertyValue(null);
    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 34 with PropertyValue

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

the class StandardStateManagerProvider method createStateProvider.

private static StateProvider createStateProvider(final File configFile, final Scope scope, final NiFiProperties properties, final VariableRegistry variableRegistry) throws ConfigParseException, IOException {
    final String providerId;
    final String providerIdPropertyName;
    final String providerDescription;
    final String providerXmlElementName;
    final String oppositeScopeXmlElementName;
    switch(scope) {
        case CLUSTER:
            providerId = properties.getClusterStateProviderId();
            providerIdPropertyName = NiFiProperties.STATE_MANAGEMENT_CLUSTER_PROVIDER_ID;
            providerDescription = "Cluster State Provider";
            providerXmlElementName = "cluster-provider";
            oppositeScopeXmlElementName = "local-provider";
            break;
        case LOCAL:
            providerId = properties.getLocalStateProviderId();
            providerIdPropertyName = NiFiProperties.STATE_MANAGEMENT_LOCAL_PROVIDER_ID;
            providerDescription = "Local State Provider";
            providerXmlElementName = "local-provider";
            oppositeScopeXmlElementName = "cluster-provider";
            break;
        default:
            throw new AssertionError("Attempted to create State Provider for unknown Scope: " + scope);
    }
    if (!configFile.exists()) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the State Management Configuration File " + configFile + " does not exist");
    }
    if (!configFile.canRead()) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the State Management Configuration File " + configFile + " cannot be read");
    }
    if (providerId == null) {
        if (scope == Scope.CLUSTER) {
            throw new IllegalStateException("Cannot create Cluster State Provider because the '" + providerIdPropertyName + "' property is missing from the NiFi Properties file. In order to run NiFi in a cluster, the " + providerIdPropertyName + " property must be configured in nifi.properties");
        }
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property is missing from the NiFi Properties file");
    }
    if (providerId.trim().isEmpty()) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property in the NiFi Properties file has no value set. This is a required property and must reference the identifier of one of the " + providerXmlElementName + " elements in the State Management Configuration File (" + configFile + ")");
    }
    final StateManagerConfiguration config = StateManagerConfiguration.parse(configFile);
    final StateProviderConfiguration providerConfig = config.getStateProviderConfiguration(providerId);
    if (providerConfig == null) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property in the NiFi Properties file is set to '" + providerId + "', but there is no " + providerXmlElementName + " entry in the State Management Configuration File (" + configFile + ") with this id");
    }
    if (providerConfig.getScope() != scope) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property in the NiFi Properties file is set to '" + providerId + "', but this id is assigned to a " + oppositeScopeXmlElementName + " entry in the State Management Configuration File (" + configFile + "), rather than a " + providerXmlElementName + " entry");
    }
    final String providerClassName = providerConfig.getClassName();
    final StateProvider provider;
    try {
        provider = instantiateStateProvider(providerClassName);
    } catch (final Exception e) {
        throw new RuntimeException("Cannot create " + providerDescription + " of type " + providerClassName, e);
    }
    if (!ArrayUtils.contains(provider.getSupportedScopes(), scope)) {
        throw new RuntimeException("Cannot use " + providerDescription + " (" + providerClassName + ") as it only supports scope(s) " + ArrayUtils.toString(provider.getSupportedScopes()) + " but " + "instance" + " is configured to use scope " + scope);
    }
    // create variable registry
    final Map<PropertyDescriptor, PropertyValue> propertyMap = new HashMap<>();
    final Map<PropertyDescriptor, String> propertyStringMap = new HashMap<>();
    for (final PropertyDescriptor descriptor : provider.getPropertyDescriptors()) {
        propertyMap.put(descriptor, new StandardPropertyValue(descriptor.getDefaultValue(), null, variableRegistry));
        propertyStringMap.put(descriptor, descriptor.getDefaultValue());
    }
    for (final Map.Entry<String, String> entry : providerConfig.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = provider.getPropertyDescriptor(entry.getKey());
        propertyStringMap.put(descriptor, entry.getValue());
        propertyMap.put(descriptor, new StandardPropertyValue(entry.getValue(), null, variableRegistry));
    }
    final SSLContext sslContext = SslContextFactory.createSslContext(properties, false);
    final ComponentLog logger = new SimpleProcessLogger(providerId, provider);
    final StateProviderInitializationContext initContext = new StandardStateProviderInitializationContext(providerId, propertyMap, sslContext, logger);
    synchronized (provider) {
        provider.initialize(initContext);
    }
    final ValidationContext validationContext = new StandardValidationContext(null, propertyStringMap, null, null, null, variableRegistry);
    final Collection<ValidationResult> results = provider.validate(validationContext);
    final StringBuilder validationFailures = new StringBuilder();
    int invalidCount = 0;
    for (final ValidationResult result : results) {
        if (!result.isValid()) {
            validationFailures.append(result.toString()).append("\n");
            invalidCount++;
        }
    }
    if (invalidCount > 0) {
        throw new IllegalStateException("Could not initialize State Providers because the " + providerDescription + " is not valid. The following " + invalidCount + " Validation Errors occurred:\n" + validationFailures.toString() + "\nPlease check the configuration of the " + providerDescription + " with ID [" + providerId.trim() + "] in the file " + configFile.getAbsolutePath());
    }
    return provider;
}
Also used : StandardStateProviderInitializationContext(org.apache.nifi.controller.state.StandardStateProviderInitializationContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) ValidationResult(org.apache.nifi.components.ValidationResult) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger) StateProviderInitializationContext(org.apache.nifi.components.state.StateProviderInitializationContext) StandardStateProviderInitializationContext(org.apache.nifi.controller.state.StandardStateProviderInitializationContext) StandardValidationContext(org.apache.nifi.processor.StandardValidationContext) StateManagerConfiguration(org.apache.nifi.controller.state.config.StateManagerConfiguration) StateProviderConfiguration(org.apache.nifi.controller.state.config.StateProviderConfiguration) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) StateProvider(org.apache.nifi.components.state.StateProvider) PropertyValue(org.apache.nifi.components.PropertyValue) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) SSLContext(javax.net.ssl.SSLContext) ComponentLog(org.apache.nifi.logging.ComponentLog) IOException(java.io.IOException) ConfigParseException(org.apache.nifi.controller.state.ConfigParseException) ValidationContext(org.apache.nifi.components.ValidationContext) StandardValidationContext(org.apache.nifi.processor.StandardValidationContext) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StateMap(org.apache.nifi.components.state.StateMap)

Example 35 with PropertyValue

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

the class FileUserGroupProvider method onConfigured.

@Override
public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
    try {
        final PropertyValue tenantsPath = configurationContext.getProperty(PROP_TENANTS_FILE);
        if (StringUtils.isBlank(tenantsPath.getValue())) {
            throw new AuthorizerCreationException("The users file must be specified.");
        }
        // get the tenants file and ensure it exists
        tenantsFile = new File(tenantsPath.getValue());
        if (!tenantsFile.exists()) {
            logger.info("Creating new users file at {}", new Object[] { tenantsFile.getAbsolutePath() });
            saveTenants(new Tenants());
        }
        final File tenantsFileDirectory = tenantsFile.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 user's directory
            if (tenantsFileDirectory.getAbsolutePath().equals(restoreDirectory.getAbsolutePath())) {
                throw new AuthorizerCreationException(String.format("Users file directory '%s' is the same as restore directory '%s' ", tenantsFileDirectory.getAbsolutePath(), restoreDirectory.getAbsolutePath()));
            }
            // the restore copy will have same file name, but reside in a different directory
            restoreTenantsFile = new File(restoreDirectory, tenantsFile.getName());
            try {
                // sync the primary copy with the restore copy
                FileUtils.syncWithRestore(tenantsFile, restoreTenantsFile, 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 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
        initialUserIdentities = new HashSet<>();
        for (Map.Entry<String, String> entry : configurationContext.getProperties().entrySet()) {
            Matcher matcher = INITIAL_USER_IDENTITY_PATTERN.matcher(entry.getKey());
            if (matcher.matches() && !StringUtils.isBlank(entry.getValue())) {
                initialUserIdentities.add(IdentityMappingUtil.mapIdentity(entry.getValue(), identityMappings));
            }
        }
        load();
        // if we've copied the authorizations file to a restore directory synchronize it
        if (restoreTenantsFile != null) {
            FileUtils.copyFile(tenantsFile, restoreTenantsFile, false, false, logger);
        }
        logger.info(String.format("Users/Groups file loaded at %s", new Date().toString()));
    } catch (IOException | AuthorizerCreationException | JAXBException | IllegalStateException | SAXException e) {
        throw new AuthorizerCreationException(e);
    }
}
Also used : Matcher(java.util.regex.Matcher) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) JAXBException(javax.xml.bind.JAXBException) PropertyValue(org.apache.nifi.components.PropertyValue) Tenants(org.apache.nifi.authorization.file.tenants.generated.Tenants) IOException(java.io.IOException) Date(java.util.Date) SAXException(org.xml.sax.SAXException) File(java.io.File) Map(java.util.Map)

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