Skip to content

Commit 88dbbf2

Browse files
committed
add tests and examples
1 parent f4f7bc0 commit 88dbbf2

17 files changed

Lines changed: 951 additions & 26 deletions

File tree

.evergreen/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ functions:
468468
type: test
469469
params:
470470
binary: "bash"
471+
env:
472+
AWS_TEST: ecs
471473
include_expansions_in_env: [SKIP_ECS_AUTH_TEST]
472474
args: [*task-runner, evg-test-aws-ecs]
473475
run-aws-auth-test-with-aws-web-identity-credentials:

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
go build ./...
5353
go test -short -run ^$$ ./...
5454
55-
go test -v ./internal/test/compilecheck -run '^TestCompileCheck/go:1\.19$'
55+
go test -v ./internal/test/compilecheck -run '^TestCompileCheck/main_driver/go:1\.19$'
5656
- name: Perform CodeQL Analysis
5757
uses: github/codeql-action/analyze@v4.37.0 #immutable
5858
with:

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
4747
# Install taskfile
4848
RUN go install github.com/go-task/task/v3/cmd/task@v3.39.2
4949

50+
# Pre-download the Go toolchains used by internal/test/compilecheck so the
51+
# compile check does not download them at runtime (the largest cost of that
52+
# test). The parent Go can fetch and run older toolchain modules, and a single
53+
# linux-amd64 toolchain per version cross-compiles for every GOARCH. Placed
54+
# before "COPY . /mongo-go-driver" so repo changes don't invalidate this layer.
55+
# Keep this list in sync with the GoVersions in
56+
# internal/test/compilecheck/compile_check_test.go.
57+
RUN for v in 1.19.0 1.20.0 1.21.0 1.22.0 1.23.0 1.24.0 1.25.0; do \
58+
GOTOOLCHAIN=go$v go version; \
59+
done
60+
5061
COPY etc/docker_entry.sh /root/docker_entry.sh
5162
COPY --from=libmongocrypt /root/install /root/install
5263

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ tasks:
2323
dir: internal/test/compilecheck
2424
cmds:
2525
- go mod download
26-
- GOTOOLCHAIN=auto go test -v -run '^TestCompileCheck/go:1\.19$'
26+
- GOTOOLCHAIN=auto go test -v -run '^TestCompileCheck/main_driver/go:1\.19$'
2727
build-compile-check-all: bash etc/run-compile-check-test.sh
2828
build-aws-ecs-test: go test -c ./internal/test/aws -o aws.testbin
2929
check-fmt:

etc/run-mongodb-aws-test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ set -x
3232
# For Go 1.16+, Go builds requires a go.mod file in the current working directory or a parent
3333
# directory. Spawn a new subshell, "cd" to the project directory, then run "go run".
3434
(cd ${PROJECT_DIRECTORY} && go test -timeout 30m -v ./internal/test/aws/... | tee -a test.suite)
35+
36+
# Also run the ext/awsauth integration test, which uses the AWS SDK default credential
37+
# chain to cover scenarios where credentials are not embedded in the URI (EC2, ECS,
38+
# WebIdentity) in addition to the inline-credential scenarios.
39+
(cd ${PROJECT_DIRECTORY}/ext/awsauth/test && go test -timeout 30m -v ./... | tee -a test.suite)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) MongoDB, Inc. 2017-present.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
package awsauth_test
8+
9+
import (
10+
"context"
11+
12+
"github.com/aws/aws-sdk-go-v2/config"
13+
"go.mongodb.org/mongo-driver/ext/awsauth"
14+
"go.mongodb.org/mongo-driver/v2/mongo"
15+
"go.mongodb.org/mongo-driver/v2/mongo/options"
16+
)
17+
18+
func Example_defaultConfig() {
19+
cfg, err := config.LoadDefaultConfig(context.TODO())
20+
if err != nil {
21+
panic(err)
22+
}
23+
awsCredentialProvider := awsauth.NewCredentialsProvider(cfg.Credentials)
24+
credential := options.Credential{
25+
AuthMechanism: "MONGODB-AWS",
26+
AWSCredentialsProvider: awsCredentialProvider,
27+
}
28+
client, err := mongo.Connect(
29+
options.Client().SetAuth(credential),
30+
)
31+
if err != nil {
32+
panic(err)
33+
}
34+
_ = client
35+
}

