Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.listen;
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.toOne;
import static io.serverlessworkflow.fluent.spec.dsl.DSL.auth;
import static io.serverlessworkflow.fluent.spec.dsl.DSL.use;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -263,7 +264,7 @@ void get_convenience_creates_http_get() {
void get_named_with_authentication_uses_auth_policy() {
Workflow wf =
FuncWorkflowBuilder.workflow("http-get-auth")
.tasks(get("fetchUsers", "http://service/api/users", auth("user-service-auth")))
.tasks(get("fetchUsers", "http://service/api/users", use("user-service-auth")))
.build();

List<TaskItem> items = wf.getDo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,44 @@
import java.util.function.Consumer;

public class ReferenceableAuthenticationPolicyBuilder {
final AuthenticationPolicyUnion authenticationPolicy;
final AuthenticationPolicyReference authenticationPolicyReference;
private AuthenticationPolicyUnion authenticationPolicy;
private AuthenticationPolicyReference authenticationPolicyReference;

public ReferenceableAuthenticationPolicyBuilder() {
this.authenticationPolicy = new AuthenticationPolicyUnion();
this.authenticationPolicyReference = new AuthenticationPolicyReference();
}
public ReferenceableAuthenticationPolicyBuilder() {}

public ReferenceableAuthenticationPolicyBuilder basic(
Consumer<BasicAuthenticationPolicyBuilder> basicConsumer) {
final BasicAuthenticationPolicyBuilder basicAuthenticationPolicyBuilder =
new BasicAuthenticationPolicyBuilder();
basicConsumer.accept(basicAuthenticationPolicyBuilder);
this.authenticationPolicy.setBasicAuthenticationPolicy(
basicAuthenticationPolicyBuilder.build());
final BasicAuthenticationPolicyBuilder builder = new BasicAuthenticationPolicyBuilder();
basicConsumer.accept(builder);
this.authenticationPolicy =
new AuthenticationPolicyUnion().withBasicAuthenticationPolicy(builder.build());
return this;
}

public ReferenceableAuthenticationPolicyBuilder bearer(
Consumer<BearerAuthenticationPolicyBuilder> bearerConsumer) {
final BearerAuthenticationPolicyBuilder bearerAuthenticationPolicyBuilder =
new BearerAuthenticationPolicyBuilder();
bearerConsumer.accept(bearerAuthenticationPolicyBuilder);
this.authenticationPolicy.setBearerAuthenticationPolicy(
bearerAuthenticationPolicyBuilder.build());
final BearerAuthenticationPolicyBuilder builder = new BearerAuthenticationPolicyBuilder();
bearerConsumer.accept(builder);
this.authenticationPolicy =
new AuthenticationPolicyUnion().withBearerAuthenticationPolicy(builder.build());
return this;
}

public ReferenceableAuthenticationPolicyBuilder digest(
Consumer<DigestAuthenticationPolicyBuilder> digestConsumer) {
final DigestAuthenticationPolicyBuilder digestAuthenticationPolicyBuilder =
new DigestAuthenticationPolicyBuilder();
digestConsumer.accept(digestAuthenticationPolicyBuilder);
this.authenticationPolicy.setDigestAuthenticationPolicy(
digestAuthenticationPolicyBuilder.build());
final DigestAuthenticationPolicyBuilder builder = new DigestAuthenticationPolicyBuilder();
digestConsumer.accept(builder);
this.authenticationPolicy =
new AuthenticationPolicyUnion().withDigestAuthenticationPolicy(builder.build());
return this;
}

public ReferenceableAuthenticationPolicyBuilder oauth2(
Consumer<OAuth2AuthenticationPolicyBuilder> oauth2Consumer) {
final OAuth2AuthenticationPolicyBuilder oauth2AuthenticationPolicyBuilder =
new OAuth2AuthenticationPolicyBuilder();
oauth2Consumer.accept(oauth2AuthenticationPolicyBuilder);
this.authenticationPolicy.setOAuth2AuthenticationPolicy(
oauth2AuthenticationPolicyBuilder.build());
final OAuth2AuthenticationPolicyBuilder builder = new OAuth2AuthenticationPolicyBuilder();
oauth2Consumer.accept(builder);
this.authenticationPolicy =
new AuthenticationPolicyUnion().withOAuth2AuthenticationPolicy(builder.build());
return this;
}

Expand All @@ -74,12 +67,13 @@ public ReferenceableAuthenticationPolicyBuilder openIDConnect(
final OpenIdConnectAuthenticationPolicyBuilder builder =
new OpenIdConnectAuthenticationPolicyBuilder();
openIdConnectConsumer.accept(builder);
this.authenticationPolicy.setOpenIdConnectAuthenticationPolicy(builder.build());
this.authenticationPolicy =
new AuthenticationPolicyUnion().withOpenIdConnectAuthenticationPolicy(builder.build());
return this;
}

public ReferenceableAuthenticationPolicyBuilder use(String use) {
this.authenticationPolicyReference.setUse(use);
this.authenticationPolicyReference = new AuthenticationPolicyReference().withUse(use);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public static Consumer<TryTaskBuilder.CatchErrorsBuilder> errorFilter(
* @param authName the name of a reusable authentication policy
* @return an {@link AuthenticationConfigurer} that sets {@code use(authName)}
*/
public static AuthenticationConfigurer auth(String authName) {
public static AuthenticationConfigurer use(String authName) {
return a -> a.use(authName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.serverlessworkflow.api.types.CallHTTP;
import io.serverlessworkflow.api.types.Endpoint;
import io.serverlessworkflow.api.types.EndpointConfiguration;
import io.serverlessworkflow.api.types.EndpointUri;
import io.serverlessworkflow.api.types.HTTPArguments;
import io.serverlessworkflow.api.types.HTTPHeaders;
import io.serverlessworkflow.api.types.HTTPQuery;
Expand Down Expand Up @@ -59,14 +60,17 @@ default SELF endpoint(URI endpoint) {
default SELF endpoint(URI endpoint, Consumer<ReferenceableAuthenticationPolicyBuilder> auth) {
final ReferenceableAuthenticationPolicyBuilder policy =
new ReferenceableAuthenticationPolicyBuilder();
final UriTemplate uriTemplate = new UriTemplate().withLiteralUri(endpoint);
auth.accept(policy);
((CallHTTP) this.self().getTask())
.getWith()
.setEndpoint(
new Endpoint()
.withEndpointConfiguration(
new EndpointConfiguration().withAuthentication(policy.build()))
.withUriTemplate(new UriTemplate().withLiteralUri(endpoint)));
new EndpointConfiguration()
.withUri(new EndpointUri().withLiteralEndpointURI(uriTemplate))
.withAuthentication(policy.build()))
.withUriTemplate(uriTemplate));
return self();
}

Expand Down
5 changes: 5 additions & 0 deletions impl/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@
<groupId>com.cronutils</groupId>
<artifactId>cron-utils</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.serverlessworkflow.impl.schema.SchemaValidator;
import io.serverlessworkflow.impl.schema.SchemaValidatorFactory;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -221,11 +222,42 @@ public static final String checkSecret(
.orElseThrow(() -> new IllegalStateException("Secret " + secretName + " does not exist"));
}

public static URI concatURI(URI uri, String pathToAppend) {
return uri.getPath().endsWith("/")
? uri.resolve(pathToAppend)
: URI.create(
uri.toString() + (pathToAppend.startsWith("/") ? pathToAppend : "/" + pathToAppend));
public static URI concatURI(URI base, String pathToAppend) {
if (!isValid(pathToAppend)) {
return base;
}

final URI child = URI.create(pathToAppend);
if (child.isAbsolute()) {
return child;
}

String basePath = base.getPath();
if (!isValid(basePath)) {
basePath = "/";
} else if (!basePath.endsWith("/")) {
basePath = basePath + "/";
}

String relPath = child.getPath();
if (relPath == null) {
relPath = "";
}
while (relPath.startsWith("/")) {
relPath = relPath.substring(1);
}

String finalPath = basePath + relPath;

String query = child.getQuery();
String fragment = child.getFragment();

try {
return new URI(base.getScheme(), base.getAuthority(), finalPath, query, fragment);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(
"Failed to build combined URI from base=" + base + " and path=" + pathToAppend, e);
}
}

public static WorkflowValueResolver<URI> getURISupplier(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.net.URI;
import org.junit.jupiter.api.Test;

public class WorkflowUtilsTest {
@Test
void openApiServerWithTrailingSlashAndRootPath() {
URI base = URI.create("https://petstore3.swagger.io/api/v3/");
URI path = URI.create("/pet/findByStatus");

URI result = WorkflowUtils.concatURI(base, path.toString());

assertEquals("https://petstore3.swagger.io/api/v3/pet/findByStatus", result.toString());
}

@Test
void openApiServerWithoutTrailingSlashAndRootPath() {
URI base = URI.create("https://petstore3.swagger.io/api/v3");
URI path = URI.create("/pet/findByStatus");

URI result = WorkflowUtils.concatURI(base, path.toString());

assertEquals("https://petstore3.swagger.io/api/v3/pet/findByStatus", result.toString());
}

@Test
void baseWithSlashAndRelativePath() {
URI base = URI.create("https://example.com/api/v1/");
URI path = URI.create("pets");

URI result = WorkflowUtils.concatURI(base, path.toString());

assertEquals("https://example.com/api/v1/pets", result.toString());
}

@Test
void baseWithoutPathAndRootPath() {
URI base = URI.create("https://example.com");
URI path = URI.create("/pets");

URI result = WorkflowUtils.concatURI(base, path.toString());

assertEquals("https://example.com/pets", result.toString());
}

@Test
void absolutePathOverridesBase() {
URI base = URI.create("https://example.com/api");
URI path = URI.create("https://other.example.com/foo");

URI result = WorkflowUtils.concatURI(base, path.toString());

assertEquals("https://other.example.com/foo", result.toString());
}

@Test
void queryAndFragmentAreTakenFromPath() {
URI base = URI.create("https://example.com/api/v1");
URI path = URI.create("/pets?status=available#top");

URI result = WorkflowUtils.concatURI(base, path.toString());

assertEquals("https://example.com/api/v1/pets?status=available#top", result.toString());
}
}
11 changes: 6 additions & 5 deletions impl/http/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.serverlessworkflow</groupId>
Expand All @@ -8,9 +9,9 @@
<artifactId>serverlessworkflow-impl-http</artifactId>
<name>Serverless Workflow :: Impl :: HTTP</name>
<dependencies>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-impl-template-resolver</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-impl-template-resolver</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ private static WorkflowValueResolver<WebTarget> getTargetSupplier(
WorkflowValueResolver<URI> uriSupplier, WorkflowValueResolver<URI> pathSupplier) {
return (w, t, n) ->
HttpClientResolver.client(w, t)
.target(uriSupplier.apply(w, t, n).resolve(pathSupplier.apply(w, t, n)));
.target(
WorkflowUtils.concatURI(
uriSupplier.apply(w, t, n), pathSupplier.apply(w, t, n).toString()));
}
}
Loading