Java Code Examples for org.apache.nifi.components.PropertyValue#getValue()

The following examples show how to use org.apache.nifi.components.PropertyValue#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RangerNiFiAuthorizer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a resource to the RangerConfiguration singleton so it is already there by the time RangerBasePlugin.init()
 * is called.
 *
 * @param name the name of the given PropertyValue from the AuthorizationConfigurationContext
 * @param resourceValue the value for the given name, should be a full path to a file
 */
private void addRequiredResource(final String name, final PropertyValue resourceValue) {
    if (resourceValue == null || StringUtils.isBlank(resourceValue.getValue())) {
        throw new AuthorizerCreationException(name + " must be specified.");
    }

    final File resourceFile = new File(resourceValue.getValue());
    if (!resourceFile.exists() || !resourceFile.canRead()) {
        throw new AuthorizerCreationException(resourceValue + " does not exist, or can not be read");
    }

    try {
        RangerConfiguration.getInstance().addResource(resourceFile.toURI().toURL());
    } catch (MalformedURLException e) {
        throw new AuthorizerCreationException("Error creating URI for " + resourceValue, e);
    }
}
 
Example 2
Source File: ShellUserGroupProvider.java    From nifi with Apache License 2.0 6 votes vote down vote up
private long getDelayProperty(AuthorizerConfigurationContext authContext, String propertyName, String defaultValue) {
    final PropertyValue intervalProperty = authContext.getProperty(propertyName);
    final String propertyValue;
    final long syncInterval;

    if (intervalProperty.isSet()) {
        propertyValue = intervalProperty.getValue();
    } else {
        propertyValue = defaultValue;
    }

    try {
        syncInterval = Math.round(FormatUtils.getPreciseTimeDuration(propertyValue, TimeUnit.MILLISECONDS));
    } catch (final IllegalArgumentException ignored) {
        throw new AuthorizerCreationException(String.format("The %s '%s' is not a valid time interval.", propertyName, propertyValue));
    }

    if (syncInterval < MINIMUM_SYNC_INTERVAL_MILLISECONDS) {
        throw new AuthorizerCreationException(String.format("The %s '%s' is below the minimum value of '%d ms'", propertyName, propertyValue, MINIMUM_SYNC_INTERVAL_MILLISECONDS));
    }
    return syncInterval;
}
 
Example 3
Source File: RecordBinManager.java    From nifi with Apache License 2.0 6 votes vote down vote up
private RecordBinThresholds createThresholds(FlowFile flowfile) {
    int minRecords = context.getProperty(MergeRecord.MIN_RECORDS).evaluateAttributeExpressions().asInteger();
    final int maxRecords = context.getProperty(MergeRecord.MAX_RECORDS).evaluateAttributeExpressions().asInteger();
    final long minBytes = context.getProperty(MergeRecord.MIN_SIZE).asDataSize(DataUnit.B).longValue();

    final PropertyValue maxSizeValue = context.getProperty(MergeRecord.MAX_SIZE);
    final long maxBytes = maxSizeValue.isSet() ? maxSizeValue.asDataSize(DataUnit.B).longValue() : Long.MAX_VALUE;

    final PropertyValue maxMillisValue = context.getProperty(MergeRecord.MAX_BIN_AGE);
    final String maxBinAge = maxMillisValue.getValue();
    final long maxBinMillis = maxMillisValue.isSet() ? maxMillisValue.asTimePeriod(TimeUnit.MILLISECONDS) : Long.MAX_VALUE;

    final String fragmentCountAttribute;
    final String mergeStrategy = context.getProperty(MergeRecord.MERGE_STRATEGY).getValue();
    if (MergeRecord.MERGE_STRATEGY_DEFRAGMENT.getValue().equals(mergeStrategy)) {
        fragmentCountAttribute = MergeContent.FRAGMENT_COUNT_ATTRIBUTE;
        // We don't know minRecords in defragment mode.
        minRecords = Integer.MAX_VALUE;
    } else {
        fragmentCountAttribute = null;
    }

    return new RecordBinThresholds(minRecords, maxRecords, minBytes, maxBytes, maxBinMillis, maxBinAge, fragmentCountAttribute);
}
 
