This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
forked from stefansundin/aws-rotate-key
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
262 lines (234 loc) · 9.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os"
"os/user"
"regexp"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/sts"
)
const version = "1.0.7"
func main() {
var yesFlag bool
var mfaFlag bool
var profileFlag string
var versionFlag bool
var deleteFlag bool
flag.BoolVar(&yesFlag, "y", false, `Automatic "yes" to prompts.`)
flag.BoolVar(&mfaFlag, "mfa", false, "Use MFA.")
flag.BoolVar(&deleteFlag, "d", false, "Delete old key without deactivation.")
flag.StringVar(&profileFlag, "profile", "default", "The profile to use.")
flag.BoolVar(&versionFlag, "version", false, "Print version number")
flag.Parse()
if versionFlag {
fmt.Println(version)
os.Exit(0)
}
// Get credentials
credentialsPath := os.Getenv("AWS_SHARED_CREDENTIALS_FILE")
if len(credentialsPath) == 0 {
usr, err := user.Current()
if err != nil {
fmt.Println("Error: Could not locate your home directory. Please set the AWS_SHARED_CREDENTIALS_FILE environment variable.")
os.Exit(1)
}
credentialsPath = fmt.Sprintf("%s/.aws/credentials", usr.HomeDir)
}
credentialsProvider := credentials.NewSharedCredentials(credentialsPath, profileFlag)
creds, err := credentialsProvider.Get()
check(err)
fmt.Printf("Using access key %s from profile \"%s\".\n", creds.AccessKeyID, profileFlag)
// Read credentials file
bytes, err := ioutil.ReadFile(credentialsPath)
check(err)
credentialsText := string(bytes)
// Check if we can find the credentials in the file
// It's better to detect a malformed file now than after we have created the new key
re_aws_access_key_id := regexp.MustCompile(fmt.Sprintf(`(?m)^aws_access_key_id *= *%s`, regexp.QuoteMeta(creds.AccessKeyID)))
re_aws_secret_access_key := regexp.MustCompile(fmt.Sprintf(`(?m)^aws_secret_access_key *= *%s`, regexp.QuoteMeta(creds.SecretAccessKey)))
if !re_aws_access_key_id.MatchString(credentialsText) || !re_aws_secret_access_key.MatchString(credentialsText) {
fmt.Println()
fmt.Printf("Unable to find your credentials in %s.\n", credentialsPath)
fmt.Println("Please make sure your file is formatted like the following:")
fmt.Println()
fmt.Printf("aws_access_key_id=%s\n", creds.AccessKeyID)
fmt.Println("aws_secret_access_key=...")
fmt.Println()
os.Exit(1)
}
// Create session
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Profile: profileFlag,
}))
// sts get-caller-identity
stsClient := sts.New(sess)
respGetCallerIdentity, err := stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
fmt.Println("Error getting caller identity. Is the key disabled?")
fmt.Println()
check(err)
}
fmt.Printf("Your user ARN is: %s\n", *respGetCallerIdentity.Arn)
// mfa
if mfaFlag {
// We have to pick the last element in case the user has a path associated with it (versus naively using [1]).
// It's probably very rare that people use paths but we should support it.
userArnSplit := strings.Split(*respGetCallerIdentity.Arn, "/")
username := userArnSplit[len(userArnSplit)-1]
iamClient := iam.New(sess)
respMFADevices, err := iamClient.ListMFADevices(&iam.ListMFADevicesInput{
UserName: aws.String(username),
})
check(err)
if len(respMFADevices.MFADevices) == 0 {
fmt.Println("You do not have an MFA device associated with your user. Aborting.")
os.Exit(1)
}
fmt.Printf("Your MFA ARN is: %s\n\n", *respMFADevices.MFADevices[0].SerialNumber)
// I have no idea how much work it would be to support U2F
serialNumberSplit := strings.Split(*respMFADevices.MFADevices[0].SerialNumber, ":")
if strings.HasPrefix(serialNumberSplit[5], "u2f/") {
fmt.Println("Sorry, U2F MFAs are not supported. Aborting.")
os.Exit(1)
}
// Prompt for the code
var code string
fmt.Print("MFA token code: ")
_, err = fmt.Scanln(&code)
check(err)
// Get the new credentials
respSessionToken, err := stsClient.GetSessionToken(&sts.GetSessionTokenInput{
DurationSeconds: aws.Int64(900), // valid for 15 minutes (the minimum)
SerialNumber: respMFADevices.MFADevices[0].SerialNumber,
TokenCode: aws.String(code),
})
check(err)
// Create a new session that use the new credentials
c := respSessionToken.Credentials
mfaCreds := credentials.NewStaticCredentials(*c.AccessKeyId, *c.SecretAccessKey, *c.SessionToken)
sess = session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Profile: profileFlag,
Config: aws.Config{Credentials: mfaCreds},
}))
}
fmt.Println()
// iam list-access-keys
// If the UserName field is not specified, the UserName is determined implicitly based on the AWS access key ID used to sign the request.
iamClient := iam.New(sess)
respListAccessKeys, err := iamClient.ListAccessKeys(&iam.ListAccessKeysInput{})
check(err)
// Print key information
fmt.Printf("You have %d access key%v associated with your user:\n", len(respListAccessKeys.AccessKeyMetadata), pluralize(len(respListAccessKeys.AccessKeyMetadata)))
for _, key := range respListAccessKeys.AccessKeyMetadata {
respAccessKeyLastUsed, err2 := iamClient.GetAccessKeyLastUsed(&iam.GetAccessKeyLastUsedInput{
AccessKeyId: key.AccessKeyId,
})
if err2 != nil {
fmt.Printf("- %s (%s, created %s)\n", *key.AccessKeyId, *key.Status, key.CreateDate)
} else if respAccessKeyLastUsed.AccessKeyLastUsed.LastUsedDate == nil {
fmt.Printf("- %s (%s, created %s, never used)\n", *key.AccessKeyId, *key.Status, key.CreateDate)
} else {
fmt.Printf("- %s (%s, created %s, last used %s for service %s in %s)\n", *key.AccessKeyId, *key.Status, key.CreateDate, respAccessKeyLastUsed.AccessKeyLastUsed.LastUsedDate, *respAccessKeyLastUsed.AccessKeyLastUsed.ServiceName, *respAccessKeyLastUsed.AccessKeyLastUsed.Region)
}
}
fmt.Println()
if len(respListAccessKeys.AccessKeyMetadata) == 2 {
keyIndex := 0
if *respListAccessKeys.AccessKeyMetadata[0].AccessKeyId == creds.AccessKeyID {
keyIndex = 1
}
if yesFlag == false {
fmt.Println("You have two access keys, which is the max number of access keys.")
fmt.Printf("Do you want to delete %s and create a new key? [yN] ", *respListAccessKeys.AccessKeyMetadata[keyIndex].AccessKeyId)
if *respListAccessKeys.AccessKeyMetadata[keyIndex].Status == "Active" {
fmt.Printf("\nWARNING: This key is currently Active! ")
}
reader := bufio.NewReader(os.Stdin)
yn, err2 := reader.ReadString('\n')
check(err2)
if yn[0] != 'y' && yn[0] != 'Y' {
os.Exit(1)
}
}
_, err2 := iamClient.DeleteAccessKey(&iam.DeleteAccessKeyInput{
AccessKeyId: respListAccessKeys.AccessKeyMetadata[keyIndex].AccessKeyId,
})
check(err2)
fmt.Printf("Deleted access key %s.\n", *respListAccessKeys.AccessKeyMetadata[keyIndex].AccessKeyId)
} else if yesFlag == false {
cleanupAction := "deactivate"
if deleteFlag {
cleanupAction = "delete"
}
fmt.Printf("Do you want to create a new key and %s %s? [yN] ", cleanupAction, *respListAccessKeys.AccessKeyMetadata[0].AccessKeyId)
reader := bufio.NewReader(os.Stdin)
yn, err2 := reader.ReadString('\n')
check(err2)
if yn[0] != 'y' && yn[0] != 'Y' {
os.Exit(1)
}
}
// Create the new access key
// If you do not specify a user name, IAM determines the user name implicitly based on the AWS access key ID signing the request.
respCreateAccessKey, err := iamClient.CreateAccessKey(&iam.CreateAccessKeyInput{})
check(err)
fmt.Printf("Created access key %s.\n", *respCreateAccessKey.AccessKey.AccessKeyId)
// Replace key pair in credentials file
// This search & replace does not limit itself to the specified profile, which may be useful if the user is using the same key in multiple profiles
credentialsText = re_aws_access_key_id.ReplaceAllString(credentialsText, `aws_access_key_id=`+*respCreateAccessKey.AccessKey.AccessKeyId)
credentialsText = re_aws_secret_access_key.ReplaceAllString(credentialsText, `aws_secret_access_key=`+*respCreateAccessKey.AccessKey.SecretAccessKey)
// Verify that the regexp actually replaced something
if !strings.Contains(credentialsText, *respCreateAccessKey.AccessKey.AccessKeyId) || !strings.Contains(credentialsText, *respCreateAccessKey.AccessKey.SecretAccessKey) {
fmt.Println("Failed to replace old access key. Aborting.")
fmt.Printf("Please verify that the file %s is formatted correctly.\n", credentialsPath)
// Delete the key we created
_, err2 := iamClient.DeleteAccessKey(&iam.DeleteAccessKeyInput{
AccessKeyId: respCreateAccessKey.AccessKey.AccessKeyId,
})
check(err2)
fmt.Printf("Deleted access key %s.\n", *respCreateAccessKey.AccessKey.AccessKeyId)
os.Exit(1)
}
// Write new file
err = ioutil.WriteFile(credentialsPath, []byte(credentialsText), 0600)
check(err)
fmt.Printf("Wrote new key pair to %s\n", credentialsPath)
// Delete the old key if flag is set, otherwise deactivate it
if deleteFlag {
_, err := iamClient.DeleteAccessKey(&iam.DeleteAccessKeyInput{
AccessKeyId: &creds.AccessKeyID,
})
check(err)
fmt.Printf("Deleted old access key %s.\n", creds.AccessKeyID)
} else {
_, err = iamClient.UpdateAccessKey(&iam.UpdateAccessKeyInput{
AccessKeyId: &creds.AccessKeyID,
Status: aws.String("Inactive"),
})
check(err)
fmt.Printf("Deactivated old access key %s.\n", creds.AccessKeyID)
fmt.Println("Please make sure this key is not used elsewhere.")
}
fmt.Println("Please note that it may take a minute for your new access key to propagate in the AWS control plane.")
}
func pluralize(n int) string {
if n == 1 {
return ""
}
return "s"
}
func check(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}