@@ -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 ,
0 commit comments