aws-inventory-manager 0.2.0__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.
Potentially problematic release.
This version of aws-inventory-manager might be problematic. Click here for more details.
- aws_inventory_manager-0.2.0.dist-info/METADATA +508 -0
- aws_inventory_manager-0.2.0.dist-info/RECORD +65 -0
- aws_inventory_manager-0.2.0.dist-info/WHEEL +5 -0
- aws_inventory_manager-0.2.0.dist-info/entry_points.txt +2 -0
- aws_inventory_manager-0.2.0.dist-info/licenses/LICENSE +21 -0
- aws_inventory_manager-0.2.0.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 +5 -0
- src/cli/config.py +130 -0
- src/cli/main.py +1450 -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 +180 -0
- src/delta/reporter.py +225 -0
- src/models/__init__.py +17 -0
- src/models/cost_report.py +87 -0
- src/models/delta_report.py +111 -0
- src/models/inventory.py +124 -0
- src/models/resource.py +99 -0
- src/models/snapshot.py +108 -0
- src/snapshot/__init__.py +6 -0
- src/snapshot/capturer.py +347 -0
- src/snapshot/filter.py +245 -0
- src/snapshot/inventory_storage.py +264 -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/eks.py +200 -0
- src/snapshot/resource_collectors/elb.py +126 -0
- src/snapshot/resource_collectors/eventbridge.py +156 -0
- src/snapshot/resource_collectors/iam.py +188 -0
- src/snapshot/resource_collectors/kms.py +111 -0
- src/snapshot/resource_collectors/lambda_func.py +112 -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 +72 -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 +259 -0
- src/utils/__init__.py +12 -0
- src/utils/export.py +87 -0
- src/utils/hash.py +60 -0
- src/utils/logging.py +63 -0
- src/utils/paths.py +51 -0
- src/utils/progress.py +41 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""ELB/ALB resource collector."""
|
|
2
|
+
|
|
3
|
+
from typing import List
|
|
4
|
+
|
|
5
|
+
from ...models.resource import Resource
|
|
6
|
+
from ...utils.hash import compute_config_hash
|
|
7
|
+
from .base import BaseResourceCollector
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ELBCollector(BaseResourceCollector):
|
|
11
|
+
"""Collector for AWS Load Balancer resources (Classic ELB, ALB, NLB)."""
|
|
12
|
+
|
|
13
|
+
@property
|
|
14
|
+
def service_name(self) -> str:
|
|
15
|
+
return "elb"
|
|
16
|
+
|
|
17
|
+
def collect(self) -> List[Resource]:
|
|
18
|
+
"""Collect ELB resources.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
List of load balancers
|
|
22
|
+
"""
|
|
23
|
+
resources = []
|
|
24
|
+
account_id = self._get_account_id()
|
|
25
|
+
|
|
26
|
+
# Collect v2 load balancers (ALB, NLB, GWLB)
|
|
27
|
+
resources.extend(self._collect_v2_load_balancers(account_id))
|
|
28
|
+
|
|
29
|
+
# Collect classic load balancers
|
|
30
|
+
resources.extend(self._collect_classic_load_balancers(account_id))
|
|
31
|
+
|
|
32
|
+
self.logger.debug(f"Collected {len(resources)} load balancers in {self.region}")
|
|
33
|
+
return resources
|
|
34
|
+
|
|
35
|
+
def _collect_v2_load_balancers(self, account_id: str) -> List[Resource]:
|
|
36
|
+
"""Collect ALB/NLB/GWLB load balancers."""
|
|
37
|
+
resources = []
|
|
38
|
+
|
|
39
|
+
try:
|
|
40
|
+
elbv2_client = self._create_client("elbv2")
|
|
41
|
+
|
|
42
|
+
paginator = elbv2_client.get_paginator("describe_load_balancers")
|
|
43
|
+
for page in paginator.paginate():
|
|
44
|
+
for lb in page.get("LoadBalancers", []):
|
|
45
|
+
lb_arn = lb["LoadBalancerArn"]
|
|
46
|
+
lb_name = lb["LoadBalancerName"]
|
|
47
|
+
lb_type = lb.get("Type", "application") # application, network, gateway
|
|
48
|
+
|
|
49
|
+
# Get tags
|
|
50
|
+
tags = {}
|
|
51
|
+
try:
|
|
52
|
+
tag_response = elbv2_client.describe_tags(ResourceArns=[lb_arn])
|
|
53
|
+
for tag_desc in tag_response.get("TagDescriptions", []):
|
|
54
|
+
tags = {tag["Key"]: tag["Value"] for tag in tag_desc.get("Tags", [])}
|
|
55
|
+
except Exception as e:
|
|
56
|
+
self.logger.debug(f"Could not get tags for load balancer {lb_name}: {e}")
|
|
57
|
+
|
|
58
|
+
# Determine resource type
|
|
59
|
+
if lb_type == "application":
|
|
60
|
+
resource_type = "AWS::ElasticLoadBalancingV2::LoadBalancer::Application"
|
|
61
|
+
elif lb_type == "network":
|
|
62
|
+
resource_type = "AWS::ElasticLoadBalancingV2::LoadBalancer::Network"
|
|
63
|
+
elif lb_type == "gateway":
|
|
64
|
+
resource_type = "AWS::ElasticLoadBalancingV2::LoadBalancer::Gateway"
|
|
65
|
+
else:
|
|
66
|
+
resource_type = "AWS::ElasticLoadBalancingV2::LoadBalancer"
|
|
67
|
+
|
|
68
|
+
# Create resource
|
|
69
|
+
resource = Resource(
|
|
70
|
+
arn=lb_arn,
|
|
71
|
+
resource_type=resource_type,
|
|
72
|
+
name=lb_name,
|
|
73
|
+
region=self.region,
|
|
74
|
+
tags=tags,
|
|
75
|
+
config_hash=compute_config_hash(lb),
|
|
76
|
+
created_at=lb.get("CreatedTime"),
|
|
77
|
+
raw_config=lb,
|
|
78
|
+
)
|
|
79
|
+
resources.append(resource)
|
|
80
|
+
|
|
81
|
+
except Exception as e:
|
|
82
|
+
self.logger.error(f"Error collecting v2 load balancers in {self.region}: {e}")
|
|
83
|
+
|
|
84
|
+
return resources
|
|
85
|
+
|
|
86
|
+
def _collect_classic_load_balancers(self, account_id: str) -> List[Resource]:
|
|
87
|
+
"""Collect classic load balancers."""
|
|
88
|
+
resources = []
|
|
89
|
+
|
|
90
|
+
try:
|
|
91
|
+
elb_client = self._create_client("elb")
|
|
92
|
+
|
|
93
|
+
paginator = elb_client.get_paginator("describe_load_balancers")
|
|
94
|
+
for page in paginator.paginate():
|
|
95
|
+
for lb in page.get("LoadBalancerDescriptions", []):
|
|
96
|
+
lb_name = lb["LoadBalancerName"]
|
|
97
|
+
|
|
98
|
+
# Build ARN (classic ELBs don't return ARN directly)
|
|
99
|
+
lb_arn = f"arn:aws:elasticloadbalancing:{self.region}:{account_id}:loadbalancer/{lb_name}"
|
|
100
|
+
|
|
101
|
+
# Get tags
|
|
102
|
+
tags = {}
|
|
103
|
+
try:
|
|
104
|
+
tag_response = elb_client.describe_tags(LoadBalancerNames=[lb_name])
|
|
105
|
+
for tag_desc in tag_response.get("TagDescriptions", []):
|
|
106
|
+
tags = {tag["Key"]: tag["Value"] for tag in tag_desc.get("Tags", [])}
|
|
107
|
+
except Exception as e:
|
|
108
|
+
self.logger.debug(f"Could not get tags for classic ELB {lb_name}: {e}")
|
|
109
|
+
|
|
110
|
+
# Create resource
|
|
111
|
+
resource = Resource(
|
|
112
|
+
arn=lb_arn,
|
|
113
|
+
resource_type="AWS::ElasticLoadBalancing::LoadBalancer",
|
|
114
|
+
name=lb_name,
|
|
115
|
+
region=self.region,
|
|
116
|
+
tags=tags,
|
|
117
|
+
config_hash=compute_config_hash(lb),
|
|
118
|
+
created_at=lb.get("CreatedTime"),
|
|
119
|
+
raw_config=lb,
|
|
120
|
+
)
|
|
121
|
+
resources.append(resource)
|
|
122
|
+
|
|
123
|
+
except Exception as e:
|
|
124
|
+
self.logger.error(f"Error collecting classic load balancers in {self.region}: {e}")
|
|
125
|
+
|
|
126
|
+
return resources
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"""EventBridge resource collector."""
|
|
2
|
+
|
|
3
|
+
from typing import List
|
|
4
|
+
|
|
5
|
+
from ...models.resource import Resource
|
|
6
|
+
from ...utils.hash import compute_config_hash
|
|
7
|
+
from .base import BaseResourceCollector
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class EventBridgeCollector(BaseResourceCollector):
|
|
11
|
+
"""Collector for Amazon EventBridge resources (Event Buses, Rules)."""
|
|
12
|
+
|
|
13
|
+
@property
|
|
14
|
+
def service_name(self) -> str:
|
|
15
|
+
return "eventbridge"
|
|
16
|
+
|
|
17
|
+
def collect(self) -> List[Resource]:
|
|
18
|
+
"""Collect EventBridge resources.
|
|
19
|
+
|
|
20
|
+
Collects:
|
|
21
|
+
- Event Buses (custom and partner buses)
|
|
22
|
+
- Event Rules (across all buses)
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
List of EventBridge resources
|
|
26
|
+
"""
|
|
27
|
+
resources = []
|
|
28
|
+
|
|
29
|
+
# Collect Event Buses
|
|
30
|
+
resources.extend(self._collect_event_buses())
|
|
31
|
+
|
|
32
|
+
# Collect Event Rules (across all buses)
|
|
33
|
+
resources.extend(self._collect_event_rules())
|
|
34
|
+
|
|
35
|
+
self.logger.debug(f"Collected {len(resources)} EventBridge resources in {self.region}")
|
|
36
|
+
return resources
|
|
37
|
+
|
|
38
|
+
def _collect_event_buses(self) -> List[Resource]:
|
|
39
|
+
"""Collect EventBridge Event Buses.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
List of Event Bus resources
|
|
43
|
+
"""
|
|
44
|
+
resources = []
|
|
45
|
+
client = self._create_client("events")
|
|
46
|
+
|
|
47
|
+
try:
|
|
48
|
+
paginator = client.get_paginator("list_event_buses")
|
|
49
|
+
for page in paginator.paginate():
|
|
50
|
+
for bus in page.get("EventBuses", []):
|
|
51
|
+
bus_name = bus["Name"]
|
|
52
|
+
bus_arn = bus["Arn"]
|
|
53
|
+
|
|
54
|
+
# Get tags
|
|
55
|
+
tags = {}
|
|
56
|
+
try:
|
|
57
|
+
tag_response = client.list_tags_for_resource(ResourceARN=bus_arn)
|
|
58
|
+
for tag in tag_response.get("Tags", []):
|
|
59
|
+
tags[tag["Key"]] = tag["Value"]
|
|
60
|
+
except Exception as e:
|
|
61
|
+
self.logger.debug(f"Could not get tags for event bus {bus_name}: {e}")
|
|
62
|
+
|
|
63
|
+
# Extract creation time (if available)
|
|
64
|
+
created_at = bus.get("CreationTime")
|
|
65
|
+
|
|
66
|
+
# Create resource
|
|
67
|
+
resource = Resource(
|
|
68
|
+
arn=bus_arn,
|
|
69
|
+
resource_type="AWS::Events::EventBus",
|
|
70
|
+
name=bus_name,
|
|
71
|
+
region=self.region,
|
|
72
|
+
tags=tags,
|
|
73
|
+
config_hash=compute_config_hash(bus),
|
|
74
|
+
created_at=created_at,
|
|
75
|
+
raw_config=bus,
|
|
76
|
+
)
|
|
77
|
+
resources.append(resource)
|
|
78
|
+
|
|
79
|
+
except Exception as e:
|
|
80
|
+
self.logger.error(f"Error collecting EventBridge event buses in {self.region}: {e}")
|
|
81
|
+
|
|
82
|
+
return resources
|
|
83
|
+
|
|
84
|
+
def _collect_event_rules(self) -> List[Resource]:
|
|
85
|
+
"""Collect EventBridge Rules across all event buses.
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
List of Event Rule resources
|
|
89
|
+
"""
|
|
90
|
+
resources = []
|
|
91
|
+
client = self._create_client("events")
|
|
92
|
+
|
|
93
|
+
try:
|
|
94
|
+
# First, get all event buses to collect rules from each
|
|
95
|
+
event_buses = ["default"] # Start with default bus
|
|
96
|
+
|
|
97
|
+
try:
|
|
98
|
+
paginator = client.get_paginator("list_event_buses")
|
|
99
|
+
for page in paginator.paginate():
|
|
100
|
+
for bus in page.get("EventBuses", []):
|
|
101
|
+
bus_name = bus["Name"]
|
|
102
|
+
if bus_name != "default":
|
|
103
|
+
event_buses.append(bus_name)
|
|
104
|
+
except Exception as e:
|
|
105
|
+
self.logger.debug(f"Error listing event buses: {e}")
|
|
106
|
+
|
|
107
|
+
# Collect rules from each bus
|
|
108
|
+
for bus_name in event_buses:
|
|
109
|
+
try:
|
|
110
|
+
paginator = client.get_paginator("list_rules")
|
|
111
|
+
for page in paginator.paginate(EventBusName=bus_name):
|
|
112
|
+
for rule in page.get("Rules", []):
|
|
113
|
+
rule_name = rule["Name"]
|
|
114
|
+
rule_arn = rule["Arn"]
|
|
115
|
+
|
|
116
|
+
# Get tags
|
|
117
|
+
tags = {}
|
|
118
|
+
try:
|
|
119
|
+
tag_response = client.list_tags_for_resource(ResourceARN=rule_arn)
|
|
120
|
+
for tag in tag_response.get("Tags", []):
|
|
121
|
+
tags[tag["Key"]] = tag["Value"]
|
|
122
|
+
except Exception as e:
|
|
123
|
+
self.logger.debug(f"Could not get tags for rule {rule_name}: {e}")
|
|
124
|
+
|
|
125
|
+
# Get full rule details
|
|
126
|
+
try:
|
|
127
|
+
rule_details = client.describe_rule(Name=rule_name, EventBusName=bus_name)
|
|
128
|
+
# Merge with basic rule info
|
|
129
|
+
full_rule = {**rule, **rule_details}
|
|
130
|
+
except Exception as e:
|
|
131
|
+
self.logger.debug(f"Could not get details for rule {rule_name}: {e}")
|
|
132
|
+
full_rule = rule
|
|
133
|
+
|
|
134
|
+
# Extract creation time (not typically available for rules)
|
|
135
|
+
created_at = None
|
|
136
|
+
|
|
137
|
+
# Create resource
|
|
138
|
+
resource = Resource(
|
|
139
|
+
arn=rule_arn,
|
|
140
|
+
resource_type="AWS::Events::Rule",
|
|
141
|
+
name=rule_name,
|
|
142
|
+
region=self.region,
|
|
143
|
+
tags=tags,
|
|
144
|
+
config_hash=compute_config_hash(full_rule),
|
|
145
|
+
created_at=created_at,
|
|
146
|
+
raw_config=full_rule,
|
|
147
|
+
)
|
|
148
|
+
resources.append(resource)
|
|
149
|
+
|
|
150
|
+
except Exception as e:
|
|
151
|
+
self.logger.error(f"Error collecting rules from event bus {bus_name}: {e}")
|
|
152
|
+
|
|
153
|
+
except Exception as e:
|
|
154
|
+
self.logger.error(f"Error collecting EventBridge rules in {self.region}: {e}")
|
|
155
|
+
|
|
156
|
+
return resources
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"""IAM resource collector."""
|
|
2
|
+
|
|
3
|
+
from typing import List
|
|
4
|
+
|
|
5
|
+
from ...models.resource import Resource
|
|
6
|
+
from ...utils.hash import compute_config_hash
|
|
7
|
+
from .base import BaseResourceCollector
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class IAMCollector(BaseResourceCollector):
|
|
11
|
+
"""Collector for AWS IAM resources (roles, users, groups, policies)."""
|
|
12
|
+
|
|
13
|
+
@property
|
|
14
|
+
def service_name(self) -> str:
|
|
15
|
+
return "iam"
|
|
16
|
+
|
|
17
|
+
@property
|
|
18
|
+
def is_global_service(self) -> bool:
|
|
19
|
+
return True
|
|
20
|
+
|
|
21
|
+
def collect(self) -> List[Resource]:
|
|
22
|
+
"""Collect IAM resources.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
List of IAM resources (roles, users, groups, policies)
|
|
26
|
+
"""
|
|
27
|
+
resources = []
|
|
28
|
+
account_id = self._get_account_id()
|
|
29
|
+
|
|
30
|
+
# Collect roles
|
|
31
|
+
resources.extend(self._collect_roles(account_id))
|
|
32
|
+
|
|
33
|
+
# Collect users
|
|
34
|
+
resources.extend(self._collect_users(account_id))
|
|
35
|
+
|
|
36
|
+
# Collect groups
|
|
37
|
+
resources.extend(self._collect_groups(account_id))
|
|
38
|
+
|
|
39
|
+
# Collect policies (customer-managed only)
|
|
40
|
+
resources.extend(self._collect_policies(account_id))
|
|
41
|
+
|
|
42
|
+
self.logger.debug(f"Collected {len(resources)} IAM resources")
|
|
43
|
+
return resources
|
|
44
|
+
|
|
45
|
+
def _collect_roles(self, account_id: str) -> List[Resource]:
|
|
46
|
+
"""Collect IAM roles."""
|
|
47
|
+
resources = []
|
|
48
|
+
client = self._create_client()
|
|
49
|
+
|
|
50
|
+
try:
|
|
51
|
+
paginator = client.get_paginator("list_roles")
|
|
52
|
+
for page in paginator.paginate():
|
|
53
|
+
for role in page["Roles"]:
|
|
54
|
+
# Build ARN
|
|
55
|
+
arn = role["Arn"]
|
|
56
|
+
|
|
57
|
+
# Extract tags
|
|
58
|
+
tags = {}
|
|
59
|
+
try:
|
|
60
|
+
tag_response = client.list_role_tags(RoleName=role["RoleName"])
|
|
61
|
+
tags = {tag["Key"]: tag["Value"] for tag in tag_response.get("Tags", [])}
|
|
62
|
+
except Exception as e:
|
|
63
|
+
self.logger.debug(f"Could not get tags for role {role['RoleName']}: {e}")
|
|
64
|
+
|
|
65
|
+
# Create resource
|
|
66
|
+
resource = Resource(
|
|
67
|
+
arn=arn,
|
|
68
|
+
resource_type="AWS::IAM::Role",
|
|
69
|
+
name=role["RoleName"],
|
|
70
|
+
region="global",
|
|
71
|
+
tags=tags,
|
|
72
|
+
config_hash=compute_config_hash(role),
|
|
73
|
+
created_at=role.get("CreateDate"),
|
|
74
|
+
raw_config=role,
|
|
75
|
+
)
|
|
76
|
+
resources.append(resource)
|
|
77
|
+
|
|
78
|
+
except Exception as e:
|
|
79
|
+
self.logger.error(f"Error collecting IAM roles: {e}")
|
|
80
|
+
|
|
81
|
+
return resources
|
|
82
|
+
|
|
83
|
+
def _collect_users(self, account_id: str) -> List[Resource]:
|
|
84
|
+
"""Collect IAM users."""
|
|
85
|
+
resources = []
|
|
86
|
+
client = self._create_client()
|
|
87
|
+
|
|
88
|
+
try:
|
|
89
|
+
paginator = client.get_paginator("list_users")
|
|
90
|
+
for page in paginator.paginate():
|
|
91
|
+
for user in page["Users"]:
|
|
92
|
+
# Build ARN
|
|
93
|
+
arn = user["Arn"]
|
|
94
|
+
|
|
95
|
+
# Extract tags
|
|
96
|
+
tags = {}
|
|
97
|
+
try:
|
|
98
|
+
tag_response = client.list_user_tags(UserName=user["UserName"])
|
|
99
|
+
tags = {tag["Key"]: tag["Value"] for tag in tag_response.get("Tags", [])}
|
|
100
|
+
except Exception as e:
|
|
101
|
+
self.logger.debug(f"Could not get tags for user {user['UserName']}: {e}")
|
|
102
|
+
|
|
103
|
+
# Create resource
|
|
104
|
+
resource = Resource(
|
|
105
|
+
arn=arn,
|
|
106
|
+
resource_type="AWS::IAM::User",
|
|
107
|
+
name=user["UserName"],
|
|
108
|
+
region="global",
|
|
109
|
+
tags=tags,
|
|
110
|
+
config_hash=compute_config_hash(user),
|
|
111
|
+
created_at=user.get("CreateDate"),
|
|
112
|
+
raw_config=user,
|
|
113
|
+
)
|
|
114
|
+
resources.append(resource)
|
|
115
|
+
|
|
116
|
+
except Exception as e:
|
|
117
|
+
self.logger.error(f"Error collecting IAM users: {e}")
|
|
118
|
+
|
|
119
|
+
return resources
|
|
120
|
+
|
|
121
|
+
def _collect_groups(self, account_id: str) -> List[Resource]:
|
|
122
|
+
"""Collect IAM groups."""
|
|
123
|
+
resources = []
|
|
124
|
+
client = self._create_client()
|
|
125
|
+
|
|
126
|
+
try:
|
|
127
|
+
paginator = client.get_paginator("list_groups")
|
|
128
|
+
for page in paginator.paginate():
|
|
129
|
+
for group in page["Groups"]:
|
|
130
|
+
# Build ARN
|
|
131
|
+
arn = group["Arn"]
|
|
132
|
+
|
|
133
|
+
# Create resource (groups don't support tags)
|
|
134
|
+
resource = Resource(
|
|
135
|
+
arn=arn,
|
|
136
|
+
resource_type="AWS::IAM::Group",
|
|
137
|
+
name=group["GroupName"],
|
|
138
|
+
region="global",
|
|
139
|
+
tags={},
|
|
140
|
+
config_hash=compute_config_hash(group),
|
|
141
|
+
created_at=group.get("CreateDate"),
|
|
142
|
+
raw_config=group,
|
|
143
|
+
)
|
|
144
|
+
resources.append(resource)
|
|
145
|
+
|
|
146
|
+
except Exception as e:
|
|
147
|
+
self.logger.error(f"Error collecting IAM groups: {e}")
|
|
148
|
+
|
|
149
|
+
return resources
|
|
150
|
+
|
|
151
|
+
def _collect_policies(self, account_id: str) -> List[Resource]:
|
|
152
|
+
"""Collect customer-managed IAM policies."""
|
|
153
|
+
resources = []
|
|
154
|
+
client = self._create_client()
|
|
155
|
+
|
|
156
|
+
try:
|
|
157
|
+
paginator = client.get_paginator("list_policies")
|
|
158
|
+
# Only get customer-managed policies (not AWS-managed)
|
|
159
|
+
for page in paginator.paginate(Scope="Local"):
|
|
160
|
+
for policy in page["Policies"]:
|
|
161
|
+
# Build ARN
|
|
162
|
+
arn = policy["Arn"]
|
|
163
|
+
|
|
164
|
+
# Extract tags
|
|
165
|
+
tags = {}
|
|
166
|
+
try:
|
|
167
|
+
tag_response = client.list_policy_tags(PolicyArn=arn)
|
|
168
|
+
tags = {tag["Key"]: tag["Value"] for tag in tag_response.get("Tags", [])}
|
|
169
|
+
except Exception as e:
|
|
170
|
+
self.logger.debug(f"Could not get tags for policy {policy['PolicyName']}: {e}")
|
|
171
|
+
|
|
172
|
+
# Create resource
|
|
173
|
+
resource = Resource(
|
|
174
|
+
arn=arn,
|
|
175
|
+
resource_type="AWS::IAM::Policy",
|
|
176
|
+
name=policy["PolicyName"],
|
|
177
|
+
region="global",
|
|
178
|
+
tags=tags,
|
|
179
|
+
config_hash=compute_config_hash(policy),
|
|
180
|
+
created_at=policy.get("CreateDate"),
|
|
181
|
+
raw_config=policy,
|
|
182
|
+
)
|
|
183
|
+
resources.append(resource)
|
|
184
|
+
|
|
185
|
+
except Exception as e:
|
|
186
|
+
self.logger.error(f"Error collecting IAM policies: {e}")
|
|
187
|
+
|
|
188
|
+
return resources
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""KMS resource collector."""
|
|
2
|
+
|
|
3
|
+
from typing import List
|
|
4
|
+
|
|
5
|
+
from ...models.resource import Resource
|
|
6
|
+
from ...utils.hash import compute_config_hash
|
|
7
|
+
from .base import BaseResourceCollector
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class KMSCollector(BaseResourceCollector):
|
|
11
|
+
"""Collector for AWS KMS (Key Management Service) resources."""
|
|
12
|
+
|
|
13
|
+
@property
|
|
14
|
+
def service_name(self) -> str:
|
|
15
|
+
return "kms"
|
|
16
|
+
|
|
17
|
+
def collect(self) -> List[Resource]:
|
|
18
|
+
"""Collect KMS keys.
|
|
19
|
+
|
|
20
|
+
Collects customer-managed KMS keys (not AWS-managed keys).
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
List of KMS key resources
|
|
24
|
+
"""
|
|
25
|
+
resources = []
|
|
26
|
+
client = self._create_client()
|
|
27
|
+
|
|
28
|
+
try:
|
|
29
|
+
paginator = client.get_paginator("list_keys")
|
|
30
|
+
for page in paginator.paginate():
|
|
31
|
+
for key_item in page.get("Keys", []):
|
|
32
|
+
key_id = key_item["KeyId"]
|
|
33
|
+
key_arn = key_item["KeyArn"]
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
# Get key metadata
|
|
37
|
+
key_metadata = client.describe_key(KeyId=key_id)["KeyMetadata"]
|
|
38
|
+
|
|
39
|
+
# Skip AWS-managed keys (we only want customer-managed keys)
|
|
40
|
+
if key_metadata.get("KeyManager") == "AWS":
|
|
41
|
+
continue
|
|
42
|
+
|
|
43
|
+
# Skip keys that are pending deletion
|
|
44
|
+
if key_metadata.get("KeyState") == "PendingDeletion":
|
|
45
|
+
self.logger.debug(f"Skipping key {key_id} - pending deletion")
|
|
46
|
+
continue
|
|
47
|
+
|
|
48
|
+
key_alias = None
|
|
49
|
+
|
|
50
|
+
# Get key aliases
|
|
51
|
+
try:
|
|
52
|
+
aliases_response = client.list_aliases(KeyId=key_id)
|
|
53
|
+
aliases = aliases_response.get("Aliases", [])
|
|
54
|
+
if aliases:
|
|
55
|
+
# Use first alias as the name
|
|
56
|
+
key_alias = aliases[0]["AliasName"]
|
|
57
|
+
except Exception as e:
|
|
58
|
+
self.logger.debug(f"Could not get aliases for key {key_id}: {e}")
|
|
59
|
+
|
|
60
|
+
# Get tags
|
|
61
|
+
tags = {}
|
|
62
|
+
try:
|
|
63
|
+
tag_response = client.list_resource_tags(KeyId=key_id)
|
|
64
|
+
for tag in tag_response.get("Tags", []):
|
|
65
|
+
tags[tag["TagKey"]] = tag["TagValue"]
|
|
66
|
+
except Exception as e:
|
|
67
|
+
self.logger.debug(f"Could not get tags for key {key_id}: {e}")
|
|
68
|
+
|
|
69
|
+
# Get key rotation status
|
|
70
|
+
rotation_enabled = False
|
|
71
|
+
try:
|
|
72
|
+
rotation_response = client.get_key_rotation_status(KeyId=key_id)
|
|
73
|
+
rotation_enabled = rotation_response.get("KeyRotationEnabled", False)
|
|
74
|
+
except Exception as e:
|
|
75
|
+
self.logger.debug(f"Could not get rotation status for key {key_id}: {e}")
|
|
76
|
+
|
|
77
|
+
# Build config for hash
|
|
78
|
+
config = {
|
|
79
|
+
**key_metadata,
|
|
80
|
+
"RotationEnabled": rotation_enabled,
|
|
81
|
+
"Aliases": [key_alias] if key_alias else [],
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
# Extract creation date
|
|
85
|
+
created_at = key_metadata.get("CreationDate")
|
|
86
|
+
|
|
87
|
+
# Use alias as name if available, otherwise use key ID
|
|
88
|
+
name = key_alias if key_alias else key_id
|
|
89
|
+
|
|
90
|
+
# Create resource
|
|
91
|
+
resource = Resource(
|
|
92
|
+
arn=key_arn,
|
|
93
|
+
resource_type="AWS::KMS::Key",
|
|
94
|
+
name=name,
|
|
95
|
+
region=self.region,
|
|
96
|
+
tags=tags,
|
|
97
|
+
config_hash=compute_config_hash(config),
|
|
98
|
+
created_at=created_at,
|
|
99
|
+
raw_config=config,
|
|
100
|
+
)
|
|
101
|
+
resources.append(resource)
|
|
102
|
+
|
|
103
|
+
except Exception as e:
|
|
104
|
+
self.logger.debug(f"Error processing key {key_id}: {e}")
|
|
105
|
+
continue
|
|
106
|
+
|
|
107
|
+
except Exception as e:
|
|
108
|
+
self.logger.error(f"Error collecting KMS keys in {self.region}: {e}")
|
|
109
|
+
|
|
110
|
+
self.logger.debug(f"Collected {len(resources)} KMS keys in {self.region}")
|
|
111
|
+
return resources
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""Lambda resource collector."""
|
|
2
|
+
|
|
3
|
+
from typing import List
|
|
4
|
+
|
|
5
|
+
from ...models.resource import Resource
|
|
6
|
+
from ...utils.hash import compute_config_hash
|
|
7
|
+
from .base import BaseResourceCollector
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class LambdaCollector(BaseResourceCollector):
|
|
11
|
+
"""Collector for AWS Lambda functions and layers."""
|
|
12
|
+
|
|
13
|
+
@property
|
|
14
|
+
def service_name(self) -> str:
|
|
15
|
+
return "lambda"
|
|
16
|
+
|
|
17
|
+
def collect(self) -> List[Resource]:
|
|
18
|
+
"""Collect Lambda resources.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
List of Lambda functions and layers
|
|
22
|
+
"""
|
|
23
|
+
resources = []
|
|
24
|
+
account_id = self._get_account_id()
|
|
25
|
+
|
|
26
|
+
# Collect functions
|
|
27
|
+
resources.extend(self._collect_functions(account_id))
|
|
28
|
+
|
|
29
|
+
# Collect layers
|
|
30
|
+
resources.extend(self._collect_layers(account_id))
|
|
31
|
+
|
|
32
|
+
self.logger.debug(f"Collected {len(resources)} Lambda resources in {self.region}")
|
|
33
|
+
return resources
|
|
34
|
+
|
|
35
|
+
def _collect_functions(self, account_id: str) -> List[Resource]:
|
|
36
|
+
"""Collect Lambda functions."""
|
|
37
|
+
resources = []
|
|
38
|
+
client = self._create_client()
|
|
39
|
+
|
|
40
|
+
try:
|
|
41
|
+
paginator = client.get_paginator("list_functions")
|
|
42
|
+
for page in paginator.paginate():
|
|
43
|
+
for function in page["Functions"]:
|
|
44
|
+
function_name = function["FunctionName"]
|
|
45
|
+
function_arn = function["FunctionArn"]
|
|
46
|
+
|
|
47
|
+
# Get full function configuration (includes tags)
|
|
48
|
+
try:
|
|
49
|
+
full_config = client.get_function(FunctionName=function_name)
|
|
50
|
+
tags = full_config.get("Tags", {})
|
|
51
|
+
config_data = full_config.get("Configuration", function)
|
|
52
|
+
except Exception as e:
|
|
53
|
+
self.logger.debug(f"Could not get full config for {function_name}: {e}")
|
|
54
|
+
tags = {}
|
|
55
|
+
config_data = function
|
|
56
|
+
|
|
57
|
+
# Create resource
|
|
58
|
+
resource = Resource(
|
|
59
|
+
arn=function_arn,
|
|
60
|
+
resource_type="AWS::Lambda::Function",
|
|
61
|
+
name=function_name,
|
|
62
|
+
region=self.region,
|
|
63
|
+
tags=tags,
|
|
64
|
+
config_hash=compute_config_hash(config_data),
|
|
65
|
+
created_at=None, # Lambda doesn't expose creation date easily
|
|
66
|
+
raw_config=config_data,
|
|
67
|
+
)
|
|
68
|
+
resources.append(resource)
|
|
69
|
+
|
|
70
|
+
except Exception as e:
|
|
71
|
+
self.logger.error(f"Error collecting Lambda functions in {self.region}: {e}")
|
|
72
|
+
|
|
73
|
+
return resources
|
|
74
|
+
|
|
75
|
+
def _collect_layers(self, account_id: str) -> List[Resource]:
|
|
76
|
+
"""Collect Lambda layers."""
|
|
77
|
+
resources = []
|
|
78
|
+
client = self._create_client()
|
|
79
|
+
|
|
80
|
+
try:
|
|
81
|
+
paginator = client.get_paginator("list_layers")
|
|
82
|
+
for page in paginator.paginate():
|
|
83
|
+
for layer in page["Layers"]:
|
|
84
|
+
layer_name = layer["LayerName"]
|
|
85
|
+
layer_arn = layer["LayerArn"]
|
|
86
|
+
|
|
87
|
+
# Get latest version info
|
|
88
|
+
try:
|
|
89
|
+
latest_version = layer.get("LatestMatchingVersion", {})
|
|
90
|
+
layer_version_arn = latest_version.get("LayerVersionArn", layer_arn)
|
|
91
|
+
created_date = latest_version.get("CreatedDate")
|
|
92
|
+
|
|
93
|
+
# Create resource
|
|
94
|
+
resource = Resource(
|
|
95
|
+
arn=layer_version_arn,
|
|
96
|
+
resource_type="AWS::Lambda::LayerVersion",
|
|
97
|
+
name=layer_name,
|
|
98
|
+
region=self.region,
|
|
99
|
+
tags={}, # Layers don't support tags
|
|
100
|
+
config_hash=compute_config_hash(layer),
|
|
101
|
+
created_at=created_date,
|
|
102
|
+
raw_config=layer,
|
|
103
|
+
)
|
|
104
|
+
resources.append(resource)
|
|
105
|
+
|
|
106
|
+
except Exception as e:
|
|
107
|
+
self.logger.debug(f"Could not get layer version for {layer_name}: {e}")
|
|
108
|
+
|
|
109
|
+
except Exception as e:
|
|
110
|
+
self.logger.error(f"Error collecting Lambda layers in {self.region}: {e}")
|
|
111
|
+
|
|
112
|
+
return resources
|