Skip to content

Commit 9adedf4

Browse files
committed
handle empty tags, add tests and close underlying body
Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent 7f2db42 commit 9adedf4

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func getTags(ctx context.Context, imdsClient IMDSClient, ec2Client EC2Client, in
153153
logger.Info("Tag extraction from IMDS failed, fallback to DescribeTags API to obtain EKS cluster name.")
154154
clusterName, err := clusterNameFromDescribeTag(ctx, ec2Client, instanceId)
155155
if err != nil {
156-
logger.Warnf("error obtaining cluster name: %s.", err)
156+
logger.Warnf("error obtaining cluster name: %v.", err)
157157
return tags
158158
}
159159

@@ -169,36 +169,52 @@ func getTags(ctx context.Context, imdsClient IMDSClient, ec2Client EC2Client, in
169169
func getTagsFromIMDS(ctx context.Context, client IMDSClient, logger *logp.Logger) (tags map[string]string, ok bool) {
170170
tags = make(map[string]string)
171171

172-
metadata, err := client.GetMetadata(ctx, &imds.GetMetadataInput{Path: tagsCategory})
172+
b, err := getMetadataHelper(ctx, client, tagsCategory, logger)
173173
if err != nil {
174-
logger.Warnf("error from IMDS tags category request: %s", err)
174+
logger.Warnf("error obtaining tags category: %v", err)
175175
return tags, false
176176
}
177177

178-
b, err := io.ReadAll(metadata.Content)
179-
if err != nil {
180-
logger.Warnf("error extracting tags category payload: %s", err)
181-
return tags, false
178+
tagInput := string(b)
179+
if tagInput == "" {
180+
logger.Info("No tags present in the EC2 instance")
181+
return tags, true
182182
}
183183

184-
for _, tag := range strings.Split(string(b), "\n") {
184+
for _, tag := range strings.Split(tagInput, "\n") {
185185
tagPath := fmt.Sprintf("%s/%s", tagsCategory, tag)
186-
metadata, err := client.GetMetadata(ctx, &imds.GetMetadataInput{Path: tagPath})
186+
b, err := getMetadataHelper(ctx, client, tagPath, logger)
187187
if err != nil {
188-
logger.Warnf("error from IMDS tag request: %s", err)
188+
logger.Warnf("error extracting tag value of %s: %v", tag, err)
189189
return tags, false
190190
}
191191

192-
b, err := io.ReadAll(metadata.Content)
192+
tags[tag] = string(b)
193+
}
194+
195+
return tags, true
196+
}
197+
198+
// getMetadataHelper performs the IMDS call for the given path and returns the response content after closing the underlying content reader.
199+
func getMetadataHelper(ctx context.Context, client IMDSClient, path string, logger *logp.Logger) (content []byte, err error) {
200+
metadata, err := client.GetMetadata(ctx, &imds.GetMetadataInput{Path: path})
201+
if err != nil {
202+
return nil, fmt.Errorf("error from IMDS metadata request: %w", err)
203+
}
204+
205+
defer func(Content io.ReadCloser) {
206+
err := Content.Close()
193207
if err != nil {
194-
logger.Warnf("error extracting tag value payload: %s", err)
195-
return tags, false
208+
logger.Warnf("error closing IMDS metadata response body: %v", err)
196209
}
210+
}(metadata.Content)
197211

198-
tags[tag] = string(b)
212+
content, err = io.ReadAll(metadata.Content)
213+
if err != nil {
214+
return nil, fmt.Errorf("error extracting metadata from the IMDS response: %w", err)
199215
}
200216

201-
return tags, true
217+
return content, nil
202218
}
203219

204220
// clusterNameFromDescribeTag is a helper to extract EKS cluster name using DescribeTag.

libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,19 @@ func Test_getTags(t *testing.T) {
482482
}},
483483
want: map[string]string{},
484484
},
485+
{
486+
name: "Empty tags results in empty tag extraction",
487+
imdsClient: &MockIMDSClient{
488+
GetMetadataFunc: func(ctx context.Context, input *imds.GetMetadataInput, f ...func(*imds.Options)) (*imds.GetMetadataOutput, error) {
489+
return &imds.GetMetadataOutput{Content: io.NopCloser(strings.NewReader(""))}, nil
490+
},
491+
},
492+
ec2Client: &MockEC2Client{
493+
DescribeTagsFunc: func(ctx context.Context, params *ec2.DescribeTagsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeTagsOutput, error) {
494+
return nil, errors.New("some error from DescribeTag")
495+
}},
496+
want: map[string]string{},
497+
},
485498
}
486499
for _, tt := range tests {
487500
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)