Skip to content

Commit fcd6f90

Browse files
cicoylesalaboydependabot[bot]artur-ciocanumarcduiker
authored
[1.15] support for api token (#1453)
* supporting spring boot property for API TOKEN on workflow interceptor (#1452) Signed-off-by: salaboy <[email protected]> * Supporting placement and scheduler custom images (#1450) * supporting placement and scheduler custom images Signed-off-by: salaboy <[email protected]> * Bump org.apache.commons:commons-lang3 from 3.9 to 3.18.0 (#1446) Bumps org.apache.commons:commons-lang3 from 3.9 to 3.18.0. --- updated-dependencies: - dependency-name: org.apache.commons:commons-lang3 dependency-version: 3.18.0 dependency-type: direct:development ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: artur-ciocanu <[email protected]> Signed-off-by: salaboy <[email protected]> * Update dapr docs for Hugo upgrade (#1443) Signed-off-by: Marc Duiker <[email protected]> Co-authored-by: Cassie Coyle <[email protected]> Signed-off-by: salaboy <[email protected]> * Adds note about workflow start time (#1444) Signed-off-by: joshvanl <[email protected]> Co-authored-by: Dapr Bot <[email protected]> Co-authored-by: artur-ciocanu <[email protected]> Signed-off-by: salaboy <[email protected]> * adding test to validate canonical names with substitutes Signed-off-by: salaboy <[email protected]> * Migrate PubSub removing flaky test (#1407) * Migrate PubSub removing flaky test Signed-off-by: Matheus Cruz <[email protected]> * Adjust assertion Signed-off-by: Matheus Cruz <[email protected]> * Change assert Signed-off-by: Matheus Cruz <[email protected]> * Apply pull request suggestions Signed-off-by: Matheus Cruz <[email protected]> * Use custom ObjectSerializer Signed-off-by: Matheus Cruz <[email protected]> --------- Signed-off-by: Matheus Cruz <[email protected]> Co-authored-by: artur-ciocanu <[email protected]> Signed-off-by: salaboy <[email protected]> * adding tests for coverage Signed-off-by: salaboy <[email protected]> --------- Signed-off-by: salaboy <[email protected]> Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: Marc Duiker <[email protected]> Signed-off-by: joshvanl <[email protected]> Signed-off-by: Matheus Cruz <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: artur-ciocanu <[email protected]> Co-authored-by: Marc Duiker <[email protected]> Co-authored-by: Cassie Coyle <[email protected]> Co-authored-by: Josh van Leeuwen <[email protected]> Co-authored-by: Dapr Bot <[email protected]> Co-authored-by: Matheus Cruz <[email protected]> --------- Signed-off-by: salaboy <[email protected]> Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: Marc Duiker <[email protected]> Signed-off-by: joshvanl <[email protected]> Signed-off-by: Matheus Cruz <[email protected]> Co-authored-by: salaboy <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: artur-ciocanu <[email protected]> Co-authored-by: Marc Duiker <[email protected]> Co-authored-by: Josh van Leeuwen <[email protected]> Co-authored-by: Dapr Bot <[email protected]> Co-authored-by: Matheus Cruz <[email protected]>
1 parent 16638a7 commit fcd6f90

File tree

11 files changed

+140
-23
lines changed

11 files changed

+140
-23
lines changed

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
/**
@@ -36,12 +36,15 @@ public DaprClientProperties() {
3636
* @param grpcEndpoint grpc endpoint to interact with the Dapr Sidecar
3737
* @param httpPort http port to interact with the Dapr Sidecar
3838
* @param grpcPort grpc port to interact with the Dapr Sidecar
39+
* @param apiToken dapr API token to interact with the Dapr Sidecar
3940
*/
40-
public DaprClientProperties(String httpEndpoint, String grpcEndpoint, Integer httpPort, Integer grpcPort) {
41+
public DaprClientProperties(String httpEndpoint, String grpcEndpoint, Integer httpPort, Integer grpcPort,
42+
String apiToken) {
4143
this.httpEndpoint = httpEndpoint;
4244
this.grpcEndpoint = grpcEndpoint;
4345
this.httpPort = httpPort;
4446
this.grpcPort = grpcPort;
47+
this.apiToken = apiToken;
4548
}
4649

4750
public String getHttpEndpoint() {
@@ -75,4 +78,12 @@ public void setHttpPort(Integer httpPort) {
7578
public void setGrpcPort(Integer grpcPort) {
7679
this.grpcPort = grpcPort;
7780
}
81+
82+
public String getApiToken() {
83+
return apiToken;
84+
}
85+
86+
public void setApiToken(String apiToken) {
87+
this.apiToken = apiToken;
88+
}
7889
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
1717

1818
public interface DaprConnectionDetails extends ConnectionDetails {
19+
1920
String getHttpEndpoint();
2021

2122
String getGrpcEndpoint();
2223

2324
Integer getHttpPort();
2425

2526
Integer getGrpcPort();
27+
28+
String getApiToken();
29+
2630
}

dapr-spring/dapr-spring-boot-autoconfigure/src/main/java/io/dapr/spring/boot/autoconfigure/client/PropertiesDaprConnectionDetails.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/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
@@ -41,5 +41,13 @@ public Integer getHttpPort() {
4141
public Integer getGrpcPort() {
4242
return getContainer().getGrpcPort();
4343
}
44+
45+
/*
46+
* No API Token for local container
47+
*/
48+
@Override
49+
public String getApiToken() {
50+
return "";
51+
}
4452
}
4553
}

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
}

testcontainers-dapr/src/main/java/io/dapr/testcontainers/DaprContainer.java

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public class DaprContainer extends GenericContainer<DaprContainer> {
5656
private static final YamlConverter<Subscription> SUBSCRIPTION_CONVERTER = new SubscriptionYamlConverter(YAML_MAPPER);
5757
private static final YamlConverter<HttpEndpoint> HTTPENDPOINT_CONVERTER = new HttpEndpointYamlConverter(YAML_MAPPER);
5858
private static final YamlConverter<Configuration> CONFIGURATION_CONVERTER = new ConfigurationYamlConverter(
59-
YAML_MAPPER);
59+
YAML_MAPPER);
6060
private static final WaitStrategy WAIT_STRATEGY = Wait.forHttp("/v1.0/healthz/outbound")
61-
.forPort(DAPRD_DEFAULT_HTTP_PORT)
62-
.forStatusCodeMatching(statusCode -> statusCode >= 200 && statusCode <= 399);
61+
.forPort(DAPRD_DEFAULT_HTTP_PORT)
62+
.forStatusCodeMatching(statusCode -> statusCode >= 200 && statusCode <= 399);
6363

6464
private final Set<Component> components = new HashSet<>();
6565
private final Set<Subscription> subscriptions = new HashSet<>();
@@ -68,8 +68,8 @@ public class DaprContainer extends GenericContainer<DaprContainer> {
6868
private String appChannelAddress = "localhost";
6969
private String placementService = "placement";
7070
private String schedulerService = "scheduler";
71-
private String placementDockerImageName = DAPR_PLACEMENT_IMAGE_TAG;
72-
private String schedulerDockerImageName = DAPR_SCHEDULER_IMAGE_TAG;
71+
private DockerImageName placementDockerImageName = DockerImageName.parse(DAPR_PLACEMENT_IMAGE_TAG);
72+
private DockerImageName schedulerDockerImageName = DockerImageName.parse(DAPR_SCHEDULER_IMAGE_TAG);
7373

7474
private Configuration configuration;
7575
private DaprPlacementContainer placementContainer;
@@ -82,6 +82,7 @@ public class DaprContainer extends GenericContainer<DaprContainer> {
8282

8383
/**
8484
* Creates a new Dapr container.
85+
*
8586
* @param dockerImageName Docker image name.
8687
*/
8788
public DaprContainer(DockerImageName dockerImageName) {
@@ -94,6 +95,7 @@ public DaprContainer(DockerImageName dockerImageName) {
9495

9596
/**
9697
* Creates a new Dapr container.
98+
*
9799
* @param image Docker image name.
98100
*/
99101
public DaprContainer(String image) {
@@ -166,16 +168,26 @@ public DaprContainer withHttpEndpoint(HttpEndpoint httpEndpoint) {
166168
return this;
167169
}
168170

169-
public DaprContainer withPlacementImage(String placementDockerImageName) {
171+
public DaprContainer withPlacementImage(DockerImageName placementDockerImageName) {
170172
this.placementDockerImageName = placementDockerImageName;
171173
return this;
172174
}
173175

174-
public DaprContainer withSchedulerImage(String schedulerDockerImageName) {
176+
public DaprContainer withPlacementImage(String placementDockerImageName) {
177+
this.placementDockerImageName = DockerImageName.parse(placementDockerImageName);
178+
return this;
179+
}
180+
181+
public DaprContainer withSchedulerImage(DockerImageName schedulerDockerImageName) {
175182
this.schedulerDockerImageName = schedulerDockerImageName;
176183
return this;
177184
}
178185

186+
public DaprContainer withSchedulerImage(String schedulerDockerImageName) {
187+
this.schedulerDockerImageName = DockerImageName.parse(schedulerDockerImageName);
188+
return this;
189+
}
190+
179191
public DaprContainer withReusablePlacement(boolean shouldReusePlacement) {
180192
this.shouldReusePlacement = shouldReusePlacement;
181193
return this;
@@ -203,6 +215,7 @@ public DaprContainer withComponent(Component component) {
203215

204216
/**
205217
* Adds a Dapr component from a YAML file.
218+
*
206219
* @param path Path to the YAML file.
207220
* @return This container.
208221
*/
@@ -217,7 +230,7 @@ public DaprContainer withComponent(Path path) {
217230
Map<String, Object> spec = (Map<String, Object>) component.get("spec");
218231
String version = (String) spec.get("version");
219232
List<Map<String, String>> specMetadata =
220-
(List<Map<String, String>>) spec.getOrDefault("metadata", Collections.emptyMap());
233+
(List<Map<String, String>>) spec.getOrDefault("metadata", Collections.emptyList());
221234

222235
ArrayList<MetadataEntry> metadataEntries = new ArrayList<>();
223236

@@ -260,17 +273,17 @@ protected void configure() {
260273

261274
if (this.placementContainer == null) {
262275
this.placementContainer = new DaprPlacementContainer(this.placementDockerImageName)
263-
.withNetwork(getNetwork())
264-
.withNetworkAliases(placementService)
265-
.withReuse(this.shouldReusePlacement);
276+
.withNetwork(getNetwork())
277+
.withNetworkAliases(placementService)
278+
.withReuse(this.shouldReusePlacement);
266279
this.placementContainer.start();
267280
}
268281

269282
if (this.schedulerContainer == null) {
270283
this.schedulerContainer = new DaprSchedulerContainer(this.schedulerDockerImageName)
271-
.withNetwork(getNetwork())
272-
.withNetworkAliases(schedulerService)
273-
.withReuse(this.shouldReuseScheduler);
284+
.withNetwork(getNetwork())
285+
.withNetworkAliases(schedulerService)
286+
.withReuse(this.shouldReuseScheduler);
274287
this.schedulerContainer.start();
275288
}
276289

@@ -386,6 +399,14 @@ public static DockerImageName getDefaultImageName() {
386399
return DEFAULT_IMAGE_NAME;
387400
}
388401

402+
public DockerImageName getPlacementDockerImageName() {
403+
return placementDockerImageName;
404+
}
405+
406+
public DockerImageName getSchedulerDockerImageName() {
407+
return schedulerDockerImageName;
408+
}
409+
389410
// Required by spotbugs plugin
390411
@Override
391412
public boolean equals(Object o) {

0 commit comments

Comments
 (0)