From 316a2b489ca8a026edcb0bf8fc2e0691f2577f3b Mon Sep 17 00:00:00 2001 From: Bernard Chesnoy Date: Mon, 10 May 2021 20:56:11 +0200 Subject: [PATCH 1/3] Apache artemis needs a header origin when joining console management api Adding the origin header making it configurable Signed-off-by: Bernard Chesnoy --- pkg/scalers/artemis_scaler.go | 11 ++++++++++- pkg/scalers/artemis_scaler_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pkg/scalers/artemis_scaler.go b/pkg/scalers/artemis_scaler.go index f1a573a5c0a..e4e53a122b8 100644 --- a/pkg/scalers/artemis_scaler.go +++ b/pkg/scalers/artemis_scaler.go @@ -34,6 +34,7 @@ type artemisMetadata struct { password string restAPITemplate string queueLength int + corsHeader string } //revive:enable:var-naming @@ -48,6 +49,7 @@ const ( artemisMetricType = "External" defaultArtemisQueueLength = 10 defaultRestAPITemplate = "http://<>/console/jolokia/read/org.apache.activemq.artemis:broker=\"<>\",component=addresses,address=\"<>\",subcomponent=queues,routing-type=\"anycast\",queue=\"<>\"/MessageCount" + defaultCorsHeader = "http://%s" ) var artemisLog = logf.Log.WithName("artemis_queue_scaler") @@ -86,6 +88,12 @@ func parseArtemisMetadata(config *ScalerConfig) (*artemisMetadata, error) { } meta.managementEndpoint = config.TriggerMetadata["managementEndpoint"] + if val, ok := config.TriggerMetadata["corsHeader"]; ok && val != "" { + meta.corsHeader = config.TriggerMetadata["corsHeader"] + } else { + meta.corsHeader = fmt.Sprintf(defaultCorsHeader, meta.managementEndpoint) + } + if config.TriggerMetadata["queueName"] == "" { return nil, errors.New("no queue name given") } @@ -176,6 +184,7 @@ func (s *artemisScaler) getQueueMessageCount() (int, error) { req, err := http.NewRequest("GET", url, nil) req.SetBasicAuth(s.metadata.username, s.metadata.password) + req.Header.Set("Origin", s.metadata.corsHeader) if err != nil { return -1, err @@ -193,7 +202,7 @@ func (s *artemisScaler) getQueueMessageCount() (int, error) { if resp.StatusCode == 200 && monitoringInfo.Status == 200 { messageCount = monitoringInfo.MsgCount } else { - return -1, fmt.Errorf("artemis management endpoint response error code : %d", resp.StatusCode) + return -1, fmt.Errorf("artemis management endpoint response error code : %d %d", resp.StatusCode, monitoringInfo.Status) } artemisLog.V(1).Info(fmt.Sprintf("Artemis scaler: Providing metrics based on current queue length %d queue length limit %d", messageCount, s.metadata.queueLength)) diff --git a/pkg/scalers/artemis_scaler_test.go b/pkg/scalers/artemis_scaler_test.go index fecb7276e28..47d30681608 100644 --- a/pkg/scalers/artemis_scaler_test.go +++ b/pkg/scalers/artemis_scaler_test.go @@ -78,6 +78,30 @@ var testArtemisMetadataWithAuthParams = []parseArtemisMetadataTestData{ {map[string]string{"managementEndpoint": "localhost:8161", "queueName": "queue1", "brokerName": "broker-activemq", "brokerAddress": "test"}, false}, } +func TestArtemisDefaultCorsHeader(t *testing.T) { + metadata := map[string]string{"managementEndpoint": "localhost:8161", "queueName": "queue1", "brokerName": "broker-activemq", "brokerAddress": "test", "username": "myUserName", "password": "myPassword"} + meta, err := parseArtemisMetadata(&ScalerConfig{ResolvedEnv: sampleArtemisResolvedEnv, TriggerMetadata: metadata, AuthParams: nil}) + + if err != nil { + t.Error("Expected success but got error", err) + } + if !(meta.corsHeader == "http://localhost:8161") { + t.Errorf("Expected http://localhost:8161 but got %s", meta.corsHeader) + } +} + +func TestArtemisCorsHeader(t *testing.T) { + metadata := map[string]string{"managementEndpoint": "localhost:8161", "queueName": "queue1", "brokerName": "broker-activemq", "brokerAddress": "test", "username": "myUserName", "password": "myPassword", "corsHeader": "test"} + meta, err := parseArtemisMetadata(&ScalerConfig{ResolvedEnv: sampleArtemisResolvedEnv, TriggerMetadata: metadata, AuthParams: nil}) + + if err != nil { + t.Error("Expected success but got error", err) + } + if !(meta.corsHeader == "test") { + t.Errorf("Expected test but got %s", meta.corsHeader) + } +} + func TestArtemisParseMetadata(t *testing.T) { for _, testData := range testArtemisMetadata { _, err := parseArtemisMetadata(&ScalerConfig{ResolvedEnv: sampleArtemisResolvedEnv, TriggerMetadata: testData.metadata, AuthParams: nil}) From 89545143fb8e53f58946684cab80d62199792ba4 Mon Sep 17 00:00:00 2001 From: Bernard Chesnoy Date: Mon, 10 May 2021 21:02:37 +0200 Subject: [PATCH 2/3] Modifying changelog Signed-off-by: Bernard Chesnoy --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e42bbb26d3..22b5bff4ae8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - Add Azure Pipelines Scaler ([#1706](https://github.com/kedacore/keda/pull/1706)) - Add OpenStack Metrics Scaler ([#1382](https://github.com/kedacore/keda/issues/1382)) - Added basic, tls and bearer authentication support to the Prometheus scaler [#1559](https://github.com/kedacore/keda/issues/1559) +- Add header Origin to apache artemis scaler ### Improvements From 57b5bbebbecacad2c4acccff1d81fc960fa1159d Mon Sep 17 00:00:00 2001 From: Bernard Chesnoy Date: Tue, 11 May 2021 14:31:33 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md Co-authored-by: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22b5bff4ae8..eb3cbff2235 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ - Add Azure Pipelines Scaler ([#1706](https://github.com/kedacore/keda/pull/1706)) - Add OpenStack Metrics Scaler ([#1382](https://github.com/kedacore/keda/issues/1382)) - Added basic, tls and bearer authentication support to the Prometheus scaler [#1559](https://github.com/kedacore/keda/issues/1559) -- Add header Origin to apache artemis scaler +- Add header Origin to Apache Artemis scaler [#1796](https://github.com/kedacore/keda/pull/1796) ### Improvements