Skip to content

Commit

Permalink
Apache artemis origin header needed (#1796)
Browse files Browse the repository at this point in the history
* Apache artemis needs a header origin when joining console management api
Adding the origin header
making it configurable
Signed-off-by: Bernard Chesnoy <bernard.chesnoy@gmail.com>

* Modifying changelog
Signed-off-by: Bernard Chesnoy <bernard.chesnoy@gmail.com>

* Update CHANGELOG.md

Co-authored-by: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com>

Co-authored-by: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com>
  • Loading branch information
bonky42 and zroubalik authored May 11, 2021
1 parent 0db5d23 commit d60a958
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
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

0 comments on commit d60a958

Please sign in to comment.