Skip to content

Commit b833bc9

Browse files
Merge pull request #5680 from alban-stourbe-wmx/feature/add-aws-profile-from-aws-credentials
Add loadConfig S3 based on AWS_PROFILE ~/.aws/credentials
2 parents d57b3a6 + 3280136 commit b833bc9

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

internal/runner/options.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,25 @@ func validateMissingS3Options(options *types.Options) []string {
235235
if options.AwsBucketName == "" {
236236
missing = append(missing, "AWS_TEMPLATE_BUCKET")
237237
}
238-
if options.AwsAccessKey == "" {
239-
missing = append(missing, "AWS_ACCESS_KEY")
240-
}
241-
if options.AwsSecretKey == "" {
242-
missing = append(missing, "AWS_SECRET_KEY")
243-
}
244-
if options.AwsRegion == "" {
245-
missing = append(missing, "AWS_REGION")
238+
if options.AwsProfile == "" {
239+
var missingCreds []string
240+
if options.AwsAccessKey == "" {
241+
missingCreds = append(missingCreds, "AWS_ACCESS_KEY")
242+
}
243+
if options.AwsSecretKey == "" {
244+
missingCreds = append(missingCreds, "AWS_SECRET_KEY")
245+
}
246+
if options.AwsRegion == "" {
247+
missingCreds = append(missingCreds, "AWS_REGION")
248+
}
249+
250+
missing = append(missing, missingCreds...)
251+
252+
if len(missingCreds) > 0 {
253+
missing = append(missing, "AWS_PROFILE")
254+
}
246255
}
256+
247257
return missing
248258
}
249259

@@ -449,6 +459,7 @@ func readEnvInputVars(options *types.Options) {
449459
options.AwsSecretKey = os.Getenv("AWS_SECRET_KEY")
450460
options.AwsBucketName = os.Getenv("AWS_TEMPLATE_BUCKET")
451461
options.AwsRegion = os.Getenv("AWS_REGION")
462+
options.AwsProfile = os.Getenv("AWS_PROFILE")
452463

453464
// Azure options for downloading templates from an Azure Blob Storage container
454465
options.AzureContainerName = os.Getenv("AZURE_CONTAINER_NAME")

pkg/external/customtemplates/s3.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (bk *customTemplateS3Bucket) Update(ctx context.Context) {
6262
func NewS3Providers(options *types.Options) ([]*customTemplateS3Bucket, error) {
6363
providers := []*customTemplateS3Bucket{}
6464
if options.AwsBucketName != "" && !options.AwsTemplateDisableDownload {
65-
s3c, err := getS3Client(context.TODO(), options.AwsAccessKey, options.AwsSecretKey, options.AwsRegion)
65+
s3c, err := getS3Client(context.TODO(), options.AwsAccessKey, options.AwsSecretKey, options.AwsRegion, options.AwsProfile)
6666
if err != nil {
6767
return nil, errorutil.NewWithErr(err).Msgf("error downloading s3 bucket %s", options.AwsBucketName)
6868
}
@@ -104,10 +104,24 @@ func downloadToFile(downloader *manager.Downloader, targetDirectory, bucket, key
104104
return err
105105
}
106106

107-
func getS3Client(ctx context.Context, accessKey string, secretKey string, region string) (*s3.Client, error) {
108-
cfg, err := config.LoadDefaultConfig(ctx, config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")), config.WithRegion(region))
109-
if err != nil {
110-
return nil, err
107+
func getS3Client(ctx context.Context, accessKey string, secretKey string, region string, profile string) (*s3.Client, error) {
108+
var cfg aws.Config
109+
var err error
110+
if profile != "" {
111+
cfg, err = config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile(profile))
112+
if err != nil {
113+
return nil, err
114+
}
115+
} else if accessKey != "" && secretKey != "" {
116+
cfg, err = config.LoadDefaultConfig(ctx, config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")), config.WithRegion(region))
117+
if err != nil {
118+
return nil, err
119+
}
120+
} else {
121+
cfg, err = config.LoadDefaultConfig(ctx)
122+
if err != nil {
123+
return nil, err
124+
}
111125
}
112126
return s3.NewFromConfig(cfg), nil
113127
}

pkg/types/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ type Options struct {
345345
GitLabTemplateRepositoryIDs []int
346346
// GitLabTemplateDisableDownload disables downloading templates from custom GitLab repositories
347347
GitLabTemplateDisableDownload bool
348+
// AWS access profile from ~/.aws/credentials file for downloading templates from S3 bucket
349+
AwsProfile string
348350
// AWS access key for downloading templates from S3 bucket
349351
AwsAccessKey string
350352
// AWS secret key for downloading templates from S3 bucket

0 commit comments

Comments
 (0)