Skip to content

Commit f165bc1

Browse files
author
Wellington Gonzalez
committed
feat: enhance execution metadata handling and improve reporting pipeline
1 parent 60c013a commit f165bc1

11 files changed

Lines changed: 393 additions & 14 deletions

File tree

.github/workflows/terraform-aws-security-analysis.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,24 @@ jobs:
7070

7171
- run: mkdir -p reports_output
7272

73+
- name: Prepare execution metadata
74+
id: metadata
75+
run: |
76+
echo "BRANCH_NAME=${GITHUB_REF_NAME}" >> $GITHUB_ENV
77+
echo "RUN_TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> $GITHUB_ENV
78+
7379
- name: Run analyzer (Docker)
7480
env:
7581
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
7682
run: |
7783
docker run --rm \
7884
-e GEMINI_API_KEY="$GEMINI_API_KEY" \
85+
-e BRANCH_NAME="$BRANCH_NAME" \
86+
-e RUN_TIMESTAMP="$RUN_TIMESTAMP" \
7987
-v "$GITHUB_WORKSPACE:/repo" \
8088
-v "$GITHUB_WORKSPACE/reports_output:/app/reports_output" \
8189
-w /repo \
82-
wellingtoong/cloud-security-analyzer:1.0.0 \
90+
wellingtoong/cloud-security-analyzer:1.0.1 \
8391
artifacts_in/plan.json
8492
8593
- name: Upload HTML report artifact

Dockerfile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,4 @@ RUN mkdir -p reports_output
3030

3131
# Define o comando padrão para executar a aplicação
3232
# O usuário deve passar o caminho do arquivo de plano como argumento ao rodar o container
33-
ENTRYPOINT ["python", "src/main.py"]
34-
35-
# Exemplo de uso:
36-
# export GEMINI_API_KEY=xxxxx
37-
# docker build -t terraform-security-analyzer:local .
38-
# docker run --rm -e GEMINI_API_KEY -v "$(pwd):/repo" -w /repo terraform-security-analyzer:local terraform/examples/vpc-network/artifacts/tfplan.json
33+
ENTRYPOINT ["python", "src/main.py"]

README.md

Lines changed: 332 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,332 @@
1-
# cloud-security-analyzer
1+
# 🛡️ Cloud Security Analyzer
2+
3+
[![CI](https://github.com/wellingtoong/cloud-security-analyzer/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/wellingtoong/cloud-security-analyzer/actions/workflows/ci.yml)
4+
![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)
5+
[![Docker Pulls](https://img.shields.io/docker/pulls/wellingtoong/cloud-security-analyzer)](https://hub.docker.com/r/wellingtoong/cloud-security-analyzer)
6+
7+
8+
## 🔎 Code Quality
9+
10+
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=wellingtoong_cloud-security-analyzer&metric=ncloc&token=f7ddc671d11a54faa1805b801607b69466f276a4)](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer)
11+
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=wellingtoong_cloud-security-analyzer&metric=duplicated_lines_density&token=f7ddc671d11a54faa1805b801607b69466f276a4)](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer)
12+
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=wellingtoong_cloud-security-analyzer&metric=sqale_rating&token=f7ddc671d11a54faa1805b801607b69466f276a4)](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer)
13+
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=wellingtoong_cloud-security-analyzer&metric=security_rating&token=f7ddc671d11a54faa1805b801607b69466f276a4)](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer)
14+
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=wellingtoong_cloud-security-analyzer&metric=reliability_rating&token=f7ddc671d11a54faa1805b801607b69466f276a4)](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer)
15+
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=wellingtoong_cloud-security-analyzer&metric=vulnerabilities&token=f7ddc671d11a54faa1805b801607b69466f276a4)](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer)
16+
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=wellingtoong_cloud-security-analyzer&metric=code_smells&token=f7ddc671d11a54faa1805b801607b69466f276a4)](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer)
17+
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=wellingtoong_cloud-security-analyzer&metric=bugs&token=f7ddc671d11a54faa1805b801607b69466f276a4)](https://sonarcloud.io/summary/new_code?id=wellingtoong_cloud-security-analyzer)
18+
19+
20+
## 🌟 Community
21+
22+
![Open Source Love](https://img.shields.io/badge/Open%20Source-%E2%9D%A4-red)
23+
![GitHub stars](https://img.shields.io/github/stars/wellingtoong/cloud-security-analyzer?style=social)
24+
![GitHub forks](https://img.shields.io/github/forks/wellingtoong/cloud-security-analyzer?style=social)
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.

prompts/terraform_security_analysis.prompt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Formato exato de saída (siga esta estrutura e nomes de campos):
146146

147147
Contexto fixo do relatório:
148148
- tool.name = "Cloud Security Analyzer"
149-
- tool.version = "v2.4.0"
149+
- tool.version = "v1.0.1"
150150
- provider = "AWS"
151151
- reportTitle = "Análise de Terraform Plan - Revisão de Segurança"
152152
- environment = "MVP"

reports/examples/base.empty.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ <h3 class="font-semibold">Próximos Passos Recomendados</h3>
243243
<div class="text-center sm:text-right">
244244
<p class="text-xs text-slate-500 mb-1">Análise gerada por</p>
245245
<p class="font-semibold text-slate-900">Cloud Security Analyzer</p>
246-
<p class="text-xs text-slate-500 mt-1">v2.4.0 • CIS Benchmark AWS 2.0</p>
246+
<p class="text-xs text-slate-500 mt-1">v1.0.1 • CIS Benchmark AWS 2.0</p>
247247
</div>
248248
</div>
249249
</section>

0 commit comments

Comments
 (0)