aws-inventory-manager 0.17.12__py3-none-any.whl
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.
- aws_inventory_manager-0.17.12.dist-info/LICENSE +21 -0
- aws_inventory_manager-0.17.12.dist-info/METADATA +1292 -0
- aws_inventory_manager-0.17.12.dist-info/RECORD +152 -0
- aws_inventory_manager-0.17.12.dist-info/WHEEL +5 -0
- aws_inventory_manager-0.17.12.dist-info/entry_points.txt +2 -0
- aws_inventory_manager-0.17.12.dist-info/top_level.txt +1 -0
- src/__init__.py +3 -0
- src/aws/__init__.py +11 -0
- src/aws/client.py +128 -0
- src/aws/credentials.py +191 -0
- src/aws/rate_limiter.py +177 -0
- src/cli/__init__.py +12 -0
- src/cli/config.py +130 -0
- src/cli/main.py +4046 -0
- src/cloudtrail/__init__.py +5 -0
- src/cloudtrail/query.py +642 -0
- src/config_service/__init__.py +21 -0
- src/config_service/collector.py +346 -0
- src/config_service/detector.py +256 -0
- src/config_service/resource_type_mapping.py +328 -0
- src/cost/__init__.py +5 -0
- src/cost/analyzer.py +226 -0
- src/cost/explorer.py +209 -0
- src/cost/reporter.py +237 -0
- src/delta/__init__.py +5 -0
- src/delta/calculator.py +206 -0
- src/delta/differ.py +185 -0
- src/delta/formatters.py +272 -0
- src/delta/models.py +154 -0
- src/delta/reporter.py +234 -0
- src/matching/__init__.py +6 -0
- src/matching/config.py +52 -0
- src/matching/normalizer.py +450 -0
- src/matching/prompts.py +33 -0
- src/models/__init__.py +21 -0
- src/models/config_diff.py +135 -0
- src/models/cost_report.py +87 -0
- src/models/deletion_operation.py +104 -0
- src/models/deletion_record.py +97 -0
- src/models/delta_report.py +122 -0
- src/models/efs_resource.py +80 -0
- src/models/elasticache_resource.py +90 -0
- src/models/group.py +318 -0
- src/models/inventory.py +133 -0
- src/models/protection_rule.py +123 -0
- src/models/report.py +288 -0
- src/models/resource.py +111 -0
- src/models/security_finding.py +102 -0
- src/models/snapshot.py +122 -0
- src/restore/__init__.py +20 -0
- src/restore/audit.py +175 -0
- src/restore/cleaner.py +461 -0
- src/restore/config.py +209 -0
- src/restore/deleter.py +976 -0
- src/restore/dependency.py +254 -0
- src/restore/safety.py +115 -0
- src/security/__init__.py +0 -0
- src/security/checks/__init__.py +0 -0
- src/security/checks/base.py +56 -0
- src/security/checks/ec2_checks.py +88 -0
- src/security/checks/elasticache_checks.py +149 -0
- src/security/checks/iam_checks.py +102 -0
- src/security/checks/rds_checks.py +140 -0
- src/security/checks/s3_checks.py +95 -0
- src/security/checks/secrets_checks.py +96 -0
- src/security/checks/sg_checks.py +142 -0
- src/security/cis_mapper.py +97 -0
- src/security/models.py +53 -0
- src/security/reporter.py +174 -0
- src/security/scanner.py +87 -0
- src/snapshot/__init__.py +6 -0
- src/snapshot/capturer.py +453 -0
- src/snapshot/filter.py +259 -0
- src/snapshot/inventory_storage.py +236 -0
- src/snapshot/report_formatter.py +250 -0
- src/snapshot/reporter.py +189 -0
- src/snapshot/resource_collectors/__init__.py +5 -0
- src/snapshot/resource_collectors/apigateway.py +140 -0
- src/snapshot/resource_collectors/backup.py +136 -0
- src/snapshot/resource_collectors/base.py +81 -0
- src/snapshot/resource_collectors/cloudformation.py +55 -0
- src/snapshot/resource_collectors/cloudwatch.py +109 -0
- src/snapshot/resource_collectors/codebuild.py +69 -0
- src/snapshot/resource_collectors/codepipeline.py +82 -0
- src/snapshot/resource_collectors/dynamodb.py +65 -0
- src/snapshot/resource_collectors/ec2.py +240 -0
- src/snapshot/resource_collectors/ecs.py +215 -0
- src/snapshot/resource_collectors/efs_collector.py +102 -0
- src/snapshot/resource_collectors/eks.py +200 -0
- src/snapshot/resource_collectors/elasticache_collector.py +79 -0
- src/snapshot/resource_collectors/elb.py +126 -0
- src/snapshot/resource_collectors/eventbridge.py +156 -0
- src/snapshot/resource_collectors/glue.py +199 -0
- src/snapshot/resource_collectors/iam.py +188 -0
- src/snapshot/resource_collectors/kms.py +111 -0
- src/snapshot/resource_collectors/lambda_func.py +139 -0
- src/snapshot/resource_collectors/rds.py +109 -0
- src/snapshot/resource_collectors/route53.py +86 -0
- src/snapshot/resource_collectors/s3.py +105 -0
- src/snapshot/resource_collectors/secretsmanager.py +70 -0
- src/snapshot/resource_collectors/sns.py +68 -0
- src/snapshot/resource_collectors/sqs.py +82 -0
- src/snapshot/resource_collectors/ssm.py +160 -0
- src/snapshot/resource_collectors/stepfunctions.py +74 -0
- src/snapshot/resource_collectors/vpcendpoints.py +79 -0
- src/snapshot/resource_collectors/waf.py +159 -0
- src/snapshot/storage.py +351 -0
- src/storage/__init__.py +21 -0
- src/storage/audit_store.py +419 -0
- src/storage/database.py +294 -0
- src/storage/group_store.py +763 -0
- src/storage/inventory_store.py +320 -0
- src/storage/resource_store.py +416 -0
- src/storage/schema.py +339 -0
- src/storage/snapshot_store.py +363 -0
- src/utils/__init__.py +12 -0
- src/utils/export.py +305 -0
- src/utils/hash.py +60 -0
- src/utils/logging.py +63 -0
- src/utils/pagination.py +41 -0
- src/utils/paths.py +51 -0
- src/utils/progress.py +41 -0
- src/utils/unsupported_resources.py +306 -0
- src/web/__init__.py +5 -0
- src/web/app.py +97 -0
- src/web/dependencies.py +69 -0
- src/web/routes/__init__.py +1 -0
- src/web/routes/api/__init__.py +18 -0
- src/web/routes/api/charts.py +156 -0
- src/web/routes/api/cleanup.py +186 -0
- src/web/routes/api/filters.py +253 -0
- src/web/routes/api/groups.py +305 -0
- src/web/routes/api/inventories.py +80 -0
- src/web/routes/api/queries.py +202 -0
- src/web/routes/api/resources.py +393 -0
- src/web/routes/api/snapshots.py +314 -0
- src/web/routes/api/views.py +260 -0
- src/web/routes/pages.py +198 -0
- src/web/services/__init__.py +1 -0
- src/web/templates/base.html +955 -0
- src/web/templates/components/navbar.html +31 -0
- src/web/templates/components/sidebar.html +104 -0
- src/web/templates/pages/audit_logs.html +86 -0
- src/web/templates/pages/cleanup.html +279 -0
- src/web/templates/pages/dashboard.html +227 -0
- src/web/templates/pages/diff.html +175 -0
- src/web/templates/pages/error.html +30 -0
- src/web/templates/pages/groups.html +721 -0
- src/web/templates/pages/queries.html +246 -0
- src/web/templates/pages/resources.html +2429 -0
- src/web/templates/pages/snapshot_detail.html +271 -0
- src/web/templates/pages/snapshots.html +429 -0
src/restore/audit.py
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"""Audit storage for deletion operations.
|
|
2
|
+
|
|
3
|
+
Stores and retrieves audit logs in YAML format for compliance and troubleshooting.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Optional
|
|
11
|
+
|
|
12
|
+
import yaml
|
|
13
|
+
|
|
14
|
+
from src.models.deletion_operation import DeletionOperation
|
|
15
|
+
from src.models.deletion_record import DeletionRecord
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class AuditStorage:
|
|
19
|
+
"""Audit log storage and retrieval.
|
|
20
|
+
|
|
21
|
+
Stores deletion operation audit logs as YAML files organized by year/month.
|
|
22
|
+
Supports querying operations by date range and retrieving detailed operation logs.
|
|
23
|
+
|
|
24
|
+
Storage structure:
|
|
25
|
+
~/.snapshots/audit-logs/
|
|
26
|
+
2025/
|
|
27
|
+
11/
|
|
28
|
+
operation-op_123.yaml
|
|
29
|
+
operation-op_456.yaml
|
|
30
|
+
|
|
31
|
+
Attributes:
|
|
32
|
+
storage_dir: Base directory for audit logs
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(self, storage_dir: Optional[str] = None) -> None:
|
|
36
|
+
"""Initialize audit storage.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
storage_dir: Base directory for audit logs (default: ~/.snapshots/audit-logs)
|
|
40
|
+
"""
|
|
41
|
+
if storage_dir is None:
|
|
42
|
+
storage_dir = str(Path.home() / ".snapshots" / "audit-logs")
|
|
43
|
+
|
|
44
|
+
self.storage_dir = Path(storage_dir)
|
|
45
|
+
self.storage_dir.mkdir(parents=True, exist_ok=True)
|
|
46
|
+
|
|
47
|
+
def log_operation(self, operation: DeletionOperation, records: list[DeletionRecord]) -> None:
|
|
48
|
+
"""Log deletion operation to audit storage.
|
|
49
|
+
|
|
50
|
+
Creates YAML file with operation metadata and all deletion records.
|
|
51
|
+
Overwrites existing log if operation ID already exists.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
operation: Deletion operation to log
|
|
55
|
+
records: List of deletion records for this operation
|
|
56
|
+
"""
|
|
57
|
+
# Create year/month directory structure
|
|
58
|
+
year = operation.timestamp.year
|
|
59
|
+
month = operation.timestamp.month
|
|
60
|
+
year_month_dir = self.storage_dir / str(year) / f"{month:02d}"
|
|
61
|
+
year_month_dir.mkdir(parents=True, exist_ok=True)
|
|
62
|
+
|
|
63
|
+
# Create audit log structure
|
|
64
|
+
audit_data = {
|
|
65
|
+
"metadata": {
|
|
66
|
+
"version": "1.0",
|
|
67
|
+
"log_type": "resource_deletion",
|
|
68
|
+
"created_at": datetime.utcnow().isoformat() + "Z",
|
|
69
|
+
},
|
|
70
|
+
"operation": {
|
|
71
|
+
"operation_id": operation.operation_id,
|
|
72
|
+
"baseline_snapshot": operation.baseline_snapshot,
|
|
73
|
+
"timestamp": operation.timestamp.isoformat() + "Z",
|
|
74
|
+
"aws_profile": operation.aws_profile,
|
|
75
|
+
"account_id": operation.account_id,
|
|
76
|
+
"mode": operation.mode.value,
|
|
77
|
+
"status": operation.status.value,
|
|
78
|
+
"filters": operation.filters,
|
|
79
|
+
"total_resources": operation.total_resources,
|
|
80
|
+
"succeeded_count": operation.succeeded_count,
|
|
81
|
+
"failed_count": operation.failed_count,
|
|
82
|
+
"skipped_count": operation.skipped_count,
|
|
83
|
+
"started_at": operation.started_at.isoformat() + "Z" if operation.started_at else None,
|
|
84
|
+
"completed_at": operation.completed_at.isoformat() + "Z" if operation.completed_at else None,
|
|
85
|
+
"duration_seconds": operation.duration_seconds,
|
|
86
|
+
},
|
|
87
|
+
"records": [
|
|
88
|
+
{
|
|
89
|
+
"record_id": record.record_id,
|
|
90
|
+
"operation_id": record.operation_id,
|
|
91
|
+
"resource_arn": record.resource_arn,
|
|
92
|
+
"resource_id": record.resource_id,
|
|
93
|
+
"resource_type": record.resource_type,
|
|
94
|
+
"region": record.region,
|
|
95
|
+
"timestamp": record.timestamp.isoformat() + "Z",
|
|
96
|
+
"status": record.status.value,
|
|
97
|
+
"error_code": record.error_code,
|
|
98
|
+
"error_message": record.error_message,
|
|
99
|
+
"protection_reason": record.protection_reason,
|
|
100
|
+
"deletion_tier": record.deletion_tier,
|
|
101
|
+
"tags": record.tags,
|
|
102
|
+
"estimated_monthly_cost": record.estimated_monthly_cost,
|
|
103
|
+
}
|
|
104
|
+
for record in records
|
|
105
|
+
],
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
# Write YAML file
|
|
109
|
+
audit_file = year_month_dir / f"operation-{operation.operation_id}.yaml"
|
|
110
|
+
with open(audit_file, "w") as f:
|
|
111
|
+
yaml.dump(audit_data, f, default_flow_style=False, sort_keys=False)
|
|
112
|
+
|
|
113
|
+
def get_operation(self, operation_id: str) -> Optional[dict]:
|
|
114
|
+
"""Retrieve operation audit log by ID.
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
operation_id: Operation ID to retrieve
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
Audit log dictionary if found, None otherwise
|
|
121
|
+
"""
|
|
122
|
+
# Search all year/month directories
|
|
123
|
+
for year_dir in self.storage_dir.glob("*"):
|
|
124
|
+
if not year_dir.is_dir():
|
|
125
|
+
continue
|
|
126
|
+
|
|
127
|
+
for month_dir in year_dir.glob("*"):
|
|
128
|
+
if not month_dir.is_dir():
|
|
129
|
+
continue
|
|
130
|
+
|
|
131
|
+
audit_file = month_dir / f"operation-{operation_id}.yaml"
|
|
132
|
+
if audit_file.exists():
|
|
133
|
+
with open(audit_file, "r") as f:
|
|
134
|
+
return yaml.safe_load(f)
|
|
135
|
+
|
|
136
|
+
return None
|
|
137
|
+
|
|
138
|
+
def query_operations(self, since: Optional[datetime] = None, until: Optional[datetime] = None) -> list[dict]:
|
|
139
|
+
"""Query operations within date range.
|
|
140
|
+
|
|
141
|
+
Args:
|
|
142
|
+
since: Start date (inclusive), None for all
|
|
143
|
+
until: End date (inclusive), None for all
|
|
144
|
+
|
|
145
|
+
Returns:
|
|
146
|
+
List of operation audit logs matching criteria
|
|
147
|
+
"""
|
|
148
|
+
results = []
|
|
149
|
+
|
|
150
|
+
# Search all year/month directories
|
|
151
|
+
for year_dir in sorted(self.storage_dir.glob("*")):
|
|
152
|
+
if not year_dir.is_dir():
|
|
153
|
+
continue
|
|
154
|
+
|
|
155
|
+
for month_dir in sorted(year_dir.glob("*")):
|
|
156
|
+
if not month_dir.is_dir():
|
|
157
|
+
continue
|
|
158
|
+
|
|
159
|
+
for audit_file in sorted(month_dir.glob("operation-*.yaml")):
|
|
160
|
+
with open(audit_file, "r") as f:
|
|
161
|
+
audit_data = yaml.safe_load(f)
|
|
162
|
+
|
|
163
|
+
# Parse timestamp
|
|
164
|
+
timestamp_str = audit_data["operation"]["timestamp"]
|
|
165
|
+
timestamp = datetime.fromisoformat(timestamp_str.rstrip("Z"))
|
|
166
|
+
|
|
167
|
+
# Filter by date range
|
|
168
|
+
if since and timestamp < since:
|
|
169
|
+
continue
|
|
170
|
+
if until and timestamp > until:
|
|
171
|
+
continue
|
|
172
|
+
|
|
173
|
+
results.append(audit_data)
|
|
174
|
+
|
|
175
|
+
return results
|
src/restore/cleaner.py
ADDED
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
"""Resource cleaner for restoration operations.
|
|
2
|
+
|
|
3
|
+
Main orchestrator for resource cleanup/restoration with preview and execution modes.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import logging
|
|
9
|
+
import uuid
|
|
10
|
+
from datetime import datetime
|
|
11
|
+
from typing import Any, Optional
|
|
12
|
+
|
|
13
|
+
from src.delta.calculator import DeltaCalculator
|
|
14
|
+
from src.models.deletion_operation import DeletionOperation, OperationMode, OperationStatus
|
|
15
|
+
from src.models.deletion_record import DeletionRecord, DeletionStatus
|
|
16
|
+
from src.restore.audit import AuditStorage
|
|
17
|
+
from src.restore.dependency import DependencyResolver
|
|
18
|
+
from src.restore.safety import SafetyChecker
|
|
19
|
+
from src.snapshot.storage import SnapshotStorage
|
|
20
|
+
|
|
21
|
+
logger = logging.getLogger(__name__)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class ResourceCleaner:
|
|
25
|
+
"""Resource cleaner orchestrator.
|
|
26
|
+
|
|
27
|
+
Coordinates resource cleanup/restoration operations with safety checks,
|
|
28
|
+
dependency resolution, and audit logging. Supports both preview (dry-run)
|
|
29
|
+
and execution modes.
|
|
30
|
+
|
|
31
|
+
Attributes:
|
|
32
|
+
snapshot_storage: Snapshot storage for baseline comparison
|
|
33
|
+
safety_checker: Safety checker for protection rules
|
|
34
|
+
audit_storage: Audit storage for deletion logs
|
|
35
|
+
dependency_resolver: Dependency resolver for deletion ordering
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
snapshot_storage: SnapshotStorage,
|
|
41
|
+
safety_checker: SafetyChecker,
|
|
42
|
+
audit_storage: AuditStorage,
|
|
43
|
+
) -> None:
|
|
44
|
+
"""Initialize resource cleaner.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
snapshot_storage: Snapshot storage instance
|
|
48
|
+
safety_checker: Safety checker with protection rules
|
|
49
|
+
audit_storage: Audit storage for logging
|
|
50
|
+
"""
|
|
51
|
+
self.snapshot_storage = snapshot_storage
|
|
52
|
+
self.safety_checker = safety_checker
|
|
53
|
+
self.audit_storage = audit_storage
|
|
54
|
+
self.dependency_resolver = DependencyResolver()
|
|
55
|
+
|
|
56
|
+
def preview(
|
|
57
|
+
self,
|
|
58
|
+
baseline_snapshot: str,
|
|
59
|
+
account_id: str,
|
|
60
|
+
aws_profile: Optional[str] = None,
|
|
61
|
+
resource_types: Optional[list[str]] = None,
|
|
62
|
+
regions: Optional[list[str]] = None,
|
|
63
|
+
) -> DeletionOperation:
|
|
64
|
+
"""Preview resources that would be deleted (dry-run mode).
|
|
65
|
+
|
|
66
|
+
Identifies resources created after baseline snapshot and applies protection
|
|
67
|
+
rules without performing any deletions.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
baseline_snapshot: Name of baseline snapshot to compare against
|
|
71
|
+
account_id: AWS account ID to validate
|
|
72
|
+
aws_profile: AWS profile name (optional)
|
|
73
|
+
resource_types: Filter by resource types (optional)
|
|
74
|
+
regions: Filter by regions (optional)
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
DeletionOperation in planned status with resource counts
|
|
78
|
+
|
|
79
|
+
Raises:
|
|
80
|
+
ValueError: If snapshot not found or account ID mismatch
|
|
81
|
+
"""
|
|
82
|
+
# Validate snapshot exists and load it
|
|
83
|
+
try:
|
|
84
|
+
snapshot = self.snapshot_storage.load_snapshot(baseline_snapshot)
|
|
85
|
+
except (FileNotFoundError, ValueError):
|
|
86
|
+
raise ValueError(f"Snapshot '{baseline_snapshot}' not found")
|
|
87
|
+
|
|
88
|
+
# Validate account ID matches
|
|
89
|
+
snapshot_account = snapshot.account_id
|
|
90
|
+
if snapshot_account != account_id:
|
|
91
|
+
raise ValueError(
|
|
92
|
+
f"Account ID mismatch: snapshot has {snapshot_account}, " f"current credentials have {account_id}"
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
# Collect current resources
|
|
96
|
+
current_resources = self._collect_current_resources(account_id, regions)
|
|
97
|
+
|
|
98
|
+
# Create a temporary snapshot for current state
|
|
99
|
+
from src.models.snapshot import Snapshot
|
|
100
|
+
|
|
101
|
+
current_snapshot = Snapshot(
|
|
102
|
+
name="current",
|
|
103
|
+
created_at=datetime.utcnow(),
|
|
104
|
+
account_id=account_id,
|
|
105
|
+
regions=regions or snapshot.regions,
|
|
106
|
+
resources=[self._dict_to_resource(r) for r in current_resources],
|
|
107
|
+
resource_count=len(current_resources),
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
# Calculate delta (new resources since baseline)
|
|
111
|
+
delta_calc = DeltaCalculator(reference_snapshot=snapshot, current_snapshot=current_snapshot)
|
|
112
|
+
delta_result = delta_calc.calculate()
|
|
113
|
+
|
|
114
|
+
# Get added resources (convert back to dict format for filtering)
|
|
115
|
+
new_resources = [self._resource_to_dict(r) for r in delta_result.added_resources]
|
|
116
|
+
|
|
117
|
+
# Apply filters
|
|
118
|
+
filtered_resources = self._apply_filters(
|
|
119
|
+
new_resources,
|
|
120
|
+
resource_types=resource_types,
|
|
121
|
+
regions=regions,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
# Apply protection rules and count protected resources
|
|
125
|
+
protected_count = 0
|
|
126
|
+
|
|
127
|
+
for resource in filtered_resources:
|
|
128
|
+
is_protected, reason = self.safety_checker.is_protected(resource)
|
|
129
|
+
|
|
130
|
+
if is_protected:
|
|
131
|
+
protected_count += 1
|
|
132
|
+
|
|
133
|
+
# Create operation
|
|
134
|
+
operation = DeletionOperation(
|
|
135
|
+
operation_id=f"op_{uuid.uuid4()}",
|
|
136
|
+
baseline_snapshot=baseline_snapshot,
|
|
137
|
+
timestamp=datetime.utcnow(),
|
|
138
|
+
account_id=account_id,
|
|
139
|
+
mode=OperationMode.DRY_RUN,
|
|
140
|
+
status=OperationStatus.PLANNED,
|
|
141
|
+
total_resources=len(filtered_resources),
|
|
142
|
+
succeeded_count=0,
|
|
143
|
+
failed_count=0,
|
|
144
|
+
skipped_count=protected_count,
|
|
145
|
+
aws_profile=aws_profile,
|
|
146
|
+
filters=self._build_filters_dict(resource_types, regions),
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
return operation
|
|
150
|
+
|
|
151
|
+
def execute(
|
|
152
|
+
self,
|
|
153
|
+
baseline_snapshot: str,
|
|
154
|
+
account_id: str,
|
|
155
|
+
confirmed: bool = False,
|
|
156
|
+
aws_profile: Optional[str] = None,
|
|
157
|
+
resource_types: Optional[list[str]] = None,
|
|
158
|
+
regions: Optional[list[str]] = None,
|
|
159
|
+
) -> DeletionOperation:
|
|
160
|
+
"""Execute resource deletion to restore to baseline (execution mode).
|
|
161
|
+
|
|
162
|
+
Performs actual deletion of resources created after baseline snapshot with
|
|
163
|
+
protection checks and audit logging.
|
|
164
|
+
|
|
165
|
+
Args:
|
|
166
|
+
baseline_snapshot: Name of baseline snapshot to restore to
|
|
167
|
+
account_id: AWS account ID to validate
|
|
168
|
+
confirmed: Must be True to proceed with deletion
|
|
169
|
+
aws_profile: AWS profile name (optional)
|
|
170
|
+
resource_types: Filter by resource types (optional)
|
|
171
|
+
regions: Filter by regions (optional)
|
|
172
|
+
|
|
173
|
+
Returns:
|
|
174
|
+
DeletionOperation with execution results
|
|
175
|
+
|
|
176
|
+
Raises:
|
|
177
|
+
ValueError: If not confirmed or snapshot not found or account ID mismatch
|
|
178
|
+
"""
|
|
179
|
+
# Require confirmation for destructive operations
|
|
180
|
+
if not confirmed:
|
|
181
|
+
raise ValueError("Deletion requires explicit confirmation. Set confirmed=True or use --confirm flag.")
|
|
182
|
+
|
|
183
|
+
# Validate snapshot exists and load it
|
|
184
|
+
try:
|
|
185
|
+
snapshot = self.snapshot_storage.load_snapshot(baseline_snapshot)
|
|
186
|
+
except (FileNotFoundError, ValueError):
|
|
187
|
+
raise ValueError(f"Snapshot '{baseline_snapshot}' not found")
|
|
188
|
+
|
|
189
|
+
# Validate account ID matches
|
|
190
|
+
snapshot_account = snapshot.account_id
|
|
191
|
+
if snapshot_account != account_id:
|
|
192
|
+
raise ValueError(
|
|
193
|
+
f"Account ID mismatch: snapshot has {snapshot_account}, " f"current credentials have {account_id}"
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
# Collect current resources
|
|
197
|
+
current_resources = self._collect_current_resources(account_id, regions)
|
|
198
|
+
|
|
199
|
+
# Create a temporary snapshot for current state
|
|
200
|
+
from src.models.snapshot import Snapshot
|
|
201
|
+
|
|
202
|
+
current_snapshot = Snapshot(
|
|
203
|
+
name="current",
|
|
204
|
+
created_at=datetime.utcnow(),
|
|
205
|
+
account_id=account_id,
|
|
206
|
+
regions=regions or snapshot.regions,
|
|
207
|
+
resources=[self._dict_to_resource(r) for r in current_resources],
|
|
208
|
+
resource_count=len(current_resources),
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
# Calculate delta (new resources since baseline)
|
|
212
|
+
delta_calc = DeltaCalculator(reference_snapshot=snapshot, current_snapshot=current_snapshot)
|
|
213
|
+
delta_result = delta_calc.calculate()
|
|
214
|
+
|
|
215
|
+
# Get added resources (convert back to dict format for filtering)
|
|
216
|
+
new_resources = [self._resource_to_dict(r) for r in delta_result.added_resources]
|
|
217
|
+
|
|
218
|
+
# Apply filters
|
|
219
|
+
filtered_resources = self._apply_filters(
|
|
220
|
+
new_resources,
|
|
221
|
+
resource_types=resource_types,
|
|
222
|
+
regions=regions,
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
# Import at module level to avoid UnboundLocalError
|
|
226
|
+
|
|
227
|
+
# Execute deletions with protection checks
|
|
228
|
+
succeeded_count = 0
|
|
229
|
+
failed_count = 0
|
|
230
|
+
skipped_count = 0
|
|
231
|
+
deletion_records = []
|
|
232
|
+
operation_id = f"op_{uuid.uuid4()}"
|
|
233
|
+
|
|
234
|
+
for resource in filtered_resources:
|
|
235
|
+
record_id = f"rec_{uuid.uuid4()}"
|
|
236
|
+
|
|
237
|
+
# Check protection rules
|
|
238
|
+
is_protected, reason = self.safety_checker.is_protected(resource)
|
|
239
|
+
|
|
240
|
+
if is_protected:
|
|
241
|
+
skipped_count += 1
|
|
242
|
+
# Create deletion record for skipped resource
|
|
243
|
+
record = DeletionRecord(
|
|
244
|
+
record_id=record_id,
|
|
245
|
+
operation_id=operation_id,
|
|
246
|
+
resource_id=resource.get("resource_id", ""),
|
|
247
|
+
resource_arn=resource.get("arn", ""),
|
|
248
|
+
resource_type=resource.get("resource_type", ""),
|
|
249
|
+
region=resource.get("region", ""),
|
|
250
|
+
status=DeletionStatus.SKIPPED,
|
|
251
|
+
protection_reason=reason or "Protected by safety rules",
|
|
252
|
+
timestamp=datetime.utcnow(),
|
|
253
|
+
)
|
|
254
|
+
deletion_records.append(record)
|
|
255
|
+
continue
|
|
256
|
+
|
|
257
|
+
# Attempt deletion
|
|
258
|
+
success = self._delete_resource(resource, aws_profile)
|
|
259
|
+
|
|
260
|
+
if success:
|
|
261
|
+
succeeded_count += 1
|
|
262
|
+
# Create successful deletion record
|
|
263
|
+
record = DeletionRecord(
|
|
264
|
+
record_id=record_id,
|
|
265
|
+
operation_id=operation_id,
|
|
266
|
+
resource_id=resource.get("resource_id", ""),
|
|
267
|
+
resource_arn=resource.get("arn", ""),
|
|
268
|
+
resource_type=resource.get("resource_type", ""),
|
|
269
|
+
region=resource.get("region", ""),
|
|
270
|
+
status=DeletionStatus.SUCCEEDED,
|
|
271
|
+
timestamp=datetime.utcnow(),
|
|
272
|
+
)
|
|
273
|
+
else:
|
|
274
|
+
failed_count += 1
|
|
275
|
+
# Create failed deletion record
|
|
276
|
+
record = DeletionRecord(
|
|
277
|
+
record_id=record_id,
|
|
278
|
+
operation_id=operation_id,
|
|
279
|
+
resource_id=resource.get("resource_id", ""),
|
|
280
|
+
resource_arn=resource.get("arn", ""),
|
|
281
|
+
resource_type=resource.get("resource_type", ""),
|
|
282
|
+
region=resource.get("region", ""),
|
|
283
|
+
status=DeletionStatus.FAILED,
|
|
284
|
+
error_code="DeletionFailed",
|
|
285
|
+
error_message="Resource deletion failed",
|
|
286
|
+
timestamp=datetime.utcnow(),
|
|
287
|
+
)
|
|
288
|
+
deletion_records.append(record)
|
|
289
|
+
|
|
290
|
+
# Determine final status
|
|
291
|
+
if failed_count > 0:
|
|
292
|
+
if succeeded_count > 0:
|
|
293
|
+
final_status = OperationStatus.PARTIAL
|
|
294
|
+
else:
|
|
295
|
+
final_status = OperationStatus.FAILED
|
|
296
|
+
else:
|
|
297
|
+
final_status = OperationStatus.COMPLETED
|
|
298
|
+
|
|
299
|
+
# Create operation (use same operation_id from records)
|
|
300
|
+
operation = DeletionOperation(
|
|
301
|
+
operation_id=operation_id,
|
|
302
|
+
baseline_snapshot=baseline_snapshot,
|
|
303
|
+
timestamp=datetime.utcnow(),
|
|
304
|
+
account_id=account_id,
|
|
305
|
+
mode=OperationMode.EXECUTE,
|
|
306
|
+
status=final_status,
|
|
307
|
+
total_resources=len(filtered_resources),
|
|
308
|
+
succeeded_count=succeeded_count,
|
|
309
|
+
failed_count=failed_count,
|
|
310
|
+
skipped_count=skipped_count,
|
|
311
|
+
aws_profile=aws_profile,
|
|
312
|
+
filters=self._build_filters_dict(resource_types, regions),
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
# Log operation to audit storage with deletion records
|
|
316
|
+
self.audit_storage.log_operation(operation, deletion_records)
|
|
317
|
+
|
|
318
|
+
return operation
|
|
319
|
+
|
|
320
|
+
def _delete_resource(self, resource: dict, aws_profile: Optional[str] = None) -> bool:
|
|
321
|
+
"""Delete a single AWS resource.
|
|
322
|
+
|
|
323
|
+
Args:
|
|
324
|
+
resource: Resource dictionary with type, ARN, region
|
|
325
|
+
aws_profile: AWS profile name (optional)
|
|
326
|
+
|
|
327
|
+
Returns:
|
|
328
|
+
True if deletion succeeded, False otherwise
|
|
329
|
+
"""
|
|
330
|
+
from src.restore.deleter import ResourceDeleter
|
|
331
|
+
|
|
332
|
+
deleter = ResourceDeleter(aws_profile=aws_profile)
|
|
333
|
+
|
|
334
|
+
resource_type = resource.get("resource_type", "")
|
|
335
|
+
resource_id = resource.get("resource_id", "")
|
|
336
|
+
region = resource.get("region", "")
|
|
337
|
+
arn = resource.get("arn", "")
|
|
338
|
+
|
|
339
|
+
success, error_message = deleter.delete_resource(
|
|
340
|
+
resource_type=resource_type,
|
|
341
|
+
resource_id=resource_id,
|
|
342
|
+
region=region,
|
|
343
|
+
arn=arn,
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
if not success:
|
|
347
|
+
logger.warning(f"Failed to delete {resource_type} {resource_id}: {error_message}")
|
|
348
|
+
|
|
349
|
+
return success
|
|
350
|
+
|
|
351
|
+
def _collect_current_resources(self, account_id: str, regions: Optional[list[str]] = None) -> list[dict]:
|
|
352
|
+
"""Collect current resources from AWS account.
|
|
353
|
+
|
|
354
|
+
This is a placeholder that would normally call AWS APIs to collect
|
|
355
|
+
current resource state. For testing, this is mocked.
|
|
356
|
+
|
|
357
|
+
Args:
|
|
358
|
+
account_id: AWS account ID
|
|
359
|
+
regions: Regions to collect from (optional)
|
|
360
|
+
|
|
361
|
+
Returns:
|
|
362
|
+
List of current resources
|
|
363
|
+
"""
|
|
364
|
+
# Placeholder - in real implementation, would use snapshot capturer
|
|
365
|
+
# or similar mechanism to collect current state
|
|
366
|
+
return []
|
|
367
|
+
|
|
368
|
+
def _apply_filters(
|
|
369
|
+
self,
|
|
370
|
+
resources: list[dict],
|
|
371
|
+
resource_types: Optional[list[str]] = None,
|
|
372
|
+
regions: Optional[list[str]] = None,
|
|
373
|
+
) -> list[dict]:
|
|
374
|
+
"""Apply filters to resource list.
|
|
375
|
+
|
|
376
|
+
Args:
|
|
377
|
+
resources: List of resources to filter
|
|
378
|
+
resource_types: Filter by resource types (optional)
|
|
379
|
+
regions: Filter by regions (optional)
|
|
380
|
+
|
|
381
|
+
Returns:
|
|
382
|
+
Filtered list of resources
|
|
383
|
+
"""
|
|
384
|
+
filtered = resources
|
|
385
|
+
|
|
386
|
+
if resource_types:
|
|
387
|
+
filtered = [r for r in filtered if r.get("resource_type") in resource_types]
|
|
388
|
+
|
|
389
|
+
if regions:
|
|
390
|
+
filtered = [r for r in filtered if r.get("region") in regions]
|
|
391
|
+
|
|
392
|
+
return filtered
|
|
393
|
+
|
|
394
|
+
def _build_filters_dict(
|
|
395
|
+
self,
|
|
396
|
+
resource_types: Optional[list[str]] = None,
|
|
397
|
+
regions: Optional[list[str]] = None,
|
|
398
|
+
) -> Optional[dict]:
|
|
399
|
+
"""Build filters dictionary for operation metadata.
|
|
400
|
+
|
|
401
|
+
Args:
|
|
402
|
+
resource_types: Resource types filter
|
|
403
|
+
regions: Regions filter
|
|
404
|
+
|
|
405
|
+
Returns:
|
|
406
|
+
Filters dictionary or None if no filters
|
|
407
|
+
"""
|
|
408
|
+
filters = {}
|
|
409
|
+
|
|
410
|
+
if resource_types:
|
|
411
|
+
filters["resource_types"] = resource_types
|
|
412
|
+
|
|
413
|
+
if regions:
|
|
414
|
+
filters["regions"] = regions
|
|
415
|
+
|
|
416
|
+
return filters if filters else None
|
|
417
|
+
|
|
418
|
+
def _resource_to_dict(self, resource: Any) -> dict:
|
|
419
|
+
"""Convert Resource object to dictionary format.
|
|
420
|
+
|
|
421
|
+
Args:
|
|
422
|
+
resource: Resource object from snapshot
|
|
423
|
+
|
|
424
|
+
Returns:
|
|
425
|
+
Dictionary representation of resource
|
|
426
|
+
"""
|
|
427
|
+
# Resource objects have these attributes
|
|
428
|
+
return {
|
|
429
|
+
"resource_id": resource.name, # Resource uses 'name' field
|
|
430
|
+
"resource_type": resource.resource_type,
|
|
431
|
+
"region": resource.region,
|
|
432
|
+
"arn": resource.arn,
|
|
433
|
+
"tags": resource.tags,
|
|
434
|
+
"estimated_monthly_cost": getattr(resource, "estimated_monthly_cost", None),
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
def _dict_to_resource(self, resource_dict: dict) -> Any:
|
|
438
|
+
"""Convert dictionary to Resource object.
|
|
439
|
+
|
|
440
|
+
Args:
|
|
441
|
+
resource_dict: Dictionary representation of resource
|
|
442
|
+
|
|
443
|
+
Returns:
|
|
444
|
+
Resource object
|
|
445
|
+
"""
|
|
446
|
+
import hashlib
|
|
447
|
+
|
|
448
|
+
from src.models.resource import Resource
|
|
449
|
+
|
|
450
|
+
# Generate config hash from resource dict for comparison
|
|
451
|
+
config_str = f"{resource_dict.get('arn', '')}{resource_dict.get('resource_type', '')}"
|
|
452
|
+
config_hash = hashlib.sha256(config_str.encode()).hexdigest()
|
|
453
|
+
|
|
454
|
+
return Resource(
|
|
455
|
+
arn=resource_dict.get("arn", ""),
|
|
456
|
+
resource_type=resource_dict.get("resource_type", ""),
|
|
457
|
+
name=resource_dict.get("resource_id", ""),
|
|
458
|
+
region=resource_dict.get("region", ""),
|
|
459
|
+
config_hash=config_hash,
|
|
460
|
+
tags=resource_dict.get("tags", {}),
|
|
461
|
+
)
|