Skip to content

Commit b7762ee

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

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ public static class Builder<T extends Builder<T>> {
419419
private ManagedChannel channel;
420420
private SslContext sslContext;
421421
private boolean enableHttps;
422+
private boolean enableHttpsExplicitlySet;
422423
private String target;
423424
private Consumer<ManagedChannelBuilder<?>> channelInitializer;
424425
private Duration healthCheckAttemptTimeout;
@@ -435,6 +436,7 @@ public static class Builder<T extends Builder<T>> {
435436
private Collection<GrpcMetadataProvider> grpcMetadataProviders;
436437
private Collection<ClientInterceptor> grpcClientInterceptors;
437438
private Scope metricsScope;
439+
private boolean apiKeyProvided;
438440

439441
protected Builder() {}
440442

@@ -443,6 +445,7 @@ protected Builder(ServiceStubsOptions options) {
443445
this.target = options.target;
444446
this.channelInitializer = options.channelInitializer;
445447
this.enableHttps = options.enableHttps;
448+
this.enableHttpsExplicitlySet = true;
446449
this.sslContext = options.sslContext;
447450
this.healthCheckAttemptTimeout = options.healthCheckAttemptTimeout;
448451
this.healthCheckTimeout = options.healthCheckTimeout;
@@ -542,6 +545,7 @@ public T setSslContext(SslContext sslContext) {
542545
*/
543546
public T setEnableHttps(boolean enableHttps) {
544547
this.enableHttps = enableHttps;
548+
this.enableHttpsExplicitlySet = true;
545549
return self();
546550
}
547551

@@ -613,6 +617,7 @@ public T addGrpcMetadataProvider(GrpcMetadataProvider grpcMetadataProvider) {
613617
* @return {@code this}
614618
*/
615619
public T addApiKey(AuthorizationTokenSupplier apiKey) {
620+
this.apiKeyProvided = true;
616621
addGrpcMetadataProvider(
617622
new AuthorizationGrpcMetadataProvider(() -> "Bearer " + apiKey.supply()));
618623
return self();
@@ -851,6 +856,14 @@ public ServiceStubsOptions validateAndBuildWithDefaults() {
851856
Collection<ClientInterceptor> grpcClientInterceptors =
852857
MoreObjects.firstNonNull(this.grpcClientInterceptors, Collections.emptyList());
853858

859+
// Auto-enable TLS when API key is provided and TLS is not explicitly set
860+
boolean enableHttps;
861+
if (this.enableHttpsExplicitlySet) {
862+
enableHttps = this.enableHttps;
863+
} else {
864+
enableHttps = this.apiKeyProvided;
865+
}
866+
854867
Scope metricsScope = this.metricsScope != null ? this.metricsScope : new NoopScope();
855868
Duration healthCheckAttemptTimeout =
856869
this.healthCheckAttemptTimeout != null
@@ -865,7 +878,7 @@ public ServiceStubsOptions validateAndBuildWithDefaults() {
865878
this.channel,
866879
target,
867880
this.channelInitializer,
868-
this.enableHttps,
881+
enableHttps,
869882
this.sslContext,
870883
healthCheckAttemptTimeout,
871884
healthCheckTimeout,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
ServiceStubsOptions options =
12+
WorkflowServiceStubsOptions.newBuilder()
13+
.setTarget("localhost:7233")
14+
.addApiKey(() -> "test-api-key")
15+
.validateAndBuildWithDefaults();
16+
17+
assertTrue(options.getEnableHttps());
18+
}
19+
20+
@Test
21+
public void testTLSCanBeExplicitlyDisabledWithAPIKey() {
22+
ServiceStubsOptions options =
23+
WorkflowServiceStubsOptions.newBuilder()
24+
.setTarget("localhost:7233")
25+
.addApiKey(() -> "test-api-key")
26+
.setEnableHttps(false)
27+
.validateAndBuildWithDefaults();
28+
29+
assertFalse(options.getEnableHttps());
30+
}
31+
32+
@Test
33+
public void testExplicitTLSDisableBeforeAPIKeyStillDisables() {
34+
ServiceStubsOptions options =
35+
WorkflowServiceStubsOptions.newBuilder()
36+
.setTarget("localhost:7233")
37+
.setEnableHttps(false)
38+
.addApiKey(() -> "test-api-key")
39+
.validateAndBuildWithDefaults();
40+
41+
// Explicit TLS=false should take precedence regardless of order
42+
assertFalse(options.getEnableHttps());
43+
}
44+
45+
@Test
46+
public void testExplicitTLSDisableAfterAPIKeyStillDisables() {
47+
ServiceStubsOptions options =
48+
WorkflowServiceStubsOptions.newBuilder()
49+
.setTarget("localhost:7233")
50+
.addApiKey(() -> "test-api-key")
51+
.setEnableHttps(false)
52+
.validateAndBuildWithDefaults();
53+
54+
// Explicit TLS=false should take precedence regardless of order
55+
assertFalse(options.getEnableHttps());
56+
}
57+
}

0 commit comments

Comments
 (0)