Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import org.apache.nifi.components.resource.StandardResourceReferenceFactory;
import org.apache.nifi.controller.ControllerServiceLookup;
import org.apache.nifi.controller.ProcessorNode;
import org.apache.nifi.parameter.Parameter;
import org.apache.nifi.parameter.ParameterLookup;
import org.apache.nifi.search.SearchContext;

import java.util.Map;
import java.util.Optional;

/**
*
Expand Down Expand Up @@ -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
*/
Comment on lines +72 to +74

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
/**
* Resolves only Parameters whose sensitivity matches the sensitivity of the Property, consistent with Parameter resolution at runtime
*/
/**
* 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.
*/

private static class SensitivityMatchingParameterLookup implements ParameterLookup {
private final ParameterLookup parameterLookup;
private final boolean sensitive;

private SensitivityMatchingParameterLookup(final ParameterLookup parameterLookup, final boolean sensitive) {
this.parameterLookup = parameterLookup == null ? EMPTY : parameterLookup;
this.sensitive = sensitive;
}

@Override
public Optional<Parameter> getParameter(final String parameterName) {
return parameterLookup.getParameter(parameterName)
.filter(parameter -> parameter.getDescriptor().isSensitive() == sensitive);
}

@Override
public boolean isEmpty() {
return parameterLookup.isEmpty();
}

@Override
public long getVersion() {
return parameterLookup.getVersion();
}
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
Comment on lines +98 to +114

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.


private record MapParameterLookup(Map<String, Parameter> parameters) implements ParameterLookup {
@Override
public Optional<Parameter> getParameter(final String parameterName) {
return Optional.ofNullable(parameters.get(parameterName));
}

@Override
public boolean isEmpty() {
return parameters.isEmpty();
}

@Override
public long getVersion() {
return 0;
}
}
}
Loading