/
resource_aws_s3_bucket_public_access_block.go
195 lines (157 loc) · 6.03 KB
/
resource_aws_s3_bucket_public_access_block.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
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/service/s3control"
"github.com/hashicorp/terraform/helper/resource"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsS3BucketPublicAccessBlock() *schema.Resource {
return &schema.Resource{
Create: resourceAwsS3BucketPublicAccessBlockCreate,
Read: resourceAwsS3BucketPublicAccessBlockRead,
Update: resourceAwsS3BucketPublicAccessBlockUpdate,
Delete: resourceAwsS3BucketPublicAccessBlockDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"block_public_acls": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"block_public_policy": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"ignore_public_acls": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"restrict_public_buckets": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceAwsS3BucketPublicAccessBlockCreate(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn
bucket := d.Get("bucket").(string)
input := &s3.PutPublicAccessBlockInput{
Bucket: aws.String(bucket),
PublicAccessBlockConfiguration: &s3.PublicAccessBlockConfiguration{
BlockPublicAcls: aws.Bool(d.Get("block_public_acls").(bool)),
BlockPublicPolicy: aws.Bool(d.Get("block_public_policy").(bool)),
IgnorePublicAcls: aws.Bool(d.Get("ignore_public_acls").(bool)),
RestrictPublicBuckets: aws.Bool(d.Get("restrict_public_buckets").(bool)),
},
}
log.Printf("[DEBUG] S3 bucket: %s, public access block: %v", bucket, input.PublicAccessBlockConfiguration)
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := s3conn.PutPublicAccessBlock(input)
if isAWSErr(err, s3.ErrCodeNoSuchBucket, "") {
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("error creating public access block policy for S3 bucket (%s): %s", bucket, err)
}
d.SetId(bucket)
return resourceAwsS3BucketPublicAccessBlockRead(d, meta)
}
func resourceAwsS3BucketPublicAccessBlockRead(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn
input := &s3.GetPublicAccessBlockInput{
Bucket: aws.String(d.Id()),
}
// Retry for eventual consistency on creation
var output *s3.GetPublicAccessBlockOutput
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
var err error
output, err = s3conn.GetPublicAccessBlock(input)
if d.IsNewResource() && (isAWSErr(err, s3control.ErrCodeNoSuchPublicAccessBlockConfiguration, "") ||
isAWSErr(err, s3.ErrCodeNoSuchBucket, "")) {
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})
if isAWSErr(err, s3control.ErrCodeNoSuchPublicAccessBlockConfiguration, "") {
log.Printf("[WARN] S3 Bucket Public Access Block (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("error reading S3 bucket Public Access Block: %s", err)
}
if output == nil || output.PublicAccessBlockConfiguration == nil {
return fmt.Errorf("error reading S3 Bucket Public Access Block (%s): missing public access block configuration", d.Id())
}
d.Set("bucket", d.Id())
d.Set("block_public_acls", output.PublicAccessBlockConfiguration.BlockPublicAcls)
d.Set("block_public_policy", output.PublicAccessBlockConfiguration.BlockPublicPolicy)
d.Set("ignore_public_acls", output.PublicAccessBlockConfiguration.IgnorePublicAcls)
d.Set("restrict_public_buckets", output.PublicAccessBlockConfiguration.RestrictPublicBuckets)
return nil
}
func resourceAwsS3BucketPublicAccessBlockUpdate(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn
input := &s3.PutPublicAccessBlockInput{
Bucket: aws.String(d.Id()),
PublicAccessBlockConfiguration: &s3.PublicAccessBlockConfiguration{
BlockPublicAcls: aws.Bool(d.Get("block_public_acls").(bool)),
BlockPublicPolicy: aws.Bool(d.Get("block_public_policy").(bool)),
IgnorePublicAcls: aws.Bool(d.Get("ignore_public_acls").(bool)),
RestrictPublicBuckets: aws.Bool(d.Get("restrict_public_buckets").(bool)),
},
}
log.Printf("[DEBUG] Updating S3 bucket Public Access Block: %s", input)
_, err := s3conn.PutPublicAccessBlock(input)
if err != nil {
return fmt.Errorf("error updating S3 Bucket Public Access Block (%s): %s", d.Id(), err)
}
// Workaround API eventual consistency issues. This type of logic should not normally be used.
// We cannot reliably determine when the Read after Update might be properly updated.
// Rather than introduce complicated retry logic, we presume that a lack of an update error
// means our update succeeded with our expected values.
d.Set("block_public_acls", input.PublicAccessBlockConfiguration.BlockPublicAcls)
d.Set("block_public_policy", input.PublicAccessBlockConfiguration.BlockPublicPolicy)
d.Set("ignore_public_acls", input.PublicAccessBlockConfiguration.IgnorePublicAcls)
d.Set("restrict_public_buckets", input.PublicAccessBlockConfiguration.RestrictPublicBuckets)
// Skip normal Read after Update due to eventual consistency issues
return nil
}
func resourceAwsS3BucketPublicAccessBlockDelete(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn
input := &s3.DeletePublicAccessBlockInput{
Bucket: aws.String(d.Id()),
}
log.Printf("[DEBUG] S3 bucket: %s, delete public access block", d.Id())
_, err := s3conn.DeletePublicAccessBlock(input)
if isAWSErr(err, s3control.ErrCodeNoSuchPublicAccessBlockConfiguration, "") {
return nil
}
if err != nil {
return fmt.Errorf("error deleting S3 Bucket Public Access Block (%s): %s", d.Id(), err)
}
return nil
}