Skip to content

feat: add Terraform plan evaluation and test framework - #214

Open
robmorgan wants to merge 9 commits into
mainfrom
feature/rewrite
Open

feat: add Terraform plan evaluation and test framework#214
robmorgan wants to merge 9 commits into
mainfrom
feature/rewrite

Conversation

@robmorgan

Copy link
Copy Markdown
Owner

Summary

This PR introduces the foundational infrastructure for InfraSpec's Terraform plan evaluation and testing capabilities:

  • Plan JSON Parser (internal/plan/): Parse Terraform plan JSON output with typed accessors for resource changes, outputs, and variables
  • Rules Engine (internal/rules/): Extensible rules engine for evaluating Terraform plans against security policies
  • AWS Safety Rules: 15 built-in rules covering S3, IAM, RDS, and Security Groups (public access, encryption, wildcards)
  • CLI Check Command: New infraspec check command for plan security evaluation
  • GeneratePlan API: High-level API with auto-init detection for Terraform plan generation
  • Test File Parser (internal/testfile/): Parser for .infraspec.hcl test file format
  • CEL Assertion Engine (internal/assert/): CEL-based expression evaluation for test assertions with custom functions (contains, anytrue, alltrue, length)

Test plan

  • All existing tests pass (make go-test-cover)
  • New packages have comprehensive unit tests
  • Linting passes (make lint)
  • Manual testing of infraspec check command against sample Terraform plans

🤖 Generated with Claude Code

robmorgan and others added 9 commits February 5, 2026 15:57
Add internal/plan package for parsing Terraform plan JSON output with
type-safe access and convenience methods. Includes:

- Type definitions for Plan, ResourceChange, Change, StateValues
- ParsePlanFile/ParsePlanBytes for parsing plan JSON
- Query methods: ResourcesByType, ResourceByAddress, ResourcesByModule
- Attribute access: GetAfter/GetBefore with nested path support
- Typed helpers: GetAfterString, GetAfterBool, GetAfterInt, etc.
- Action detection: IsCreate, IsUpdate, IsDelete, IsReplace, IsNoOp
- Runner for terraform plan orchestration (RunPlan, PlanJSON, etc.)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add a new internal/rules package that provides a rule interface and
registry for evaluating Terraform plan resources. This enables
extensible rule-based validation of infrastructure changes.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implement AWS-specific rules to detect security misconfigurations:

Security Group rules (10 total):
- aws-sg-no-public-ssh: Block SSH (port 22) from 0.0.0.0/0
- aws-sg-no-public-rdp: Block RDP (port 3389) from 0.0.0.0/0
- aws-sg-no-public-mysql: Block MySQL (port 3306) from 0.0.0.0/0
- aws-sg-no-public-postgres: Block PostgreSQL (port 5432) from 0.0.0.0/0
- aws-sg-no-unrestricted-ingress: Block protocol -1 from 0.0.0.0/0
- Plus 5 variants for aws_security_group_rule resources

S3 rules (4 total):
- aws-s3-no-public-acl: Disallow public-read/public-read-write ACLs
- aws-s3-encryption-enabled: Require server-side encryption
- aws-s3-versioning-enabled: Recommend versioning for data protection
- aws-s3-no-public-policy: Require block_public_policy = true

IAM rules (4 total):
- aws-iam-no-wildcard-action: Block Action:* with Resource:*
- aws-iam-no-admin-policy-role: Block AdministratorAccess on roles
- aws-iam-no-admin-policy-user: Block AdministratorAccess on users
- aws-iam-no-user-inline-policy: Discourage inline policies on users

RDS rules (3 total):
- aws-rds-encryption-enabled: Require storage_encrypted = true
- aws-rds-no-public-access: Require publicly_accessible = false
- aws-rds-backup-enabled: Require backup_retention_period > 0

Also adds provider registration pattern to avoid import cycles.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implements `infraspec check` to evaluate Terraform plans against security
rules before applying changes. Supports colorized terminal output, JSON
format, severity filtering, and rule ignore lists.