Example 4
Source File: ParametersIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSensitivePropertyReferenceParameterSupportsEL() {
    final ProcessorNode usernamePassword = createProcessorNode(UsernamePasswordProcessor.class);

    final ParameterReferenceManager referenceManager = new StandardParameterReferenceManager(getFlowController().getFlowManager());
    final ParameterContext parameterContext = new StandardParameterContext(UUID.randomUUID().toString(), "param-context", referenceManager, null);
    parameterContext.setParameters(Collections.singletonMap("pass", new Parameter(new ParameterDescriptor.Builder().name("pass").sensitive(true).build(), "secret")));

    getRootGroup().setParameterContext(parameterContext);

    final Map<String, String> properties = new HashMap<>();
    properties.put("password", "#{pass}");
    usernamePassword.setProperties(properties);

    final ProcessContext processContext = new StandardProcessContext(usernamePassword, getFlowController().getControllerServiceProvider(), getFlowController().getEncryptor(),
        getFlowController().getStateManagerProvider().getStateManager(usernamePassword.getIdentifier()), () -> false);
    final PropertyDescriptor descriptor = usernamePassword.getPropertyDescriptor("password");
    final PropertyValue propertyValue = processContext.getProperty(descriptor);
    final PropertyValue evaluatedPropertyValue = propertyValue.evaluateAttributeExpressions();
    final String evaluatedPassword = evaluatedPropertyValue.getValue();
    assertEquals("secret", evaluatedPassword);
}
 
Example 5
Source File: RangerNiFiAuthorizer.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a resource to the RangerConfiguration singleton so it is already there by the time RangerBasePlugin.init()
 * is called.
 *
 * @param name the name of the given PropertyValue from the AuthorizationConfigurationContext
 * @param resourceValue the value for the given name, should be a full path to a file
 */
private void addRequiredResource(final String name, final PropertyValue resourceValue) {
    if (resourceValue == null || StringUtils.isBlank(resourceValue.getValue())) {
        throw new AuthorizerCreationException(name + " must be specified.");
    }

    final File resourceFile = new File(resourceValue.getValue());
    if (!resourceFile.exists() || !resourceFile.canRead()) {
        throw new AuthorizerCreationException(resourceValue + " does not exist, or can not be read");
    }

    try {
        RangerConfiguration.getInstance().addResource(resourceFile.toURI().toURL());
    } catch (MalformedURLException e) {
        throw new AuthorizerCreationException("Error creating URI for " + resourceValue, e);
    }
}
 
Example 6
Source File: ShellUserGroupProvider.java    From nifi with Apache License 2.0 6 votes vote down vote up
private int getTimeoutProperty(AuthorizerConfigurationContext authContext, String propertyName, String defaultValue) {
    final PropertyValue timeoutProperty = authContext.getProperty(propertyName);

    final String propertyValue;
    if (timeoutProperty.isSet()) {
        propertyValue = timeoutProperty.getValue();
    } else {
        propertyValue = defaultValue;
    }

    final long timeoutValue;
    try {
        timeoutValue = Math.round(FormatUtils.getPreciseTimeDuration(propertyValue, TimeUnit.SECONDS));
    } catch (final IllegalArgumentException ignored) {
        throw new AuthorizerCreationException(String.format("The %s '%s' is not a valid time interval.", propertyName, propertyValue));
    }

    return Math.toIntExact(timeoutValue);
}
 
Example 7
Source File: ShellUserGroupProvider.java    From nifi with Apache License 2.0 5 votes vote down vote up
private String getProperty(AuthorizerConfigurationContext authContext, String propertyName, String defaultValue) {
    final PropertyValue property = authContext.getProperty(propertyName);
    final String value;

    if (property != null && property.isSet()) {
        value = property.getValue();
    } else {
        value = defaultValue;
    }
    return value;

}
 
Example 8
Source File: FetchDistributedMapCache.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    List<ValidationResult> results = new ArrayList<>(super.customValidate(validationContext));

    PropertyValue cacheEntryIdentifier = validationContext.getProperty(PROP_CACHE_ENTRY_IDENTIFIER);
    boolean elPresent = false;
    try {
        elPresent = cacheEntryIdentifier.isExpressionLanguagePresent();
    } catch (NullPointerException npe) {
        // Unfortunate workaround to a mock framework bug (NIFI-4590)
    }

    if (elPresent) {
        // This doesn't do a full job of validating against the requirement that Put Cache Value In Attribute must be set if multiple
        // Cache Entry Identifiers are supplied (if Expression Language is used). The user could conceivably have a comma-separated list of EL statements,
        // or a single EL statement with commas inside it but that evaluates to a single item.
        results.add(new ValidationResult.Builder().valid(true).explanation("Contains Expression Language").build());
    } else {
        if (!validationContext.getProperty(FetchDistributedMapCache.PROP_PUT_CACHE_VALUE_IN_ATTRIBUTE).isSet()) {
            String identifierString = cacheEntryIdentifier.getValue();
            if (identifierString.contains(",")) {
                results.add(new ValidationResult.Builder().valid(false)
                        .explanation("Multiple Cache Entry Identifiers specified without Put Cache Value In Attribute set").build());
            }
        }
    }
    return results;
}
 
