Skip to content

Commit

Permalink
Rename and add utility functions for imdsVersion enum
Browse files Browse the repository at this point in the history
feat(pkg/util/ec2/imds_helpers): Add enableIMDSv2 function and forceIMDSv2 function to describe IMDS action to determine http request configuration
fix(pkg/util/ec2/imds_helpers): Rename imdsVersion enum to EC2IMDSVersionConfig
  • Loading branch information
louis-cqrl committed Nov 5, 2024
1 parent fb3b7c8 commit 9fdf71d
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions pkg/util/ec2/imds_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ var (
imdsNetworkMacs = "/network/interfaces/macs"
)

// imdsVersion is an enum to determine how to interact with the IMDSv2 option
type imdsVersion int
// ec2IMDSVersionConfig is an enum to determine how to interact with the IMDSv2 option
type ec2IMDSVersionConfig int

const (
imdsV1 imdsVersion = iota
imdsV1 ec2IMDSVersionConfig = iota
imdsAllVersions
imdsV2
)

func (v ec2IMDSVersionConfig) enableV2() bool {
return v == imdsAllVersions || v == imdsV2
}

func (v ec2IMDSVersionConfig) forceV2() bool {
return v == imdsV2
}

func getToken(ctx context.Context) (string, time.Time, error) {
tokenLifetime := time.Duration(pkgconfigsetup.Datadog().GetInt("ec2_metadata_token_lifetime")) * time.Second
// Set the local expiration date before requesting the metadata endpoint so the local expiration date will always
Expand All @@ -53,7 +61,7 @@ func getToken(ctx context.Context) (string, time.Time, error) {
return res, expirationDate, nil
}

func getMetadataItemWithMaxLength(ctx context.Context, endpoint string, allowedIMDSVersions imdsVersion, updateMetadataSource bool) (string, error) {
func getMetadataItemWithMaxLength(ctx context.Context, endpoint string, allowedIMDSVersions ec2IMDSVersionConfig, updateMetadataSource bool) (string, error) {
result, err := getMetadataItem(ctx, endpoint, allowedIMDSVersions, updateMetadataSource)
if err != nil {
return result, err
Expand All @@ -66,7 +74,7 @@ func getMetadataItemWithMaxLength(ctx context.Context, endpoint string, allowedI
return result, err
}

func getMetadataItem(ctx context.Context, endpoint string, allowedIMDSVersions imdsVersion, updateMetadataSource bool) (string, error) {
func getMetadataItem(ctx context.Context, endpoint string, allowedIMDSVersions ec2IMDSVersionConfig, updateMetadataSource bool) (string, error) {
if !pkgconfigsetup.IsCloudProviderEnabled(CloudProviderName, pkgconfigsetup.Datadog()) {
return "", fmt.Errorf("cloud provider is disabled by configuration")
}
Expand All @@ -75,34 +83,34 @@ func getMetadataItem(ctx context.Context, endpoint string, allowedIMDSVersions i
}

// useIMDSv2 returns true if the agent should use IMDSv2
func useIMDSv2() imdsVersion {
func useIMDSv2() ec2IMDSVersionConfig {
if pkgconfigsetup.Datadog().GetBool("ec2_prefer_imdsv2") || pkgconfigsetup.Datadog().GetBool("ec2_imdsv2_transition_payload_enabled") {
return imdsAllVersions
}
// if nothing indicates to use IMDSv2, we default to IMDSv1
return imdsV1
}

func doHTTPRequest(ctx context.Context, url string, allowedIMDSVersions imdsVersion, updateMetadataSource bool) (string, error) {
func doHTTPRequest(ctx context.Context, url string, allowedIMDSVersions ec2IMDSVersionConfig, updateMetadataSource bool) (string, error) {
source := metadataSourceIMDSv1
headers := map[string]string{}
if allowedIMDSVersions == imdsAllVersions || allowedIMDSVersions == imdsV2 {
if allowedIMDSVersions.enableV2() {
tokenValue, err := token.Get(ctx)
if err != nil {
if allowedIMDSVersions == imdsV2 {
if allowedIMDSVersions.forceV2() {
return "", fmt.Errorf("could not fetch token from IMDSv2")
}
log.Warnf("ec2_prefer_imdsv2 is set to true in the configuration but the agent was unable to proceed: %s", err)
} else {
headers["X-aws-ec2-metadata-token"] = tokenValue
if allowedIMDSVersions != imdsV2 {
if !allowedIMDSVersions.forceV2() {
source = metadataSourceIMDSv2
}
}
}
res, err := httputils.Get(ctx, url, headers, time.Duration(pkgconfigsetup.Datadog().GetInt("ec2_metadata_timeout"))*time.Millisecond, pkgconfigsetup.Datadog())
// We don't want to register the source when we force imdsv2
if err == nil && allowedIMDSVersions != imdsV2 && updateMetadataSource {
if err == nil && !allowedIMDSVersions.forceV2() && updateMetadataSource {
setCloudProviderSource(source)
}
return res, err
Expand Down

0 comments on commit 9fdf71d

Please sign in to comment.