How to Check an SCP Deny List in Python
Load a JSON SCP policy file, extract the deny_list, and check if a target ARN is denied.
Python code
30 linesimport json
from pathlib import Path
def evaluate_scp_deny_list(policy_path: Path, target_path: str) -> bool:
policy = json.loads(policy_path.read_text())
deny_list = policy.get("deny_list", [])
return target_path in deny_list
if __name__ == "__main__":
policy_file = Path("scp_policy.json")
policy_file.write_text(
json.dumps({
"deny_list": [
"arn:aws:s3:::confidential-bucket/*",
"arn:aws:s3:::internal-only/*",
]
})
)
test_targets = [
"arn:aws:s3:::confidential-bucket/data.docx",
"arn:aws:s3:::public-bucket/index.html",
"arn:aws:s3:::internal-only/notes.txt",
]
for target in test_targets:
result = evaluate_scp_deny_list(policy_file, target)
print(f"{target} -> {'DENIED' if result else 'ALLOWED'}")
Output
arn:aws:s3:::confidential-bucket/data.docx -> DENIED
arn:aws:s3:::public-bucket/index.html -> ALLOWED
arn:aws:s3:::internal-only/notes.txt -> DENIED
How it works
The code reads the JSON policy from a file using Path.read_text, then parses it with json.loads. It uses .get("deny_list", []) to safely retrieve the list, defaulting to an empty list if the key is missing. The target ARN is checked with the in operator, which works because deny_list is a list of strings. This pattern is simple and effective for mock evaluations of SCP restrictions.
Common mistakes
- Assuming the policy JSON always has a deny_list key without using .get()
- Using json.load instead of json.loads on a file object
- Forgetting to handle file not found errors when reading the policy
Variations
- Use a set for deny_list to speed up lookups on large lists
- Load the policy from an environment variable string instead of a file
Real-world use cases
- Testing SCP configurations in CI/CD pipelines before deployment to AWS.
- Validating access control rules in infrastructure-as-code tools like Terraform.
- Mocking policy checks in unit tests for IAM or S3 access logic.
Sponsored
More from Cloud + Python
- Build a URL Shortener Client with Python medium
- Create a Cloud Storage Helper Class in Python easy
- Create a Data Helper Class for Beginners in Python easy
- Cross Account Role Chaining Mock Credentials in Python medium
- Exponential Backoff with Jitter for Cloud API Calls in Python medium
- Generate Mock CloudFormation Stack Events in Python easy
Keep learning
Related tutorials and quizzes for this topic.