Search in sources :

Example 16 with ValidationResult

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

the class StandardProcessorTestRunner method setProperty.

@Override
public ValidationResult setProperty(final ControllerService service, final PropertyDescriptor property, final String value) {
    final MockStateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier());
    if (serviceStateManager == null) {
        throw new IllegalStateException("Controller service " + service + " has not been added to this TestRunner via the #addControllerService method");
    }
    final ControllerServiceConfiguration configuration = getConfigToUpdate(service);
    final Map<PropertyDescriptor, String> curProps = configuration.getProperties();
    final Map<PropertyDescriptor, String> updatedProps = new HashMap<>(curProps);
    final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service);
    final ValidationResult validationResult = property.validate(value, validationContext);
    final String oldValue = updatedProps.get(property);
    updatedProps.put(property, value);
    configuration.setProperties(updatedProps);
    if ((value == null && oldValue != null) || (value != null && !value.equals(oldValue))) {
        service.onPropertyModified(property, oldValue, value);
    }
    return validationResult;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) MockStateManager(org.apache.nifi.state.MockStateManager) HashMap(java.util.HashMap) ValidationResult(org.apache.nifi.components.ValidationResult) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 17 with ValidationResult

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

the class TestStandardProcessorTestRunner method testControllerServiceUpdateShouldCallOnSetProperty.

@Test
public void testControllerServiceUpdateShouldCallOnSetProperty() {
    // Arrange
    final ControllerService testService = new SimpleTestService();
    final AddAttributeProcessor proc = new AddAttributeProcessor();
    final TestRunner runner = TestRunners.newTestRunner(proc);
    final String serviceIdentifier = "test";
    final String pdName = "name";
    final String pdValue = "exampleName";
    try {
        runner.addControllerService(serviceIdentifier, testService);
    } catch (InitializationException e) {
        fail(e.getMessage());
    }
    assertFalse("onPropertyModified has been called", ((SimpleTestService) testService).isOpmCalled());
    // Act
    ValidationResult vr = runner.setProperty(testService, pdName, pdValue);
    // Assert
    assertTrue(vr.isValid());
    ControllerServiceConfiguration csConf = ((MockProcessContext) runner.getProcessContext()).getConfiguration(serviceIdentifier);
    PropertyDescriptor propertyDescriptor = testService.getPropertyDescriptor(pdName);
    String retrievedPDValue = csConf.getProperties().get(propertyDescriptor);
    assertEquals(pdValue, retrievedPDValue);
    assertTrue("onPropertyModified has not been called", ((SimpleTestService) testService).isOpmCalled());
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) InitializationException(org.apache.nifi.reporting.InitializationException) ValidationResult(org.apache.nifi.components.ValidationResult) AbstractControllerService(org.apache.nifi.controller.AbstractControllerService) ControllerService(org.apache.nifi.controller.ControllerService) Test(org.junit.Test)

Example 18 with ValidationResult

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

the class ReportLineageToAtlas method customValidate.

