Skip to content

Commit df6eee8

Browse files
committed
enable TLS if api key specified
1 parent e6b89f1 commit df6eee8

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

internal/client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,11 @@ type (
542542
// TLS configures connection level security credentials.
543543
TLS *tls.Config
544544

545+
// TLSDisabled explicitly disables TLS. When true, TLS will not be used even
546+
// if API key credentials are provided (which would normally auto-enable TLS).
547+
// This is not recommended for production use as it sends credentials in plaintext.
548+
TLSDisabled bool
549+
545550
// Authority specifies the value to be used as the :authority pseudo-header.
546551
// This value only used when TLS is nil.
547552
Authority string
@@ -1210,7 +1215,13 @@ func NewAPIKeyDynamicCredentials(apiKeyCallback func(context.Context) (string, e
12101215
return apiKeyCredentials(apiKeyCallback)
12111216
}
12121217

1213-
func (apiKeyCredentials) applyToOptions(*ConnectionOptions) error { return nil }
1218+
func (apiKeyCredentials) applyToOptions(opts *ConnectionOptions) error {
1219+
// Auto-enable TLS when API key is provided and TLS is not explicitly set/disabled
1220+
if opts.TLS == nil && !opts.TLSDisabled {
1221+
opts.TLS = &tls.Config{}
1222+
}
1223+
return nil
1224+
}
12141225

12151226
func (a apiKeyCredentials) gRPCInterceptor() grpc.UnaryClientInterceptor { return a.gRPCIntercept }
12161227

internal/credentials_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package internal
2+
3+
import (
4+
"crypto/tls"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestAPIKeyCredentials_TLSEnabledByDefaultWhenAPIKeyProvided(t *testing.T) {
11+
creds := NewAPIKeyStaticCredentials("test-api-key")
12+
opts := &ConnectionOptions{}
13+
14+
err := creds.applyToOptions(opts)
15+
require.NoError(t, err)
16+
17+
// TLS should be auto-enabled when api_key is provided and tls not explicitly set
18+
require.NotNil(t, opts.TLS)
19+
}
20+
21+
func TestAPIKeyCredentials_TLSCanBeExplicitlyDisabledWithAPIKey(t *testing.T) {
22+
// Test that TLS can be explicitly disabled using TLSDisabled field
23+
creds := NewAPIKeyStaticCredentials("test-api-key")
24+
opts := &ConnectionOptions{
25+
TLSDisabled: true,
26+
}
27+
28+
err := creds.applyToOptions(opts)
29+
require.NoError(t, err)
30+
31+
// TLS should remain nil when TLSDisabled is true
32+
require.Nil(t, opts.TLS)
33+
}
34+
35+
func TestConnectionOptions_TLSDisabledByDefaultWithoutAPIKey(t *testing.T) {
36+
// Test that TLS is disabled by default when no API key is provided
37+
opts := &ConnectionOptions{}
38+
39+
// Without API key credentials, TLS should remain nil
40+
require.Nil(t, opts.TLS)
41+
}
42+
43+
func TestAPIKeyCredentials_ExplicitTLSConfigPreservedWithAPIKey(t *testing.T) {
44+
// Test that explicit TLS configuration is preserved when API key is provided
45+
tlsConfig := &tls.Config{
46+
ServerName: "test-domain",
47+
MinVersion: tls.VersionTLS12,
48+
}
49+
50+
creds := NewAPIKeyStaticCredentials("test-api-key")
51+
opts := &ConnectionOptions{
52+
TLS: tlsConfig,
53+
}
54+
55+
err := creds.applyToOptions(opts)
56+
require.NoError(t, err)
57+
58+
// Explicit TLS config should be preserved
59+
require.NotNil(t, opts.TLS)
60+
require.Equal(t, "test-domain", opts.TLS.ServerName)
61+
require.Equal(t, uint16(tls.VersionTLS12), opts.TLS.MinVersion)
62+
}

0 commit comments

Comments
 (0)