use of org.apache.nifi.components.ValidationResult in project nifi by apache.
the class StandardSSLContextService method onConfigured.
@OnEnabled
public void onConfigured(final ConfigurationContext context) throws InitializationException {
configContext = context;
final Collection<ValidationResult> results = new ArrayList<>();
results.addAll(validateStore(context.getProperties(), KeystoreValidationGroup.KEYSTORE));
results.addAll(validateStore(context.getProperties(), KeystoreValidationGroup.TRUSTSTORE));
if (!results.isEmpty()) {
final StringBuilder sb = new StringBuilder(this + " is not valid due to:");
for (final ValidationResult result : results) {
sb.append("\n").append(result.toString());
}
throw new InitializationException(sb.toString());
}
if (countNulls(context.getProperty(KEYSTORE).getValue(), context.getProperty(KEYSTORE_PASSWORD).getValue(), context.getProperty(KEYSTORE_TYPE).getValue(), context.getProperty(TRUSTSTORE).getValue(), context.getProperty(TRUSTSTORE_PASSWORD).getValue(), context.getProperty(TRUSTSTORE_TYPE).getValue()) >= 4) {
throw new InitializationException(this + " does not have the KeyStore or the TrustStore populated");
}
// verify that the filename, password, and type match
createSSLContext(ClientAuth.REQUIRED);
}
use of org.apache.nifi.components.ValidationResult in project nifi by apache.
the class StandardSSLContextService method validateStore.
private static Collection<ValidationResult> validateStore(final Map<PropertyDescriptor, String> properties, final KeystoreValidationGroup keyStoreOrTrustStore) {
final Collection<ValidationResult> results = new ArrayList<>();
final String filename;
final String password;
final String type;
if (keyStoreOrTrustStore == KeystoreValidationGroup.KEYSTORE) {
filename = properties.get(KEYSTORE);
password = properties.get(KEYSTORE_PASSWORD);
type = properties.get(KEYSTORE_TYPE);
} else {
filename = properties.get(TRUSTSTORE);
password = properties.get(TRUSTSTORE_PASSWORD);
type = properties.get(TRUSTSTORE_TYPE);
}
final String keystoreDesc = (keyStoreOrTrustStore == KeystoreValidationGroup.KEYSTORE) ? "Keystore" : "Truststore";
final int nulls = countNulls(filename, password, type);
if (nulls != 3 && nulls != 0) {
results.add(new ValidationResult.Builder().valid(false).explanation("Must set either 0 or 3 properties for " + keystoreDesc).subject(keystoreDesc + " Properties").build());
} else if (nulls == 0) {
// all properties were filled in.
final File file = new File(filename);
if (!file.exists() || !file.canRead()) {
results.add(new ValidationResult.Builder().valid(false).subject(keystoreDesc + " Properties").explanation("Cannot access file " + file.getAbsolutePath()).build());
} else {
try {
final boolean storeValid = CertificateUtils.isStoreValid(file.toURI().toURL(), KeystoreType.valueOf(type), password.toCharArray());
if (!storeValid) {
results.add(new ValidationResult.Builder().subject(keystoreDesc + " Properties").valid(false).explanation("Invalid KeyStore Password or Type specified for file " + filename).build());
}
} catch (MalformedURLException e) {
results.add(new ValidationResult.Builder().subject(keystoreDesc + " Properties").valid(false).explanation("Malformed URL from file: " + e).build());
}
}
}
return results;
}
use of org.apache.nifi.components.ValidationResult in project nifi by apache.
the class TestMergeContent method testTextDelimitersValidation.
@Test
public void testTextDelimitersValidation() throws IOException, InterruptedException {
final TestRunner runner = TestRunners.newTestRunner(new MergeContent());
runner.setProperty(MergeContent.MAX_BIN_AGE, "1 sec");
runner.setProperty(MergeContent.MERGE_FORMAT, MergeContent.MERGE_FORMAT_CONCAT);
runner.setProperty(MergeContent.DELIMITER_STRATEGY, MergeContent.DELIMITER_STRATEGY_TEXT);
runner.setProperty(MergeContent.HEADER, "");
runner.setProperty(MergeContent.DEMARCATOR, "");
runner.setProperty(MergeContent.FOOTER, "");
Collection<ValidationResult> results = new HashSet<>();
ProcessContext context = runner.getProcessContext();
if (context instanceof MockProcessContext) {
MockProcessContext mockContext = (MockProcessContext) context;
results = mockContext.validate();
}
Assert.assertEquals(3, results.size());
for (ValidationResult vr : results) {
Assert.assertTrue(vr.toString().contains("cannot be empty"));
}
}
use of org.apache.nifi.components.ValidationResult in project nifi by apache.
the class TestRouteOnAttribute method testInvalidOnNonBooleanProperty.
@Test
public void testInvalidOnNonBooleanProperty() {
final RouteOnAttribute proc = new RouteOnAttribute();
final MockProcessContext ctx = new MockProcessContext(proc);
// Should be boolean
final ValidationResult validationResult = ctx.setProperty("RouteA", "${a:length()");
assertFalse(validationResult.isValid());
}
use of org.apache.nifi.components.ValidationResult in project nifi by apache.
the class StandardValidators method createAttributeExpressionLanguageValidator.
public static Validator createAttributeExpressionLanguageValidator(final ResultType expectedResultType, final boolean allowExtraCharacters) {
return new Validator() {
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
final String syntaxError = context.newExpressionLanguageCompiler().validateExpression(input, allowExtraCharacters);
if (syntaxError != null) {
return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation(syntaxError).build();
}
final ResultType resultType = allowExtraCharacters ? ResultType.STRING : context.newExpressionLanguageCompiler().getResultType(input);
if (!resultType.equals(expectedResultType)) {
return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation("Expected Attribute Query to return type " + expectedResultType + " but query returns type " + resultType).build();
}
return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
}
};
}
Aggregations