diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e42bbb26d3..eb3cbff2235 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 [#1796](https://github.com/kedacore/keda/pull/1796) ### Improvements 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})