github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/cloud/aws/config/config.go (about) 1 package config 2 3 import ( 4 "context" 5 6 "github.com/aws/aws-sdk-go-v2/aws" 7 awsconfig "github.com/aws/aws-sdk-go-v2/config" 8 "golang.org/x/xerrors" 9 ) 10 11 func EndpointResolver(endpoint string) aws.EndpointResolverWithOptionsFunc { 12 return aws.EndpointResolverWithOptionsFunc(func(_, reg string, options ...interface{}) (aws.Endpoint, error) { 13 return aws.Endpoint{ 14 PartitionID: "aws", 15 URL: endpoint, 16 SigningRegion: reg, 17 Source: aws.EndpointSourceCustom, 18 }, nil 19 }) 20 } 21 22 func MakeAWSOptions(region, endpoint string) []func(*awsconfig.LoadOptions) error { 23 var options []func(*awsconfig.LoadOptions) error 24 25 if region != "" { 26 options = append(options, awsconfig.WithRegion(region)) 27 } 28 29 if endpoint != "" { 30 options = append(options, awsconfig.WithEndpointResolverWithOptions(EndpointResolver(endpoint))) 31 } 32 33 return options 34 } 35 36 func LoadDefaultAWSConfig(ctx context.Context, region, endpoint string) (aws.Config, error) { 37 cfg, err := awsconfig.LoadDefaultConfig(ctx, MakeAWSOptions(region, endpoint)...) 38 if err != nil { 39 return aws.Config{}, xerrors.Errorf("aws config load error: %w", err) 40 } 41 42 if cfg.Region == "" { 43 return aws.Config{}, xerrors.New("aws region is required") 44 } 45 46 return cfg, nil 47 }