Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto scaling groups data model refactor #1394

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
.*.swp
.DS_Store
/venv*
.venv/
.cache/
build/
.idea/
Expand Down
327 changes: 142 additions & 185 deletions cartography/intel/aws/ec2/auto_scaling_groups.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions cartography/intel/aws/ec2/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cartography.client.core.tx import load
from cartography.graph.job import GraphJob
from cartography.intel.aws.ec2.util import get_botocore_config
from cartography.models.aws.ec2.auto_scaling_groups import EC2InstanceAutoScalingGroupSchema
from cartography.models.aws.ec2.instances import EC2InstanceSchema
from cartography.models.aws.ec2.keypairs import EC2KeyPairSchema
from cartography.models.aws.ec2.networkinterface_instance import EC2NetworkInterfaceInstanceSchema
Expand Down Expand Up @@ -308,6 +309,7 @@ def cleanup(neo4j_session: neo4j.Session, common_job_parameters: Dict[str, Any])
logger.debug("Running EC2 instance cleanup")
GraphJob.from_node_schema(EC2ReservationSchema(), common_job_parameters).run(neo4j_session)
GraphJob.from_node_schema(EC2InstanceSchema(), common_job_parameters).run(neo4j_session)
GraphJob.from_node_schema(EC2InstanceAutoScalingGroupSchema(), common_job_parameters).run(neo4j_session)


@timeit
Expand Down
188 changes: 188 additions & 0 deletions cartography/models/aws/ec2/auto_scaling_groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
from dataclasses import dataclass

from cartography.models.aws.ec2.subnet_instance import EC2SubnetToAWSAccount
from cartography.models.core.common import PropertyRef
from cartography.models.core.nodes import CartographyNodeProperties
from cartography.models.core.nodes import CartographyNodeSchema
from cartography.models.core.relationships import CartographyRelProperties
from cartography.models.core.relationships import CartographyRelSchema
from cartography.models.core.relationships import LinkDirection
from cartography.models.core.relationships import make_target_node_matcher
from cartography.models.core.relationships import OtherRelationships
from cartography.models.core.relationships import TargetNodeMatcher


@dataclass(frozen=True)
class AutoScalingGroupNodeProperties(CartographyNodeProperties):
id: PropertyRef = PropertyRef('AutoScalingGroupARN')
arn: PropertyRef = PropertyRef('AutoScalingGroupARN')
capacityrebalance: PropertyRef = PropertyRef('CapacityRebalance')
createdtime: PropertyRef = PropertyRef('CreatedTime')
defaultcooldown: PropertyRef = PropertyRef('DefaultCooldown')
desiredcapacity: PropertyRef = PropertyRef('DesiredCapacity')
healthcheckgraceperiod: PropertyRef = PropertyRef('HealthCheckGracePeriod')
healthchecktype: PropertyRef = PropertyRef('HealthCheckType')
launchconfigurationname: PropertyRef = PropertyRef('LaunchConfigurationName')
launchtemplatename: PropertyRef = PropertyRef('LaunchTemplateName')
launchtemplateid: PropertyRef = PropertyRef('LaunchTemplateId')
launchtemplateversion: PropertyRef = PropertyRef('LaunchTemplateVersion')
maxinstancelifetime: PropertyRef = PropertyRef('MaxInstanceLifetime')
maxsize: PropertyRef = PropertyRef('MaxSize')
minsize: PropertyRef = PropertyRef('MinSize')
name: PropertyRef = PropertyRef('AutoScalingGroupName')
newinstancesprotectedfromscalein: PropertyRef = PropertyRef('NewInstancesProtectedFromScaleIn')
region: PropertyRef = PropertyRef('Region', set_in_kwargs=True)
status: PropertyRef = PropertyRef('Status')
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True)


@dataclass(frozen=True)
class EC2SubnetAutoScalingGroupNodeProperties(CartographyNodeProperties):
id: PropertyRef = PropertyRef('VPCZoneIdentifier')
subnetid: PropertyRef = PropertyRef('VPCZoneIdentifier', extra_index=True)
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True)


# EC2 to AutoScalingGroup
@dataclass(frozen=True)
class EC2InstanceToAwsAccountRelProperties(CartographyRelProperties):
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True)


