Skip to content

Commit 6f424c1

Browse files
authoredApr 7, 2022
Merge pull request #189 from tsurdilo/ups2
[4.0.x] latest updates from main

File tree

15 files changed

+275
-59
lines changed

15 files changed

+275
-59
lines changed
 
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.auth.AuthDefinition;
25+
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
26+
import io.serverlessworkflow.api.utils.Utils;
27+
import io.serverlessworkflow.api.workflow.Auth;
28+
import java.io.IOException;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
import org.json.JSONObject;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
34+
35+
public class AuthDeserializer extends StdDeserializer<Auth> {
36+
37+
private static final long serialVersionUID = 520L;
38+
private static Logger logger = LoggerFactory.getLogger(AuthDeserializer.class);
39+
40+
@SuppressWarnings("unused")
41+
private WorkflowPropertySource context;
42+
43+
public AuthDeserializer() {
44+
this(Auth.class);
45+
}
46+
47+
public AuthDeserializer(Class<?> vc) {
48+
super(vc);
49+
}
50+
51+
public AuthDeserializer(WorkflowPropertySource context) {
52+
this(Auth.class);
53+
this.context = context;
54+
}
55+
56+
@Override
57+
public Auth deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
58+
59+
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
60+
JsonNode node = jp.getCodec().readTree(jp);
61+
62+
Auth auth = new Auth();
63+
List<AuthDefinition> authDefinitions = new ArrayList<>();
64+
65+
if (node.isArray()) {
66+
for (final JsonNode nodeEle : node) {
67+
authDefinitions.add(mapper.treeToValue(nodeEle, AuthDefinition.class));
68+
}
69+
} else {
70+
String authFileDef = node.asText();
71+
String authFileSrc = Utils.getResourceFileAsString(authFileDef);
72+
JsonNode authRefNode;
73+
ObjectMapper jsonWriter = new ObjectMapper();
74+
if (authFileSrc != null && authFileSrc.trim().length() > 0) {
75+
// if its a yaml def convert to json first
76+
if (!authFileSrc.trim().startsWith("{")) {
77+
// convert yaml to json to validate
78+
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
79+
Object obj = yamlReader.readValue(authFileSrc, Object.class);
80+
81+
authRefNode =
82+
jsonWriter.readTree(new JSONObject(jsonWriter.writeValueAsString(obj)).toString());
83+
} else {
84+
authRefNode = jsonWriter.readTree(new JSONObject(authFileSrc).toString());
85+
}
86+
87+
JsonNode refAuth = authRefNode.get("auth");
88+
if (refAuth != null) {
89+
for (final JsonNode nodeEle : refAuth) {
90+
authDefinitions.add(mapper.treeToValue(nodeEle, AuthDefinition.class));
91+
}
92+
} else {
93+
logger.error("Unable to find auth definitions in reference file: {}", authFileSrc);
94+
}
95+
96+
} else {
97+
logger.error("Unable to load auth defs reference file: {}", authFileSrc);
98+
}
99+
}
100+
auth.setAuthDefs(authDefinitions);
101+
return auth;
102+
}
103+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ private void addDefaultDeserializers() {
121121
StateExecTimeout.class, new StateExecTimeoutDeserializer(workflowPropertySource));
122122
addDeserializer(Errors.class, new ErrorsDeserializer(workflowPropertySource));
123123
addDeserializer(ContinueAs.class, new ContinueAsDeserializer(workflowPropertySource));
124+
addDeserializer(Auth.class, new AuthDeserializer(workflowPropertySource));
124125
}
125126

