-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added support for TLS * added no-tls tests --------- Co-authored-by: Lucas Theisen <[email protected]>
- Loading branch information
1 parent
c9657f0
commit d37d262
Showing
3 changed files
with
274 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,43 @@ | ||
package gohive | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
// This certificate was sourced from the examples in go's documentation | ||
// for [x509 Certificate.Verify]. | ||
// | ||
// [x509 Certificate.Verify]: https://pkg.go.dev/crypto/x509#example-Certificate.Verify | ||
rootPEM = `-----BEGIN CERTIFICATE----- | ||
MIIEBDCCAuygAwIBAgIDAjppMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT | ||
MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i | ||
YWwgQ0EwHhcNMTMwNDA1MTUxNTU1WhcNMTUwNDA0MTUxNTU1WjBJMQswCQYDVQQG | ||
EwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzElMCMGA1UEAxMcR29vZ2xlIEludGVy | ||
bmV0IEF1dGhvcml0eSBHMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB | ||
AJwqBHdc2FCROgajguDYUEi8iT/xGXAaiEZ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PX4WecigUCAkVDNx | ||
-----END CERTIFICATE-----` | ||
) | ||
|
||
func TestParseDSNWithSessionConf(t *testing.T) { | ||
|
@@ -100,6 +134,59 @@ func TestParseDSNWithoutDBName(t *testing.T) { | |
assert.Equal(t, cfg.Addr, "127.0.0.1") | ||
} | ||
|
||
func TestParseDSNWithTLSConfig(t *testing.T) { | ||
b64 := base64.URLEncoding.EncodeToString([]byte(rootPEM)) | ||
t.Run("no tls", func(t *testing.T) { | ||
cfg, e := ParseDSN("127.0.0.1") | ||
require.NoError(t, e) | ||
require.Nil(t, cfg.TLSCfg) | ||
}) | ||
|
||
t.Run("one ca", func(t *testing.T) { | ||
cfg, e := ParseDSN(fmt.Sprintf("127.0.0.1?tls.root_ca=%s", b64)) | ||
require.NoError(t, e) | ||
require.NotNil(t, cfg.TLSCfg) | ||
require.Len(t, cfg.TLSCfg.RootCAs, 1) | ||
require.Equal(t, cfg.TLSCfg.RootCAs[0], rootPEM) | ||
}) | ||
|
||
t.Run("two cas", func(t *testing.T) { | ||
cfg, e := ParseDSN(fmt.Sprintf("127.0.0.1?tls.root_ca=%s&tls.root_ca=%s", b64, b64)) | ||
require.NoError(t, e) | ||
require.NotNil(t, cfg.TLSCfg) | ||
require.Len(t, cfg.TLSCfg.RootCAs, 2) | ||
require.Equal(t, cfg.TLSCfg.RootCAs[0], rootPEM) | ||
require.Equal(t, cfg.TLSCfg.RootCAs[1], rootPEM) | ||
}) | ||
|
||
t.Run("one ca fiel", func(t *testing.T) { | ||
file := "cert.pem" | ||
cfg, e := ParseDSN(fmt.Sprintf("127.0.0.1?tls.root_ca_file=%s", file)) | ||
require.NoError(t, e) | ||
require.NotNil(t, cfg.TLSCfg) | ||
require.Len(t, cfg.TLSCfg.RootCAFiles, 1) | ||
require.Equal(t, cfg.TLSCfg.RootCAFiles[0], file) | ||
}) | ||
|
||
t.Run("two ca files", func(t *testing.T) { | ||
file := "cert.pem" | ||
file2 := "cert2.pem" | ||
cfg, e := ParseDSN(fmt.Sprintf("127.0.0.1?tls.root_ca_file=%s&tls.root_ca_file=%s", file, file2)) | ||
require.NoError(t, e) | ||
require.NotNil(t, cfg.TLSCfg) | ||
require.Len(t, cfg.TLSCfg.RootCAFiles, 2) | ||
require.Equal(t, cfg.TLSCfg.RootCAFiles[0], file) | ||
require.Equal(t, cfg.TLSCfg.RootCAFiles[1], file2) | ||
}) | ||
|
||
t.Run("insecure skip verify", func(t *testing.T) { | ||
cfg, e := ParseDSN("127.0.0.1?tls.insecure_skip_verify=true") | ||
require.NoError(t, e) | ||
require.NotNil(t, cfg.TLSCfg) | ||
require.True(t, cfg.TLSCfg.InsecureSkipVerify) | ||
}) | ||
} | ||
|
||
func TestFormatDSNWithDBName(t *testing.T) { | ||
ds := "user:[email protected]/mnist?batch=100000&auth=NOSASL" | ||
cfg, e := ParseDSN(ds) | ||
|
@@ -117,3 +204,72 @@ func TestFormatDSNWithoutDBName(t *testing.T) { | |
ds2 := cfg.FormatDSN() | ||
assert.Equal(t, ds2, ds) | ||
} | ||
|
||
func TestFormatDSNWithTLSConfig(t *testing.T) { | ||
b64 := base64.URLEncoding.EncodeToString([]byte(rootPEM)) | ||
t.Run("no tls", func(t *testing.T) { | ||
require.Equal(t, | ||
":@127.0.0.1?batch=0", | ||
(&Config{ | ||
Addr: "127.0.0.1", | ||
}).FormatDSN()) | ||
}) | ||
|
||
t.Run("one ca", func(t *testing.T) { | ||
require.Equal(t, | ||
fmt.Sprintf(":@127.0.0.1?batch=0&tls.root_ca=%s", b64), | ||
(&Config{ | ||
Addr: "127.0.0.1", | ||
TLSCfg: &TLSConfig{ | ||
RootCAs: []string{rootPEM}, | ||
}, | ||
}).FormatDSN()) | ||
}) | ||
|
||
t.Run("two cas", func(t *testing.T) { | ||
require.Equal(t, | ||
fmt.Sprintf(":@127.0.0.1?batch=0&tls.root_ca=%s&tls.root_ca=%s", b64, b64), | ||
(&Config{ | ||
Addr: "127.0.0.1", | ||
TLSCfg: &TLSConfig{ | ||
RootCAs: []string{rootPEM, rootPEM}, | ||
}, | ||
}).FormatDSN()) | ||
}) | ||
|
||
t.Run("one ca file", func(t *testing.T) { | ||
file := "cert.pem" | ||
require.Equal(t, | ||
fmt.Sprintf(":@127.0.0.1?batch=0&tls.root_ca_file=%s", file), | ||
(&Config{ | ||
Addr: "127.0.0.1", | ||
TLSCfg: &TLSConfig{ | ||
RootCAFiles: []string{file}, | ||
}, | ||
}).FormatDSN()) | ||
}) | ||
|
||
t.Run("two ca files", func(t *testing.T) { | ||
file := "cert.pem" | ||
file2 := "cert2.pem" | ||
require.Equal(t, | ||
fmt.Sprintf(":@127.0.0.1?batch=0&tls.root_ca_file=%s&tls.root_ca_file=%s", file, file2), | ||
(&Config{ | ||
Addr: "127.0.0.1", | ||
TLSCfg: &TLSConfig{ | ||
RootCAFiles: []string{file, file2}, | ||
}, | ||
}).FormatDSN()) | ||
}) | ||
|
||
t.Run("insecure skip verify", func(t *testing.T) { | ||
require.Equal(t, | ||
":@127.0.0.1?batch=0&tls.insecure_skip_verify=true", | ||
(&Config{ | ||
Addr: "127.0.0.1", | ||
TLSCfg: &TLSConfig{ | ||
InsecureSkipVerify: true, | ||
}, | ||
}).FormatDSN()) | ||
}) | ||
} |