Search in sources :

Example 21 with PropertyValue

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

the class DistributeLoad method customValidate.

@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;
}
Also used : TreeSet(java.util.TreeSet) Relationship(org.apache.nifi.processor.Relationship) DynamicRelationship(org.apache.nifi.annotation.behavior.DynamicRelationship) ArrayList(java.util.ArrayList) PropertyValue(org.apache.nifi.components.PropertyValue) ValidationResult(org.apache.nifi.components.ValidationResult)

Example 22 with PropertyValue

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

the class FetchDistributedMapCache method customValidate.

@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;
}
Also used : ArrayList(java.util.ArrayList) PropertyValue(org.apache.nifi.components.PropertyValue) ValidationResult(org.apache.nifi.components.ValidationResult)

Example 23 with PropertyValue

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

the class RangerNiFiAuthorizer method onConfigured.

@Override
public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
    try {
        if (nifiPlugin == null) {
            logger.info("RangerNiFiAuthorizer(): initializing base plugin");
            final PropertyValue securityConfigValue = configurationContext.getProperty(RANGER_SECURITY_PATH_PROP);
            addRequiredResource(RANGER_SECURITY_PATH_PROP, securityConfigValue);
            final PropertyValue auditConfigValue = configurationContext.getProperty(RANGER_AUDIT_PATH_PROP);
            addRequiredResource(RANGER_AUDIT_PATH_PROP, auditConfigValue);
            final String rangerKerberosEnabledValue = getConfigValue(configurationContext, RANGER_KERBEROS_ENABLED_PROP, Boolean.FALSE.toString());
            rangerKerberosEnabled = rangerKerberosEnabledValue.equals(Boolean.TRUE.toString()) ? true : false;
            if (rangerKerberosEnabled) {
                // configure UGI for when RangerAdminRESTClient calls UserGroupInformation.isSecurityEnabled()
                final Configuration securityConf = new Configuration();
                securityConf.set(HADOOP_SECURITY_AUTHENTICATION, KERBEROS_AUTHENTICATION);
                UserGroupInformation.setConfiguration(securityConf);
                // login with the nifi principal and keytab, RangerAdminRESTClient will use Ranger's MiscUtil which
                // will grab UserGroupInformation.getLoginUser() and call ugi.checkTGTAndReloginFromKeytab();
                final String nifiPrincipal = nifiProperties.getKerberosServicePrincipal();
                final String nifiKeytab = nifiProperties.getKerberosServiceKeytabLocation();
                if (StringUtils.isBlank(nifiPrincipal) || StringUtils.isBlank(nifiKeytab)) {
                    throw new AuthorizerCreationException("Principal and Keytab must be provided when Kerberos is enabled");
                }
                UserGroupInformation.loginUserFromKeytab(nifiPrincipal.trim(), nifiKeytab.trim());
            }
            final String serviceType = getConfigValue(configurationContext, RANGER_SERVICE_TYPE_PROP, DEFAULT_SERVICE_TYPE);
            final String appId = getConfigValue(configurationContext, RANGER_APP_ID_PROP, DEFAULT_APP_ID);
            nifiPlugin = createRangerBasePlugin(serviceType, appId);
            nifiPlugin.init();
            defaultAuditHandler = new RangerDefaultAuditHandler();
            rangerAdminIdentity = getConfigValue(configurationContext, RANGER_ADMIN_IDENTITY_PROP, null);
        } else {
            logger.info("RangerNiFiAuthorizer(): base plugin already initialized");
        }
    } catch (Throwable t) {
        throw new AuthorizerCreationException("Error creating RangerBasePlugin", t);
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) RangerConfiguration(org.apache.ranger.authorization.hadoop.config.RangerConfiguration) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) PropertyValue(org.apache.nifi.components.PropertyValue) RangerDefaultAuditHandler(org.apache.ranger.plugin.audit.RangerDefaultAuditHandler)

Example 24 with PropertyValue

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

the class TestAvroSchemaRegistry method validateStrictAndNonStrictSchemaRegistrationFromDynamicProperties.

