cartography 0.108.0rc1__py3-none-any.whl → 0.109.0rc1__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 cartography might be problematic. Click here for more details.

Files changed (37) hide show
  1. cartography/_version.py +2 -2
  2. cartography/data/indexes.cypher +0 -2
  3. cartography/data/jobs/cleanup/gcp_compute_vpc_cleanup.json +0 -12
  4. cartography/intel/aws/cloudtrail.py +17 -4
  5. cartography/intel/aws/cloudtrail_management_events.py +593 -16
  6. cartography/intel/aws/cloudwatch.py +73 -4
  7. cartography/intel/aws/ec2/subnets.py +37 -63
  8. cartography/intel/aws/ecr.py +55 -80
  9. cartography/intel/aws/elasticache.py +102 -79
  10. cartography/intel/aws/resourcegroupstaggingapi.py +77 -18
  11. cartography/intel/aws/secretsmanager.py +62 -44
  12. cartography/intel/entra/groups.py +29 -1
  13. cartography/intel/gcp/__init__.py +10 -0
  14. cartography/intel/gcp/compute.py +19 -42
  15. cartography/models/aws/cloudtrail/management_events.py +95 -6
  16. cartography/models/aws/cloudtrail/trail.py +21 -0
  17. cartography/models/aws/cloudwatch/metric_alarm.py +53 -0
  18. cartography/models/aws/ec2/subnets.py +65 -0
  19. cartography/models/aws/ecr/__init__.py +0 -0
  20. cartography/models/aws/ecr/image.py +41 -0
  21. cartography/models/aws/ecr/repository.py +72 -0
  22. cartography/models/aws/ecr/repository_image.py +95 -0
  23. cartography/models/aws/elasticache/__init__.py +0 -0
  24. cartography/models/aws/elasticache/cluster.py +65 -0
  25. cartography/models/aws/elasticache/topic.py +67 -0
  26. cartography/models/aws/secretsmanager/secret.py +106 -0
  27. cartography/models/entra/group.py +26 -0
  28. cartography/models/entra/user.py +6 -0
  29. cartography/models/gcp/compute/__init__.py +0 -0
  30. cartography/models/gcp/compute/vpc.py +50 -0
  31. {cartography-0.108.0rc1.dist-info → cartography-0.109.0rc1.dist-info}/METADATA +1 -1
  32. {cartography-0.108.0rc1.dist-info → cartography-0.109.0rc1.dist-info}/RECORD +36 -25
  33. cartography/data/jobs/cleanup/aws_import_secrets_cleanup.json +0 -8
  34. {cartography-0.108.0rc1.dist-info → cartography-0.109.0rc1.dist-info}/WHEEL +0 -0
  35. {cartography-0.108.0rc1.dist-info → cartography-0.109.0rc1.dist-info}/entry_points.txt +0 -0
  36. {cartography-0.108.0rc1.dist-info → cartography-0.109.0rc1.dist-info}/licenses/LICENSE +0 -0
  37. {cartography-0.108.0rc1.dist-info → cartography-0.109.0rc1.dist-info}/top_level.txt +0 -0
@@ -13,6 +13,7 @@ from cartography.models.aws.cloudwatch.log_metric_filter import (
13
13
  CloudWatchLogMetricFilterSchema,
14
14
  )
15
15
  from cartography.models.aws.cloudwatch.loggroup import CloudWatchLogGroupSchema
16
+ from cartography.models.aws.cloudwatch.metric_alarm import CloudWatchMetricAlarmSchema
16
17
  from cartography.util import aws_handle_regions
17
18
  from cartography.util import timeit
18
19
 