@dataclass(frozen=True)
class EC2InstanceToAWSAccount(CartographyRelSchema):
target_node_label: str = 'AWSAccount'
target_node_matcher: TargetNodeMatcher = make_target_node_matcher(
{'id': PropertyRef('AWS_ID', set_in_kwargs=True)},
)
direction: LinkDirection = LinkDirection.INWARD
rel_label: str = "RESOURCE"
properties: EC2InstanceToAwsAccountRelProperties = EC2InstanceToAwsAccountRelProperties()


@dataclass(frozen=True)
class EC2InstanceToAutoScalingGroupRelProperties(CartographyRelProperties):
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True)


@dataclass(frozen=True)
class EC2InstanceToAutoScalingGroup(CartographyRelSchema):
target_node_label: str = 'AutoScalingGroup'
target_node_matcher: TargetNodeMatcher = make_target_node_matcher(
{'id': PropertyRef('AutoScalingGroupARN')},
)
direction: LinkDirection = LinkDirection.OUTWARD
rel_label: str = "MEMBER_AUTO_SCALE_GROUP"
properties: EC2InstanceToAutoScalingGroupRelProperties = EC2InstanceToAutoScalingGroupRelProperties()


@dataclass(frozen=True)
class EC2InstanceAutoScalingGroupProperties(CartographyNodeProperties):
id: PropertyRef = PropertyRef('InstanceId')
instanceid: PropertyRef = PropertyRef('InstanceId', extra_index=True)
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True)
region: PropertyRef = PropertyRef('Region', set_in_kwargs=True)


@dataclass(frozen=True)
class EC2InstanceAutoScalingGroupSchema(CartographyNodeSchema):
label: str = 'EC2Instance'
properties: EC2InstanceAutoScalingGroupProperties = EC2InstanceAutoScalingGroupProperties()
sub_resource_relationship: EC2InstanceToAWSAccount = EC2InstanceToAWSAccount()
other_relationships: OtherRelationships = OtherRelationships(
[
EC2InstanceToAutoScalingGroup(),
],
)


@dataclass(frozen=True)
class AutoScalingGroupToEC2SubnetRelProperties(CartographyRelProperties):
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True)


@dataclass(frozen=True)
class AutoScalingGroupToAwsAccountRelProperties(CartographyRelProperties):
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True)


@dataclass(frozen=True)
class AutoScalingGroupToLaunchTemplateRelProperties(CartographyRelProperties):
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True)


@dataclass(frozen=True)
class AutoScalingGroupToLaunchConfigurationRelProperties(CartographyRelProperties):
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True)


@dataclass(frozen=True)
class AutoScalingGroupToEC2Subnet(CartographyRelSchema):
target_node_label: str = 'EC2Subnet'
target_node_matcher: TargetNodeMatcher = make_target_node_matcher(
{'id': PropertyRef('VPCZoneIdentifier')},
)
direction: LinkDirection = LinkDirection.OUTWARD
rel_label: str = "VPC_IDENTIFIER"
properties: AutoScalingGroupToEC2SubnetRelProperties = AutoScalingGroupToEC2SubnetRelProperties()


@dataclass(frozen=True)
class AutoScalingGroupToAWSAccount(CartographyRelSchema):
target_node_label: str = 'AWSAccount'
target_node_matcher: TargetNodeMatcher = make_target_node_matcher(
{'id': PropertyRef('AWS_ID', set_in_kwargs=True)},
)
direction: LinkDirection = LinkDirection.INWARD
rel_label: str = "RESOURCE"
properties: AutoScalingGroupToAwsAccountRelProperties = AutoScalingGroupToAwsAccountRelProperties()


@dataclass(frozen=True)
class AutoScalingGroupToLaunchTemplate(CartographyRelSchema):
target_node_label: str = 'LaunchTemplate'
target_node_matcher: TargetNodeMatcher = make_target_node_matcher(
{'id': PropertyRef('LaunchTemplateId')},
)
direction: LinkDirection = LinkDirection.OUTWARD
rel_label: str = "HAS_LAUNCH_TEMPLATE"
properties: AutoScalingGroupToLaunchTemplateRelProperties = AutoScalingGroupToLaunchTemplateRelProperties()


