Skip to content

Commit 779a4da

Browse files
authored
supporting spring boot property for API TOKEN on workflow interceptor (#1452)
Signed-off-by: salaboy <[email protected]>
1 parent 7a4f7d3 commit 779a4da

File tree

9 files changed

+57
-9
lines changed

9 files changed

+57
-9
lines changed

dapr-spring/dapr-spring-boot-autoconfigure/src/main/java/io/dapr/spring/boot/autoconfigure/client/ClientPropertiesDaprConnectionDetails.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,10 @@ public Integer getHttpPort() {
4040
public Integer getGrpcPort() {
4141
return this.daprClientProperties.getGrpcPort();
4242
}
43+
44+
@Override
45+
public String getApiToken() {
46+
return this.daprClientProperties.getApiToken();
47+
}
48+
4349
}

dapr-spring/dapr-spring-boot-autoconfigure/src/main/java/io/dapr/spring/boot/autoconfigure/client/DaprClientAutoConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ DaprClientBuilder daprClientBuilder(DaprConnectionDetails daprConnectionDetails)
6868
builder.withPropertyOverride(Properties.GRPC_PORT, String.valueOf(grpcPort));
6969
}
7070

71+
String apiToken = daprConnectionDetails.getApiToken();
72+
if (apiToken != null) {
73+
builder.withPropertyOverride(Properties.API_TOKEN, apiToken);
74+
}
75+
7176
return builder;
7277
}
7378

@@ -145,6 +150,11 @@ protected Properties createPropertiesFromConnectionDetails(DaprConnectionDetails
145150
propertyOverrides.put(Properties.GRPC_PORT.getName(), String.valueOf(grpcPort));
146151
}
147152

153+
String apiToken = daprConnectionDetails.getApiToken();
154+
if (apiToken != null) {
155+
propertyOverrides.put(Properties.API_TOKEN.getName(), apiToken);
156+
}
157+
148158
return new Properties(propertyOverrides);
149159
}
150160

dapr-spring/dapr-spring-boot-autoconfigure/src/main/java/io/dapr/spring/boot/autoconfigure/client/DaprClientProperties.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
package io.dapr.spring.boot.autoconfigure.client;
1515

16-
import io.dapr.spring.data.DaprKeyValueAdapterResolver;
1716
import org.springframework.boot.context.properties.ConfigurationProperties;
1817