- Add internal/check package with runner, formatters, and types
- Add cmd/check.go with --plan, --dir, --severity, --ignore, --format flags
- Exit code 1 on rule violations, 0 on success
- Unit and integration tests included

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add a simpler, higher-level API for running terraform plans:
- PlanOptions struct for simplified configuration (vars, timeout, etc.)
- FindTerraformBinary() to check terraform availability
- GeneratePlan() and GeneratePlanWithContext() that handle init detection,
  plan execution, JSON output, and parsing in a single call
- Progress message in check command when generating plans

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implement HCL-based test file format similar to terraform test, enabling
declarative infrastructure test definitions with variables, runs, and
assertions. Uses hashicorp/hcl/v2 for parsing with expression preservation
for future evaluation.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implement a CEL (Common Expression Language) based assertion engine that
evaluates expressions from .infraspec.hcl test files against Terraform
plan data.

New internal/assert package includes:
- EvalContext type to build evaluation context from Terraform plans
- CEL environment with custom functions (contains, anytrue, alltrue, length)
- Automatic HCL dot notation to CEL bracket notation conversion
- Engine with Evaluate, EvaluateExpression, and EvaluateAll methods

Also fixes extractExprSource in testfile parser to return actual
expression text using byte offsets instead of range info.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
infraspec Ready Ready Preview, Comment Feb 5, 2026 9:19am

@github-actions

github-actions Bot commented Feb 5, 2026

Copy link
Copy Markdown

AWS API Coverage Summary

Service Implemented Total Coverage
APPLICATIONAUTOSCALING 14 14 100%
Service Implemented Total Coverage
DYNAMODB 33 57 57.89%
Service Implemented Total Coverage
EC2 47 749 6.28%
Service Implemented Total Coverage
IAM 105 176 59.66%
Service Implemented Total Coverage
LAMBDA 0 85 0%
Service Implemented Total Coverage
RDS 9 163 5.52%
Service Implemented Total Coverage
S3 23 107 21.5%
Service Implemented Total Coverage
SQS 16 23 69.57%
Service Implemented Total Coverage
STS 10 11 90.91%

Total: 257 operations implemented across 9 services (45.7% average coverage)


Generated by CloudMirror - updates automatically when internal/emulator/services/ files change

@claude

claude Bot commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

Pull Request Review: Terraform Plan Evaluation Framework

This PR introduces a comprehensive Terraform plan evaluation framework with excellent architecture and code quality.

Strengths

  • Clean package structure with proper separation of concerns
  • Strong type safety with typed accessors (GetAfterString, GetAfterBool, etc.)
  • 64 test files showing solid test coverage
  • Consistent error handling with fmt.Errorf and %w wrapping
  • 15 built-in AWS security rules covering S3, IAM, RDS, and Security Groups
  • Well-designed CEL integration for assertions

Issues & Recommendations

High Priority

  1. Command Injection Risk (internal/plan/runner.go) - Add validation to ensure directory paths don't contain shell metacharacters
  2. Input Validation (internal/check/runner.go:116-131) - Validate plan file paths before use
  3. Verify that iacprovisioner.RunCommand properly escapes all arguments

Medium Priority

  1. context.TODO() Usage (pkg/assertions/aws/) - Replace with proper context propagation throughout
  2. Add integration tests for the full check command
  3. Document error handling strategy in assertion engine

Low Priority

  1. Add godoc examples for key functions
  2. Consider parallelizing rule evaluation for large plans
  3. Add overflow checks in toInt() conversion

Security Notes

  • Terraform binary validated via exec.LookPath ✅
  • Good file permissions (0o600) ✅
  • Consider adding sensitive data redaction flags

Final Assessment

⭐⭐⭐⭐ Strong implementation with minor improvements needed

Recommendation: Approve with changes requested for input validation and context management.

Great work! 🚀

@github-actions

github-actions Bot commented Mar 7, 2026

Copy link
Copy Markdown

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 365 days.

@github-actions github-actions Bot added the stale label Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant