From ae8fde2abd99390ced977cc41a9c08ef4f3613e5 Mon Sep 17 00:00:00 2001 From: Arpit Agarwal Date: Thu, 14 Mar 2024 03:58:34 +0530 Subject: [PATCH] [awsproxy] Expose service name as config option (#29550) awsproxy extension can be used as proxy to any service not just xray. The service name variable was hardcoded to "xray". This PR exposes it to be configurable in the config.yaml Appropriate README of the extension has been updated to reflect the addition of new option Signed-off-by: Arpit Agarwal --- .chloggen/main.yaml | 27 +++++++++++++++++++++++++ extension/awsproxy/README.md | 3 +++ extension/awsproxy/config_test.go | 1 + extension/awsproxy/testdata/config.yaml | 1 + internal/aws/proxy/cfg.go | 5 +++++ internal/aws/proxy/server.go | 14 +++++++------ internal/aws/proxy/server_test.go | 2 +- receiver/awsxrayreceiver/config_test.go | 2 ++ 8 files changed, 48 insertions(+), 7 deletions(-) create mode 100755 .chloggen/main.yaml diff --git a/.chloggen/main.yaml b/.chloggen/main.yaml new file mode 100755 index 000000000000..c0c8fe2d2061 --- /dev/null +++ b/.chloggen/main.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: awsproxyextension + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Expose service_name as configurable option. Previously, it was hardcoded as xray." + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [29550] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/extension/awsproxy/README.md b/extension/awsproxy/README.md index b6b31428afd3..beeb2b3f76a0 100644 --- a/extension/awsproxy/README.md +++ b/extension/awsproxy/README.md @@ -35,6 +35,7 @@ extensions: role_arn: "" aws_endpoint: "" local_mode: false + service_name: "xray" ``` ### endpoint (Optional) @@ -66,3 +67,5 @@ The IAM role used by this proxy when communicating with the AWS service. If non- ### aws_endpoint (Optional) The AWS service endpoint which this proxy forwards requests to. If not set, will default to the AWS X-Ray endpoint. +### service_name (Optional) +The AWS service name which this proxy forwards requests to. If not set, will default to "xray" diff --git a/extension/awsproxy/config_test.go b/extension/awsproxy/config_test.go index 294569979f34..4834342dfa2c 100644 --- a/extension/awsproxy/config_test.go +++ b/extension/awsproxy/config_test.go @@ -44,6 +44,7 @@ func TestLoadConfig(t *testing.T) { Region: "us-west-1", RoleARN: "arn:aws:iam::123456789012:role/awesome_role", AWSEndpoint: "https://another.aws.endpoint.com", + ServiceName: "es", }, }, }, diff --git a/extension/awsproxy/testdata/config.yaml b/extension/awsproxy/testdata/config.yaml index 4c49c01f9f05..95a454c45979 100644 --- a/extension/awsproxy/testdata/config.yaml +++ b/extension/awsproxy/testdata/config.yaml @@ -8,3 +8,4 @@ awsproxy/1: region: "us-west-1" role_arn: "arn:aws:iam::123456789012:role/awesome_role" aws_endpoint: "https://another.aws.endpoint.com" + service_name: "es" diff --git a/internal/aws/proxy/cfg.go b/internal/aws/proxy/cfg.go index c6a1fc966620..4a20b817b4a9 100644 --- a/internal/aws/proxy/cfg.go +++ b/internal/aws/proxy/cfg.go @@ -40,6 +40,10 @@ type Config struct { // will be called or not. Set to `true` to skip EC2 instance // metadata check. LocalMode bool `mapstructure:"local_mode"` + + // ServiceName determines which service the requests are sent to. + // will be default to `xray`. This is mandatory for SigV4 + ServiceName string `mapstructure:"service_name"` } func DefaultConfig() *Config { @@ -55,5 +59,6 @@ func DefaultConfig() *Config { Region: "", RoleARN: "", AWSEndpoint: "", + ServiceName: "xray", } } diff --git a/internal/aws/proxy/server.go b/internal/aws/proxy/server.go index 8a78fdcde8a8..4b15762f30fb 100644 --- a/internal/aws/proxy/server.go +++ b/internal/aws/proxy/server.go @@ -25,7 +25,6 @@ import ( ) const ( - service = "xray" connHeader = "Connection" ) @@ -45,13 +44,16 @@ func NewServer(cfg *Config, logger *zap.Logger) (Server, error) { if cfg.ProxyAddress != "" { logger.Debug("Using remote proxy", zap.String("address", cfg.ProxyAddress)) } + if cfg.ServiceName == "" { + cfg.ServiceName = "xray" + } awsCfg, sess, err := getAWSConfigSession(cfg, logger) if err != nil { return nil, err } - awsEndPoint, err := getServiceEndpoint(awsCfg) + awsEndPoint, err := getServiceEndpoint(awsCfg, cfg.ServiceName) if err != nil { return nil, err } @@ -101,7 +103,7 @@ func NewServer(cfg *Config, logger *zap.Logger) (Server, error) { } // Sign request. signer.Sign() also repopulates the request body. - _, err = signer.Sign(req, body, service, *awsCfg.Region, time.Now()) + _, err = signer.Sign(req, body, cfg.ServiceName, *awsCfg.Region, time.Now()) if err != nil { logger.Error("Unable to sign request", zap.Error(err)) } @@ -117,13 +119,13 @@ func NewServer(cfg *Config, logger *zap.Logger) (Server, error) { // getServiceEndpoint returns X-Ray service endpoint. // It is guaranteed that awsCfg config instance is non-nil and the region value is non nil or non empty in awsCfg object. -// Currently the caller takes care of it. -func getServiceEndpoint(awsCfg *aws.Config) (string, error) { +// Currently, the caller takes care of it. +func getServiceEndpoint(awsCfg *aws.Config, serviceName string) (string, error) { if isEmpty(awsCfg.Endpoint) { if isEmpty(awsCfg.Region) { return "", errors.New("unable to generate endpoint from region with nil value") } - resolved, err := endpoints.DefaultResolver().EndpointFor(service, *awsCfg.Region, setResolverConfig()) + resolved, err := endpoints.DefaultResolver().EndpointFor(serviceName, *awsCfg.Region, setResolverConfig()) return resolved.URL, err } return *awsCfg.Endpoint, nil diff --git a/internal/aws/proxy/server_test.go b/internal/aws/proxy/server_test.go index 7bff20df8e63..a869cf851f27 100644 --- a/internal/aws/proxy/server_test.go +++ b/internal/aws/proxy/server_test.go @@ -241,7 +241,7 @@ func TestCanCreateTransport(t *testing.T) { } func TestGetServiceEndpointInvalidAWSConfig(t *testing.T) { - _, err := getServiceEndpoint(&aws.Config{}) + _, err := getServiceEndpoint(&aws.Config{}, "") assert.EqualError(t, err, "unable to generate endpoint from region with nil value") } diff --git a/receiver/awsxrayreceiver/config_test.go b/receiver/awsxrayreceiver/config_test.go index a17007b65a5d..d1de6d0672c0 100644 --- a/receiver/awsxrayreceiver/config_test.go +++ b/receiver/awsxrayreceiver/config_test.go @@ -51,6 +51,7 @@ func TestLoadConfig(t *testing.T) { Region: "", RoleARN: "", AWSEndpoint: "", + ServiceName: "xray", }, }, }, @@ -74,6 +75,7 @@ func TestLoadConfig(t *testing.T) { RoleARN: "arn:aws:iam::123456789012:role/awesome_role", AWSEndpoint: "https://another.aws.endpoint.com", LocalMode: true, + ServiceName: "xray", }, }}, }