Skip to content

Commit

Permalink
Merge pull request #17 from moritzheiber/master
Browse files Browse the repository at this point in the history
Use MFA session for key rotation
  • Loading branch information
sam701 authored Feb 9, 2017
2 parents e6e4761 + 61e85b0 commit 45a8275
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
12 changes: 5 additions & 7 deletions assume.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,21 @@ func assumeRole(account, role string) {

err := tryToAssumeRole(account, role)
if err != nil {
needToBeRotatedChan := make(chan bool)
go isNeedRotateKey(needToBeRotatedChan)

getMainAccountMfaSessionToken()
err = tryToAssumeRole(account, role)
if err != nil {
log.Fatalln(err)
}

if <-needToBeRotatedChan {
rotateMainAccountKey()
if needRotateKey() {
rotateMainAccountKey(iam.New(sess.New(config.Current.Profiles.MainAccountMfaSession)))
}
}
}

func isNeedRotateKey(needToBeRotated chan<- bool) {
session := sess.New(config.Current.Profiles.MainAccount)
func needRotateKey() bool {
session := sess.New(config.Current.Profiles.MainAccountMfaSession)
cl := iam.New(session)

keyId := cred.GetMainAccountKeyId(config.Current.Profiles.MainAccount)
Expand All @@ -85,7 +83,7 @@ func isNeedRotateKey(needToBeRotated chan<- bool) {
log.Fatalln("Cannot get creation time for key", keyId)
}

needToBeRotated <- int(time.Now().Sub(creationTime).Minutes()) >= config.Current.KeyRotationIntervalMinutes
return int(time.Now().Sub(creationTime).Minutes()) >= config.Current.KeyRotationIntervalMinutes
}

func adjustAccountName(account string) string {
Expand Down
6 changes: 3 additions & 3 deletions rotate_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
)

func rotateMainAccountKeyAction(*cli.Context) error {
rotateMainAccountKey()
client := iam.New(sess.New(config.Current.Profiles.MainAccount))
rotateMainAccountKey(client)
return nil
}

func rotateMainAccountKey() {
client := iam.New(sess.New(config.Current.Profiles.MainAccount))
func rotateMainAccountKey(client *iam.IAM) {
key, err := client.CreateAccessKey(&iam.CreateAccessKeyInput{
UserName: aws.String(getUserName()),
})
Expand Down

0 comments on commit 45a8275

Please sign in to comment.