-
Notifications
You must be signed in to change notification settings - Fork 918
GODRIVER-3548 Test MONGODB-X509 on cloud-dev #2166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
9922f9f
72a2d63
5b31916
4fc9667
9967f6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,56 +8,161 @@ package main | |
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"encoding/base64" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/v2/bson" | ||
"go.mongodb.org/mongo-driver/v2/internal/assert" | ||
"go.mongodb.org/mongo-driver/v2/internal/handshake" | ||
"go.mongodb.org/mongo-driver/v2/internal/require" | ||
"go.mongodb.org/mongo-driver/v2/mongo" | ||
"go.mongodb.org/mongo-driver/v2/mongo/options" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
flag.Parse() | ||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestAtlas(t *testing.T) { | ||
uris := flag.Args() | ||
ctx := context.Background() | ||
|
||
t.Logf("Running atlas tests for %d uris\n", len(uris)) | ||
|
||
for idx, uri := range uris { | ||
t.Logf("Running test %d\n", idx) | ||
|
||
// Set a low server selection timeout so we fail fast if there are errors. | ||
clientOpts := options.Client(). | ||
ApplyURI(uri). | ||
SetServerSelectionTimeout(1 * time.Second) | ||
|
||
// Run basic connectivity test. | ||
if err := runTest(ctx, clientOpts); err != nil { | ||
t.Fatalf("error running test with TLS at index %d: %v", idx, err) | ||
} | ||
|
||
tlsConfigSkipVerify := clientOpts.TLSConfig | ||
tlsConfigSkipVerify.InsecureSkipVerify = true | ||
|
||
// Run the connectivity test with InsecureSkipVerify to ensure SNI is done correctly even if verification is | ||
// disabled. | ||
clientOpts.SetTLSConfig(tlsConfigSkipVerify) | ||
|
||
if err := runTest(ctx, clientOpts); err != nil { | ||
t.Fatalf("error running test with tlsInsecure at index %d: %v", idx, err) | ||
} | ||
cases := []struct { | ||
name string | ||
envVar string | ||
certKeyFile string | ||
wantErr string | ||
}{ | ||
{ | ||
name: "Atlas with TLS", | ||
envVar: "ATLAS_REPL", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with TLS and shared cluster", | ||
envVar: "ATLAS_SHRD", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with free tier", | ||
envVar: "ATLAS_FREE", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with TLS 1.1", | ||
envVar: "ATLAS_TLS11", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with TLS 1.2", | ||
envVar: "ATLAS_TLS12", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with serverless", | ||
envVar: "ATLAS_SERVERLESS", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with srv file on replica set", | ||
envVar: "ATLAS_SRV_REPL", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with srv file on shared cluster", | ||
envVar: "ATLAS_SRV_SHRD", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with srv file on free tier", | ||
envVar: "ATLAS_SRV_FREE", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with srv file on TLS 1.1", | ||
envVar: "ATLAS_SRV_TLS11", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with srv file on TLS 1.2", | ||
envVar: "ATLAS_SRV_TLS12", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with srv file on serverless", | ||
envVar: "ATLAS_SRV_SERVERLESS", | ||
certKeyFile: "", | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with X509 Dev", | ||
envVar: "ATLAS_X509_DEV", | ||
certKeyFile: createAtlasX509DevCertKeyFile(t), | ||
wantErr: "", | ||
}, | ||
{ | ||
name: "Atlas with X509 Dev no user", | ||
envVar: "ATLAS_X509_DEV", | ||
certKeyFile: createAtlasX509DevCertKeyFileNoUser(t), | ||
wantErr: "UserNotFound", | ||
}, | ||
} | ||
|
||
t.Logf("Finished!") | ||
for _, tc := range cases { | ||
t.Run(fmt.Sprintf("%s (%s)", tc.name, tc.envVar), func(t *testing.T) { | ||
uri := os.Getenv(tc.envVar) | ||
if uri == "" { | ||
t.Skipf("Environment variable %q is not set", tc.envVar) | ||
} | ||
|
||
|
||
if tc.certKeyFile != "" { | ||
uri = addTLSCertKeyFile(t, tc.certKeyFile, uri) | ||
} | ||
|
||
// Set a low server selection timeout so we fail fast if there are errors. | ||
clientOpts := options.Client(). | ||
ApplyURI(uri). | ||
SetServerSelectionTimeout(1 * time.Second) | ||
|
||
// Run basic connectivity test. | ||
err := runTest(context.Background(), clientOpts) | ||
if tc.wantErr != "" { | ||
assert.ErrorContains(t, err, tc.wantErr, "expected error to contain %q", tc.wantErr) | ||
|
||
return | ||
} | ||
require.NoError(t, err, "error running test with TLS") | ||
|
||
orig := clientOpts.TLSConfig | ||
if orig == nil { | ||
orig = &tls.Config{} | ||
} | ||
|
||
insecure := orig.Clone() | ||
insecure.InsecureSkipVerify = true | ||
|
||
// Run the connectivity test with InsecureSkipVerify to ensure SNI is done | ||
// correctly even if verification is disabled. | ||
insecureClientOpts := options.Client(). | ||
ApplyURI(uri). | ||
SetServerSelectionTimeout(1 * time.Second). | ||
SetTLSConfig(insecure) | ||
|
||
err = runTest(context.Background(), insecureClientOpts) | ||
require.NoError(t, err, "error running test with tlsInsecure") | ||
}) | ||
} | ||
} | ||
|
||
func runTest(ctx context.Context, clientOpts *options.ClientOptions) error { | ||
|
@@ -83,3 +188,51 @@ func runTest(ctx context.Context, clientOpts *options.ClientOptions) error { | |
} | ||
return nil | ||
} | ||
|
||
func createAtlasX509DevCertKeyFile(t *testing.T) string { | ||
t.Helper() | ||
|
||
b64 := os.Getenv("ATLAS_X509_DEV_CERT_BASE64") | ||
assert.NotEmpty(t, b64, "Environment variable ATLAS_X509_DEV_CERT_BASE64 is not set") | ||
|
||
certBytes, err := base64.StdEncoding.DecodeString(b64) | ||
require.NoError(t, err, "failed to decode ATLAS_X509_DEV_CERT_BASE64") | ||
|
||
certFilePath := t.TempDir() + "/atlas_x509_dev_cert.pem" | ||
prestonvasquez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
err = os.WriteFile(certFilePath, certBytes, 0600) | ||
require.NoError(t, err, "failed to write ATLAS_X509_DEV_CERT_BASE64 to file") | ||
|
||
return certFilePath | ||
} | ||
|
||
func createAtlasX509DevCertKeyFileNoUser(t *testing.T) string { | ||
t.Helper() | ||
|
||
b64 := os.Getenv("ATLAS_X509_DEV_CERT_NOUSER_BASE64") | ||
assert.NotEmpty(t, b64, "Environment variable ATLAS_X509_DEV_CERT_NOUSER_BASE64 is not set") | ||
|
||
keyBytes, err := base64.StdEncoding.DecodeString(b64) | ||
require.NoError(t, err, "failed to decode ATLAS_X509_DEV_CERT_NOUSER_BASE64") | ||
|
||
keyFilePath := t.TempDir() + "/atlas_x509_dev_cert_no_user.pem" | ||
prestonvasquez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
err = os.WriteFile(keyFilePath, keyBytes, 0600) | ||
require.NoError(t, err, "failed to write ATLAS_X509_DEV_CERT_NOUSER_BASE64 to file") | ||
|
||
return keyFilePath | ||
} | ||
|
||
func addTLSCertKeyFile(t *testing.T, certKeyFile, uri string) string { | ||
t.Helper() | ||
|
||
u, err := url.Parse(uri) | ||
require.NoError(t, err, "failed to parse uri") | ||
|
||
q := u.Query() | ||
q.Set("tlsCertificateKeyFile", filepath.ToSlash(certKeyFile)) | ||
|
||
u.RawQuery = q.Encode() | ||
|
||
return u.String() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems none of the URI env vars were previously populated, effectively making this test a no-op. It's unclear when that change happened because the test passes silently.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests do pass and are not skipped on the correct task: https://parsley.mongodb.com/test/mongo_go_driver_atlas_test_atlas_test_patch_5b79d946e0414b30e7a4ce53beedd43abef23eb2_689a9b41febc2d0007ffba2d_25_08_12_01_39_16/0/f9ccdfa9f8beaef7f7d584890b0b7de3?bookmarks=0,31&shareLine=1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see that now, thanks for the clarification!