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 @@ -40,4 +40,10 @@ public Integer getHttpPort() {
public Integer getGrpcPort() {
return this.daprClientProperties.getGrpcPort();
}

@Override
public String getApiToken() {
return this.daprClientProperties.getApiToken();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ DaprClientBuilder daprClientBuilder(DaprConnectionDetails daprConnectionDetails)
builder.withPropertyOverride(Properties.GRPC_PORT, String.valueOf(grpcPort));
}

String apiToken = daprConnectionDetails.getApiToken();
if (apiToken != null) {
builder.withPropertyOverride(Properties.API_TOKEN, apiToken);
}

return builder;
}

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

String apiToken = daprConnectionDetails.getApiToken();
if (apiToken != null) {
propertyOverrides.put(Properties.API_TOKEN.getName(), apiToken);
}

return new Properties(propertyOverrides);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

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

import io.dapr.spring.data.DaprKeyValueAdapterResolver;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "dapr.client")
Expand All @@ -22,6 +21,7 @@ public class DaprClientProperties {
private String grpcEndpoint;
private Integer httpPort;
private Integer grpcPort;
private String apiToken;

/**
* Constructs a {@link DaprClientProperties}.
Expand All @@ -35,12 +35,15 @@ public DaprClientProperties() {
* @param grpcEndpoint grpc endpoint to interact with the Dapr Sidecar
* @param httpPort http port to interact with the Dapr Sidecar
* @param grpcPort grpc port to interact with the Dapr Sidecar
* @param apiToken dapr API token to interact with the Dapr Sidecar
*/
public DaprClientProperties(String httpEndpoint, String grpcEndpoint, Integer httpPort, Integer grpcPort) {
public DaprClientProperties(String httpEndpoint, String grpcEndpoint, Integer httpPort, Integer grpcPort,
String apiToken) {
this.httpEndpoint = httpEndpoint;
this.grpcEndpoint = grpcEndpoint;
this.httpPort = httpPort;
this.grpcPort = grpcPort;
this.apiToken = apiToken;
}

public String getHttpEndpoint() {
Expand Down Expand Up @@ -74,4 +77,12 @@ public void setHttpPort(Integer httpPort) {
public void setGrpcPort(Integer grpcPort) {
this.grpcPort = grpcPort;
}

public String getApiToken() {
return apiToken;
}

public void setApiToken(String apiToken) {
this.apiToken = apiToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface DaprConnectionDetails extends ConnectionDetails {

Integer getGrpcPort();

String getApiToken();

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public class DaprClientPropertiesTest {
public void shouldCreateDaprClientPropertiesCorrectly() {

DaprClientProperties properties = new DaprClientProperties(
"http://localhost", "localhost", 3500, 50001
"http://localhost", "localhost", 3500, 50001, "ABC"
);

SoftAssertions.assertSoftly(softly -> {
softly.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
softly.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
softly.assertThat(properties.getHttpPort()).isEqualTo(3500);
softly.assertThat(properties.getGrpcPort()).isEqualTo(50001);
softly.assertThat(properties.getApiToken()).isEqualTo("ABC");
});
}

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

SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(properties.getGrpcEndpoint()).isEqualTo("localhost");
softAssertions.assertThat(properties.getHttpEndpoint()).isEqualTo("http://localhost");
softAssertions.assertThat(properties.getHttpPort()).isEqualTo(3500);
softAssertions.assertThat(properties.getGrpcPort()).isEqualTo(50001);
softAssertions.assertThat(properties.getApiToken()).isEqualTo("ABC");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,13 @@ public Integer getHttpPort() {
public Integer getGrpcPort() {
return getContainer().getGrpcPort();
}

/*
* No API Token for local container
*/
@Override
public String getApiToken() {
return "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
*/
public class DaprWorkflowClient implements AutoCloseable {

private static final ClientInterceptor WORKFLOW_INTERCEPTOR = new ApiTokenClientInterceptor();

private ClientInterceptor workflowApiTokenInterceptor;
private DurableTaskClient innerClient;
private ManagedChannel grpcChannel;

Expand All @@ -55,7 +54,7 @@ public DaprWorkflowClient() {
* @param properties Properties for the GRPC Channel.
*/
public DaprWorkflowClient(Properties properties) {
this(NetworkUtils.buildGrpcManagedChannel(properties, WORKFLOW_INTERCEPTOR));
this(NetworkUtils.buildGrpcManagedChannel(properties, new ApiTokenClientInterceptor(properties)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
import io.grpc.MethodDescriptor;

public class ApiTokenClientInterceptor implements ClientInterceptor {

private Properties properties;

public ApiTokenClientInterceptor(Properties properties) {
this.properties = properties;
}

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> methodDescriptor,
Expand All @@ -34,7 +41,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(clientCall) {
@Override
public void start(final Listener<RespT> responseListener, final Metadata metadata) {
String daprApiToken = Properties.API_TOKEN.get();
String daprApiToken = properties.getValue(Properties.API_TOKEN);
if (daprApiToken != null) {
metadata.put(Metadata.Key.of(Headers.DAPR_API_TOKEN, Metadata.ASCII_STRING_MARSHALLER), daprApiToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
import io.grpc.ManagedChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class WorkflowRuntimeBuilder {
private static final ClientInterceptor WORKFLOW_INTERCEPTOR = new ApiTokenClientInterceptor();
private ClientInterceptor workflowApiTokenInterceptor;
private static volatile WorkflowRuntime instance;
private final Logger logger;
private final Set<String> workflows = new HashSet<>();
Expand Down Expand Up @@ -62,7 +63,8 @@ public WorkflowRuntimeBuilder(Logger logger) {
}

private WorkflowRuntimeBuilder(Properties properties, Logger logger) {
this.managedChannel = NetworkUtils.buildGrpcManagedChannel(properties, WORKFLOW_INTERCEPTOR);
this.workflowApiTokenInterceptor = new ApiTokenClientInterceptor(properties);
this.managedChannel = NetworkUtils.buildGrpcManagedChannel(properties, workflowApiTokenInterceptor);
this.builder = new DurableTaskGrpcWorkerBuilder().grpcChannel(this.managedChannel);
this.logger = logger;
}
Expand Down
Loading