Example 9
Source File: PutKudu.java    From nifi with Apache License 2.0 5 votes vote down vote up
private String getEvaluatedProperty(PropertyDescriptor property, ProcessContext context, FlowFile flowFile) {
    PropertyValue evaluatedProperty = context.getProperty(property).evaluateAttributeExpressions(flowFile);
    if (property.isRequired() && evaluatedProperty == null) {
        throw new ProcessException(String.format("Property `%s` is required but evaluated to null", property.getDisplayName()));
    }
    return evaluatedProperty.getValue();
}
 
Example 10
Source File: MockPropertyValue.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyValue evaluateAttributeExpressions(FlowFile flowFile, Map<String, String> additionalAttributes, AttributeValueDecorator decorator, Map<String, String> stateValues)
        throws ProcessException {
    markEvaluated();
    if (rawValue == null) {
        return this;
    }

    validateExpressionScope(flowFile != null || additionalAttributes != null);

    final PropertyValue newValue = stdPropValue.evaluateAttributeExpressions(flowFile, additionalAttributes, decorator, stateValues);
    return new MockPropertyValue(newValue.getValue(), serviceLookup, propertyDescriptor, true, variableRegistry);
}
 
Example 11
Source File: StatelessPropertyValue.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyValue evaluateAttributeExpressions(FlowFile flowFile, Map<String, String> additionalAttributes, AttributeValueDecorator decorator, Map<String, String> stateValues)
    throws ProcessException {
    markEvaluated();
    if (rawValue == null) {
        return this;
    }

    validateExpressionScope(flowFile != null || additionalAttributes != null);

    final PropertyValue newValue = stdPropValue.evaluateAttributeExpressions(flowFile, additionalAttributes, decorator, stateValues);
    return new StatelessPropertyValue(newValue.getValue(), serviceLookup, propertyDescriptor, true, parameterLookup, variableRegistry);
}
 
Example 12
Source File: RangerNiFiAuthorizer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private String getConfigValue(final AuthorizerConfigurationContext context, final String name, final String defaultValue) {
    final PropertyValue configValue = context.getProperty(name);

    String retValue = defaultValue;
    if (configValue != null && !StringUtils.isBlank(configValue.getValue())) {
        retValue = configValue.getValue();
    }

    return retValue;
}
 
Example 13
Source File: StandardSSLContextService.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public String getTrustStorePassword() {
    PropertyValue truststorePassword = configContext.getProperty(TRUSTSTORE_PASSWORD);
    return truststorePassword.isSet() ? truststorePassword.getValue() : "";
}
 
Example 14
Source File: CSVUtils.java    From nifi with Apache License 2.0 4 votes vote down vote up
private static CSVFormat buildCustomFormat(final PropertyContext context, final Map<String, String> variables) {
    final Character valueSeparator = getCharUnescapedJava(context, VALUE_SEPARATOR, variables);
    CSVFormat format = CSVFormat.newFormat(valueSeparator)
        .withAllowMissingColumnNames()
        .withIgnoreEmptyLines();

    final PropertyValue firstLineIsHeaderPropertyValue = context.getProperty(FIRST_LINE_IS_HEADER);
    if (firstLineIsHeaderPropertyValue.getValue() != null && firstLineIsHeaderPropertyValue.asBoolean()) {
        format = format.withFirstRecordAsHeader();
    }

    final Character quoteChar = getCharUnescaped(context, QUOTE_CHAR, variables);
    format = format.withQuote(quoteChar);

    final Character escapeChar = getCharUnescaped(context, ESCAPE_CHAR, variables);
    format = format.withEscape(escapeChar);

    format = format.withTrim(context.getProperty(TRIM_FIELDS).asBoolean());

    if (context.getProperty(COMMENT_MARKER).isSet()) {
        final Character commentMarker = getCharUnescaped(context, COMMENT_MARKER, variables);
        if (commentMarker != null) {
            format = format.withCommentMarker(commentMarker);
        }
    }
    if (context.getProperty(NULL_STRING).isSet()) {
        format = format.withNullString(unescape(context.getProperty(NULL_STRING).getValue()));
    }

    final PropertyValue quoteValue = context.getProperty(QUOTE_MODE);
    if (quoteValue != null && quoteValue.isSet()) {
        final QuoteMode quoteMode = QuoteMode.valueOf(quoteValue.getValue());
        format = format.withQuoteMode(quoteMode);
    }

    final PropertyValue trailingDelimiterValue = context.getProperty(TRAILING_DELIMITER);
    if (trailingDelimiterValue != null && trailingDelimiterValue.isSet()) {
        final boolean trailingDelimiter = trailingDelimiterValue.asBoolean();
        format = format.withTrailingDelimiter(trailingDelimiter);
    }

    final PropertyValue recordSeparator = context.getProperty(RECORD_SEPARATOR);
    if (recordSeparator != null && recordSeparator.isSet()) {
        final String separator = unescape(recordSeparator.getValue());
        format = format.withRecordSeparator(separator);
    }

    return format;
}
 
