Skip to content

Commit 6824503

Browse files
authored
GODRIVER-3321 Fix CSE SetTLSConfig option. (#1900)
1 parent 153ea1d commit 6824503

File tree

3 files changed

+39
-48
lines changed

3 files changed

+39
-48
lines changed

internal/integration/client_side_encryption_prose_test.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,10 @@ func TestClientSideEncryptionProse(t *testing.T) {
14441444
if os.Getenv("KMS_MOCK_SERVERS_RUNNING") == "" {
14451445
mt.Skipf("Skipping test as KMS_MOCK_SERVERS_RUNNING is not set")
14461446
}
1447+
if tlsCAFileKMIP == "" || tlsClientCertificateKeyFileKMIP == "" {
1448+
mt.Fatal("Env vars CSFLE_TLS_CA_FILE and CSFLE_TLS_CLIENT_CERT_FILE must be set")
1449+
}
1450+
14471451
validKmsProviders := map[string]map[string]interface{}{
14481452
"aws": {
14491453
"accessKeyId": awsAccessKeyID,
@@ -1513,50 +1517,50 @@ func TestClientSideEncryptionProse(t *testing.T) {
15131517
SetKeyVaultNamespace(kvNamespace)
15141518

15151519
// make TLS opts containing client certificate and CA file
1516-
tlsConfig := make(map[string]*tls.Config)
1517-
if tlsCAFileKMIP != "" && tlsClientCertificateKeyFileKMIP != "" {
1518-
clientAndCATlsMap := map[string]interface{}{
1519-
"tlsCertificateKeyFile": tlsClientCertificateKeyFileKMIP,
1520-
"tlsCAFile": tlsCAFileKMIP,
1521-
}
1522-
certConfig, err := options.BuildTLSConfig(clientAndCATlsMap)
1523-
assert.Nil(mt, err, "BuildTLSConfig error: %v", err)
1524-
tlsConfig["aws"] = certConfig
1525-
tlsConfig["azure"] = certConfig
1526-
tlsConfig["gcp"] = certConfig
1527-
tlsConfig["kmip"] = certConfig
1528-
}
1520+
clientAndCATLSConfig, err := options.BuildTLSConfig(map[string]interface{}{
1521+
"tlsCertificateKeyFile": tlsClientCertificateKeyFileKMIP,
1522+
"tlsCAFile": tlsCAFileKMIP,
1523+
})
1524+
assert.Nil(mt, err, "BuildTLSConfig error: %v", err)
15291525

15301526
// create valid Client Encryption options and set valid TLS options
15311527
validClientEncryptionOptionsWithTLS := options.ClientEncryption().
15321528
SetKmsProviders(validKmsProviders).
15331529
SetKeyVaultNamespace(kvNamespace).
1534-
SetTLSConfig(tlsConfig)
1530+
SetTLSConfig(map[string]*tls.Config{
1531+
"aws": clientAndCATLSConfig,
1532+
"azure": clientAndCATLSConfig,
1533+
"gcp": clientAndCATLSConfig,
1534+
"kmip": clientAndCATLSConfig,
1535+
})
15351536

15361537
// make TLS opts containing only CA file
1537-
if tlsCAFileKMIP != "" {
1538-
caTlsMap := map[string]interface{}{
1539-
"tlsCAFile": tlsCAFileKMIP,
1540-
}
1541-
certConfig, err := options.BuildTLSConfig(caTlsMap)
1542-
assert.Nil(mt, err, "BuildTLSConfig error: %v", err)
1543-
tlsConfig["aws"] = certConfig
1544-
tlsConfig["azure"] = certConfig
1545-
tlsConfig["gcp"] = certConfig
1546-
tlsConfig["kmip"] = certConfig
1547-
}
1538+
caTLSConfig, err := options.BuildTLSConfig(map[string]interface{}{
1539+
"tlsCAFile": tlsCAFileKMIP,
1540+
})
1541+
assert.Nil(mt, err, "BuildTLSConfig error: %v", err)
15481542

15491543
// create invalid Client Encryption options with expired credentials
15501544
expiredClientEncryptionOptions := options.ClientEncryption().
15511545
SetKmsProviders(expiredKmsProviders).
15521546
SetKeyVaultNamespace(kvNamespace).
1553-
SetTLSConfig(tlsConfig)
1547+
SetTLSConfig(map[string]*tls.Config{
1548+
"aws": caTLSConfig,
1549+
"azure": caTLSConfig,
1550+
"gcp": caTLSConfig,
1551+
"kmip": caTLSConfig,
1552+
})
15541553

15551554
// create invalid Client Encryption options with invalid hostnames
15561555
invalidHostnameClientEncryptionOptions := options.ClientEncryption().
15571556
SetKmsProviders(invalidKmsProviders).
15581557
SetKeyVaultNamespace(kvNamespace).
1559-
SetTLSConfig(tlsConfig)
1558+
SetTLSConfig(map[string]*tls.Config{
1559+
"aws": caTLSConfig,
1560+
"azure": caTLSConfig,
1561+
"gcp": caTLSConfig,
1562+
"kmip": caTLSConfig,
1563+
})
15601564

15611565
awsMasterKeyNoClientCert := map[string]interface{}{
15621566
"region": "us-east-1",
@@ -1622,7 +1626,8 @@ func TestClientSideEncryptionProse(t *testing.T) {
16221626

16231627
possibleErrors := []string{
16241628
"x509: certificate signed by unknown authority", // Windows
1625-
"x509: “valid.testing.golang.invalid” certificate is not trusted", // MacOS
1629+
"x509: “valid.testing.golang.invalid” certificate is not trusted", // macOS
1630+
"x509: “server” certificate is not standards compliant", // macOS
16261631
"x509: certificate is not authorized to sign other certificates", // All others
16271632
}
16281633

mongo/options/autoencryptionoptions.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,9 @@ func (a *AutoEncryptionOptionsBuilder) SetExtraOptions(extraOpts map[string]inte
184184
// to the KMS provider.
185185
//
186186
// This should only be used to set custom TLS configurations. By default, the connection will use an empty tls.Config{} with MinVersion set to tls.VersionTLS12.
187-
func (a *AutoEncryptionOptionsBuilder) SetTLSConfig(tlsOpts map[string]*tls.Config) *AutoEncryptionOptionsBuilder {
187+
func (a *AutoEncryptionOptionsBuilder) SetTLSConfig(cfg map[string]*tls.Config) *AutoEncryptionOptionsBuilder {
188188
a.Opts = append(a.Opts, func(args *AutoEncryptionOptions) error {
189-
tlsConfigs := make(map[string]*tls.Config)
190-
for provider, config := range tlsOpts {
191-
// use TLS min version 1.2 to enforce more secure hash algorithms and advanced cipher suites
192-
if config.MinVersion == 0 {
193-
config.MinVersion = tls.VersionTLS12
194-
}
195-
tlsConfigs[provider] = config
196-
}
197-
args.TLSConfig = tlsConfigs
189+
args.TLSConfig = cfg
198190

199191
return nil
200192
})

mongo/options/clientencryptionoptions.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,13 @@ func (c *ClientEncryptionOptionsBuilder) SetKmsProviders(providers map[string]ma
7070
// to the KMS provider.
7171
//
7272
// This should only be used to set custom TLS configurations. By default, the connection will use an empty tls.Config{} with MinVersion set to tls.VersionTLS12.
73-
func (c *ClientEncryptionOptionsBuilder) SetTLSConfig(tlsOpts map[string]*tls.Config) *ClientEncryptionOptionsBuilder {
73+
func (c *ClientEncryptionOptionsBuilder) SetTLSConfig(cfg map[string]*tls.Config) *ClientEncryptionOptionsBuilder {
7474
c.Opts = append(c.Opts, func(opts *ClientEncryptionOptions) error {
75-
tlsConfigs := make(map[string]*tls.Config)
76-
for provider, config := range tlsOpts {
77-
// use TLS min version 1.2 to enforce more secure hash algorithms and advanced cipher suites
78-
if config.MinVersion == 0 {
79-
config.MinVersion = tls.VersionTLS12
80-
}
81-
tlsConfigs[provider] = config
82-
}
83-
opts.TLSConfig = tlsConfigs
75+
opts.TLSConfig = cfg
76+
8477
return nil
8578
})
79+
8680
return c
8781
}
8882

0 commit comments

Comments
 (0)