@@ -20,19 +20,16 @@ package assetinventory
2020import  (
2121	"context" 
2222	"fmt" 
23- 	"strings" 
2423
2524	awssdk "github.com/aws/aws-sdk-go-v2/aws" 
2625	"github.com/aws/aws-sdk-go-v2/credentials/stscreds" 
27- 	"github.com/aws/aws-sdk-go-v2/service/s3" 
2826	"github.com/aws/aws-sdk-go-v2/service/sts" 
2927
3028	"github.com/elastic/cloudbeat/internal/config" 
31- 	"github.com/elastic/cloudbeat/internal/infra/clog" 
3229	"github.com/elastic/cloudbeat/internal/inventory" 
3330	"github.com/elastic/cloudbeat/internal/inventory/awsfetcher" 
3431	"github.com/elastic/cloudbeat/internal/resources/providers/awslib" 
35- 	"github.com/elastic/cloudbeat/internal/resources/utils/pointers " 
32+ 	"github.com/elastic/cloudbeat/internal/statushandler " 
3633)
3734
3835const  (
@@ -48,7 +45,7 @@ func (s *strategy) getInitialAWSConfig(ctx context.Context, cfg *config.Config)
4845	return  awslib .InitializeAWSConfig (cfg .CloudConfig .Aws .Cred , s .logger )
4946}
5047
51- func  (s  * strategy ) initAwsFetchers (ctx  context.Context ) ([]inventory.AssetFetcher , error ) {
48+ func  (s  * strategy ) initAwsFetchers (ctx  context.Context ,  statusHandler  statushandler. StatusHandlerAPI ) ([]inventory.AssetFetcher , error ) {
5249	awsConfig , err  :=  s .getInitialAWSConfig (ctx , s .cfg )
5350	if  err  !=  nil  {
5451		return  nil , err 
@@ -62,7 +59,7 @@ func (s *strategy) initAwsFetchers(ctx context.Context) ([]inventory.AssetFetche
6259
6360	// Early exit if we're scanning the entire account. 
6461	if  s .cfg .CloudConfig .Aws .AccountType  ==  config .SingleAccount  {
65- 		return  awsfetcher .New (ctx , s .logger , awsIdentity , * awsConfig ), nil 
62+ 		return  awsfetcher .New (ctx , s .logger , awsIdentity , * awsConfig ,  statusHandler ), nil 
6663	}
6764
6865	// Assume audit roles per selected account and generate fetchers for them 
@@ -84,30 +81,18 @@ func (s *strategy) initAwsFetchers(ctx context.Context) ([]inventory.AssetFetche
8481			rootRoleConfig ,
8582			fmtIAMRole (identity .Account , memberRole ),
8683		)
87- 		if  ok  :=  tryListingBuckets (ctx , s .logger ,  assumedRoleConfig ); ! ok  {
84+ 		if  ok  :=  awslib . CredentialsValid (ctx , assumedRoleConfig ,  s .logger ); ! ok  {
8885			// role does not exist, skip identity/account 
8986			s .logger .Infof ("Skipping identity on purpose %+v" , identity )
9087			continue 
9188		}
92- 		accountFetchers  :=  awsfetcher .New (ctx , s .logger , & identity , assumedRoleConfig )
89+ 		accountFetchers  :=  awsfetcher .New (ctx , s .logger , & identity , assumedRoleConfig ,  statusHandler )
9390		fetchers  =  append (fetchers , accountFetchers ... )
9491	}
9592
9693	return  fetchers , nil 
9794}
9895
99- func  tryListingBuckets (ctx  context.Context , log  * clog.Logger , roleConfig  awssdk.Config ) bool  {
100- 	s3Client  :=  s3 .NewFromConfig (roleConfig )
101- 	_ , err  :=  s3Client .ListBuckets (ctx , & s3.ListBucketsInput {MaxBuckets : pointers .Ref (int32 (1 ))})
102- 	if  err  ==  nil  {
103- 		return  true 
104- 	}
105- 	if  ! strings .Contains (err .Error (), "not authorized to perform: sts:AssumeRole" ) {
106- 		log .Errorf ("Expected a 403 autorization error, but got: %v" , err )
107- 	}
108- 	return  false 
109- }
110- 
11196func  assumeRole (client  stscreds.AssumeRoleAPIClient , cfg  awssdk.Config , arn  string ) awssdk.Config  {
11297	cfg .Credentials  =  awssdk .NewCredentialsCache (stscreds .NewAssumeRoleProvider (client , arn ))
11398	return  cfg 
0 commit comments