-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcredentials-manager.go
410 lines (344 loc) · 10.1 KB
/
credentials-manager.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
409
410
package main
import (
"errors"
"fmt"
"log"
"os"
"sync"
"time"
"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"
)
// Common errors for credential manager
var (
errMFANeeded = fmt.Errorf("MFA needed")
errUnknownProfile = fmt.Errorf("Unknown profile")
errProtectedProfile = fmt.Errorf("Protected profile")
// errSourceSessionExpired = fmt.Errorf("Source session expired")
)
type fatalError struct {
err error
}
func (e fatalError) Error() string {
return fmt.Sprintf("%v", e.err.Error())
}
func makeFatal(err error) error {
return &fatalError{
err: err,
}
}
func isFatalError(err error) bool {
_, ok := err.(*fatalError)
return ok
}
// AwsCredentials is a wrapper of sts.Credentials that adds Region
type AwsCredentials struct {
sts.Credentials
Region string
}
// CredentialsManager provides an interface
type CredentialsManager interface {
Role() string
RetrieveRole(name, MFA string) (*AwsCredentials, error)
RetrieveRoleARN(RoleARN, MFASerial, MFA string) (*sts.Credentials, error)
AssumeRole(name, mfa string) error
AssumeRoleARN(name, RoleARN, MFASerial, MFA string) error
GetCredentials() (*sts.Credentials, error)
SetSourceProfile(name, mfa string) error
Region() string
}
// CredentialsExpirationManager is responsible for renewing a set of credentials
type CredentialsExpirationManager struct {
lock sync.Mutex
// config is the loaded configuration
config Config
// profile is the current base profile
sourceProfile Profile
sourceProfileName string
// err is the current internal error
err error
// This is the default session and information
sourceSession *session.Session
sourceCredentials *sts.Credentials
sourceSTSClient *sts.STS
// This is the current active credentials
role string
credentials *sts.Credentials
}
// NewCredentialsExpirationManager returns a credentialsExpirationManager
// It creates a session, then it will call GetSessionToken to retrieve a pair of
// temporary credentials.
func NewCredentialsExpirationManager(profileName string, conf Config, mfa string) *CredentialsExpirationManager {
cm := newTemporaryCredentialsManager(profileName, conf, mfa)
go cm.Refresher()
return cm
}
func newTemporaryCredentialsManager(profileName string, conf Config, mfa string) *CredentialsExpirationManager {
cm := &CredentialsExpirationManager{
role: profileName,
config: conf,
}
err := cm.SetSourceProfile(profileName, mfa)
if err != nil {
if isFatalError(err) {
fmt.Fprintf(errout, "%v\n", err)
os.Exit(1)
}
}
return cm
}
// SetSourceProfile updates the credentials manager with new soruce profile.
// This operation will also update the current profile to the source profile
func (m *CredentialsExpirationManager) SetSourceProfile(name, mfa string) error {
m.lock.Lock()
defer m.lock.Unlock()
fatal := false
checkErr := func(err error) error {
if fatal {
return makeFatal(err)
}
return err
}
m.err = nil
log.Printf("Setting base profile: %v", name)
profile, ok := m.config.Profiles[name]
if !ok {
m.err = errUnknownProfile
if name != profileDefault {
return makeFatal(errUnknownProfile)
}
return errUnknownProfile
}
sess := session.New(&aws.Config{
Region: &profile.Region,
Credentials: credentials.NewStaticCredentials(
profile.AwsAccessKeyID,
profile.AwsSecretAccessKey,
profile.AwsSessionToken,
),
})
stsClient := sts.New(sess)
if profile.MFASerial != "" && mfa == "" {
m.err = errMFANeeded
return errMFANeeded
}
sessionTokenInput := &sts.GetSessionTokenInput{
DurationSeconds: aws.Int64(10 * 3600),
}
if profile.MFASerial != "" {
log.Println("Setting serial: ", profile.MFASerial)
sessionTokenInput.SerialNumber = aws.String(profile.MFASerial)
}
if mfa != "" {
log.Println("Setting mfa:", mfa)
sessionTokenInput.TokenCode = aws.String(mfa)
fatal = true
}
log.Println("Serial: ", profile.MFASerial, ", token: ", mfa)
sessionTokenResp, err := stsClient.GetSessionToken(sessionTokenInput)
if err != nil {
log.Println("request failed:", sessionTokenInput)
err = checkErr(err)
m.err = err
return err
}
m.credentials = sessionTokenResp.Credentials
m.sourceCredentials = sessionTokenResp.Credentials
m.sourceSession = session.New(&aws.Config{
Region: &profile.Region,
Credentials: credentials.NewStaticCredentials(
*m.credentials.AccessKeyId,
*m.credentials.SecretAccessKey,
*m.credentials.SessionToken,
),
})
m.role = name
m.sourceProfile = profile
m.sourceProfileName = name
m.sourceSTSClient = sts.New(m.sourceSession)
return nil
}
// Role returns the name of the current active role
func (m *CredentialsExpirationManager) Role() string {
return m.role
}
// Region returns the configured region for the profile or empty string if not
// defined
func (m *CredentialsExpirationManager) Region() string {
role := m.role
if role == "" {
role = profileDefault
}
profile, ok := m.config.Profiles[role]
if !ok {
fmt.Printf("FAiled to lookup: %v\n", role)
return ""
}
return profile.Region
}
// Refresher starts a Go routine and refreshes the credentials
func (m *CredentialsExpirationManager) Refresher() {
for {
select {
case <-time.After(10 * time.Second):
if m.err != nil {
continue
}
m.refreshCredentials()
}
}
}
// AssumeRole changes (assumes) the role `name`. An optional MFA can be passed
// to the function, if set to "" the MFA is ignored
func (m *CredentialsExpirationManager) AssumeRole(name, MFA string) error {
profile, ok := m.config.Profiles[name]
if !ok {
return errUnknownProfile
}
if profile.protected() {
return errProtectedProfile
}
log.Printf("source profile: %v, needed source profile: %v\n", m.sourceProfileName, profile.SourceProfile)
log.Printf("Cerentials expired: %v", m.sourceCredentialsExpired())
if profile.SourceProfile != m.sourceProfileName || m.sourceCredentialsExpired() {
err := m.SetSourceProfile(profile.SourceProfile, MFA)
if err != nil {
return err
}
}
errAssume := m.AssumeRoleARN(name, profile.RoleARN, profile.MFASerial, MFA)
if errAssume != nil {
return errAssume
}
err := writeAwsConfig(profile.Region)
if err != nil {
fmt.Fprintf(errout, "error updating region: %v\n", err)
}
err = writeAwsCredentials(profile.Region)
if err != nil {
fmt.Fprintf(errout, "error updating region: %v\n", err)
}
return nil
}
// RetrieveRole will assume and fetch temporary credentials, but does not update
// the role and credentials stored by the manager.
func (m *CredentialsExpirationManager) RetrieveRole(name, MFA string) (*AwsCredentials, error) {
profile, ok := m.config.Profiles[name]
if !ok {
return nil, errUnknownProfile
}
if profile.SourceProfile == m.sourceProfileName && !m.sourceCredentialsExpired() {
c, err := m.RetrieveRoleARN(profile.RoleARN, profile.MFASerial, MFA)
if err != nil {
return nil, err
}
return &AwsCredentials{Credentials: *c, Region: profile.Region}, nil
}
cm := newTemporaryCredentialsManager(profileDefault, m.config, "")
err := cm.SetSourceProfile(profile.SourceProfile, MFA)
if err != nil {
return nil, err
}
c, err := cm.RetrieveRoleARN(profile.RoleARN, profile.MFASerial, MFA)
if err != nil {
return nil, err
}
return &AwsCredentials{Credentials: *c, Region: profile.Region}, nil
}
// RetrieveRoleARN assumes and fetch temporary credentials based on the RoleArn
func (m *CredentialsExpirationManager) RetrieveRoleARN(RoleARN, MFASerial, MFA string) (*sts.Credentials, error) {
if m.err != nil {
return nil, m.err
}
if m.sourceCredentialsExpired() {
err := m.SetSourceProfile(m.sourceProfileName, MFA)
if err != nil {
return nil, err
}
}
// source profile is requested return sourceCredentials
if RoleARN == m.sourceProfile.RoleARN {
return m.sourceCredentials, nil
}
if MFASerial != "" && MFA == "" {
return nil, errMFANeeded
}
assumeRoleInput := &sts.AssumeRoleInput{
RoleArn: &RoleARN,
RoleSessionName: &m.sourceProfile.RoleSessionName,
}
if MFASerial != "" {
assumeRoleInput.SerialNumber = &MFASerial
if MFA != "" {
assumeRoleInput.TokenCode = &MFA
}
}
resp, err := m.sourceSTSClient.AssumeRole(assumeRoleInput)
if err != nil {
return nil, err
}
return resp.Credentials, nil
}
// AssumeRoleARN assumes the role specified by RoleARN and will store it as
// with the name specified.
func (m *CredentialsExpirationManager) AssumeRoleARN(name, RoleARN, MFASerial, MFA string) error {
if m.err != nil {
return m.err
}
creds, err := m.RetrieveRoleARN(RoleARN, MFASerial, MFA)
if err != nil {
return err
}
m.setCredentials(creds, name)
return nil
}
// SetCredentials updates the stored credentials and the name of the role associated
// with the credentials
func (m *CredentialsExpirationManager) setCredentials(newCreds *sts.Credentials, role string) {
m.lock.Lock()
defer m.lock.Unlock()
m.credentials = newCreds
m.role = role
}
// GetCredentials returns the current saved credentials. The returned credentials
// are copied before they are returned.
func (m *CredentialsExpirationManager) GetCredentials() (*sts.Credentials, error) {
if m.err != nil {
return nil, m.err
}
m.lock.Lock()
defer m.lock.Unlock()
return &sts.Credentials{
AccessKeyId: aws.String(*m.credentials.AccessKeyId),
Expiration: aws.Time(*m.credentials.Expiration),
SecretAccessKey: aws.String(*m.credentials.SecretAccessKey),
SessionToken: aws.String(*m.credentials.SessionToken),
}, nil
}
func (m *CredentialsExpirationManager) sourceCredentialsExpired() bool {
if m.sourceCredentials == nil {
return true
}
return m.sourceCredentials.Expiration.Before(time.Now())
}
func (m *CredentialsExpirationManager) refreshCredentials() error {
if m.sourceSTSClient == nil {
return errors.New("No STS client set for refreshing credentials")
}
creds, err := m.GetCredentials()
if err != nil {
return err
}
if time.Now().Add(600 * time.Second).Before(*creds.Expiration) {
// We no not need to refresh
return nil
}
if m.role == "" || m.role == profileDefault {
// Do not refresh main default role, let it time out
return nil
}
fmt.Println("====> refreshing credentials")
return m.AssumeRole(m.role, "")
}