Skip to content

Commit

Permalink
[KOGITO-9810] Supporting nested properties
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Sep 14, 2023
1 parent 402e4cc commit 8df264f
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.kie.kogito.jackson.utils.JsonObjectUtils;
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
import org.kie.kogito.process.expr.Expression;
import org.kie.kogito.serverless.workflow.utils.ConfigResolverHolder;
import org.kie.kogito.serverless.workflow.utils.ExpressionHandlerUtils;
import org.kie.kogito.serverless.workflow.utils.JsonNodeContext;
import org.slf4j.Logger;
Expand Down Expand Up @@ -151,7 +152,7 @@ public void assign(Object target, Object value, KogitoProcessContext context) {

private Scope getScope(KogitoProcessContext processInfo) {
Scope childScope = Scope.newChildScope(scope.get());
childScope.setValue(ExpressionHandlerUtils.SECRET_MAGIC, new FunctionJsonNode(ExpressionHandlerUtils::getSecret));
childScope.setValue(ExpressionHandlerUtils.SECRET_MAGIC, JsonObjectUtils.fromValue(ConfigResolverHolder.getConfigResolver().asNestedMap()));
childScope.setValue(ExpressionHandlerUtils.CONTEXT_MAGIC, new FunctionJsonNode(ExpressionHandlerUtils.getContextFunction(processInfo)));
childScope.setValue(ExpressionHandlerUtils.CONST_MAGIC, ExpressionHandlerUtils.getConstants(processInfo));
return childScope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ private static Stream<Arguments> provideMagicWordExpressionsToTest() {
Arguments.of("$SECRET.none", "null", getContext()),
Arguments.of("\"$SECRET.none\"", "$SECRET.none", getContext()),
Arguments.of("$SECRET.lettersonly", "secretlettersonly", getContext()),
Arguments.of("$SECRET.dot.secret", "null", getContext()),
Arguments.of("$SECRET.\"dot.secret\"", "secretdotsecret", getContext()),
Arguments.of("$SECRET.dot.secret", "secretdotsecret", getContext()),
Arguments.of("$SECRET.\"dash-secret\"", "secretdashsecret", getContext()),
Arguments.of("$CONST.someconstant", "value", getContext()),
Arguments.of("$CONST.\"someconstant\"", "value", getContext()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.function.Function;

import org.kie.kogito.internal.process.runtime.KogitoProcessContext;
import org.kie.kogito.jackson.utils.JsonObjectUtils;
import org.kie.kogito.serverless.workflow.utils.ConfigResolverHolder;
import org.kie.kogito.serverless.workflow.utils.ExpressionHandlerUtils;

import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
Expand All @@ -40,7 +42,7 @@ public Object getMapValue(Object obj, String key) {
} else {
switch (key) {
case "$" + ExpressionHandlerUtils.SECRET_MAGIC:
return (Function<String, Object>) ExpressionHandlerUtils::getSecret;
return JsonObjectUtils.fromValue(ConfigResolverHolder.getConfigResolver().asNestedMap());
case "$" + ExpressionHandlerUtils.CONTEXT_MAGIC:
return ExpressionHandlerUtils.getContextFunction(context);
case "$" + ExpressionHandlerUtils.CONST_MAGIC:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ private static Stream<Arguments> provideMagicWordExpressionsToTest() {
return Stream.of(
Arguments.of("$WORKFLOW.instanceId", "1111-2222-3333", getContext()),
Arguments.of("$SECRET.lettersonly", "secretlettersonly", getContext()),
Arguments.of("$SECRET.none", "null", getContext()),
// Arguments.of("$SECRET.dot.secret", "null", getContext()), // exception due to missing object at path .dot
Arguments.of("$SECRET[\"dot.secret\"]", "secretdotsecret", getContext()),
Arguments.of("$SECRET.dot.secret", "secretdotsecret", getContext()),
Arguments.of("$SECRET[\"dash-secret\"]", "secretdashsecret", getContext()),
Arguments.of("$CONST.someconstant", "value", getContext()),
Arguments.of("$CONST[\"someconstant\"]", "value", getContext()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private JsonNode transform(JsonNode node, Object inputModel, KogitoProcessContex
}
return expr.isValid() ? expr.eval(inputModel, JsonNode.class, context) : node;
} catch (Exception ex) {
logger.info("Error evaluating expression, returning original text {}", node);
logger.info("Error {} evaluating expression}, returning original text {}", ex.getMessage(), expr.asString(), node);
return node;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ public interface ConfigResolver {

<T> Collection<T> getIndexedConfigProperty(String name, Class<T> clazz);

default Map<String, Object> asNestedMap() {
Map<String, Object> map = new HashMap<>();
for (String name : getPropertyNames()) {
Optional<String> value = getConfigProperty(name, String.class);
if (value.isPresent()) {
String[] parts = name.split("\\.");
Map<String, Object> localMap = map;
for (int i = 0; i < parts.length - 1; i++) {
Map<String, Object> newMap = new HashMap<>();
localMap.put(parts[i], newMap);
localMap = newMap;
}
localMap.put(parts[parts.length - 1], value.orElseThrow());
}
}
return map;
}

default Map<String, Object> asMap() {
Map<String, Object> map = new HashMap<>();
for (String name : getPropertyNames()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import org.jbpm.ruleflow.core.Metadata;
Expand Down Expand Up @@ -54,7 +55,11 @@ public static JsonNode getConstants(KogitoProcessContext context) {
}

public static String getSecret(String key) {
return ConfigResolverHolder.getConfigResolver().getConfigProperty(key, String.class).orElse(null);
return getProperty(key).orElse(null);
}

private static Optional<String> getProperty(String key) {
return ConfigResolverHolder.getConfigResolver().getConfigProperty(key, String.class);
}

public static Function<String, Object> getContextFunction(KogitoProcessContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ mp.messaging.incoming.event2Exclusive.path=/event2Exclusive
mp.messaging.incoming.event3Exclusive.connector=quarkus-http
mp.messaging.incoming.event3Exclusive.path=/event3Exclusive

my_name=javierito
expression.my_name=javierito

# Kafka configuration for the sw tests that produce events
mp.messaging.outgoing.kogito_outgoing_stream.connector=smallrye-kafka
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{
"name": "secretMessage",
"type": "expression",
"operation": ".message |= \"my name is \"+$SECRET.my_name"
"operation": ".message |= \"my name is \"+$SECRET.expression.my_name"
},
{
"name": "constantMessage",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.kie.kogito.serverless.workflow.utils.ConfigResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.quarkus.runtime.Startup;
import io.smallrye.config.SecretKeys;

@Startup
public class QuarkusConfigResolver implements ConfigResolver {

private static Logger logger = LoggerFactory.getLogger(QuarkusConfigResolver.class);

private Config config;

public QuarkusConfigResolver() {
Expand All @@ -45,6 +49,9 @@ public <T> Optional<T> getConfigProperty(String key, Class<T> clazz) {
} catch (SecurityException ex) {
// see https://smallrye.io/docs/smallrye-config/config/secret-keys.html
return SecretKeys.doUnlocked(() -> config.getOptionalValue(key, clazz));
} catch (IllegalArgumentException ex) {
logger.info("Error {} retrieving key {}", ex.getMessage(), key);
return Optional.empty();
}
}

Expand Down

0 comments on commit 8df264f

Please sign in to comment.