mcgilman commented on code in PR #11955:
URL: https://github.com/apache/nifi/pull/11955#discussion_r4647283373


##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/controller/StandardSearchContext.java:
##########
@@ -58,12 +60,41 @@ public String getAnnotationData() {
     public PropertyValue getProperty(final PropertyDescriptor property) {
         final String configuredValue = 
processorNode.getRawPropertyValue(property);
         final ResourceContext resourceContext = new 
StandardResourceContext(new StandardResourceReferenceFactory(), property);
-        return new StandardPropertyValue(resourceContext, configuredValue == 
null ? property.getDefaultValue() : configuredValue, controllerServiceLookup, 
ParameterLookup.EMPTY);
+        final ParameterLookup parameterLookup = new 
SensitivityMatchingParameterLookup(processorNode.getParameterLookup(), 
property.isSensitive());
+        return new StandardPropertyValue(resourceContext, configuredValue == 
null ? property.getDefaultValue() : configuredValue, controllerServiceLookup, 
parameterLookup);
     }
 
     @Override
     public Map<PropertyDescriptor, String> getProperties() {
         return processorNode.getRawPropertyValues();
     }
 
+    /**
+     * Resolves only Parameters whose sensitivity matches the sensitivity of 
the Property, consistent with Parameter resolution at runtime
+     */

Review Comment:
   This isn't quite consistent with runtime. At runtime, 
`AbstractComponentNode.isResolveParameter` keeps the entire raw value when any 
referenced parameter's sensitivity differs from the property's. This lookup 
filters each reference on its own, and expression language evaluates a filtered 
parameter to an empty value. For example, `#{scripts.dir}/#{secret}` on a 
non-sensitive property evaluates to `/opt/scripts/` here, but runtime keeps the 
raw string. That configuration is already invalid, and the sensitive value is 
never substituted, so I don't think the behavior needs to change. The comment 
should describe what it does, though. Maybe something like:
   
   ```suggestion
       /**
        * Resolves only Parameters whose sensitivity matches the sensitivity of 
the Property. A reference to a Parameter with
        * mismatched sensitivity evaluates to an empty value, so a sensitive 
Parameter value is never substituted into a non-sensitive Property.
        */
   ```



##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/web/controller/TestStandardSearchContext.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.web.controller;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.Validator;
+import org.apache.nifi.components.resource.ResourceCardinality;
+import org.apache.nifi.components.resource.ResourceType;
+import org.apache.nifi.controller.ControllerServiceLookup;
+import org.apache.nifi.controller.ProcessorNode;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.parameter.Parameter;
+import org.apache.nifi.parameter.ParameterLookup;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Map;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class TestStandardSearchContext {
+
+    private static final String SCRIPTS_DIR_PARAMETER = "scripts.dir";
+
+    private static final String SECRET_PARAMETER = "secret";
+
+    private static final String SECRET_VALUE = "s3cr3t";
+
+    private static final PropertyDescriptor SCRIPT_FILE = new 
PropertyDescriptor.Builder()
+            .name("Script File")
+            .required(false)
+            .addValidator(Validator.VALID)
+            .identifiesExternalResource(ResourceCardinality.SINGLE, 
ResourceType.FILE)
+            .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+            .build();
+
+    private static final PropertyDescriptor PASSWORD = new 
PropertyDescriptor.Builder()
+            .name("Password")
+            .required(false)
+            .sensitive(true)
+            .addValidator(Validator.VALID)
+            .build();
+
+    @Mock
+    private ProcessorNode processorNode;
+
+    @Mock
+    private ControllerServiceLookup controllerServiceLookup;
+
+    private StandardSearchContext context;
+
+    @BeforeEach
+    void setContext() {
+        final Map<String, Parameter> parameters = Map.of(
+                SCRIPTS_DIR_PARAMETER, new 
Parameter.Builder().name(SCRIPTS_DIR_PARAMETER).value("/opt/scripts").sensitive(false).build(),
+                SECRET_PARAMETER, new 
Parameter.Builder().name(SECRET_PARAMETER).value(SECRET_VALUE).sensitive(true).build()
+        );
+        when(processorNode.getParameterLookup()).thenReturn(new 
MapParameterLookup(parameters));
+
+        context = new StandardSearchContext("search-me", processorNode, 
controllerServiceLookup);
+    }
+
+    @Test
+    void getPropertyEvaluatedResolvesParameter() {
+        
when(processorNode.getRawPropertyValue(SCRIPT_FILE)).thenReturn("#{scripts.dir}/file.groovy");
+
+        assertEquals("/opt/scripts/file.groovy", 
context.getProperty(SCRIPT_FILE).evaluateAttributeExpressions().getValue());
+    }
+
+    @Test
+    void getPropertyValueRemainsRaw() {
+        
when(processorNode.getRawPropertyValue(SCRIPT_FILE)).thenReturn("#{scripts.dir}/file.groovy");
+
+        assertEquals("#{scripts.dir}/file.groovy", 
context.getProperty(SCRIPT_FILE).getValue());
+    }
+
+    @Test
+    void 
getPropertyEvaluatedExcludesSensitiveParameterForNonSensitiveProperty() {
+        
when(processorNode.getRawPropertyValue(SCRIPT_FILE)).thenReturn("#{secret}");
+
+        final String evaluated = 
context.getProperty(SCRIPT_FILE).evaluateAttributeExpressions().getValue();
+
+        assertFalse(evaluated != null && evaluated.contains(SECRET_VALUE));
+    }
+
+    @Test
+    void 
getPropertyEvaluatedExcludesNonSensitiveParameterForSensitiveProperty() {
+        
when(processorNode.getRawPropertyValue(PASSWORD)).thenReturn("#{scripts.dir}");
+
+        final String evaluated = 
context.getProperty(PASSWORD).evaluateAttributeExpressions().getValue();
+
+        assertFalse(evaluated != null && evaluated.contains("/opt/scripts"));
+    }

Review Comment:
   `assertFalse(evaluated != null && evaluated.contains(...))` passes for null, 
an empty string, the raw reference, or a truncated path, so it doesn't pin down 
the actual behavior. Could these assert the exact strings? For Script File:
   
   - `#{secret}` evaluates to `""`
   - `#{scripts.dir}/#{secret}` evaluates to `/opt/scripts/`
   
   Could you also add a case where `processorNode.getParameterLookup()` returns 
`null`? That's the common case of a processor whose group has no Parameter 
Context. `#{scripts.dir}/file.groovy` should evaluate to `/file.groovy` without 
throwing. The null guard in `SensitivityMatchingParameterLookup` handles this, 
but nothing currently tests it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to