fix: restore AWS_PROFILE env var correctly when it was previously unset#1046
Merged
Conversation
606be7b to
f8c1102
Compare
Signed-off-by: Jim Robinson <[email protected]>
f8c1102 to
95b571e
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix: correctly restore AWS_PROFILE when it was not previously set
Problem
When using
ref+tfstates3withaws_profilein a helmfile values file, theReadTFStatefunction temporarily setsAWS_PROFILEto the specified profile, then attempts to restore the previous value on exit.The bug is in the restore logic:
os.Getenvreturns""for both an unset variable and a variable explicitly set to an empty string. The defer always callsos.Setenv, which means after the tfstate lookup completes,AWS_PROFILEis left set to an empty string rather than being unset.Impact
This affects environments that rely on the AWS default credential chain — such as EC2 instance profiles or Kubernetes pods with IAM roles — where
AWS_PROFILEis intentionally not set. After the tfstate lookup, any subsequent AWS calls (e.g.aws eks get-tokenin a kubeconfig exec credential plugin) fail with:Fix
Use
os.LookupEnvinstead ofos.Getenvto distinguish between "not set" and "set to empty string", and callos.Unsetenvwhen the variable was not originally set: