Skip to content

Commit

Permalink
remove redundant custom endpoint resolver conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
faec committed Nov 4, 2024
1 parent 3332f3e commit a7f1fc2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 40 deletions.
19 changes: 14 additions & 5 deletions x-pack/filebeat/input/awss3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package awss3
import (
"errors"
"fmt"
"net/url"
"time"

awssdk "github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -106,6 +107,13 @@ func (c *config) Validate() error {
if c.ProviderOverride != "" && c.NonAWSBucketName == "" {
return errors.New("provider can only be overridden when polling non-AWS S3 services")
}
if c.AWSConfig.Endpoint != "" {
// Make sure the given endpoint can be parsed
_, err := url.Parse(c.AWSConfig.Endpoint)
if err != nil {
return fmt.Errorf("failed to parse endpoint: %w", err)
}
}
if c.BackupConfig.NonAWSBackupToBucketName != "" && c.NonAWSBucketName == "" {
return errors.New("backup to non-AWS bucket can only be used for non-AWS sources")
}
Expand Down Expand Up @@ -249,12 +257,13 @@ func (c config) s3ConfigModifier(o *s3.Options) {
o.EndpointOptions.UseFIPSEndpoint = awssdk.FIPSEndpointStateEnabled
}
// Apply slightly different endpoint resolvers depending on whether we're in S3 or SQS mode.
if c.NonAWSBucketName != "" {
//nolint:staticcheck // haven't migrated to the new interface yet
o.EndpointResolver = nonAWSBucketResolver{endpoint: c.AWSConfig.Endpoint}
} else if c.QueueURL != "" && c.AWSConfig.Endpoint != "" {
if c.AWSConfig.Endpoint != "" {
//nolint:staticcheck // haven't migrated to the new interface yet
o.EndpointResolver = s3.EndpointResolverFromURL(c.AWSConfig.Endpoint)
o.EndpointResolver = s3.EndpointResolverFromURL(c.AWSConfig.Endpoint,
func(e *awssdk.Endpoint) {
// The S3 hostname is immutable in bucket polling mode, mutable otherwise.
e.HostnameImmutable = (c.getBucketARN() != "")
})
}
o.UsePathStyle = c.PathStyle

Expand Down
26 changes: 0 additions & 26 deletions x-pack/filebeat/input/awss3/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ package awss3

import (
"fmt"
"net/url"
"strings"

awssdk "github.com/aws/aws-sdk-go-v2/aws"

"github.com/elastic/beats/v7/filebeat/beater"
v2 "github.com/elastic/beats/v7/filebeat/input/v2"
Expand Down Expand Up @@ -56,28 +52,6 @@ func (im *s3InputManager) Create(cfg *conf.C) (v2.Input, error) {
awsConfig.Region = config.RegionName
}

if config.AWSConfig.Endpoint != "" {
// Parse a URL for the host regardless of it missing the scheme
endpointUri, err := url.Parse(config.AWSConfig.Endpoint)
if err != nil {
return nil, fmt.Errorf("failed to parse endpoint: %w", err)
}

// For backwards compat:
// If the endpoint does not start with S3, we will use the endpoint resolver to make all SDK requests use the specified endpoint
// If the endpoint does start with S3, we will use the default resolver uses the endpoint field but can replace s3 with the desired service name like sqs
if !strings.HasPrefix(endpointUri.Hostname(), "s3") {
awsConfig.EndpointResolverWithOptions = awssdk.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (awssdk.Endpoint, error) {
return awssdk.Endpoint{
PartitionID: "aws",
Source: awssdk.EndpointSourceCustom,
URL: config.AWSConfig.Endpoint,
SigningRegion: awsConfig.Region,
}, nil
})
}
}

if config.QueueURL != "" {
return newSQSReaderInput(config, awsConfig), nil
}
Expand Down
9 changes: 0 additions & 9 deletions x-pack/filebeat/input/awss3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,3 @@ func getProviderFromDomain(endpoint string, ProviderOverride string) string {
}
return "unknown"
}

type nonAWSBucketResolver struct {
endpoint string
}

func (n nonAWSBucketResolver) ResolveEndpoint(region string, options s3.EndpointResolverOptions) (awssdk.Endpoint, error) {
//nolint:staticcheck // haven't migrated to the new interface yet
return awssdk.Endpoint{URL: n.endpoint, SigningRegion: region, HostnameImmutable: true, Source: awssdk.EndpointSourceCustom}, nil
}

0 comments on commit a7f1fc2

Please sign in to comment.