2022-01-18 20:10:22 +00:00
|
|
|
import boto3
|
|
|
|
import pytest
|
|
|
|
from boto3 import Session
|
|
|
|
from botocore.client import ClientError
|
2023-11-30 15:55:51 +00:00
|
|
|
|
2022-02-24 21:35:07 +00:00
|
|
|
from moto import mock_s3control
|
2022-01-18 20:10:22 +00:00
|
|
|
|
|
|
|
|
2022-02-24 21:35:07 +00:00
|
|
|
@mock_s3control
|
|
|
|
def test_get_public_access_block_for_account():
|
2022-08-13 09:49:43 +00:00
|
|
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
2022-01-18 20:10:22 +00:00
|
|
|
|
2022-02-24 21:35:07 +00:00
|
|
|
client = boto3.client("s3control", region_name="us-west-2")
|
2022-01-18 20:10:22 +00:00
|
|
|
|
2022-02-24 21:35:07 +00:00
|
|
|
# With an invalid account ID:
|
2023-08-04 21:51:28 +00:00
|
|
|
with pytest.raises(ClientError) as ce_err:
|
2022-02-24 21:35:07 +00:00
|
|
|
client.get_public_access_block(AccountId="111111111111")
|
2023-08-04 21:51:28 +00:00
|
|
|
assert ce_err.value.response["Error"]["Code"] == "AccessDenied"
|
2022-01-18 20:10:22 +00:00
|
|
|
|
2022-02-24 21:35:07 +00:00
|
|
|
# Without one defined:
|
2023-08-04 21:51:28 +00:00
|
|
|
with pytest.raises(ClientError) as ce_err:
|
2022-02-24 21:35:07 +00:00
|
|
|
client.get_public_access_block(AccountId=ACCOUNT_ID)
|
2023-08-04 21:51:28 +00:00
|
|
|
assert (
|
|
|
|
ce_err.value.response["Error"]["Code"] == "NoSuchPublicAccessBlockConfiguration"
|
|
|
|
)
|
2022-01-18 20:10:22 +00:00
|
|
|
|
2022-02-24 21:35:07 +00:00
|
|
|
# Put a with an invalid account ID:
|
2023-08-04 21:51:28 +00:00
|
|
|
with pytest.raises(ClientError) as ce_err:
|
2022-02-24 21:35:07 +00:00
|
|
|
client.put_public_access_block(
|
|
|
|
AccountId="111111111111",
|
|
|
|
PublicAccessBlockConfiguration={"BlockPublicAcls": True},
|
2022-01-18 20:10:22 +00:00
|
|
|
)
|
2023-08-04 21:51:28 +00:00
|
|
|
assert ce_err.value.response["Error"]["Code"] == "AccessDenied"
|
2022-01-18 20:10:22 +00:00
|
|
|
|
2022-02-24 21:35:07 +00:00
|
|
|
# Put with an invalid PAB:
|
2023-08-04 21:51:28 +00:00
|
|
|
with pytest.raises(ClientError) as ce_err:
|
2022-01-18 20:10:22 +00:00
|
|
|
client.put_public_access_block(
|
2022-02-24 21:35:07 +00:00
|
|
|
AccountId=ACCOUNT_ID, PublicAccessBlockConfiguration={}
|
2022-01-18 20:10:22 +00:00
|
|
|
)
|
2023-08-04 21:51:28 +00:00
|
|
|
assert ce_err.value.response["Error"]["Code"] == "InvalidRequest"
|
2022-02-24 21:35:07 +00:00
|
|
|
assert (
|
|
|
|
"Must specify at least one configuration."
|
2023-08-04 21:51:28 +00:00
|
|
|
in ce_err.value.response["Error"]["Message"]
|
2022-02-24 21:35:07 +00:00
|
|
|
)
|
2022-01-18 20:10:22 +00:00
|
|
|
|
2022-02-24 21:35:07 +00:00
|
|
|
# Correct PAB:
|
|
|
|
client.put_public_access_block(
|
|
|
|
AccountId=ACCOUNT_ID,
|
|
|
|
PublicAccessBlockConfiguration={
|
|
|
|
"BlockPublicAcls": True,
|
|
|
|
"IgnorePublicAcls": True,
|
|
|
|
"BlockPublicPolicy": True,
|
|
|
|
"RestrictPublicBuckets": True,
|
|
|
|
},
|
|
|
|
)
|
2022-01-18 20:10:22 +00:00
|
|
|
|
2022-02-24 21:35:07 +00:00
|
|
|
# Get the correct PAB (for all regions):
|
|
|
|
for region in Session().get_available_regions("s3control"):
|
|
|
|
region_client = boto3.client("s3control", region_name=region)
|
|
|
|
assert region_client.get_public_access_block(AccountId=ACCOUNT_ID)[
|
|
|
|
"PublicAccessBlockConfiguration"
|
|
|
|
] == {
|
|
|
|
"BlockPublicAcls": True,
|
|
|
|
"IgnorePublicAcls": True,
|
|
|
|
"BlockPublicPolicy": True,
|
|
|
|
"RestrictPublicBuckets": True,
|
|
|
|
}
|
2022-01-18 20:10:22 +00:00
|
|
|
|
2022-02-24 21:35:07 +00:00
|
|
|
# Delete with an invalid account ID:
|
2023-08-04 21:51:28 +00:00
|
|
|
with pytest.raises(ClientError) as ce_err:
|
2022-02-24 21:35:07 +00:00
|
|
|
client.delete_public_access_block(AccountId="111111111111")
|
2023-08-04 21:51:28 +00:00
|
|
|
assert ce_err.value.response["Error"]["Code"] == "AccessDenied"
|
2022-01-18 20:10:22 +00:00
|
|
|
|
2022-02-24 21:35:07 +00:00
|
|
|
# Delete successfully:
|
|
|
|
client.delete_public_access_block(AccountId=ACCOUNT_ID)
|
|
|
|
|
|
|
|
# Confirm that it's deleted:
|
2023-08-04 21:51:28 +00:00
|
|
|
with pytest.raises(ClientError) as ce_err:
|
2022-02-24 21:35:07 +00:00
|
|
|
client.get_public_access_block(AccountId=ACCOUNT_ID)
|
2023-08-04 21:51:28 +00:00
|
|
|
assert (
|
|
|
|
ce_err.value.response["Error"]["Code"] == "NoSuchPublicAccessBlockConfiguration"
|
|
|
|
)
|