126127
public ExtensionSerializer getExtensionSerializer() {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ public void serialize(Workflow workflow, JsonGenerator gen, SerializerProvider p
167167
gen.writeObjectField("timeouts", workflow.getTimeouts());
168168
}
169169

170-
if (workflow.getAuth() != null) {
171-
gen.writeObjectField("auth", workflow.getAuth());
170+
if (workflow.getAuth() != null && !workflow.getAuth().getAuthDefs().isEmpty()) {
171+
gen.writeObjectField("auth", workflow.getAuth().getAuthDefs());
172172
}
173173

174174
if (workflow.getStates() != null && !workflow.getStates().isEmpty()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2022-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+
17+
package io.serverlessworkflow.api.workflow;
18+
19+
import io.serverlessworkflow.api.auth.AuthDefinition;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
public class Auth {
24+
private String refValue;
25+
private List<AuthDefinition> authDefs;
26+
27+
public Auth() {}
28+
29+
public Auth(AuthDefinition authDef) {
30+
this.authDefs = new ArrayList<>();
31+
this.authDefs.add(authDef);
32+
}
33+
34+
public Auth(List<AuthDefinition> authDefs) {
35+
this.authDefs = authDefs;
36+
}
37+
38+
public Auth(String refValue) {
39+
this.refValue = refValue;
40+
}
41+
42+
public String getRefValue() {
43+
return refValue;
44+
}
45+
46+
public void setRefValue(String refValue) {
47+
this.refValue = refValue;
48+
}
49+
50+
public List<AuthDefinition> getAuthDefs() {
51+
return authDefs;
52+
}
53+
54+
public void setAuthDefs(List<AuthDefinition> authDefs) {
55+
this.authDefs = authDefs;
56+
}
57+
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@
100100
"$ref": "timeouts/timeoutsdef.json"
101101
},
102102
"auth": {
103-
"$ref": "auth/auth.json"
103+
"type": "object",
104+
"existingJavaType": "io.serverlessworkflow.api.workflow.Auth",
105+
"description": "Workflow Auth definitions"
104106
},
105107
"states": {
106108
"type": "array",

‎api/src/test/java/io/serverlessworkflow/api/test/MarkupToWorkflowTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ public void testAuthBasic(String workflowLocation) {
627627
assertNotNull(workflow.getName());
628628

629629
assertNotNull(workflow.getAuth());
630-
AuthDefinition auth = workflow.getAuth();
630+
AuthDefinition auth = workflow.getAuth().getAuthDefs().get(0);
631631
assertNotNull(auth.getName());
632632
assertEquals("authname", auth.getName());
633633
assertNotNull(auth.getScheme());
@@ -647,7 +647,7 @@ public void testAuthBearer(String workflowLocation) {
647647
assertNotNull(workflow.getName());
648648

649649
assertNotNull(workflow.getAuth());
650-
AuthDefinition auth = workflow.getAuth();
650+
AuthDefinition auth = workflow.getAuth().getAuthDefs().get(0);
651651
assertNotNull(auth.getName());
652652
assertEquals("authname", auth.getName());
653653
assertNotNull(auth.getScheme());
@@ -666,7 +666,7 @@ public void testAuthOAuth(String workflowLocation) {
666666
assertNotNull(workflow.getName());
667667

668668
assertNotNull(workflow.getAuth());
669-
AuthDefinition auth = workflow.getAuth();
669+
AuthDefinition auth = workflow.getAuth().getAuthDefs().get(0);
670670
assertNotNull(auth.getName());
671671
assertEquals("authname", auth.getName());
672672
assertNotNull(auth.getScheme());

‎api/src/test/java/io/serverlessworkflow/api/test/WorkflowToMarkupTest.java

+20-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package io.serverlessworkflow.api.test;
1717

1818
import static io.serverlessworkflow.api.states.DefaultState.Type.SLEEP;
19-
import static org.junit.jupiter.api.Assertions.*;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2022

2123
import io.serverlessworkflow.api.Workflow;
2224
import io.serverlessworkflow.api.auth.AuthDefinition;
@@ -29,6 +31,7 @@
2931
import io.serverlessworkflow.api.schedule.Schedule;
3032
import io.serverlessworkflow.api.start.Start;
3133
import io.serverlessworkflow.api.states.SleepState;
34+
import io.serverlessworkflow.api.workflow.Auth;
3235
import io.serverlessworkflow.api.workflow.Events;
3336
import io.serverlessworkflow.api.workflow.Functions;
3437
import java.util.Arrays;
@@ -162,22 +165,24 @@ public void testAuth() {
162165
.withVersion("1.0")
163166
.withStart(new Start())
164167
.withAuth(
165-
new AuthDefinition()
166-
.withName("authname")
167-
.withScheme(AuthDefinition.Scheme.BASIC)
168-
.withBasicauth(
169-
new BasicAuthDefinition()
170-
.withUsername("testuser")
171-
.withPassword("testPassword")));
168+
new Auth(
169+
new AuthDefinition()
170+
.withName("authname")
171+
.withScheme(AuthDefinition.Scheme.BASIC)
172+
.withBasicauth(
173+
new BasicAuthDefinition()
174+
.withUsername("testuser")
175+
.withPassword("testPassword"))));
172176

173177
assertNotNull(workflow);
174178
assertNotNull(workflow.getAuth());
175-
assertNotNull(workflow.getAuth().getName());
176-
assertEquals("authname", workflow.getAuth().getName());
177-
assertNotNull(workflow.getAuth().getScheme());
178-
assertEquals("basic", workflow.getAuth().getScheme().value());
179-
assertNotNull(workflow.getAuth().getBasicauth());
180-
assertEquals("testuser", workflow.getAuth().getBasicauth().getUsername());
181-
assertEquals("testPassword", workflow.getAuth().getBasicauth().getPassword());
179+
assertNotNull(workflow.getAuth().getAuthDefs().get(0));
180+
assertEquals("authname", workflow.getAuth().getAuthDefs().get(0).getName());
181+
assertNotNull(workflow.getAuth().getAuthDefs().get(0).getScheme());
182+
assertEquals("basic", workflow.getAuth().getAuthDefs().get(0).getScheme().value());
183+
assertNotNull(workflow.getAuth().getAuthDefs().get(0).getBasicauth());
184+
assertEquals("testuser", workflow.getAuth().getAuthDefs().get(0).getBasicauth().getUsername());
185+
assertEquals(
186+
"testPassword", workflow.getAuth().getAuthDefs().get(0).getBasicauth().getPassword());
182187
}
183188
}
+12-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
{
2-
"id" : "test-workflow",
3-
"name" : "test-workflow-name",
4-
"version" : "1.0",
5-
"auth" : {
6-
"name" : "authname",
7-
"scheme" : "basic",
8-
"properties" : {
9-
"username" : "testuser",
10-
"password" : "testpassword"
2+
"id": "test-workflow",
3+
"name": "test-workflow-name",
4+
"version": "1.0",
5+
"auth": [
6+
{
7+
"name": "authname",
8+
"scheme": "basic",
9+
"properties": {
10+
"username": "testuser",
11+
"password": "testpassword"
12+
}
1113
}
12-
}
14+
]
1315
}

‎api/src/test/resources/features/authbasic.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ id: test-workflow
22
name: test-workflow-name
33
version: '1.0'
44
auth:
5-
name: authname
6-
scheme: basic
7-
properties:
8-
username: testuser
9-
password: testpassword
5+
- name: authname
6+
scheme: basic
7+
properties:
8+
username: testuser
9+
password: testpassword
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
2-
"id" : "test-workflow",
3-
"name" : "test-workflow-name",
4-
"version" : "1.0",
5-
"auth" : {
6-
"name" : "authname",
7-
"scheme" : "bearer",
8-
"properties" : {
9-
"token" : "testtoken"
2+
"id": "test-workflow",
3+
"name": "test-workflow-name",
4+
"version": "1.0",
5+
"auth": [
6+
{
7+
"name": "authname",
8+
"scheme": "bearer",
9+
"properties": {
10+
"token": "testtoken"
11+
}
1012
}
11-
}
13+
]
1214
}

‎api/src/test/resources/features/authbearer.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ id: test-workflow
22
name: test-workflow-name
33
version: '1.0'
44
auth:
5-
name: authname
6-
scheme: bearer
7-
properties:
8-
token: testtoken
5+
- name: authname
6+
scheme: bearer
7+
properties:
8+
token: testtoken

‎api/src/test/resources/features/authoauth.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id" : "test-workflow",
33
"name" : "test-workflow-name",
44
"version" : "1.0",
5-
"auth" : {
5+
"auth" : [{
66
"name" : "authname",
77
"scheme" : "oauth2",
88
"properties" : {
@@ -11,5 +11,5 @@
1111
"clientId": "${ $SECRETS.clientid }",
1212
"clientSecret": "${ $SECRETS.clientsecret }"
1313
}
14-
}
14+
}]
1515
}

‎api/src/test/resources/features/authoauth.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ id: test-workflow
22
name: test-workflow-name
33
version: '1.0'
44
auth:
5-
name: authname
6-
scheme: oauth2
7-
properties:
8-
authority: testauthority
9-
grantType: clientCredentials
10-
clientId: "${ $SECRETS.clientid }"
11-
clientSecret: "${ $SECRETS.clientsecret }"
5+
- name: authname
6+
scheme: oauth2
7+
properties:
8+
authority: testauthority
9+
grantType: clientCredentials
10+
clientId: "${ $SECRETS.clientid }"
11+
clientSecret: "${ $SECRETS.clientsecret }"

‎validation/src/main/java/io/serverlessworkflow/validation/WorkflowValidatorImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public List<ValidationError> validate() {
330330
ValidationError.WORKFLOW_VALIDATION);
331331
}
332332

333-
if (haveFunctionDefinition(
333+
if (!haveFunctionDefinition(
334334
callbackState.getAction().getFunctionRef().getRefName(), functions)) {
335335
addValidationError(
336336
"CallbackState action function ref does not reference a defined workflow function definition",

0 commit comments

Comments
 (0)
Please sign in to comment.