Example 15
Source File: AbstractCassandraProcessor.java    From nifi with Apache License 2.0 4 votes vote down vote up
void connectToCassandra(ProcessContext context) {
    if (cluster.get() == null) {
        ComponentLog log = getLogger();
        final String contactPointList = context.getProperty(CONTACT_POINTS).evaluateAttributeExpressions().getValue();
        final String consistencyLevel = context.getProperty(CONSISTENCY_LEVEL).getValue();
        final String compressionType = context.getProperty(COMPRESSION_TYPE).getValue();
        List<InetSocketAddress> contactPoints = getContactPoints(contactPointList);

        // Set up the client for secure (SSL/TLS communications) if configured to do so
        final SSLContextService sslService = context.getProperty(PROP_SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
        final String rawClientAuth = context.getProperty(CLIENT_AUTH).getValue();
        final SSLContext sslContext;

        if (sslService != null) {
            final SslContextFactory.ClientAuth clientAuth;

            if (StringUtils.isBlank(rawClientAuth)) {
                clientAuth = SslContextFactory.ClientAuth.REQUIRED;
            } else {
                try {
                    clientAuth = SslContextFactory.ClientAuth.valueOf(rawClientAuth);
                } catch (final IllegalArgumentException iae) {
                    throw new IllegalStateException(String.format("Unrecognized client auth '%s'. Possible values are [%s]",
                            rawClientAuth, StringUtils.join(SslContextFactory.ClientAuth.values(), ", ")));
                }
            }

            sslContext = sslService.createSSLContext(clientAuth);
        } else {
            sslContext = null;
        }

        final String username, password;
        PropertyValue usernameProperty = context.getProperty(USERNAME).evaluateAttributeExpressions();
        PropertyValue passwordProperty = context.getProperty(PASSWORD).evaluateAttributeExpressions();

        if (usernameProperty != null && passwordProperty != null) {
            username = usernameProperty.getValue();
            password = passwordProperty.getValue();
        } else {
            username = null;
            password = null;
        }

        // Create the cluster and connect to it
        Cluster newCluster = createCluster(contactPoints, sslContext, username, password, compressionType);
        PropertyValue keyspaceProperty = context.getProperty(KEYSPACE).evaluateAttributeExpressions();

        final Session newSession;
        // For Java 11, the getValue() call was added so the test could pass
        if (keyspaceProperty != null && keyspaceProperty.getValue() != null) {
            newSession = newCluster.connect(keyspaceProperty.getValue());
        } else {
            newSession = newCluster.connect();
        }

        newCluster.getConfiguration().getQueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel));
        Metadata metadata = newCluster.getMetadata();

        log.info("Connected to Cassandra cluster: {}", new Object[]{metadata.getClusterName()});

        cluster.set(newCluster);
        cassandraSession.set(newSession);
    }
}
 
Example 16
Source File: FileUserGroupProvider.java    From nifi with Apache License 2.0 4 votes vote down vote up
@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 and group mappings from nifi.properties if any are provided
        identityMappings = Collections.unmodifiableList(IdentityMappingUtil.getIdentityMappings(properties));
        groupMappings = Collections.unmodifiableList(IdentityMappingUtil.getGroupMappings(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 e) {
        throw new AuthorizerCreationException(e);
    }
}
 
