forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_state_s3.go
663 lines (532 loc) · 25.8 KB
/
remote_state_s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package remote
import (
"fmt"
"reflect"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/gruntwork-io/terragrunt/aws_helper"
"github.com/gruntwork-io/terragrunt/dynamodb"
"github.com/gruntwork-io/terragrunt/errors"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/shell"
"github.com/gruntwork-io/terragrunt/util"
"github.com/mitchellh/mapstructure"
)
/*
* We use this construct to separate the two config keys 's3_bucket_tags' and 'dynamodb_table_tags'
* from the others, as they are specific to the s3 backend, but only used by terragrunt to tag
* the s3 bucket and the dynamo db, in case it has to create them.
*/
type ExtendedRemoteStateConfigS3 struct {
remoteStateConfigS3 RemoteStateConfigS3
S3BucketTags map[string]string `mapstructure:"s3_bucket_tags"`
DynamotableTags map[string]string `mapstructure:"dynamodb_table_tags"`
SkipBucketVersioning bool `mapstructure:"skip_bucket_versioning"`
SkipBucketSSEncryption bool `mapstructure:"skip_bucket_ssencryption"`
SkipBucketAccessLogging bool `mapstructure:"skip_bucket_accesslogging"`
EnableLockTableSSEncryption bool `mapstructure:"enable_lock_table_ssencryption"`
}
// These are settings that can appear in the remote_state config that are ONLY used by Terragrunt and NOT forwarded
// to the underlying Terraform backend configuration
var terragruntOnlyConfigs = []string{
"s3_bucket_tags",
"dynamodb_table_tags",
"skip_bucket_versioning",
"skip_bucket_ssencryption",
"skip_bucket_accesslogging",
"enable_lock_table_ssencryption",
}
// A representation of the configuration options available for S3 remote state
type RemoteStateConfigS3 struct {
Encrypt bool `mapstructure:"encrypt"`
Bucket string `mapstructure:"bucket"`
Key string `mapstructure:"key"`
Region string `mapstructure:"region"`
Endpoint string `mapstructure:"endpoint"`
Profile string `mapstructure:"profile"`
RoleArn string `mapstructure:"role_arn"`
LockTable string `mapstructure:"lock_table"`
DynamoDBTable string `mapstructure:"dynamodb_table"`
CredsFilename string `mapstructure:"shared_credentials_file"`
S3ForcePathStyle bool `mapstructure:"force_path_style"`
}
// Builds a session config for AWS related requests from the RemoteStateConfigS3 configuration
func (c *RemoteStateConfigS3) GetAwsSessionConfig() *aws_helper.AwsSessionConfig {
return &aws_helper.AwsSessionConfig{
Region: c.Region,
CustomS3Endpoint: c.Endpoint,
Profile: c.Profile,
RoleArn: c.RoleArn,
CredsFilename: c.CredsFilename,
S3ForcePathStyle: c.S3ForcePathStyle,
}
}
// The DynamoDB lock table name used to be called lock_table, but has since been renamed to dynamodb_table, and the old
// name deprecated. To maintain backwards compatibility, we support both names.
func (s3Config *RemoteStateConfigS3) GetLockTableName() string {
if s3Config.DynamoDBTable != "" {
return s3Config.DynamoDBTable
}
return s3Config.LockTable
}
const MAX_RETRIES_WAITING_FOR_S3_BUCKET = 12
const SLEEP_BETWEEN_RETRIES_WAITING_FOR_S3_BUCKET = 5 * time.Second
// To enable access logging in an S3 bucket, you must grant WRITE and READ_ACP permissions to the Log Delivery Group,
// which is represented by the following URI. For more info, see:
// https://docs.aws.amazon.com/AmazonS3/latest/dev/enable-logging-programming.html
const s3LogDeliveryGranteeUri = "http://acs.amazonaws.com/groups/s3/LogDelivery"
type S3Initializer struct{}
// Returns true if:
//
// 1. Any of the existing backend settings are different than the current config
// 2. The configured S3 bucket or DynamoDB table does not exist
func (s3Initializer S3Initializer) NeedsInitialization(remoteState *RemoteState, existingBackend *TerraformBackend, terragruntOptions *options.TerragruntOptions) (bool, error) {
if remoteState.DisableInit {
return false, nil
}
if !configValuesEqual(remoteState.Config, existingBackend, terragruntOptions) {
return true, nil
}
s3Config, err := parseS3Config(remoteState.Config)
if err != nil {
return false, err
}
sessionConfig := s3Config.GetAwsSessionConfig()
s3Client, err := CreateS3Client(sessionConfig, terragruntOptions)
if err != nil {
return false, err
}
if !DoesS3BucketExist(s3Client, s3Config) {
return true, nil
}
if s3Config.GetLockTableName() != "" {
dynamodbClient, err := dynamodb.CreateDynamoDbClient(sessionConfig, terragruntOptions)
if err != nil {
return false, err
}
tableExists, err := dynamodb.LockTableExistsAndIsActive(s3Config.GetLockTableName(), dynamodbClient)
if err != nil {
return false, err
}
if !tableExists {
return true, nil
}
}
return false, nil
}
// Return true if the given config is in any way different than what is configured for the backend
func configValuesEqual(config map[string]interface{}, existingBackend *TerraformBackend, terragruntOptions *options.TerragruntOptions) bool {
if existingBackend == nil {
return len(config) == 0
}
if existingBackend.Type != "s3" {
terragruntOptions.Logger.Printf("Backend type has changed from s3 to %s", existingBackend.Type)
return false
}
if len(config) == 0 && len(existingBackend.Config) == 0 {
return true
}
// Terraform's `backend` configuration uses a boolean for the `encrypt` parameter. However, perhaps for backwards compatibility reasons,
// Terraform stores that parameter as a string in the `terraform.tfstate` file. Therefore, we have to convert it accordingly, or `DeepEqual`
// will fail.
if util.KindOf(existingBackend.Config["encrypt"]) == reflect.String && util.KindOf(config["encrypt"]) == reflect.Bool {
// If encrypt in remoteState is a bool and a string in existingBackend, DeepEqual will consider the maps to be different.
// So we convert the value from string to bool to make them equivalent.
if value, err := strconv.ParseBool(existingBackend.Config["encrypt"].(string)); err == nil {
existingBackend.Config["encrypt"] = value
} else {
terragruntOptions.Logger.Printf("Remote state configuration encrypt contains invalid value %v, should be boolean.", existingBackend.Config["encrypt"])
}
}
// If other keys in config are bools, DeepEqual also will consider the maps to be different.
for key, value := range existingBackend.Config {
if util.KindOf(existingBackend.Config[key]) == reflect.String && util.KindOf(config[key]) == reflect.Bool {
if convertedValue, err := strconv.ParseBool(value.(string)); err == nil {
existingBackend.Config[key] = convertedValue
}
}
}
// Delete custom S3 and DynamoDB tags that are only used in Terragrunt config and not in Terraform's backend
for _, key := range terragruntOnlyConfigs {
delete(config, key)
}
if !terraformStateConfigEqual(existingBackend.Config, config) {
terragruntOptions.Logger.Printf("Backend config has changed from %s to %s", existingBackend.Config, config)
return false
}
return true
}
// Initialize the remote state S3 bucket specified in the given config. This function will validate the config
// parameters, create the S3 bucket if it doesn't already exist, and check that versioning is enabled.
func (s3Initializer S3Initializer) Initialize(remoteState *RemoteState, terragruntOptions *options.TerragruntOptions) error {
s3ConfigExtended, err := parseExtendedS3Config(remoteState.Config)
if err != nil {
return err
}
if err := validateS3Config(s3ConfigExtended, terragruntOptions); err != nil {
return err
}
var s3Config = s3ConfigExtended.remoteStateConfigS3
s3Client, err := CreateS3Client(s3Config.GetAwsSessionConfig(), terragruntOptions)
if err != nil {
return err
}
if err := createS3BucketIfNecessary(s3Client, s3ConfigExtended, terragruntOptions); err != nil {
return err
}
if !s3ConfigExtended.SkipBucketVersioning {
if err := checkIfVersioningEnabled(s3Client, &s3Config, terragruntOptions); err != nil {
return err
}
}
if err := createLockTableIfNecessary(&s3Config, s3ConfigExtended.DynamotableTags, terragruntOptions); err != nil {
return err
}
if err := UpdateLockTableSetSSEncryptionOnIfNecessary(&s3Config, s3ConfigExtended, terragruntOptions); err != nil {
return err
}
return nil
}
func (s3Initializer S3Initializer) GetTerraformInitArgs(config map[string]interface{}) map[string]interface{} {
var filteredConfig = make(map[string]interface{})
for key, val := range config {
if util.ListContainsElement(terragruntOnlyConfigs, key) {
continue
}
filteredConfig[key] = val
}
return filteredConfig
}
// Parse the given map into an S3 config
func parseS3Config(config map[string]interface{}) (*RemoteStateConfigS3, error) {
var s3Config RemoteStateConfigS3
if err := mapstructure.Decode(config, &s3Config); err != nil {
return nil, errors.WithStackTrace(err)
}
return &s3Config, nil
}
// Parse the given map into an extended S3 config
func parseExtendedS3Config(config map[string]interface{}) (*ExtendedRemoteStateConfigS3, error) {
var s3Config RemoteStateConfigS3
var extendedConfig ExtendedRemoteStateConfigS3
if err := mapstructure.Decode(config, &s3Config); err != nil {
return nil, errors.WithStackTrace(err)
}
if err := mapstructure.Decode(config, &extendedConfig); err != nil {
return nil, errors.WithStackTrace(err)
}
extendedConfig.remoteStateConfigS3 = s3Config
return &extendedConfig, nil
}
// Validate all the parameters of the given S3 remote state configuration
func validateS3Config(extendedConfig *ExtendedRemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
var config = extendedConfig.remoteStateConfigS3
if config.Region == "" {
return errors.WithStackTrace(MissingRequiredS3RemoteStateConfig("region"))
}
if config.Bucket == "" {
return errors.WithStackTrace(MissingRequiredS3RemoteStateConfig("bucket"))
}
if config.Key == "" {
return errors.WithStackTrace(MissingRequiredS3RemoteStateConfig("key"))
}
if !config.Encrypt {
terragruntOptions.Logger.Printf("WARNING: encryption is not enabled on the S3 remote state bucket %s. Terraform state files may contain secrets, so we STRONGLY recommend enabling encryption!", config.Bucket)
}
return nil
}
// If the bucket specified in the given config doesn't already exist, prompt the user to create it, and if the user
// confirms, create the bucket and enable versioning for it.
func createS3BucketIfNecessary(s3Client *s3.S3, config *ExtendedRemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
if !DoesS3BucketExist(s3Client, &config.remoteStateConfigS3) {
prompt := fmt.Sprintf("Remote state S3 bucket %s does not exist or you don't have permissions to access it. Would you like Terragrunt to create it?", config.remoteStateConfigS3.Bucket)
shouldCreateBucket, err := shell.PromptUserForYesNo(prompt, terragruntOptions)
if err != nil {
return err
}
if shouldCreateBucket {
// Creating the S3 bucket occasionally fails with eventual consistency errors: e.g., the S3 HeadBucket
// operation says the bucket exists, but a subsequent call to enable versioning on that bucket fails with
// the error "NoSuchBucket: The specified bucket does not exist." Therefore, when creating and configuring
// the S3 bucket, we do so in a retry loop with a sleep between retries that will hopefully work around the
// eventual consistency issues. Each S3 operation should be idempotent, so redoing steps that have already
// been performed should be a no-op.
description := fmt.Sprintf("Create S3 bucket %s", config.remoteStateConfigS3.Bucket)
maxRetries := 3
sleepBetweenRetries := 10 * time.Second
return util.DoWithRetry(description, maxRetries, sleepBetweenRetries, terragruntOptions.Logger, func() error {
return CreateS3BucketWithVersioningSSEncryptionAndAccessLogging(s3Client, config, terragruntOptions)
})
}
}
return nil
}
// Check if versioning is enabled for the S3 bucket specified in the given config and warn the user if it is not
func checkIfVersioningEnabled(s3Client *s3.S3, config *RemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
out, err := s3Client.GetBucketVersioning(&s3.GetBucketVersioningInput{Bucket: aws.String(config.Bucket)})
if err != nil {
return errors.WithStackTrace(err)
}
// NOTE: There must be a bug in the AWS SDK since out == nil when versioning is not enabled. In the future,
// check the AWS SDK for updates to see if we can remove "out == nil ||".
if out == nil || out.Status == nil || *out.Status != s3.BucketVersioningStatusEnabled {
terragruntOptions.Logger.Printf("WARNING: Versioning is not enabled for the remote state S3 bucket %s. We recommend enabling versioning so that you can roll back to previous versions of your Terraform state in case of error.", config.Bucket)
}
return nil
}
// Create the given S3 bucket and enable versioning for it
func CreateS3BucketWithVersioningSSEncryptionAndAccessLogging(s3Client *s3.S3, config *ExtendedRemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
err := CreateS3Bucket(s3Client, &config.remoteStateConfigS3, terragruntOptions)
if err != nil {
if isBucketAlreadyOwnedByYourError(err) {
terragruntOptions.Logger.Printf("Looks like someone is creating bucket %s at the same time. Will not attempt to create it again.", config.remoteStateConfigS3.Bucket)
return WaitUntilS3BucketExists(s3Client, &config.remoteStateConfigS3, terragruntOptions)
}
return err
}
if err := WaitUntilS3BucketExists(s3Client, &config.remoteStateConfigS3, terragruntOptions); err != nil {
return err
}
if err := EnablePublicAccessBlockingForS3Bucket(s3Client, &config.remoteStateConfigS3, terragruntOptions); err != nil {
return err
}
if err := TagS3Bucket(s3Client, config, terragruntOptions); err != nil {
return err
}
if config.SkipBucketVersioning {
terragruntOptions.Logger.Printf("Versioning is disabled for the remote state S3 bucket %s using 'skip_bucket_versioning' config.", config.remoteStateConfigS3.Bucket)
} else if err := EnableVersioningForS3Bucket(s3Client, &config.remoteStateConfigS3, terragruntOptions); err != nil {
return err
}
if config.SkipBucketSSEncryption {
terragruntOptions.Logger.Printf("Server-Side Encryption is disabled for the remote state AWS S3 bucket %s using 'skip_bucket_ssencryption' config.", config.remoteStateConfigS3.Bucket)
} else if err := EnableSSEForS3BucketWide(s3Client, &config.remoteStateConfigS3, terragruntOptions); err != nil {
return err
}
if config.SkipBucketAccessLogging {
terragruntOptions.Logger.Printf("Access Logging is disabled for the remote state AWS S3 bucket %s using 'skip_bucket_accesslogging' config.", config.remoteStateConfigS3.Bucket)
} else if err := EnableAccessLoggingForS3BucketWide(s3Client, &config.remoteStateConfigS3, terragruntOptions); err != nil {
return err
}
return nil
}
func TagS3Bucket(s3Client *s3.S3, config *ExtendedRemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
if config.S3BucketTags == nil || len(config.S3BucketTags) == 0 {
terragruntOptions.Logger.Printf("No tags specified for bucket %s.", config.remoteStateConfigS3.Bucket)
return nil
}
// There must be one entry in the list
var tagsConverted = convertTags(config.S3BucketTags)
terragruntOptions.Logger.Printf("Tagging S3 bucket with %s", config.S3BucketTags)
putBucketTaggingInput := s3.PutBucketTaggingInput{
Bucket: aws.String(config.remoteStateConfigS3.Bucket),
Tagging: &s3.Tagging{
TagSet: tagsConverted}}
_, err := s3Client.PutBucketTagging(&putBucketTaggingInput)
if err != nil {
return errors.WithStackTrace(err)
}
return nil
}
func convertTags(tags map[string]string) []*s3.Tag {
var tagsConverted []*s3.Tag
for k, v := range tags {
var tag = s3.Tag{
Key: aws.String(k),
Value: aws.String(v)}
tagsConverted = append(tagsConverted, &tag)
}
return tagsConverted
}
// AWS is eventually consistent, so after creating an S3 bucket, this method can be used to wait until the information
// about that S3 bucket has propagated everywhere
func WaitUntilS3BucketExists(s3Client *s3.S3, config *RemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
terragruntOptions.Logger.Printf("Waiting for bucket %s to be created", config.Bucket)
for retries := 0; retries < MAX_RETRIES_WAITING_FOR_S3_BUCKET; retries++ {
if DoesS3BucketExist(s3Client, config) {
terragruntOptions.Logger.Printf("S3 bucket %s created.", config.Bucket)
return nil
} else if retries < MAX_RETRIES_WAITING_FOR_S3_BUCKET-1 {
terragruntOptions.Logger.Printf("S3 bucket %s has not been created yet. Sleeping for %s and will check again.", config.Bucket, SLEEP_BETWEEN_RETRIES_WAITING_FOR_S3_BUCKET)
time.Sleep(SLEEP_BETWEEN_RETRIES_WAITING_FOR_S3_BUCKET)
}
}
return errors.WithStackTrace(MaxRetriesWaitingForS3BucketExceeded(config.Bucket))
}
// Create the S3 bucket specified in the given config
func CreateS3Bucket(s3Client *s3.S3, config *RemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
terragruntOptions.Logger.Printf("Creating S3 bucket %s", config.Bucket)
_, err := s3Client.CreateBucket(&s3.CreateBucketInput{Bucket: aws.String(config.Bucket)})
return errors.WithStackTrace(err)
}
// Determine if this is an error that implies you've already made a request to create the S3 bucket and it succeeded
// or is in progress. This usually happens when running many tests in parallel or xxx-all commands.
func isBucketAlreadyOwnedByYourError(err error) bool {
awsErr, isAwsErr := errors.Unwrap(err).(awserr.Error)
return isAwsErr && (awsErr.Code() == "BucketAlreadyOwnedByYou" || awsErr.Code() == "OperationAborted")
}
// Enable versioning for the S3 bucket specified in the given config
func EnableVersioningForS3Bucket(s3Client *s3.S3, config *RemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
terragruntOptions.Logger.Printf("Enabling versioning on S3 bucket %s", config.Bucket)
input := s3.PutBucketVersioningInput{
Bucket: aws.String(config.Bucket),
VersioningConfiguration: &s3.VersioningConfiguration{Status: aws.String(s3.BucketVersioningStatusEnabled)},
}
_, err := s3Client.PutBucketVersioning(&input)
return errors.WithStackTrace(err)
}
// Enable bucket-wide Server-Side Encryption for the AWS S3 bucket specified in the given config
func EnableSSEForS3BucketWide(s3Client *s3.S3, config *RemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
terragruntOptions.Logger.Printf("Enabling bucket-wide SSE on AWS S3 bucket %s", config.Bucket)
// Encrypt with KMS by default
defEnc := &s3.ServerSideEncryptionByDefault{KMSMasterKeyID: aws.String("aws/s3"), SSEAlgorithm: aws.String(s3.ServerSideEncryptionAwsKms)}
rule := &s3.ServerSideEncryptionRule{ApplyServerSideEncryptionByDefault: defEnc}
rules := []*s3.ServerSideEncryptionRule{rule}
serverConfig := &s3.ServerSideEncryptionConfiguration{Rules: rules}
input := &s3.PutBucketEncryptionInput{Bucket: aws.String(config.Bucket), ServerSideEncryptionConfiguration: serverConfig}
_, err := s3Client.PutBucketEncryption(input)
return errors.WithStackTrace(err)
}
// Enable bucket-wide Access Logging for the AWS S3 bucket specified in the given config
func EnableAccessLoggingForS3BucketWide(s3Client *s3.S3, config *RemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
if err := configureBucketAccessLoggingAcl(s3Client, config, terragruntOptions); err != nil {
return errors.WithStackTrace(err)
}
terragruntOptions.Logger.Printf("Enabling bucket-wide Access Logging on AWS S3 bucket \"%s\" - using as TargetBucket \"%s\"", config.Bucket, config.Bucket)
loggingInput := s3.PutBucketLoggingInput{
Bucket: aws.String(config.Bucket),
BucketLoggingStatus: &s3.BucketLoggingStatus{
LoggingEnabled: &s3.LoggingEnabled{
TargetBucket: aws.String(config.Bucket),
TargetPrefix: aws.String("TFStateLogs/"),
},
},
}
if _, err := s3Client.PutBucketLogging(&loggingInput); err != nil {
return errors.WithStackTrace(err)
}
return nil
}
// Block all public access policies on the bucket and objects. These settings ensure that a misconfiguration of the
// bucket or objects will not accidentally enable public access to those items. See
// https://docs.aws.amazon.com/AmazonS3/latest/dev/access-control-block-public-access.html for more information.
func EnablePublicAccessBlockingForS3Bucket(s3Client *s3.S3, config *RemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
terragruntOptions.Logger.Printf("Blocking all public access to S3 bucket %s", config.Bucket)
_, err := s3Client.PutPublicAccessBlock(
&s3.PutPublicAccessBlockInput{
Bucket: aws.String(config.Bucket),
PublicAccessBlockConfiguration: &s3.PublicAccessBlockConfiguration{
BlockPublicAcls: aws.Bool(true),
BlockPublicPolicy: aws.Bool(true),
IgnorePublicAcls: aws.Bool(true),
RestrictPublicBuckets: aws.Bool(true),
},
},
)
return errors.WithStackTrace(err)
}
// To enable access logging in an S3 bucket, you must grant WRITE and READ_ACP permissions to the Log Delivery
// Group. For more info, see:
// https://docs.aws.amazon.com/AmazonS3/latest/dev/enable-logging-programming.html
func configureBucketAccessLoggingAcl(s3Client *s3.S3, config *RemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
terragruntOptions.Logger.Printf("Granting WRITE and READ_ACP permissions to S3 Log Delivery (%s) for bucket %s. This is required for access logging.", s3LogDeliveryGranteeUri, config.Bucket)
uri := fmt.Sprintf("uri=%s", s3LogDeliveryGranteeUri)
aclInput := s3.PutBucketAclInput{
Bucket: aws.String(config.Bucket),
GrantWrite: aws.String(uri),
GrantReadACP: aws.String(uri),
}
if _, err := s3Client.PutBucketAcl(&aclInput); err != nil {
return errors.WithStackTrace(err)
}
return waitUntilBucketHasAccessLoggingAcl(s3Client, config, terragruntOptions)
}
func waitUntilBucketHasAccessLoggingAcl(s3Client *s3.S3, config *RemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
terragruntOptions.Logger.Printf("Waiting for ACL bucket %s to have the updated ACL for access logging.", config.Bucket)
maxRetries := 10
timeBetweenRetries := 5 * time.Second
for i := 0; i < maxRetries; i++ {
out, err := s3Client.GetBucketAcl(&s3.GetBucketAclInput{Bucket: aws.String(config.Bucket)})
if err != nil {
return errors.WithStackTrace(err)
}
hasReadAcp := false
hasWrite := false
for _, grant := range out.Grants {
if aws.StringValue(grant.Grantee.URI) == s3LogDeliveryGranteeUri {
if aws.StringValue(grant.Permission) == s3.PermissionReadAcp {
hasReadAcp = true
}
if aws.StringValue(grant.Permission) == s3.PermissionWrite {
hasWrite = true
}
}
}
if hasReadAcp && hasWrite {
terragruntOptions.Logger.Printf("Bucket %s now has the proper ACL permissions for access logging!", config.Bucket)
return nil
}
terragruntOptions.Logger.Printf("Bucket %s still does not have the ACL permissions for access logging. Will sleep for %v and check again.", config.Bucket, timeBetweenRetries)
time.Sleep(timeBetweenRetries)
}
return errors.WithStackTrace(MaxRetriesWaitingForS3ACLExceeded(config.Bucket))
}
// Returns true if the S3 bucket specified in the given config exists and the current user has the ability to access
// it.
func DoesS3BucketExist(s3Client *s3.S3, config *RemoteStateConfigS3) bool {
_, err := s3Client.HeadBucket(&s3.HeadBucketInput{Bucket: aws.String(config.Bucket)})
return err == nil
}
// Create a table for locks in DynamoDB if the user has configured a lock table and the table doesn't already exist
func createLockTableIfNecessary(s3Config *RemoteStateConfigS3, tags map[string]string, terragruntOptions *options.TerragruntOptions) error {
if s3Config.GetLockTableName() == "" {
return nil
}
dynamodbClient, err := dynamodb.CreateDynamoDbClient(s3Config.GetAwsSessionConfig(), terragruntOptions)
if err != nil {
return err
}
return dynamodb.CreateLockTableIfNecessary(s3Config.GetLockTableName(), tags, dynamodbClient, terragruntOptions)
}
// Update a table for locks in DynamoDB if the user has configured a lock table and the table's server-side encryption isn't turned on
func UpdateLockTableSetSSEncryptionOnIfNecessary(s3Config *RemoteStateConfigS3, config *ExtendedRemoteStateConfigS3, terragruntOptions *options.TerragruntOptions) error {
if !config.EnableLockTableSSEncryption {
return nil
}
if s3Config.GetLockTableName() == "" {
return nil
}
dynamodbClient, err := dynamodb.CreateDynamoDbClient(s3Config.GetAwsSessionConfig(), terragruntOptions)
if err != nil {
return err
}
return dynamodb.UpdateLockTableSetSSEncryptionOnIfNecessary(s3Config.GetLockTableName(), dynamodbClient, terragruntOptions)
}
// Create an authenticated client for DynamoDB
func CreateS3Client(config *aws_helper.AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (*s3.S3, error) {
session, err := aws_helper.CreateAwsSession(config, terragruntOptions)
if err != nil {
return nil, err
}
return s3.New(session), nil
}
// Custom error types
type MissingRequiredS3RemoteStateConfig string
func (configName MissingRequiredS3RemoteStateConfig) Error() string {
return fmt.Sprintf("Missing required S3 remote state configuration %s", string(configName))
}
type MultipleTagsDeclarations string
func (target MultipleTagsDeclarations) Error() string {
return fmt.Sprintf("Tags for %s got declared multiple times. Please do only declare in one block.", string(target))
}
type MaxRetriesWaitingForS3BucketExceeded string
func (err MaxRetriesWaitingForS3BucketExceeded) Error() string {
return fmt.Sprintf("Exceeded max retries (%d) waiting for bucket S3 bucket %s", MAX_RETRIES_WAITING_FOR_S3_BUCKET, string(err))
}
type MaxRetriesWaitingForS3ACLExceeded string
func (err MaxRetriesWaitingForS3ACLExceeded) Error() string {
return fmt.Sprintf("Exceeded max retries waiting for bucket S3 bucket %s to have proper ACL for access logging", string(err))
}