Skip to content

Commit c4df038

Browse files
authored
GODRIVER-3331 Fix default authSource for SRV connections [master] (#1803)
1 parent 4f21584 commit c4df038

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

.evergreen/config.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,7 +1715,7 @@ tasks:
17151715
- name: "testgcpkms-task"
17161716
commands:
17171717
- command: shell.exec
1718-
type: setup
1718+
type: test
17191719
params:
17201720
shell: "bash"
17211721
working_dir: src/go.mongodb.org/mongo-driver
@@ -1796,7 +1796,7 @@ tasks:
17961796
- name: "testazurekms-task"
17971797
commands:
17981798
- command: shell.exec
1799-
type: setup
1799+
type: test
18001800
params:
18011801
shell: "bash"
18021802
working_dir: src/go.mongodb.org/mongo-driver
@@ -1862,6 +1862,7 @@ tasks:
18621862
role_arn: ${LAMBDA_AWS_ROLE_ARN}
18631863
duration_seconds: 3600
18641864
- command: shell.exec
1865+
type: test
18651866
params:
18661867
working_dir: src/go.mongodb.org/mongo-driver
18671868
shell: bash
@@ -1884,6 +1885,7 @@ tasks:
18841885
- name: "oidc-auth-test-azure"
18851886
commands:
18861887
- command: shell.exec
1888+
type: test
18871889
params:
18881890
working_dir: src/go.mongodb.org/mongo-driver
18891891
shell: bash
@@ -1909,6 +1911,7 @@ tasks:
19091911
- name: "oidc-auth-test-gcp"
19101912
commands:
19111913
- command: shell.exec
1914+
type: test
19121915
params:
19131916
working_dir: src/go.mongodb.org/mongo-driver
19141917
shell: bash
@@ -2604,7 +2607,7 @@ buildvariants:
26042607
- name: testoidc-variant
26052608
display_name: "OIDC"
26062609
run_on:
2607-
- ubuntu2204-large
2610+
- ubuntu2204-small
26082611
expansions:
26092612
GO_DIST: "/opt/golang/go1.22"
26102613
tasks:

mongo/options/clientoptions_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ func TestSetURIopts(t *testing.T) {
12851285
wantErrs: nil,
12861286
},
12871287
{
1288-
name: "tmp",
1288+
name: "oidc azure",
12891289
uri: "mongodb://example.com/?authMechanism=MONGODB-OIDC&authMechanismProperties=TOKEN_RESOURCE:mongodb://test-cluster,ENVIRONMENT:azureManagedIdentities",
12901290
wantopts: &ClientOptions{
12911291
Hosts: []string{"example.com"},
@@ -1296,6 +1296,18 @@ func TestSetURIopts(t *testing.T) {
12961296
},
12971297
wantErrs: nil,
12981298
},
1299+
{
1300+
name: "oidc gcp",
1301+
uri: "mongodb://test.mongodb.net/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:mongodb://test-cluster",
1302+
wantopts: &ClientOptions{
1303+
Hosts: []string{"test.mongodb.net"},
1304+
Auth: &Credential{AuthMechanism: "MONGODB-OIDC", AuthSource: "$external", AuthMechanismProperties: map[string]string{
1305+
"ENVIRONMENT": "gcp",
1306+
"TOKEN_RESOURCE": "mongodb://test-cluster"}},
1307+
HTTPClient: httputil.DefaultHTTPClient,
1308+
},
1309+
wantErrs: nil,
1310+
},
12991311
{
13001312
name: "comma in key:value pair causes error",
13011313
uri: "mongodb://example.com/?authMechanismProperties=TOKEN_RESOURCE:mongodb://host1%2Chost2",

x/mongo/driver/connstring/connstring.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ func (u *ConnString) setDefaultAuthParams(dbName string) error {
292292
}
293293
fallthrough
294294
case "mongodb-aws", "mongodb-x509", "mongodb-oidc":
295+
// dns.LookupTXT will get "authSource=admin" from Atlas hosts.
296+
if u.AuthSource == "admin" {
297+
u.AuthSource = "$external"
298+
}
295299
if u.AuthSource == "" {
296300
u.AuthSource = "$external"
297301
} else if u.AuthSource != "$external" {

x/mongo/driver/connstring/connstring_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ func TestAuthSource(t *testing.T) {
9090
}
9191
})
9292
}
93+
94+
tests = []struct {
95+
s string
96+
expected string
97+
err bool
98+
}{
99+
{s: "authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:mongodb://test-cluster", expected: "$external"},
100+
}
101+
102+
for _, test := range tests {
103+
s := fmt.Sprintf("mongodb://test.mongodb.net/?authMechanism=MONGODB-OIDC&/%s", test.s)
104+
t.Run(s, func(t *testing.T) {
105+
cs, err := connstring.ParseAndValidate(s)
106+
if test.err {
107+
require.Error(t, err)
108+
} else {
109+
require.NoError(t, err)
110+
require.Equal(t, test.expected, cs.AuthSource)
111+
}
112+
})
113+
}
114+
93115
}
94116

95117
func TestConnect(t *testing.T) {

0 commit comments

Comments
 (0)