-
Notifications
You must be signed in to change notification settings - Fork 680
[do not merge] feat: support account id in imds / new profile configs #3067
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
lucix-aws
wants to merge
2
commits into
main
Choose a base branch
from
feat-imdsaccountid
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"id": "3a4c3951-c250-4554-a64b-14ce2dddf6ef", | ||
"type": "feature", | ||
"description": "Support account ID retrieval in IMDS credentials provider, and support new IMDS profile name config:\n\n1. environment: `AWS_EC2_INSTANCE_PROFILE_NAME`\n2. shared config: `ec2_instance_profile_name`", | ||
"modules": [ | ||
"config", | ||
"credentials" | ||
] | ||
} |
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
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
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
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ import ( | |
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds" | ||
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds" | ||
"github.com/aws/aws-sdk-go-v2/internal/awstesting" | ||
"github.com/aws/aws-sdk-go-v2/service/sso" | ||
|
@@ -82,9 +83,15 @@ func setupCredentialsEndpoints() (aws.EndpointResolverWithOptions, func()) { | |
|
||
ec2MetadataServer := httptest.NewServer(http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == "/latest/meta-data/iam/security-credentials/RoleName" { | ||
if r.URL.Path == "/latest/meta-data/iam/security-credentials-extended/RoleName" { | ||
w.Write([]byte(ec2MetadataResponse)) | ||
} else if r.URL.Path == "/latest/meta-data/iam/security-credentials/" { | ||
} else if r.URL.Path == "/latest/meta-data/iam/security-credentials-extended/LoadOptions" { | ||
w.Write([]byte(ec2MetadataResponseLoadOptions)) | ||
} else if r.URL.Path == "/latest/meta-data/iam/security-credentials-extended/EnvCfg" { | ||
w.Write([]byte(ec2MetadataResponseEnvCfg)) | ||
} else if r.URL.Path == "/latest/meta-data/iam/security-credentials-extended/SharedCfg" { | ||
w.Write([]byte(ec2MetadataResponseSharedCfg)) | ||
} else if r.URL.Path == "/latest/meta-data/iam/security-credentials-extended/" { | ||
w.Write([]byte("RoleName")) | ||
} else if r.URL.Path == "/latest/api/token" { | ||
header := w.Header() | ||
|
@@ -750,6 +757,103 @@ func TestResolveCredentialsEcsContainer(t *testing.T) { | |
|
||
} | ||
|
||
func TestResolveCredentialsEC2RoleCreds(t *testing.T) { | ||
testCases := map[string]struct { | ||
expectedAccessKey string | ||
expectedSecretKey string | ||
envVar map[string]string | ||
configFile string | ||
configProfile string | ||
loadOptions func(*LoadOptions) error | ||
}{ | ||
"no config whatsoever": { | ||
expectedAccessKey: "ec2-access-key", | ||
expectedSecretKey: "ec2-secret-key", | ||
envVar: map[string]string{}, | ||
configFile: "", | ||
}, | ||
"env cfg": { | ||
expectedAccessKey: "ec2-access-key-envcfg", | ||
expectedSecretKey: "ec2-secret-key-envcfg", | ||
envVar: map[string]string{ | ||
"AWS_EC2_INSTANCE_PROFILE_NAME": "EnvCfg", | ||
}, | ||
configFile: "", | ||
}, | ||
"shared cfg": { | ||
expectedAccessKey: "ec2-access-key-sharedcfg", | ||
expectedSecretKey: "ec2-secret-key-sharedcfg", | ||
envVar: map[string]string{}, | ||
configFile: filepath.Join("testdata", "config_source_shared"), | ||
configProfile: "ec2metadata-profilename", | ||
}, | ||
"loadopts + env cfg + shared cfg": { | ||
expectedAccessKey: "ec2-access-key-loadopts", | ||
expectedSecretKey: "ec2-secret-key-loadopts", | ||
envVar: map[string]string{ | ||
"AWS_EC2_INSTANCE_PROFILE_NAME": "EnvCfg", | ||
}, | ||
configFile: filepath.Join("testdata", "config_source_shared"), | ||
configProfile: "ec2metadata-profilename", | ||
loadOptions: WithEC2RoleCredentialOptions(func(o *ec2rolecreds.Options) { | ||
o.ProfileName = "LoadOptions" | ||
}), | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
endpointResolver, cleanupFn := setupCredentialsEndpoints() | ||
defer cleanupFn() | ||
|
||
// setupCredentialsEndpoints sets this above and then we hold onto | ||
// it for this test | ||
ec2MetadataURL := os.Getenv("AWS_EC2_METADATA_SERVICE_ENDPOINT") | ||
|
||
restoreEnv := awstesting.StashEnv() | ||
defer awstesting.PopEnv(restoreEnv) | ||
|
||
os.Setenv("AWS_EC2_METADATA_SERVICE_ENDPOINT", ec2MetadataURL) | ||
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. nit: use |
||
for k, v := range tc.envVar { | ||
os.Setenv(k, v) | ||
} | ||
var sharedConfigFiles []string | ||
if tc.configFile != "" { | ||
sharedConfigFiles = append(sharedConfigFiles, tc.configFile) | ||
} | ||
opts := []func(*LoadOptions) error{ | ||
WithEndpointResolverWithOptions(endpointResolver), | ||
WithRetryer(func() aws.Retryer { return aws.NopRetryer{} }), | ||
WithSharedConfigFiles(sharedConfigFiles), | ||
WithSharedCredentialsFiles([]string{}), | ||
} | ||
if len(tc.configProfile) != 0 { | ||
opts = append(opts, WithSharedConfigProfile(tc.configProfile)) | ||
} | ||
|
||
if tc.loadOptions != nil { | ||
opts = append(opts, tc.loadOptions) | ||
} | ||
|
||
cfg, err := LoadDefaultConfig(context.TODO(), opts...) | ||
if err != nil { | ||
t.Fatalf("could not load config: %s", err) | ||
} | ||
actual, err := cfg.Credentials.Retrieve(context.TODO()) | ||
if err != nil { | ||
t.Fatalf("could not retrieve credentials: %s", err) | ||
} | ||
if actual.AccessKeyID != tc.expectedAccessKey { | ||
t.Errorf("expected access key to be %s, got %s", tc.expectedAccessKey, actual.AccessKeyID) | ||
} | ||
if actual.SecretAccessKey != tc.expectedSecretKey { | ||
t.Errorf("expected secret key to be %s, got %s", tc.expectedSecretKey, actual.SecretAccessKey) | ||
} | ||
}) | ||
} | ||
|
||
} | ||
|
||
type stubErrorClient struct { | ||
err error | ||
} | ||
|
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
Oops, something went wrong.
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.
It was discussed to err on this rather than leaving it empty. My main concern would be that someone would want to set this to something on their shell, like
export AWS_EC2_INSTANCE_PROFILE_NAME=$(some_process_that_returns_a_value)
, and that something returns an empty string, then they would make a request to the base URL rather than to the profile they were trying to useThere 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.
This was addressed but not here -- it actually uses
Lookupenv
to check for explicit empty in the main load routine.