Skip to content

Commit 15f0649

Browse files
committed
enable TLS if api key provided
1 parent b06f15d commit 15f0649

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

temporal-serviceclient/src/main/java/io/temporal/serviceclient/ServiceStubsOptions.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public String toString() {
418418
public static class Builder<T extends Builder<T>> {
419419
private ManagedChannel channel;
420420
private SslContext sslContext;
421-
private boolean enableHttps;
421+
private Boolean enableHttps;
422422
private String target;
423423
private Consumer<ManagedChannelBuilder<?>> channelInitializer;
424424
private Duration healthCheckAttemptTimeout;
@@ -851,6 +851,18 @@ public ServiceStubsOptions validateAndBuildWithDefaults() {
851851
Collection<ClientInterceptor> grpcClientInterceptors =
852852
MoreObjects.firstNonNull(this.grpcClientInterceptors, Collections.emptyList());
853853

854+
// Auto-enable TLS when API key is provided and TLS is not explicitly set
855+
boolean enableHttps = this.enableHttps != null ? this.enableHttps : false;
856+
if (this.enableHttps == null && this.sslContext == null) {
857+
// Check if an API key provider was added
858+
boolean hasApiKey =
859+
grpcMetadataProviders.stream()
860+
.anyMatch(provider -> provider instanceof AuthorizationGrpcMetadataProvider);
861+
if (hasApiKey) {
862+
enableHttps = true;
863+
}
864+
}
865+
854866
Scope metricsScope = this.metricsScope != null ? this.metricsScope : new NoopScope();
855867
Duration healthCheckAttemptTimeout =
856868
this.healthCheckAttemptTimeout != null
@@ -865,7 +877,7 @@ public ServiceStubsOptions validateAndBuildWithDefaults() {
865877
this.channel,
866878
target,
867879
this.channelInitializer,
868-
this.enableHttps,
880+
enableHttps,
869881
this.sslContext,
870882
healthCheckAttemptTimeout,
871883
healthCheckTimeout,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.temporal.serviceclient;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class ServiceStubsOptionsTest {
8+
9+
@Test
10+
public void testTLSEnabledByDefaultWhenAPIKeyProvided() {
11+
// Test that TLS is enabled by default when API key is provided and TLS is not configured
12+
ServiceStubsOptions options =
13+
WorkflowServiceStubsOptions.newBuilder()
14+
.setTarget("localhost:7233")
15+
.addApiKey(() -> "test-api-key")
16+
.validateAndBuildWithDefaults();
17+
18+
// TLS should be auto-enabled when api_key is provided and tls not explicitly set
19+
assertTrue(options.getEnableHttps());
20+
}
21+
22+
@Test
23+
public void testTLSCanBeExplicitlyDisabledWithAPIKey() {
24+
// Test that TLS can be explicitly disabled even when API key is provided
25+
ServiceStubsOptions options =
26+
WorkflowServiceStubsOptions.newBuilder()
27+
.setTarget("localhost:7233")
28+
.addApiKey(() -> "test-api-key")
29+
.setEnableHttps(false)
30+
.validateAndBuildWithDefaults();
31+
32+
// TLS should remain disabled when explicitly set to false
33+
assertFalse(options.getEnableHttps());
34+
}
35+
36+
@Test
37+
public void testTLSDisabledByDefaultWithoutAPIKey() {
38+
// Test that TLS is disabled by default when no API key is provided
39+
ServiceStubsOptions options =
40+
WorkflowServiceStubsOptions.newBuilder()
41+
.setTarget("localhost:7233")
42+
.validateAndBuildWithDefaults();
43+
44+
// TLS should remain disabled when no api_key is provided
45+
assertFalse(options.getEnableHttps());
46+
}
47+
48+
@Test
49+
public void testExplicitTLSConfigPreservedWithAPIKey() {
50+
// Test that explicit TLS configuration is preserved when API key is provided
51+
ServiceStubsOptions options =
52+
WorkflowServiceStubsOptions.newBuilder()
53+
.setTarget("localhost:7233")
54+
.addApiKey(() -> "test-api-key")
55+
.setEnableHttps(true)
56+
.validateAndBuildWithDefaults();
57+
58+
// Explicit TLS config should be preserved
59+
assertTrue(options.getEnableHttps());
60+
}
61+
}

0 commit comments

Comments
 (0)