@Override
protected Collection<ValidationResult> customValidate(ValidationContext context) {
    final Collection<ValidationResult> results = new ArrayList<>();
    final boolean isSSLContextServiceSet = context.getProperty(SSL_CONTEXT_SERVICE).isSet();
    final ValidationResult.Builder invalidSSLService = new ValidationResult.Builder().subject(SSL_CONTEXT_SERVICE.getDisplayName()).valid(false);
    parseAtlasUrls(context.getProperty(ATLAS_URLS), input -> {
        final ValidationResult.Builder builder = new ValidationResult.Builder().subject(ATLAS_URLS.getDisplayName()).input(input);
        try {
            final URL url = new URL(input);
            if ("https".equalsIgnoreCase(url.getProtocol()) && !isSSLContextServiceSet) {
                results.add(invalidSSLService.explanation("required by HTTPS Atlas access").build());
            } else {
                results.add(builder.explanation("Valid URI").valid(true).build());
            }
        } catch (Exception e) {
            results.add(builder.explanation("Contains invalid URI: " + e).valid(false).build());
        }
    });
    final String atlasAuthNMethod = context.getProperty(ATLAS_AUTHN_METHOD).getValue();
    final AtlasAuthN atlasAuthN = getAtlasAuthN(atlasAuthNMethod);
    results.addAll(atlasAuthN.validate(context));
    clusterResolverLoader.forEach(resolver -> results.addAll(resolver.validate(context)));
    if (context.getProperty(ATLAS_CONF_CREATE).asBoolean()) {
        Stream.of(ATLAS_CONF_DIR, ATLAS_DEFAULT_CLUSTER_NAME, KAFKA_BOOTSTRAP_SERVERS).filter(p -> !context.getProperty(p).isSet()).forEach(p -> results.add(new ValidationResult.Builder().subject(p.getDisplayName()).explanation("required to create Atlas configuration file.").valid(false).build()));
        validateKafkaProperties(context, results, isSSLContextServiceSet, invalidSSLService);
    }
    return results;
}
Also used : AtlasAuthN(org.apache.nifi.atlas.security.AtlasAuthN) StandardValidators(org.apache.nifi.processor.util.StandardValidators) Arrays(java.util.Arrays) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) URL(java.net.URL) Kerberos(org.apache.nifi.atlas.security.Kerberos) StringUtils(org.apache.commons.lang3.StringUtils) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) PropertyValue(org.apache.nifi.components.PropertyValue) Scope(org.apache.nifi.components.state.Scope) AbstractReportingTask(org.apache.nifi.reporting.AbstractReportingTask) Map(java.util.Map) PROVENANCE_START_POSITION(org.apache.nifi.reporting.util.provenance.ProvenanceEventConsumer.PROVENANCE_START_POSITION) ProvenanceRepository(org.apache.nifi.provenance.ProvenanceRepository) ZoneOffset(java.time.ZoneOffset) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) PROVENANCE_BATCH_SIZE(org.apache.nifi.reporting.util.provenance.ProvenanceEventConsumer.PROVENANCE_BATCH_SIZE) RequiresInstanceClassLoading(org.apache.nifi.annotation.behavior.RequiresInstanceClassLoading) OnUnscheduled(org.apache.nifi.annotation.lifecycle.OnUnscheduled) AtlasServiceException(org.apache.atlas.AtlasServiceException) Collection(java.util.Collection) ProcessGroupStatus(org.apache.nifi.controller.status.ProcessGroupStatus) SSLContextService(org.apache.nifi.ssl.SSLContextService) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) AllowableValue(org.apache.nifi.components.AllowableValue) Instant(java.time.Instant) AnalysisContext(org.apache.nifi.atlas.provenance.AnalysisContext) CompleteFlowPathLineage(org.apache.nifi.atlas.provenance.lineage.CompleteFlowPathLineage) Stateful(org.apache.nifi.annotation.behavior.Stateful) List(java.util.List) DynamicProperty(org.apache.nifi.annotation.behavior.DynamicProperty) Stream(java.util.stream.Stream) NiFiFlowAnalyzer(org.apache.nifi.atlas.NiFiFlowAnalyzer) Tags(org.apache.nifi.annotation.documentation.Tags) ClusterResolver(org.apache.nifi.atlas.resolver.ClusterResolver) EventAccess(org.apache.nifi.reporting.EventAccess) ProvenanceEventConsumer(org.apache.nifi.reporting.util.provenance.ProvenanceEventConsumer) StandardAnalysisContext(org.apache.nifi.atlas.provenance.StandardAnalysisContext) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) Basic(org.apache.nifi.atlas.security.Basic) ValidationContext(org.apache.nifi.components.ValidationContext) ClientResponse(com.sun.jersey.api.client.ClientResponse) NIFI_KERBEROS_PRINCIPAL(org.apache.nifi.atlas.reporting.ReportLineageToAtlas.NIFI_KERBEROS_PRINCIPAL) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) RegexClusterResolver(org.apache.nifi.atlas.resolver.RegexClusterResolver) NiFiAtlasHook(org.apache.nifi.atlas.NiFiAtlasHook) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) SslConfigs(org.apache.kafka.common.config.SslConfigs) ValidationResult(org.apache.nifi.components.ValidationResult) LinkedHashSet(java.util.LinkedHashSet) PropertyContext(org.apache.nifi.context.PropertyContext) NiFiFlow(org.apache.nifi.atlas.NiFiFlow) Properties(java.util.Properties) NiFiAtlasClient(org.apache.nifi.atlas.NiFiAtlasClient) MalformedURLException(java.net.MalformedURLException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ReportingContext(org.apache.nifi.reporting.ReportingContext) File(java.io.File) SimpleFlowPathLineage(org.apache.nifi.atlas.provenance.lineage.SimpleFlowPathLineage) Consumer(java.util.function.Consumer) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) KerberosCredentialsService(org.apache.nifi.kerberos.KerberosCredentialsService) AtlasAuthN(org.apache.nifi.atlas.security.AtlasAuthN) NIFI_KERBEROS_KEYTAB(org.apache.nifi.atlas.reporting.ReportLineageToAtlas.NIFI_KERBEROS_KEYTAB) LineageStrategy(org.apache.nifi.atlas.provenance.lineage.LineageStrategy) ClusterResolvers(org.apache.nifi.atlas.resolver.ClusterResolvers) DateTimeFormatter(java.time.format.DateTimeFormatter) ApplicationProperties(org.apache.atlas.ApplicationProperties) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped) Collections(java.util.Collections) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult) URL(java.net.URL) AtlasServiceException(org.apache.atlas.AtlasServiceException) ProcessException(org.apache.nifi.processor.exception.ProcessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 19 with ValidationResult

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

the class ReportLineageToAtlas method validateKafkaProperties.

private void validateKafkaProperties(ValidationContext context, Collection<ValidationResult> results, boolean isSSLContextServiceSet, ValidationResult.Builder invalidSSLService) {
    final String kafkaSecurityProtocol = context.getProperty(KAFKA_SECURITY_PROTOCOL).getValue();
    if ((SEC_SSL.equals(kafkaSecurityProtocol) || SEC_SASL_SSL.equals(kafkaSecurityProtocol)) && !isSSLContextServiceSet) {
        results.add(invalidSSLService.explanation("required by SSL Kafka connection").build());
    }
    final String explicitPrincipal = context.getProperty(NIFI_KERBEROS_PRINCIPAL).evaluateAttributeExpressions().getValue();
    final String explicitKeytab = context.getProperty(NIFI_KERBEROS_KEYTAB).evaluateAttributeExpressions().getValue();
    final KerberosCredentialsService credentialsService = context.getProperty(ReportLineageToAtlas.KERBEROS_CREDENTIALS_SERVICE).asControllerService(KerberosCredentialsService.class);
    String principal;
    String keytab;
    if (credentialsService == null) {
        principal = explicitPrincipal;
        keytab = explicitKeytab;
    } else {
        principal = credentialsService.getPrincipal();
        keytab = credentialsService.getKeytab();
    }
    if (SEC_SASL_PLAINTEXT.equals(kafkaSecurityProtocol) || SEC_SASL_SSL.equals(kafkaSecurityProtocol)) {
        if (!context.getProperty(KAFKA_KERBEROS_SERVICE_NAME).isSet()) {
            results.add(new ValidationResult.Builder().subject(KAFKA_KERBEROS_SERVICE_NAME.getDisplayName()).explanation("Required by Kafka SASL authentication.").valid(false).build());
        }
        if (keytab == null || principal == null) {
            results.add(new ValidationResult.Builder().subject("Kerberos Authentication").explanation("Keytab and Principal are required for Kerberos authentication with Apache Kafka.").valid(false).build());
        }
    }
}
Also used : KerberosCredentialsService(org.apache.nifi.kerberos.KerberosCredentialsService) ValidationResult(org.apache.nifi.components.ValidationResult)

Example 20 with ValidationResult

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

the class RegexClusterResolver method validate.

@Override
public Collection<ValidationResult> validate(ValidationContext validationContext) {
    final List<ValidationResult> validationResults = new ArrayList<>();
    consumeConfigurations(validationContext.getAllProperties(), (clusterNamePatterns, patterns) -> {
    }, (entry, e) -> {
        final ValidationResult result = new ValidationResult.Builder().subject(entry.getKey()).input(entry.getValue()).explanation(e.getMessage()).valid(false).build();
        validationResults.add(result);
    });
    return validationResults;
}
Also used : ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult)

Aggregations

ValidationResult (org.apache.nifi.components.ValidationResult)153 ArrayList (java.util.ArrayList)63 Test (org.junit.Test)56 ValidationContext (org.apache.nifi.components.ValidationContext)36 MockProcessContext (org.apache.nifi.util.MockProcessContext)26 TestRunner (org.apache.nifi.util.TestRunner)21 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)20 Validator (org.apache.nifi.components.Validator)20 HashSet (java.util.HashSet)18 ProcessContext (org.apache.nifi.processor.ProcessContext)15 HashMap (java.util.HashMap)12 File (java.io.File)11 Collection (java.util.Collection)11 SSLContextService (org.apache.nifi.ssl.SSLContextService)11 Map (java.util.Map)10 List (java.util.List)9 ComponentLog (org.apache.nifi.logging.ComponentLog)9 IOException (java.io.IOException)8 ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)8 KerberosCredentialsService (org.apache.nifi.kerberos.KerberosCredentialsService)8