ext/awsauth/examples/go.mod

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module go.mongodb.org/mongo-driver/ext/awsauth/examples
2+
3+
go 1.25.0
4+
5+
replace (
6+
go.mongodb.org/mongo-driver/ext/awsauth => ..
7+
go.mongodb.org/mongo-driver/v2 => ../../..
8+
)
9+
10+
require (
11+
github.com/aws/aws-sdk-go-v2/config v1.32.30
12+
go.mongodb.org/mongo-driver/ext/awsauth v0.0.0
13+
go.mongodb.org/mongo-driver/v2 v2.0.0-beta2
14+
)
15+
16+
require (
17+
github.com/aws/aws-sdk-go-v2 v1.42.1 // indirect
18+
github.com/aws/aws-sdk-go-v2/credentials v1.19.29 // indirect
19+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.30 // indirect
20+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.30 // indirect
21+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.30 // indirect
22+
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.31 // indirect
23+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.13 // indirect
24+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.30 // indirect
25+
github.com/aws/aws-sdk-go-v2/service/signin v1.4.1 // indirect
26+
github.com/aws/aws-sdk-go-v2/service/sso v1.32.1 // indirect
27+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.37.1 // indirect
28+
github.com/aws/aws-sdk-go-v2/service/sts v1.44.1 // indirect
29+
github.com/aws/smithy-go v1.27.3 // indirect
30+
github.com/klauspost/compress v1.17.6 // indirect
31+
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
32+
github.com/xdg-go/scram v1.2.0 // indirect
33+
github.com/xdg-go/stringprep v1.0.4 // indirect
34+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
35+
golang.org/x/crypto v0.33.0 // indirect
36+
golang.org/x/sync v0.11.0 // indirect
37+
golang.org/x/text v0.22.0 // indirect
38+
)

