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 @@ -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;


/**
Expand All @@ -36,12 +36,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 @@ -75,4 +78,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 @@ -16,11 +16,15 @@
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;

public interface DaprConnectionDetails extends ConnectionDetails {

String getHttpEndpoint();

String getGrpcEndpoint();

Integer getHttpPort();

Integer getGrpcPort();

String getApiToken();

}
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 @@ -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 @@ -41,5 +41,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
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public class DaprContainer extends GenericContainer<DaprContainer> {
private static final YamlConverter<Subscription> SUBSCRIPTION_CONVERTER = new SubscriptionYamlConverter(YAML_MAPPER);
private static final YamlConverter<HttpEndpoint> HTTPENDPOINT_CONVERTER = new HttpEndpointYamlConverter(YAML_MAPPER);
private static final YamlConverter<Configuration> CONFIGURATION_CONVERTER = new ConfigurationYamlConverter(
YAML_MAPPER);
YAML_MAPPER);
private static final WaitStrategy WAIT_STRATEGY = Wait.forHttp("/v1.0/healthz/outbound")
.forPort(DAPRD_DEFAULT_HTTP_PORT)
.forStatusCodeMatching(statusCode -> statusCode >= 200 && statusCode <= 399);
.forPort(DAPRD_DEFAULT_HTTP_PORT)
.forStatusCodeMatching(statusCode -> statusCode >= 200 && statusCode <= 399);

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

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

/**
* Creates a new Dapr container.
*
* @param dockerImageName Docker image name.
*/
public DaprContainer(DockerImageName dockerImageName) {
Expand All @@ -94,6 +95,7 @@ public DaprContainer(DockerImageName dockerImageName) {

/**
* Creates a new Dapr container.
*
* @param image Docker image name.
*/
public DaprContainer(String image) {
Expand Down Expand Up @@ -166,16 +168,26 @@ public DaprContainer withHttpEndpoint(HttpEndpoint httpEndpoint) {
return this;
}

public DaprContainer withPlacementImage(String placementDockerImageName) {
public DaprContainer withPlacementImage(DockerImageName placementDockerImageName) {
this.placementDockerImageName = placementDockerImageName;
return this;
}

public DaprContainer withSchedulerImage(String schedulerDockerImageName) {
public DaprContainer withPlacementImage(String placementDockerImageName) {
this.placementDockerImageName = DockerImageName.parse(placementDockerImageName);
return this;
}

public DaprContainer withSchedulerImage(DockerImageName schedulerDockerImageName) {
this.schedulerDockerImageName = schedulerDockerImageName;
return this;
}

public DaprContainer withSchedulerImage(String schedulerDockerImageName) {
this.schedulerDockerImageName = DockerImageName.parse(schedulerDockerImageName);
return this;
}

public DaprContainer withReusablePlacement(boolean shouldReusePlacement) {
this.shouldReusePlacement = shouldReusePlacement;
return this;
Expand Down Expand Up @@ -203,6 +215,7 @@ public DaprContainer withComponent(Component component) {

/**
* Adds a Dapr component from a YAML file.
*
* @param path Path to the YAML file.
* @return This container.
*/
Expand All @@ -217,7 +230,7 @@ public DaprContainer withComponent(Path path) {
Map<String, Object> spec = (Map<String, Object>) component.get("spec");
String version = (String) spec.get("version");
List<Map<String, String>> specMetadata =
(List<Map<String, String>>) spec.getOrDefault("metadata", Collections.emptyMap());
(List<Map<String, String>>) spec.getOrDefault("metadata", Collections.emptyList());

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

Expand Down Expand Up @@ -260,17 +273,17 @@ protected void configure() {

if (this.placementContainer == null) {
this.placementContainer = new DaprPlacementContainer(this.placementDockerImageName)
.withNetwork(getNetwork())
.withNetworkAliases(placementService)
.withReuse(this.shouldReusePlacement);
.withNetwork(getNetwork())
.withNetworkAliases(placementService)
.withReuse(this.shouldReusePlacement);
this.placementContainer.start();
}

if (this.schedulerContainer == null) {
this.schedulerContainer = new DaprSchedulerContainer(this.schedulerDockerImageName)
.withNetwork(getNetwork())
.withNetworkAliases(schedulerService)
.withReuse(this.shouldReuseScheduler);
.withNetwork(getNetwork())
.withNetworkAliases(schedulerService)
.withReuse(this.shouldReuseScheduler);
this.schedulerContainer.start();
}

Expand Down Expand Up @@ -386,6 +399,14 @@ public static DockerImageName getDefaultImageName() {
return DEFAULT_IMAGE_NAME;
}

public DockerImageName getPlacementDockerImageName() {
return placementDockerImageName;
}

public DockerImageName getSchedulerDockerImageName() {
return schedulerDockerImageName;
}

// Required by spotbugs plugin
@Override
public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.dapr.testcontainers;

import org.junit.jupiter.api.Test;
import org.testcontainers.utility.DockerImageName;

import static io.dapr.testcontainers.DaprContainerConstants.DAPR_RUNTIME_IMAGE_TAG;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DaprContainerTest {

@Test
public void schedulerAndPlacementCustomImagesTest() {

DaprContainer dapr = new DaprContainer(DAPR_RUNTIME_IMAGE_TAG)
.withAppName("dapr-app")
.withSchedulerImage(DockerImageName.parse("custom/scheduler:1.15.4")
.asCompatibleSubstituteFor("daprio/scheduler:1.15.4"))
.withPlacementImage(DockerImageName.parse("custom/placement:1.15.4")
.asCompatibleSubstituteFor("daprio/placement:1.15.4"))
.withAppPort(8081)
.withDaprLogLevel(DaprLogLevel.DEBUG)
.withAppChannelAddress("host.testcontainers.internal");


assertEquals("custom/placement:1.15.4", dapr.getPlacementDockerImageName().asCanonicalNameString());
assertEquals("custom/scheduler:1.15.4", dapr.getSchedulerDockerImageName().asCanonicalNameString());

}

@Test
public void schedulerAndPlacementCustomImagesStringTest() {

DaprContainer dapr = new DaprContainer(DAPR_RUNTIME_IMAGE_TAG)
.withAppName("dapr-app")
.withSchedulerImage("daprio/scheduler:1.15.4")
.withPlacementImage("daprio/placement:1.15.4")
.withAppPort(8081)
.withDaprLogLevel(DaprLogLevel.DEBUG)
.withAppChannelAddress("host.testcontainers.internal");


assertEquals("daprio/placement:1.15.4", dapr.getPlacementDockerImageName().asCanonicalNameString());
assertEquals("daprio/scheduler:1.15.4", dapr.getSchedulerDockerImageName().asCanonicalNameString());

}
}
Loading