|
| 1 | +# 🛡️ AI-Powered DevSecOps Terraform Security Guardrail |
| 2 | + |
| 3 | +An automated continuous compliance gatekeeper built inside **GitHub Actions** that intercepts Infrastructure as Code (IaC) misconfigurations prior to deployment using Generative AI and structured schemas. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🚀 Overview |
| 8 | + |
| 9 | +In modern cloud environments, small mistakes in Infrastructure as Code (like leaving an S3 bucket public or an SSH port open to the world) can lead to catastrophic data breaches. |
| 10 | + |
| 11 | +This repository implements a **Shift-Left DevSecOps approach**. Whenever code is pushed or a Pull Request is opened, a CI/CD pipeline compiles an offline Terraform execution plan, converts it into structured machine-readable JSON, and routes it to an **AI Security Architect (Gemini 2.5 Flash)**. |
| 12 | + |
| 13 | +Utilizing strict deterministic **Pydantic validation schemas**, the AI evaluates the plan. If critical security flaws are detected, the pipeline automatically crashes (`exit 1`), blocking insecure deployments from hitting production and providing actionable, code-level remediation steps directly in the pipeline logs. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 🛠️ Key Features |
| 18 | + |
| 19 | +* **Shift-Left Security Compliance:** Validates security configurations natively during the code review stage of the Software Development Life Cycle (SDLC). |
| 20 | +* **Deterministic AI Responses:** Uses Structured Outputs (JSON Schema enforcement via Pydantic) to force the LLM to return consistent, parseable, and reliable analytical datasets. |
| 21 | +* **Cost-Efficient Planning:** Runs entirely offline using dummy credential targets, ensuring no cloud cloud API rate limits are hit during the planning phase. |
| 22 | +* **CI/CD Gatekeeping:** Leverages POSIX exit signals to hard-abort builds upon identifying `CRITICAL` or `WARNING` vulnerabilities. |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## 🧰 Tech Stack |
| 27 | + |
| 28 | +* **Infrastructure Automation:** HashiCorp Terraform (AWS Provider) |
| 29 | +* **AI Orchestration:** Google GenAI SDK (Gemini 2.5 Flash) |
| 30 | +* **Data Validation & Parsing:** Python 3.11, Pydantic v2 |
| 31 | +* **CI/CD Platform:** GitHub Actions |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## 📂 Repository Structure |
| 36 | + |
| 37 | +* `.github/workflows/security-gate.yml` — The GitHub Actions automation workflow pipeline. |
| 38 | +* `main.tf` — The core infrastructure setup containing the evaluated cloud resources. |
| 39 | +* `scan_plan.py` — The Python audit engine that loads the plan, orchestrates the AI request, and enforces the pipeline gate. |
| 40 | +* `.gitignore` — Built to safeguard sensitive local files (`plan.json`, binary configurations, python virtual environments) from accidental source control tracking. |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## 📊 Pipeline Behavior & SRE Impact |
| 45 | + |
| 46 | +### ❌ The Catch (Security Flaw Detected) |
| 47 | +When insecure configurations are committed (e.g., `cidr_blocks = ["0.0.0.0/0"]` on port 22), the pipeline captures the violation: |
| 48 | + |
| 49 | +```text |
| 50 | +================================================== |
| 51 | +🛡️ DEVSECOPS SECURITY VERDICT: FAIL |
| 52 | +================================================== |
| 53 | +Summary: Critical security vulnerabilities detected. The plan includes an AWS Security Group allowing global SSH access (0.0.0.0/0). |
| 54 | +
|
| 55 | +⚠️ Found 1 Security Vulnerabilities: |
| 56 | +
|
| 57 | + [1] Resource: aws_security_group.allow_ssh_global |
| 58 | + Severity: CRITICAL |
| 59 | + Vulnerability: Overly permissive networking rule: The security group permits inbound SSH from 0.0.0.0/0. |
| 60 | + Fix: Restrict the ingress rule for port 22 to specific, trusted internal corporate IP spaces (e.g., 10.0.0.0/16). |
| 61 | +
|
| 62 | +❌ Deployment Blocked: Critical security flaws must be resolved. |
| 63 | +
|
| 64 | +================================================== |
| 65 | +🛡️ DEVSECOPS SECURITY VERDICT: PASS |
| 66 | +================================================== |
| 67 | +Summary: All infrastructure changes comply with enterprise compliance guidelines. |
| 68 | +✅ No critical security violations detected. |
| 69 | +🚀 Deployment Approved: Infrastructure compliant. |
| 70 | +
|
| 71 | +## 🛠️ Local Development & Replication |
| 72 | +
|
| 73 | +Follow these steps to run the AI security scanner locally on your workstation to test infrastructure changes offline before pushing them to GitHub. |
| 74 | +
|
| 75 | +### 1. Clone the Repository |
| 76 | +```bash |
| 77 | +git clone |
| 78 | +cd tf-security-guardrail |
| 79 | +
|
| 80 | +### 2. Set Up Python Virtual Environment |
| 81 | +python3 -m venv venv |
| 82 | +source venv/bin/activate |
| 83 | +pip install google-genai pydantic |
| 84 | +
|
| 85 | +### 3. Generate the Offline Terraform JSON Plan |
| 86 | +terraform init |
| 87 | +terraform plan -out=tfplan.binary |
| 88 | +terraform show -json tfplan.binary > plan.json |
| 89 | +
|
| 90 | +### 4. Run the AI Security Scanner |
| 91 | +export GEMINI_API_KEY="your_actual_api_key_here" |
| 92 | +python scan_plan.py |
0 commit comments