@dataclass(frozen=True)
class AutoScalingGroupToLaunchConfiguration(CartographyRelSchema):
target_node_label: str = 'LaunchConfiguration'
target_node_matcher: TargetNodeMatcher = make_target_node_matcher(
{'name': PropertyRef('LaunchConfigurationName')},
)
direction: LinkDirection = LinkDirection.OUTWARD
rel_label: str = "HAS_LAUNCH_CONFIG"
properties: AutoScalingGroupToLaunchConfigurationRelProperties = (
AutoScalingGroupToLaunchConfigurationRelProperties()
)


@dataclass(frozen=True)
class EC2SubnetAutoScalingGroupSchema(CartographyNodeSchema):
label: str = 'EC2Subnet'
properties: EC2SubnetAutoScalingGroupNodeProperties = EC2SubnetAutoScalingGroupNodeProperties()
sub_resource_relationship: EC2SubnetToAWSAccount = EC2SubnetToAWSAccount()
other_relationships: OtherRelationships = OtherRelationships(
[
AutoScalingGroupToEC2Subnet(),
],
)


@dataclass(frozen=True)
class AutoScalingGroupSchema(CartographyNodeSchema):
label: str = 'AutoScalingGroup'
properties: AutoScalingGroupNodeProperties = AutoScalingGroupNodeProperties()
sub_resource_relationship: AutoScalingGroupToAWSAccount = AutoScalingGroupToAWSAccount()
other_relationships: OtherRelationships = OtherRelationships(
[
AutoScalingGroupToEC2Subnet(),
AutoScalingGroupToLaunchTemplate(),
AutoScalingGroupToLaunchConfiguration(),
],
)
55 changes: 55 additions & 0 deletions cartography/models/aws/ec2/launch_configurations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from dataclasses import dataclass

from cartography.models.core.common import PropertyRef
from cartography.models.core.nodes import CartographyNodeProperties
from cartography.models.core.nodes import CartographyNodeSchema
from cartography.models.core.relationships import CartographyRelProperties
from cartography.models.core.relationships import CartographyRelSchema
from cartography.models.core.relationships import LinkDirection
from cartography.models.core.relationships import make_target_node_matcher
from cartography.models.core.relationships import TargetNodeMatcher


@dataclass(frozen=True)
class LaunchConfigurationNodeProperties(CartographyNodeProperties):
id: PropertyRef = PropertyRef('LaunchConfigurationARN')
arn: PropertyRef = PropertyRef('LaunchConfigurationARN')
created_time = PropertyRef('CreatedTime')
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True)
image_id: PropertyRef = PropertyRef('ImageId')
key_name: PropertyRef = PropertyRef('KeyName')
name: PropertyRef = PropertyRef('LaunchConfigurationName')
security_groups: PropertyRef = PropertyRef('SecurityGroups')
instance_type: PropertyRef = PropertyRef('InstanceType')
kernel_id: PropertyRef = PropertyRef('KernelId')
ramdisk_id: PropertyRef = PropertyRef('RamdiskId')
instance_monitoring_enabled: PropertyRef = PropertyRef('InstanceMonitoringEnabled')
spot_price: PropertyRef = PropertyRef('SpotPrice')
iam_instance_profile: PropertyRef = PropertyRef('IamInstanceProfile')
ebs_optimized: PropertyRef = PropertyRef('EbsOptimized')
associate_public_ip_address: PropertyRef = PropertyRef('AssociatePublicIpAddress')
placement_tenancy: PropertyRef = PropertyRef('PlacementTenancy')
region: PropertyRef = PropertyRef('Region', set_in_kwargs=True)


@dataclass(frozen=True)
class LaunchConfiguratiionToAwsAccountRelProperties(CartographyRelProperties):
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True)


@dataclass(frozen=True)
class LaunchConfigurationToAwsAccount(CartographyRelSchema):
target_node_label: str = 'AWSAccount'
target_node_matcher: TargetNodeMatcher = make_target_node_matcher(
{'id': PropertyRef('AWS_ID', set_in_kwargs=True)},
)
direction: LinkDirection = LinkDirection.INWARD
rel_label: str = "RESOURCE"
properties: LaunchConfiguratiionToAwsAccountRelProperties = LaunchConfiguratiionToAwsAccountRelProperties()


@dataclass(frozen=True)
class LaunchConfigurationSchema(CartographyNodeSchema):
label: str = 'LaunchConfiguration'
properties: LaunchConfigurationNodeProperties = LaunchConfigurationNodeProperties()
sub_resource_relationship: LaunchConfigurationToAwsAccount = LaunchConfigurationToAwsAccount()
Loading
Loading