Example 17
Source File: FileAuthorizer.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void doOnConfigured(final 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 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();
        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 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()));
            }

            // 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
            restoreAuthorizationsFile = new File(restoreDirectory, authorizationsFile.getName());
            restoreTenantsFile = new File(restoreDirectory, tenantsFile.getName());

            try {
                // sync the primary copy with the restore copy
                FileUtils.syncWithRestore(authorizationsFile, restoreAuthorizationsFile, logger);
                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 initial admin identity
        final PropertyValue initialAdminIdentityProp = configurationContext.getProperty(PROP_INITIAL_ADMIN_IDENTITY);
        initialAdminIdentity = initialAdminIdentityProp == null ? null : IdentityMappingUtil.mapIdentity(initialAdminIdentityProp.getValue(), identityMappings);

        // get the value of the legacy authorized users file
        final PropertyValue legacyAuthorizedUsersProp = configurationContext.getProperty(PROP_LEGACY_AUTHORIZED_USERS_FILE);
        legacyAuthorizedUsersFile = legacyAuthorizedUsersProp == null ? null : legacyAuthorizedUsersProp.getValue();

        // 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);
        }

        // 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("Authorizations file loaded at %s", new Date().toString()));

    } catch (IOException | AuthorizerCreationException | JAXBException | IllegalStateException | SAXException e) {
        throw new AuthorizerCreationException(e);
    }
}
 
Example 18
Source File: AbstractCassandraProcessor.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
protected void connectToCassandra(ProcessContext context) {
    if (cluster.get() == null) {
        ComponentLog log = getLogger();
        final String contactPointList = context.getProperty(CONTACT_POINTS).getValue();
        final String consistencyLevel = context.getProperty(CONSISTENCY_LEVEL).getValue();
        List<InetSocketAddress> contactPoints = getContactPoints(contactPointList);

        // Set up the client for secure (SSL/TLS communications) if configured to do so
        final SSLContextService sslService =
                context.getProperty(PROP_SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
        final String rawClientAuth = context.getProperty(CLIENT_AUTH).getValue();
        final SSLContext sslContext;

        if (sslService != null) {
            final SSLContextService.ClientAuth clientAuth;
            if (StringUtils.isBlank(rawClientAuth)) {
                clientAuth = SSLContextService.ClientAuth.REQUIRED;
            } else {
                try {
                    clientAuth = SSLContextService.ClientAuth.valueOf(rawClientAuth);
                } catch (final IllegalArgumentException iae) {
                    throw new ProviderCreationException(String.format("Unrecognized client auth '%s'. Possible values are [%s]",
                            rawClientAuth, StringUtils.join(SslContextFactory.ClientAuth.values(), ", ")));
                }
            }
            sslContext = sslService.createSSLContext(clientAuth);
        } else {
            sslContext = null;
        }

        final String username, password;
        PropertyValue usernameProperty = context.getProperty(USERNAME);
        PropertyValue passwordProperty = context.getProperty(PASSWORD);

        if (usernameProperty != null && passwordProperty != null) {
            username = usernameProperty.getValue();
            password = passwordProperty.getValue();
        } else {
            username = null;
            password = null;
        }

        // Create the cluster and connect to it
        Cluster newCluster = createCluster(contactPoints, sslContext, username, password);
        PropertyValue keyspaceProperty = context.getProperty(KEYSPACE);
        final Session newSession;
        if (keyspaceProperty != null) {
            newSession = newCluster.connect(keyspaceProperty.getValue());
        } else {
            newSession = newCluster.connect();
        }
        newCluster.getConfiguration().getQueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel));
        Metadata metadata = newCluster.getMetadata();
        log.info("Connected to Cassandra cluster: {}", new Object[]{metadata.getClusterName()});
        cluster.set(newCluster);
        cassandraSession.set(newSession);
    }
}
 