ext/awsauth/examples/go.sum

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
github.com/aws/aws-sdk-go-v2 v1.42.1 h1:9eOTgu1z/dVtYpNZ3/8/XbbaX0x/BqE3HUzAzs6K0ek=
2+
github.com/aws/aws-sdk-go-v2 v1.42.1/go.mod h1:5pKeft2eJj+gElQ38Jqg4ibCqh+/AK33/0X3hip7IjM=
3+
github.com/aws/aws-sdk-go-v2/config v1.32.30 h1:XwsEzpTJfQYJbFicz/QMLwAZdyeNVVoOEkbF7R3gPJk=
4+
github.com/aws/aws-sdk-go-v2/config v1.32.30/go.mod h1:Ud32SuMc+/9BGxfpSVld7HrE2o05JwKmXY4M3jOQNZU=
5+
github.com/aws/aws-sdk-go-v2/credentials v1.19.29 h1:WHZGssHH887cO0ox07SIQZsFx3MKD4ps6w0xUEmnKYQ=
6+
github.com/aws/aws-sdk-go-v2/credentials v1.19.29/go.mod h1:Mhl0xR6zjguiuj00XRx2wMx22sAltk7oya39sT7fdg8=
7+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.30 h1:/hi1JADLEW9YYryEz1w4GQu0EtP23pP553Cf9KgsDV4=
8+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.30/go.mod h1:/3AOgy4K17Dm4ucMZVC/MJkzy5kmfKUcINRHZyo0koQ=
9+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.30 h1:xM/Is9cKMHa8Jj8zkvWhvrFkZsXJV9E+BB4g0HW0duQ=
10+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.30/go.mod h1:WueJeNDZvK1fMYEWJIkcivBfEzUkTpBhzlrUKKY8EuA=
11+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.30 h1:jn46zC9LdsVR/ZpMIJqMqb8hHv31BlLx3ulVqNspUOk=
12+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.30/go.mod h1:1hTMsAgbdS/AtUi4bw8+gUuh1pceo+eXRLfpSuSQj3M=
13+
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.31 h1:3GUprIsfmGcC5SACIyB0e7E0BM1O1b3Erl5CePYIAeQ=
14+
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.31/go.mod h1:7PuV1yl5e2xnUbm+RqvVg5i2iBM8EyijZNoI9wsOoOc=
15+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.13 h1:mbRIur/BiHK6SKPjoBIXSE/hJ6g6JGRLuxQy1jGjlN4=
16+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.13/go.mod h1:ITg9em2KbJx1s0y4aqRX5OYWG6HBZ5TVR//OdpEZ2CQ=
17+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.30 h1:/Z5jmNrKsSD7EmDjzAPsm/3L9IuOkzaynklJZ1qX7S4=
18+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.30/go.mod h1:lEzEZnOosE7zi8Z6royW1cFJTD9fpab4Ul1SBrllewk=
19+
github.com/aws/aws-sdk-go-v2/service/signin v1.4.1 h1:V7ZZ300WPXGjvkyore5DGe0ljVPOxCXie/thWdtSBXE=
20+
github.com/aws/aws-sdk-go-v2/service/signin v1.4.1/go.mod h1:mxC0nT/C8wMMS97DemZPzvUZxvIt+2Iq+eS3JdFZGgg=
21+
github.com/aws/aws-sdk-go-v2/service/sso v1.32.1 h1:gYFYh4iLLcAOJRLNPY2aD2g9DIhKn4eof8UkIrr1rTk=
22+
github.com/aws/aws-sdk-go-v2/service/sso v1.32.1/go.mod h1:u8af9Nqkmqnr96f7v9nHqzZT9XBwbXEkTiqT4ROuJSE=
23+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.37.1 h1:arjT9Cm3/WYbGmD5TUZHk4UQn4Lle1fUNZs5FC6CtF0=
24+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.37.1/go.mod h1:DMPWJBjYs6+3+f/qhBFEFPPlQ6NlhWjai3dJNvipJ84=
25+
github.com/aws/aws-sdk-go-v2/service/sts v1.44.1 h1:RvfHDg+xvAeZ+5741vUEjpOVtYSIm93W2zhx10Xtydw=
26+
github.com/aws/aws-sdk-go-v2/service/sts v1.44.1/go.mod h1:9gdl4RrflIdpDb2TlXshWgR1F9TeCkvqDx77Vpr4Z/Q=
27+
github.com/aws/smithy-go v1.27.3 h1:F3Zb497UhhskkfpJmfkXswyo+t0sh9OTBnIHjogWbVY=
28+
github.com/aws/smithy-go v1.27.3/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
29+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
30+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
31+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
32+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
33+
github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI=
34+
github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
35+
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
36+
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
37+
github.com/xdg-go/scram v1.2.0 h1:bYKF2AEwG5rqd1BumT4gAnvwU/M9nBp2pTSxeZw7Wvs=
38+
github.com/xdg-go/scram v1.2.0/go.mod h1:3dlrS0iBaWKYVt2ZfA4cj48umJZ+cAEbR6/SjLA88I8=
39+
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
40+
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
41+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
42+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
43+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
44+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
45+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
46+
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
47+
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
48+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
49+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
50+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
51+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
52+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
53+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
54+
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
55+
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
56+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
57+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
58+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
59+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
60+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
61+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
62+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
63+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
64+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
65+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
66+
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
67+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
68+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
69+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
70+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
71+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
72+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

