9
9
10
10
"github.com/aws/aws-sdk-go/aws/request"
11
11
"github.com/aws/aws-sdk-go/service/iam"
12
+ "github.com/aws/aws-sdk-go/service/s3"
12
13
"github.com/gruntwork-io/go-commons/version"
13
14
14
15
"github.com/aws/aws-sdk-go/aws"
@@ -46,7 +47,7 @@ var addUserAgent = request.NamedHandler{
46
47
47
48
// CreateAwsSessionFromConfig returns an AWS session object for the given config region (required), profile name (optional), and IAM role to assume
48
49
// (optional), ensuring that the credentials are available.
49
- func CreateAwsSessionFromConfig (config * AwsSessionConfig , terragruntOptions * options.TerragruntOptions ) (* session.Session , error ) {
50
+ func CreateAwsSessionFromConfig (config * AwsSessionConfig , opts * options.TerragruntOptions ) (* session.Session , error ) {
50
51
defaultResolver := endpoints .DefaultResolver ()
51
52
s3CustResolverFn := func (service , region string , optFns ... func (* endpoints.Options )) (endpoints.ResolvedEndpoint , error ) {
52
53
if service == "s3" && config .CustomS3Endpoint != "" {
@@ -89,7 +90,7 @@ func CreateAwsSessionFromConfig(config *AwsSessionConfig, terragruntOptions *opt
89
90
sess .Handlers .Build .PushFrontNamed (addUserAgent )
90
91
91
92
// Merge the config based IAMRole options into the original one, as the config has higher precedence than CLI.
92
- iamRoleOptions := terragruntOptions .IAMRoleOptions
93
+ iamRoleOptions := opts .IAMRoleOptions
93
94
if config .RoleArn != "" {
94
95
iamRoleOptions = options .MergeIAMRoleOptions (
95
96
iamRoleOptions ,
@@ -113,7 +114,7 @@ func CreateAwsSessionFromConfig(config *AwsSessionConfig, terragruntOptions *opt
113
114
114
115
if iamRoleOptions .RoleARN != "" {
115
116
sess .Config .Credentials = getSTSCredentialsFromIAMRoleOptions (sess , iamRoleOptions , credentialOptFn )
116
- } else if creds := getCredentialsFromEnvs (terragruntOptions ); creds != nil {
117
+ } else if creds := getCredentialsFromEnvs (opts ); creds != nil {
117
118
sess .Config .Credentials = creds
118
119
}
119
120
@@ -188,14 +189,23 @@ func getCredentialsFromEnvs(opts *options.TerragruntOptions) *credentials.Creden
188
189
return credentials .NewStaticCredentials (accessKeyID , secretAccessKey , sessionToken )
189
190
}
190
191
192
+ func CreateS3Client (config * AwsSessionConfig , opts * options.TerragruntOptions ) (* s3.S3 , error ) {
193
+ session , err := CreateAwsSession (config , opts )
194
+ if err != nil {
195
+ return nil , errors .New (err )
196
+ }
197
+
198
+ return s3 .New (session ), nil
199
+ }
200
+
191
201
// CreateAwsSession returns an AWS session object. The session is configured by either:
192
202
// - The provided AwsSessionConfig struct, which specifies region (required), profile name (optional), and IAM role to
193
203
// assume (optional).
194
204
// - The provided TerragruntOptions struct, which specifies any IAM role to assume (optional).
195
205
//
196
206
// Note that if the AwsSessionConfig object is null, this will return default session credentials using the default
197
207
// credentials chain of the AWS SDK.
198
- func CreateAwsSession (config * AwsSessionConfig , terragruntOptions * options.TerragruntOptions ) (* session.Session , error ) {
208
+ func CreateAwsSession (config * AwsSessionConfig , opts * options.TerragruntOptions ) (* session.Session , error ) {
199
209
var (
200
210
sess * session.Session
201
211
err error
@@ -211,19 +221,19 @@ func CreateAwsSession(config *AwsSessionConfig, terragruntOptions *options.Terra
211
221
212
222
sess .Handlers .Build .PushFrontNamed (addUserAgent )
213
223
214
- if terragruntOptions .IAMRoleOptions .RoleARN != "" {
215
- if terragruntOptions .IAMRoleOptions .WebIdentityToken != "" {
216
- terragruntOptions .Logger .Debugf ("Assuming role %s using WebIdentity token" , terragruntOptions .IAMRoleOptions .RoleARN )
217
- sess .Config .Credentials = getWebIdentityCredentialsFromIAMRoleOptions (sess , terragruntOptions .IAMRoleOptions )
224
+ if opts .IAMRoleOptions .RoleARN != "" {
225
+ if opts .IAMRoleOptions .WebIdentityToken != "" {
226
+ opts .Logger .Debugf ("Assuming role %s using WebIdentity token" , opts .IAMRoleOptions .RoleARN )
227
+ sess .Config .Credentials = getWebIdentityCredentialsFromIAMRoleOptions (sess , opts .IAMRoleOptions )
218
228
} else {
219
- terragruntOptions .Logger .Debugf ("Assuming role %s" , terragruntOptions .IAMRoleOptions .RoleARN )
220
- sess .Config .Credentials = getSTSCredentialsFromIAMRoleOptions (sess , terragruntOptions .IAMRoleOptions )
229
+ opts .Logger .Debugf ("Assuming role %s" , opts .IAMRoleOptions .RoleARN )
230
+ sess .Config .Credentials = getSTSCredentialsFromIAMRoleOptions (sess , opts .IAMRoleOptions )
221
231
}
222
- } else if creds := getCredentialsFromEnvs (terragruntOptions ); creds != nil {
232
+ } else if creds := getCredentialsFromEnvs (opts ); creds != nil {
223
233
sess .Config .Credentials = creds
224
234
}
225
235
} else {
226
- sess , err = CreateAwsSessionFromConfig (config , terragruntOptions )
236
+ sess , err = CreateAwsSessionFromConfig (config , opts )
227
237
if err != nil {
228
238
return nil , errors .New (err )
229
239
}
@@ -323,12 +333,7 @@ func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error)
323
333
}
324
334
325
335
// GetAWSCallerIdentity returns the AWS caller identity associated with the current set of credentials
326
- func GetAWSCallerIdentity (config * AwsSessionConfig , terragruntOptions * options.TerragruntOptions ) (sts.GetCallerIdentityOutput , error ) {
327
- sess , err := CreateAwsSession (config , terragruntOptions )
328
- if err != nil {
329
- return sts.GetCallerIdentityOutput {}, errors .New (err )
330
- }
331
-
336
+ func GetAWSCallerIdentity (sess * session.Session ) (sts.GetCallerIdentityOutput , error ) {
332
337
identity , err := sts .New (sess ).GetCallerIdentity (nil )
333
338
if err != nil {
334
339
return sts.GetCallerIdentityOutput {}, errors .New (err )
@@ -338,15 +343,15 @@ func GetAWSCallerIdentity(config *AwsSessionConfig, terragruntOptions *options.T
338
343
}
339
344
340
345
// ValidateAwsSession - Validate if current AWS session is valid
341
- func ValidateAwsSession (config * AwsSessionConfig , terragruntOptions * options. TerragruntOptions ) error {
346
+ func ValidateAwsSession (sess * session. Session ) error {
342
347
// read the caller identity to check if the credentials are valid
343
- _ , err := GetAWSCallerIdentity (config , terragruntOptions )
348
+ _ , err := GetAWSCallerIdentity (sess )
344
349
return err
345
350
}
346
351
347
352
// GetAWSPartition gets the AWS Partition of the current session configuration
348
- func GetAWSPartition (config * AwsSessionConfig , terragruntOptions * options. TerragruntOptions ) (string , error ) {
349
- identity , err := GetAWSCallerIdentity (config , terragruntOptions )
353
+ func GetAWSPartition (sess * session. Session ) (string , error ) {
354
+ identity , err := GetAWSCallerIdentity (sess )
350
355
if err != nil {
351
356
return "" , errors .New (err )
352
357
}
@@ -361,12 +366,7 @@ func GetAWSPartition(config *AwsSessionConfig, terragruntOptions *options.Terrag
361
366
362
367
// GetAWSAccountAlias gets the AWS account Alias of the current session configuration,
363
368
// if there is no alias an empty string is return.
364
- func GetAWSAccountAlias (config * AwsSessionConfig , terragruntOptions * options.TerragruntOptions ) (string , error ) {
365
- sess , err := CreateAwsSession (config , terragruntOptions )
366
- if err != nil {
367
- return "" , errors .New (err )
368
- }
369
-
369
+ func GetAWSAccountAlias (sess * session.Session ) (string , error ) {
370
370
aliases , err := iam .New (sess ).ListAccountAliases (nil )
371
371
if err != nil {
372
372
return "" , errors .New (err )
@@ -385,8 +385,8 @@ func GetAWSAccountAlias(config *AwsSessionConfig, terragruntOptions *options.Ter
385
385
}
386
386
387
387
// GetAWSAccountID gets the AWS account ID of the current session configuration.
388
- func GetAWSAccountID (config * AwsSessionConfig , terragruntOptions * options. TerragruntOptions ) (string , error ) {
389
- identity , err := GetAWSCallerIdentity (config , terragruntOptions )
388
+ func GetAWSAccountID (sess * session. Session ) (string , error ) {
389
+ identity , err := GetAWSCallerIdentity (sess )
390
390
if err != nil {
391
391
return "" , errors .New (err )
392
392
}
@@ -395,8 +395,8 @@ func GetAWSAccountID(config *AwsSessionConfig, terragruntOptions *options.Terrag
395
395
}
396
396
397
397
// GetAWSIdentityArn gets the ARN of the AWS identity associated with the current set of credentials.
398
- func GetAWSIdentityArn (config * AwsSessionConfig , terragruntOptions * options. TerragruntOptions ) (string , error ) {
399
- identity , err := GetAWSCallerIdentity (config , terragruntOptions )
398
+ func GetAWSIdentityArn (sess * session. Session ) (string , error ) {
399
+ identity , err := GetAWSCallerIdentity (sess )
400
400
if err != nil {
401
401
return "" , errors .New (err )
402
402
}
@@ -405,11 +405,23 @@ func GetAWSIdentityArn(config *AwsSessionConfig, terragruntOptions *options.Terr
405
405
}
406
406
407
407
// GetAWSUserID gets the AWS user ID of the current session configuration.
408
- func GetAWSUserID (config * AwsSessionConfig , terragruntOptions * options. TerragruntOptions ) (string , error ) {
409
- identity , err := GetAWSCallerIdentity (config , terragruntOptions )
408
+ func GetAWSUserID (sess * session. Session ) (string , error ) {
409
+ identity , err := GetAWSCallerIdentity (sess )
410
410
if err != nil {
411
411
return "" , errors .New (err )
412
412
}
413
413
414
414
return * identity .UserId , nil
415
415
}
416
+
417
+ func ValidatePublicAccessBlock (output * s3.GetPublicAccessBlockOutput ) (bool , error ) {
418
+ if output .PublicAccessBlockConfiguration == nil {
419
+ return false , nil
420
+ }
421
+
422
+ if ! aws .BoolValue (output .PublicAccessBlockConfiguration .BlockPublicAcls ) {
423
+ return false , nil
424
+ }
425
+
426
+ return true , nil
427
+ }
0 commit comments