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

feat(scaler): Add TLS support for Artemis scaler #6474

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
70 changes: 54 additions & 16 deletions pkg/scalers/artemis_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,22 @@ type artemisScaler struct {
//revive:disable:var-naming breaking change on restApiTemplate, wouldn't bring any benefit to users
type artemisMetadata struct {
TriggerIndex int
ManagementEndpoint string `keda:"name=managementEndpoint, order=triggerMetadata, optional"`
QueueName string `keda:"name=queueName, order=triggerMetadata, optional"`
BrokerName string `keda:"name=brokerName, order=triggerMetadata, optional"`
BrokerAddress string `keda:"name=brokerAddress, order=triggerMetadata, optional"`
Username string `keda:"name=username, order=authParams;triggerMetadata;resolvedEnv"`
Password string `keda:"name=password, order=authParams;triggerMetadata;resolvedEnv"`
RestAPITemplate string `keda:"name=restApiTemplate, order=triggerMetadata, optional"`
QueueLength int64 `keda:"name=queueLength, order=triggerMetadata, default=10"`
ActivationQueueLength int64 `keda:"name=activationQueueLength, order=triggerMetadata, default=10"`
CorsHeader string `keda:"name=corsHeader, order=triggerMetadata, optional"`
ManagementEndpoint string `keda:"name=managementEndpoint, order=triggerMetadata, optional"`
QueueName string `keda:"name=queueName, order=triggerMetadata, optional"`
BrokerName string `keda:"name=brokerName, order=triggerMetadata, optional"`
BrokerAddress string `keda:"name=brokerAddress, order=triggerMetadata, optional"`
Username string `keda:"name=username, order=authParams;triggerMetadata;resolvedEnv"`
Password string `keda:"name=password, order=authParams;triggerMetadata;resolvedEnv"`
RestAPITemplate string `keda:"name=restApiTemplate, order=triggerMetadata, optional"`
QueueLength int64 `keda:"name=queueLength, order=triggerMetadata, optional, default=10"`
ActivationQueueLength int64 `keda:"name=activationQueueLength, order=triggerMetadata, optional, default=10"`
CorsHeader string `keda:"name=corsHeader, order=triggerMetadata, optional"`
UnsafeSsl bool `keda:"name=unsafeSsl, order=triggerMetadata, optional, default=false"`
TLS bool `keda:"name=tls, order=triggerMetadata, optional, default=false"`
CA string `keda:"name=ca, order=triggerMetadata, optional"`
Cert string `keda:"name=cert, order=triggerMetadata, optional"`
Key string `keda:"name=key, order=triggerMetadata, optional"`
KeyPassword string `keda:"name=keyPassword, order=triggerMetadata, optional"`
}

//revive:enable:var-naming
Expand Down Expand Up @@ -77,16 +83,24 @@ func (a *artemisMetadata) Validate() error {
if a.CorsHeader == "" {
a.CorsHeader = fmt.Sprintf(defaultCorsHeader, a.ManagementEndpoint)
}

if (a.Cert == "") != (a.Key == "") {
return fmt.Errorf("both cert and key must be provided when using TLS")
}

if a.TLS && a.CA == "" {
return fmt.Errorf("CA certificate must be provided when using TLS")
}

if a.TLS && a.UnsafeSsl {
return fmt.Errorf("'tls' and 'unsafeSsl' cannot both be specified")
}

return nil
}

// NewArtemisQueueScaler creates a new artemis queue Scaler
func NewArtemisQueueScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
// do we need to guarantee this timeout for a specific
// reason? if not, we can have buildScaler pass in
// the global client
httpClient := kedautil.CreateHTTPClient(config.GlobalHTTPTimeout, false)

metricType, err := GetMetricTargetType(config)
if err != nil {
return nil, fmt.Errorf("error getting scaler metric type: %w", err)
Expand All @@ -96,6 +110,24 @@ func NewArtemisQueueScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
if err != nil {
return nil, fmt.Errorf("error parsing artemis metadata: %w", err)
}
// do we need to guarantee this timeout for a specific
// reason? if not, we can have buildScaler pass in
// the global client
Comment on lines +113 to +115
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We pass the value here because it can be changed via ENV by users and some scaler allow overriding it via scaler configuration. Said this, maybe we should move the env evaluation to the http package and add another builder to build the client with another different timeout. This could make the code cleaner @wozniakjan @zroubalik ?

Suggested change
// do we need to guarantee this timeout for a specific
// reason? if not, we can have buildScaler pass in
// the global client

Checking the current options, I'm wondering if a Builder patter can make sense in general to configure all the parameters of the HTTP client as currently we are calling multiple times to the same code, e.g: To create a client with an specific transport, we create the client (which creates a TLS config and a transport) and then we create the new transport and replace the just default created one.
I'm thinking about something like:

client, err := kedautil.NewHTTPClientBuilder().WithTimeout(...).WithTransport(...).With...Build()

It's more verbose but more efficient too, and it shouldn't be a huge change in terms of changes (to address them in another PR. If we agree with changing this, I'll create another issue)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue that builder pattern is not golang idiomatic, you should pass these as options

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options are good enough for me as it will support generating the transport once

httpClient := kedautil.CreateHTTPClient(config.GlobalHTTPTimeout, artemisMetadata.UnsafeSsl)

if artemisMetadata.TLS {
tlsConfig, err := kedautil.NewTLSConfigWithPassword(
artemisMetadata.Cert,
artemisMetadata.Key,
artemisMetadata.KeyPassword,
artemisMetadata.CA,
artemisMetadata.UnsafeSsl,
)
if err != nil {
return nil, fmt.Errorf("failed to configure TLS: %w", err)
}
httpClient.Transport = kedautil.CreateHTTPTransportWithTLSConfig(tlsConfig)
}

return &artemisScaler{
metricType: metricType,
Expand Down Expand Up @@ -149,7 +181,13 @@ func getAPIParameters(meta artemisMetadata) (artemisMetadata, error) {
}

func (s *artemisScaler) getMonitoringEndpoint() string {
replacer := strings.NewReplacer("<<managementEndpoint>>", s.metadata.ManagementEndpoint,
scheme := natsStreamingHTTPProtocol

if s.metadata.TLS {
scheme = natsStreamingHTTPSProtocol
}
replacer := strings.NewReplacer(
"<<managementEndpoint>>", fmt.Sprintf("%s://%s", scheme, s.metadata.ManagementEndpoint),
"<<queueName>>", s.metadata.QueueName,
"<<brokerName>>", s.metadata.BrokerName,
"<<brokerAddress>>", s.metadata.BrokerAddress)
Expand Down
27 changes: 27 additions & 0 deletions pkg/scalers/artemis_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,30 @@ func TestArtemisGetMetricSpecForScaling(t *testing.T) {
}
}
}

func TestArtemisTLSConfiguration(t *testing.T) {
metadata := map[string]string{
"managementEndpoint": "localhost:8161",
"queueName": "queue1",
"brokerName": "broker-activemq",
"brokerAddress": "test",
"ca": "/path/to/ca.pem",
"cert": "/path/to/cert.pem",
"key": "/path/to/key.pem",
}

resolvedEnv := map[string]string{
"username": "admin",
"password": "admin",
}

_, err := parseArtemisMetadata(&scalersconfig.ScalerConfig{
ResolvedEnv: resolvedEnv,
TriggerMetadata: metadata,
AuthParams: artemisAuthParams, // Ensure valid AuthParams are provided
})

if err != nil {
t.Errorf("Expected success but got error: %v", err)
}
}
Loading