Skip to content

Commit

Permalink
Add support to get connection data from Trigger Authorization in MSSQ…
Browse files Browse the repository at this point in the history
…L Scaler (#2112)

Signed-off-by: jorturfer <jorge_turrado@hotmail.es>
Co-authored-by: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com>
  • Loading branch information
Jorge Turrado Ferrero and zroubalik authored Oct 4, 2021
1 parent 3ecc171 commit 0242b98
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- Escape `queueName` and `vhostName` in RabbitMQ Scaler before use them in query string (bug fix) ([#2055](https://github.com/kedacore/keda/pull/2055))
- TriggerAuthentication/Vault: add support for HashiCorp Vault namespace (Vault Enterprise) ([#2085](https://github.com/kedacore/keda/pull/2085))
- Add custom http timeout in RabbitMQ Scaler ([#2086](https://github.com/kedacore/keda/pull/2086))
- Add support to get connection data from Trigger Authorization in MSSQL Scaler ([#2112](https://github.com/kedacore/keda/pull/2112))
- Add support to provide the metric name in Azure Log Analytics Scaler ([#2106](https://github.com/kedacore/keda/pull/2106))

### Breaking Changes
Expand Down
33 changes: 23 additions & 10 deletions pkg/scalers/mssql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,41 @@ func parseMSSQLMetadata(config *ScalerConfig) (*mssqlMetadata, error) {
meta.connectionString = config.ResolvedEnv[config.TriggerMetadata["connectionStringFromEnv"]]
default:
meta.connectionString = ""
if val, ok := config.TriggerMetadata["host"]; ok {
meta.host = val
} else {

if config.AuthParams["host"] != "" {
meta.host = config.AuthParams["host"]
} else if config.TriggerMetadata["host"] != "" {
meta.host = config.TriggerMetadata["host"]
}
if meta.host == "" {
return nil, fmt.Errorf("no host given")
}

if val, ok := config.TriggerMetadata["port"]; ok {
port, err := strconv.Atoi(val)
var paramPort string
if config.AuthParams["port"] != "" {
paramPort = config.AuthParams["port"]
} else if config.TriggerMetadata["port"] != "" {
paramPort = config.TriggerMetadata["port"]
}
if paramPort != "" {
port, err := strconv.Atoi(paramPort)
if err != nil {
return nil, fmt.Errorf("port parsing error %s", err.Error())
}

meta.port = port
}

if val, ok := config.TriggerMetadata["username"]; ok {
meta.username = val
if config.AuthParams["username"] != "" {
meta.username = config.AuthParams["username"]
} else if config.TriggerMetadata["username"] != "" {
meta.username = config.TriggerMetadata["username"]
}

// database is optional in SQL s
if val, ok := config.TriggerMetadata["database"]; ok {
meta.database = val
if config.AuthParams["database"] != "" {
meta.database = config.AuthParams["database"]
} else if config.TriggerMetadata["database"] != "" {
meta.database = config.TriggerMetadata["database"]
}

if config.AuthParams["password"] != "" {
Expand Down
8 changes: 8 additions & 0 deletions pkg/scalers/mssql_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ var testInputs = []mssqlTestData{
expectedMetricName: "mssql-AdventureWorks",
expectedConnectionString: "sqlserver://user2:Password%232@example.database.windows.net?database=AdventureWorks",
},
// connection string generated from full authParams
{
metadata: map[string]string{"query": "SELECT 1", "targetValue": "1"},
resolvedEnv: map[string]string{},
authParams: map[string]string{"password": "Password#2", "host": "example.database.windows.net", "username": "user2", "database": "AdventureWorks", "port": "1433"},
expectedMetricName: "mssql-AdventureWorks",
expectedConnectionString: "sqlserver://user2:Password%232@example.database.windows.net:1433?database=AdventureWorks",
},
// variation of previous: no database name, metricName from host
{
metadata: map[string]string{"query": "SELECT 1", "targetValue": "1", "host": "example.database.windows.net", "username": "user3"},
Expand Down

0 comments on commit 0242b98

Please sign in to comment.