fix: AWS credential fallback robust against AWS_PROFILE set to missing profile#1110
Conversation
|
@Kellen275 please fix DCO |
|
@Kellen275 should I merge this? |
There was a problem hiding this comment.
Pull request overview
Improves vals AWS config/session creation so that when an explicit profile is provided but the profile is missing, it can still fall back to the AWS SDK v2 default credential chain even if AWS_PROFILE / AWS_DEFAULT_PROFILE are set to the same missing profile.
Changes:
- Adds a missing-profile fallback path that temporarily suppresses
AWS_PROFILE/AWS_DEFAULT_PROFILEunder a mutex and retries config loading without an explicit profile. - Keeps explicit
profiletaking precedence, withFORCE_AWS_PROFILE=trueas the secondary profile-selection mechanism.
| os.Unsetenv("AWS_PROFILE") | ||
| } | ||
| if savedDefaultProfile != "" { | ||
| os.Unsetenv("AWS_DEFAULT_PROFILE") | ||
| } | ||
|
|
||
| // Restore environment variables after the recursive call completes | ||
| defer func() { | ||
| if savedProfile != "" { | ||
| os.Setenv("AWS_PROFILE", savedProfile) | ||
| } | ||
| if savedDefaultProfile != "" { | ||
| os.Setenv("AWS_DEFAULT_PROFILE", savedDefaultProfile) |
There was a problem hiding this comment.
errcheck is enabled in this repo, and the calls to os.Unsetenv/os.Setenv here ignore the returned error values. This will be reported by golangci-lint (errcheck) and fail CI. Please either handle and return these errors for the initial Unsetenv calls, and use _ = os.Setenv/_ = os.Unsetenv (or equivalent) in the deferred restore where you can’t return an error, mirroring patterns used elsewhere (e.g. tfstate provider).
| os.Unsetenv("AWS_PROFILE") | |
| } | |
| if savedDefaultProfile != "" { | |
| os.Unsetenv("AWS_DEFAULT_PROFILE") | |
| } | |
| // Restore environment variables after the recursive call completes | |
| defer func() { | |
| if savedProfile != "" { | |
| os.Setenv("AWS_PROFILE", savedProfile) | |
| } | |
| if savedDefaultProfile != "" { | |
| os.Setenv("AWS_DEFAULT_PROFILE", savedDefaultProfile) | |
| if err := os.Unsetenv("AWS_PROFILE"); err != nil { | |
| profileEnvMu.Unlock() | |
| return aws.Config{}, err | |
| } | |
| } | |
| if savedDefaultProfile != "" { | |
| if err := os.Unsetenv("AWS_DEFAULT_PROFILE"); err != nil { | |
| if savedProfile != "" { | |
| _ = os.Setenv("AWS_PROFILE", savedProfile) | |
| } | |
| profileEnvMu.Unlock() | |
| return aws.Config{}, err | |
| } | |
| } | |
| // Restore environment variables after the recursive call completes | |
| defer func() { | |
| if savedProfile != "" { | |
| _ = os.Setenv("AWS_PROFILE", savedProfile) | |
| } | |
| if savedDefaultProfile != "" { | |
| _ = os.Setenv("AWS_DEFAULT_PROFILE", savedDefaultProfile) |
4c32724 to
cd4e79c
Compare
Signed-off-by: Kellen Sappington <kellen275@gmail.com>
cd4e79c to
e80a840
Compare
…aws-profile-fallback-no-config
|
The comment from @copilot should be addressed now |
45481dd
into
helmfile:fix-aws-profile-fallback-no-config
…g profile (#1104) * fix: AWS credential fallback should not require default profile in config When a specified AWS profile is not found, the fallback was using WithSharedConfigProfile("default") which still requires a [default] section in ~/.aws/config. EC2 users with no config file at all would panic with "failed to get shared config profile, default". Remove the explicit profile override in the fallback so the SDK uses its default credential chain (env vars, shared credentials, EC2 IMDS) without requiring any profile to exist in config files. Fixes #1094 Signed-off-by: yxxhero <aiopsclub@163.com> * fix: handle AWS_PROFILE env var in profile-not-found fallback path Agent-Logs-Url: https://github.com/helmfile/vals/sessions/58c724eb-18ad-4e34-b1ed-9eb28fccfd2a Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com> * fix: address lint errors in loadDefaultConfigWithoutProfileEnv Agent-Logs-Url: https://github.com/helmfile/vals/sessions/00c2fd7d-20cf-42d6-a319-e391f15517e2 Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com> * fix commit history to please DCO (#1110) Signed-off-by: Kellen Sappington <kellen275@gmail.com> --------- Signed-off-by: yxxhero <aiopsclub@163.com> Signed-off-by: Kellen Sappington <kellen275@gmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Kellen275 <59538148+Kellen275@users.noreply.github.com>
The diff is perhaps more easily viewed when compared with the
v0.43.7release (before we began addressing #1094)I have intentionally kept the
session_test.gofile identical to that of #1104 as well as theprofileEnvMudefinition just to minimize changes.