Skip to content

Commit 7409880

Browse files
committed
NIFI-14381: Incorporate review comments, refactored SSL Context identification
1 parent bb4fa4d commit 7409880

File tree

8 files changed

+119
-135
lines changed

8 files changed

+119
-135
lines changed

nifi-extension-bundles/nifi-standard-bundle/nifi-standard-rules/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<dependency>
2828
<groupId>org.apache.nifi</groupId>
2929
<artifactId>nifi-ssl-context-service-api</artifactId>
30-
<scope>compile</scope>
30+
<scope>test</scope>
3131
</dependency>
3232
</dependencies>
3333

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.nifi.flowanalysis.rules;
18+
19+
import org.apache.nifi.annotation.documentation.CapabilityDescription;
20+
import org.apache.nifi.annotation.documentation.Tags;
21+
import org.apache.nifi.annotation.documentation.UseCase;
22+
import org.apache.nifi.flow.VersionedComponent;
23+
import org.apache.nifi.flow.VersionedConfigurableExtension;
24+
import org.apache.nifi.flowanalysis.AbstractFlowAnalysisRule;
25+
import org.apache.nifi.flowanalysis.ComponentAnalysisResult;
26+
import org.apache.nifi.flowanalysis.FlowAnalysisRuleContext;
27+
import org.apache.nifi.util.StringUtils;
28+
29+
import java.util.Collection;
30+
import java.util.HashSet;
31+
import java.util.List;
32+
33+
34+
@Tags({"component", "processor", "controller service", "type", "ssl", "tls", "listen"})
35+
@CapabilityDescription("Produces rule violations for each component (i.e. processors or controller services) having a property "
36+
+ "identifying an SSLContextService that is not set.")
37+
@UseCase(
38+
description = "Ensure that an SSL Context has been configured for the specified components. This helps avoid ports being opened for insecure (plaintext, e.g.) communications.",
39+
configuration = """
40+
To avoid the violation, ensure that the "SSL Context Service" property is set for the specified component(s).
41+
"""
42+
)
43+
public class RequireCustomSSLContext extends AbstractFlowAnalysisRule {
44+
45+
private final List<String> componentTypes = List.of(
46+
"ListenFTP",
47+
"ListenHTTP",
48+
"ListenTCP",
49+
"ListenOLTP",
50+
"ListenSyslog",
51+
"HandleHttpRequest",
52+
"JettyWebSocketServer"
53+
);
54+
55+
@Override
56+
public Collection<ComponentAnalysisResult> analyzeComponent(VersionedComponent component, FlowAnalysisRuleContext context) {
57+
Collection<ComponentAnalysisResult> results = new HashSet<>();
58+
59+
if (component instanceof VersionedConfigurableExtension versionedConfigurableExtension) {
60+
61+
String encounteredComponentType = versionedConfigurableExtension.getType();
62+
String encounteredSimpleComponentType = encounteredComponentType.substring(encounteredComponentType.lastIndexOf(".") + 1);
63+
64+
if (componentTypes.isEmpty() || componentTypes.contains(encounteredComponentType) || componentTypes.contains(encounteredSimpleComponentType)) {
65+
// Loop over the properties for this component looking for an SSLContextService
66+
versionedConfigurableExtension.getProperties().forEach((propertyName, propertyValue) -> {
67+
68+
// If the SSL Context property exists and the value is not set, report a violation
69+
if (("SSL Context Service".equalsIgnoreCase(propertyName) || "ssl-context-service".equalsIgnoreCase(propertyName)) &&
70+
StringUtils.isEmpty(propertyValue)) {
71+
ComponentAnalysisResult result = new ComponentAnalysisResult(
72+
component.getInstanceIdentifier(),
73+
"'" + encounteredSimpleComponentType + "' must specify an SSL Context Service"
74+
);
75+
76+
results.add(result);
77+
78+
}
79+
});
80+
}
81+
}
82+
return results;
83+
}
84+
}

nifi-extension-bundles/nifi-standard-bundle/nifi-standard-rules/src/main/java/org/apache/nifi/flowanalysis/rules/RequireSecureConnection.java

Lines changed: 0 additions & 121 deletions
This file was deleted.

nifi-extension-bundles/nifi-standard-bundle/nifi-standard-rules/src/main/resources/META-INF/services/org.apache.nifi.flowanalysis.FlowAnalysisRule

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
# limitations under the License.
1515

