Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

[WIP] PMM-8125 Set the default authentication database #817

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion models/agent_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ func (s *Agent) DSN(service *Service, dialTimeout time.Duration, database string
// between the last host and the question mark (?) that begins the string of options.
path := database
if database == "" {
path = "/"
// Don't use an empty database because not all MongoDB flavors will set it to the
// default authentication database so, we need to do it manually.
path = "/admin"
}

if s.TLS {
Expand Down
16 changes: 8 additions & 8 deletions models/agent_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func TestAgent(t *testing.T) {
t.Run("MongoDBNoDatabase", func(t *testing.T) {
agent.AgentType = models.MongoDBExporterType

assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/?connectTimeoutMS=1000", agent.DSN(service, time.Second, "", nil))
assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/", agent.DSN(service, 0, "", nil))
assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/admin?connectTimeoutMS=1000", agent.DSN(service, time.Second, "", nil))
assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/admin", agent.DSN(service, 0, "", nil))
})
})

Expand Down Expand Up @@ -152,8 +152,8 @@ func TestAgent(t *testing.T) {
agent.MongoDBOptions.TLSCertificateKeyFilePassword = ""
agent.MongoDBOptions.AuthenticationMechanism = ""

assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/?connectTimeoutMS=1000&ssl=true&tlsCaFile={{.TextFiles.caFilePlaceholder}}&tlsCertificateKeyFile={{.TextFiles.certificateKeyFilePlaceholder}}", agent.DSN(service, time.Second, "", nil))
assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/?ssl=true&tlsCaFile={{.TextFiles.caFilePlaceholder}}&tlsCertificateKeyFile={{.TextFiles.certificateKeyFilePlaceholder}}", agent.DSN(service, 0, "", nil))
assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/admin?connectTimeoutMS=1000&ssl=true&tlsCaFile={{.TextFiles.caFilePlaceholder}}&tlsCertificateKeyFile={{.TextFiles.certificateKeyFilePlaceholder}}", agent.DSN(service, time.Second, "", nil))
assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/admin?ssl=true&tlsCaFile={{.TextFiles.caFilePlaceholder}}&tlsCertificateKeyFile={{.TextFiles.certificateKeyFilePlaceholder}}", agent.DSN(service, 0, "", nil))
expectedFiles := map[string]string{
"caFilePlaceholder": "cert",
"certificateKeyFilePlaceholder": "key",
Expand All @@ -168,8 +168,8 @@ func TestAgent(t *testing.T) {
agent.MongoDBOptions.AuthenticationMechanism = "MONGO-X509"
agent.MongoDBOptions.AuthenticationDatabase = "$external"

assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/?authMechanism=MONGO-X509&authSource=%24external&connectTimeoutMS=1000&ssl=true&tlsCaFile={{.TextFiles.caFilePlaceholder}}&tlsCertificateKeyFile={{.TextFiles.certificateKeyFilePlaceholder}}", agent.DSN(service, time.Second, "", nil))
assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/?authMechanism=MONGO-X509&authSource=%24external&ssl=true&tlsCaFile={{.TextFiles.caFilePlaceholder}}&tlsCertificateKeyFile={{.TextFiles.certificateKeyFilePlaceholder}}", agent.DSN(service, 0, "", nil))
assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/admin?authMechanism=MONGO-X509&authSource=%24external&connectTimeoutMS=1000&ssl=true&tlsCaFile={{.TextFiles.caFilePlaceholder}}&tlsCertificateKeyFile={{.TextFiles.certificateKeyFilePlaceholder}}", agent.DSN(service, time.Second, "", nil))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [Linters] reported by reviewdog 🐶
line is 321 characters (lll)

assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/admin?authMechanism=MONGO-X509&authSource=%24external&ssl=true&tlsCaFile={{.TextFiles.caFilePlaceholder}}&tlsCertificateKeyFile={{.TextFiles.certificateKeyFilePlaceholder}}", agent.DSN(service, 0, "", nil))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [Linters] reported by reviewdog 🐶
line is 289 characters (lll)

expectedFiles := map[string]string{
"caFilePlaceholder": "cert",
"certificateKeyFilePlaceholder": "key",
Expand Down Expand Up @@ -207,8 +207,8 @@ func TestAgent(t *testing.T) {
t.Run("MongoDBNoDatabase", func(t *testing.T) {
agent.AgentType = models.MongoDBExporterType

assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/?connectTimeoutMS=1000&ssl=true&tlsInsecure=true", agent.DSN(service, time.Second, "", nil))
assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/?ssl=true&tlsInsecure=true", agent.DSN(service, 0, "", nil))
assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/admin?connectTimeoutMS=1000&ssl=true&tlsInsecure=true", agent.DSN(service, time.Second, "", nil))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [Linters] reported by reviewdog 🐶
line is 180 characters (lll)

assert.Equal(t, "mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:12345/admin?ssl=true&tlsInsecure=true", agent.DSN(service, 0, "", nil))
})
})
}
Expand Down
14 changes: 7 additions & 7 deletions services/agents/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestMongodbExporterConfig(t *testing.T) {
"--web.listen-address=:{{ .listen_port }}",
},
Env: []string{
"MONGODB_URI=mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:27017/?connectTimeoutMS=1000",
"MONGODB_URI=mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:27017/admin?connectTimeoutMS=1000",
"HTTP_AUTH=pmm:agent-password",
},
RedactWords: []string{"s3cur3 p@$$w0r4.", "agent-password"},
Expand All @@ -70,13 +70,13 @@ func TestMongodbExporterConfig(t *testing.T) {
t.Run("EmptyPassword", func(t *testing.T) {
exporter.Password = nil
actual := mongodbExporterConfig(mongodb, exporter, exposeSecrets, pmmAgentVersion)
assert.Equal(t, "MONGODB_URI=mongodb://username@1.2.3.4:27017/?connectTimeoutMS=1000", actual.Env[0])
assert.Equal(t, "MONGODB_URI=mongodb://username@1.2.3.4:27017/admin?connectTimeoutMS=1000", actual.Env[0])
})

t.Run("EmptyUsername", func(t *testing.T) {
exporter.Username = nil
actual := mongodbExporterConfig(mongodb, exporter, exposeSecrets, pmmAgentVersion)
assert.Equal(t, "MONGODB_URI=mongodb://1.2.3.4:27017/?connectTimeoutMS=1000", actual.Env[0])
assert.Equal(t, "MONGODB_URI=mongodb://1.2.3.4:27017/admin?connectTimeoutMS=1000", actual.Env[0])
})
t.Run("SSLEnabled", func(t *testing.T) {
exporter.TLS = true
Expand All @@ -86,7 +86,7 @@ func TestMongodbExporterConfig(t *testing.T) {
TLSCa: "content-of-tls-ca",
}
actual := mongodbExporterConfig(mongodb, exporter, exposeSecrets, pmmAgentVersion)
expected := "MONGODB_URI=mongodb://1.2.3.4:27017/?connectTimeoutMS=1000&ssl=true&" +
expected := "MONGODB_URI=mongodb://1.2.3.4:27017/admin?connectTimeoutMS=1000&ssl=true&" +
"tlsCaFile={{.TextFiles.caFilePlaceholder}}&tlsCertificateKeyFile={{.TextFiles.certificateKeyFilePlaceholder}}&tlsCertificateKeyFilePassword=passwordoftls"
assert.Equal(t, expected, actual.Env[0])
expectedFiles := map[string]string{
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestNewMongodbExporterConfig(t *testing.T) {
"--web.listen-address=:{{ .listen_port }}",
},
Env: []string{
"MONGODB_URI=mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:27017/?connectTimeoutMS=1000",
"MONGODB_URI=mongodb://username:s3cur3%20p%40$$w0r4.@1.2.3.4:27017/admin?connectTimeoutMS=1000",
"HTTP_AUTH=pmm:agent-id",
},
RedactWords: []string{"s3cur3 p@$$w0r4."},
Expand All @@ -176,12 +176,12 @@ func TestNewMongodbExporterConfig(t *testing.T) {
t.Run("EmptyPassword", func(t *testing.T) {
exporter.Password = nil
actual := mongodbExporterConfig(mongodb, exporter, exposeSecrets, pmmAgentVersion)
assert.Equal(t, "MONGODB_URI=mongodb://username@1.2.3.4:27017/?connectTimeoutMS=1000", actual.Env[0])
assert.Equal(t, "MONGODB_URI=mongodb://username@1.2.3.4:27017/admin?connectTimeoutMS=1000", actual.Env[0])
})

t.Run("EmptyUsername", func(t *testing.T) {
exporter.Username = nil
actual := mongodbExporterConfig(mongodb, exporter, exposeSecrets, pmmAgentVersion)
assert.Equal(t, "MONGODB_URI=mongodb://1.2.3.4:27017/?connectTimeoutMS=1000", actual.Env[0])
assert.Equal(t, "MONGODB_URI=mongodb://1.2.3.4:27017/admin?connectTimeoutMS=1000", actual.Env[0])
})
}