Skip to content

Commit 933c355

Browse files
authored
Merge pull request #109 from tsurdilo/secretsconstants
adding secrets and constants
2 parents 6213467 + dc5eec6 commit 933c355

File tree

12 files changed

+468
-3
lines changed

12 files changed

+468
-3
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.api.deserializers;
17+
18+
import com.fasterxml.jackson.core.JsonParser;
19+
import com.fasterxml.jackson.databind.DeserializationContext;
20+
import com.fasterxml.jackson.databind.JsonNode;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
23+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
24+
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
25+
import io.serverlessworkflow.api.utils.Utils;
26+
import io.serverlessworkflow.api.workflow.Constants;
27+
import org.json.JSONObject;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
31+
import java.io.IOException;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
35+
public class ConstantsDeserializer extends StdDeserializer<Constants> {
36+
37+
private static final long serialVersionUID = 510l;
38+
private static Logger logger = LoggerFactory.getLogger(ConstantsDeserializer.class);
39+
40+
@SuppressWarnings("unused")
41+
private WorkflowPropertySource context;
42+
43+
public ConstantsDeserializer() {
44+
this(Constants.class);
45+
}
46+
47+
public ConstantsDeserializer(Class<?> vc) {
48+
super(vc);
49+
}
50+
51+
public ConstantsDeserializer(WorkflowPropertySource context) {
52+
this(Constants.class);
53+
this.context = context;
54+
}
55+
56+
@Override
57+
public Constants deserialize(JsonParser jp,
58+
DeserializationContext ctxt) throws IOException {
59+
60+
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
61+
JsonNode node = jp.getCodec().readTree(jp);
62+
63+
Constants constants = new Constants();
64+
JsonNode constantsDefinition = null;
65+
66+
if (node.isObject()) {
67+
constantsDefinition = node;
68+
} else {
69+
String constantsFileDef = node.asText();
70+
String constantsFileSrc = Utils.getResourceFileAsString(constantsFileDef);
71+
JsonNode constantsRefNode;
72+
ObjectMapper jsonWriter = new ObjectMapper();
73+
if (constantsFileSrc != null && constantsFileSrc.trim().length() > 0) {
74+
// if its a yaml def convert to json first
75+
if (!constantsFileSrc.trim().startsWith("{")) {
76+
// convert yaml to json to validate
77+
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
78+
Object obj = yamlReader.readValue(constantsFileSrc, Object.class);
79+
80+
constantsRefNode = jsonWriter.readTree(new JSONObject(jsonWriter.writeValueAsString(obj)).toString());
81+
} else {
82+
constantsRefNode = jsonWriter.readTree(new JSONObject(constantsFileSrc).toString());
83+
}
84+
85+
JsonNode refConstants = constantsRefNode.get("constants");
86+
if (refConstants != null) {
87+
constantsDefinition = refConstants;
88+
} else {
89+
logger.error("Unable to find constants definitions in reference file: {}", constantsFileSrc);
90+
}
91+
92+
} else {
93+
logger.error("Unable to load constants defs reference file: {}", constantsFileSrc);
94+
}
95+
96+
}
97+
constants.setConstantsDef(constantsDefinition);
98+
return constants;
99+
100+
}
101+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.api.deserializers;
17+
18+
import com.fasterxml.jackson.core.JsonParser;
19+
import com.fasterxml.jackson.databind.DeserializationContext;
20+
import com.fasterxml.jackson.databind.JsonNode;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
23+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
24+
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
25+
import io.serverlessworkflow.api.utils.Utils;
26+
import io.serverlessworkflow.api.workflow.Secrets;
27+
import org.json.JSONObject;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
31+
import java.io.IOException;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
35+
public class SecretsDeserializer extends StdDeserializer<Secrets> {
36+
37+
private static final long serialVersionUID = 510l;
38+
private static Logger logger = LoggerFactory.getLogger(SecretsDeserializer.class);
39+
40+
@SuppressWarnings("unused")
41+
private WorkflowPropertySource context;
42+
43+
public SecretsDeserializer() {
44+
this(Secrets.class);
45+
}
46+
47+
public SecretsDeserializer(Class<?> vc) {
48+
super(vc);
49+
}
50+
51+
public SecretsDeserializer(WorkflowPropertySource context) {
52+
this(Secrets.class);
53+
this.context = context;
54+
}
55+
56+
@Override
57+
public Secrets deserialize(JsonParser jp,
58+
DeserializationContext ctxt) throws IOException {
59+
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
60+
JsonNode node = jp.getCodec().readTree(jp);
61+
62+
Secrets secrets = new Secrets();
63+
List<String> secretsDefinitions = new ArrayList<>();
64+
65+
if (node.isArray()) {
66+
for (final JsonNode nodeEle : node) {
67+
secretsDefinitions.add(nodeEle.asText());
68+
}
69+
} else {
70+
String secretsFileDef = node.asText();
71+
String secretsFileSrc = Utils.getResourceFileAsString(secretsFileDef);
72+
JsonNode secretsRefNode;
73+
ObjectMapper jsonWriter = new ObjectMapper();
74+
if (secretsFileSrc != null && secretsFileSrc.trim().length() > 0) {
75+
// if its a yaml def convert to json first
76+
if (!secretsFileSrc.trim().startsWith("{")) {
77+
// convert yaml to json to validate
78+
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
79+
Object obj = yamlReader.readValue(secretsFileSrc, Object.class);
80+
81+
secretsRefNode = jsonWriter.readTree(new JSONObject(jsonWriter.writeValueAsString(obj)).toString());
82+
} else {
83+
secretsRefNode = jsonWriter.readTree(new JSONObject(secretsFileSrc).toString());
84+
}
85+
86+
JsonNode refSecrets = secretsRefNode.get("secrets");
87+
if (refSecrets != null) {
88+
for (final JsonNode nodeEle : refSecrets) {
89+
secretsDefinitions.add(nodeEle.asText());
90+
}
91+
} else {
92+
logger.error("Unable to find secrets definitions in reference file: {}", secretsFileSrc);
93+
}
94+
95+
} else {
96+
logger.error("Unable to load secrets defs reference file: {}", secretsFileSrc);
97+
}
98+
99+
}
100+
secrets.setSecretDefs(secretsDefinitions);
101+
return secrets;
102+
}
103+
}

api/src/main/java/io/serverlessworkflow/api/mapper/WorkflowModule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
import io.serverlessworkflow.api.states.OperationState;
3636
import io.serverlessworkflow.api.states.ParallelState;
3737
import io.serverlessworkflow.api.transitions.Transition;
38-
import io.serverlessworkflow.api.workflow.Events;
39-
import io.serverlessworkflow.api.workflow.Functions;
40-
import io.serverlessworkflow.api.workflow.Retries;
38+
import io.serverlessworkflow.api.workflow.*;
4139

4240
public class WorkflowModule extends SimpleModule {
4341

@@ -94,6 +92,8 @@ private void addDefaultDeserializers() {
9492
addDeserializer(EventDefinition.Kind.class, new EventDefinitionKindDeserializer(workflowPropertySource));
9593
addDeserializer(ParallelState.CompletionType.class, new ParallelStateCompletionTypeDeserializer(workflowPropertySource));
9694
addDeserializer(Retries.class, new RetriesDeserializer(workflowPropertySource));
95+
addDeserializer(Secrets.class, new SecretsDeserializer(workflowPropertySource));
96+
addDeserializer(Constants.class, new ConstantsDeserializer(workflowPropertySource));
9797
addDeserializer(Functions.class, new FunctionsDeserializer(workflowPropertySource));
9898
addDeserializer(Events.class, new EventsDeserializer(workflowPropertySource));
9999
addDeserializer(Start.class, new StartDefinitionDeserializer(workflowPropertySource));

api/src/main/java/io/serverlessworkflow/api/serializers/WorkflowSerializer.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.serverlessworkflow.api.serializers;
1717

1818
import com.fasterxml.jackson.core.JsonGenerator;
19+
import com.fasterxml.jackson.databind.JsonNode;
1920
import com.fasterxml.jackson.databind.SerializerProvider;
2021
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
2122
import io.serverlessworkflow.api.Workflow;
@@ -143,6 +144,21 @@ public void serialize(Workflow workflow,
143144
gen.writeEndArray();
144145
}
145146

147+
if (workflow.getSecrets() != null && !workflow.getSecrets().getSecretDefs().isEmpty()) {
148+
gen.writeArrayFieldStart("secrets");
149+
for (String secretDef : workflow.getSecrets().getSecretDefs()) {
150+
gen.writeString(secretDef);
151+
}
152+
gen.writeEndArray();
153+
} else {
154+
gen.writeArrayFieldStart("secrets");
155+
gen.writeEndArray();
156+
}
157+
158+
if (workflow.getConstants() != null && !workflow.getConstants().getConstantsDef().isEmpty()) {
159+
gen.writeObjectField("constants", workflow.getConstants().getConstantsDef());
160+
}
161+
146162
if (workflow.getStates() != null && !workflow.getStates().isEmpty()) {
147163
gen.writeArrayFieldStart("states");
148164
for (State state : workflow.getStates()) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.api.workflow;
17+
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
20+
import java.util.List;
21+
22+
public class Constants {
23+
private String refValue;
24+
private JsonNode constantsDef;
25+
26+
public String getRefValue() {
27+
return refValue;
28+
}
29+
30+
public void setRefValue(String refValue) {
31+
this.refValue = refValue;
32+
}
33+
34+
public JsonNode getConstantsDef() {
35+
return constantsDef;
36+
}
37+
38+
public void setConstantsDef(JsonNode constantsDef) {
39+
this.constantsDef = constantsDef;
40+
}
41+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.api.workflow;
17+
18+
import java.util.List;
19+
20+
public class Secrets {
21+
private String refValue;
22+
private List<String> secretDefs;
23+
24+
public String getRefValue() {
25+
return refValue;
26+
}
27+
28+
public void setRefValue(String refValue) {
29+
this.refValue = refValue;
30+
}
31+
32+
public List<String> getSecretDefs() {
33+
return secretDefs;
34+
}
35+
36+
public void setSecretDefs(List<String> secretDefs) {
37+
this.secretDefs = secretDefs;
38+
}
39+
}

api/src/main/resources/schema/workflow.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@
7272
"existingJavaType": "io.serverlessworkflow.api.workflow.Retries",
7373
"description": "Workflow retry definitions"
7474
},
75+
"secrets": {
76+
"type": "object",
77+
"existingJavaType": "io.serverlessworkflow.api.workflow.Secrets",
78+
"description": "Workflow secrets definitions"
79+
},
80+
"constants": {
81+
"type": "object",
82+
"existingJavaType": "io.serverlessworkflow.api.workflow.Constants",
83+
"description": "Workflow constants definitions"
84+
},
7585
"states": {
7686
"type": "array",
7787
"description": "State Definitions",

0 commit comments

Comments
 (0)