Skip to content

Commit 26c3b26

Browse files
authored
call DeletePolicy if bucket policy is empty (#201)
Issue [#2731](aws-controllers-k8s/community#2731) Description of changes: currently, we call DeletePolicy is not nil. This causes issues if the value of the bucket policy is an empty string. With these changes, we call DeletePolicy when the bucket policy is nil or empty. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 08b50a6 commit 26c3b26

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

pkg/resource/bucket/hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ func (rm *resourceManager) syncPolicy(
12831283
ctx context.Context,
12841284
r *resource,
12851285
) (err error) {
1286-
if r.ko.Spec.Policy == nil {
1286+
if r.ko.Spec.Policy == nil || *r.ko.Spec.Policy == "" {
12871287
return rm.deletePolicy(ctx, r)
12881288
}
12891289
return rm.putPolicy(ctx, r)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: s3.services.k8s.aws/v1alpha1
2+
kind: Bucket
3+
metadata:
4+
name: $BUCKET_NAME
5+
spec:
6+
name: $BUCKET_NAME
7+
policy: ""

test/e2e/tests/test_bucket.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def test_put_fields(self, s3_client, s3_resource, basic_bucket):
147147
self._update_assert_notification(basic_bucket, s3_resource)
148148
self._update_assert_ownership_controls(basic_bucket, s3_client)
149149
self._update_assert_policy(basic_bucket, s3_resource)
150+
self._update_assert_empty_policy(basic_bucket, s3_resource)
150151
self._update_assert_public_access_block(basic_bucket, s3_client)
151152
self._update_assert_replication(basic_bucket, s3_client)
152153
self._update_assert_request_payment(basic_bucket, s3_resource)
@@ -247,6 +248,21 @@ def _update_assert_policy(self, bucket: Bucket, s3_resource):
247248

248249
assert desired == latest
249250

251+
def _update_assert_empty_policy(self, bucket: Bucket, s3_resource):
252+
replace_bucket_spec(bucket, "bucket_empty_policy")
253+
254+
latest = get_bucket(s3_resource, bucket.resource_name)
255+
policy = latest.Policy()
256+
257+
try:
258+
policy.policy
259+
except Exception as e:
260+
assert "NoSuchBucketPolicy" in str(e)
261+
return
262+
263+
# if there is no error, fail test
264+
assert False
265+
250266
def _update_assert_public_access_block(self, bucket: Bucket, s3_client):
251267
replace_bucket_spec(bucket, "bucket_public_access_block")
252268

0 commit comments

Comments
 (0)