mirror of https://github.com/dapr/java-sdk.git
supporting spring boot property for API TOKEN on workflow interceptor (#1452)
Signed-off-by: salaboy <Salaboy@gmail.com> Signed-off-by: sirivarma <siri.varma@outlook.com>
This commit is contained in:
parent
ffebf976bc
commit
3c9bee7546
|
|
@ -40,4 +40,10 @@ public class ClientPropertiesDaprConnectionDetails implements DaprConnectionDeta
|
|||
public Integer getGrpcPort() {
|
||||
return this.daprClientProperties.getGrpcPort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getApiToken() {
|
||||
return this.daprClientProperties.getApiToken();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,6 +68,11 @@ public class DaprClientAutoConfiguration {
|
|||
builder.withPropertyOverride(Properties.GRPC_PORT, String.valueOf(grpcPort));
|
||||
}
|
||||
|
||||
String apiToken = daprConnectionDetails.getApiToken();
|
||||
if (apiToken != null) {
|
||||
builder.withPropertyOverride(Properties.API_TOKEN, apiToken);
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
|
@ -145,6 +150,11 @@ public class DaprClientAutoConfiguration {
|
|||
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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ limitations under the License.
|
|||
|
||||
package io.dapr.spring.boot.autoconfigure.client;
|
||||
|
||||
import io.dapr.spring.data.DaprKeyValueAdapterResolver;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties(prefix = "dapr.client")
|
||||
|
|
@ -22,6 +21,7 @@ public class DaprClientProperties {
|
|||
private String grpcEndpoint;
|
||||
private Integer httpPort;
|
||||
private Integer grpcPort;
|
||||
private String apiToken;
|
||||
|
||||
/**
|
||||
* Constructs a {@link DaprClientProperties}.
|
||||
|
|
@ -35,12 +35,15 @@ public class 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() {
|
||||
|
|
@ -74,4 +77,12 @@ public class DaprClientProperties {
|
|||
public void setGrpcPort(Integer grpcPort) {
|
||||
this.grpcPort = grpcPort;
|
||||
}
|
||||
|
||||
public String getApiToken() {
|
||||
return apiToken;
|
||||
}
|
||||
|
||||
public void setApiToken(String apiToken) {
|
||||
this.apiToken = apiToken;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,4 +25,6 @@ public interface DaprConnectionDetails extends ConnectionDetails {
|
|||
|
||||
Integer getGrpcPort();
|
||||
|
||||
String getApiToken();
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ public class DaprClientPropertiesTest {
|
|||
public void shouldCreateDaprClientPropertiesCorrectly() {
|
||||
|
||||
DaprClientProperties properties = new DaprClientProperties(
|
||||
"http://localhost", "localhost", 3500, 50001
|
||||
"http://localhost", "localhost", 3500, 50001, "ABC"
|
||||
);
|
||||
|
||||
SoftAssertions.assertSoftly(softly -> {
|
||||
|
|
@ -39,6 +39,7 @@ public class DaprClientPropertiesTest {
|
|||
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");
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -52,12 +53,14 @@ public class DaprClientPropertiesTest {
|
|||
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");
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -54,5 +54,13 @@ public class DaprContainerConnectionDetailsFactory
|
|||
public Integer getGrpcPort() {
|
||||
return getContainer().getGrpcPort();
|
||||
}
|
||||
|
||||
/*
|
||||
* No API Token for local container
|
||||
*/
|
||||
@Override
|
||||
public String getApiToken() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,8 +37,7 @@ import java.util.concurrent.TimeoutException;
|
|||
*/
|
||||
public class DaprWorkflowClient implements AutoCloseable {
|
||||
|
||||
private static final ClientInterceptor WORKFLOW_INTERCEPTOR = new ApiTokenClientInterceptor();
|
||||
|
||||
private ClientInterceptor workflowApiTokenInterceptor;
|
||||
private DurableTaskClient innerClient;
|
||||
private ManagedChannel grpcChannel;
|
||||
|
||||
|
|
@ -55,7 +54,7 @@ public class DaprWorkflowClient implements AutoCloseable {
|
|||
* @param properties Properties for the GRPC Channel.
|
||||
*/
|
||||
public DaprWorkflowClient(Properties properties) {
|
||||
this(NetworkUtils.buildGrpcManagedChannel(properties, WORKFLOW_INTERCEPTOR));
|
||||
this(NetworkUtils.buildGrpcManagedChannel(properties, new ApiTokenClientInterceptor(properties)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -24,6 +24,13 @@ import io.grpc.Metadata;
|
|||
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,
|
||||
|
|
@ -34,7 +41,7 @@ public class ApiTokenClientInterceptor implements ClientInterceptor {
|
|||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import io.grpc.ClientInterceptor;
|
|||
import io.grpc.ManagedChannel;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
|
@ -30,7 +31,7 @@ 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<>();
|
||||
|
|
@ -62,7 +63,8 @@ public class WorkflowRuntimeBuilder {
|
|||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue