From 0afb18fd7fd3b2f9266869c8f911947093536271 Mon Sep 17 00:00:00 2001 From: Andy Cobaugh Date: Fri, 25 Jun 2021 05:19:38 -0400 Subject: [PATCH] Add support for Trigger Authentication for InfluxDB (#1904) --- CHANGELOG.md | 2 +- pkg/scalers/influxdb_scaler.go | 6 ++++++ pkg/scalers/influxdb_scaler_test.go | 26 +++++++++++++++----------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8083fdb0928..d7d770d02e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,7 +29,7 @@ - Fix READY and ACTIVE fields of ScaledJob to show status when we run `kubectl get sj` ([#1855](https://github.com/kedacore/keda/pull/1855)) - Don't panic when HashiCorp Vault path doesn't exist ([#1864](https://github.com/kedacore/keda/pull/1864)) - +- Allow influxdb `authToken`, `serverURL`, and `organizationName` to be sourced from `(Cluster)TriggerAuthentication` ([#1904](https://github.com/kedacore/keda/pull/1904)) ### Breaking Changes - TODO ([#XXX](https://github.com/kedacore/keda/pull/XXX)) diff --git a/pkg/scalers/influxdb_scaler.go b/pkg/scalers/influxdb_scaler.go index 8fce45f1443..fdaf2d77a6e 100644 --- a/pkg/scalers/influxdb_scaler.go +++ b/pkg/scalers/influxdb_scaler.go @@ -67,6 +67,8 @@ func parseInfluxDBMetadata(config *ScalerConfig) (*influxDBMetadata, error) { } else { return nil, fmt.Errorf("no auth token given") } + case config.AuthParams["authToken"] != "": + authToken = config.AuthParams["authToken"] default: return nil, fmt.Errorf("no auth token given") } @@ -81,6 +83,8 @@ func parseInfluxDBMetadata(config *ScalerConfig) (*influxDBMetadata, error) { } else { return nil, fmt.Errorf("no organization name given") } + case config.AuthParams["organizationName"] != "": + organizationName = config.AuthParams["organizationName"] default: return nil, fmt.Errorf("no organization name given") } @@ -93,6 +97,8 @@ func parseInfluxDBMetadata(config *ScalerConfig) (*influxDBMetadata, error) { if val, ok := config.TriggerMetadata["serverURL"]; ok { serverURL = val + } else if val, ok := config.AuthParams["serverURL"]; ok { + serverURL = val } else { return nil, fmt.Errorf("no server url given") } diff --git a/pkg/scalers/influxdb_scaler_test.go b/pkg/scalers/influxdb_scaler_test.go index 7aea02f5dc2..7c1b3d62dcd 100644 --- a/pkg/scalers/influxdb_scaler_test.go +++ b/pkg/scalers/influxdb_scaler_test.go @@ -12,8 +12,9 @@ var testInfluxDBResolvedEnv = map[string]string{ } type parseInfluxDBMetadataTestData struct { - metadata map[string]string - isError bool + metadata map[string]string + isError bool + authParams map[string]string } type influxDBMetricIdentifier struct { @@ -23,21 +24,24 @@ type influxDBMetricIdentifier struct { var testInfluxDBMetadata = []parseInfluxDBMetadataTestData{ // nothing passed - {map[string]string{}, true}, + {map[string]string{}, true, map[string]string{}}, // everything is passed in verbatim - {map[string]string{"serverURL": "https://influxdata.com", "metricName": "influx_metric", "organizationName": "influx_org", "query": "from(bucket: hello)", "thresholdValue": "10", "authToken": "myToken"}, false}, + {map[string]string{"serverURL": "https://influxdata.com", "metricName": "influx_metric", "organizationName": "influx_org", "query": "from(bucket: hello)", "thresholdValue": "10", "authToken": "myToken"}, false, map[string]string{}}, // everything is passed in (environment variables) - {map[string]string{"serverURL": "https://influxdata.com", "organizationNameFromEnv": "INFLUX_ORG", "query": "from(bucket: hello)", "thresholdValue": "10", "authTokenFromEnv": "INFLUX_TOKEN"}, false}, + {map[string]string{"serverURL": "https://influxdata.com", "organizationNameFromEnv": "INFLUX_ORG", "query": "from(bucket: hello)", "thresholdValue": "10", "authTokenFromEnv": "INFLUX_TOKEN"}, false, map[string]string{}}, // no serverURL passed - {map[string]string{"metricName": "influx_metric", "organizationName": "influx_org", "query": "from(bucket: hello)", "thresholdValue": "10", "authToken": "myToken"}, true}, + {map[string]string{"metricName": "influx_metric", "organizationName": "influx_org", "query": "from(bucket: hello)", "thresholdValue": "10", "authToken": "myToken"}, true, map[string]string{}}, // no organization name passed - {map[string]string{"serverURL": "https://influxdata.com", "metricName": "influx_metric", "query": "from(bucket: hello)", "thresholdValue": "10", "authToken": "myToken"}, true}, + {map[string]string{"serverURL": "https://influxdata.com", "metricName": "influx_metric", "query": "from(bucket: hello)", "thresholdValue": "10", "authToken": "myToken"}, true, map[string]string{}}, // no query passed - {map[string]string{"serverURL": "https://influxdata.com", "organizationName": "influx_org", "thresholdValue": "10", "authToken": "myToken"}, true}, + {map[string]string{"serverURL": "https://influxdata.com", "organizationName": "influx_org", "thresholdValue": "10", "authToken": "myToken"}, true, map[string]string{}}, // no threshold value passed - {map[string]string{"serverURL": "https://influxdata.com", "organizationName": "influx_org", "query": "from(bucket: hello)", "authToken": "myToken"}, true}, + {map[string]string{"serverURL": "https://influxdata.com", "organizationName": "influx_org", "query": "from(bucket: hello)", "authToken": "myToken"}, true, map[string]string{}}, // no auth token passed - {map[string]string{"serverURL": "https://influxdata.com", "organizationName": "influx_org", "query": "from(bucket: hello)", "thresholdValue": "10"}, true}} + {map[string]string{"serverURL": "https://influxdata.com", "organizationName": "influx_org", "query": "from(bucket: hello)", "thresholdValue": "10"}, true, map[string]string{}}, + // authToken, organizationName, and serverURL are defined in authParams + {map[string]string{"query": "from(bucket: hello)", "thresholdValue": "10"}, false, map[string]string{"serverURL": "https://influxdata.com", "organizationName": "influx_org", "authToken": "myToken"}}, +} var influxDBMetricIdentifiers = []influxDBMetricIdentifier{ {&testInfluxDBMetadata[1], "influxdb-influx_metric"}, @@ -47,7 +51,7 @@ var influxDBMetricIdentifiers = []influxDBMetricIdentifier{ func TestInfluxDBParseMetadata(t *testing.T) { testCaseNum := 1 for _, testData := range testInfluxDBMetadata { - _, err := parseInfluxDBMetadata(&ScalerConfig{TriggerMetadata: testData.metadata, ResolvedEnv: testInfluxDBResolvedEnv}) + _, err := parseInfluxDBMetadata(&ScalerConfig{TriggerMetadata: testData.metadata, ResolvedEnv: testInfluxDBResolvedEnv, AuthParams: testData.authParams}) if err != nil && !testData.isError { t.Errorf("Expected success but got error for unit test # %v", testCaseNum) }