Skip to content

Commit 25bfdf5

Browse files
authored
[Fix #999] Support for more than one level of indirection for secret (#1000)
Signed-off-by: fjtirado <[email protected]>
1 parent fa6bec6 commit 25bfdf5

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/config/ConfigSecretManager.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.HashMap;
1919
import java.util.Map;
20+
import java.util.StringTokenizer;
2021
import java.util.concurrent.ConcurrentHashMap;
2122

2223
public class ConfigSecretManager implements SecretManager {
@@ -41,9 +42,21 @@ private Map<String, Object> buildMap(String secretName) {
4142
if (name.startsWith(prefix)) {
4243
configManager
4344
.config(name, String.class)
44-
.ifPresent(v -> map.put(name.substring(prefix.length()), v));
45+
.ifPresent(v -> addToMap(map, name.substring(prefix.length()), v));
4546
}
4647
}
4748
return map;
4849
}
50+
51+
private void addToMap(Map<String, Object> map, String name, Object v) {
52+
StringTokenizer tokenizer = new StringTokenizer(name, ".");
53+
while (tokenizer.hasMoreTokens()) {
54+
name = tokenizer.nextToken();
55+
if (tokenizer.hasMoreTokens()) {
56+
map = (Map<String, Object>) map.computeIfAbsent(name, k -> new HashMap<>());
57+
} else {
58+
map.put(name, v);
59+
}
60+
}
61+
}
4962
}

impl/test/src/test/java/io/serverlessworkflow/impl/test/SecretExpressionTest.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ static void init() throws IOException {
4545
@ResourceLock(Resources.SYSTEM_PROPERTIES)
4646
void testDefault() {
4747
System.setProperty("superman.name", "ClarkKent");
48+
System.setProperty("superman.enemy.name", "Lex Luthor");
49+
System.setProperty("superman.enemy.isHuman", "true");
4850
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
49-
assertThat(
50-
appl.workflowDefinition(workflow)
51-
.instance(Map.of())
52-
.start()
53-
.join()
54-
.asMap()
55-
.orElseThrow()
56-
.get("superSecret"))
57-
.isEqualTo("ClarkKent");
51+
Map<String, Object> map =
52+
appl.workflowDefinition(workflow).instance(Map.of()).start().join().asMap().orElseThrow();
53+
assertThat(map.get("superSecret")).isEqualTo("ClarkKent");
54+
assertThat(map.get("theEnemy")).isEqualTo("Lex Luthor");
55+
assertThat(map.get("humanEnemy")).isEqualTo("true");
5856
} finally {
5957
System.clearProperty("superman.name");
58+
System.clearProperty("superman.enemy.name");
59+
System.clearProperty("superman.enemy.isHuman");
6060
}
6161
}
6262

@@ -78,16 +78,20 @@ void testMissing() {
7878
@Test
7979
void testCustom() {
8080
try (WorkflowApplication appl =
81-
WorkflowApplication.builder().withSecretManager(k -> Map.of("name", "ClarkKent")).build()) {
82-
assertThat(
83-
appl.workflowDefinition(workflow)
84-
.instance(Map.of())
85-
.start()
86-
.join()
87-
.asMap()
88-
.orElseThrow()
89-
.get("superSecret"))
90-
.isEqualTo("ClarkKent");
81+
WorkflowApplication.builder()
82+
.withSecretManager(
83+
k ->
84+
Map.of(
85+
"name",
86+
"ClarkKent",
87+
"enemy",
88+
Map.of("name", "Lex Luthor", "isHuman", true)))
89+
.build()) {
90+
Map<String, Object> map =
91+
appl.workflowDefinition(workflow).instance(Map.of()).start().join().asMap().orElseThrow();
92+
assertThat(map.get("superSecret")).isEqualTo("ClarkKent");
93+
assertThat(map.get("theEnemy")).isEqualTo("Lex Luthor");
94+
assertThat(map.get("humanEnemy")).isEqualTo(true);
9195
}
9296
}
9397
}

impl/test/src/test/resources/workflows-samples/secret-expression.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ use:
99
do:
1010
- useExpression:
1111
set:
12-
superSecret: ${$secret.superman.name}
12+
superSecret: ${$secret.superman.name}
13+
theEnemy: ${$secret.superman.enemy.name}
14+
humanEnemy: ${$secret.superman.enemy.isHuman}

0 commit comments

Comments
 (0)