Skip to content

Commit 5b9a289

Browse files
committed
NIFI-15022 Created a custom validator to enforce the value for the LOG_LEVEL property when the value is not specified as an Expression Language Expression.
1 parent d92d9f7 commit 5b9a289

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

nifi-extension-bundles/nifi-enrich-bundle/nifi-enrich-processors/src/main/java/org/apache/nifi/processors/AbstractEnrichIP.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.apache.nifi.annotation.lifecycle.OnScheduled;
2121
import org.apache.nifi.annotation.lifecycle.OnStopped;
2222
import org.apache.nifi.components.PropertyDescriptor;
23+
import org.apache.nifi.components.ValidationResult;
24+
import org.apache.nifi.components.Validator;
2325
import org.apache.nifi.components.resource.ResourceCardinality;
2426
import org.apache.nifi.components.resource.ResourceType;
2527
import org.apache.nifi.expression.AttributeExpression;
@@ -43,6 +45,7 @@
4345
import java.util.concurrent.locks.Lock;
4446
import java.util.concurrent.locks.ReadWriteLock;
4547
import java.util.concurrent.locks.ReentrantReadWriteLock;
48+
import java.util.regex.Pattern;
4649

4750
public abstract class AbstractEnrichIP extends AbstractProcessor {
4851

@@ -63,11 +66,31 @@ public abstract class AbstractEnrichIP extends AbstractProcessor {
6366
.addValidator(StandardValidators.createAttributeExpressionLanguageValidator(AttributeExpression.ResultType.STRING))
6467
.build();
6568

69+
private static final Pattern LOG_LEVEL_PATTERN = Pattern.compile("^(?:INFO|DEBUG|WARN|ERROR)$");
70+
private static final Validator LOG_LEVEL_VALIDATOR = (subject, input, context) -> {
71+
if (!context.isExpressionLanguagePresent(input)) {
72+
final boolean matches = LOG_LEVEL_PATTERN.matcher(input).matches();
73+
if (!matches) {
74+
return (new ValidationResult.Builder())
75+
.subject(subject)
76+
.valid(false)
77+
.explanation(String.format("%s must be either INFO, DEBUG, WARN or ERROR", subject))
78+
.input(input)
79+
.build();
80+
}
81+
}
82+
return (new ValidationResult.Builder())
83+
.subject(subject)
84+
.input(input)
85+
.valid(true)
86+
.build();
87+
};
88+
6689
public static final PropertyDescriptor LOG_LEVEL = new PropertyDescriptor.Builder()
6790
.name("Log Level")
6891
.required(true)
6992
.description("The Log Level to use when an IP is not found in the database. Accepted values: INFO, DEBUG, WARN, ERROR.")
70-
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
93+
.addValidator(LOG_LEVEL_VALIDATOR)
7194
.defaultValue(MessageLogLevel.WARN.toString())
7295
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
7396
.build();

nifi-extension-bundles/nifi-enrich-bundle/nifi-enrich-processors/src/test/java/org/apache/nifi/processors/TestGeoEnrichIPRecord.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@
3939
import org.apache.nifi.util.TestRunners;
4040
import org.junit.jupiter.api.BeforeEach;
4141
import org.junit.jupiter.api.Test;
42+
import org.junit.jupiter.params.ParameterizedTest;
43+
import org.junit.jupiter.params.provider.Arguments;
44+
import org.junit.jupiter.params.provider.MethodSource;
4245

4346
import java.io.InputStream;
4447
import java.net.InetAddress;
4548
import java.nio.charset.StandardCharsets;
4649
import java.util.HashMap;
4750
import java.util.List;
4851
import java.util.Map;
52+
import java.util.stream.Stream;
4953

5054
import static org.apache.nifi.processors.GeoEnrichTestUtils.getFullCityResponse;
5155
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -90,7 +94,28 @@ public void setup() throws Exception {
9094
runner.enableControllerService(registry);
9195
runner.enableControllerService(reader);
9296
runner.enableControllerService(writer);
97+
}
98+
99+
@ParameterizedTest
100+
@MethodSource("logLevelArgs")
101+
void testInvalidLogLevel(String logLevel, boolean expectValid) {
102+
runner.setProperty(AbstractEnrichIP.LOG_LEVEL, logLevel);
103+
104+
if (expectValid) {
105+
runner.assertValid();
106+
} else {
107+
runner.assertNotValid();
108+
}
109+
}
93110

111+
private static Stream<Arguments> logLevelArgs() {
112+
return Stream.of(
113+
Arguments.argumentSet("Invalid Log Level Specified", "GIBBERISH", false),
114+
Arguments.argumentSet("Log Level Specified as Expression Language Expression", "${log.level}", true)
115+
);
116+
}
117+
118+
private void commonTest(String path, int not, int found, int original) {
94119
runner.setProperty(GeoEnrichIPRecord.GEO_CITY, "/geo/city");
95120
runner.setProperty(GeoEnrichIPRecord.GEO_COUNTRY, "/geo/country");
96121
runner.setProperty(GeoEnrichIPRecord.GEO_COUNTRY_ISO, "/geo/country_iso");
@@ -99,9 +124,7 @@ public void setup() throws Exception {
99124
runner.setProperty(GeoEnrichIPRecord.GEO_LONGITUDE, "/geo/lon");
100125
runner.setProperty(AbstractEnrichIP.LOG_LEVEL, "WARN");
101126
runner.assertValid();
102-
}
103127

104-
private void commonTest(String path, int not, int found, int original) {
105128
Map<String, String> attrs = new HashMap<>();
106129
attrs.put("schema.name", "record");
107130
runner.enqueue(getClass().getResourceAsStream(path), attrs);

0 commit comments

Comments
 (0)