@@ -38,18 +38,31 @@ func main() {
38
38
must (fmt .Errorf ("%s not in ~/.aws/roles" , role ))
39
39
}
40
40
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
+
41
47
creds , err := assumeRole (roleConfig .Role , roleConfig .MFA )
42
48
must (err )
43
49
44
50
if len (args ) == 0 {
45
- printCredentials (creds )
51
+ printCredentials (role , creds )
46
52
return
47
53
}
48
54
49
55
err = execWithCredentials (args , creds )
50
56
must (err )
51
57
}
52
58
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
+
53
66
func execWithCredentials (argv []string , creds * credentials ) error {
54
67
argv0 , err := exec .LookPath (argv [0 ])
55
68
if err != nil {
@@ -73,11 +86,12 @@ type credentials struct {
73
86
74
87
// printCredentials prints the credentials in a way that can easily be sourced
75
88
// with bash.
76
- func printCredentials (creds * credentials ) {
89
+ func printCredentials (role string , creds * credentials ) {
77
90
fmt .Printf ("export AWS_ACCESS_KEY_ID=\" %s\" \n " , creds .AccessKeyID )
78
91
fmt .Printf ("export AWS_SECRET_ACCESS_KEY=\" %s\" \n " , creds .SecretAccessKey )
79
92
fmt .Printf ("export AWS_SESSION_TOKEN=\" %s\" \n " , creds .SessionToken )
80
93
fmt .Printf ("export AWS_SECURITY_TOKEN=\" %s\" \n " , creds .SessionToken )
94
+ fmt .Printf ("export ASSUMED_ROLE=\" %s\" \n " , role )
81
95
fmt .Printf ("# Run this to configure your shell:\n " )
82
96
fmt .Printf ("# eval $(%s)\n " , strings .Join (os .Args , " " ))
83
97
}
0 commit comments