forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support env variable in condition (elastic#13608)
This PR substitutes ${VAR} in Expression, except RegexValueExpression, with the value in secret store, env. The substitution happens after syntax parsing and before graph execution. Fixed: elastic#5115
- Loading branch information
1 parent
bdf2748
commit b05326d
Showing
25 changed files
with
466 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
logstash-core/src/main/java/org/logstash/config/ir/expression/ExpressionSubstitution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.logstash.config.ir.expression; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.logstash.common.SourceWithMetadata; | ||
import org.logstash.config.ir.CompiledPipeline; | ||
import org.logstash.config.ir.InvalidIRException; | ||
import org.logstash.plugins.ConfigVariableExpander; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Map; | ||
|
||
public class ExpressionSubstitution { | ||
/** | ||
* Replace ${VAR:defaultValue} in BinaryBooleanExpression, UnaryBooleanExpression and ValueExpression, excluding RegexValueExpression | ||
* with the value in the following precedence: secret store, environment variable, default value | ||
* @param cve The actual pattern matching take place | ||
* @param expression The Expression to be substituted | ||
* @return substituted Expression | ||
*/ | ||
public static Expression substituteBoolExpression(ConfigVariableExpander cve, Expression expression) { | ||
try { | ||
if (expression instanceof BinaryBooleanExpression) { | ||
BinaryBooleanExpression binaryBoolExp = (BinaryBooleanExpression) expression; | ||
Expression substitutedLeftExp = substituteBoolExpression(cve, binaryBoolExp.getLeft()); | ||
Expression substitutedRightExp = substituteBoolExpression(cve, binaryBoolExp.getRight()); | ||
if (substitutedLeftExp != binaryBoolExp.getLeft() || substitutedRightExp != binaryBoolExp.getRight()) { | ||
Constructor<? extends BinaryBooleanExpression> constructor = binaryBoolExp.getClass().getConstructor(SourceWithMetadata.class, Expression.class, Expression.class); | ||
return constructor.newInstance(binaryBoolExp.getSourceWithMetadata(), substitutedLeftExp, substitutedRightExp); | ||
} | ||
} else if (expression instanceof UnaryBooleanExpression) { | ||
UnaryBooleanExpression unaryBoolExp = (UnaryBooleanExpression) expression; | ||
Expression substitutedExp = substituteBoolExpression(cve, unaryBoolExp.getExpression()); | ||
if (substitutedExp != unaryBoolExp.getExpression()) { | ||
Constructor<? extends UnaryBooleanExpression> constructor = unaryBoolExp.getClass().getConstructor(SourceWithMetadata.class, Expression.class); | ||
return constructor.newInstance(unaryBoolExp.getSourceWithMetadata(), substitutedExp); | ||
} | ||
} else if (expression instanceof ValueExpression && !(expression instanceof RegexValueExpression) && (((ValueExpression) expression).get() != null)) { | ||
final String key = "placeholder"; | ||
Map<String, Object> args = ImmutableMap.of(key, ((ValueExpression) expression).get()); | ||
Map<String, Object> substitutedArgs = CompiledPipeline.expandConfigVariables(cve, args); | ||
return new ValueExpression(expression.getSourceWithMetadata(), substitutedArgs.get(key)); | ||
} | ||
|
||
return expression; | ||
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | InvalidIRException e) { | ||
throw new IllegalStateException("Unable to instantiate substituted condition expression", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.