Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SpEL): implement to configure the limit of characters for SpEL expressions #4755

Merged
merged 2 commits into from
Jul 2, 2024
Merged
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 @@ -35,6 +35,7 @@
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

Expand Down Expand Up @@ -130,8 +131,7 @@ public static SpelEvaluatorVersion Default() {
PipelineExecution.AuthenticationDetails.class,
PipelineExecution.PausedDetails.class
};

private final ExpressionParser parser = new SpelExpressionParser();
private final ExpressionParser parser;
private final ParserContext parserContext = new TemplateParserContext("${", "}");
private final ExpressionsSupport support;

Expand All @@ -148,6 +148,12 @@ public PipelineExpressionEvaluator(
pluginManager,
expressionProperties);
initExecutionAwareFunctions(expressionFunctionProviders);
parser =
new SpelExpressionParser(
expressionProperties.getMaxExpressionLength() > 0
? new SpelParserConfiguration(
Copy link
Contributor

Choose a reason for hiding this comment

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

too bad there's no builder for SpelParserConfiguration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code can be dropped moving beyond springframework >=6.1.3.

null, null, false, false, 0, expressionProperties.getMaxExpressionLength())
: new SpelParserConfiguration());
}

public Map<String, Object> evaluate(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2024 OpsMx, Inc.
*
* Licensed 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 com.netflix.spinnaker.orca.pipeline.expressions;

import static org.junit.jupiter.api.Assertions.assertTrue;

import com.netflix.spinnaker.kork.expressions.ExpressionEvaluationSummary;
import com.netflix.spinnaker.kork.expressions.config.ExpressionProperties;
import com.netflix.spinnaker.orca.test.YamlFileApplicationContextInitializer;
import java.util.ArrayList;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.pf4j.PluginManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(
classes = PipelineExpressionLengthTest.class,
initializers = PipelineExpressionLengthTest.class)
@SpringBootTest
@EnableConfigurationProperties(ExpressionProperties.class)
public class PipelineExpressionLengthTest extends YamlFileApplicationContextInitializer {

@Mock private PluginManager pluginManager;
@Autowired private ExpressionProperties expressionProperties;

@Override
protected String getResourceLocation() {
return "classpath:expression-properties.yml";
}

@Test
void customExpressionLength() {
String expression = String.format("%s", repeat("T", 10975));
String rootObjectExpression = String.format("${status.toString() == \"%s\"}", expression);

Map<String, String> rootObject = Map.of("status", expression);
Map<String, Object> source = Map.of("test", rootObjectExpression);

PipelineExpressionEvaluator evaluator =
new PipelineExpressionEvaluator(new ArrayList<>(), pluginManager, expressionProperties);

Map<String, Object> result =
evaluator.evaluate(source, rootObject, new ExpressionEvaluationSummary(), true);
assertTrue(Boolean.parseBoolean(result.get("test").toString()));
}

private String repeat(String str, int count) {
String res = "";
for (int i = 0; i < count; i++) {
res += str;
}
return res;
}
}
2 changes: 2 additions & 0 deletions orca-core/src/test/resources/expression-properties.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
expression:
max-expression-length: 11000