Skip to content

Commit e8242d8

Browse files
committed
Validate task's auth being referencing a not defined use.authentications
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent a165007 commit e8242d8

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/HttpExecutorBuilder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package io.serverlessworkflow.impl.executors.http;
1717

1818
import io.serverlessworkflow.api.types.ReferenceableAuthenticationPolicy;
19+
import io.serverlessworkflow.api.types.Use;
20+
import io.serverlessworkflow.api.types.UseAuthentications;
1921
import io.serverlessworkflow.impl.WorkflowDefinition;
2022
import io.serverlessworkflow.impl.WorkflowUtils;
2123
import io.serverlessworkflow.impl.WorkflowValueResolver;
@@ -41,10 +43,25 @@ private HttpExecutorBuilder(WorkflowDefinition definition) {
4143
}
4244

4345
public HttpExecutorBuilder withAuth(ReferenceableAuthenticationPolicy policy) {
46+
checkAuthentication(policy);
4447
this.policy = policy;
4548
return this;
4649
}
4750

51+
private void checkAuthentication(ReferenceableAuthenticationPolicy policy) {
52+
if (policy == null || policy.getAuthenticationPolicyReference() == null) {
53+
return;
54+
}
55+
String name = policy.getAuthenticationPolicyReference().getUse();
56+
Use use = definition.workflow().getUse();
57+
UseAuthentications authentications = use == null ? null : use.getAuthentications();
58+
if (authentications == null || !authentications.getAdditionalProperties().containsKey(name)) {
59+
throw new IllegalArgumentException(
60+
String.format(
61+
"Authentication '%s' is referenced but not defined in use.authentications.", name));
62+
}
63+
}
64+
4865
public HttpExecutorBuilder withBody(Object body) {
4966
this.body = body;
5067
return this;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.impl.test;
17+
18+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
19+
20+
import io.serverlessworkflow.api.types.Workflow;
21+
import io.serverlessworkflow.fluent.spec.WorkflowBuilder;
22+
import io.serverlessworkflow.fluent.spec.dsl.DSL;
23+
import io.serverlessworkflow.impl.WorkflowApplication;
24+
import java.net.URI;
25+
import org.junit.jupiter.api.Test;
26+
27+
class UndefinedAuthReferenceTest {
28+
29+
@Test
30+
void httpWithUndefinedAuthReferenceShouldFailAtBuildTime() {
31+
Workflow workflow =
32+
WorkflowBuilder.workflow("undefined-auth-ref-http", "test", "0.1.0")
33+
.tasks(
34+
DSL.call(
35+
DSL.http()
36+
.method("GET")
37+
.uri(
38+
URI.create("http://localhost:10110/dir/index.html"),
39+
a -> a.use("sampleDigest"))))
40+
.build();
41+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
42+
assertThatThrownBy(() -> app.workflowDefinition(workflow))
43+
.isInstanceOf(IllegalArgumentException.class)
44+
.hasMessageContaining("sampleDigest")
45+
.hasMessageContaining("not defined in use.authentications");
46+
}
47+
}
48+
49+
@Test
50+
void openApiWithUndefinedAuthReferenceShouldFailAtBuildTime() {
51+
Workflow workflow =
52+
WorkflowBuilder.workflow("undefined-auth-ref-openapi", "test", "0.1.0")
53+
.tasks(
54+
DSL.call(
55+
DSL.openapi()
56+
.document("http://localhost:10110/openapi.json")
57+
.operation("getPet")
58+
.authentication(a -> a.use("sampleDigest"))))
59+
.build();
60+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
61+
assertThatThrownBy(() -> app.workflowDefinition(workflow))
62+
.isInstanceOf(IllegalArgumentException.class)
63+
.hasMessageContaining("sampleDigest")
64+
.hasMessageContaining("not defined in use.authentications");
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)