Skip to content

Commit

Permalink
fix: don't allow parallel delete errors to fail writing new keys
Browse files Browse the repository at this point in the history
  • Loading branch information
pckilgore committed Oct 10, 2021
1 parent 5f78c33 commit 9bfdae4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
6 changes: 2 additions & 4 deletions aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ func CreateKey(ctx context.Context, client *iam.Client) new_key {
}
}

func DeleteKey(ctx context.Context, client *iam.Client, key_id string) {
func DeleteKey(ctx context.Context, client *iam.Client, key_id string) error {
input := &iam.DeleteAccessKeyInput{AccessKeyId: &key_id}
_, err := client.DeleteAccessKey(ctx, input)

if err != nil {
Boom("Could not delete old key", err)
}
return err
}
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func Boom(msg string, err error) {
fmt.Fprintf(
os.Stderr,
"\n%s\n%+v\n%s\n\n",
"\n%s\n%+v\n\n%s\n",
msg,
err,
"Stopping for manual intervention",
Expand Down
16 changes: 11 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,23 @@ func main() {
new_key := CreateKey(ctx, client)

// Parallelize deletion of current credentials
del_op := make(chan bool)
del_op := make(chan error)
go func() {
DeleteKey(ctx, client, string(key_match))
del_op <- true
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
<-del_op
// 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)
}
}

0 comments on commit 9bfdae4

Please sign in to comment.