From 84482241509a32e9d90197a45fbdf9dba09964b4 Mon Sep 17 00:00:00 2001 From: Rishikesh Betigeri <53863619+Rishikesh01@users.noreply.github.com> Date: Wed, 1 May 2024 23:48:24 +0530 Subject: [PATCH] fix: `+srv` mongodb url scheme parsing bug 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 | 3 +++ pkg/scalers/mongo_scaler_test.go | 4 ++-- pkg/scalers/scaler.go | 2 ++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd671c7516b..202f6e40399 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)) +- **General**: fix `+srv` mongodb url scheme parsing bug ([#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..30aabda415e 100644 --- a/pkg/scalers/mongo_scaler.go +++ b/pkg/scalers/mongo_scaler.go @@ -202,6 +202,9 @@ func parseMongoDBMetadata(config *scalersconfig.ScalerConfig) (*mongoDBMetadata, if meta.connectionString != "" { connStr = meta.connectionString + } else if meta.scheme == "mongodb+srv" { + connStr = fmt.Sprintf("%s://%s:%s@%s/%s", meta.scheme, url.QueryEscape(meta.username), url.QueryEscape(meta.password), meta.host, meta.dbName) + } else { // Build connection str addr := net.JoinHostPort(meta.host, meta.port) 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{ diff --git a/pkg/scalers/scaler.go b/pkg/scalers/scaler.go index df37eb210e0..aa9098c5e53 100644 --- a/pkg/scalers/scaler.go +++ b/pkg/scalers/scaler.go @@ -79,6 +79,8 @@ func GetFromAuthOrMeta(config *scalersconfig.ScalerConfig, field string) (string result = config.AuthParams[field] } else if config.TriggerMetadata[field] != "" { result = config.TriggerMetadata[field] + } else if value, ok := config.AuthParams["scheme"]; ok && strings.Contains(value, "mongodb+srv") { + return result, err } if result == "" { err = fmt.Errorf("%w: no %s given", ErrScalerConfigMissingField, field)