-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (52 loc) · 1.96 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
package main
import (
"context"
"fmt"
"time"
)
func main() {
// Timeout SDK interactions in go routines after ten seconds.
// This should take less than a second, so something is horribly wrong.
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
// Parallelize credential chain + credentials file load operations
creds_file_load := make(chan CredentialsFile)
cred_load := make(chan Credentials)
go func() {
creds_file_load <- GetCredentialsFile()
}()
go func() {
cred_load <- EnvironmentCredentials(ctx)
}()
creds_file := <-creds_file_load
creds := <-cred_load
client := Client(creds.Config)
// Identify correct credentials to rotate
key_match := AccessKeyLocator.MatchKey(creds.AccessKeyID, creds_file.content)
secret_match := SecretKeyLocator.MatchKey(creds.SecretAccessKey, creds_file.content)
if len(secret_match) == 0 || len(key_match) == 0 {
tmpl := "Cannot find key_id=%s in file=%s)"
msg := fmt.Sprintf(tmpl, creds.AccessKeyID, creds_file.filepath)
Boom("Aborting Intentionally:", fmt.Errorf(msg))
}
// Create new Credentials
new_key := CreateKey(ctx, client)
// Parallelize deletion of current credentials
del_op := make(chan error)
go func() {
del_op <- DeleteKey(ctx, client, string(key_match))
}()
// Write out new credentials
creds_file.content = AccessKeyLocator.Replace(key_match, new_key.key, creds_file.content)
creds_file.content = SecretKeyLocator.Replace(secret_match, new_key.secret, creds_file.content)
WriteCredentialsFile(creds_file)
// Wait for delete routine to complete and handle its errors, which we would
// not otherwise want interupting writing new credentials to the local system.
del_err := <-del_op
if del_err != nil {
note := `Error! Cannot delete old IAM keys from server, however, your new
keys were successfully saved to your credentials file. You might have to remove
old keys manually via the console or CLI before rollit will work again.`
Boom(note, del_err)
}
}