Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apache artemis origin header needed #1796

Merged
merged 3 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 10 additions & 1 deletion pkg/scalers/artemis_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type artemisMetadata struct {
password string
restAPITemplate string
queueLength int
corsHeader string
}

//revive:enable:var-naming
Expand All @@ -48,6 +49,7 @@ const (
artemisMetricType = "External"
defaultArtemisQueueLength = 10
defaultRestAPITemplate = "http://<<managementEndpoint>>/console/jolokia/read/org.apache.activemq.artemis:broker=\"<<brokerName>>\",component=addresses,address=\"<<brokerAddress>>\",subcomponent=queues,routing-type=\"anycast\",queue=\"<<queueName>>\"/MessageCount"
defaultCorsHeader = "http://%s"
)

var artemisLog = logf.Log.WithName("artemis_queue_scaler")
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
24 changes: 24 additions & 0 deletions pkg/scalers/artemis_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down