Skip to content

Commit 405d480

Browse files
committed
Clean AWS_ environment vars between evals.
1 parent ac01f41 commit 405d480

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ This is a small utility that makes it easier to use the `aws sts assume-role` co
22

33
## Installation
44

5+
On OS X, the best way to get it is to use homebrew:
6+
7+
```bash
8+
brew install remind101/formulae/assume-role
9+
```
10+
11+
If you have a working Go 1.6 environment:
12+
513
```bash
614
$ go get -u github.com/remind101/assume-role
715
```

main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,31 @@ func main() {
3838
must(fmt.Errorf("%s not in ~/.aws/roles", role))
3939
}
4040

41+
if os.Getenv("ASSUMED_ROLE") != "" {
42+
// Clear out any previously set AWS_ environment variables so
43+
// they aren't used with the assumeRole command.
44+
cleanEnv()
45+
}
46+
4147
creds, err := assumeRole(roleConfig.Role, roleConfig.MFA)
4248
must(err)
4349

4450
if len(args) == 0 {
45-
printCredentials(creds)
51+
printCredentials(role, creds)
4652
return
4753
}
4854

4955
err = execWithCredentials(args, creds)
5056
must(err)
5157
}
5258

59+
func cleanEnv() {
60+
os.Unsetenv("AWS_ACCESS_KEY_ID")
61+
os.Unsetenv("AWS_SECRET_ACCESS_KEY")
62+
os.Unsetenv("AWS_SESSION_TOKEN")
63+
os.Unsetenv("AWS_SECURITY_TOKEN")
64+
}
65+
5366
func execWithCredentials(argv []string, creds *credentials) error {
5467
argv0, err := exec.LookPath(argv[0])
5568
if err != nil {
@@ -73,11 +86,12 @@ type credentials struct {
7386

7487
// printCredentials prints the credentials in a way that can easily be sourced
7588
// with bash.
76-
func printCredentials(creds *credentials) {
89+
func printCredentials(role string, creds *credentials) {
7790
fmt.Printf("export AWS_ACCESS_KEY_ID=\"%s\"\n", creds.AccessKeyID)
7891
fmt.Printf("export AWS_SECRET_ACCESS_KEY=\"%s\"\n", creds.SecretAccessKey)
7992
fmt.Printf("export AWS_SESSION_TOKEN=\"%s\"\n", creds.SessionToken)
8093
fmt.Printf("export AWS_SECURITY_TOKEN=\"%s\"\n", creds.SessionToken)
94+
fmt.Printf("export ASSUMED_ROLE=\"%s\"\n", role)
8195
fmt.Printf("# Run this to configure your shell:\n")
8296
fmt.Printf("# eval $(%s)\n", strings.Join(os.Args, " "))
8397
}

0 commit comments

Comments
 (0)