Level 10 / Project 03 - Policy As Code Validator¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | — | — |
Focus¶
- Chain of Responsibility pattern for composable policy rules
- Protocol-based rule interface for extensibility
- Declarative policy configuration loaded from JSON
- Batch evaluation with per-rule verdicts and severity levels
Why this project exists¶
Infrastructure-as-code demands that compliance checks live alongside the code they govern. By expressing policies as composable Python objects rather than config files, teams get IDE support, type checking, and the ability to unit-test compliance rules. This project builds an OPA-style policy engine in pure Python.
Run (copy/paste)¶
cd <repo-root>/projects/level-10/03-policy-as-code-validator
python project.py --config data/config.json --resource data/sample_input.txt
pytest -v
Expected terminal output¶
Expected artifacts¶
- Evaluation report printed to stdout
- Passing tests (
pytest -vshows ~16 passed)
Alter it (required)¶
- Add a
RegexMatchRulethat validates a field against a regex pattern — register it inload_policies_from_config. - Add AND/OR composite rules:
AllOfRule(all sub-rules must pass) andAnyOfRule(at least one must pass). - Re-run tests and add coverage for the new rule types.
Break it (required)¶
- Pass an unknown rule type in the JSON config and observe the
ValueError. - Evaluate a resource missing all required fields and verify every rule returns FAIL.
- Pass a non-numeric value to
NumericRangeRuleand confirm the FAIL verdict.
Fix it (required)¶
- Add graceful handling for malformed JSON config (catch
json.JSONDecodeErrorwith a friendly message). - Make
NumericRangeRulereturn SKIP instead of FAIL when the field is missing (distinguish "absent" from "invalid"). - Add tests for the fixed behavior.
Explain it (teach-back)¶
- How does the
PolicyRuleProtocol enable adding new rule types without modifying the engine? - Why is severity separate from verdict — when would a FAIL with WARNING severity be useful?
- How does
evaluate_batchmake it efficient to validate many resources against the same ruleset? - What are the tradeoffs between policy-as-code (Python) vs policy-as-data (JSON/YAML)?
Mastery check¶
You can move on when you can: - write a new rule class that satisfies the PolicyRule protocol, - explain the difference between PASS/FAIL/SKIP verdicts, - load policies from JSON and evaluate them programmatically, - describe how this pattern scales to hundreds of rules across multiple frameworks.
Related Concepts¶
| ← Prev | Home | Next → |
|---|---|---|