Skip to content

Commit 32e8705

Browse files
authored
Fix delete bucket access (#712)
* Test for bug * Fix revoking access to buckets Revoking access to an S3 bucket was failing because the code that updates the IAM policy was not called due to a missing Django signal connection. Since we do not revoke access in bulk, Signals are not needed and make the code less readable.
1 parent ac6a91c commit 32e8705

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

controlpanel/api/models/access_to_s3bucket.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def save(self, *args, **kwargs):
4242

4343
return self
4444

45+
def delete(self, *args, **kwargs):
46+
with S3AccessPolicy.load(self.aws_role_name()) as policy:
47+
policy.revoke_access(self.s3bucket.arn)
4548

46-
def _access_to_bucket_deleted(sender, **kwargs):
47-
access_to_bucket = kwargs['instance']
49+
super().delete(*args, **kwargs)
4850

49-
with S3AccessPolicy.load(access_to_bucket.aws_role_name()) as policy:
50-
policy.revoke_access(access_to_bucket.s3bucket.arn)

tests/api/models/test_apps3bucket.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ def revoke_bucket_access():
2323
yield p
2424

2525

26+
@pytest.yield_fixture
27+
def s3_access_policy():
28+
with patch('controlpanel.api.models.access_to_s3bucket.S3AccessPolicy') as S3AccessPolicy:
29+
yield S3AccessPolicy
30+
31+
2632
@pytest.mark.django_db
2733
def test_one_record_per_app_per_s3bucket(app, bucket):
2834
# Give app access to bucket (read-only)
@@ -67,7 +73,7 @@ def test_aws_create(app, bucket, aws):
6773

6874

6975
@pytest.mark.django_db
70-
def test_delete_revoke_permissions(app, bucket, aws):
76+
def test_delete_revoke_permissions(app, bucket, s3_access_policy):
7177
apps3bucket = mommy.make(
7278
'api.AppS3Bucket',
7379
app=app,
@@ -77,5 +83,6 @@ def test_delete_revoke_permissions(app, bucket, aws):
7783

7884
apps3bucket.delete()
7985

80-
aws.put_role_policy.assert_called()
81-
# TODO get policy from call and assert ARN removed
86+
s3_access_policy.load.assert_called_with(apps3bucket.aws_role_name())
87+
policy = s3_access_policy.load.return_value.__enter__.return_value
88+
policy.revoke_access.assert_called_with(bucket.arn)

0 commit comments

Comments
 (0)