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

Generate systems from get tagging resources call #939

Merged
merged 5 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
91 changes: 91 additions & 0 deletions src/fidesctl/ctl/connectors/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ def describe_rds_instances(client: Any) -> Dict[str, List[Dict]]: # type: ignor
return describe_instances


@handle_common_aws_errors
def get_tagging_resources(client: Any) -> List[str]: # type: ignore
earmenda marked this conversation as resolved.
Show resolved Hide resolved
"""
Returns a list of resource arns given a resourcegroupstaggingapi boto3 client.
earmenda marked this conversation as resolved.
Show resolved Hide resolved
"""
paginator = client.get_paginator("get_resources")
found_arns = [
resource["ResourceARN"]
for page in paginator.paginate()
for resource in page["ResourceTagMappingList"]
]
return found_arns


def create_redshift_systems(
describe_clusters: Dict[str, List[Dict]], organization_key: str
) -> List[System]:
Expand Down Expand Up @@ -162,3 +176,80 @@ def create_rds_systems(
if not instance.get("DBClusterIdentifier")
]
return rds_cluster_systems + rds_instances_systems


def create_resource_tagging_systems(
resource_arns: List[str],
organization_key: str,
) -> List[System]:
"""
Given a list of resource arns, build a list of systems object which represents
each resource.
"""
resource_generators = {
"dynamodb": create_tagging_dynamodb_system,
"s3": create_tagging_s3_system,
}
systems = []
for arn in resource_arns:
arn_split = arn.split(":")
arn_resource_type = arn_split[2]
resource_generator = resource_generators.get(arn_resource_type)
if resource_generator:
generated_system = resource_generator(arn, organization_key)
if generated_system:
systems.append(generated_system)
return systems


def create_tagging_dynamodb_system(
arn: str,
organization_key: str,
) -> Optional[System]:
"""
Given an AWS arn for a dynamodb resource, returns a System representation
if System is desired.
"""
arn_split = arn.split(":")
resource_name = arn_split[5]

if resource_name.startswith("table/"):
table_name = resource_name[len("table/") :]
system = System(
fides_key=table_name,
name=table_name,
description=f"Fides Generated Description for DynamoDb table: {table_name}",
system_type="dynamodb_table",
organization_fides_key=organization_key,
fidesctl_meta=SystemMetadata(
resource_id=arn,
),
privacy_declarations=[],
)
return system


def create_tagging_s3_system(
arn: str,
organization_key: str,
) -> Optional[System]:
"""
Given an AWS arn for a s3 resource, returns a System representation
if System is desired.
ThomasLaPiana marked this conversation as resolved.
Show resolved Hide resolved
"""
arn_split = arn.split(":")
resource_name = arn_split[5]

bucket_name = resource_name.split("/")[0]
system = System(
fides_key=bucket_name,
name=bucket_name,
description=f"Fides Generated Description for S3 bucket: {bucket_name}",
system_type="s3_bucket",
organization_fides_key=organization_key,
fidesctl_meta=SystemMetadata(
resource_id=arn,
),
privacy_declarations=[],
)
return system
28 changes: 25 additions & 3 deletions src/fidesctl/ctl/core/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def generate_redshift_systems(
organization_key: str, aws_config: Optional[AWSConfig]
) -> List[System]:
"""
Fetches Redshift clusters from AWS and returns the transformed Sytem representations.
Fetches Redshift clusters from AWS and returns the transformed System representations.
"""
import fidesctl.ctl.connectors.aws as aws_connector

Expand All @@ -37,7 +37,7 @@ def generate_rds_systems(
organization_key: str, aws_config: Optional[AWSConfig]
) -> List[System]:
"""
Fetches RDS clusters and instances from AWS and returns the transformed Sytem representations.
Fetches RDS clusters and instances from AWS and returns the transformed System representations.
"""
import fidesctl.ctl.connectors.aws as aws_connector

Expand All @@ -52,6 +52,24 @@ def generate_rds_systems(
return rds_systems


def generate_resource_tagging_systems(
organization_key: str, aws_config: Optional[AWSConfig]
) -> List[System]:
"""
Fetches AWS Resources from the resource tagging api and returns the transformed System representations.
"""
import fidesctl.ctl.connectors.aws as aws_connector

client = aws_connector.get_aws_client(
service="resourcegroupstaggingapi", aws_config=aws_config
)
resource_arns = aws_connector.get_tagging_resources(client=client)
resource_tagging_systems = aws_connector.create_resource_tagging_systems(
resource_arns=resource_arns, organization_key=organization_key
)
return resource_tagging_systems


def get_organization(
organization_key: str,
manifest_organizations: List[Organization],
Expand Down Expand Up @@ -95,7 +113,11 @@ def generate_aws_systems(

Returns a list of systems with any filters applied
"""
generate_system_functions = [generate_redshift_systems, generate_rds_systems]
generate_system_functions = [
generate_redshift_systems,
generate_rds_systems,
generate_resource_tagging_systems,
]

aws_systems = [
found_system
Expand Down
19 changes: 13 additions & 6 deletions tests/ctl/core/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,19 @@ def create_test_server_systems(

@pytest.fixture(scope="function")
def create_external_server_systems(test_config: FidesctlConfig) -> Generator:
systems = _system.generate_redshift_systems(
organization_key="default_organization",
aws_config={},
) + _system.generate_rds_systems(
organization_key="default_organization",
aws_config={},
systems = (
_system.generate_redshift_systems(
organization_key="default_organization",
aws_config={},
)
+ _system.generate_rds_systems(
organization_key="default_organization",
aws_config={},
)
+ _system.generate_resource_tagging_systems(
organization_key="default_organization",
aws_config={},
)
)
delete_server_systems(test_config, systems)
create_server_systems(test_config, systems)
Expand Down