ext/awsauth/test/aws_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright (C) MongoDB, Inc. 2025-present.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
package awsauthtest
8+
9+
import (
10+
"context"
11+
"errors"
12+
"os"
13+
"testing"
14+
15+
"github.com/aws/aws-sdk-go-v2/config"
16+
"go.mongodb.org/mongo-driver/ext/awsauth"
17+
"go.mongodb.org/mongo-driver/v2/bson"
18+
"go.mongodb.org/mongo-driver/v2/mongo"
19+
"go.mongodb.org/mongo-driver/v2/mongo/options"
20+
)
21+
22+
// trackingCredentialsProvider wraps an options.AWSCredentialsProvider and counts calls.
23+
type trackingCredentialsProvider struct {
24+
inner options.AWSCredentialsProvider
25+
called int
26+
}
27+
28+
func (p *trackingCredentialsProvider) Retrieve(ctx context.Context) (awsauth.AWSCredentials, error) {
29+
p.called++
30+
return p.inner.Retrieve(ctx)
31+
}
32+
33+
// TestAWSDefaultCustomCredentialProviderAuthenticates is prose test 1:
34+
// "Custom Credential Provider Authenticates" from the MongoDB AWS auth spec.
35+
// https://github.com/mongodb/specifications/blob/master/source/auth/tests/mongodb-aws.md
36+
//
37+
// Uses the AWS SDK default credential chain so all 6 scenarios (Regular, EC2, ECS,
38+
// AssumeRole, WebIdentity, Lambda) are covered in environments where the SDK can
39+
// resolve credentials automatically without needing inline credentials in the URI.
40+
func TestAWSDefaultCustomCredentialProviderAuthenticates(t *testing.T) {
41+
if test := os.Getenv("AWS_TEST"); test == "assume-role" || test == "regular" {
42+
t.Skipf("Skipping test for %s", test)
43+
}
44+
45+
rawURI := os.Getenv("MONGODB_URI")
46+
if rawURI == "" {
47+
t.Skip("MONGODB_URI not set")
48+
}
49+
50+
cfg, err := config.LoadDefaultConfig(context.Background())
51+
if err != nil {
52+
t.Fatalf("failed to load AWS config: %v", err)
53+
}
54+
55+
tracking := &trackingCredentialsProvider{
56+
inner: awsauth.NewCredentialsProvider(cfg.Credentials),
57+
}
58+
59+
// SetAuth overrides any inline credentials that ApplyURI may have extracted
60+
// from the URI; the AWS SDK default chain provides credentials instead.
61+
client, err := mongo.Connect(
62+
options.Client().
63+
ApplyURI(rawURI).
64+
SetAuth(options.Credential{
65+
AuthMechanism: "MONGODB-AWS",
66+
AWSCredentialsProvider: tracking,
67+
}),
68+
)
69+
if err != nil {
70+
t.Fatalf("failed to connect: %v", err)
71+
}
72+
defer func() {
73+
if err := client.Disconnect(context.Background()); err != nil {
74+
t.Errorf("Disconnect: %v", err)
75+
}
76+
}()
77+
78+
err = client.Database("aws").Collection("test").
79+
FindOne(context.Background(), bson.D{{Key: "x", Value: 1}}).Err()
80+
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
81+
t.Fatalf("unexpected FindOne error: %v", err)
82+
}
83+
84+
if tracking.called == 0 {
85+
t.Fatal("expected custom credential provider to be called at least once")
86+
}
87+
}

ext/awsauth/test/go.mod

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module go.mongodb.org/mongo-driver/ext/awsauth/test
2+
3+
go 1.25.0
4+
5+
replace (
6+
go.mongodb.org/mongo-driver/ext/awsauth => ..
7+
go.mongodb.org/mongo-driver/v2 => ../../..
8+
)
9+
10+
require (
11+
github.com/aws/aws-sdk-go-v2/config v1.32.30
12+
go.mongodb.org/mongo-driver/ext/awsauth v0.0.0
13+
go.mongodb.org/mongo-driver/v2 v2.0.0-beta2
14+
)
15+
16+
require (
17+
github.com/aws/aws-sdk-go-v2 v1.42.1 // indirect
18+
github.com/aws/aws-sdk-go-v2/credentials v1.19.29 // indirect
19+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.30 // indirect
20+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.30 // indirect
21+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.30 // indirect
22+
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.31 // indirect
23+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.13 // indirect
24+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.30 // indirect
25+
github.com/aws/aws-sdk-go-v2/service/signin v1.4.1 // indirect
26+
github.com/aws/aws-sdk-go-v2/service/sso v1.32.1 // indirect
27+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.37.1 // indirect
28+
github.com/aws/aws-sdk-go-v2/service/sts v1.44.1 // indirect
29+
github.com/aws/smithy-go v1.27.3 // indirect
30+
github.com/klauspost/compress v1.17.6 // indirect
31+
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
32+
github.com/xdg-go/scram v1.2.0 // indirect
33+
github.com/xdg-go/stringprep v1.0.4 // indirect
34+
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
35+
golang.org/x/crypto v0.33.0 // indirect
36+
golang.org/x/sync v0.11.0 // indirect
37+
golang.org/x/text v0.22.0 // indirect
38+
)

0 commit comments

Comments
 (0)