-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor AWS EKS to new model (#1247)
Refactoring EKS module the the new data model since it will make it a lot easier to add in new relationships. Will make a follow-up PR to attach subnets, roles, security groups, VPCs, etc.
- Loading branch information
Showing
7 changed files
with
135 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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 EKSClusterNodeProperties(CartographyNodeProperties): | ||
id: PropertyRef = PropertyRef('arn') | ||
arn: PropertyRef = PropertyRef('arn', extra_index=True) | ||
name: PropertyRef = PropertyRef('name', extra_index=True) | ||
region: PropertyRef = PropertyRef('Region', set_in_kwargs=True) | ||
created_at: PropertyRef = PropertyRef('created_at') | ||
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True) | ||
endpoint: PropertyRef = PropertyRef('endpoint') | ||
endpoint_public_access: PropertyRef = PropertyRef('ClusterEndpointPublic') | ||
rolearn: PropertyRef = PropertyRef('roleArn') | ||
version: PropertyRef = PropertyRef('version') | ||
platform_version: PropertyRef = PropertyRef('platformVersion') | ||
status: PropertyRef = PropertyRef('status') | ||
audit_logging: PropertyRef = PropertyRef('ClusterLogging') | ||
|
||
|
||
@dataclass(frozen=True) | ||
class EKSClusterToAwsAccountRelProperties(CartographyRelProperties): | ||
lastupdated: PropertyRef = PropertyRef('lastupdated', set_in_kwargs=True) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class EKSClusterToAWSAccount(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: EKSClusterToAwsAccountRelProperties = EKSClusterToAwsAccountRelProperties() | ||
|
||
|
||
@dataclass(frozen=True) | ||
class EKSClusterSchema(CartographyNodeSchema): | ||
label: str = 'EKSCluster' | ||
properties: EKSClusterNodeProperties = EKSClusterNodeProperties() | ||
sub_resource_relationship: EKSClusterToAWSAccount = EKSClusterToAWSAccount() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,51 @@ | ||
from unittest.mock import MagicMock | ||
from unittest.mock import patch | ||
|
||
import cartography.intel.aws.eks | ||
import tests.data.aws.eks | ||
from cartography.intel.aws.eks import sync | ||
from tests.data.aws.eks import DESCRIBE_CLUSTERS | ||
from tests.data.aws.eks import LIST_CLUSTERS | ||
from tests.integration.cartography.intel.aws.common import create_test_account | ||
from tests.integration.util import check_nodes | ||
from tests.integration.util import check_rels | ||
|
||
TEST_ACCOUNT_ID = '000000000000' | ||
TEST_REGION = 'eu-west-1' | ||
TEST_UPDATE_TAG = 123456789 | ||
|
||
|
||
def test_load_eks_clusters_nodes(neo4j_session): | ||
data = tests.data.aws.eks.DESCRIBE_CLUSTERS | ||
cartography.intel.aws.eks.load_eks_clusters( | ||
@patch.object(cartography.intel.aws.eks, 'get_eks_clusters', return_value=LIST_CLUSTERS) | ||
@patch.object(cartography.intel.aws.eks, 'get_eks_describe_cluster', side_effect=DESCRIBE_CLUSTERS) | ||
def test_sync_eks_clusters(mock_describe_clusters, mock_get_clusters, neo4j_session): | ||
# Arrange | ||
create_test_account(neo4j_session, TEST_ACCOUNT_ID, TEST_UPDATE_TAG) | ||
boto3_session = MagicMock() | ||
|
||
# Act | ||
sync( | ||
neo4j_session, | ||
data, | ||
TEST_REGION, | ||
boto3_session, | ||
[TEST_REGION], | ||
TEST_ACCOUNT_ID, | ||
TEST_UPDATE_TAG, | ||
{'UPDATE_TAG': TEST_UPDATE_TAG, 'AWS_ID': TEST_ACCOUNT_ID}, | ||
) | ||
|
||
expected_nodes = { | ||
"arn:aws:eks:eu-west-1:111111111111:cluster/cluster_1", | ||
"arn:aws:eks:eu-west-2:222222222222:cluster/cluster_2", | ||
# Assert | ||
assert check_nodes(neo4j_session, 'EKSCluster', ['id', 'platform_version']) == { | ||
('arn:aws:eks:eu-west-1:111111111111:cluster/cluster_1', 'eks.9'), | ||
('arn:aws:eks:eu-west-2:222222222222:cluster/cluster_2', 'eks.9'), | ||
} | ||
|
||
nodes = neo4j_session.run( | ||
""" | ||
MATCH (r:EKSCluster) RETURN r.arn; | ||
""", | ||
) | ||
actual_nodes = {n['r.arn'] for n in nodes} | ||
|
||
assert actual_nodes == expected_nodes | ||
|
||
|
||
def test_load_eks_clusters_relationships(neo4j_session): | ||
# Create Test AWSAccount | ||
neo4j_session.run( | ||
""" | ||
MERGE (aws:AWSAccount{id: $aws_account_id}) | ||
ON CREATE SET aws.firstseen = timestamp() | ||
SET aws.lastupdated = $aws_update_tag | ||
""", | ||
aws_account_id=TEST_ACCOUNT_ID, | ||
aws_update_tag=TEST_UPDATE_TAG, | ||
) | ||
|
||
# Load Test EKS Clusters | ||
data = tests.data.aws.eks.DESCRIBE_CLUSTERS | ||
cartography.intel.aws.eks.load_eks_clusters( | ||
assert check_rels( | ||
neo4j_session, | ||
data, | ||
TEST_REGION, | ||
TEST_ACCOUNT_ID, | ||
TEST_UPDATE_TAG, | ||
) | ||
expected = { | ||
(TEST_ACCOUNT_ID, 'arn:aws:eks:eu-west-1:111111111111:cluster/cluster_1'), | ||
(TEST_ACCOUNT_ID, 'arn:aws:eks:eu-west-2:222222222222:cluster/cluster_2'), | ||
'EKSCluster', | ||
'id', | ||
'AWSAccount', | ||
'id', | ||
'RESOURCE', | ||
rel_direction_right=False, | ||
) == { | ||
('arn:aws:eks:eu-west-1:111111111111:cluster/cluster_1', '000000000000'), | ||
('arn:aws:eks:eu-west-2:222222222222:cluster/cluster_2', '000000000000'), | ||
} | ||
|
||
# Fetch relationships | ||
result = neo4j_session.run( | ||
""" | ||
MATCH (n1:AWSAccount)-[:RESOURCE]->(n2:EKSCluster) RETURN n1.id, n2.arn; | ||
""", | ||
) | ||
actual = { | ||
(r['n1.id'], r['n2.arn']) for r in result | ||
} | ||
|
||
assert actual == expected |