Skip to content

Commit

Permalink
adding CORS header to activemq scaler for issue #2884 (#2924)
Browse files Browse the repository at this point in the history
Signed-off-by: Mark DeNeve <[email protected]>
  • Loading branch information
xphyr authored Apr 21, 2022
1 parent 501098e commit 7513ffc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ To learn more about our roadmap, we recommend reading [this document](ROADMAP.md
- **General:** Properly handle `restoreToOriginalReplicaCount` if `ScaleTarget` is missing ([#2872](https://github.com/kedacore/keda/issues/2872))
- **General:** Synchronize HPA annotations from ScaledObject ([#2659](https://github.com/kedacore/keda/pull/2659))
- **General:** Updated HTTPClient to be proxy-aware, if available, from environment variables. ([#2577](https://github.com/kedacore/keda/issues/2577))
- **ActiveMQ Scaler:** Add CorsHeader information to ActiveMQ Scaler ([#2884](https://github.com/kedacore/keda/issues/2884))
- **Azure Application Insights Scaler:** Provide support for non-public clouds ([#2735](https://github.com/kedacore/keda/issues/2735))
- **Azure Blob Storage Scaler:** Add optional parameters for counting blobs recursively ([#1789](https://github.com/kedacore/keda/issues/1789))
- **Azure Event Hub Scaler:** Improve logging when blob container not found ([#2363](https://github.com/kedacore/keda/issues/2363))
Expand Down
8 changes: 8 additions & 0 deletions pkg/scalers/activemq_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type activeMQMetadata struct {
password string
restAPITemplate string
targetQueueSize int64
corsHeader string
metricName string
scalerIndex int
}
Expand Down Expand Up @@ -123,6 +124,12 @@ func parseActiveMQMetadata(config *ScalerConfig) (*activeMQMetadata, error) {
}
}

if val, ok := config.TriggerMetadata["corsHeader"]; ok && val != "" {
meta.corsHeader = config.TriggerMetadata["corsHeader"]
} else {
meta.corsHeader = fmt.Sprintf(defaultCorsHeader, meta.managementEndpoint)
}

if meta.username == "" {
return nil, fmt.Errorf("username cannot be empty")
}
Expand Down Expand Up @@ -225,6 +232,7 @@ func (s *activeMQScaler) getQueueMessageCount(ctx context.Context) (int64, error
// Add HTTP Auth and Headers
req.SetBasicAuth(s.metadata.username, s.metadata.password)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Origin", s.metadata.corsHeader)

resp, err := client.Do(req)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions pkg/scalers/activemq_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,30 @@ var testActiveMQMetadata = []parseActiveMQMetadataTestData{
},
}

func TestActiveMQDefaultCorsHeader(t *testing.T) {
metadata := map[string]string{"managementEndpoint": "localhost:8161", "destinationName": "queue1", "brokerName": "broker-activemq", "username": "myUserName", "password": "myPassword"}
meta, err := parseActiveMQMetadata(&ScalerConfig{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 TestActiveMQCorsHeader(t *testing.T) {
metadata := map[string]string{"managementEndpoint": "localhost:8161", "destinationName": "queue1", "brokerName": "broker-activemq", "username": "myUserName", "password": "myPassword", "corsHeader": "test"}
meta, err := parseActiveMQMetadata(&ScalerConfig{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 TestParseActiveMQMetadata(t *testing.T) {
for _, testData := range testActiveMQMetadata {
t.Run(testData.name, func(t *testing.T) {
Expand Down

0 comments on commit 7513ffc

Please sign in to comment.