Skip to content

Commit

Permalink
Add RetrieveWithoutSessionToken() to handle NoSession
Browse files Browse the repository at this point in the history
  • Loading branch information
lox committed Sep 1, 2017
1 parent 50263f5 commit c867a32
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions vault/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,48 @@ func (p *VaultProvider) Retrieve() (credentials.Value, error) {
return value, nil
}

// RetrieveWithoutSessionToken returns credentials that are either the master credentials or
// a session created with AssumeRole. This allows for usecases where a token created with AssumeRole
// wouldn't work.
func (p *VaultProvider) RetrieveWithoutSessionToken() (credentials.Value, error) {
log.Println("Skipping session token and using master credentials directly")

creds, err := p.getMasterCreds()
if err != nil {
return credentials.Value{}, err
}

if role, ok := p.profiles[p.profile]["role_arn"]; ok {
session, err := p.assumeRole(creds, role)
if err != nil {
return credentials.Value{}, err
}

log.Printf("Using role ****************%s, expires in %s",
(*session.AccessKeyId)[len(*session.AccessKeyId)-4:],
session.Expiration.Sub(time.Now()).String())

window := p.ExpiryWindow
if window == 0 {
window = time.Minute * 5
}

p.SetExpiration(*session.Expiration, window)
p.expires = *session.Expiration

value := credentials.Value{
AccessKeyID: *session.AccessKeyId,
SecretAccessKey: *session.SecretAccessKey,
SessionToken: *session.SessionToken,
}

return value, nil
}

// no role, exposes master credentials which don't expire
return creds, nil
}

func (p *VaultProvider) getMasterCreds() (credentials.Value, error) {
if p.MasterCreds != nil {
return *p.MasterCreds, nil
Expand Down

0 comments on commit c867a32

Please sign in to comment.