1616
org.apache.nifi.flowanalysis.rules.DisallowComponentType
17-
org.apache.nifi.flowanalysis.rules.RequireSecureConnection
17+
org.apache.nifi.flowanalysis.rules.RequireCustomSSLContext
1818
org.apache.nifi.flowanalysis.rules.RestrictBackpressureSettings
1919
org.apache.nifi.flowanalysis.rules.RestrictFlowFileExpiration
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
16+
# RequireCustomSSLContext
17+
18+
## Usage Information
19+
20+
This rule should be applied to prevent flow designers from opening ports for insecure connections. The list of components to which this rule applies is as follows:
21+
22+
- **ListenFTP**
23+
- **ListenHTTP**
24+
- **ListenTCP**
25+
- **ListenOLTP**
26+
- **ListenSyslog**
27+
- **HandleHttpRequest**
28+
- **JettyWebSocketServer**

nifi-extension-bundles/nifi-standard-bundle/nifi-standard-rules/src/test/java/org/apache/nifi/flowanalysis/rules/RequireSecureConnectionTest.java renamed to nifi-extension-bundles/nifi-standard-bundle/nifi-standard-rules/src/test/java/org/apache/nifi/flowanalysis/rules/RequireCustomSSLContextTest.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
import org.apache.nifi.components.PropertyDescriptor;
2020
import org.apache.nifi.flowanalysis.ComponentAnalysisResult;
2121
import org.apache.nifi.ssl.SSLContextProvider;
22-
import org.junit.jupiter.api.BeforeEach;
2322
import org.junit.jupiter.api.Test;
2423

2524
import java.util.List;
2625

27-
public class RequireSecureConnectionTest extends AbstractFlowAnalaysisRuleTest<RequireSecureConnection> {
26+
public class RequireCustomSSLContextTest extends AbstractFlowAnalaysisRuleTest<RequireCustomSSLContext> {
2827

2928
public static final PropertyDescriptor SSL_CONTEXT_SERVICE = new PropertyDescriptor.Builder()
3029
.name("SSL Context Service")
@@ -33,21 +32,15 @@ public class RequireSecureConnectionTest extends AbstractFlowAnalaysisRuleTest<R
3332
.identifiesControllerService(SSLContextProvider.class)
3433
.build();
3534
@Override
36-
protected RequireSecureConnection initializeRule() {
37-
return new RequireSecureConnection();
35+
protected RequireCustomSSLContext initializeRule() {
36+
return new RequireCustomSSLContext();
3837
}
3938

40-
@BeforeEach
41-
@Override
42-
public void setup() {
43-
super.setup();
44-
setProperty(RequireSecureConnection.COMPONENT_TYPE, "ListenHTTP");
45-
}
4639
@Test
4740
public void testNoViolations() throws Exception {
4841
setProperty(SSL_CONTEXT_SERVICE, "9c50e433-c2aa-3d19-aae6-20299f4ac38c");
4942
testAnalyzeProcessors(
50-
"src/test/resources/RequireSecureConnection/RequireSecureConnection_noViolation.json",
43+
"src/test/resources/RequireCustomSSLContext/RequireSecureConnection_noViolation.json",
5144
List.of()
5245
);
5346
}
@@ -58,7 +51,7 @@ public void testViolations() throws Exception {
5851

5952
ComponentAnalysisResult expectedResult = new ComponentAnalysisResult("b5734be4-0195-1000-0e75-bc0f150d06bd", "'ListenHTTP' is not allowed");
6053
testAnalyzeProcessors(
61-
"src/test/resources/RequireSecureConnection/RequireSecureConnection.json",
54+
"src/test/resources/RequireCustomSSLContext/RequireCustomSSLContext.json",
6255
List.of(
6356
expectedResult // processor ListenHttp with no SSLContextService set
6457
)

nifi-extension-bundles/nifi-standard-bundle/nifi-standard-rules/src/test/resources/RequireSecureConnection/RequireSecureConnection.json renamed to nifi-extension-bundles/nifi-standard-bundle/nifi-standard-rules/src/test/resources/RequireCustomSSLContext/RequireCustomSSLContext.json

File renamed without changes.

nifi-extension-bundles/nifi-standard-bundle/nifi-standard-rules/src/test/resources/RequireSecureConnection/RequireSecureConnection_noViolation.json renamed to nifi-extension-bundles/nifi-standard-bundle/nifi-standard-rules/src/test/resources/RequireCustomSSLContext/RequireSecureConnection_noViolation.json

File renamed without changes.

0 commit comments

Comments
 (0)