Skip to content

Commit c84290a

Browse files
authored
💥 enable TLS if api key provided (#366)
* enable TLS if api key provided * fix * update sig files, push tls/api key check to bridge options construction * change cloud tests to use api key to enable TLS
1 parent bf2ad8e commit c84290a

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

‎temporalio/lib/temporalio/client.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def self.connect(
9999
target_host,
100100
namespace,
101101
api_key: nil,
102-
tls: false,
102+
tls: nil,
103103
data_converter: Converters::DataConverter.default,
104104
interceptors: [],
105105
logger: Logger.new($stdout, level: Logger::WARN),

‎temporalio/lib/temporalio/client/connection.rb‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ class HTTPConnectProxyOptions; end # rubocop:disable Lint/EmptyClass
154154
# +localhost:7233+.
155155
# @param api_key [String, nil] API key for Temporal. This becomes the +Authorization+ HTTP header with +"Bearer "+
156156
# prepended. This is only set if RPC metadata doesn't already have an +authorization+ key.
157-
# @param tls [Boolean, TLSOptions] If false, do not use TLS. If true, use system default TLS options. If TLS
158-
# options are present, those TLS options will be used.
157+
# @param tls [Boolean, TLSOptions, nil] If false, do not use TLS. If true, use system default TLS options. If TLS
158+
# options are present, those TLS options will be used. If nil (the default), TLS will be auto-enabled if
159+
# api_key is provided.
159160
# @param rpc_metadata [Hash<String, String>] Headers to use for all calls to the server. Keys here can be
160161
# overriden by per-call RPC metadata keys.
161162
# @param rpc_retry [RPCRetryOptions] Retry options for direct service calls (when opted in) or all high-level
@@ -173,7 +174,7 @@ class HTTPConnectProxyOptions; end # rubocop:disable Lint/EmptyClass
173174
def initialize(
174175
target_host:,
175176
api_key: nil,
176-
tls: false,
177+
tls: nil,
177178
rpc_metadata: {},
178179
rpc_retry: RPCRetryOptions.new,
179180
identity: "#{Process.pid}@#{Socket.gethostname}",
@@ -285,13 +286,17 @@ def new_core_client
285286
),
286287
identity: @options.identity || "#{Process.pid}@#{Socket.gethostname}"
287288
)
288-
if @options.tls
289-
options.tls = if @options.tls.is_a?(TLSOptions)
289+
# Auto-enable TLS when API key is provided and tls not explicitly set
290+
tls = @options.tls
291+
tls = true if tls.nil? && @options.api_key
292+
293+
if tls
294+
options.tls = if tls.is_a?(TLSOptions)
290295
Internal::Bridge::Client::TLSOptions.new(
291-
client_cert: @options.tls.client_cert, # steep:ignore
292-
client_private_key: @options.tls.client_private_key, # steep:ignore
293-
server_root_ca_cert: @options.tls.server_root_ca_cert, # steep:ignore
294-
domain: @options.tls.domain # steep:ignore
296+
client_cert: tls.client_cert, # steep:ignore
297+
client_private_key: tls.client_private_key, # steep:ignore
298+
server_root_ca_cert: tls.server_root_ca_cert, # steep:ignore
299+
domain: tls.domain # steep:ignore
295300
)
296301
else
297302
Internal::Bridge::Client::TLSOptions.new

‎temporalio/sig/temporalio/client.rbs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module Temporalio
3636
String target_host,
3737
String namespace,
3838
?api_key: String?,
39-
?tls: bool | Connection::TLSOptions,
39+
?tls: bool | Connection::TLSOptions | nil,
4040
?data_converter: Converters::DataConverter,
4141
?interceptors: Array[Interceptor],
4242
?logger: Logger,

‎temporalio/sig/temporalio/client/connection.rbs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Temporalio
44
class Options
55
attr_reader target_host: String
66
attr_reader api_key: String?
7-
attr_reader tls: bool | Connection::TLSOptions
7+
attr_reader tls: bool | Connection::TLSOptions | nil
88
attr_reader rpc_metadata: Hash[String, String]
99
attr_reader rpc_retry: RPCRetryOptions
1010
attr_reader identity: String
@@ -16,7 +16,7 @@ module Temporalio
1616
def initialize: (
1717
target_host: String,
1818
api_key: String?,
19-
tls: bool | Connection::TLSOptions,
19+
tls: bool | Connection::TLSOptions | nil,
2020
rpc_metadata: Hash[String, String],
2121
rpc_retry: RPCRetryOptions,
2222
identity: String,
@@ -98,7 +98,7 @@ module Temporalio
9898
def initialize: (
9999
target_host: String,
100100
?api_key: String?,
101-
?tls: bool | Connection::TLSOptions,
101+
?tls: bool | Connection::TLSOptions | nil,
102102
?rpc_metadata: Hash[String, String],
103103
?rpc_retry: RPCRetryOptions,
104104
?identity: String,

‎temporalio/test/client_cloud_test.rb‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ def test_mtls
2828
end
2929

3030
def test_api_key
31+
# This test validates the auto-TLS feature: TLS is auto-enabled when api_key is provided
32+
# and tls is not explicitly set.
3133
api_key = ENV.fetch('TEMPORAL_CLOUD_API_KEY_TEST_API_KEY', '')
3234
skip('No cloud API key') if api_key.empty?
3335

3436
client = Temporalio::Client.connect(
3537
ENV.fetch('TEMPORAL_CLOUD_API_KEY_TEST_TARGET_HOST'),
3638
ENV.fetch('TEMPORAL_CLOUD_API_KEY_TEST_NAMESPACE'),
3739
api_key:,
38-
tls: true,
3940
rpc_metadata: { 'temporal-namespace' => ENV.fetch('TEMPORAL_CLOUD_API_KEY_TEST_NAMESPACE') }
4041
)
4142
# Run workflow
@@ -52,14 +53,14 @@ def test_api_key
5253
end
5354

5455
def test_cloud_ops
56+
# This test also validates auto-TLS: TLS is auto-enabled when api_key is provided.
5557
api_key = ENV.fetch('TEMPORAL_CLOUD_OPS_TEST_API_KEY', '')
5658
skip('No cloud API key') if api_key.empty?
5759

58-
# Create connection
60+
# Create connection (tls not set, auto-enabled due to api_key)
5961
conn = Temporalio::Client::Connection.new(
6062
target_host: ENV.fetch('TEMPORAL_CLOUD_OPS_TEST_TARGET_HOST'),
6163
api_key:,
62-
tls: true,
6364
rpc_metadata: { 'temporal-cloud-api-version' => ENV.fetch('TEMPORAL_CLOUD_OPS_TEST_API_VERSION') }
6465
)
6566

0 commit comments

Comments
 (0)