|
1 | | -# cloud-security-analyzer |
| 1 | +# 🛡️ Cloud Security Analyzer |
| 2 | + |
| 3 | +[](https://github.com/wellingtoong/cloud-security-analyzer/actions/workflows/ci.yml) |
| 4 | + |
| 5 | +[](https://hub.docker.com/r/wellingtoong/cloud-security-analyzer) |
| 6 | + |
| 7 | + |
| 8 | +## 🔎 Code Quality |
| 9 | + |
| 10 | +[](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer) |
| 11 | +[](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer) |
| 12 | +[](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer) |
| 13 | +[](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer) |
| 14 | +[](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer) |
| 15 | +[](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer) |
| 16 | +[](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer) |
| 17 | +[](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer) |
| 18 | + |
| 19 | + |
| 20 | +## 🌟 Community |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +A powerful, AI-driven tool for analyzing Terraform Plan JSON files to identify security vulnerabilities and generate actionable recommendations. Built for DevSecOps workflows, it leverages Google Gemini AI to provide intelligent insights into cloud infrastructure configurations, ensuring compliance with best practices like CIS Benchmarks. |
| 29 | + |
| 30 | + |
| 31 | +## 📘 Overview |
| 32 | + |
| 33 | +Infrastructure as Code (IaC) tools like Terraform enable rapid deployment of cloud resources, but misconfigurations can introduce critical security risks. |
| 34 | + |
| 35 | +Cloud Security Analyzer addresses this by applying AI-driven contextual analysis to Terraform Plan (JSON) files before deployment, allowing teams to detect and remediate risks proactively. |
| 36 | + |
| 37 | +It provides: |
| 38 | + |
| 39 | +- 🔍 **Static Analysis**: Scanning Terraform plans for vulnerabilities without executing changes. |
| 40 | +- 🤖 **AI-Powered Insights**: Detecting nuanced security issues beyond traditional rule-based checks. |
| 41 | +- 📊 **Comprehensive Reporting**: Generating structured JSON outputs and executive-ready HTML reports with severity scores, risk assessments, and remediation guidance. |
| 42 | +- 🔄 **CI/CD Integration**: Seamlessly integrating into pipelines for automated infrastructure security reviews. |
| 43 | + |
| 44 | +This tool is particularly valuable for teams deploying to AWS (with roadmap support for Azure and GCP), helping prevent breaches caused by exposed databases, overly permissive security groups, unencrypted storage, and other misconfigurations. |
| 45 | + |
| 46 | + |
| 47 | +## 🎯 Why Cloud Security Analyzer? |
| 48 | + |
| 49 | +- 🛡️ Prevent security misconfigurations before deployment |
| 50 | +- 🤖 AI-enhanced contextual risk detection |
| 51 | +- 📊 Executive-ready HTML and JSON reports |
| 52 | +- 🔄 CI/CD native integration |
| 53 | +- ☁️ Built for AWS (Azure & GCP roadmap) |
| 54 | +- 🧱 Designed for scalable DevSecOps environments |
| 55 | + |
| 56 | + |
| 57 | +## 📘 Overview |
| 58 | + |
| 59 | +Infrastructure as Code (IaC) tools like Terraform enable rapid deployment of cloud resources, but misconfigurations can introduce critical security risks. Cloud Security Analyzer addresses this by: |
| 60 | + |
| 61 | +- 🔍 **Static Analysis**: Scanning Terraform plans for vulnerabilities without executing changes. |
| 62 | +- 🤖 **AI-Powered Insights**: Using advanced AI to detect nuanced issues beyond rule-based checks. |
| 63 | +- 📊 **Comprehensive Reporting**: Generating structured JSON outputs and visually appealing HTML reports with severity scores, risk assessments, and remediation steps. |
| 64 | +- 🔄 **CI/CD Integration**: Seamlessly integrating into pipelines for automated security reviews. |
| 65 | + |
| 66 | +This tool is particularly valuable for teams deploying to AWS (with roadmap support for Azure and GCP), helping prevent breaches from exposed databases, overly permissive security groups, unencrypted storage, and more. |
| 67 | + |
| 68 | +## ⚙️ How It Works |
| 69 | + |
| 70 | +1. 📥 **Input**: Provide a Terraform Plan JSON file (generated via `terraform plan -out=tfplan.binary && terraform show -json tfplan.binary > plan.json`). |
| 71 | +2. 🧠 **Analysis**: The tool sends the plan to Google Gemini AI, guided by a specialized prompt template, to identify vulnerabilities, assign severities (CRITICAL, HIGH, MEDIUM, LOW), and suggest fixes. |
| 72 | +3. 📤 **Output**: |
| 73 | + - 🧾 **JSON Report**: Structured data for programmatic consumption or further processing. |
| 74 | + - 🖥️ **HTML Report**: Interactive, Tailwind CSS-styled dashboard with executive summaries, vulnerability cards, code recommendations, and references to AWS documentation. |
| 75 | +4. 🏷️ **Metadata Enrichment**: Automatically captures execution context like branch, timestamp, Terraform version, and environment for traceability. |
| 76 | + |
| 77 | +The process is fast, typically completing in seconds, and supports both local execution and containerized runs. |
| 78 | + |
| 79 | +## 🏗️ Architecture |
| 80 | + |
| 81 | +The project follows a modular Python architecture for maintainability and extensibility: |
| 82 | + |
| 83 | +- 🧠 **`core/`**: Core logic, including the `TerraformAnalyzer` class that interfaces with Google Gemini AI for analysis. |
| 84 | +- 📂 **`data/`**: Data handling modules for loading Terraform plans and prompts. |
| 85 | +- 📝 **`reports/`**: Report generation, featuring Jinja2 templates for HTML rendering and utility functions for severity classification and formatting. |
| 86 | +- 🖥️ **`cli/`**: Command-line interface for user interaction, path resolution, and orchestration. |
| 87 | + |
| 88 | +Key dependencies include `google-generativeai` for AI integration, `jinja2` for templating, and standard libraries for JSON handling. The design emphasizes separation of concerns, making it easy to extend for multi-cloud support or alternative AI models. |
| 89 | + |
| 90 | +## 📦 Installation |
| 91 | + |
| 92 | +### 📋 Prerequisites |
| 93 | + |
| 94 | +- Python 3.12+ |
| 95 | +- Google Gemini API Key (obtain from [Google AI Studio](https://makersuite.google.com/app/apikey)) |
| 96 | +- Terraform (for generating plan files) |
| 97 | + |
| 98 | +### 🖥️ Local Setup |
| 99 | + |
| 100 | +1. Clone the repository: |
| 101 | + ```bash |
| 102 | + git clone https://github.com/wellingtoong/cloud-security-analyzer.git |
| 103 | + cd cloud-security-analyzer |
| 104 | + ``` |
| 105 | + |
| 106 | +2. Install dependencies: |
| 107 | + ```bash |
| 108 | + pip install -r src/requirements.txt |
| 109 | + ``` |
| 110 | + |
| 111 | +3. Set environment variables: |
| 112 | + ```bash |
| 113 | + export GEMINI_API_KEY="your-api-key-here" |
| 114 | + export GEMINI_MODEL="gemini-2.0-flash" # Optional, defaults to this model |
| 115 | + export BRANCH_NAME="develop" |
| 116 | + export RUN_TIMESTAMP="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" |
| 117 | + ``` |
| 118 | + |
| 119 | +4. Generate a Terraform plan (example for AWS): |
| 120 | + ```bash |
| 121 | + cd terraform/examples/aws/vpc-network/env/dev |
| 122 | + terraform init |
| 123 | + terraform plan -out=tfplan.binary |
| 124 | + terraform show -json tfplan.binary > ../../plans/tfplan.json |
| 125 | + ``` |
| 126 | + |
| 127 | +## 🚀 Usage |
| 128 | + |
| 129 | +### 🖥️ Local Execution |
| 130 | + |
| 131 | +Run the analyzer on a plan file: |
| 132 | +```bash |
| 133 | +python src/main.py terraform/examples/aws/vpc-network/artifacts/tfplan.json |
| 134 | +``` |
| 135 | + |
| 136 | +Outputs will be generated in `reports_output/`: |
| 137 | +- `terraform_security_report.json` |
| 138 | +- `terraform_security_report.html` |
| 139 | + |
| 140 | +### 🐳 Docker Execution |
| 141 | + |
| 142 | +1. Build the image: |
| 143 | + ```bash |
| 144 | + docker build -t cloud-security-analyzer . |
| 145 | + ``` |
| 146 | + |
| 147 | +2. Run the container: |
| 148 | + ```bash |
| 149 | + docker run --rm \ |
| 150 | + -e GEMINI_API_KEY="your-api-key-here" \ |
| 151 | + -e BRANCH_NAME \ |
| 152 | + -e RUN_TIMESTAMP \ |
| 153 | + -e ENVIRONMENT="dev" \ |
| 154 | + -v "$(pwd):/app" \ |
| 155 | + terraform-security-analyzer \ |
| 156 | + terraform/examples/aws/vpc-network/artifacts/tfplan.json |
| 157 | + ``` |
| 158 | + |
| 159 | +### 🔄 CI/CD Pipeline (GitHub Actions) |
| 160 | + |
| 161 | +Integrate into your workflow for automated analysis. Example `.github/workflows/security-analysis.yml`: |
| 162 | + |
| 163 | +```yaml |
| 164 | +name: Security Analysis |
| 165 | + |
| 166 | +on: |
| 167 | + pull_request: |
| 168 | + paths: |
| 169 | + - 'terraform/**' |
| 170 | + |
| 171 | +jobs: |
| 172 | + analyze: |
| 173 | + runs-on: ubuntu-latest |
| 174 | + steps: |
| 175 | + - uses: actions/checkout@v4 |
| 176 | + - name: Set up Terraform |
| 177 | + uses: hashicorp/setup-terraform@v3 |
| 178 | + with: |
| 179 | + terraform_version: "1.6.0" |
| 180 | + - name: Generate Plan |
| 181 | + run: | |
| 182 | + cd terraform/examples/your-example/env/dev |
| 183 | + terraform init |
| 184 | + terraform plan -out=tfplan.binary |
| 185 | + terraform show -json tfplan.binary > ../../../../plans/tfplan.json |
| 186 | + - name: Run Security Analyzer |
| 187 | + run: | |
| 188 | + docker build -t analyzer . |
| 189 | + docker run --rm \ |
| 190 | + -e GEMINI_API_KEY="${{ secrets.GEMINI_API_KEY }}" \ |
| 191 | + -v "$(pwd):/app" \ |
| 192 | + analyzer \ |
| 193 | + plans/tfplan.json |
| 194 | + - name: Upload Report |
| 195 | + uses: actions/upload-artifact@v4 |
| 196 | + with: |
| 197 | + name: security-report |
| 198 | + path: reports_output/ |
| 199 | +``` |
| 200 | +
|
| 201 | +This pipeline generates plans, runs analysis, and publishes HTML reports as artifacts for review. |
| 202 | +
|
| 203 | +## 🗂️ Project Structure |
| 204 | +
|
| 205 | +``` |
| 206 | +cloud-security-analyzer/ |
| 207 | +├── .github/ |
| 208 | +│ └── workflows/ # CI/CD pipelines |
| 209 | +├── src/ |
| 210 | +│ ├── main.py # Entry point |
| 211 | +│ ├── requirements.txt # Python dependencies |
| 212 | +│ ├── terraform_analyzer/ |
| 213 | +│ │ ├── cli.py # CLI logic |
| 214 | +│ │ ├── config.py # Configuration management |
| 215 | +│ │ ├── core/ |
| 216 | +│ │ │ ├── execution_metadata.py # Metadata utility |
| 217 | +│ │ │ └── analyzer.py # AI analysis engine |
| 218 | +│ │ ├── data/ |
| 219 | +│ │ │ ├── plan_loader.py # Terraform plan loading |
| 220 | +│ │ │ └── prompt_loader.py # Prompt template handling |
| 221 | +│ │ └── reports/ |
| 222 | +│ │ ├── html_renderer.py # HTML generation |
| 223 | +│ │ └── utils.py # Report utilities |
| 224 | +├── reports/ |
| 225 | +│ ├── templates/ # Jinja2 templates |
| 226 | +│ └── examples/ # Sample reports |
| 227 | +├── terraform/ |
| 228 | +│ └── examples/ # Terraform configurations for testing |
| 229 | +├── prompts/ # AI prompt templates |
| 230 | +├── tests/ # Unit tests |
| 231 | +├── Dockerfile # Container definition |
| 232 | +└── README.md # This file |
| 233 | +``` |
| 234 | + |
| 235 | +## 🚀 Roadmap |
| 236 | + |
| 237 | +> The items below represent potential roadmap initiatives and planned evolutions of the platform. |
| 238 | +> They reflect strategic directions and may be progressively implemented in future releases. |
| 239 | +
|
| 240 | +### 🔐 Security & Compliance |
| 241 | +- Support for CIS, NIST, ISO 27001 and SOC2 mappings |
| 242 | +- Automatic control mapping by compliance framework |
| 243 | +- Aggregated security score per environment |
| 244 | +- SARIF export for GitHub Advanced Security |
| 245 | +- Integration with AWS Security Hub |
| 246 | + |
| 247 | +### ☁️ Multi-Cloud Support |
| 248 | +- Full Azure and GCP support |
| 249 | +- Automatic provider detection from Terraform plan |
| 250 | +- Cloud-specific AI prompt templates |
| 251 | +- Cross-cloud severity normalization |
| 252 | + |
| 253 | +### 🤖 AI & Intelligence |
| 254 | +- Smart caching for repeated analyses |
| 255 | +- Incremental analysis (plan diff only) |
| 256 | +- False-positive classification layer |
| 257 | +- Explainability: "why is this a risk?" |
| 258 | +- Full Terraform remediation snippet generation |
| 259 | + |
| 260 | +### 🔎 DevSecOps Integrations |
| 261 | +- Official GitHub Action release |
| 262 | +- Official Docker image release |
| 263 | +- GitLab CI integration |
| 264 | +- Azure DevOps integration |
| 265 | +- Slack / Microsoft Teams webhook notifications |
| 266 | + |
| 267 | +### 📊 Visualization & UX |
| 268 | +- Web dashboard (SaaS mode) |
| 269 | +- Historical analysis per branch |
| 270 | +- Baseline vs current comparison |
| 271 | +- Security maturity metrics |
| 272 | +- Dynamic security score badge |
| 273 | + |
| 274 | +### ⚡ Performance & Scalability |
| 275 | +- Parallel processing for large plans |
| 276 | +- Support for very large plans (>50MB) |
| 277 | +- JSON chunking for AI token optimization |
| 278 | +- Streaming-based AI analysis |
| 279 | + |
| 280 | +### 🧱 Enterprise Features |
| 281 | +- Multi-tenant architecture |
| 282 | +- RBAC for report access |
| 283 | +- Structured logs for SIEM ingestion |
| 284 | +- Public REST API for integrations |
| 285 | +- Custom policy-as-code validation |
| 286 | + |
| 287 | +## ⚠️ Limitations |
| 288 | + |
| 289 | +> The limitations listed below represent current technical and architectural constraints. |
| 290 | +> Some of these constraints may be addressed and improved in future releases as the platform evolves. |
| 291 | +
|
| 292 | +### 🤖 AI Dependency |
| 293 | +- Requires a valid Google Gemini API key |
| 294 | +- Results are probabilistic (not deterministic) |
| 295 | +- Possible false positives and false negatives |
| 296 | +- Requires internet connectivity for AI analysis |
| 297 | + |
| 298 | +### ☁️ Cloud Provider Constraints |
| 299 | +- Azure and GCP require valid credentials for `terraform plan` |
| 300 | +- Fully offline mode currently supported only for AWS (via LocalStack) |
| 301 | +- Limited support for newly released provider resources |
| 302 | + |
| 303 | +### 📦 Technical Scope |
| 304 | +- Focused exclusively on Terraform |
| 305 | +- Does not support CloudFormation, Pulumi or ARM templates |
| 306 | +- No runtime (dynamic) security analysis |
| 307 | +- Does not replace traditional SAST/DAST scanners |
| 308 | + |
| 309 | +### 📊 Scalability Constraints |
| 310 | +- Very large plans may increase analysis latency |
| 311 | +- AI token usage may generate cost depending on plan size |
| 312 | +- No built-in rate limiting or quota management yet |
| 313 | + |
| 314 | +### 🔐 Governance Constraints |
| 315 | +- Does not automatically block deployments (advisory analysis only) |
| 316 | +- No native SIEM integration yet |
| 317 | +- No persistent historical storage outside generated reports |
| 318 | + |
| 319 | +## 🤝 Contributing |
| 320 | + |
| 321 | +We welcome contributions! Please follow these steps: |
| 322 | + |
| 323 | +1. Fork the repository and create a feature branch. |
| 324 | +2. Ensure code adheres to PEP 8 and includes tests. |
| 325 | +3. Run tests: `pytest tests/` |
| 326 | +4. Submit a pull request with a clear description of changes. |
| 327 | + |
| 328 | +For issues or feature requests, use the [GitHub Issues](https://github.com/wellingtoong/cloud-security-analyzer/issues) page. |
| 329 | + |
| 330 | +## 📄 License |
| 331 | + |
| 332 | +This project is licensed under the MIT License. See [LICENSE](LICENSE) for details. |
0 commit comments