From b8d8c21a3d2e1d63aa6c6eb1e95c0b7be6ff7dbe Mon Sep 17 00:00:00 2001 From: jorturfer Date: Tue, 21 Sep 2021 23:28:01 +0200 Subject: [PATCH 1/7] Add support to set the connection data from Trigger Authorization Signed-off-by: jorturfer --- pkg/scalers/postgresql_scaler.go | 53 ++++++++++++++++---------- pkg/scalers/postgresql_scaler_test.go | 55 +++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 20 deletions(-) diff --git a/pkg/scalers/postgresql_scaler.go b/pkg/scalers/postgresql_scaler.go index 998b44a3dcc..970ea0f0ad0 100644 --- a/pkg/scalers/postgresql_scaler.go +++ b/pkg/scalers/postgresql_scaler.go @@ -81,31 +81,30 @@ func parsePostgreSQLMetadata(config *ScalerConfig) (*postgreSQLMetadata, error) meta.connection = config.ResolvedEnv[config.TriggerMetadata["connectionFromEnv"]] default: meta.connection = "" - if val, ok := config.TriggerMetadata["host"]; ok { - meta.host = val - } else { - return nil, fmt.Errorf("no host given") + var err error + meta.host, err = getFromAuthOrMeta(config, "host") + if err != nil { + return nil, err } - if val, ok := config.TriggerMetadata["port"]; ok { - meta.port = val - } else { - return nil, fmt.Errorf("no port given") + + meta.port, err = getFromAuthOrMeta(config, "port") + if err != nil { + return nil, err } - if val, ok := config.TriggerMetadata["userName"]; ok { - meta.userName = val - } else { - return nil, fmt.Errorf("no username given") + meta.port, err = getFromAuthOrMeta(config, "userName") + if err != nil { + return nil, err } - if val, ok := config.TriggerMetadata["dbName"]; ok { - meta.dbName = val - } else { - return nil, fmt.Errorf("no dbname given") + + meta.dbName, err = getFromAuthOrMeta(config, "dbName") + if err != nil { + return nil, err } - if val, ok := config.TriggerMetadata["sslmode"]; ok { - meta.sslmode = val - } else { - return nil, fmt.Errorf("no sslmode name given") + + meta.sslmode, err = getFromAuthOrMeta(config, "sslmode") + if err != nil { + return nil, err } if config.AuthParams["password"] != "" { @@ -225,3 +224,17 @@ func (s *postgreSQLScaler) GetMetrics(ctx context.Context, metricName string, me return append([]external_metrics.ExternalMetricValue{}, metric), nil } + +func getFromAuthOrMeta(config *ScalerConfig, field string) (string, error) { + var result string + var err error + if config.AuthParams[field] != "" { + result = config.AuthParams[field] + } else if config.TriggerMetadata[field] != "" { + result = config.TriggerMetadata[field] + } + if result == "" { + err = fmt.Errorf("no %s given", field) + } + return result, err +} diff --git a/pkg/scalers/postgresql_scaler_test.go b/pkg/scalers/postgresql_scaler_test.go index 24be355880d..d68795d24db 100644 --- a/pkg/scalers/postgresql_scaler_test.go +++ b/pkg/scalers/postgresql_scaler_test.go @@ -54,3 +54,58 @@ func TestPosgresSQLGetMetricSpecForScaling(t *testing.T) { } } } + +var testPostgresResolvedEnv = map[string]string{ + "POSTGRE_PASSWORD": "pass", + "POSTGRE_CONN_STR": "test_conn_str", +} + +type parsePostgresMetadataTestData struct { + metadata map[string]string + authParams map[string]string + resolvedEnv map[string]string + raisesError bool +} + +var testPostgresMetadata = []parsePostgresMetadataTestData{ + // No metadata + { + metadata: map[string]string{}, + authParams: map[string]string{}, + resolvedEnv: map[string]string{}, + raisesError: true, + }, + // connectionString + { + metadata: map[string]string{"query": "query", "targetQueryValue": "12", "connectionFromEnv": "POSTGRE_CONN_STR"}, + authParams: map[string]string{}, + resolvedEnv: testPostgresResolvedEnv, + raisesError: false, + }, + // Params instead of conn str + { + metadata: map[string]string{"query": "query", "targetQueryValue": "12", "host": "test_host", "port": "test_port", "userName": "test_username", "passwordFromEnv": "POSTGRE_PASSWORD", "dbName": "test_dbname", "sslmode": "require"}, + authParams: map[string]string{}, + resolvedEnv: testPostgresResolvedEnv, + raisesError: false, + }, + // Params from trigger authentication + { + metadata: map[string]string{"query": "query", "targetQueryValue": "12"}, + authParams: map[string]string{"host": "test_host", "port": "test_port", "userName": "test_username", "password": "POSTGRE_PASSWORD", "dbName": "test_dbname", "sslmode": "disable"}, + resolvedEnv: testPostgresResolvedEnv, + raisesError: false, + }, +} + +func TestParsePosgresSQLMetadata(t *testing.T) { + for _, testData := range testPostgresMetadata { + _, err := parsePostgreSQLMetadata(&ScalerConfig{ResolvedEnv: map[string]string{}, TriggerMetadata: testData.metadata, AuthParams: testData.authParams}) + if err != nil && !testData.raisesError { + t.Error("Expected success but got error", err) + } + if err == nil && testData.raisesError { + t.Error("Expected error but got success") + } + } +} From b5f4a4fb90fa6009ebf51d719fa376b44d3d63d2 Mon Sep 17 00:00:00 2001 From: jorturfer Date: Tue, 21 Sep 2021 23:32:37 +0200 Subject: [PATCH 2/7] Update CHANGELOG Signed-off-by: jorturfer --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d278c433fc6..acba9928d8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 PostgreSQL Scaler ([#2114](https://github.com/kedacore/keda/pull/2114)) ### Breaking Changes From 8275ce4e04ea166323d319aa4538ed8dfd27460c Mon Sep 17 00:00:00 2001 From: jorturfer Date: Wed, 22 Sep 2021 00:01:46 +0200 Subject: [PATCH 3/7] Fix typo Signed-off-by: jorturfer --- pkg/scalers/postgresql_scaler_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/scalers/postgresql_scaler_test.go b/pkg/scalers/postgresql_scaler_test.go index d68795d24db..388c6858596 100644 --- a/pkg/scalers/postgresql_scaler_test.go +++ b/pkg/scalers/postgresql_scaler_test.go @@ -100,7 +100,7 @@ var testPostgresMetadata = []parsePostgresMetadataTestData{ func TestParsePosgresSQLMetadata(t *testing.T) { for _, testData := range testPostgresMetadata { - _, err := parsePostgreSQLMetadata(&ScalerConfig{ResolvedEnv: map[string]string{}, TriggerMetadata: testData.metadata, AuthParams: testData.authParams}) + _, err := parsePostgreSQLMetadata(&ScalerConfig{ResolvedEnv: testData.resolvedEnv, TriggerMetadata: testData.metadata, AuthParams: testData.authParams}) if err != nil && !testData.raisesError { t.Error("Expected success but got error", err) } From d5a7c853317f363df992e93f7dd349fa89736bc5 Mon Sep 17 00:00:00 2001 From: jorturfer Date: Mon, 4 Oct 2021 11:25:22 +0200 Subject: [PATCH 4/7] Move GetFromAuthOrMeta from postgresql_scaler.go to scaler.go Signed-off-by: jorturfer --- pkg/scalers/postgresql_scaler.go | 24 +++++------------------- pkg/scalers/scaler.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/pkg/scalers/postgresql_scaler.go b/pkg/scalers/postgresql_scaler.go index 970ea0f0ad0..5dc20b6e247 100644 --- a/pkg/scalers/postgresql_scaler.go +++ b/pkg/scalers/postgresql_scaler.go @@ -82,27 +82,27 @@ func parsePostgreSQLMetadata(config *ScalerConfig) (*postgreSQLMetadata, error) default: meta.connection = "" var err error - meta.host, err = getFromAuthOrMeta(config, "host") + meta.host, err = GetFromAuthOrMeta(config, "host") if err != nil { return nil, err } - meta.port, err = getFromAuthOrMeta(config, "port") + meta.port, err = GetFromAuthOrMeta(config, "port") if err != nil { return nil, err } - meta.port, err = getFromAuthOrMeta(config, "userName") + meta.port, err = GetFromAuthOrMeta(config, "userName") if err != nil { return nil, err } - meta.dbName, err = getFromAuthOrMeta(config, "dbName") + meta.dbName, err = GetFromAuthOrMeta(config, "dbName") if err != nil { return nil, err } - meta.sslmode, err = getFromAuthOrMeta(config, "sslmode") + meta.sslmode, err = GetFromAuthOrMeta(config, "sslmode") if err != nil { return nil, err } @@ -224,17 +224,3 @@ func (s *postgreSQLScaler) GetMetrics(ctx context.Context, metricName string, me return append([]external_metrics.ExternalMetricValue{}, metric), nil } - -func getFromAuthOrMeta(config *ScalerConfig, field string) (string, error) { - var result string - var err error - if config.AuthParams[field] != "" { - result = config.AuthParams[field] - } else if config.TriggerMetadata[field] != "" { - result = config.TriggerMetadata[field] - } - if result == "" { - err = fmt.Errorf("no %s given", field) - } - return result, err -} diff --git a/pkg/scalers/scaler.go b/pkg/scalers/scaler.go index ddaead64a99..50949fed0d0 100644 --- a/pkg/scalers/scaler.go +++ b/pkg/scalers/scaler.go @@ -18,6 +18,7 @@ package scalers import ( "context" + "fmt" "time" v2beta2 "k8s.io/api/autoscaling/v2beta2" @@ -81,3 +82,18 @@ type ScalerConfig struct { // PodIdentity PodIdentity kedav1alpha1.PodIdentityProvider } + +// This method helps getting a field from Auth or Meta sections +func GetFromAuthOrMeta(config *ScalerConfig, field string) (string, error) { + var result string + var err error + if config.AuthParams[field] != "" { + result = config.AuthParams[field] + } else if config.TriggerMetadata[field] != "" { + result = config.TriggerMetadata[field] + } + if result == "" { + err = fmt.Errorf("no %s given", field) + } + return result, err +} From 15136e91d0134af718d3617b12200f9eb93435a8 Mon Sep 17 00:00:00 2001 From: jorturfer Date: Mon, 4 Oct 2021 11:30:14 +0200 Subject: [PATCH 5/7] Update mongodb and mysql to use GetFromAuthOrMeta Signed-off-by: jorturfer --- pkg/scalers/mongo_scaler.go | 22 ++++------------------ pkg/scalers/mysql_scaler.go | 22 ++++------------------ 2 files changed, 8 insertions(+), 36 deletions(-) diff --git a/pkg/scalers/mongo_scaler.go b/pkg/scalers/mongo_scaler.go index b2ce5074e8c..232e6458c7d 100644 --- a/pkg/scalers/mongo_scaler.go +++ b/pkg/scalers/mongo_scaler.go @@ -133,22 +133,22 @@ func parseMongoDBMetadata(config *ScalerConfig) (*mongoDBMetadata, string, error default: meta.connectionString = "" var err error - meta.host, err = getFromAuthOrMeta(config, "host") + meta.host, err = GetFromAuthOrMeta(config, "host") if err != nil { return nil, "", err } - meta.dbName, err = getFromAuthOrMeta(config, "dbName") + meta.dbName, err = GetFromAuthOrMeta(config, "dbName") if err != nil { return nil, "", err } - meta.port, err = getFromAuthOrMeta(config, "port") + meta.port, err = GetFromAuthOrMeta(config, "port") if err != nil { return nil, "", err } - meta.username, err = getFromAuthOrMeta(config, "username") + meta.username, err = GetFromAuthOrMeta(config, "username") if err != nil { return nil, "", err } @@ -275,17 +275,3 @@ func json2BsonDoc(js string) (doc bsonx.Doc, err error) { return doc, nil } - -func getFromAuthOrMeta(config *ScalerConfig, field string) (string, error) { - var result string - var err error - if config.AuthParams[field] != "" { - result = config.AuthParams[field] - } else if config.TriggerMetadata[field] != "" { - result = config.TriggerMetadata[field] - } - if result == "" { - err = fmt.Errorf("no %s given", field) - } - return result, err -} diff --git a/pkg/scalers/mysql_scaler.go b/pkg/scalers/mysql_scaler.go index 350e761291b..a7445925b48 100644 --- a/pkg/scalers/mysql_scaler.go +++ b/pkg/scalers/mysql_scaler.go @@ -79,22 +79,22 @@ func parseMySQLMetadata(config *ScalerConfig) (*mySQLMetadata, error) { default: meta.connectionString = "" var err error - meta.host, err = getFromAuthOrMeta(config, "host") + meta.host, err = GetFromAuthOrMeta(config, "host") if err != nil { return nil, err } - meta.port, err = getFromAuthOrMeta(config, "port") + meta.port, err = GetFromAuthOrMeta(config, "port") if err != nil { return nil, err } - meta.username, err = getFromAuthOrMeta(config, "username") + meta.username, err = GetFromAuthOrMeta(config, "username") if err != nil { return nil, err } - meta.dbName, err = getFromAuthOrMeta(config, "dbName") + meta.dbName, err = GetFromAuthOrMeta(config, "dbName") if err != nil { return nil, err } @@ -218,17 +218,3 @@ func (s *mySQLScaler) GetMetrics(ctx context.Context, metricName string, metricS return append([]external_metrics.ExternalMetricValue{}, metric), nil } - -func getFromAuthOrMeta(config *ScalerConfig, field string) (string, error) { - var result string - var err error - if config.AuthParams[field] != "" { - result = config.AuthParams[field] - } else if config.TriggerMetadata[field] != "" { - result = config.TriggerMetadata[field] - } - if result == "" { - err = fmt.Errorf("no %s given", field) - } - return result, err -} From 7286bdf219c9fd7a6b0f8a62bff41a57d909b481 Mon Sep 17 00:00:00 2001 From: jorturfer Date: Mon, 4 Oct 2021 14:10:20 +0200 Subject: [PATCH 6/7] Fix errors in mssql_scaler Signed-off-by: jorturfer --- pkg/scalers/mssql_scaler.go | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/pkg/scalers/mssql_scaler.go b/pkg/scalers/mssql_scaler.go index 981327b887e..20c6b66ac30 100644 --- a/pkg/scalers/mssql_scaler.go +++ b/pkg/scalers/mssql_scaler.go @@ -108,22 +108,15 @@ func parseMSSQLMetadata(config *ScalerConfig) (*mssqlMetadata, error) { meta.connectionString = config.ResolvedEnv[config.TriggerMetadata["connectionStringFromEnv"]] default: meta.connectionString = "" + var err error - 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") + meta.host, err = GetFromAuthOrMeta(config, "host") + if err != nil { + return nil, err } var paramPort string - if config.AuthParams["port"] != "" { - paramPort = config.AuthParams["port"] - } else if config.TriggerMetadata["port"] != "" { - paramPort = config.TriggerMetadata["port"] - } + paramPort, _ = GetFromAuthOrMeta(config, "port") if paramPort != "" { port, err := strconv.Atoi(paramPort) if err != nil { @@ -132,18 +125,10 @@ func parseMSSQLMetadata(config *ScalerConfig) (*mssqlMetadata, error) { meta.port = port } - if config.AuthParams["username"] != "" { - meta.username = config.AuthParams["username"] - } else if config.TriggerMetadata["username"] != "" { - meta.username = config.TriggerMetadata["username"] - } + meta.username, _ = GetFromAuthOrMeta(config, "username") // database is optional in SQL s - if config.AuthParams["database"] != "" { - meta.database = config.AuthParams["database"] - } else if config.TriggerMetadata["database"] != "" { - meta.database = config.TriggerMetadata["database"] - } + meta.database, _ = GetFromAuthOrMeta(config, "database") if config.AuthParams["password"] != "" { meta.password = config.AuthParams["password"] From f2c99c57be66cb8de53c887a2e7f25deddf969ce Mon Sep 17 00:00:00 2001 From: jorturfer Date: Mon, 4 Oct 2021 14:22:06 +0200 Subject: [PATCH 7/7] Apply suggestions Co-authored-by: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com> Signed-off-by: jorturfer --- pkg/scalers/scaler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/scalers/scaler.go b/pkg/scalers/scaler.go index 50949fed0d0..1a7cc9e171f 100644 --- a/pkg/scalers/scaler.go +++ b/pkg/scalers/scaler.go @@ -83,7 +83,7 @@ type ScalerConfig struct { PodIdentity kedav1alpha1.PodIdentityProvider } -// This method helps getting a field from Auth or Meta sections +// GetFromAuthOrMeta helps getting a field from Auth or Meta sections func GetFromAuthOrMeta(config *ScalerConfig, field string) (string, error) { var result string var err error