-
Notifications
You must be signed in to change notification settings - Fork 49
/
awssso.go
502 lines (443 loc) · 16.4 KB
/
awssso.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package sso
/*
* AWS SSO CLI
* Copyright (c) 2021-2023 Aaron Turner <synfinatic at gmail dot com>
*
* This program is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or with the authors permission any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import (
"context"
"errors"
"fmt"
"reflect"
"strconv"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/sso"
ssotypes "github.com/aws/aws-sdk-go-v2/service/sso/types"
"github.com/aws/aws-sdk-go-v2/service/ssooidc"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/davecgh/go-spew/spew"
"github.com/synfinatic/aws-sso-cli/internal/storage"
"github.com/synfinatic/aws-sso-cli/internal/url"
"github.com/synfinatic/aws-sso-cli/internal/utils"
"github.com/synfinatic/gotable"
)
var MAX_RETRY_ATTEMPTS int = 10
var MAX_BACKOFF_SECONDS int = 5
// Necessary for mocking
type SsoOidcAPI interface {
RegisterClient(context.Context, *ssooidc.RegisterClientInput, ...func(*ssooidc.Options)) (*ssooidc.RegisterClientOutput, error)
StartDeviceAuthorization(context.Context, *ssooidc.StartDeviceAuthorizationInput, ...func(*ssooidc.Options)) (*ssooidc.StartDeviceAuthorizationOutput, error)
CreateToken(context.Context, *ssooidc.CreateTokenInput, ...func(*ssooidc.Options)) (*ssooidc.CreateTokenOutput, error)
}
type SsoAPI interface {
ListAccountRoles(context.Context, *sso.ListAccountRolesInput, ...func(*sso.Options)) (*sso.ListAccountRolesOutput, error)
ListAccounts(context.Context, *sso.ListAccountsInput, ...func(*sso.Options)) (*sso.ListAccountsOutput, error)
GetRoleCredentials(context.Context, *sso.GetRoleCredentialsInput, ...func(*sso.Options)) (*sso.GetRoleCredentialsOutput, error)
Logout(context.Context, *sso.LogoutInput, ...func(*sso.Options)) (*sso.LogoutOutput, error)
}
type AWSSSO struct {
key string // key in the settings file that names us
sso SsoAPI
ssooidc SsoOidcAPI
store storage.SecureStorage
ClientName string `json:"ClientName"`
ClientType string `json:"ClientType"`
SsoRegion string `json:"ssoRegion"`
StartUrl string `json:"startUrl"`
ClientData storage.RegisterClientData `json:"RegisterClient"`
DeviceAuth storage.StartDeviceAuthData `json:"StartDeviceAuth"`
Token storage.CreateTokenResponse `json:"TokenResponse"`
tokenLock sync.RWMutex // lock for our Token
Accounts []AccountInfo `json:"Accounts"`
Roles map[string][]RoleInfo `json:"Roles"` // key is AccountId
rolesLock sync.RWMutex // lock for our Roles
SSOConfig *SSOConfig `json:"SSOConfig"`
urlAction url.Action // cache for future calls
browser string // cache for future calls
urlExecCommand []string // cache for future calls
authenticateLock sync.RWMutex // lock for reauthenticate()
}
func NewAWSSSO(s *SSOConfig, store *storage.SecureStorage) *AWSSSO {
var maxRetry int = MAX_RETRY_ATTEMPTS
if s.MaxRetry > 0 {
maxRetry = s.MaxRetry
}
var maxBackoff int = MAX_BACKOFF_SECONDS
if s.MaxBackoff > 0 {
maxBackoff = s.MaxBackoff
}
log.Debugf("loading SSO using %d retries and max %dsec backoff", maxRetry, maxBackoff)
r := retry.NewStandard(func(o *retry.StandardOptions) {
o.MaxAttempts = maxRetry
o.MaxBackoff = time.Duration(maxBackoff) * time.Second
})
oidcSession := ssooidc.New(ssooidc.Options{
Region: s.SSORegion,
Retryer: r,
})
ssoSession := sso.New(sso.Options{
Region: s.SSORegion,
Retryer: r,
})
as := AWSSSO{
key: s.key,
sso: ssoSession,
ssooidc: oidcSession,
store: *store,
ClientName: awsSSOClientName,
ClientType: awsSSOClientType,
SsoRegion: s.SSORegion,
StartUrl: s.StartUrl,
Roles: map[string][]RoleInfo{}, // key is AccountId
SSOConfig: s,
urlAction: s.settings.UrlAction,
browser: s.settings.Browser,
urlExecCommand: s.settings.UrlExecCommand,
}
return &as
}
type RoleInfo struct {
Id int `yaml:"Id" json:"Id" header:"Id"`
Arn string `yaml:"-" json:"-" header:"Arn"`
RoleName string `yaml:"RoleName" json:"RoleName" header:"RoleName"`
AccountId string `yaml:"AccountId" json:"AccountId" header:"AccountId"`
AccountName string `yaml:"AccountName" json:"AccountName" header:"AccountName"`
EmailAddress string `yaml:"EmailAddress" json:"EmailAddress" header:"EmailAddress"`
Expires int64 `yaml:"Expires" json:"Expires" header:"Expires"`
Profile string `yaml:"Profile" json:"Profile" header:"Profile"`
Region string `yaml:"Region" json:"Region" header:"Region"`
SSORegion string `header:"SSORegion"`
StartUrl string `header:"StartUrl"`
Via string `header:"Via"`
}
func (ri RoleInfo) GetHeader(fieldName string) (string, error) {
v := reflect.ValueOf(ri)
return gotable.GetHeaderTag(v, fieldName)
}
func (ri RoleInfo) RoleArn() string {
a, _ := strconv.ParseInt(ri.AccountId, 10, 64)
return utils.MakeRoleARN(a, ri.RoleName)
}
func (ri RoleInfo) GetAccountId64() int64 {
i64, err := strconv.ParseInt(ri.AccountId, 10, 64)
if err != nil {
log.WithError(err).Panicf("Invalid AWS AccountID from AWS SSO: %s", ri.AccountId)
}
if i64 < 0 {
log.WithError(err).Panicf("AWS AccountID must be >= 0: %s", ri.AccountId)
}
return i64
}
// GetRoles fetches all the AWS SSO IAM Roles for the given AWS Account
// Code is running up to X Threads via cache.processSSORoles()
// and we must stricly protect reads & writes to our as.Roles[] dict
func (as *AWSSSO) GetRoles(account AccountInfo) ([]RoleInfo, error) {
as.rolesLock.RLock()
roles, ok := as.Roles[account.AccountId]
as.rolesLock.RUnlock()
if ok && len(roles) > 0 {
return roles, nil
}
as.rolesLock.Lock()
as.Roles[account.AccountId] = []RoleInfo{}
as.rolesLock.Unlock()
// must lock this becacase the access token can change
as.tokenLock.Lock()
input := sso.ListAccountRolesInput{
AccessToken: aws.String(as.Token.AccessToken),
AccountId: aws.String(account.AccountId),
MaxResults: aws.Int32(1000),
}
as.tokenLock.Unlock()
output, err := as.ListAccountRoles(&input)
if err != nil {
// failed... give up
as.rolesLock.RLock()
defer as.rolesLock.RUnlock()
return as.Roles[account.AccountId], err
}
// Process the output
for i, r := range output.RoleList {
as.makeRoleInfo(account, i, r)
}
for aws.ToString(output.NextToken) != "" {
input.NextToken = output.NextToken
output, err = as.ListAccountRoles(&input)
if err != nil {
// failed... give up
as.rolesLock.RLock()
defer as.rolesLock.RUnlock()
return as.Roles[account.AccountId], err
}
as.rolesLock.RLock()
roleCount := len(as.Roles[account.AccountId])
as.rolesLock.RUnlock()
for i, r := range output.RoleList {
x := roleCount + i
as.makeRoleInfo(account, x, r)
}
}
as.rolesLock.RLock()
defer as.rolesLock.RUnlock()
return as.Roles[account.AccountId], nil
}
func (as *AWSSSO) ListAccounts(input *sso.ListAccountsInput) (*sso.ListAccountsOutput, error) {
var err error = errors.New("foo")
var output *sso.ListAccountsOutput
for cnt := 0; err != nil && cnt <= MAX_RETRY_ATTEMPTS; cnt++ {
output, err = as.sso.ListAccounts(context.TODO(), input)
if err != nil {
var tmr *ssotypes.TooManyRequestsException
var ue *ssotypes.UnauthorizedException
switch {
case errors.As(err, &ue):
// sometimes our AccessToken is invalid so try a new one once?
// if we have to re-auth, hold everyone else up since that will reduce other failures
as.rolesLock.Lock()
log.Errorf("AccessToken Unauthorized Error; refreshing: %s", err.Error())
if err = as.reauthenticate(); err != nil {
// fail hard now
return output, err
}
input.AccessToken = aws.String(as.Token.AccessToken)
as.rolesLock.Unlock()
case errors.As(err, &tmr):
// try again
log.Warnf("Exceeded MaxRetry/MaxBackoff. Consider tuning values.")
time.Sleep(time.Duration(MAX_BACKOFF_SECONDS) * time.Second)
default:
log.WithError(err).Error("Unexpected error")
}
}
}
return output, err
}
// ListAccountRoles is a wrapper around sso.ListAccountRoles which does our retry logic
func (as *AWSSSO) ListAccountRoles(input *sso.ListAccountRolesInput) (*sso.ListAccountRolesOutput, error) {
var err error = errors.New("foo")
var output *sso.ListAccountRolesOutput
for cnt := 0; err != nil && cnt <= MAX_RETRY_ATTEMPTS; cnt++ {
output, err = as.sso.ListAccountRoles(context.TODO(), input)
if err != nil {
var tmr *ssotypes.TooManyRequestsException
var ue *ssotypes.UnauthorizedException
switch {
case errors.As(err, &ue):
// sometimes our AccessToken is invalid so try a new one once?
// if we have to re-auth, hold everyone else up since that will reduce other failures
as.rolesLock.Lock()
log.Errorf("AccessToken Unauthorized Error; refreshing: %s", err.Error())
if err = as.reauthenticate(); err != nil {
// fail hard now
log.WithError(err).Fatalf("Unexpected auth failure")
}
input.AccessToken = aws.String(as.Token.AccessToken)
as.rolesLock.Unlock()
case errors.As(err, &tmr):
// try again
log.Warnf("Exceeded MaxRetry/MaxBackoff. Consider tuning values.")
time.Sleep(time.Duration(MAX_BACKOFF_SECONDS) * time.Second)
default:
log.WithError(err).Error("Unexpected error")
}
}
}
return output, err
}
// makeRoleInfo takes the sso.types.RoleInfo and adds it onto our as.Roles[accountId] list
func (as *AWSSSO) makeRoleInfo(account AccountInfo, i int, r ssotypes.RoleInfo) {
var via string
aId, _ := strconv.ParseInt(account.AccountId, 10, 64)
ssoRole, err := as.SSOConfig.GetRole(aId, aws.ToString(r.RoleName))
if err != nil && len(ssoRole.Via) > 0 {
via = ssoRole.Via
}
as.rolesLock.Lock()
defer as.rolesLock.Unlock()
as.Roles[account.AccountId] = append(as.Roles[account.AccountId], RoleInfo{
Id: i,
AccountId: aws.ToString(r.AccountId),
Arn: utils.MakeRoleARN(aId, aws.ToString(r.RoleName)),
RoleName: aws.ToString(r.RoleName),
AccountName: account.AccountName,
EmailAddress: account.EmailAddress,
SSORegion: as.SsoRegion,
StartUrl: as.StartUrl,
Via: via,
})
}
type AccountInfo struct {
Id int `yaml:"Id" json:"Id" header:"Id"`
AccountId string `yaml:"AccountId" json:"AccountId" header:"AccountId"`
AccountName string `yaml:"AccountName" json:"AccountName" header:"AccountName"`
EmailAddress string `yaml:"EmailAddress" json:"EmailAddress" header:"EmailAddress"`
}
func (ai AccountInfo) GetHeader(fieldName string) (string, error) {
v := reflect.ValueOf(ai)
return gotable.GetHeaderTag(v, fieldName)
}
func (ai AccountInfo) GetAccountId64() int64 {
i64, err := strconv.ParseInt(ai.AccountId, 10, 64)
if err != nil {
log.WithError(err).Panicf("Invalid AWS AccountID from AWS SSO: %s", ai.AccountId)
}
if i64 < 0 {
log.WithError(err).Panicf("AWS AccountID must be >= 0: %s", ai.AccountId)
}
return i64
}
// GetAccounts queries AWS and returns a list of AWS accounts
func (as *AWSSSO) GetAccounts() ([]AccountInfo, error) {
if len(as.Accounts) > 0 {
return as.Accounts, nil
}
input := sso.ListAccountsInput{
AccessToken: aws.String(as.Token.AccessToken),
MaxResults: aws.Int32(1000),
}
output, err := as.ListAccounts(&input)
if err != nil {
return as.Accounts, err
}
for i, r := range output.AccountList {
as.Accounts = append(as.Accounts, AccountInfo{
Id: i,
AccountId: aws.ToString(r.AccountId),
AccountName: aws.ToString(r.AccountName),
EmailAddress: aws.ToString(r.EmailAddress),
})
}
for aws.ToString(output.NextToken) != "" {
input.NextToken = output.NextToken
output, err = as.ListAccounts(&input)
if err != nil {
return as.Accounts, err
}
x := len(as.Accounts)
for i, r := range output.AccountList {
as.Accounts = append(as.Accounts, AccountInfo{
Id: x + i,
AccountId: aws.ToString(r.AccountId),
AccountName: aws.ToString(r.AccountName),
EmailAddress: aws.ToString(r.EmailAddress),
})
}
}
return as.Accounts, nil
}
var roleChainMap map[string]bool = map[string]bool{} // track our roles
// GetRoleCredentials recursively does any sts:AssumeRole calls as necessary for role-chaining
// through `Via` and returns the final set of RoleCredentials for the requested role
func (as *AWSSSO) GetRoleCredentials(accountId int64, role string) (storage.RoleCredentials, error) {
aId, err := utils.AccountIdToString(accountId)
if err != nil {
return storage.RoleCredentials{}, err
}
// is the role defined in the config file?
configRole, err := as.SSOConfig.GetRole(accountId, role)
if err != nil {
log.Debugf("SSOConfig.GetRole(): %s", err.Error())
}
// If not in config OR config does not require doing a Via
if err != nil || configRole.Via == "" {
log.Debugf("Getting %s:%s directly", aId, role)
// This is the actual role creds requested through AWS SSO
input := sso.GetRoleCredentialsInput{
AccessToken: aws.String(as.Token.AccessToken),
AccountId: aws.String(aId),
RoleName: aws.String(role),
}
output, err := as.sso.GetRoleCredentials(context.TODO(), &input)
if err != nil {
return storage.RoleCredentials{}, err
}
ret := storage.RoleCredentials{
AccountId: accountId,
RoleName: role,
AccessKeyId: aws.ToString(output.RoleCredentials.AccessKeyId),
SecretAccessKey: aws.ToString(output.RoleCredentials.SecretAccessKey),
SessionToken: aws.ToString(output.RoleCredentials.SessionToken),
Expiration: output.RoleCredentials.Expiration,
}
return ret, nil
}
// Detect loops
roleChainMap[configRole.ARN] = true
for k := range roleChainMap {
if k == configRole.Via {
log.Fatalf("Detected role chain loop! Getting %s via %s", configRole.ARN, configRole.Via)
}
roleChainMap[k] = true
}
// Need to recursively call sts:AssumeRole in order to retrieve the STS creds for
// the requested role
// role has a Via
log.Debugf("Getting %s:%s via %s", aId, role, configRole.Via)
viaAccountId, viaRole, err := utils.ParseRoleARN(configRole.Via)
if err != nil {
return storage.RoleCredentials{}, fmt.Errorf("Invalid Via %s: %s", configRole.Via, err.Error())
}
// recurse
creds, err := as.GetRoleCredentials(viaAccountId, viaRole)
if err != nil {
return storage.RoleCredentials{}, err
}
cfgCreds := credentials.NewStaticCredentialsProvider(
creds.AccessKeyId,
creds.SecretAccessKey,
creds.SessionToken,
)
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(as.SsoRegion),
config.WithCredentialsProvider(cfgCreds),
)
if err != nil {
return storage.RoleCredentials{}, err
}
stsSession := sts.NewFromConfig(cfg)
previousAccount, _ := utils.AccountIdToString(creds.AccountId)
previousRole := fmt.Sprintf("%s@%s", creds.RoleName, previousAccount)
input := sts.AssumeRoleInput{
// DurationSeconds: aws.Int64(900),
RoleArn: aws.String(utils.MakeRoleARN(accountId, role)),
RoleSessionName: aws.String(previousRole),
}
if configRole.ExternalId != "" {
// Optional vlaue: https://docs.aws.amazon.com/sdk-for-go/api/service/sts/#AssumeRoleInput
input.ExternalId = aws.String(configRole.ExternalId)
}
if configRole.SourceIdentity != "" {
input.SourceIdentity = aws.String(configRole.SourceIdentity)
}
output, err := stsSession.AssumeRole(context.TODO(), &input)
if err != nil {
return storage.RoleCredentials{}, err
}
log.Debugf("%s", spew.Sdump(output))
ret := storage.RoleCredentials{
AccountId: accountId,
RoleName: role,
AccessKeyId: aws.ToString(output.Credentials.AccessKeyId),
SecretAccessKey: aws.ToString(output.Credentials.SecretAccessKey),
SessionToken: aws.ToString(output.Credentials.SessionToken),
Expiration: aws.ToTime(output.Credentials.Expiration).UnixMilli(),
}
return ret, nil
}