Skip to content

Commit

Permalink
Rollback on useIMDSv2 function name
Browse files Browse the repository at this point in the history
Since the `useIMDSv2` function is not used anymore to check if IMDSv2 should be disable, we fall back on the previous use case
fix(pkg/util/ec2/ec2.go, pkg/util/ec2/ec2_tags.go, pkg/util/ec2/imds_helpers.go, pkg/util/ec2/network.go): Rollback on `useIMDSv2` function name
  • Loading branch information
louis-cqrl committed Nov 5, 2024
1 parent 1993369 commit fb3b7c8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pkg/util/ec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func GetSourceName() string {
var instanceIDFetcher = cachedfetch.Fetcher{
Name: "EC2 or DMI InstanceID",
Attempt: func(ctx context.Context) (interface{}, error) {
hostname, err := getMetadataItemWithMaxLength(ctx, imdsInstanceID, getIMDSVersion(), true)
hostname, err := getMetadataItemWithMaxLength(ctx, imdsInstanceID, useIMDSv2(), true)
if err != nil {
if pkgconfigsetup.Datadog().GetBool(ec2IMDSv2TransitionPayloadConfigFlag) {
log.Debugf("Failed to get instance ID from IMDSv2 - ec2_imdsv2_transition_payload_enabled is set, falling back on DMI: %s", err.Error())
Expand Down Expand Up @@ -177,7 +177,7 @@ func GetHostAliases(ctx context.Context) ([]string, error) {
log.Debugf("failed to get instance ID from DMI for Host Alias: %s", err)

// Try to use IMSDv2 if GetInstanceID didn't try it already
imdsv2Action := getIMDSVersion()
imdsv2Action := useIMDSv2()
if imdsv2Action == imdsV1 {
imsdv2InstanceID, err := GetIDMSv2InstanceID(ctx)
if err == nil {
Expand All @@ -193,7 +193,7 @@ func GetHostAliases(ctx context.Context) ([]string, error) {
var hostnameFetcher = cachedfetch.Fetcher{
Name: "EC2 Hostname",
Attempt: func(ctx context.Context) (interface{}, error) {
return getMetadataItemWithMaxLength(ctx, imdsHostname, getIMDSVersion(), true)
return getMetadataItemWithMaxLength(ctx, imdsHostname, useIMDSv2(), true)
},
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/util/ec2/ec2_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func fetchEc2Tags(ctx context.Context) ([]string, error) {
}

func fetchEc2TagsFromIMDS(ctx context.Context) ([]string, error) {
keysStr, err := getMetadataItem(ctx, imdsTags, getIMDSVersion(), true)
keysStr, err := getMetadataItem(ctx, imdsTags, useIMDSv2(), true)
if err != nil {
return nil, err
}
Expand All @@ -73,7 +73,7 @@ func fetchEc2TagsFromIMDS(ctx context.Context) ([]string, error) {
// > keys can only use letters (a-z, A-Z), numbers (0-9), and the
// > following characters: -_+=,.@:. Instance tag keys can't use spaces,
// > /, or the reserved names ., .., or _index.
val, err := getMetadataItem(ctx, imdsTags+"/"+key, getIMDSVersion(), true)
val, err := getMetadataItem(ctx, imdsTags+"/"+key, useIMDSv2(), true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -194,7 +194,7 @@ type EC2Identity struct {
// GetInstanceIdentity returns the instance identity document for the current instance
func GetInstanceIdentity(ctx context.Context) (*EC2Identity, error) {
instanceIdentity := &EC2Identity{}
res, err := doHTTPRequest(ctx, instanceIdentityURL, getIMDSVersion(), true)
res, err := doHTTPRequest(ctx, instanceIdentityURL, useIMDSv2(), true)
if err != nil {
return instanceIdentity, fmt.Errorf("unable to fetch EC2 API to get identity: %s", err)
}
Expand All @@ -221,7 +221,7 @@ func getSecurityCreds(ctx context.Context) (*ec2SecurityCred, error) {
return iamParams, err
}

res, err := doHTTPRequest(ctx, metadataURL+"/iam/security-credentials/"+iamRole, getIMDSVersion(), true)
res, err := doHTTPRequest(ctx, metadataURL+"/iam/security-credentials/"+iamRole, useIMDSv2(), true)
if err != nil {
return iamParams, fmt.Errorf("unable to fetch EC2 API to get iam role: %s", err)
}
Expand All @@ -234,7 +234,7 @@ func getSecurityCreds(ctx context.Context) (*ec2SecurityCred, error) {
}

func getIAMRole(ctx context.Context) (string, error) {
res, err := doHTTPRequest(ctx, metadataURL+"/iam/security-credentials/", getIMDSVersion(), true)
res, err := doHTTPRequest(ctx, metadataURL+"/iam/security-credentials/", useIMDSv2(), true)
if err != nil {
return "", fmt.Errorf("unable to fetch EC2 API to get security credentials: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/ec2/imds_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func getMetadataItem(ctx context.Context, endpoint string, allowedIMDSVersions i
return doHTTPRequest(ctx, metadataURL+endpoint, allowedIMDSVersions, updateMetadataSource)
}

// getIMDSVersion returns true if the agent should use IMDSv2
func getIMDSVersion() imdsVersion {
// useIMDSv2 returns true if the agent should use IMDSv2
func useIMDSv2() imdsVersion {
if pkgconfigsetup.Datadog().GetBool("ec2_prefer_imdsv2") || pkgconfigsetup.Datadog().GetBool("ec2_imdsv2_transition_payload_enabled") {
return imdsAllVersions
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/ec2/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
var publicIPv4Fetcher = cachedfetch.Fetcher{
Name: "EC2 Public IPv4 Address",
Attempt: func(ctx context.Context) (interface{}, error) {
return getMetadataItem(ctx, imdsIPv4, getIMDSVersion(), true)
return getMetadataItem(ctx, imdsIPv4, useIMDSv2(), true)
},
}

Expand Down

0 comments on commit fb3b7c8

Please sign in to comment.