forked from 99designs/aws-vault
/
session.go
135 lines (109 loc) · 3.47 KB
/
session.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
package vault
import (
"log"
"time"
"github.com/99designs/aws-vault/Godeps/_workspace/src/github.com/aws/aws-sdk-go/aws"
"github.com/99designs/aws-vault/Godeps/_workspace/src/github.com/aws/aws-sdk-go/aws/credentials"
"github.com/99designs/aws-vault/Godeps/_workspace/src/github.com/aws/aws-sdk-go/service/sts"
"github.com/99designs/aws-vault/keyring"
)
type TokenAgent interface {
GetToken(serial string) (string, error)
}
type SessionConfig struct {
Profile *Profile
TokenAgent TokenAgent
Duration time.Duration
Credentials *Credentials
Refresh bool
}
type SessionProvider struct {
}
func (sp *SessionProvider) Session(conf SessionConfig) (SessionCredentials, error) {
awsConf := aws.DefaultConfig
if conf.Credentials != nil {
awsConf = awsConf.WithCredentials(credentials.NewStaticCredentials(
conf.Credentials.AccessKeyId, conf.Credentials.SecretKey, "",
))
}
svc := sts.New(awsConf)
var serialNumber, token string
if conf.Profile.MFASerial != "" && conf.TokenAgent != nil {
var err error
if token, err = conf.TokenAgent.GetToken(conf.Profile.MFASerial); err != nil {
return SessionCredentials{}, err
}
serialNumber = conf.Profile.MFASerial
}
// handle assume role
if conf.Profile.RoleARN != "" {
input := &sts.AssumeRoleInput{
RoleARN: aws.String(conf.Profile.RoleARN),
RoleSessionName: aws.String(conf.Profile.Name),
DurationSeconds: aws.Int64(int64(conf.Duration.Seconds())),
SerialNumber: aws.String(serialNumber),
TokenCode: aws.String(token),
}
if token != "" {
log.Printf("assuming role %s with mfa %s", conf.Profile.RoleARN, serialNumber)
} else {
log.Printf("assuming role %s", conf.Profile.RoleARN)
}
resp, err := svc.AssumeRole(input)
if err != nil {
log.Printf("%#v", err)
return SessionCredentials{}, err
}
return SessionCredentials{resp.Credentials}, nil
}
// otherwise get a session token
input := &sts.GetSessionTokenInput{
DurationSeconds: aws.Int64(int64(conf.Duration.Seconds())),
SerialNumber: aws.String(serialNumber),
TokenCode: aws.String(token),
}
if token != "" {
log.Printf("getting session token with mfa %s", serialNumber)
} else {
log.Printf("getting session token")
}
resp, err := svc.GetSessionToken(input)
if err != nil {
return SessionCredentials{}, err
}
return SessionCredentials{resp.Credentials}, nil
}
type KeyringSessionProvider struct {
SessionProvider
Keyring keyring.Keyring
CredsFunc func() (Credentials, error)
}
func (ksp *KeyringSessionProvider) Session(conf SessionConfig) (SessionCredentials, error) {
var sessionCreds *SessionCredentials
if !conf.Refresh {
// look for cached session credentials first
keyring.Unmarshal(ksp.Keyring, SessionServiceName, conf.Profile.Name, &sessionCreds)
}
if sessionCreds == nil || time.Now().After(*sessionCreds.Expiration) {
log.Println("fetching new session")
if ksp.CredsFunc != nil {
creds, err := ksp.CredsFunc()
if err != nil {
return SessionCredentials{}, err
}
conf.Credentials = &creds
}
newCreds, err := ksp.SessionProvider.Session(conf)
if err != nil {
return SessionCredentials{}, err
}
// cache the session credentials for next time
if err = keyring.Marshal(ksp.Keyring, SessionServiceName, conf.Profile.Name, &newCreds); err != nil {
return SessionCredentials{}, err
}
sessionCreds = &newCreds
} else {
log.Printf("using cached session (expires in %s)", sessionCreds.Expiration.Sub(time.Now()))
}
return *sessionCreds, nil
}