Skip to content

Commit ba85b7d

Browse files
authored
Introducing Context Management for Analysis and reporting (#6)
* adding more testing and policy preparation * Introducing more unit tests and documentation updates * Fix CI build * Fix CI build
1 parent a66c074 commit ba85b7d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3580
-738
lines changed

.github/workflows/ci.yaml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ jobs:
2020
uses: actions/setup-python@v5
2121
with:
2222
python-version: ${{ matrix.python-version }}
23-
23+
2424
- name: Install dependencies
2525
run: |
2626
python -m pip install --upgrade pip
27-
pip install flake8 pytest pytest-cov
28-
pip install -e .
29-
30-
- name: Run tests with pytest
31-
run: |
32-
pytest --cov=tfsumpy --cov-report=xml
27+
pip install poetry twine
28+
poetry install --with dev
29+
30+
- name: Run tests
31+
run: poetry run pytest
32+
33+
- name: Build package
34+
run: poetry build
3335

34-
- name: Upload coverage reports to Codecov
35-
uses: codecov/codecov-action@v4
36-
env:
37-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
36+

.github/workflows/release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ jobs:
2222
- name: Install dependencies
2323
run: |
2424
python -m pip install --upgrade pip
25-
pip install build twine wheel
26-
pip install -e ".[dev]"
25+
pip install poetry twine
26+
poetry install --with dev
2727
2828
- name: Run tests
29-
run: pytest
29+
run: poetry run pytest
3030

3131
- name: Build package
32-
run: python -m build
32+
run: poetry build
3333

3434
- name: Get version from tag
3535
id: get_version

README.md

Lines changed: 124 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -11,172 +11,172 @@ TFSumPy is a Python-based tool that analyzes Terraform plan files to provide a c
1111

1212
## Features
1313

14-
- 🔍 Analyzes Terraform plan JSON output
15-
- ⚠️ Identifies high-risk changes (deletions of critical resources, security group modifications)
16-
- 🔒 Automatically redacts sensitive information (credentials, IPs, resource names)
17-
- 📊 Provides clear summary statistics
18-
- 🛡️ Supports Terraform 1.0+ plan formats
19-
- 📂 Module-aware resource grouping
14+
- 🔍 Detailed plan analysis with change breakdown
15+
- 📊 Clear summary statistics for resource changes
16+
- 🔒 Automatic sensitive information redaction
17+
- 🛡️ Risk assessment for infrastructure changes
18+
- 📋 Policy compliance checking
19+
- 🎨 Color-coded output for better readability
2020
- 🔄 Detailed attribute change tracking
2121

2222
## Installation
2323

24-
Currently, TFSumPy can only be installed from source:
24+
Install using pip:
25+
```bash
26+
pip install tfsumpy
27+
```
28+
Or install from source:
29+
```bash
30+
git clone https://github.com/rafaelherik/tfsumpy.git
31+
cd tfsumpy
32+
pip install .
33+
```
34+
## Usage
2535

36+
### Basic Usage
37+
38+
1. Generate a Terraform plan JSON file:
2639
```bash
27-
git clone https://github.com/rafaelherik/tfsumpy.git
28-
cd tfsumpy
29-
pip install .
40+
terraform plan -out=tfplan
41+
terraform show -json tfplan > plan.json
3042
```
3143

32-
## Usage
44+
2. Analyze the plan:
3345

34-
First, generate a Terraform plan JSON file:
46+
Basic summary:
47+
```bash
48+
tfsumpy plan.json
49+
```
3550

51+
Show detailed changes:
3652
```bash
37-
terraform plan -out=tfplan
38-
terraform show -json tfplan > plan.json
53+
tfsumpy plan.json --changes
3954
```
4055

41-
Then analyze the plan using TFSumPy:
56+
Show resource details:
57+
```bash
58+
tfsumpy plan.json --details
59+
```
4260

61+
Enable risk assessment:
4362
```bash
44-
# Basic usage
45-
tfsumpy plan.json
63+
tfsumpy plan.json --risks
64+
```
4665

47-
# Show resources grouped by module
48-
tfsumpy plan.json --show-module
66+
Enable policy compliance check:
67+
```bash
68+
tfsumpy plan.json --policies
69+
```
4970

50-
# Show detailed attribute changes
51-
tfsumpy plan.json --show-changes
71+
### Example Output
5272

53-
# Using custom rules configuration
54-
tfsumpy plan.json --config rules_config.json
73+
```bash
74+
Terraform Plan Analysis
75+
======================
76+
Total Changes: 3
77+
Create: 1
78+
Update: 1
79+
Delete: 1
80+
81+
Resource Changes:
82+
CREATE aws_s3_bucket: data_bucket
83+
+ bucket = "new-bucket"
84+
85+
UPDATE aws_instance: web_server
86+
~ instance_type = t2.micro -> t2.small
87+
88+
DELETE aws_security_group: old_sg
89+
- name = "old-sg"
90+
```
91+
### Advanced Features
5592

56-
# Enable debug logging
57-
tfsumpy plan.json --debug
93+
1. Risk Assessment:
5894

59-
# Show only specific sections
60-
tfsumpy plan.json --risks --details
95+
```bash
96+
tfsumpy plan.json --risks
6197
```
6298

63-
### Command Line Options
99+
This will show:
100+
- High and medium risk changes
101+
- Impact assessment
102+
- Mitigation suggestions
103+
104+
2. Policy Compliance:
64105

65-
- `--show-module`: Group resources by their Terraform module
66-
- `--show-changes`: Display detailed attribute changes for resources
67-
- `--risks`: Show only the risk assessment section
68-
- `--details`: Show only the resource details section
69-
- `--debug`: Enable debug logging
70-
- `--config`: Specify a custom rules configuration file
106+
```bash
107+
tfsumpy plan.json --policies
108+
```
71109

72-
### Custom Rules Configuration
110+
Checks resources against:
111+
- Security best practices
112+
- Compliance requirements
113+
- Custom policy rules
73114

74-
You can customize the analysis rules by creating a JSON configuration file. Here's an example structure:
115+
3. Detailed Analysis:
116+
117+
```bash
118+
tfsumpy plan.json --changes --details --risks
119+
```
120+
121+
### Configuration
122+
123+
Create a custom configuration file (config.json):
75124

76125
```json
77-
{
78-
"sensitive_patterns": [
79126
{
80-
"pattern": "\\bAKIA[0-9A-Z]{16}\\b",
81-
"replacement": "[AWS-KEY-REDACTED]"
82-
},
83-
{
84-
"pattern": "\\b(password|secret|token)\\b[\"']?:?[\\s\"']+[^\\s\"']+",
85-
"replacement": "[SECRET-REDACTED]"
86-
}
87-
],
88-
"risk_rules": {
89-
"high": [
90-
{
91-
"pattern": "\\bdelete\\b.*\\b(database|storage)\\b",
92-
"message": "High risk: Critical storage resource deletion detected"
93-
}
94-
],
95-
"medium": [
96-
{
97-
"pattern": "\\bcreate\\b.*\\b(bucket|storage)\\b.*public:\\s*true",
98-
"message": "Medium risk: Public storage resource being created"
127+
"sensitive_patterns": [
128+
{
129+
"pattern": "\\b(?:password|secret|key)\\b",
130+
"replacement": "[REDACTED]"
131+
}
132+
],
133+
"risk_rules": {
134+
"high": [
135+
{
136+
"pattern": "\\bdelete\\b.*\\b(database|storage)\\b",
137+
"message": "Critical resource deletion"
138+
}
139+
]
99140
}
100-
]
101-
}
102-
}
141+
}
103142
```
104143

105-
The configuration file allows you to define:
106-
- `sensitive_patterns`: Regular expressions to identify and redact sensitive information
107-
- `risk_rules`: Patterns to identify high and medium risk changes
108-
109-
## Example Output
144+
Use the configuration:
110145

111-
### Default Output (without --show-module)
112-
```
113-
Infrastructure Change Analysis
114-
==============================
115-
Total Changes: 5
116-
Create: 2
117-
Update: 2
118-
Delete: 1
119-
120-
Risk Assessment:
121-
High Risks:
122-
- High risk: Security-related configuration change
123-
Medium Risks:
124-
- Medium risk: Version change could cause compatibility issues
125-
126-
Resource Details:
127-
CREATE aws_s3_bucket: project-storage-[REDACTED]
128-
UPDATE aws_security_group: app-sg-[REDACTED]
129-
~ ingress = [] -> [{port = 443}]
130-
UPDATE aws_ecs_service: api-service
131-
DELETE aws_iam_role: legacy-role
132-
CREATE aws_lambda_function: processor-function
146+
```bash
147+
tfsumpy plan.json --config config.json
133148
```
134149

135-
### With Module Grouping (--show-module)
136-
```
137-
Infrastructure Change Analysis
138-
==============================
139-
Total Changes: 5
140-
Create: 2
141-
Update: 2
142-
Delete: 1
143-
144-
Changes by Module:
145-
root:
146-
Create: 1
147-
Update: 1
148-
module.storage:
149-
Create: 1
150-
Update: 1
151-
Delete: 1
152-
153-
Risk Assessment:
154-
High Risks:
155-
- High risk: Security-related configuration change
156-
Medium Risks:
157-
- Medium risk: Version change could cause compatibility issues
158-
159-
Resource Details:
160-
Module: root
161-
CREATE aws_s3_bucket: project-storage-[REDACTED]
162-
UPDATE aws_security_group: app-sg-[REDACTED]
163-
~ ingress = [] -> [{port = 443}]
164-
165-
Module: module.storage
166-
UPDATE aws_ecs_service: api-service
167-
DELETE aws_iam_role: legacy-role
168-
CREATE aws_lambda_function: processor-function
150+
### Debug Mode
151+
152+
For troubleshooting or detailed logging:
153+
154+
```bash
155+
tfsumpy plan.json --debug
169156
```
170157

158+
This will:
159+
- Enable verbose logging
160+
- Show detailed error messages
161+
- Display analysis process information
162+
171163
## Requirements
172164

173165
- Python 3.10 or higher
174-
- Terraform 1.0 or higher (for plan generation)
166+
- Terraform 1.0 or higher
175167

176168
## Contributing
177169

178-
Contributions are welcome! Please feel free to submit a Pull Request. Visit our [GitHub repository](https://github.com/rafaelherik/tfsumpy) for more information.
170+
Contributions are welcome! Please feel free to submit a Pull Request. For major changes:
171+
172+
1. Fork the repository
173+
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
174+
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
175+
4. Push to the branch (`git push origin feature/AmazingFeature`)
176+
5. Open a Pull Request
177+
178+
Please make sure to update tests as appropriate.
179179

180180
## License
181181

182-
This project is licensed under the MIT License - see the LICENSE file for details.
182+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

docs/DEFAULT_POLICIES.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# TFSumPy Default Policies
2+
3+
This document lists all default policies included with TFSumPy, organized by cloud provider.
4+
5+
## AWS Policies
6+
7+
### S3 Bucket Policies
8+
9+
#### AWS_S3_VERSIONING
10+
- **Severity**: High
11+
- **Description**: Ensure S3 buckets have versioning enabled
12+
- **Resource Type**: aws_s3_bucket
13+
- **Condition**: Check if versioning is enabled
14+
- **Remediation**: Enable versioning on the S3 bucket using versioning configuration block
15+
16+
#### AWS_S3_ENCRYPTION
17+
- **Severity**: High
18+
- **Description**: Ensure S3 buckets have encryption enabled
19+
- **Resource Type**: aws_s3_bucket
20+
- **Condition**: Check if server-side encryption is configured
21+
- **Remediation**: Configure server-side encryption using server_side_encryption_configuration block
22+
23+
[Note: This file should be expanded with all default policies from the policies/*.yaml files]

0 commit comments

Comments
 (0)