Skip to content

Commit c5b9705

Browse files
authored
GODRIVER-3331 Fix default authSource for SRV connections (#1795)
1 parent 485e74d commit c5b9705

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
@@ -1804,7 +1804,7 @@ tasks:
18041804
- name: "testgcpkms-task"
18051805
commands:
18061806
- command: shell.exec
1807-
type: setup
1807+
type: test
18081808
params:
18091809
shell: "bash"
18101810
working_dir: src/go.mongodb.org/mongo-driver
@@ -1893,7 +1893,7 @@ tasks:
18931893
- name: "testazurekms-task"
18941894
commands:
18951895
- command: shell.exec
1896-
type: setup
1896+
type: test
18971897
params:
18981898
shell: "bash"
18991899
working_dir: src/go.mongodb.org/mongo-driver
@@ -1964,6 +1964,7 @@ tasks:
19641964
role_arn: ${LAMBDA_AWS_ROLE_ARN}
19651965
duration_seconds: 3600
19661966
- command: shell.exec
1967+
type: test
19671968
params:
19681969
working_dir: src/go.mongodb.org/mongo-driver
19691970
shell: bash
@@ -1986,6 +1987,7 @@ tasks:
19861987
- name: "oidc-auth-test-azure"
19871988
commands:
19881989
- command: shell.exec
1990+
type: test
19891991
params:
19901992
working_dir: src/go.mongodb.org/mongo-driver
19911993
shell: bash
@@ -2011,6 +2013,7 @@ tasks:
20112013
- name: "oidc-auth-test-gcp"
20122014
commands:
20132015
- command: shell.exec
2016+
type: test
20142017
params:
20152018
working_dir: src/go.mongodb.org/mongo-driver
20162019
shell: bash
@@ -2735,7 +2738,7 @@ buildvariants:
27352738
- name: testoidc-variant
27362739
display_name: "OIDC"
27372740
run_on:
2738-
- ubuntu2204-large
2741+
- ubuntu2204-small
27392742
expansions:
27402743
GO_DIST: "/opt/golang/go1.22"
27412744
tasks:

mongo/options/clientoptions_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ func TestClientOptions(t *testing.T) {
589589
},
590590
},
591591
{
592-
"tmp",
592+
"oidc azure",
593593
"mongodb://example.com/?authMechanism=MONGODB-OIDC&authMechanismProperties=TOKEN_RESOURCE:mongodb://test-cluster,ENVIRONMENT:azureManagedIdentities",
594594
&ClientOptions{
595595
Hosts: []string{"example.com"},
@@ -600,6 +600,18 @@ func TestClientOptions(t *testing.T) {
600600
HTTPClient: httputil.DefaultHTTPClient,
601601
},
602602
},
603+
{
604+
"oidc gcp",
605+
"mongodb://test.mongodb.net/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:mongodb://test-cluster",
606+
&ClientOptions{
607+
Hosts: []string{"test.mongodb.net"},
608+
Auth: &Credential{AuthMechanism: "MONGODB-OIDC", AuthSource: "$external", AuthMechanismProperties: map[string]string{
609+
"ENVIRONMENT": "gcp",
610+
"TOKEN_RESOURCE": "mongodb://test-cluster"}},
611+
err: nil,
612+
HTTPClient: httputil.DefaultHTTPClient,
613+
},
614+
},
603615
{
604616
"comma in key:value pair causes error",
605617
"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
@@ -297,6 +297,10 @@ func (u *ConnString) setDefaultAuthParams(dbName string) error {
297297
}
298298
fallthrough
299299
case "mongodb-aws", "mongodb-x509", "mongodb-oidc":
300+
// dns.LookupTXT will get "authSource=admin" from Atlas hosts.
301+
if u.AuthSource == "admin" {
302+
u.AuthSource = "$external"
303+
}
300304
if u.AuthSource == "" {
301305
u.AuthSource = "$external"
302306
} 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)