forked from 99designs/aws-vault
/
provider.go
408 lines (338 loc) · 11.2 KB
/
provider.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package vault
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"time"
"github.com/99designs/aws-vault/prompt"
"github.com/99designs/keyring"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
)
const (
MaxSessionDuration = time.Hour * 36
MinSessionDuration = time.Minute * 15
MinAssumeRoleDuration = time.Minute * 15
MaxAssumeRoleDuration = time.Hour * 12
DefaultSessionDuration = time.Hour * 4
DefaultAssumeRoleDuration = time.Minute * 15
)
type VaultOptions struct {
SessionDuration time.Duration
AssumeRoleDuration time.Duration
ExpiryWindow time.Duration
MfaToken string
MfaPrompt prompt.PromptFunc
NoSession bool
Config *Config
MasterCreds *credentials.Value
}
func (o VaultOptions) Validate() error {
if o.SessionDuration < MinSessionDuration {
return errors.New("Minimum session duration is " + MinSessionDuration.String())
} else if o.SessionDuration > MaxSessionDuration {
return errors.New("Maximum session duration is " + MaxSessionDuration.String())
}
if o.AssumeRoleDuration < MinAssumeRoleDuration {
return errors.New("Minimum duration for assumed roles is " + MinAssumeRoleDuration.String())
} else if o.AssumeRoleDuration > MaxAssumeRoleDuration {
return errors.New("Maximum duration for assumed roles is " + MaxAssumeRoleDuration.String())
}
return nil
}
func (o VaultOptions) ApplyDefaults() VaultOptions {
if o.AssumeRoleDuration == 0 {
o.AssumeRoleDuration = DefaultAssumeRoleDuration
}
if o.SessionDuration == 0 {
o.SessionDuration = DefaultSessionDuration
}
return o
}
type VaultProvider struct {
credentials.Expiry
VaultOptions
profile string
expires time.Time
keyring keyring.Keyring
sessions *KeyringSessions
config *Config
creds map[string]credentials.Value
}
func NewVaultProvider(k keyring.Keyring, profile string, opts VaultOptions) (*VaultProvider, error) {
opts = opts.ApplyDefaults()
if err := opts.Validate(); err != nil {
return nil, err
}
return &VaultProvider{
VaultOptions: opts,
keyring: k,
sessions: &KeyringSessions{k, opts.Config},
profile: profile,
config: opts.Config,
creds: map[string]credentials.Value{},
}, nil
}
// Retrieve returns credentials protected by a GetSessionToken. If there is an associated
// role in the profile then AssumeRole is applied. The benefit of a session is that it doesn't
// require MFA or a user prompt to access the keychain item, much like sudo.
func (p *VaultProvider) Retrieve() (credentials.Value, error) {
if p.NoSession {
return p.RetrieveWithoutSessionToken()
}
// sessions get stored by profile, not the source
session, err := p.sessions.Retrieve(p.profile)
if err != nil {
if err == keyring.ErrKeyNotFound {
log.Printf("Session not found in keyring for %s", p.profile)
} else {
log.Println(err)
}
// session lookup missed, we need to create a new one
creds, err := p.getMasterCreds()
if err != nil {
return credentials.Value{}, err
}
session, err = p.getSessionToken(&creds)
if err != nil {
return credentials.Value{}, err
}
if err = p.sessions.Store(p.profile, session, time.Now().Add(p.SessionDuration)); err != nil {
return credentials.Value{}, err
}
}
log.Printf("Using session ****************%s, expires in %s",
(*session.AccessKeyId)[len(*session.AccessKeyId)-4:],
session.Expiration.Sub(time.Now()).String())
if profile, exists := p.config.Profile(p.profile); exists && profile.RoleARN != "" {
session, err = p.assumeRoleFromSession(session, profile)
if err != nil {
return credentials.Value{}, err
}
log.Printf("Using role ****************%s (from session token), 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
}
// 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 profile, exists := p.config.Profile(p.profile); exists && profile.RoleARN != "" {
session, err := p.assumeRole(creds, profile)
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) awsConfig() *aws.Config {
if region := os.Getenv("AWS_REGION"); region != "" {
log.Printf("Using region %q from AWS_REGION", region)
return aws.NewConfig().WithRegion(region)
}
if region := os.Getenv("AWS_DEFAULT_REGION"); region != "" {
log.Printf("Using region %q from AWS_DEFAULT_REGION", region)
return aws.NewConfig().WithRegion(region)
}
if profile, ok := p.Config.Profile(p.profile); ok {
if profile.Region != "" {
log.Printf("Using region %q from profile", profile.Region)
return aws.NewConfig().WithRegion(profile.Region)
}
}
return aws.NewConfig()
}
func (p *VaultProvider) getMasterCreds() (credentials.Value, error) {
if p.MasterCreds != nil {
return *p.MasterCreds, nil
}
source, _ := p.Config.SourceProfile(p.profile)
val, ok := p.creds[source.Name]
if !ok {
creds := credentials.NewCredentials(&KeyringProvider{Keyring: p.keyring, Profile: source.Name})
var err error
if val, err = creds.Get(); err != nil {
log.Printf("Failed to find credentials for profile %q in keyring", source.Name)
return val, err
}
p.creds[source.Name] = val
}
return val, nil
}
func (p *VaultProvider) getSessionToken(creds *credentials.Value) (sts.Credentials, error) {
params := &sts.GetSessionTokenInput{
DurationSeconds: aws.Int64(int64(p.SessionDuration.Seconds())),
}
if profile, _ := p.Config.Profile(p.profile); profile.MFASerial != "" {
params.SerialNumber = aws.String(profile.MFASerial)
if p.MfaToken == "" {
token, err := p.MfaPrompt(fmt.Sprintf("Enter token for %s: ", profile.MFASerial))
if err != nil {
return sts.Credentials{}, err
}
params.TokenCode = aws.String(token)
} else {
params.TokenCode = aws.String(p.MfaToken)
}
}
client := sts.New(session.New(p.awsConfig().
WithCredentials(credentials.NewCredentials(&credentials.StaticProvider{
Value: *creds,
}))))
source, _ := p.Config.SourceProfile(p.profile)
log.Printf("Getting new session token for profile %s", source.Name)
resp, err := client.GetSessionToken(params)
if err != nil {
return sts.Credentials{}, err
}
return *resp.Credentials, nil
}
func (p *VaultProvider) roleSessionName() string {
if profile, _ := p.Config.Profile(p.profile); profile.RoleSessionName != "" {
return profile.RoleSessionName
}
// Try to work out a role name that will hopefully end up unique.
return fmt.Sprintf("%d", time.Now().UTC().UnixNano())
}
// assumeRoleFromSession takes a session created with GetSessionToken and uses that to assume a role
func (p *VaultProvider) assumeRoleFromSession(creds sts.Credentials, profile Profile) (sts.Credentials, error) {
client := sts.New(session.New(p.awsConfig().
WithCredentials(credentials.NewStaticCredentials(
*creds.AccessKeyId,
*creds.SecretAccessKey,
*creds.SessionToken,
))))
input := &sts.AssumeRoleInput{
RoleArn: aws.String(profile.RoleARN),
RoleSessionName: aws.String(p.roleSessionName()),
DurationSeconds: aws.Int64(int64(p.AssumeRoleDuration.Seconds())),
}
if profile.ExternalID != "" {
input.ExternalId = aws.String(profile.ExternalID)
}
log.Printf("Assuming role %s from session token", profile.RoleARN)
resp, err := client.AssumeRole(input)
if err != nil {
return sts.Credentials{}, err
}
return *resp.Credentials, nil
}
// assumeRole uses IAM credentials to assume a role
func (p *VaultProvider) assumeRole(creds credentials.Value, profile Profile) (sts.Credentials, error) {
client := sts.New(session.New(p.awsConfig().
WithCredentials(credentials.NewCredentials(&credentials.StaticProvider{Value: creds})),
))
input := &sts.AssumeRoleInput{
RoleArn: aws.String(profile.RoleARN),
RoleSessionName: aws.String(p.roleSessionName()),
DurationSeconds: aws.Int64(int64(p.AssumeRoleDuration.Seconds())),
}
if profile.ExternalID != "" {
input.ExternalId = aws.String(profile.ExternalID)
}
// if we don't have a session, we need to include MFA token in the AssumeRole call
if profile.MFASerial != "" {
input.SerialNumber = aws.String(profile.MFASerial)
if p.MfaToken == "" {
token, err := p.MfaPrompt(fmt.Sprintf("Enter token for %s: ", profile.MFASerial))
if err != nil {
return sts.Credentials{}, err
}
input.TokenCode = aws.String(token)
} else {
input.TokenCode = aws.String(p.MfaToken)
}
}
log.Printf("Assuming role %s with iam credentials", profile.RoleARN)
resp, err := client.AssumeRole(input)
if err != nil {
return sts.Credentials{}, err
}
return *resp.Credentials, nil
}
type KeyringProvider struct {
Keyring keyring.Keyring
Profile string
Region string
}
func (p *KeyringProvider) IsExpired() bool {
return false
}
func (p *KeyringProvider) Retrieve() (val credentials.Value, err error) {
log.Printf("Looking up keyring for %s", p.Profile)
item, err := p.Keyring.Get(p.Profile)
if err != nil {
log.Println("Error from keyring", err)
return val, err
}
if err = json.Unmarshal(item.Data, &val); err != nil {
return val, fmt.Errorf("Invalid data in keyring: %v", err)
}
return val, err
}
func (p *KeyringProvider) Store(val credentials.Value) error {
bytes, err := json.Marshal(val)
if err != nil {
return err
}
return p.Keyring.Set(keyring.Item{
Key: p.Profile,
Label: fmt.Sprintf("aws-vault (%s)", p.Profile),
Data: bytes,
})
}
func (p *KeyringProvider) Delete() error {
return p.Keyring.Remove(p.Profile)
}
type VaultCredentials struct {
*credentials.Credentials
provider *VaultProvider
}
func NewVaultCredentials(k keyring.Keyring, profile string, opts VaultOptions) (*VaultCredentials, error) {
provider, err := NewVaultProvider(k, profile, opts)
if err != nil {
return nil, err
}
return &VaultCredentials{credentials.NewCredentials(provider), provider}, nil
}
func (v *VaultCredentials) Expires() time.Time {
return v.provider.expires
}