Skip to content

Commit

Permalink
fix: +srv mongodb url scheme parsing bug
Browse files Browse the repository at this point in the history
This commit fixs issue kedacore#5760. where OP was facing problem with +srv schema

Signed-off-by: Rishikesh Betigeri <53863619+Rishikesh01@users.noreply.github.com>
  • Loading branch information
Rishikesh01 committed May 2, 2024
1 parent a168022 commit 8448224
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions pkg/scalers/mongo_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/scalers/mongo_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions pkg/scalers/scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8448224

Please sign in to comment.