-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: connect to rethinkdb with custom root certificate (#116)
* Connect to rethinkdb with a custom certificate Signed-off-by: Matteo Suppo <[email protected]> * Test importRethinkDBRootCA Signed-off-by: Matteo Suppo <[email protected]> * Move backend_connections tests Signed-off-by: Matteo Suppo <[email protected]>
- Loading branch information
1 parent
5b44457
commit 74432b0
Showing
3 changed files
with
131 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package config | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
r "gopkg.in/dancannon/gorethink.v2" | ||
) | ||
|
||
var cert1 = `-----BEGIN CERTIFICATE----- | ||
MIIB/zCCAamgAwIBAgIJAPKYmr9KduB0MA0GCSqGSIb3DQEBCwUAMFsxCzAJBgNV | ||
BAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRQwEgYDVQQHDAtMb3MgQW5nZWxl | ||
czEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMB4XDTE2MDYyMzEx | ||
NTM0NloXDTI2MDYyMTExNTM0NlowWzELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNh | ||
bGlmb3JuaWExFDASBgNVBAcMC0xvcyBBb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OU46GAdGNQ7UWDQEjMAwGA1UdEwQFMAMBAf8w | ||
DQYJKoZIhvcNAQELBQADQQC4aXmUbyBiVaXWnkKCGzbf5Uxs6y3td+togd1La1mn | ||
d9ahfgeVHNG0Dz9VJu2LA3aB4pAWPlbxd2m0frzK1Sx4 | ||
-----END CERTIFICATE-----` | ||
|
||
var TestImportRethinkDBRootCAData = []struct { | ||
Name string | ||
EnvKey string | ||
EnvValue string | ||
Cert string | ||
}{ | ||
{"Without Certificate", "", "", ""}, | ||
{"Certificate string 1", "RETHINK_TLS_CERT", cert1, cert1}, | ||
{"Certificate string 2", "RETHINK_TLS_CERT", cert2, cert2}, | ||
{"Certificate path 1", "RETHINK_TLS_CERT_PATH", "cert1.pem", cert1}, | ||
{"Certificate path 2", "RETHINK_TLS_CERT_PATH", "cert2.pem", cert2}, | ||
} | ||
|
||
func TestImportRethinkDBRootCA(t *testing.T) { | ||
ioutil.WriteFile("cert1.pem", []byte(cert1), 0644) | ||
ioutil.WriteFile("cert2.pem", []byte(cert2), 0644) | ||
|
||
for _, test := range TestImportRethinkDBRootCAData { | ||
viper.Reset() | ||
if test.EnvKey != "" { | ||
viper.Set(test.EnvKey, test.EnvValue) | ||
} | ||
|
||
opts := r.ConnectOpts{} | ||
|
||
importRethinkDBRootCA(&opts) | ||
|
||
if test.Cert != "" { | ||
require.NotNil(t, opts.TLSConfig, test.Name) | ||
|
||
// We try to add the same certificate twice to see if it has added the correct one | ||
assert.Equal(t, 1, len(opts.TLSConfig.RootCAs.Subjects()), test.Name) | ||
opts.TLSConfig.RootCAs.AppendCertsFromPEM([]byte(test.Cert)) | ||
assert.Equal(t, 1, len(opts.TLSConfig.RootCAs.Subjects()), test.Name) | ||
} | ||
} | ||
|
||
// Cleanup | ||
viper.Reset() | ||
os.Remove("cert1.pem") | ||
os.Remove("cert2.pem") | ||
} |