-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix: CloudWatch metadata enrichment failing due to account ID prefix in event identifiers #48712
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
Open
MichaelKatsoulis
wants to merge
4
commits into
elastic:main
Choose a base branch
from
MichaelKatsoulis:bugfix/ec2-metadata-identifier
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−18
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9ad1a11
Get resource id from identifier
MichaelKatsoulis 50d9363
Merge branch 'main' into bugfix/ec2-metadata-identifier
MichaelKatsoulis 1ad211e
Fix linter errors
MichaelKatsoulis 5f03e3a
Merge branch 'main' into bugfix/ec2-metadata-identifier
MichaelKatsoulis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
x-pack/metricbeat/module/aws/cloudwatch/metadata/metadata.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package metadata | ||
|
|
||
| import ( | ||
| "strings" | ||
| "unicode" | ||
| ) | ||
|
|
||
| // ExtractResourceID extracts the resource identifier from an event identifier. | ||
| // Event identifier format: {accountId}-{resourceId}-{index} | ||
| // Account ID is always 12 digits, so we detect and strip it. | ||
| func ExtractResourceID(eventIdentifier string) string { | ||
| parts := strings.Split(eventIdentifier, "-") | ||
| if len(parts) < 2 { | ||
| return eventIdentifier | ||
| } | ||
|
|
||
| startIdx := 0 | ||
| // Check if first part is a 12-digit account ID | ||
| if len(parts[0]) == 12 && isAllDigits(parts[0]) { | ||
| startIdx = 1 | ||
| } | ||
|
|
||
| // Remove the last part (index) and join the rest | ||
| if startIdx < len(parts)-1 { | ||
| return strings.Join(parts[startIdx:len(parts)-1], "-") | ||
| } | ||
| return eventIdentifier | ||
| } | ||
|
|
||
| // isAllDigits returns true if the string contains only digits | ||
| func isAllDigits(s string) bool { | ||
| for _, c := range s { | ||
| if !unicode.IsDigit(c) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
x-pack/metricbeat/module/aws/cloudwatch/metadata/metadata_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package metadata | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestExtractResourceID(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| eventIdentifier string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "EC2 instance ID", | ||
| eventIdentifier: "123456789012-i-0abcd1234efgh5678-0", | ||
| expected: "i-0abcd1234efgh5678", | ||
| }, | ||
| { | ||
| name: "RDS with multiple dashes", | ||
| eventIdentifier: "123456789012-my-database-instance-0", | ||
| expected: "my-database-instance", | ||
| }, | ||
| { | ||
| name: "11-digit prefix is not stripped", | ||
| eventIdentifier: "12345678901-resource-0", | ||
| expected: "12345678901-resource", | ||
| }, | ||
| { | ||
| name: "Single part returns as is", | ||
| eventIdentifier: "singlevalue", | ||
| expected: "singlevalue", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := ExtractResourceID(tt.eventIdentifier) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have one concern, we create identifier as below,
if
labels[aws.LabelConst.IdentifierValueIdx]contain dashes, then this logic can fail 🤔