Skip to content

Commit

Permalink
Avoid manually configuring the endpoint for Access Points
Browse files Browse the repository at this point in the history
  • Loading branch information
chemamartinez committed Nov 19, 2024
1 parent 7b5236f commit b4a41e4
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 78 deletions.
6 changes: 1 addition & 5 deletions x-pack/filebeat/input/awss3/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ func (im *s3InputManager) Create(cfg *conf.C) (v2.Input, error) {
return nil, fmt.Errorf("initializing AWS config: %w", err)
}

if config.AccessPointARN != "" {
configureAccessPointEndpoint(&awsConfig, config.AccessPointARN)
}

if config.AccessPointARN == "" && config.RegionName != "" {
if config.RegionName != "" {
// The awsConfig now contains the region from the credential profile or default region
// if the region is explicitly set in the config, then it wins
awsConfig.Region = config.RegionName
Expand Down
26 changes: 0 additions & 26 deletions x-pack/filebeat/input/awss3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,3 @@ func getProviderFromDomain(endpoint string, ProviderOverride string) string {
}
return "unknown"
}

func configureAccessPointEndpoint(awsConfig *awssdk.Config, accessPointARN string) error {
// When using the access point ARN, requests must be directed to the
// access point hostname. The access point hostname takes the form
// AccessPointName-AccountId.s3-accesspoint.Region.amazonaws.com
parts := strings.Split(accessPointARN, ":")
region := parts[3]
accountID := parts[4]
accessPointName := strings.Split(parts[5], "/")[1]

// Construct the endpoint for the Access Point
endpoint := fmt.Sprintf("%s-%s.s3-accesspoint.%s.amazonaws.com", accessPointName, accountID, region)

// Set up a custom endpoint resolver for Access Points
//nolint:staticcheck // haven't migrated to the new interface yet
awsConfig.EndpointResolverWithOptions = awssdk.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (awssdk.Endpoint, error) {
return awssdk.Endpoint{
URL: fmt.Sprintf("https://%s", endpoint),
SigningRegion: region,
HostnameImmutable: true,
}, nil
})
awsConfig.Region = region

return nil
}
47 changes: 0 additions & 47 deletions x-pack/filebeat/input/awss3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent-libs/logp"
Expand Down Expand Up @@ -295,49 +294,3 @@ func TestS3ReaderLoop(t *testing.T) {
func TestS3WorkerLoop(t *testing.T) {

}

// TestConfigureAccessPointEndpoint tests the configureAccessPointEndpoint function
func TestConfigureAccessPointEndpoint(t *testing.T) {
testCases := []struct {
name string
accessPointARN string
expectedRegion string
expectedEndpoint string
}{
{
name: "Valid Access Point ARN in us-east-1",
accessPointARN: "arn:aws:s3:us-east-1:123456789:accesspoint/my-access-point",
expectedRegion: "us-east-1",
expectedEndpoint: "https://my-access-point-123456789.s3-accesspoint.us-east-1.amazonaws.com",
},
{
name: "Valid Access Point ARN in eu-west-1",
accessPointARN: "arn:aws:s3:eu-west-1:123456789:accesspoint/my-other-access-point",
expectedRegion: "eu-west-1",
expectedEndpoint: "https://my-other-access-point-123456789.s3-accesspoint.eu-west-1.amazonaws.com",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create a new aws.Config
awsConfig := aws.Config{}

// Call the function under test
configureAccessPointEndpoint(&awsConfig, tc.accessPointARN)

// Assert the region was set correctly
assert.Equal(t, tc.expectedRegion, awsConfig.Region)

// Assert the custom endpoint resolver was configured correctly
//nolint:staticcheck // haven't migrated to the new interface yet
if resolver, ok := awsConfig.EndpointResolverWithOptions.(aws.EndpointResolverWithOptionsFunc); ok {
endpoint, err := resolver("s3", tc.expectedRegion)
assert.NoError(t, err)
assert.Equal(t, tc.expectedEndpoint, endpoint.URL)
} else {
t.Fatalf("EndpointResolverWithOptions is not properly configured")
}
})
}
}

0 comments on commit b4a41e4

Please sign in to comment.