Skip to content

Commit 73a972c

Browse files
committed
S3 Logging add option to disable ACL setup
1 parent 7862e9e commit 73a972c

File tree

3 files changed

+128
-3
lines changed

3 files changed

+128
-3
lines changed

plugins/modules/s3_logging.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
- "The prefix that should be prepended to the generated log files written to the target_bucket."
3535
default: ""
3636
type: str
37+
acl:
38+
description:
39+
- "Setup target bucket ACLs to grant AWS special log delivery account to write server access logs."
40+
- "Setting to False will remove the ACL for log delivery on the target bucket."
41+
default: True
42+
type: bool
43+
version_added: 8.3.0
3744
extends_documentation_fragment:
3845
- amazon.aws.common.modules
3946
- amazon.aws.region.modules
@@ -95,23 +102,37 @@ def verify_acls(connection, module, target_bucket):
95102
botocore.exceptions.BotoCoreError,
96103
botocore.exceptions.ClientError,
97104
) as e: # pylint: disable=duplicate-except
105+
if not module.params.get("acl"):
106+
module.warn(f"Unable to fetch Bucket ACLs ({e})")
107+
return False
98108
module.fail_json_aws(e, msg="Failed to fetch target bucket ACL")
99109

100110
required_grant = {
101111
"Grantee": {"URI": "http://acs.amazonaws.com/groups/s3/LogDelivery", "Type": "Group"},
102112
"Permission": "FULL_CONTROL",
103113
}
104114

115+
grant_present = False
105116
for grant in current_grants:
106117
if grant == required_grant:
107-
return False
118+
grant_present = True
119+
120+
if module.params.get("acl") == grant_present:
121+
return False
108122

109123
if module.check_mode:
110124
return True
111125

112126
updated_acl = dict(current_acl)
113-
updated_grants = list(current_grants)
114-
updated_grants.append(required_grant)
127+
updated_grants = []
128+
if module.params.get("acl"):
129+
updated_grants = list(current_grants)
130+
updated_grants.append(required_grant)
131+
else:
132+
for grant in current_grants:
133+
if grant != required_grant:
134+
updated_grants.append(grant)
135+
115136
updated_acl["Grants"] = updated_grants
116137
del updated_acl["ResponseMetadata"]
117138
try:
@@ -197,6 +218,7 @@ def main():
197218
target_bucket=dict(required=False, default=None),
198219
target_prefix=dict(required=False, default=""),
199220
state=dict(required=False, default="present", choices=["present", "absent"]),
221+
acl=dict(type="bool", default=True),
200222
)
201223

202224
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)

tests/integration/targets/s3_logging/defaults/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
test_bucket: '{{ tiny_prefix }}-s3-logging'
33
log_bucket_1: '{{ tiny_prefix }}-logs-1'
44
log_bucket_2: '{{ tiny_prefix }}-logs-2'
5+
log_bucket_3: '{{ tiny_prefix }}-logs-3'

tests/integration/targets/s3_logging/tasks/main.yml

+102
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@
6464
- output is changed
6565
- output.name == log_bucket_2
6666

67+
- name: Create simple s3_bucket as third target for logs
68+
s3_bucket:
69+
state: present
70+
name: '{{ log_bucket_3 }}'
71+
object_ownership: BucketOwnerPreferred
72+
register: output
73+
- assert:
74+
that:
75+
- output is changed
76+
- output.name == log_bucket_3
77+
6778
# ============================================================
6879

6980
- name: Enable logging (check_mode)
@@ -152,6 +163,97 @@
152163
that:
153164
- result is not changed
154165

166+
# ============================================================
167+
168+
- name: Disable ACL on logging bucket (check_mode)
169+
s3_logging:
170+
state: present
171+
name: '{{ test_bucket }}'
172+
target_bucket: '{{ log_bucket_2 }}'
173+
acl: False
174+
register: result
175+
check_mode: True
176+
- assert:
177+
that:
178+
- result is changed
179+
180+
- name: Disable ACL logging bucket
181+
s3_logging:
182+
state: present
183+
name: '{{ test_bucket }}'
184+
target_bucket: '{{ log_bucket_2 }}'
185+
acl: False
186+
register: result
187+
- assert:
188+
that:
189+
- result is changed
190+
191+
- name: Disable ACL on logging bucket idempotency (check_mode)
192+
s3_logging:
193+
state: present
194+
name: '{{ test_bucket }}'
195+
target_bucket: '{{ log_bucket_2 }}'
196+
acl: False
197+
register: result
198+
check_mode: True
199+
- assert:
200+
that:
201+
- result is not changed
202+
203+
- name: Disable ACL on logging bucket idempotency
204+
s3_logging:
205+
state: present
206+
name: '{{ test_bucket }}'
207+
target_bucket: '{{ log_bucket_2 }}'
208+
acl: False
209+
register: result
210+
- assert:
211+
that:
212+
- result is not changed
213+
214+
- name: Re-Enable ACL on logging bucket (check_mode)
215+
s3_logging:
216+
state: present
217+
name: '{{ test_bucket }}'
218+
target_bucket: '{{ log_bucket_2 }}'
219+
register: result
220+
check_mode: True
221+
- assert:
222+
that:
223+
- result is changed
224+
225+
- name: Re-Enable ACL logging bucket
226+
s3_logging:
227+
state: present
228+
name: '{{ test_bucket }}'
229+
target_bucket: '{{ log_bucket_2 }}'
230+
register: result
231+
- assert:
232+
that:
233+
- result is changed
234+
235+
- name: Re-Enable ACL on logging bucket idempotency (check_mode)
236+
s3_logging:
237+
state: present
238+
name: '{{ test_bucket }}'
239+
target_bucket: '{{ log_bucket_2 }}'
240+
register: result
241+
check_mode: True
242+
- assert:
243+
that:
244+
- result is not changed
245+
246+
- name: Re-Enable ACL on logging bucket idempotency
247+
s3_logging:
248+
state: present
249+
name: '{{ test_bucket }}'
250+
target_bucket: '{{ log_bucket_2 }}'
251+
register: result
252+
- assert:
253+
that:
254+
- result is not changed
255+
256+
155257
# ============================================================
156258

157259
- name: Change logging prefix (check_mode)

0 commit comments

Comments
 (0)