@Test
public void validateStrictAndNonStrictSchemaRegistrationFromDynamicProperties() throws Exception {
    String schemaName = "fooSchema";
    ConfigurationContext configContext = mock(ConfigurationContext.class);
    Map<PropertyDescriptor, String> properties = new HashMap<>();
    PropertyDescriptor fooSchema = new PropertyDescriptor.Builder().name(schemaName).dynamic(true).build();
    // NOTE: name of record and name of first field are not Avro-compliant, verified below
    String fooSchemaText = "{\"namespace\": \"example.avro\", " + "\"type\": \"record\", " + "\"name\": \"$User\", " + "\"fields\": [ " + "{\"name\": \"@name\", \"type\": [\"string\", \"null\"]}, " + "{\"name\": \"favorite_number\",  \"type\": [\"int\", \"null\"]}, " + "{\"name\": \"foo\",  \"type\": [\"int\", \"null\"]}, " + "{\"name\": \"favorite_color\", \"type\": [\"string\", \"null\"]} " + "]" + "}";
    PropertyDescriptor barSchema = new PropertyDescriptor.Builder().name("barSchema").dynamic(false).build();
    properties.put(fooSchema, fooSchemaText);
    properties.put(barSchema, "");
    AvroSchemaRegistry delegate = new AvroSchemaRegistry();
    delegate.getSupportedPropertyDescriptors().forEach(prop -> properties.put(prop, prop.getDisplayName()));
    when(configContext.getProperties()).thenReturn(properties);
    ValidationContext validationContext = mock(ValidationContext.class);
    when(validationContext.getProperties()).thenReturn(properties);
    PropertyValue propertyValue = mock(PropertyValue.class);
    when(validationContext.getProperty(AvroSchemaRegistry.VALIDATE_FIELD_NAMES)).thenReturn(propertyValue);
    // Strict parsing
    when(propertyValue.asBoolean()).thenReturn(true);
    Collection<ValidationResult> results = delegate.customValidate(validationContext);
    assertTrue(results.stream().anyMatch(result -> !result.isValid()));
    // Non-strict parsing
    when(propertyValue.asBoolean()).thenReturn(false);
    results = delegate.customValidate(validationContext);
    results.forEach(result -> assertTrue(result.isValid()));
}
Also used : ValidationContext(org.apache.nifi.components.ValidationContext) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) HashMap(java.util.HashMap) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) PropertyValue(org.apache.nifi.components.PropertyValue) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Map(java.util.Map) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) ValidationResult(org.apache.nifi.components.ValidationResult) Mockito.mock(org.mockito.Mockito.mock) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) PropertyValue(org.apache.nifi.components.PropertyValue) ValidationResult(org.apache.nifi.components.ValidationResult) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 25 with PropertyValue

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

the class StandardSSLContextService method verifySslConfig.

private void verifySslConfig(final ValidationContext validationContext) throws ProcessException {
    final String protocol = getSSLProtocolForValidation(validationContext);
    try {
        final PropertyValue keyPasswdProp = validationContext.getProperty(KEY_PASSWORD);
        final char[] keyPassword = keyPasswdProp.isSet() ? keyPasswdProp.getValue().toCharArray() : null;
        final String keystoreFile = validationContext.getProperty(KEYSTORE).getValue();
        if (keystoreFile == null) {
            SslContextFactory.createTrustSslContext(validationContext.getProperty(TRUSTSTORE).getValue(), validationContext.getProperty(TRUSTSTORE_PASSWORD).getValue().toCharArray(), validationContext.getProperty(TRUSTSTORE_TYPE).getValue(), protocol);
            return;
        }
        final String truststoreFile = validationContext.getProperty(TRUSTSTORE).getValue();
        if (truststoreFile == null) {
            SslContextFactory.createSslContext(validationContext.getProperty(KEYSTORE).getValue(), validationContext.getProperty(KEYSTORE_PASSWORD).getValue().toCharArray(), keyPassword, validationContext.getProperty(KEYSTORE_TYPE).getValue(), protocol);
            return;
        }
        SslContextFactory.createSslContext(validationContext.getProperty(KEYSTORE).getValue(), validationContext.getProperty(KEYSTORE_PASSWORD).getValue().toCharArray(), keyPassword, validationContext.getProperty(KEYSTORE_TYPE).getValue(), validationContext.getProperty(TRUSTSTORE).getValue(), validationContext.getProperty(TRUSTSTORE_PASSWORD).getValue().toCharArray(), validationContext.getProperty(TRUSTSTORE_TYPE).getValue(), org.apache.nifi.security.util.SslContextFactory.ClientAuth.REQUIRED, protocol);
    } catch (final Exception e) {
        throw new ProcessException(e);
    }
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) PropertyValue(org.apache.nifi.components.PropertyValue) MalformedURLException(java.net.MalformedURLException) InitializationException(org.apache.nifi.reporting.InitializationException) ProcessException(org.apache.nifi.processor.exception.ProcessException)

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