Skip to content

Commit f2de030

Browse files
fjtiradomcruzdev
authored andcommitted
Fix authorization fluent
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent 9ba0131 commit f2de030

File tree

6 files changed

+96
-55
lines changed

6 files changed

+96
-55
lines changed

experimental/fluent/func/src/test/java/io/serverlessworkflow/fluent/func/FuncDSLTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,13 @@ void get_named_with_authentication_uses_auth_policy() {
266266
assertEquals("GET", http.getWith().getMethod());
267267
assertEquals(
268268
"http://service/api/users",
269-
http.getWith().getEndpoint().getUriTemplate().getLiteralUri().toString(),
269+
http.getWith()
270+
.getEndpoint()
271+
.getEndpointConfiguration()
272+
.getUri()
273+
.getLiteralEndpointURI()
274+
.getLiteralUri()
275+
.toString(),
270276
"endpoint should be set from get(name, endpoint, auth)");
271277

272278
assertNotNull(
@@ -371,7 +377,13 @@ void post_named_with_authentication() {
371377
assertEquals("POST", http.getWith().getMethod());
372378
assertEquals(
373379
"https://orders.example.com/api/orders",
374-
http.getWith().getEndpoint().getUriTemplate().getLiteralUri().toString());
380+
http.getWith()
381+
.getEndpoint()
382+
.getEndpointConfiguration()
383+
.getUri()
384+
.getLiteralEndpointURI()
385+
.getLiteralUri()
386+
.toString());
375387
assertEquals(body, http.getWith().getBody());
376388

377389
assertNotNull(http.getWith().getEndpoint().getEndpointConfiguration().getAuthentication());
@@ -409,7 +421,13 @@ void call_with_preconfigured_http_spec() {
409421
assertEquals("POST", http.getWith().getMethod());
410422
assertEquals(
411423
"http://service/api",
412-
http.getWith().getEndpoint().getUriTemplate().getLiteralUri().toString());
424+
http.getWith()
425+
.getEndpoint()
426+
.getEndpointConfiguration()
427+
.getUri()
428+
.getLiteralEndpointURI()
429+
.getLiteralUri()
430+
.toString());
413431
assertEquals(
414432
"svc-auth",
415433
http.getWith()

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/spi/CallHttpTaskFluent.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,21 @@ default SELF endpoint(String expr, Consumer<ReferenceableAuthenticationPolicyBui
8484
new ReferenceableAuthenticationPolicyBuilder();
8585
auth.accept(policy);
8686

87-
final Endpoint endpoint = EndpointUtil.fromString(expr);
88-
endpoint.setEndpointConfiguration(
89-
new EndpointConfiguration().withAuthentication(policy.build()));
90-
91-
((CallHTTP) this.self().getTask()).getWith().setEndpoint(endpoint);
87+
((CallHTTP) this.self().getTask())
88+
.getWith()
89+
.setEndpoint(EndpointUtil.fromString(expr, policy.build()));
9290
return self();
9391
}
9492

9593
default SELF endpoint(String expr, String authUse) {
96-
final Endpoint endpoint = EndpointUtil.fromString(expr);
97-
endpoint.withEndpointConfiguration(
98-
new EndpointConfiguration()
99-
.withAuthentication(
94+
((CallHTTP) this.self().getTask())
95+
.getWith()
96+
.setEndpoint(
97+
EndpointUtil.fromString(
98+
expr,
10099
new ReferenceableAuthenticationPolicy()
101100
.withAuthenticationPolicyReference(
102101
new AuthenticationPolicyReference(authUse))));
103-
((CallHTTP) this.self().getTask()).getWith().setEndpoint(endpoint);
104102
return self();
105103
}
106104

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/spi/CallOpenAPITaskFluent.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.serverlessworkflow.api.types.EndpointUri;
2222
import io.serverlessworkflow.api.types.ExternalResource;
2323
import io.serverlessworkflow.api.types.OpenAPIArguments;
24+
import io.serverlessworkflow.api.types.ReferenceableAuthenticationPolicy;
2425
import io.serverlessworkflow.api.types.UriTemplate;
2526
import io.serverlessworkflow.fluent.spec.ReferenceableAuthenticationPolicyBuilder;
2627
import io.serverlessworkflow.fluent.spec.TaskBaseBuilder;
@@ -50,7 +51,9 @@ default CallOpenAPI build() {
5051
* @see #document(String, AuthenticationConfigurer) for setting a document with authentication
5152
*/
5253
default SELF document(String uri) {
53-
((CallOpenAPI) this.self().getTask()).getWith().setDocument(EndpointUtil.externalResource(uri));
54+
((CallOpenAPI) this.self().getTask())
55+
.getWith()
56+
.setDocument(new ExternalResource().withEndpoint(EndpointUtil.fromString(uri)));
5457
return self();
5558
}
5659

@@ -68,41 +71,33 @@ default SELF document(String uri, AuthenticationConfigurer authenticationConfigu
6871
final ReferenceableAuthenticationPolicyBuilder policy =
6972
new ReferenceableAuthenticationPolicyBuilder();
7073
authenticationConfigurer.accept(policy);
71-
((CallOpenAPI) this.self().getTask()).getWith().setAuthentication(policy.build());
74+
ReferenceableAuthenticationPolicy auth = policy.build();
75+
((CallOpenAPI) this.self().getTask()).getWith().setAuthentication(auth);
7276
((CallOpenAPI) this.self().getTask())
7377
.getWith()
74-
.setDocument(
75-
new ExternalResource()
76-
.withEndpoint(
77-
EndpointUtil.externalResource(uri)
78-
.getEndpoint()
79-
.withEndpointConfiguration(
80-
new EndpointConfiguration()
81-
.withUri(new EndpointUri().withExpressionEndpointURI(uri))
82-
.withAuthentication(policy.build()))));
78+
.setDocument(new ExternalResource().withEndpoint(EndpointUtil.fromString(uri, auth)));
8379
return self();
8480
}
8581

8682
default SELF document(URI uri, AuthenticationConfigurer authenticationConfigurer) {
8783
final ReferenceableAuthenticationPolicyBuilder policy =
8884
new ReferenceableAuthenticationPolicyBuilder();
8985
authenticationConfigurer.accept(policy);
90-
91-
((CallOpenAPI) this.self().getTask()).getWith().setAuthentication(policy.build());
86+
ReferenceableAuthenticationPolicy auth = policy.build();
87+
((CallOpenAPI) this.self().getTask()).getWith().setAuthentication(auth);
9288
((CallOpenAPI) this.self().getTask())
9389
.getWith()
9490
.setDocument(
9591
new ExternalResource()
9692
.withEndpoint(
9793
new Endpoint()
98-
.withUriTemplate(new UriTemplate().withLiteralUri(uri))
9994
.withEndpointConfiguration(
10095
new EndpointConfiguration()
10196
.withUri(
10297
new EndpointUri()
10398
.withLiteralEndpointURI(
10499
new UriTemplate().withLiteralUri(uri)))
105-
.withAuthentication(policy.build()))));
100+
.withAuthentication(auth))));
106101
return self();
107102
}
108103

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/spi/EndpointUtil.java

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package io.serverlessworkflow.fluent.spec.spi;
1717

1818
import io.serverlessworkflow.api.types.Endpoint;
19-
import io.serverlessworkflow.api.types.ExternalResource;
19+
import io.serverlessworkflow.api.types.EndpointConfiguration;
20+
import io.serverlessworkflow.api.types.EndpointUri;
21+
import io.serverlessworkflow.api.types.ReferenceableAuthenticationPolicy;
2022
import io.serverlessworkflow.api.types.UriTemplate;
2123
import java.net.URI;
2224
import java.util.Objects;
@@ -25,27 +27,48 @@ public final class EndpointUtil {
2527

2628
private EndpointUtil() {}
2729

28-
public static Endpoint fromString(String expr) {
29-
Objects.requireNonNull(expr, "Endpoint expression cannot be null");
30-
String trimmed = expr.trim();
30+
public static Endpoint fromString(String uri) {
31+
return fromString(uri, null);
32+
}
33+
34+
public static Endpoint fromString(String uri, ReferenceableAuthenticationPolicy auth) {
35+
Objects.requireNonNull(uri, "Endpoint URI cannot be null");
36+
String trimmed = uri.trim();
3137
Endpoint endpoint = new Endpoint();
32-
if (isUrlLike(trimmed)) {
33-
UriTemplate template = new UriTemplate();
34-
if (trimmed.indexOf('{') >= 0 || trimmed.indexOf('}') >= 0) {
35-
template.setLiteralUriTemplate(trimmed);
36-
} else {
37-
template.setLiteralUri(URI.create(trimmed));
38-
}
39-
endpoint.setUriTemplate(template);
40-
return endpoint;
41-
}
4238

43-
// Let the runtime engine to verify if it's a valid jq expression since ${} it's not the only
44-
// way of checking it.
45-
endpoint.setRuntimeExpression(expr);
39+
if (auth != null) {
40+
endpoint.setEndpointConfiguration(
41+
new EndpointConfiguration(buildEndpointUri(trimmed)).withAuthentication(auth));
42+
} else if (isUrlLike(trimmed)) {
43+
endpoint.setUriTemplate(buildUriTemplate(trimmed));
44+
} else {
45+
// Let the runtime engine to verify if it's a valid jq expression since ${} it's not the only
46+
// way of checking it.
47+
endpoint.setRuntimeExpression(uri);
48+
}
4649
return endpoint;
4750
}
4851

52+
private static EndpointUri buildEndpointUri(String uri) {
53+
EndpointUri endpointUri = new EndpointUri();
54+
if (isUrlLike(uri)) {
55+
endpointUri.setLiteralEndpointURI(buildUriTemplate(uri));
56+
} else {
57+
endpointUri.setExpressionEndpointURI(uri);
58+
}
59+
return endpointUri;
60+
}
61+
62+
private static UriTemplate buildUriTemplate(String trimmed) {
63+
UriTemplate template = new UriTemplate();
64+
if (trimmed.indexOf('{') >= 0 || trimmed.indexOf('}') >= 0) {
65+
template.setLiteralUriTemplate(trimmed);
66+
} else {
67+
template.setLiteralUri(URI.create(trimmed));
68+
}
69+
return template;
70+
}
71+
4972
private static boolean isUrlLike(String value) {
5073
// same idea as UriTemplate.literalUriTemplate_Pattern: ^[A-Za-z][A-Za-z0-9+\\-.]*://.*
5174
int idx = value.indexOf("://");
@@ -64,8 +87,4 @@ private static boolean isUrlLike(String value) {
6487
}
6588
return true;
6689
}
67-
68-
public static ExternalResource externalResource(String expr) {
69-
return new ExternalResource().withEndpoint(fromString(expr));
70-
}
7190
}

fluent/spec/src/test/java/io/serverlessworkflow/fluent/spec/dsl/CallHttpAuthDslTest.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ void when_call_http_with_basic_auth_on_endpoint_expr() {
4747
assertThat(wf.getDo().get(0).getTask().getCallTask().get()).isNotNull();
4848

4949
// Endpoint expression is set
50-
assertThat(args.getEndpoint().getRuntimeExpression()).isEqualTo(EXPR_ENDPOINT);
50+
assertThat(args.getEndpoint().getEndpointConfiguration().getUri().getExpressionEndpointURI())
51+
.isEqualTo(EXPR_ENDPOINT);
5152

5253
// Auth populated: BASIC (others null)
5354
var auth =
@@ -83,7 +84,8 @@ void when_call_http_with_bearer_auth_on_endpoint_expr() {
8384

8485
var args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith();
8586

86-
assertThat(args.getEndpoint().getRuntimeExpression()).isEqualTo(EXPR_ENDPOINT);
87+
assertThat(args.getEndpoint().getEndpointConfiguration().getUri().getExpressionEndpointURI())
88+
.isEqualTo(EXPR_ENDPOINT);
8789

8890
var auth =
8991
args.getEndpoint().getEndpointConfiguration().getAuthentication().getAuthenticationPolicy();
@@ -112,7 +114,8 @@ void when_call_http_with_digest_auth_on_endpoint_expr() {
112114

113115
var args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith();
114116

115-
assertThat(args.getEndpoint().getRuntimeExpression()).isEqualTo(EXPR_ENDPOINT);
117+
assertThat(args.getEndpoint().getEndpointConfiguration().getUri().getExpressionEndpointURI())
118+
.isEqualTo(EXPR_ENDPOINT);
116119

117120
var auth =
118121
args.getEndpoint().getEndpointConfiguration().getAuthentication().getAuthenticationPolicy();
@@ -158,7 +161,8 @@ void when_call_http_with_oidc_auth_on_endpoint_expr_with_client() {
158161

159162
var args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith();
160163

161-
assertThat(args.getEndpoint().getRuntimeExpression()).isEqualTo(EXPR_ENDPOINT);
164+
assertThat(args.getEndpoint().getEndpointConfiguration().getUri().getExpressionEndpointURI())
165+
.isEqualTo(EXPR_ENDPOINT);
162166

163167
var auth =
164168
args.getEndpoint().getEndpointConfiguration().getAuthentication().getAuthenticationPolicy();
@@ -203,7 +207,8 @@ void when_call_http_with_oauth2_alias_on_endpoint_expr_without_client() {
203207

204208
var args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith();
205209

206-
assertThat(args.getEndpoint().getRuntimeExpression()).isEqualTo(EXPR_ENDPOINT);
210+
assertThat(args.getEndpoint().getEndpointConfiguration().getUri().getExpressionEndpointURI())
211+
.isEqualTo(EXPR_ENDPOINT);
207212

208213
var auth =
209214
args.getEndpoint().getEndpointConfiguration().getAuthentication().getAuthenticationPolicy();

fluent/spec/src/test/java/io/serverlessworkflow/fluent/spec/dsl/CallOpenApiDslTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ void when_call_openapi_with_basic_auth_on_document_expr() {
5555
// Document and endpoint expression
5656
assertThat(with.getDocument()).isNotNull();
5757
assertThat(with.getDocument().getEndpoint()).isNotNull();
58-
assertThat(with.getDocument().getEndpoint().getRuntimeExpression()).isEqualTo(EXPR_DOCUMENT);
58+
assertThat(
59+
with.getDocument()
60+
.getEndpoint()
61+
.getEndpointConfiguration()
62+
.getUri()
63+
.getExpressionEndpointURI())
64+
.isEqualTo(EXPR_DOCUMENT);
5965

6066
// Endpoint configuration URI expression
6167
var endpointConfig = with.getDocument().getEndpoint().getEndpointConfiguration();

0 commit comments

Comments
 (0)