@@ -75,6 +76,43 @@ def transform_metric_filters(
75
76
  return transformed_filters
76
77
 
77
78
 
79
+ @timeit
80
+ @aws_handle_regions
81
+ def get_cloudwatch_metric_alarms(
82
+ boto3_session: boto3.Session, region: str
83
+ ) -> List[Dict[str, Any]]:
84
+ client = boto3_session.client(
85
+ "cloudwatch", region_name=region, config=get_botocore_config()
86
+ )
87
+ paginator = client.get_paginator("describe_alarms")
88
+ alarms = []
89
+ for page in paginator.paginate():
90
+ alarms.extend(page["MetricAlarms"])
91
+ return alarms
92
+
93
+
94
+ def transform_metric_alarms(
95
+ metric_alarms: List[Dict[str, Any]], region: str
96
+ ) -> List[Dict[str, Any]]:
97
+ """
98
+ Transform CloudWatch metric alarm data for ingestion into Neo4j.
99
+ """
100
+ transformed_alarms = []
101
+ for alarm in metric_alarms:
102
+ transformed_alarm = {
103
+ "AlarmArn": alarm["AlarmArn"],
104
+ "AlarmName": alarm.get("AlarmName"),
105
+ "AlarmDescription": alarm.get("AlarmDescription"),
106
+ "StateValue": alarm.get("StateValue"),
107
+ "StateReason": alarm.get("StateReason"),
108
+ "ActionsEnabled": alarm.get("ActionsEnabled"),
109
+ "ComparisonOperator": alarm.get("ComparisonOperator"),
110
+ "Region": region,
111
+ }
112
+ transformed_alarms.append(transformed_alarm)
113
+ return transformed_alarms
114
+
115
+
78
116
  @timeit
79
117
  def load_cloudwatch_log_groups(
80
118
  neo4j_session: neo4j.Session,
@@ -117,6 +155,27 @@ def load_cloudwatch_log_metric_filters(
117
155
  )
118
156
 
119
157
 
158
+ @timeit
159
+ def load_cloudwatch_metric_alarms(
160
+ neo4j_session: neo4j.Session,
161
+ data: List[Dict[str, Any]],
162
+ region: str,
163
+ current_aws_account_id: str,
164
+ aws_update_tag: int,
165
+ ) -> None:
166
+ logger.info(
167
+ f"Loading CloudWatch {len(data)} metric alarms for region '{region}' into graph.",
168
+ )
169
+ load(
170
+ neo4j_session,
171
+ CloudWatchMetricAlarmSchema(),
172
+ data,
173
+ lastupdated=aws_update_tag,
174
+ Region=region,
175
+ AWS_ID=current_aws_account_id,
176
+ )
177
+
178
+
120
179
  @timeit
121
180
  def cleanup(
122
181
  neo4j_session: neo4j.Session,
@@ -130,6 +189,9 @@ def cleanup(
130
189
  GraphJob.from_node_schema(
131
190
  CloudWatchLogMetricFilterSchema(), common_job_parameters
132
191
  ).run(neo4j_session)
192
+ GraphJob.from_node_schema(CloudWatchMetricAlarmSchema(), common_job_parameters).run(
193
+ neo4j_session
194
+ )
133
195
 
134
196
 
135
197
  @timeit
@@ -146,13 +208,10 @@ def sync(
146
208
  f"Syncing CloudWatch for region '{region}' in account '{current_aws_account_id}'.",
147
209
  )
148
210
  logGroups = get_cloudwatch_log_groups(boto3_session, region)
149
- group_data: List[Dict[str, Any]] = []
150
- for logGroup in logGroups:
151
- group_data.append(logGroup)
152
211
 
153
212
  load_cloudwatch_log_groups(
154
213
  neo4j_session,
155
- group_data,
214
+ logGroups,
156
215
  region,
157
216
  current_aws_account_id,
158
217
  update_tag,
@@ -167,4 +226,14 @@ def sync(
167
226
  current_aws_account_id,
168
227
  update_tag,
169
228
  )
229
+
230
+ metric_alarms = get_cloudwatch_metric_alarms(boto3_session, region)
231
+ transformed_alarms = transform_metric_alarms(metric_alarms, region)
232
+ load_cloudwatch_metric_alarms(
233
+ neo4j_session,
234
+ transformed_alarms,
235
+ region,
236
+ current_aws_account_id,
237
+ update_tag,
238
+ )
170
239
  cleanup(neo4j_session, common_job_parameters)
@@ -1,17 +1,17 @@
1
1
  import logging
2
- from typing import Dict
3
- from typing import List
2
+ from typing import Any
4
3
 
5
4
  import boto3
6
5
  import neo4j
7
6
 
7
+ from cartography.client.core.tx import load
8
8
  from cartography.graph.job import GraphJob
9
9
  from cartography.models.aws.ec2.auto_scaling_groups import (
10
10
  EC2SubnetAutoScalingGroupSchema,
11
11
  )
12
12
  from cartography.models.aws.ec2.subnet_instance import EC2SubnetInstanceSchema
13
+ from cartography.models.aws.ec2.subnets import EC2SubnetSchema
13
14
  from cartography.util import aws_handle_regions
14
- from cartography.util import run_cleanup_job
15
15
  from cartography.util import timeit
16
16
 
17
17
  from .util import get_botocore_config
@@ -21,86 +21,53 @@ logger = logging.getLogger(__name__)
21
21
 
22
22
  @timeit
23
23
  @aws_handle_regions
24
- def get_subnet_data(boto3_session: boto3.session.Session, region: str) -> List[Dict]:
24
+ def get_subnet_data(
25
+ boto3_session: boto3.session.Session, region: str
26
+ ) -> list[dict[str, Any]]:
25
27
  client = boto3_session.client(
26
28
  "ec2",
27
29
  region_name=region,
28
30
  config=get_botocore_config(),
29
31
  )
30
32
  paginator = client.get_paginator("describe_subnets")
31
- subnets: List[Dict] = []
33
+ subnets: list[dict[str, Any]] = []
32
34
  for page in paginator.paginate():
33
35
  subnets.extend(page["Subnets"])
34
36
  return subnets
35
37
 
36
38
 
39
+ def transform_subnet_data(subnets: list[dict[str, Any]]) -> list[dict[str, Any]]:
40
+ """Transform subnet data into a loadable format."""
41
+ transformed: list[dict[str, Any]] = []
42
+ for subnet in subnets:
43
+ transformed.append(subnet.copy())
44
+ return transformed
45
+
46
+
37
47
  @timeit
38
48
  def load_subnets(
39
49
  neo4j_session: neo4j.Session,
40
- data: List[Dict],
50
+ data: list[dict[str, Any]],
41
51
  region: str,
42
52
  aws_account_id: str,
43
53
  aws_update_tag: int,
44
54
  ) -> None:
45
-
46
- ingest_subnets = """
47
- UNWIND $subnets as subnet
48
- MERGE (snet:EC2Subnet{subnetid: subnet.SubnetId})
49
- ON CREATE SET snet.firstseen = timestamp()
50
- SET snet.lastupdated = $aws_update_tag, snet.name = subnet.CidrBlock, snet.cidr_block = subnet.CidrBlock,
51
- snet.available_ip_address_count = subnet.AvailableIpAddressCount, snet.default_for_az = subnet.DefaultForAz,
52
- snet.map_customer_owned_ip_on_launch = subnet.MapCustomerOwnedIpOnLaunch,
53
- snet.state = subnet.State, snet.assignipv6addressoncreation = subnet.AssignIpv6AddressOnCreation,
54
- snet.map_public_ip_on_launch = subnet.MapPublicIpOnLaunch, snet.subnet_arn = subnet.SubnetArn,
55
- snet.availability_zone = subnet.AvailabilityZone, snet.availability_zone_id = subnet.AvailabilityZoneId,
56
- snet.subnet_id = subnet.SubnetId
57
- """
58
-
59
- ingest_subnet_vpc_relations = """
60
- UNWIND $subnets as subnet
61
- MATCH (snet:EC2Subnet{subnetid: subnet.SubnetId}), (vpc:AWSVpc{id: subnet.VpcId})
62
- MERGE (snet)-[r:MEMBER_OF_AWS_VPC]->(vpc)
63
- ON CREATE SET r.firstseen = timestamp()
64
- SET r.lastupdated = $aws_update_tag
65
- """
66
-
67
- ingest_subnet_aws_account_relations = """
68
- UNWIND $subnets as subnet
69
- MATCH (snet:EC2Subnet{subnetid: subnet.SubnetId}), (aws:AWSAccount{id: $aws_account_id})
70
- MERGE (aws)-[r:RESOURCE]->(snet)
71
- ON CREATE SET r.firstseen = timestamp()
72
- SET r.lastupdated = $aws_update_tag
73
- """
74
-
75
- neo4j_session.run(
76
- ingest_subnets,
77
- subnets=data,
78
- aws_update_tag=aws_update_tag,
79
- region=region,
80
- aws_account_id=aws_account_id,
81
- )
82
- neo4j_session.run(
83
- ingest_subnet_vpc_relations,
84
- subnets=data,
85
- aws_update_tag=aws_update_tag,
86
- region=region,
87
- aws_account_id=aws_account_id,
88
- )
89
- neo4j_session.run(
90
- ingest_subnet_aws_account_relations,
91
- subnets=data,
92
- aws_update_tag=aws_update_tag,
93
- region=region,
94
- aws_account_id=aws_account_id,
55
+ load(
56
+ neo4j_session,
57
+ EC2SubnetSchema(),
58
+ data,
59
+ Region=region,
60
+ AWS_ID=aws_account_id,
61
+ lastupdated=aws_update_tag,
95
62
  )
96
63
 
97
64
 
98
65
  @timeit
99
- def cleanup_subnets(neo4j_session: neo4j.Session, common_job_parameters: Dict) -> None:
100
- run_cleanup_job(
101
- "aws_ingest_subnets_cleanup.json",
66
+ def cleanup_subnets(
67
+ neo4j_session: neo4j.Session, common_job_parameters: dict[str, Any]
68
+ ) -> None:
69
+ GraphJob.from_node_schema(EC2SubnetSchema(), common_job_parameters).run(
102
70
  neo4j_session,
103
- common_job_parameters,
104
71
  )
105
72
  GraphJob.from_node_schema(EC2SubnetInstanceSchema(), common_job_parameters).run(
106
73
  neo4j_session,
@@ -115,10 +82,10 @@ def cleanup_subnets(neo4j_session: neo4j.Session, common_job_parameters: Dict) -
115
82
  def sync_subnets(
116
83
  neo4j_session: neo4j.Session,
117
84
  boto3_session: boto3.session.Session,
118
- regions: List[str],
85
+ regions: list[str],
119
86
  current_aws_account_id: str,
120
87
  update_tag: int,
121
- common_job_parameters: Dict,
88
+ common_job_parameters: dict[str, Any],
122
89
  ) -> None:
123
90
  for region in regions:
124
91
  logger.info(
@@ -127,5 +94,12 @@ def sync_subnets(
127
94
  current_aws_account_id,
128
95
  )
129
96
  data = get_subnet_data(boto3_session, region)
130
- load_subnets(neo4j_session, data, region, current_aws_account_id, update_tag)
97
+ transformed = transform_subnet_data(data)
98
+ load_subnets(
99
+ neo4j_session,
100
+ transformed,
101
+ region,
102
+ current_aws_account_id,
103
+ update_tag,
104
+ )
131
105
  cleanup_subnets(neo4j_session, common_job_parameters)
@@ -6,9 +6,12 @@ from typing import List
6
6
  import boto3
7
7
  import neo4j
8
8
 
9
+ from cartography.client.core.tx import load
10
+ from cartography.graph.job import GraphJob
11
+ from cartography.models.aws.ecr.image import ECRImageSchema
12
+ from cartography.models.aws.ecr.repository import ECRRepositorySchema
13
+ from cartography.models.aws.ecr.repository_image import ECRRepositoryImageSchema
9
14
  from cartography.util import aws_handle_regions
10
- from cartography.util import batch
11
- from cartography.util import run_cleanup_job
12
15
  from cartography.util import timeit
13
16
  from cartography.util import to_asynchronous
14
17
  from cartography.util import to_synchronous
@@ -74,33 +77,17 @@ def load_ecr_repositories(
74
77
  current_aws_account_id: str,
75
78
  aws_update_tag: int,
76
79
  ) -> None:
77
- query = """
78
- UNWIND $Repositories as ecr_repo
79
- MERGE (repo:ECRRepository{id: ecr_repo.repositoryArn})
80
- ON CREATE SET repo.firstseen = timestamp(),
81
- repo.arn = ecr_repo.repositoryArn,
82
- repo.name = ecr_repo.repositoryName,
83
- repo.region = $Region,
84
- repo.created_at = ecr_repo.createdAt
85
- SET repo.lastupdated = $aws_update_tag,
86
- repo.uri = ecr_repo.repositoryUri
87
- WITH repo
88
-
89
- MATCH (owner:AWSAccount{id: $AWS_ACCOUNT_ID})
90
- MERGE (owner)-[r:RESOURCE]->(repo)
91
- ON CREATE SET r.firstseen = timestamp()
92
- SET r.lastupdated = $aws_update_tag
93
- """
94
80
  logger.info(
95
81
  f"Loading {len(repos)} ECR repositories for region {region} into graph.",
96
82
  )
97
- neo4j_session.run(
98
- query,
99
- Repositories=repos,
83
+ load(
84
+ neo4j_session,
85
+ ECRRepositorySchema(),
86
+ repos,
87
+ lastupdated=aws_update_tag,
100
88
  Region=region,
101
- aws_update_tag=aws_update_tag,
102
- AWS_ACCOUNT_ID=current_aws_account_id,
103
- ).consume() # See issue #440
89
+ AWS_ID=current_aws_account_id,
90
+ )
104
91
 
105
92
 
106
93
  @timeit
@@ -114,8 +101,13 @@ def transform_ecr_repository_images(repo_data: Dict) -> List[Dict]:
114
101
  for repo_uri in sorted(repo_data.keys()):
115
102
  repo_images = repo_data[repo_uri]
116
103
  for img in repo_images:
117
- if "imageDigest" in img and img["imageDigest"]:
104
+ digest = img.get("imageDigest")
105
+ if digest:
106
+ tag = img.get("imageTag")
107
+ uri = repo_uri + (f":{tag}" if tag else "")
118
108
  img["repo_uri"] = repo_uri
109
+ img["uri"] = uri
110
+ img["id"] = uri
119
111
  repo_images_list.append(img)
120
112
  else:
121
113
  logger.warning(
@@ -127,74 +119,51 @@ def transform_ecr_repository_images(repo_data: Dict) -> List[Dict]:
127
119
  return repo_images_list
128
120
 
129
121
 
130
- def _load_ecr_repo_img_tx(
131
- tx: neo4j.Transaction,
132
- repo_images_list: List[Dict],
133
- aws_update_tag: int,
134
- region: str,
135
- ) -> None:
136
- query = """
137
- UNWIND $RepoList as repo_img
138
- MERGE (ri:ECRRepositoryImage{id: repo_img.repo_uri + COALESCE(":" + repo_img.imageTag, '')})
139
- ON CREATE SET ri.firstseen = timestamp()
140
- SET ri.lastupdated = $aws_update_tag,
141
- ri.tag = repo_img.imageTag,
142
- ri.uri = repo_img.repo_uri + COALESCE(":" + repo_img.imageTag, ''),
143
- ri.image_size_bytes = repo_img.imageSizeInBytes,
144
- ri.image_pushed_at = repo_img.imagePushedAt,
145
- ri.image_manifest_media_type = repo_img.imageManifestMediaType,
146
- ri.artifact_media_type = repo_img.artifactMediaType,
147
- ri.last_recorded_pull_time = repo_img.lastRecordedPullTime
148
- WITH ri, repo_img
149
-
150
- MERGE (img:ECRImage{id: repo_img.imageDigest})
151
- ON CREATE SET img.firstseen = timestamp(),
152
- img.digest = repo_img.imageDigest
153
- SET img.lastupdated = $aws_update_tag,
154
- img.region = $Region
155
- WITH ri, img, repo_img
156
-
157
- MERGE (ri)-[r1:IMAGE]->(img)
158
- ON CREATE SET r1.firstseen = timestamp()
159
- SET r1.lastupdated = $aws_update_tag
160
- WITH ri, repo_img
161
-
162
- MATCH (repo:ECRRepository{uri: repo_img.repo_uri})
163
- MERGE (repo)-[r2:REPO_IMAGE]->(ri)
164
- ON CREATE SET r2.firstseen = timestamp()
165
- SET r2.lastupdated = $aws_update_tag
166
- """
167
- tx.run(
168
- query,
169
- RepoList=repo_images_list,
170
- Region=region,
171
- aws_update_tag=aws_update_tag,
172
- )
173
-
174
-
175
122
  @timeit
176
123
  def load_ecr_repository_images(
177
124
  neo4j_session: neo4j.Session,
178
125
  repo_images_list: List[Dict],
179
126
  region: str,
127
+ current_aws_account_id: str,
180
128
  aws_update_tag: int,
181
129
  ) -> None:
182
130
  logger.info(
183
131
  f"Loading {len(repo_images_list)} ECR repository images in {region} into graph.",
184
132
  )
185
- for repo_image_batch in batch(repo_images_list, size=10000):
186
- neo4j_session.write_transaction(
187
- _load_ecr_repo_img_tx,
188
- repo_image_batch,
189
- aws_update_tag,
190
- region,
191
- )
133
+ image_digests = {img["imageDigest"] for img in repo_images_list}
134
+ ecr_images = [{"imageDigest": d} for d in image_digests]
135
+
136
+ load(
137
+ neo4j_session,
138
+ ECRImageSchema(),
139
+ ecr_images,
140
+ lastupdated=aws_update_tag,
141
+ Region=region,
142
+ AWS_ID=current_aws_account_id,
143
+ )
144
+
145
+ load(
146
+ neo4j_session,
147
+ ECRRepositoryImageSchema(),
148
+ repo_images_list,
149
+ lastupdated=aws_update_tag,
150
+ Region=region,
151
+ AWS_ID=current_aws_account_id,
152
+ )
192
153
 
193
154
 
194
155
  @timeit
195
156
  def cleanup(neo4j_session: neo4j.Session, common_job_parameters: Dict) -> None:
196
157
  logger.debug("Running ECR cleanup job.")
197
- run_cleanup_job("aws_import_ecr_cleanup.json", neo4j_session, common_job_parameters)
158
+ GraphJob.from_node_schema(ECRRepositorySchema(), common_job_parameters).run(
159
+ neo4j_session
160
+ )
161
+ GraphJob.from_node_schema(ECRRepositoryImageSchema(), common_job_parameters).run(
162
+ neo4j_session
163
+ )
164
+ GraphJob.from_node_schema(ECRImageSchema(), common_job_parameters).run(
165
+ neo4j_session
166
+ )
198
167
 
199
168
 
200
169
  def _get_image_data(
@@ -251,5 +220,11 @@ def sync(
251
220
  update_tag,
252
221
  )
253
222
  repo_images_list = transform_ecr_repository_images(image_data)
254
- load_ecr_repository_images(neo4j_session, repo_images_list, region, update_tag)
223
+ load_ecr_repository_images(
224
+ neo4j_session,
225
+ repo_images_list,
226
+ region,
227
+ current_aws_account_id,
228
+ update_tag,
229
+ )
255
230
  cleanup(neo4j_session, common_job_parameters)