driftwatch-cli 3.0.1__tar.gz → 3.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- driftwatch_cli-3.0.2/PKG-INFO +181 -0
- driftwatch_cli-3.0.2/README.md +144 -0
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/drift_engine/aws_client.py +67 -41
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/drift_engine/core.py +52 -17
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/drift_engine/database.py +6 -1
- driftwatch_cli-3.0.2/drift_engine/explain.py +72 -0
- driftwatch_cli-3.0.2/drift_engine/models.py +65 -0
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/drift_engine/notifications.py +25 -32
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/drift_engine/remediation.py +114 -54
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/drift_engine/tf_parser.py +7 -6
- driftwatch_cli-3.0.2/driftwatch/cli.py +367 -0
- driftwatch_cli-3.0.2/driftwatch_cli.egg-info/PKG-INFO +181 -0
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/driftwatch_cli.egg-info/SOURCES.txt +6 -1
- driftwatch_cli-3.0.2/driftwatch_cli.egg-info/requires.txt +14 -0
- driftwatch_cli-3.0.2/pyproject.toml +56 -0
- driftwatch_cli-3.0.2/tests/test_aws_client.py +148 -0
- driftwatch_cli-3.0.2/tests/test_cli.py +414 -0
- driftwatch_cli-3.0.2/tests/test_diff_engine.py +207 -0
- driftwatch_cli-3.0.2/tests/test_explain_notifications.py +173 -0
- driftwatch_cli-3.0.2/tests/test_remediation.py +284 -0
- driftwatch_cli-3.0.1/PKG-INFO +0 -17
- driftwatch_cli-3.0.1/drift_engine/explain.py +0 -47
- driftwatch_cli-3.0.1/drift_engine/models.py +0 -42
- driftwatch_cli-3.0.1/driftwatch/cli.py +0 -174
- driftwatch_cli-3.0.1/driftwatch_cli.egg-info/PKG-INFO +0 -17
- driftwatch_cli-3.0.1/driftwatch_cli.egg-info/requires.txt +0 -7
- driftwatch_cli-3.0.1/pyproject.toml +0 -31
- driftwatch_cli-3.0.1/tests/test_diff_engine.py +0 -39
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/LICENSE +0 -0
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/drift_engine/__init__.py +0 -0
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/driftwatch/__init__.py +0 -0
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/driftwatch_cli.egg-info/dependency_links.txt +0 -0
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/driftwatch_cli.egg-info/entry_points.txt +0 -0
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/driftwatch_cli.egg-info/top_level.txt +0 -0
- {driftwatch_cli-3.0.1 → driftwatch_cli-3.0.2}/setup.cfg +0 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: driftwatch-cli
|
|
3
|
+
Version: 3.0.2
|
|
4
|
+
Summary: CLI tool that detects Terraform infrastructure drift against live AWS, explains it with AI, and guides remediation.
|
|
5
|
+
Author: Nitin Gupta
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/hastagnitin/driftwatch
|
|
8
|
+
Project-URL: Repository, https://github.com/hastagnitin/driftwatch
|
|
9
|
+
Keywords: terraform,aws,drift-detection,iac,devops,cloud-security,remediation
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Intended Audience :: System Administrators
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: System :: Systems Administration
|
|
20
|
+
Classifier: Topic :: Security
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: boto3>=1.34.0
|
|
25
|
+
Requires-Dist: typer>=0.9.0
|
|
26
|
+
Requires-Dist: requests>=2.31.0
|
|
27
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
28
|
+
Provides-Extra: postgres
|
|
29
|
+
Requires-Dist: psycopg2-binary>=2.9.0; extra == "postgres"
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
32
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
33
|
+
Requires-Dist: moto[all]>=5.0.0; extra == "dev"
|
|
34
|
+
Requires-Dist: build; extra == "dev"
|
|
35
|
+
Requires-Dist: twine; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# DriftWatch 🛡️
|
|
39
|
+
|
|
40
|
+
**DriftWatch** is a production-ready CLI tool and automation engine that detects Terraform infrastructure drift against live AWS environments, explains the security and reliability impact using AI, and safely guides remediation.
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## 🚀 Key Features
|
|
45
|
+
|
|
46
|
+
- **Multi-Resource Drift Detection**: Continuously monitors and compares EC2 instances, S3 buckets, Security Groups, RDS databases, Lambda functions, and IAM roles against your Terraform state.
|
|
47
|
+
- **Data-Driven Severity Scoring**: Evaluates changes dynamically at the attribute level (e.g. security group open ports vs description updates) to classify drifts as `CRITICAL`, `HIGH`, `MEDIUM`, or `LOW`.
|
|
48
|
+
- **AI-Powered Risk Summaries**: Integrates with LLMs to provide plain-English security analysis and compliance impact assessments.
|
|
49
|
+
- **Deterministic IaC Remediation**: Recommends safe, template-generated `terraform import` and `terraform apply` commands rather than hallucinated AI outputs.
|
|
50
|
+
- **Guarded Auto-Remediation**: Pre-flight validation checks for EC2 (EBS verification, Spot skip, running state), RDS maintenance-window defaults, and explicit interactive confirmations.
|
|
51
|
+
- **Multi-Channel Alerting**: Instant notifications via Telegram, Slack, and Email.
|
|
52
|
+
- **CI/CD Quality Gate**: Built-in GitHub Actions integration to enforce zero-tolerance drift policies in pull requests.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## 🏛️ Architecture Overview
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
driftwatch/
|
|
60
|
+
├── drift_engine/ # Core drift detection & reconciliation engine
|
|
61
|
+
│ ├── aws_client.py # Live AWS resource discovery (boto3)
|
|
62
|
+
│ ├── core.py # Diff evaluation & data-driven severity engine
|
|
63
|
+
│ ├── database.py # PostgreSQL scan history recorder
|
|
64
|
+
│ ├── explain.py # AI risk summaries & deterministic IaC templates
|
|
65
|
+
│ ├── models.py # Data models & attribute severity tables
|
|
66
|
+
│ ├── notifications.py # Alert dispatcher (Telegram, Slack, Email)
|
|
67
|
+
│ ├── remediation.py # Guarded auto-remediation handlers
|
|
68
|
+
│ └── tf_parser.py # Terraform state JSON parser
|
|
69
|
+
├── driftwatch/ # CLI Entrypoint (Typer)
|
|
70
|
+
│ └── cli.py # Command definitions: scan, explain, remediate
|
|
71
|
+
├── terraform/ # Example infrastructure and state configuration
|
|
72
|
+
├── kubernetes/ # Kubernetes CronJob deployment
|
|
73
|
+
└── tests/ # Comprehensive unit tests with moto AWS mocks
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## 📋 Prerequisites
|
|
79
|
+
|
|
80
|
+
- **Python**: `>= 3.10`
|
|
81
|
+
- **AWS Credentials**: Configured via environment variables, IAM roles, or AWS CLI credentials (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_DEFAULT_REGION`).
|
|
82
|
+
- **Terraform State File**: Local JSON state or remote state (`terraform.tfstate`).
|
|
83
|
+
- **PostgreSQL** *(Optional)*: For persistent scan audit history.
|
|
84
|
+
- **Groq API Key** *(Optional)*: `GROQ_API_KEY` for AI risk explanations.
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## 📦 Installation
|
|
89
|
+
|
|
90
|
+
### From PyPI (Recommended)
|
|
91
|
+
```bash
|
|
92
|
+
pip install driftwatch-cli
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### From Source (Local Development)
|
|
96
|
+
```bash
|
|
97
|
+
git clone https://github.com/hastagnitin/driftwatch.git
|
|
98
|
+
cd driftwatch
|
|
99
|
+
pip install -e .[dev]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## ⚙️ Configuration
|
|
105
|
+
|
|
106
|
+
Create a `.env` file in the root directory:
|
|
107
|
+
|
|
108
|
+
```env
|
|
109
|
+
AWS_DEFAULT_REGION=ap-south-1
|
|
110
|
+
TF_STATE_PATH=terraform/terraform.tfstate
|
|
111
|
+
|
|
112
|
+
# Optional: AI Risk Summaries
|
|
113
|
+
GROQ_API_KEY=your_groq_api_key
|
|
114
|
+
|
|
115
|
+
# Optional: Notifications
|
|
116
|
+
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/...
|
|
117
|
+
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
|
|
118
|
+
TELEGRAM_CHAT_ID=your_telegram_chat_id
|
|
119
|
+
|
|
120
|
+
# Optional: PostgreSQL Database
|
|
121
|
+
DB_HOST=localhost
|
|
122
|
+
DB_PORT=5432
|
|
123
|
+
DB_NAME=driftwatch
|
|
124
|
+
DB_USER=postgres
|
|
125
|
+
DB_PASSWORD=your_db_password
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## 💻 Usage & CLI Commands
|
|
131
|
+
|
|
132
|
+
### 1. Scan for Drift
|
|
133
|
+
Scan live AWS infrastructure against your Terraform state:
|
|
134
|
+
```bash
|
|
135
|
+
# Basic scan
|
|
136
|
+
driftwatch scan --region ap-south-1 --state terraform/terraform.tfstate
|
|
137
|
+
|
|
138
|
+
# Enforce CI Gate (fails build if CRITICAL drift is found)
|
|
139
|
+
driftwatch scan --region ap-south-1 --fail-on CRITICAL
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### 2. Explain Drift
|
|
143
|
+
Generate AI risk analysis and deterministic IaC fix recommendations:
|
|
144
|
+
```bash
|
|
145
|
+
driftwatch explain sg-0123456789abcdef0 --region ap-south-1
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### 3. Remediate Drift
|
|
149
|
+
Safely remediate drifted resources back to IaC specifications:
|
|
150
|
+
```bash
|
|
151
|
+
# Dry run mode (default)
|
|
152
|
+
driftwatch remediate sg-0123456789abcdef0 --region ap-south-1 --dry-run
|
|
153
|
+
|
|
154
|
+
# Apply mode with interactive confirmation
|
|
155
|
+
driftwatch remediate sg-0123456789abcdef0 --region ap-south-1 --apply
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## ⚠️ Security & Safety Guidelines
|
|
161
|
+
|
|
162
|
+
> [!WARNING]
|
|
163
|
+
> **Auto-Remediation Safety**:
|
|
164
|
+
> - Automated drift remediation is intended for **Development** and **Staging** environments.
|
|
165
|
+
> - In **Production**, DriftWatch enforces manual confirmation prompts (`confirm_action()`) and recommends template-generated `terraform apply` / `terraform import` workflows.
|
|
166
|
+
> - RDS modifications default to maintenance windows (`ApplyImmediately=False`) to avoid unplanned reboots.
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## 🧪 Testing
|
|
171
|
+
|
|
172
|
+
Run the test suite with test coverage:
|
|
173
|
+
```bash
|
|
174
|
+
pytest tests/ -v --cov=drift_engine --cov=driftwatch --cov-report=term-missing
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## 📄 License
|
|
180
|
+
|
|
181
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# DriftWatch 🛡️
|
|
2
|
+
|
|
3
|
+
**DriftWatch** is a production-ready CLI tool and automation engine that detects Terraform infrastructure drift against live AWS environments, explains the security and reliability impact using AI, and safely guides remediation.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 🚀 Key Features
|
|
8
|
+
|
|
9
|
+
- **Multi-Resource Drift Detection**: Continuously monitors and compares EC2 instances, S3 buckets, Security Groups, RDS databases, Lambda functions, and IAM roles against your Terraform state.
|
|
10
|
+
- **Data-Driven Severity Scoring**: Evaluates changes dynamically at the attribute level (e.g. security group open ports vs description updates) to classify drifts as `CRITICAL`, `HIGH`, `MEDIUM`, or `LOW`.
|
|
11
|
+
- **AI-Powered Risk Summaries**: Integrates with LLMs to provide plain-English security analysis and compliance impact assessments.
|
|
12
|
+
- **Deterministic IaC Remediation**: Recommends safe, template-generated `terraform import` and `terraform apply` commands rather than hallucinated AI outputs.
|
|
13
|
+
- **Guarded Auto-Remediation**: Pre-flight validation checks for EC2 (EBS verification, Spot skip, running state), RDS maintenance-window defaults, and explicit interactive confirmations.
|
|
14
|
+
- **Multi-Channel Alerting**: Instant notifications via Telegram, Slack, and Email.
|
|
15
|
+
- **CI/CD Quality Gate**: Built-in GitHub Actions integration to enforce zero-tolerance drift policies in pull requests.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## 🏛️ Architecture Overview
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
driftwatch/
|
|
23
|
+
├── drift_engine/ # Core drift detection & reconciliation engine
|
|
24
|
+
│ ├── aws_client.py # Live AWS resource discovery (boto3)
|
|
25
|
+
│ ├── core.py # Diff evaluation & data-driven severity engine
|
|
26
|
+
│ ├── database.py # PostgreSQL scan history recorder
|
|
27
|
+
│ ├── explain.py # AI risk summaries & deterministic IaC templates
|
|
28
|
+
│ ├── models.py # Data models & attribute severity tables
|
|
29
|
+
│ ├── notifications.py # Alert dispatcher (Telegram, Slack, Email)
|
|
30
|
+
│ ├── remediation.py # Guarded auto-remediation handlers
|
|
31
|
+
│ └── tf_parser.py # Terraform state JSON parser
|
|
32
|
+
├── driftwatch/ # CLI Entrypoint (Typer)
|
|
33
|
+
│ └── cli.py # Command definitions: scan, explain, remediate
|
|
34
|
+
├── terraform/ # Example infrastructure and state configuration
|
|
35
|
+
├── kubernetes/ # Kubernetes CronJob deployment
|
|
36
|
+
└── tests/ # Comprehensive unit tests with moto AWS mocks
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## 📋 Prerequisites
|
|
42
|
+
|
|
43
|
+
- **Python**: `>= 3.10`
|
|
44
|
+
- **AWS Credentials**: Configured via environment variables, IAM roles, or AWS CLI credentials (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_DEFAULT_REGION`).
|
|
45
|
+
- **Terraform State File**: Local JSON state or remote state (`terraform.tfstate`).
|
|
46
|
+
- **PostgreSQL** *(Optional)*: For persistent scan audit history.
|
|
47
|
+
- **Groq API Key** *(Optional)*: `GROQ_API_KEY` for AI risk explanations.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## 📦 Installation
|
|
52
|
+
|
|
53
|
+
### From PyPI (Recommended)
|
|
54
|
+
```bash
|
|
55
|
+
pip install driftwatch-cli
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### From Source (Local Development)
|
|
59
|
+
```bash
|
|
60
|
+
git clone https://github.com/hastagnitin/driftwatch.git
|
|
61
|
+
cd driftwatch
|
|
62
|
+
pip install -e .[dev]
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## ⚙️ Configuration
|
|
68
|
+
|
|
69
|
+
Create a `.env` file in the root directory:
|
|
70
|
+
|
|
71
|
+
```env
|
|
72
|
+
AWS_DEFAULT_REGION=ap-south-1
|
|
73
|
+
TF_STATE_PATH=terraform/terraform.tfstate
|
|
74
|
+
|
|
75
|
+
# Optional: AI Risk Summaries
|
|
76
|
+
GROQ_API_KEY=your_groq_api_key
|
|
77
|
+
|
|
78
|
+
# Optional: Notifications
|
|
79
|
+
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/...
|
|
80
|
+
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
|
|
81
|
+
TELEGRAM_CHAT_ID=your_telegram_chat_id
|
|
82
|
+
|
|
83
|
+
# Optional: PostgreSQL Database
|
|
84
|
+
DB_HOST=localhost
|
|
85
|
+
DB_PORT=5432
|
|
86
|
+
DB_NAME=driftwatch
|
|
87
|
+
DB_USER=postgres
|
|
88
|
+
DB_PASSWORD=your_db_password
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## 💻 Usage & CLI Commands
|
|
94
|
+
|
|
95
|
+
### 1. Scan for Drift
|
|
96
|
+
Scan live AWS infrastructure against your Terraform state:
|
|
97
|
+
```bash
|
|
98
|
+
# Basic scan
|
|
99
|
+
driftwatch scan --region ap-south-1 --state terraform/terraform.tfstate
|
|
100
|
+
|
|
101
|
+
# Enforce CI Gate (fails build if CRITICAL drift is found)
|
|
102
|
+
driftwatch scan --region ap-south-1 --fail-on CRITICAL
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### 2. Explain Drift
|
|
106
|
+
Generate AI risk analysis and deterministic IaC fix recommendations:
|
|
107
|
+
```bash
|
|
108
|
+
driftwatch explain sg-0123456789abcdef0 --region ap-south-1
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### 3. Remediate Drift
|
|
112
|
+
Safely remediate drifted resources back to IaC specifications:
|
|
113
|
+
```bash
|
|
114
|
+
# Dry run mode (default)
|
|
115
|
+
driftwatch remediate sg-0123456789abcdef0 --region ap-south-1 --dry-run
|
|
116
|
+
|
|
117
|
+
# Apply mode with interactive confirmation
|
|
118
|
+
driftwatch remediate sg-0123456789abcdef0 --region ap-south-1 --apply
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## ⚠️ Security & Safety Guidelines
|
|
124
|
+
|
|
125
|
+
> [!WARNING]
|
|
126
|
+
> **Auto-Remediation Safety**:
|
|
127
|
+
> - Automated drift remediation is intended for **Development** and **Staging** environments.
|
|
128
|
+
> - In **Production**, DriftWatch enforces manual confirmation prompts (`confirm_action()`) and recommends template-generated `terraform apply` / `terraform import` workflows.
|
|
129
|
+
> - RDS modifications default to maintenance windows (`ApplyImmediately=False`) to avoid unplanned reboots.
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## 🧪 Testing
|
|
134
|
+
|
|
135
|
+
Run the test suite with test coverage:
|
|
136
|
+
```bash
|
|
137
|
+
pytest tests/ -v --cov=drift_engine --cov=driftwatch --cov-report=term-missing
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## 📄 License
|
|
143
|
+
|
|
144
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -1,35 +1,48 @@
|
|
|
1
|
+
import os
|
|
1
2
|
import sys
|
|
2
3
|
import boto3
|
|
3
4
|
from datetime import datetime, timedelta
|
|
4
5
|
|
|
5
|
-
def
|
|
6
|
-
|
|
6
|
+
def get_boto3_session(profile: str = None, region: str = None) -> boto3.Session:
|
|
7
|
+
actual_profile = profile or os.environ.get("AWS_PROFILE")
|
|
8
|
+
actual_region = region or os.environ.get("AWS_DEFAULT_REGION")
|
|
9
|
+
if actual_profile:
|
|
10
|
+
return boto3.Session(profile_name=actual_profile, region_name=actual_region)
|
|
11
|
+
return boto3.Session(region_name=actual_region)
|
|
12
|
+
|
|
13
|
+
def get_boto3_client(service_name: str, profile: str = None, region: str = None):
|
|
14
|
+
session = get_boto3_session(profile=profile, region=region)
|
|
15
|
+
return session.client(service_name, region_name=region or session.region_name)
|
|
16
|
+
|
|
17
|
+
def fetch_live_ec2_instances(region: str, profile: str = None) -> dict:
|
|
18
|
+
ec2 = get_boto3_client("ec2", profile=profile, region=region)
|
|
7
19
|
live = {}
|
|
8
20
|
try:
|
|
9
21
|
paginator = ec2.get_paginator("describe_instances")
|
|
10
|
-
|
|
11
22
|
for page in paginator.paginate():
|
|
12
|
-
for reservation in page
|
|
13
|
-
for instance in reservation
|
|
14
|
-
if instance
|
|
23
|
+
for reservation in page.get("Reservations", []):
|
|
24
|
+
for instance in reservation.get("Instances", []):
|
|
25
|
+
if instance.get("State", {}).get("Name") == "terminated":
|
|
15
26
|
continue
|
|
16
27
|
|
|
17
28
|
tags_list = instance.get("Tags", [])
|
|
18
|
-
tags_dict = {t["Key"]: t["Value"] for t in tags_list}
|
|
29
|
+
tags_dict = {t["Key"]: t["Value"] for t in tags_list if "Key" in t and "Value" in t}
|
|
19
30
|
name = tags_dict.get("Name", "Unknown")
|
|
20
31
|
|
|
21
32
|
sg_ids = []
|
|
22
33
|
for sg in instance.get("SecurityGroups", []):
|
|
23
|
-
|
|
34
|
+
if "GroupId" in sg:
|
|
35
|
+
sg_ids.append(sg.get("GroupId"))
|
|
24
36
|
sg_ids.sort()
|
|
25
37
|
|
|
26
|
-
|
|
38
|
+
instance_id = instance["InstanceId"]
|
|
39
|
+
live[instance_id] = {
|
|
27
40
|
"type": "aws_instance",
|
|
28
41
|
"name": name,
|
|
29
42
|
"attributes": {
|
|
30
|
-
"id":
|
|
31
|
-
"instance_type": instance
|
|
32
|
-
"ami": instance
|
|
43
|
+
"id": instance_id,
|
|
44
|
+
"instance_type": instance.get("InstanceType"),
|
|
45
|
+
"ami": instance.get("ImageId"),
|
|
33
46
|
"tags": tags_dict,
|
|
34
47
|
"vpc_security_group_ids": sg_ids
|
|
35
48
|
},
|
|
@@ -39,8 +52,8 @@ def fetch_live_ec2_instances(region: str) -> dict:
|
|
|
39
52
|
return None
|
|
40
53
|
return live
|
|
41
54
|
|
|
42
|
-
def fetch_live_s3_buckets(region: str) -> dict:
|
|
43
|
-
s3 =
|
|
55
|
+
def fetch_live_s3_buckets(region: str, profile: str = None) -> dict:
|
|
56
|
+
s3 = get_boto3_client("s3", profile=profile, region=region)
|
|
44
57
|
live = {}
|
|
45
58
|
try:
|
|
46
59
|
response = s3.list_buckets()
|
|
@@ -49,7 +62,7 @@ def fetch_live_s3_buckets(region: str) -> dict:
|
|
|
49
62
|
try:
|
|
50
63
|
tags_response = s3.get_bucket_tagging(Bucket=bucket_name)
|
|
51
64
|
tags_list = tags_response.get("TagSet", [])
|
|
52
|
-
tags_dict = {t["Key"]: t["Value"] for t in tags_list}
|
|
65
|
+
tags_dict = {t["Key"]: t["Value"] for t in tags_list if "Key" in t and "Value" in t}
|
|
53
66
|
name = tags_dict.get("Name", bucket_name)
|
|
54
67
|
except Exception:
|
|
55
68
|
tags_dict = {}
|
|
@@ -69,18 +82,18 @@ def fetch_live_s3_buckets(region: str) -> dict:
|
|
|
69
82
|
return None
|
|
70
83
|
return live
|
|
71
84
|
|
|
72
|
-
def fetch_live_security_groups(region: str) -> dict:
|
|
73
|
-
ec2 =
|
|
85
|
+
def fetch_live_security_groups(region: str, profile: str = None) -> dict:
|
|
86
|
+
ec2 = get_boto3_client("ec2", profile=profile, region=region)
|
|
74
87
|
live = {}
|
|
75
88
|
try:
|
|
76
89
|
paginator = ec2.get_paginator("describe_security_groups")
|
|
77
90
|
for page in paginator.paginate():
|
|
78
|
-
for sg in page
|
|
91
|
+
for sg in page.get("SecurityGroups", []):
|
|
79
92
|
sg_id = sg["GroupId"]
|
|
80
|
-
sg_name = sg
|
|
93
|
+
sg_name = sg.get("GroupName", "")
|
|
81
94
|
|
|
82
95
|
tags_list = sg.get("Tags", [])
|
|
83
|
-
tags_dict = {t["Key"]: t["Value"] for t in tags_list}
|
|
96
|
+
tags_dict = {t["Key"]: t["Value"] for t in tags_list if "Key" in t and "Value" in t}
|
|
84
97
|
name_tag = tags_dict.get("Name", sg_name)
|
|
85
98
|
|
|
86
99
|
ingress_rules = []
|
|
@@ -122,22 +135,22 @@ def fetch_live_security_groups(region: str) -> dict:
|
|
|
122
135
|
return None
|
|
123
136
|
return live
|
|
124
137
|
|
|
125
|
-
def fetch_live_rds_instances(region: str) -> dict:
|
|
126
|
-
rds =
|
|
138
|
+
def fetch_live_rds_instances(region: str, profile: str = None) -> dict:
|
|
139
|
+
rds = get_boto3_client("rds", profile=profile, region=region)
|
|
127
140
|
live = {}
|
|
128
141
|
try:
|
|
129
142
|
paginator = rds.get_paginator("describe_db_instances")
|
|
130
143
|
for page in paginator.paginate():
|
|
131
|
-
for db in page
|
|
132
|
-
db_id = db.get("
|
|
133
|
-
db_name = db["DBInstanceIdentifier"]
|
|
144
|
+
for db in page.get("DBInstances", []):
|
|
145
|
+
db_id = db.get("DBInstanceIdentifier")
|
|
134
146
|
if not db_id:
|
|
135
147
|
continue
|
|
136
148
|
live[db_id] = {
|
|
137
149
|
"type": "aws_db_instance",
|
|
138
|
-
"name":
|
|
150
|
+
"name": db_id,
|
|
139
151
|
"attributes": {
|
|
140
152
|
"id": db_id,
|
|
153
|
+
"identifier": db_id,
|
|
141
154
|
"allocated_storage": db.get("AllocatedStorage"),
|
|
142
155
|
"engine": db.get("Engine"),
|
|
143
156
|
"engine_version": db.get("EngineVersion"),
|
|
@@ -150,14 +163,16 @@ def fetch_live_rds_instances(region: str) -> dict:
|
|
|
150
163
|
return None
|
|
151
164
|
return live
|
|
152
165
|
|
|
153
|
-
def fetch_live_lambda_functions(region: str) -> dict:
|
|
154
|
-
lambda_client =
|
|
166
|
+
def fetch_live_lambda_functions(region: str, profile: str = None) -> dict:
|
|
167
|
+
lambda_client = get_boto3_client("lambda", profile=profile, region=region)
|
|
155
168
|
live = {}
|
|
156
169
|
try:
|
|
157
170
|
paginator = lambda_client.get_paginator("list_functions")
|
|
158
171
|
for page in paginator.paginate():
|
|
159
|
-
for func in page
|
|
160
|
-
func_name = func
|
|
172
|
+
for func in page.get("Functions", []):
|
|
173
|
+
func_name = func.get("FunctionName")
|
|
174
|
+
if not func_name:
|
|
175
|
+
continue
|
|
161
176
|
live[func_name] = {
|
|
162
177
|
"type": "aws_lambda_function",
|
|
163
178
|
"name": func_name,
|
|
@@ -176,19 +191,26 @@ def fetch_live_lambda_functions(region: str) -> dict:
|
|
|
176
191
|
return None
|
|
177
192
|
return live
|
|
178
193
|
|
|
179
|
-
def fetch_live_iam_roles(region: str) -> dict:
|
|
180
|
-
iam =
|
|
194
|
+
def fetch_live_iam_roles(region: str, profile: str = None) -> dict:
|
|
195
|
+
iam = get_boto3_client("iam", profile=profile, region=region)
|
|
181
196
|
live = {}
|
|
182
197
|
try:
|
|
183
198
|
paginator = iam.get_paginator("list_roles")
|
|
184
199
|
for page in paginator.paginate():
|
|
185
|
-
for role in page
|
|
186
|
-
role_name = role
|
|
200
|
+
for role in page.get("Roles", []):
|
|
201
|
+
role_name = role.get("RoleName")
|
|
202
|
+
if not role_name:
|
|
203
|
+
continue
|
|
187
204
|
if role_name.startswith("AWSServiceRoleFor") or role.get("Path", "").startswith("/aws-service-role/"):
|
|
188
205
|
continue
|
|
189
206
|
|
|
190
|
-
|
|
191
|
-
|
|
207
|
+
try:
|
|
208
|
+
policies = iam.list_attached_role_policies(RoleName=role_name)
|
|
209
|
+
attached_policies = sorted(
|
|
210
|
+
p["PolicyArn"] for p in policies.get("AttachedPolicies", []) if "PolicyArn" in p
|
|
211
|
+
)
|
|
212
|
+
except Exception:
|
|
213
|
+
attached_policies = []
|
|
192
214
|
|
|
193
215
|
live[role_name] = {
|
|
194
216
|
"type": "aws_iam_role",
|
|
@@ -196,7 +218,8 @@ def fetch_live_iam_roles(region: str) -> dict:
|
|
|
196
218
|
"attributes": {
|
|
197
219
|
"id": role_name,
|
|
198
220
|
"name": role_name,
|
|
199
|
-
"
|
|
221
|
+
"path": role.get("Path", "/"),
|
|
222
|
+
"arn": role.get("Arn", ""),
|
|
200
223
|
"attached_policies": attached_policies
|
|
201
224
|
}
|
|
202
225
|
}
|
|
@@ -205,9 +228,9 @@ def fetch_live_iam_roles(region: str) -> dict:
|
|
|
205
228
|
return None
|
|
206
229
|
return live
|
|
207
230
|
|
|
208
|
-
def get_resource_cost(resource_id: str) -> float:
|
|
231
|
+
def get_resource_cost(resource_id: str, profile: str = None) -> float | None:
|
|
209
232
|
try:
|
|
210
|
-
client =
|
|
233
|
+
client = get_boto3_client("ce", profile=profile, region="us-east-1")
|
|
211
234
|
|
|
212
235
|
end_date = datetime.today().strftime("%Y-%m-%d")
|
|
213
236
|
start_date = (datetime.today() - timedelta(days=30)).strftime("%Y-%m-%d")
|
|
@@ -224,7 +247,10 @@ def get_resource_cost(resource_id: str) -> float:
|
|
|
224
247
|
}
|
|
225
248
|
)
|
|
226
249
|
|
|
227
|
-
|
|
250
|
+
results_by_time = response.get("ResultsByTime", [])
|
|
251
|
+
if not results_by_time:
|
|
252
|
+
return None
|
|
253
|
+
usd_cost = float(results_by_time[0]["Total"]["UnblendedCost"]["Amount"])
|
|
228
254
|
return usd_cost
|
|
229
255
|
except Exception:
|
|
230
|
-
return
|
|
256
|
+
return None
|
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
import os
|
|
2
|
-
|
|
2
|
+
import sys
|
|
3
|
+
from drift_engine.models import (
|
|
4
|
+
DriftResult,
|
|
5
|
+
DriftType,
|
|
6
|
+
MONITORED_ATTRIBUTES,
|
|
7
|
+
ATTRIBUTE_SEVERITY
|
|
8
|
+
)
|
|
3
9
|
from drift_engine.tf_parser import load_terraform_state
|
|
4
10
|
from drift_engine.aws_client import (
|
|
5
|
-
fetch_live_ec2_instances,
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
fetch_live_ec2_instances,
|
|
12
|
+
fetch_live_s3_buckets,
|
|
13
|
+
fetch_live_security_groups,
|
|
14
|
+
fetch_live_rds_instances,
|
|
15
|
+
fetch_live_lambda_functions,
|
|
16
|
+
fetch_live_iam_roles
|
|
8
17
|
)
|
|
9
18
|
|
|
19
|
+
SEVERITY_LEVELS = {"LOW": 0, "MEDIUM": 1, "HIGH": 2, "CRITICAL": 3}
|
|
20
|
+
|
|
10
21
|
def normalize_sg_rules(rules) -> list:
|
|
11
22
|
normalized = []
|
|
12
23
|
if not isinstance(rules, list):
|
|
@@ -56,43 +67,43 @@ def compare_attributes(tf, live, r_type) -> dict:
|
|
|
56
67
|
|
|
57
68
|
return diff
|
|
58
69
|
|
|
59
|
-
def detect_drift(tf_state_path: str, region: str):
|
|
70
|
+
def detect_drift(tf_state_path: str, region: str, profile: str = None):
|
|
60
71
|
tf_resources = load_terraform_state(tf_state_path)
|
|
61
|
-
|
|
62
|
-
if not tf_resources:
|
|
63
|
-
return [], 0
|
|
64
72
|
|
|
65
73
|
failed_types = set()
|
|
66
74
|
|
|
67
|
-
live_ec2 = fetch_live_ec2_instances(region)
|
|
75
|
+
live_ec2 = fetch_live_ec2_instances(region, profile=profile)
|
|
68
76
|
if live_ec2 is None:
|
|
69
77
|
failed_types.add("aws_instance")
|
|
70
78
|
live_ec2 = {}
|
|
71
79
|
|
|
72
|
-
live_s3 = fetch_live_s3_buckets(region)
|
|
80
|
+
live_s3 = fetch_live_s3_buckets(region, profile=profile)
|
|
73
81
|
if live_s3 is None:
|
|
74
82
|
failed_types.add("aws_s3_bucket")
|
|
75
83
|
live_s3 = {}
|
|
76
84
|
|
|
77
|
-
live_sg = fetch_live_security_groups(region)
|
|
85
|
+
live_sg = fetch_live_security_groups(region, profile=profile)
|
|
78
86
|
if live_sg is None:
|
|
79
87
|
failed_types.add("aws_security_group")
|
|
80
88
|
live_sg = {}
|
|
81
89
|
|
|
82
|
-
live_rds = fetch_live_rds_instances(region)
|
|
90
|
+
live_rds = fetch_live_rds_instances(region, profile=profile)
|
|
83
91
|
if live_rds is None:
|
|
84
92
|
failed_types.add("aws_db_instance")
|
|
85
93
|
live_rds = {}
|
|
86
94
|
|
|
87
|
-
live_lambda = fetch_live_lambda_functions(region)
|
|
95
|
+
live_lambda = fetch_live_lambda_functions(region, profile=profile)
|
|
88
96
|
if live_lambda is None:
|
|
89
97
|
failed_types.add("aws_lambda_function")
|
|
90
98
|
live_lambda = {}
|
|
91
99
|
|
|
92
|
-
live_iam = fetch_live_iam_roles(region)
|
|
100
|
+
live_iam = fetch_live_iam_roles(region, profile=profile)
|
|
93
101
|
if live_iam is None:
|
|
94
102
|
failed_types.add("aws_iam_role")
|
|
95
103
|
live_iam = {}
|
|
104
|
+
|
|
105
|
+
if failed_types:
|
|
106
|
+
raise RuntimeError(f"Failed to fetch live AWS resources for: {', '.join(sorted(failed_types))}")
|
|
96
107
|
|
|
97
108
|
live_resources = {**live_ec2, **live_s3, **live_sg, **live_rds, **live_lambda, **live_iam}
|
|
98
109
|
|
|
@@ -147,9 +158,33 @@ def detect_drift(tf_state_path: str, region: str):
|
|
|
147
158
|
|
|
148
159
|
return results, total_scanned
|
|
149
160
|
|
|
150
|
-
def get_severity(r_type, d_type):
|
|
161
|
+
def get_severity(r_type: str, d_type: DriftType, diff: dict = None) -> str:
|
|
162
|
+
if d_type == DriftType.MISSING or d_type == DriftType.UNMANAGED:
|
|
163
|
+
if r_type in ["aws_security_group", "aws_iam_role"]:
|
|
164
|
+
return "CRITICAL"
|
|
165
|
+
return "HIGH"
|
|
166
|
+
|
|
167
|
+
if diff:
|
|
168
|
+
type_severity_map = ATTRIBUTE_SEVERITY.get(r_type, {})
|
|
169
|
+
highest_sev = "LOW"
|
|
170
|
+
for attr in diff.keys():
|
|
171
|
+
attr_sev = type_severity_map.get(attr, "MEDIUM")
|
|
172
|
+
if SEVERITY_LEVELS.get(attr_sev, 1) > SEVERITY_LEVELS.get(highest_sev, 0):
|
|
173
|
+
highest_sev = attr_sev
|
|
174
|
+
return highest_sev
|
|
175
|
+
|
|
151
176
|
if r_type in ["aws_security_group", "aws_iam_role"]:
|
|
152
177
|
return "CRITICAL"
|
|
153
|
-
if
|
|
178
|
+
if r_type == "aws_instance":
|
|
154
179
|
return "HIGH"
|
|
155
|
-
return "MEDIUM"
|
|
180
|
+
return "MEDIUM"
|
|
181
|
+
|
|
182
|
+
if __name__ == "__main__":
|
|
183
|
+
state_file = os.environ.get("TF_STATE_PATH", "terraform/terraform.tfstate")
|
|
184
|
+
scan_region = os.environ.get("AWS_DEFAULT_REGION", "ap-south-1")
|
|
185
|
+
print(f"Executing DriftWatch Engine on {state_file} ({scan_region})...")
|
|
186
|
+
drift_items, scanned = detect_drift(state_file, scan_region)
|
|
187
|
+
print(f"Scanned {scanned} resources. Found {len(drift_items)} drift items.")
|
|
188
|
+
for item in drift_items:
|
|
189
|
+
sev = get_severity(item.resource_type, item.drift_type, item.diff)
|
|
190
|
+
print(f" - [{item.drift_type.value}] {item.resource_type} ({item.resource_id}): Severity={sev}")
|