From b7d9bd42f968b901f861128ccedf836466012c9f Mon Sep 17 00:00:00 2001 From: Rishikesh <53863619+Rishikesh01@users.noreply.github.com> Date: Sat, 4 May 2024 20:45:40 +0530 Subject: [PATCH] fix: `+srv` mongodb url scheme parsing bug (#5773) This commit fixs issue #5760. where OP was facing problem with +srv schema Signed-off-by: Rishikesh Betigeri <53863619+Rishikesh01@users.noreply.github.com> --- CHANGELOG.md | 2 +- pkg/scalers/mongo_scaler.go | 21 ++++++++++++++------- pkg/scalers/mongo_scaler_test.go | 4 ++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd671c7516b..2b289d4569b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,7 +68,7 @@ Here is an overview of all new **experimental** features: ### Fixes -- TODO ([#XXX](https://github.com/kedacore/keda/issues/XXX)) +- **MongoDB Scaler**: MongoDB url parses correctly `+srv` scheme ([#5760](https://github.com/kedacore/keda/issues/5760)) ### Deprecations diff --git a/pkg/scalers/mongo_scaler.go b/pkg/scalers/mongo_scaler.go index f7871c8567e..f30b8fb97ec 100644 --- a/pkg/scalers/mongo_scaler.go +++ b/pkg/scalers/mongo_scaler.go @@ -7,6 +7,7 @@ import ( "net" "net/url" "strconv" + "strings" "time" "github.com/go-logr/logr" @@ -178,11 +179,13 @@ func parseMongoDBMetadata(config *scalersconfig.ScalerConfig) (*mongoDBMetadata, } meta.host = host - port, err := GetFromAuthOrMeta(config, "port") - if err != nil { - return nil, "", err + if !strings.Contains(scheme, "mongodb+srv") { + port, err := GetFromAuthOrMeta(config, "port") + if err != nil { + return nil, "", err + } + meta.port = port } - meta.port = port username, err := GetFromAuthOrMeta(config, "username") if err != nil { @@ -200,14 +203,18 @@ func parseMongoDBMetadata(config *scalersconfig.ScalerConfig) (*mongoDBMetadata, } } - if meta.connectionString != "" { + switch { + case meta.connectionString != "": connStr = meta.connectionString - } else { - // Build connection str + case meta.scheme == "mongodb+srv": + // nosemgrep: db-connection-string + connStr = fmt.Sprintf("%s://%s:%s@%s/%s", meta.scheme, url.QueryEscape(meta.username), url.QueryEscape(meta.password), meta.host, meta.dbName) + default: addr := net.JoinHostPort(meta.host, meta.port) // nosemgrep: db-connection-string connStr = fmt.Sprintf("%s://%s:%s@%s/%s", meta.scheme, url.QueryEscape(meta.username), url.QueryEscape(meta.password), addr, meta.dbName) } + meta.triggerIndex = config.TriggerIndex return &meta, connStr, nil } diff --git a/pkg/scalers/mongo_scaler_test.go b/pkg/scalers/mongo_scaler_test.go index 02f1e9479ef..fd9f54f8337 100644 --- a/pkg/scalers/mongo_scaler_test.go +++ b/pkg/scalers/mongo_scaler_test.go @@ -73,7 +73,7 @@ var testMONGODBMetadata = []parseMongoDBMetadataTestData{ // mongodb srv support { metadata: map[string]string{"query": `{"name":"John"}`, "collection": "demo", "queryValue": "12"}, - authParams: map[string]string{"dbName": "test", "scheme": "mongodb+srv", "host": "localhost", "port": "1234", "username": "sample", "password": "sec@ure"}, + authParams: map[string]string{"dbName": "test", "scheme": "mongodb+srv", "host": "localhost", "port": "", "username": "sample", "password": "sec@ure"}, resolvedEnv: testMongoDBResolvedEnv, raisesError: false, }, @@ -90,7 +90,7 @@ var mongoDBConnectionStringTestDatas = []mongoDBConnectionStringTestData{ {metadataTestData: &testMONGODBMetadata[2], connectionString: "mongodb://mongodb0.example.com:27017"}, {metadataTestData: &testMONGODBMetadata[3], connectionString: "mongodb://sample:test%40password@localhost:1234/test"}, {metadataTestData: &testMONGODBMetadata[4], connectionString: "mongodb://sample:sec%40ure@localhost:1234/test"}, - {metadataTestData: &testMONGODBMetadata[5], connectionString: "mongodb+srv://sample:sec%40ure@localhost:1234/test"}, + {metadataTestData: &testMONGODBMetadata[5], connectionString: "mongodb+srv://sample:sec%40ure@localhost/test"}, } var mongoDBMetricIdentifiers = []mongoDBMetricIdentifier{