1918
@ConfigurationProperties(prefix = "dapr.client")
@@ -22,6 +21,7 @@ public class DaprClientProperties {
2221
private String grpcEndpoint;
2322
private Integer httpPort;
2423
private Integer grpcPort;
24+
private String apiToken;
2525

2626
/**
2727
* Constructs a {@link DaprClientProperties}.
@@ -35,12 +35,15 @@ public DaprClientProperties() {
3535
* @param grpcEndpoint grpc endpoint to interact with the Dapr Sidecar
3636
* @param httpPort http port to interact with the Dapr Sidecar
3737
* @param grpcPort grpc port to interact with the Dapr Sidecar
38+
* @param apiToken dapr API token to interact with the Dapr Sidecar
3839
*/
39-
public DaprClientProperties(String httpEndpoint, String grpcEndpoint, Integer httpPort, Integer grpcPort) {
40+
public DaprClientProperties(String httpEndpoint, String grpcEndpoint, Integer httpPort, Integer grpcPort,
41+
String apiToken) {
4042
this.httpEndpoint = httpEndpoint;
4143
this.grpcEndpoint = grpcEndpoint;
4244
this.httpPort = httpPort;
4345
this.grpcPort = grpcPort;
46+
this.apiToken = apiToken;
4447
}
4548

4649
public String getHttpEndpoint() {
@@ -74,4 +77,12 @@ public void setHttpPort(Integer httpPort) {
7477
public void setGrpcPort(Integer grpcPort) {
7578
this.grpcPort = grpcPort;
7679
}
80+
81+
public String getApiToken() {
82+
return apiToken;
83+
}
84+
85+
public void setApiToken(String apiToken) {
86+
this.apiToken = apiToken;
87+
}
7788
}

dapr-spring/dapr-spring-boot-autoconfigure/src/main/java/io/dapr/spring/boot/autoconfigure/client/DaprConnectionDetails.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ public interface DaprConnectionDetails extends ConnectionDetails {
2525

2626
Integer getGrpcPort();
2727

28+
String getApiToken();
29+
2830
}

dapr-spring/dapr-spring-boot-autoconfigure/src/test/java/io/dapr/spring/boot/autoconfigure/client/DaprClientPropertiesTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ public class DaprClientPropertiesTest {
3131
public void shouldCreateDaprClientPropertiesCorrectly() {
3232

3333
DaprClientProperties properties = new DaprClientProperties(
34-
"http://localhost", "localhost", 3500, 50001
34+
"http://localhost", "localhost", 3500, 50001, "ABC"
3535
);
3636

3737
SoftAssertions.assertSoftly(softly -> {
3838
softly.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
3939
softly.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
4040
softly.assertThat(properties.getHttpPort()).isEqualTo(3500);
4141
softly.assertThat(properties.getGrpcPort()).isEqualTo(50001);
42+
softly.assertThat(properties.getApiToken()).isEqualTo("ABC");
4243
});
4344
}
4445

@@ -52,12 +53,14 @@ public void shouldSetDaprClientPropertiesCorrectly() {
5253
properties.setGrpcPort(50001);
5354
properties.setHttpEndpoint("http://localhost");
5455
properties.setHttpPort(3500);
56+
properties.setApiToken("ABC");
5557

5658
SoftAssertions.assertSoftly(softAssertions -> {
5759
softAssertions.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
5860
softAssertions.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
5961
softAssertions.assertThat(properties.getHttpPort()).isEqualTo(3500);
6062
softAssertions.assertThat(properties.getGrpcPort()).isEqualTo(50001);
63+
softAssertions.assertThat(properties.getApiToken()).isEqualTo("ABC");
6164
});
6265
}
6366

dapr-spring/dapr-spring-boot-tests/src/main/java/io/dapr/spring/boot/testcontainers/service/connection/DaprContainerConnectionDetailsFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,13 @@ public Integer getHttpPort() {
5454
public Integer getGrpcPort() {
5555
return getContainer().getGrpcPort();
5656
}
57+
58+
/*
59+
* No API Token for local container
60+
*/
61+
@Override
62+
public String getApiToken() {
63+
return "";
64+
}
5765
}
5866
}

sdk-workflows/src/main/java/io/dapr/workflows/client/DaprWorkflowClient.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
*/
3838
public class DaprWorkflowClient implements AutoCloseable {
3939

40-
private static final ClientInterceptor WORKFLOW_INTERCEPTOR = new ApiTokenClientInterceptor();
41-
40+
private ClientInterceptor workflowApiTokenInterceptor;
4241
private DurableTaskClient innerClient;
4342
private ManagedChannel grpcChannel;
4443

@@ -55,7 +54,7 @@ public DaprWorkflowClient() {
5554
* @param properties Properties for the GRPC Channel.
5655
*/
5756
public DaprWorkflowClient(Properties properties) {
58-
this(NetworkUtils.buildGrpcManagedChannel(properties, WORKFLOW_INTERCEPTOR));
57+
this(NetworkUtils.buildGrpcManagedChannel(properties, new ApiTokenClientInterceptor(properties)));
5958
}
6059

6160
/**

sdk-workflows/src/main/java/io/dapr/workflows/internal/ApiTokenClientInterceptor.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
import io.grpc.MethodDescriptor;
2525

2626
public class ApiTokenClientInterceptor implements ClientInterceptor {
27+
28+
private Properties properties;
29+
30+
public ApiTokenClientInterceptor(Properties properties) {
31+
this.properties = properties;
32+
}
33+
2734
@Override
2835
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
2936
MethodDescriptor<ReqT, RespT> methodDescriptor,
@@ -34,7 +41,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
3441
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(clientCall) {
3542
@Override
3643
public void start(final Listener<RespT> responseListener, final Metadata metadata) {
37-
String daprApiToken = Properties.API_TOKEN.get();
44+
String daprApiToken = properties.getValue(Properties.API_TOKEN);
3845
if (daprApiToken != null) {
3946
metadata.put(Metadata.Key.of(Headers.DAPR_API_TOKEN, Metadata.ASCII_STRING_MARSHALLER), daprApiToken);
4047
}

sdk-workflows/src/main/java/io/dapr/workflows/runtime/WorkflowRuntimeBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
import io.grpc.ManagedChannel;
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
26+
2627
import java.util.Collections;
2728
import java.util.HashSet;
2829
import java.util.Set;
2930
import java.util.concurrent.ExecutorService;
3031
import java.util.concurrent.Executors;
3132

3233
public class WorkflowRuntimeBuilder {
33-
private static final ClientInterceptor WORKFLOW_INTERCEPTOR = new ApiTokenClientInterceptor();
34+
private ClientInterceptor workflowApiTokenInterceptor;
3435
private static volatile WorkflowRuntime instance;
3536
private final Logger logger;
3637
private final Set<String> workflows = new HashSet<>();
@@ -62,7 +63,8 @@ public WorkflowRuntimeBuilder(Logger logger) {
6263
}
6364

6465
private WorkflowRuntimeBuilder(Properties properties, Logger logger) {
65-
this.managedChannel = NetworkUtils.buildGrpcManagedChannel(properties, WORKFLOW_INTERCEPTOR);
66+
this.workflowApiTokenInterceptor = new ApiTokenClientInterceptor(properties);
67+
this.managedChannel = NetworkUtils.buildGrpcManagedChannel(properties, workflowApiTokenInterceptor);
6668
this.builder = new DurableTaskGrpcWorkerBuilder().grpcChannel(this.managedChannel);
6769
this.logger = logger;
6870
}

0 commit comments

Comments
 (0)