Skip to content

Commit dd50d49

Browse files
mergify[bot]efd6bhapas
authored
x-pack/filebeat/input/awss3: fix priority of region name configurations (#36034) (#36096)
The code currently prioritises the region_name configuration, even when it is not provided, against the claims of the documentation. Make a check whether it is empty before claiming a conflict and using it. (cherry picked from commit 57d649d) Co-authored-by: Dan Kortschak <[email protected]> Co-authored-by: Bharat Pasupula <[email protected]>
1 parent e155daa commit dd50d49

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

CHANGELOG.next.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
147147
- Improve error reporting and fix IPv6 handling of TCP and UDP metric collection. {pull}35996[35996]
148148
- Fix handling of NUL-terminated log lines in Fortinet Firewall module. {issue}36026[36026] {pull}36027[36027]
149149
- Make redact field configuration recommended in CEL input and log warning if missing. {pull}36008[36008]
150+
- Fix handling of region name configuration in awss3 input {pull}36034[36034]
150151

151152
*Heartbeat*
152153

x-pack/filebeat/input/awss3/input.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ func (in *s3Input) Run(inputContext v2.Context, pipeline beat.Pipeline) error {
119119
if err != nil && in.config.RegionName == "" {
120120
return fmt.Errorf("failed to get AWS region from queue_url: %w", err)
121121
}
122-
if regionName != in.config.RegionName {
123-
inputContext.Logger.Warnf("configured region disagrees with queue_url region: %q != %q: using %[1]q",
124-
in.config.RegionName, regionName)
125-
regionName = in.config.RegionName
122+
var warn regionMismatchError
123+
if errors.As(err, &warn) {
124+
// Warn of mismatch, but go ahead with configured region name.
125+
inputContext.Logger.Warnf("%v: using %q", err, regionName)
126126
}
127127
in.awsConfig.Region = regionName
128128

@@ -306,7 +306,7 @@ func (in *s3Input) createS3Lister(ctx v2.Context, cancelCtx context.Context, cli
306306

307307
var errBadQueueURL = errors.New("QueueURL is not in format: https://sqs.{REGION_ENDPOINT}.{ENDPOINT}/{ACCOUNT_NUMBER}/{QUEUE_NAME}")
308308

309-
func getRegionFromQueueURL(queueURL string, endpoint, defaultRegion string) (string, error) {
309+
func getRegionFromQueueURL(queueURL string, endpoint, defaultRegion string) (region string, err error) {
310310
// get region from queueURL
311311
// Example: https://sqs.us-east-1.amazonaws.com/627959692251/test-s3-logs
312312
u, err := url.Parse(queueURL)
@@ -317,7 +317,11 @@ func getRegionFromQueueURL(queueURL string, endpoint, defaultRegion string) (str
317317
queueHostSplit := strings.SplitN(u.Host, ".", 3)
318318
if len(queueHostSplit) == 3 {
319319
if queueHostSplit[2] == endpoint || (endpoint == "" && strings.HasPrefix(queueHostSplit[2], "amazonaws.")) {
320-
return queueHostSplit[1], nil
320+
region = queueHostSplit[1]
321+
if defaultRegion != "" && region != defaultRegion {
322+
return defaultRegion, regionMismatchError{queueURLRegion: region, defaultRegion: defaultRegion}
323+
}
324+
return region, nil
321325
}
322326
} else if defaultRegion != "" {
323327
return defaultRegion, nil
@@ -326,6 +330,15 @@ func getRegionFromQueueURL(queueURL string, endpoint, defaultRegion string) (str
326330
return "", errBadQueueURL
327331
}
328332

333+
type regionMismatchError struct {
334+
queueURLRegion string
335+
defaultRegion string
336+
}
337+
338+
func (e regionMismatchError) Error() string {
339+
return fmt.Sprintf("configured region disagrees with queue_url region: %q != %q", e.queueURLRegion, e.defaultRegion)
340+
}
341+
329342
func getRegionForBucket(ctx context.Context, s3Client *s3.Client, bucketName string) (string, error) {
330343
getBucketLocationOutput, err := s3Client.GetBucketLocation(ctx, &s3.GetBucketLocationInput{
331344
Bucket: awssdk.String(bucketName),

x-pack/filebeat/input/awss3/input_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ func TestGetRegionFromQueueURL(t *testing.T) {
8181
endpoint: "googlecloud.com",
8282
wantErr: errBadQueueURL,
8383
},
84+
{
85+
name: "mismatch_regions_no_default",
86+
queueURL: "https://sqs.us-east-1.amazonaws.com/627959692251/test-s3-logs",
87+
deflt: "",
88+
want: "us-east-1",
89+
},
90+
{
91+
name: "mismatch_regions",
92+
queueURL: "https://sqs.us-east-1.amazonaws.com/627959692251/test-s3-logs",
93+
deflt: "ap-west-1",
94+
want: "ap-west-1",
95+
wantErr: regionMismatchError{queueURLRegion: "us-east-1", defaultRegion: "ap-west-1"},
96+
},
8497
{
8598
name: "localstack",
8699
queueURL: "http://localhost:4566/000000000000/filebeat-s3-integtest-d9clk9",

0 commit comments

Comments
 (0)