-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
ec2 client credential init fix for AWS IRSA #29784
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/credentials" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
|
@@ -97,7 +98,7 @@ func fetchEc2TagsFromAPI(ctx context.Context) ([]string, error) { | |
// except when a more specific role (e.g. task role in ECS) does not have | ||
// EC2:DescribeTags permission, but a more general role (e.g. instance role) | ||
// does have it. | ||
tags, err := getTagsWithCreds(ctx, instanceIdentity, nil) | ||
tags, err := getTagsWithCreds(ctx, instanceIdentity, ec2Connection(ctx, instanceIdentity.Region, nil)) | ||
if err == nil { | ||
return tags, nil | ||
} | ||
|
@@ -111,15 +112,29 @@ func fetchEc2TagsFromAPI(ctx context.Context) ([]string, error) { | |
} | ||
|
||
awsCreds := credentials.NewStaticCredentialsProvider(iamParams.AccessKeyID, iamParams.SecretAccessKey, iamParams.Token) | ||
return getTagsWithCreds(ctx, instanceIdentity, awsCreds) | ||
return getTagsWithCreds(ctx, instanceIdentity, ec2Connection(ctx, instanceIdentity.Region, awsCreds)) | ||
} | ||
|
||
func getTagsWithCreds(ctx context.Context, instanceIdentity *EC2Identity, awsCreds aws.CredentialsProvider) ([]string, error) { | ||
connection := ec2.New(ec2.Options{ | ||
Region: instanceIdentity.Region, | ||
Credentials: awsCreds, | ||
}) | ||
type ec2ClientInterface interface { | ||
DescribeTags(ctx context.Context, params *ec2.DescribeTagsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeTagsOutput, error) | ||
} | ||
|
||
// ec2Connection creates an ec2 client with the given region and credentials | ||
func ec2Connection(ctx context.Context, region string, awsCreds aws.CredentialsProvider) ec2ClientInterface { | ||
// using aws config to read the build in credentials to set up the ec2 client. | ||
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region), config.WithCredentialsProvider(awsCreds)) | ||
if err != nil { | ||
log.Warnf("unable to get aws configurations: %s", err) | ||
return nil | ||
} | ||
log.Debug("aws config loaded successfully") | ||
return ec2.NewFromConfig(cfg) | ||
} | ||
|
||
func getTagsWithCreds(ctx context.Context, instanceIdentity *EC2Identity, connection ec2ClientInterface) ([]string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good find, I've added the check and updated the test |
||
if connection == nil { | ||
return nil, fmt.Errorf("ec2 client is not set, unable to get tags") | ||
} | ||
// We want to use 'ec2_metadata_timeout' here instead of current context. 'ctx' comes from the agent main and will | ||
// only be canceled if the agent is stopped. The default timeout for the AWS SDK is 1 minutes (20s timeout with | ||
// 3 retries). Since we call getTagsWithCreds twice in a row, it can be a 2 minutes latency. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Each section from every release note are combined when the | ||
# CHANGELOG.rst is rendered. So the text needs to be worded so that | ||
# it does not depend on any information only available in another | ||
# section. This may mean repeating some details, but each section | ||
# must be readable independently of the other. | ||
# | ||
# Each section note must be formatted as reStructuredText. | ||
--- | ||
enhancements: | ||
- | | ||
Modifies the initialization of the Amazon EC2 client to use the AWS configuration package. | ||
This change enables support for built-in credentials, such as AWS IAM Roles for Service Accounts (IRSA). |
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.
❓ maybe add a log such as:
log.Info("AWS configurations successfully fetched")
What do you think ?
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.
@louis-cqrl I believe that will populate unnecessary log output. How about log.Debug() instead.