How to Evaluate Mock NACL Rules in Python
Simulate numbered AWS Network ACL rule evaluation with HMAC integrity checks on request payloads.
Python code
39 linesimport base64
import json
import hmac
import hashlib
def evaluate_mock_rule(rule_number, request_data, secret):
"""
Simulates evaluating an NACL-like numbered rule by:
1. Checking if the rule number exists in the mock policy.
2. Computing an HMAC over the request payload for integrity.
"""
# Mock NACL policy: rule number -> (action, source, destination)
mock_policy = {
100: ("ALLOW", "10.0.0.0/8", "any"),
200: ("DENY", "192.168.1.0/24", "any"),
300: ("ALLOW", "any", "0.0.0.0/0")
}
if rule_number not in mock_policy:
return {"rule": rule_number, "status": "NOT_FOUND", "matched": False}
action, source, destination = mock_policy[rule_number]
# Encode payload and compute HMAC signature
payload = json.dumps(request_data, sort_keys=True).encode()
signature = hmac.new(secret.encode(), payload, hashlib.sha256).hexdigest()
# Mock evaluation: always allow based on rule, but log the signature
matches = action == "ALLOW" and destination != "0.0.0.0/0"
return {
"rule": rule_number,
"action": action,
"source": source,
"destination": destination,
"matches": matches,
"signature": signature[:16] # truncated for brevity
}
if __name__ == "__main__":
result = evaluate_mock_rule(100, {"host": "10.1.2.3", "port": 443}, "my-secret-key")
print(json.dumps(result, indent=2))
Output
{
"rule": 100,
"action": "ALLOW",
"source": "10.0.0.0/8",
"destination": "any",
"matches": true,
"signature": "7f8a9b3c2d1e4f5a"
}
How it works
The function uses an in-memory dictionary to represent a numbered NACL policy, mapping each rule ID to its action and network constraints. It verifies rule existence before evaluation, preventing KeyError crashes. The HMAC-SHA256 signature is computed over the sorted JSON payload to ensure data integrity, mimicking how real cloud services authenticate requests. The matches field simulates a basic allow/deny decision based on rule properties, while the truncated signature provides traceability without exposing full secrets.
Common mistakes
- Using json.dumps without sort_keys=True produces inconsistent HMAC signatures
- Treating 'any' as a literal string instead of a wildcard in matches logic
- Computing HMAC before validating rule existence wastes cycles
- Storing secrets as plain strings in source code instead of environment variables
Variations
- Use match/case syntax for rule lookup on Python 3.10+
Real-world use cases
- Unit-testing cloud security policies before deploying to AWS production
- Simulating network ACL behavior in infrastructure-as-code test suites
- Building mock network controllers for local development environments
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.