|
| 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 | +} |
0 commit comments