Skip to content

Commit 54652cf

Browse files
authored
Force directConnection=true for mongo clients (#52979)
1 parent f60a87c commit 54652cf

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

lib/client/db/dbcmd/dbcmd.go

+12
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,18 @@ func (c *CLICommandBuilder) getMongoAddress() string {
573573
serverSelectionTimeoutMS = envValue
574574
}
575575
query.Set("serverSelectionTimeoutMS", serverSelectionTimeoutMS)
576+
// If directConnection is false (default for many clients), the client
577+
// attempts to discover all servers in the replica set, and sends operations
578+
// to the primary member.
579+
// https://www.mongodb.com/docs/manual/reference/connection-string-options/#mongodb-urioption-urioption.directConnection
580+
//
581+
// Since Teleport is a proxy that appears only as a single server,
582+
// directConnection should always be used.
583+
//
584+
// mongosh automatically adds the directConnection=true parameter. However,
585+
// here we explicitly set it for other clients like MongoDB compass.
586+
// https://www.mongodb.com/docs/mongodb-shell/connect/
587+
query.Set("directConnection", "true")
576588

577589
address := url.URL{
578590
Scheme: connstring.SchemeMongoDB,

lib/client/db/dbcmd/dbcmd_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func TestCLICommandBuilderGetConnectCommand(t *testing.T) {
332332
cmd: []string{"mongo",
333333
"--ssl",
334334
"--sslPEMKeyFile", "/tmp/keys/example.com/bob-db/db.example.com/mysql.crt",
335-
"mongodb://localhost:12345/mydb?serverSelectionTimeoutMS=5000",
335+
"mongodb://localhost:12345/mydb?directConnection=true&serverSelectionTimeoutMS=5000",
336336
},
337337
wantErr: false,
338338
},
@@ -347,7 +347,7 @@ func TestCLICommandBuilderGetConnectCommand(t *testing.T) {
347347
},
348348
},
349349
cmd: []string{"mongo",
350-
"mongodb://localhost:12345/mydb?serverSelectionTimeoutMS=5000",
350+
"mongodb://localhost:12345/mydb?directConnection=true&serverSelectionTimeoutMS=5000",
351351
},
352352
wantErr: false,
353353
},
@@ -365,7 +365,7 @@ func TestCLICommandBuilderGetConnectCommand(t *testing.T) {
365365
"--tls",
366366
"--tlsCertificateKeyFile", "/tmp/keys/example.com/bob-db/db.example.com/mysql.crt",
367367
"--tlsUseSystemCA",
368-
"mongodb://localhost:12345/mydb?serverSelectionTimeoutMS=5000",
368+
"mongodb://localhost:12345/mydb?directConnection=true&serverSelectionTimeoutMS=5000",
369369
},
370370
},
371371
{
@@ -385,7 +385,7 @@ func TestCLICommandBuilderGetConnectCommand(t *testing.T) {
385385
"--tls",
386386
"--tlsCertificateKeyFile", "/tmp/keys/example.com/bob-db/db.example.com/mysql.crt",
387387
"--tlsCAFile", "/tmp/keys/example.com/cas/example.com.pem",
388-
"mongodb://localhost:12345/mydb?serverSelectionTimeoutMS=5000",
388+
"mongodb://localhost:12345/mydb?directConnection=true&serverSelectionTimeoutMS=5000",
389389
},
390390
},
391391
{
@@ -399,7 +399,7 @@ func TestCLICommandBuilderGetConnectCommand(t *testing.T) {
399399
},
400400
},
401401
cmd: []string{"mongosh",
402-
"mongodb://localhost:12345/mydb?serverSelectionTimeoutMS=5000",
402+
"mongodb://localhost:12345/mydb?directConnection=true&serverSelectionTimeoutMS=5000",
403403
},
404404
},
405405
{
@@ -411,7 +411,7 @@ func TestCLICommandBuilderGetConnectCommand(t *testing.T) {
411411
execOutput: map[string][]byte{}, // Cannot find either bin.
412412
},
413413
cmd: []string{"mongosh",
414-
"mongodb://localhost:12345/mydb?serverSelectionTimeoutMS=5000",
414+
"mongodb://localhost:12345/mydb?directConnection=true&serverSelectionTimeoutMS=5000",
415415
},
416416
},
417417
{
@@ -427,7 +427,7 @@ func TestCLICommandBuilderGetConnectCommand(t *testing.T) {
427427
},
428428
},
429429
cmd: []string{"mongo",
430-
"mongodb://localhost:12345/docdb?serverSelectionTimeoutMS=5000",
430+
"mongodb://localhost:12345/docdb?directConnection=true&serverSelectionTimeoutMS=5000",
431431
},
432432
wantErr: false,
433433
},
@@ -442,7 +442,7 @@ func TestCLICommandBuilderGetConnectCommand(t *testing.T) {
442442
},
443443
},
444444
cmd: []string{"mongosh",
445-
"mongodb://localhost:12345/docdb?serverSelectionTimeoutMS=5000",
445+
"mongodb://localhost:12345/docdb?directConnection=true&serverSelectionTimeoutMS=5000",
446446
"--retryWrites=false",
447447
},
448448
wantErr: false,

0 commit comments

Comments
 (0)