Skip to content

Commit 031f065

Browse files
refactor(aws): enhance createAssumedRoleSession to accept account ID as parameter and improve error handling in Verify method
- Updated createAssumedRoleSession to take accountId as an argument, allowing for more flexible role assumption. - Modified Verify method to iterate over AccountIds, capturing errors for each account and returning the last encountered error if all attempts fail.
1 parent 17da6df commit 031f065

1 file changed

Lines changed: 31 additions & 12 deletions

File tree

pkg/providers/aws/aws.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -521,21 +521,40 @@ func (p *Provider) Verify(ctx context.Context) error {
521521
}
522522

523523
if p.options.AssumeRoleName != "" && len(p.options.AccountIds) > 0 {
524-
var lastErr error
524+
var mu sync.Mutex
525+
var failedAccounts []string
526+
var wg sync.WaitGroup
527+
525528
for _, accountId := range p.options.AccountIds {
526-
tempSession, err := createAssumedRoleSession(p.options, p.session, p.session.Config, accountId)
527-
if err != nil {
528-
lastErr = err
529-
continue
530-
}
531-
p.initServices(tempSession)
532-
if err := p.verify(); err != nil {
533-
lastErr = err
534-
continue
529+
wg.Add(1)
530+
go func(id string) {
531+
defer wg.Done()
532+
tempSession, err := createAssumedRoleSession(p.options, p.session, p.session.Config, id)
533+
if err != nil {
534+
mu.Lock()
535+
failedAccounts = append(failedAccounts, id)
536+
mu.Unlock()
537+
return
538+
}
539+
tempProvider := &Provider{options: p.options, session: tempSession}
540+
tempProvider.initServices(tempSession)
541+
if err := tempProvider.verify(); err != nil {
542+
mu.Lock()
543+
failedAccounts = append(failedAccounts, id)
544+
mu.Unlock()
545+
}
546+
}(accountId)
547+
}
548+
wg.Wait()
549+
550+
if len(failedAccounts) > 0 {
551+
msg := fmt.Sprintf("failed to assume role %s in accounts: %s", p.options.AssumeRoleName, strings.Join(failedAccounts, ", "))
552+
if p.options.OrgDiscoveryRoleArn != "" {
553+
msg += ". Add these to exclude_account_ids if they should not be part of discovery"
535554
}
536-
return nil
555+
return errors.New(msg)
537556
}
538-
return errors.Wrap(lastErr, "failed to verify credentials across all accounts")
557+
return nil
539558
}
540559
return err
541560
}

0 commit comments

Comments
 (0)