Example 19
Source File: DistributeLoad.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    Collection<ValidationResult> results = new ArrayList<>();
    if (doCustomValidate.getAndSet(false)) {
        String distStrat = validationContext.getProperty(DISTRIBUTION_STRATEGY).getValue();
        if (distStrat.equals(STRATEGY_LOAD_DISTRIBUTION_SERVICE)) {
            // make sure Hostnames and Controller service are set
            PropertyValue propDesc = validationContext.getProperty(HOSTNAMES);
            if (null == propDesc || null == propDesc.getValue() || propDesc.getValue().isEmpty()) {
                results.add(new ValidationResult.Builder().subject(HOSTNAMES.getName())
                        .explanation("Must specify Hostnames when using 'Load Distribution Strategy'").valid(false).build());
            }
            propDesc = validationContext.getProperty(LOAD_DISTRIBUTION_SERVICE_TEMPLATE);
            if (null == propDesc || null == propDesc.getValue() || propDesc.getValue().isEmpty()) {
                results.add(new ValidationResult.Builder()
                        .subject(LOAD_DISTRIBUTION_SERVICE_TEMPLATE.getName())
                        .explanation("Must specify 'Load Distribution Service ID' when using 'Load Distribution Strategy'")
                        .valid(false).build());
            }
            if (results.isEmpty()) {
                int numRels = validationContext.getProperty(NUM_RELATIONSHIPS).asInteger();
                String hostNamesValue = validationContext.getProperty(HOSTNAMES).getValue();
                String[] hostNames = hostNamesValue.split("(?:,+|;+|\\s+)");
                int numHosts = 0;
                for (String hostName : hostNames) {
                    if (StringUtils.isNotBlank(hostName)) {
                        hostNames[numHosts++] = hostName;
                    }
                }
                if (numHosts > numRels) {
                    results.add(new ValidationResult.Builder()
                            .subject("Number of Relationships and Hostnames")
                            .explanation("Number of Relationships must be equal to, or greater than, the number of host names")
                            .valid(false).build());
                } else {
                    // create new relationships with descriptions of hostname
                    Set<Relationship> relsWithDesc = new TreeSet<>();
                    for (int i = 0; i < numHosts; i++) {
                        relsWithDesc.add(new Relationship.Builder().name(String.valueOf(i + 1))
                                .description(hostNames[i]).build());
                    }
                    // add add'l rels if configuration requires it...it probably shouldn't
                    for (int i = numHosts + 1; i <= numRels; i++) {
                        relsWithDesc.add(createRelationship(i));
                    }
                    relationshipsRef.set(Collections.unmodifiableSet(relsWithDesc));
                }
            }
        }
    }
    return results;
}
 
Example 20
Source File: GetHDFSFileInfo.java    From nifi with Apache License 2.0 4 votes vote down vote up
protected HDFSFileInfoRequest buildRequestDetails(ProcessContext context, ProcessSession session, FlowFile ff) {
    HDFSFileInfoRequest req = new HDFSFileInfoRequest();
    req.fullPath = context.getProperty(FULL_PATH).evaluateAttributeExpressions(ff).getValue();
    req.isRecursive = context.getProperty(RECURSE_SUBDIRS).asBoolean();

    PropertyValue pv = null;
    String v = null;

    if (context.getProperty(DIR_FILTER).isSet() && (pv=context.getProperty(DIR_FILTER).evaluateAttributeExpressions(ff))!=null) {
        v = pv.getValue();
        req.dirFilter = v == null ? null : Pattern.compile(v);
    }

    if (context.getProperty(FILE_FILTER).isSet() && (pv=context.getProperty(FILE_FILTER).evaluateAttributeExpressions(ff))!=null) {
        v = pv.getValue();
        req.fileFilter = v == null ? null : Pattern.compile(v);
    }

    if (context.getProperty(FILE_EXCLUDE_FILTER).isSet() && (pv=context.getProperty(FILE_EXCLUDE_FILTER).evaluateAttributeExpressions(ff))!=null) {
        v = pv.getValue();
        req.fileExcludeFilter = v == null ? null : Pattern.compile(v);
    }

    req.isIgnoreDotFiles = context.getProperty(IGNORE_DOTTED_FILES).asBoolean();
    req.isIgnoreDotDirs = context.getProperty(IGNORE_DOTTED_DIRS).asBoolean();

    req.groupping = HDFSFileInfoRequest.Groupping.getEnum(context.getProperty(GROUPING).getValue());
    req.batchSize = Optional.ofNullable(context.getProperty(BATCH_SIZE))
        .filter(propertyValue -> propertyValue.getValue() != null)
        .map(PropertyValue::asInteger)
        .orElse(1);

    v = context.getProperty(DESTINATION).getValue();
    if (DESTINATION_CONTENT.getValue().equals(v)) {
        req.isDestContent = true;
    }else {
        req.isDestContent = false;
    }

    return req;
}