How stackguardian/tirith works
Project Type and Purpose
• Tirith is an open-source, Python-based CLI policy framework for enforcing declarative policies on Infrastructure-as-Code (IaC) inputs (Terraform plans, Infracost outputs, Kubernetes manifests, plain JSON, StackGuardian workflows).
• It provides a pluggable architecture: a core evaluation engine plus “provider” plugins (terraform_plan, infracost, json, kubernetes, sg_workflow) and reusable evaluator classes (Equals, Contains, RegexMatch, etc.).
High-Level File Structure Analysis
• src/tirith: core package
• __main__.py & cli.py: entrypoint for the “tirith” CLI
• core/: engine logic, policy parsing, evaluator registry
• providers/: one subpackage per provider, each exposing a handler implementing a common interface
• exceptions.py, logging.py, prettyprinter.py, utils.py: shared utilities
• tests/: unit and integration tests organized by core, providers
• documentation/: Docusaurus-based documentation site
• Pipfile / pyproject.toml / setup.py: build & dependency configuration
• .github/, .devcontainer/: CI pipelines, codeowners, linting, dev container config
a. Main Components
• CLI Layer (“tirith” command)
• Policy Loader & Parser
• Core Evaluation Engine
• Provider Plugins (terraform_plan, infracost, json, kubernetes, sg_workflow)
• Evaluator Modules (base_evaluator + specific conditions)
• Input Data Sources (IaC files, JSON, Kubernetes YAML, Terraform plan JSON, Infracost JSON)
• Output Formatter (console printer, JSON output, prettyprinter)
b. Relationships and Data Flow
1. User invokes CLI → CLI Layer
2. CLI parses flags & paths → Policy Loader loads policy.json → builds Policy object
3. CLI reads input file → passes raw data to Engine + selected Provider
4. Core Engine registers and initializes the required Provider Handler
5. Provider Handler transforms raw input into standardized “resource” or “data” model
6. Core Engine iterates each Evaluator:
– Evaluator retrieves values via Provider Handler APIs
– Condition objects run logic (Equals, Contains, etc.)
7. Engine aggregates per-evaluator results → computes final eval_expression
8. Output Formatter prints to console or returns JSON
c. Architectural Patterns & Design Principles
• Plugin Architecture: Providers implement a common handler interface; new providers drop into src/tirith/providers
• Strategy Pattern: Evaluators encapsulate condition logic, interchangeable at runtime
• Separation of Concerns: CLI vs. Core vs. Providers vs. Formatters vs. Tests vs. Docs
• Declarative Policy as Code: policies define evaluators + expression, engine interprets
d. Key Technologies & Frameworks
• Python 3.6+
• argparse (builtin CLI parsing)
• pipenv / virtualenv for dependency management
• Docusaurus (documentation site)
• GitHub Actions (CI: build_test, lint, coverage, deploy_docs)
• pytest (tests suite)
• Black (code style)
Essential Elements to Capture
• CLI entrypoint and how it invokes core
• Core vs. Providers vs. Evaluators separation
• Data flow from input through handler to evaluator to output
• Extensibility: new provider, new evaluator
• Testing layer as a separate validation path (tests/ folder)