Ejemplo n.º 1
0
    def testToJson(self):
        pred = "test-multi-pred"
        obj = LinkCollection(simple_links=(
            SimpleLink(pred="test-simple-pred-1", obj="test-simple-obj-1"),
            SimpleLink(pred="test-simple-pred-1", obj="test-simple-obj-2"),
            SimpleLink(pred="test-simple-pred-2", obj="test-simple-obj-3"),
        ), )
        link = MultiLink(pred=pred, obj=obj)
        link_dict = link.dict(exclude_unset=True)

        expected_link_dict = {
            "pred": "test-multi-pred",
            "obj": {
                "simple_links": (
                    {
                        "pred": "test-simple-pred-1",
                        "obj": "test-simple-obj-1"
                    },
                    {
                        "pred": "test-simple-pred-1",
                        "obj": "test-simple-obj-2"
                    },
                    {
                        "pred": "test-simple-pred-2",
                        "obj": "test-simple-obj-3"
                    },
                ),
            },
        }
        self.assertDictEqual(expected_link_dict, link_dict)
Ejemplo n.º 2
0
 def testToRdf(self):
     pred = "test-multi-pred"
     obj = LinkCollection(simple_links=(
         SimpleLink(pred="test-simple-pred-1", obj="test-simple-obj-1"),
         SimpleLink(pred="test-simple-pred-2", obj="test-simple-obj-2"),
         SimpleLink(pred="test-simple-pred-3", obj="test-simple-obj-3"),
     ), )
     link = MultiLink(pred=pred, obj=obj)
     bnode = BNode()
     graph = Graph()
     namespace = Namespace("test:")
     node_cache = NodeCache()
     link.to_rdf(subj=bnode,
                 namespace=namespace,
                 graph=graph,
                 node_cache=node_cache)
     results = graph.query(
         "select ?p ?o where {?s a <test:test-multi-pred> ; ?p ?o} order by ?p ?o"
     )
     result_tuples = []
     for result in results:
         self.assertEqual(2, len(result))
         result_tuples.append((str(result[0]), str(result[1])))
     expected_result_tuples = [
         ("http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
          "test:test-multi-pred"),
         ("test:test-simple-pred-1", "test-simple-obj-1"),
         ("test:test-simple-pred-2", "test-simple-obj-2"),
         ("test:test-simple-pred-3", "test-simple-obj-3"),
     ]
     self.assertEqual(result_tuples, expected_result_tuples)
    def test_valid_dicts_input_with_alti_key(self):
        input_str = '{"People": [{"Name": "Bob", "Age": 49}, {"Name": "Sue", "Age": 42}]}'
        field = ListField(
            "People", EmbeddedDictField(ScalarField("Name"), ScalarField("Age")), alti_key="person"
        )

        input_data = json.loads(input_str)
        link_collection = field.parse(data=input_data, context={})

        expected_link_collection = LinkCollection(
            multi_links=(
                MultiLink(
                    pred="person",
                    obj=LinkCollection(
                        simple_links=(
                            SimpleLink(pred="name", obj="Bob"),
                            SimpleLink(pred="age", obj=49),
                        ),
                    ),
                ),
                MultiLink(
                    pred="person",
                    obj=LinkCollection(
                        simple_links=(
                            SimpleLink(pred="name", obj="Sue"),
                            SimpleLink(pred="age", obj=42),
                        ),
                    ),
                ),
            ),
        )
        self.assertEqual(link_collection, expected_link_collection)
    def parse(self, data: Dict[str, Any],
              context: Dict[str, Any]) -> LinkCollection:
        """Parse this field and return a LinkCollection.

        Args:
            data: dictionary of data to parse
            context: context dict containing data from higher level parsing code.

        Returns:
            LinkCollection

        Raises:
            DictFieldSourceKeyNotFoundException if self.source_key is not in data.
            DictFieldValueNotADictException if the data does not appear to represent a dict.
        """
        parent_alti_key = self.get_parent_alti_key(data, context)
        if not isinstance(data, dict):
            raise Exception(f"{type(data)} {data} was expected to be a dict.")
        updated_context = deepcopy(context)
        updated_context.update({"parent_alti_key": parent_alti_key})
        multi_link_object_link_collection = LinkCollection()
        for field in self.fields:
            multi_link_object_link_collection += field.parse(data, context)
        return LinkCollection(multi_links=[
            MultiLink(pred=parent_alti_key,
                      obj=multi_link_object_link_collection)
        ])
    def test_valid_dicts_input(self):
        input_str = (
            '{"Biota": {"People": [{"Name": "Bob", "Age": 49}, {"Name": "Sue", "Age": 42}]}}'
        )
        field = DictField(
            "Biota",
            AnonymousListField(
                "People", EmbeddedDictField(ScalarField("Name"), ScalarField("Age"))
            ),
        )

        input_data = json.loads(input_str)
        link_collection = field.parse(data=input_data, context={})

        expected_link_collection = LinkCollection(
            multi_links=(
                MultiLink(
                    pred="biota",
                    obj=LinkCollection(
                        multi_links=(
                            MultiLink(
                                pred="biota",
                                obj=LinkCollection(
                                    simple_links=(
                                        SimpleLink(pred="name", obj="Bob"),
                                        SimpleLink(pred="age", obj=49),
                                    ),
                                ),
                            ),
                            MultiLink(
                                pred="biota",
                                obj=LinkCollection(
                                    simple_links=(
                                        SimpleLink(pred="name", obj="Sue"),
                                        SimpleLink(pred="age", obj=42),
                                    ),
                                ),
                            ),
                        ),
                    ),
                ),
            )
        )
        self.assertEqual(link_collection, expected_link_collection)
    def test_optional(self):
        input_str = '{"Biota": {"Plants": ["tree", "fern"]}}'
        field = DictField(
            "Biota", AnonymousListField("Animals", EmbeddedScalarField(), optional=True)
        )

        input_data = json.loads(input_str)
        link_collection = field.parse(data=input_data, context={})

        expected_link_collection = LinkCollection(
            multi_links=(MultiLink(pred="biota", obj=LinkCollection()),),
        )
        self.assertEqual(link_collection, expected_link_collection)
    def test_allow_scalar(self):
        input_str = '{"Biota": {"Plants": "tree"}}'
        field = DictField(
            "Biota", AnonymousListField("Plants", EmbeddedScalarField(), allow_scalar=True)
        )
        input_data = json.loads(input_str)
        link_collection = field.parse(data=input_data, context={})

        expected_link_collection = LinkCollection(
            multi_links=(
                MultiLink(
                    pred="biota",
                    obj=LinkCollection(simple_links=(SimpleLink(pred="biota", obj="tree"),),),
                ),
            )
        )
        self.assertEqual(expected_link_collection, link_collection)
    def test_valid_strings_input(self):
        input_str = '{"Biota": {"Animals": ["cow", "pig", "human"], "Plants": ["tree", "fern"]}}'
        field = DictField("Biota", AnonymousListField("Animals", EmbeddedScalarField()))

        input_data = json.loads(input_str)
        link_collection = field.parse(data=input_data, context={})

        expected_link_collection = LinkCollection(
            multi_links=(
                MultiLink(
                    pred="biota",
                    obj=LinkCollection(
                        simple_links=(
                            SimpleLink(pred="biota", obj="cow"),
                            SimpleLink(pred="biota", obj="pig"),
                            SimpleLink(pred="biota", obj="human"),
                        )
                    ),
                ),
            )
        )
        self.assertEqual(link_collection, expected_link_collection)
    def parse(self, data: Dict[str, Any],
              context: Dict[str, Any]) -> LinkCollection:
        """Parse this field and return a LinkCollection.

       Args:
           data: dictionary of data to parse
           context: context dict containing data from higher level parsing code.

        Returns:
            LinkCollection

        Raises:
            DictFieldSourceKeyNotFoundException if self.source_key is not in data.
            DictFieldValueNotADictException if the data does not appear to represent a dict.
        """
        if self.source_key not in data:
            if self.optional:
                return LinkCollection()
            raise DictFieldSourceKeyNotFoundException(
                f"Expected key '{self.source_key}' in data, present keys: {', '.join(data.keys())}"
            )
        field_data = data.get(self.source_key, {})
        if not isinstance(field_data, dict):
            raise DictFieldValueNotADictException(
                (f"Key '{self.source_key}' value was expected to "
                 f"contain a dict, actual: {field_data} "
                 f"({type(field_data)})"))
        updated_context = deepcopy(context)
        updated_context.update({"parent_alti_key": self.alti_key})
        multi_link_object_link_collection = LinkCollection()
        for field in self.fields:
            multi_link_object_link_collection += field.parse(
                field_data, updated_context)
        return LinkCollection(multi_links=[
            MultiLink(pred=self.alti_key,
                      obj=multi_link_object_link_collection)
        ])
Ejemplo n.º 10
0
    def testToLpg(self):
        pred = "test-multi-pred"
        obj = LinkCollection(simple_links=(
            SimpleLink(pred="test-simple-pred-1", obj="test-simple-obj-1"),
            SimpleLink(pred="test-simple-pred-2", obj="test-simple-obj-2"),
            SimpleLink(pred="test-simple-pred-3", obj="test-simple-obj-3"),
        ), )
        link = MultiLink(pred=pred, obj=obj)

        parent = {"~id": "parent_id"}
        vertices = []
        edges = []
        link.to_lpg(parent, vertices, edges, "")

        expected_vertex = {
            "~label": "test-multi-pred",
            "test-simple-pred-1": "test-simple-obj-1",
            "test-simple-pred-2": "test-simple-obj-2",
            "test-simple-pred-3": "test-simple-obj-3",
        }

        self.assertEqual(len(vertices), 1)
        self.assertIsInstance(vertices[0]["~id"], uuid.UUID)
        vertex_id = vertices[0]["~id"]
        del vertices[0]["~id"]
        self.assertDictEqual(expected_vertex, vertices[0])

        expected_edge = {
            "~label": "test-multi-pred",
            "~from": "parent_id",
            "~to": vertex_id,
        }
        self.assertEqual(len(edges), 1)
        self.assertIsInstance(edges[0]["~id"], uuid.UUID)
        del edges[0]["~id"]
        self.assertDictEqual(expected_edge, edges[0])

        vertices = []
        edges = []
        link.to_lpg(parent, vertices, edges, "test_prefix")
        self.assertEqual(len(edges), 1)
        self.assertEqual("test_prefix", edges[0]["~label"])
Ejemplo n.º 11
0
    def test(self):
        with tempfile.TemporaryDirectory() as temp_dir:
            resource_region_name = "us-east-1"
            # get moto"s enabled regions
            ec2_client = boto3.client("ec2", region_name=resource_region_name)
            all_regions = ec2_client.describe_regions(
                Filters=[{
                    "Name": "opt-in-status",
                    "Values": ["opt-in-not-required", "opted-in"]
                }])["Regions"]
            account_id = get_account_id()
            all_region_names = tuple(region["RegionName"]
                                     for region in all_regions)
            enabled_region_names = tuple(
                region["RegionName"] for region in all_regions
                if region["OptInStatus"] != "not-opted-in")
            delete_vpcs(all_region_names)
            # add a diverse set of resources which are supported by moto
            ## dynamodb
            # TODO moto is not returning TableId in list/describe
            #            dynamodb_table_1_arn = create_dynamodb_table(
            #                name="test_table_1",
            #                attr_name="test_hash_key_attr_1",
            #                attr_type="S",
            #                key_type="HASH",
            #                region_name=region_name,
            #            )
            ## s3
            bucket_1_name = "test_bucket"
            bucket_1_arn, bucket_1_creation_date = create_bucket(
                name=bucket_1_name,
                account_id=account_id,
                region_name=resource_region_name)
            ## ec2
            vpc_1_cidr = "10.0.0.0/16"
            vpc_1_id = create_vpc(cidr_block=vpc_1_cidr,
                                  region_name=resource_region_name)
            vpc_1_arn = VPCResourceSpec.generate_arn(
                resource_id=vpc_1_id,
                account_id=account_id,
                region=resource_region_name)
            subnet_1_cidr = "10.0.0.0/24"
            subnet_1_cidr_network = ipaddress.IPv4Network(subnet_1_cidr,
                                                          strict=False)
            subnet_1_first_ip, subnet_1_last_ip = (
                int(subnet_1_cidr_network[0]),
                int(subnet_1_cidr_network[-1]),
            )
            subnet_1_id = create_subnet(cidr_block=subnet_1_cidr,
                                        vpc_id=vpc_1_id,
                                        region_name=resource_region_name)
            subnet_1_arn = SubnetResourceSpec.generate_arn(
                resource_id=subnet_1_id,
                account_id=account_id,
                region=resource_region_name)
            fixed_bucket_1_arn = f"arn:aws:s3:::{bucket_1_name}"
            flow_log_1_id, flow_log_1_creation_time = create_flow_log(
                vpc_id=vpc_1_id,
                dest_bucket_arn=fixed_bucket_1_arn,
                region_name=resource_region_name,
            )
            flow_log_1_arn = FlowLogResourceSpec.generate_arn(
                resource_id=flow_log_1_id,
                account_id=account_id,
                region=resource_region_name)
            ebs_volume_1_size = 128
            ebs_volume_1_az = f"{resource_region_name}a"
            ebs_volume_1_arn, ebs_volume_1_create_time = create_volume(
                size=ebs_volume_1_size,
                az=ebs_volume_1_az,
                region_name=resource_region_name)
            ## iam
            policy_1_name = "test_policy_1"
            policy_1_arn, policy_1_id = create_iam_policy(
                name=policy_1_name,
                policy_doc={
                    "Version":
                    "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Action": "logs:CreateLogGroup",
                            "Resource": "*"
                        },
                    ],
                },
            )
            role_1_name = "test_role_1"
            role_1_assume_role_policy_doc = {
                "Version":
                "2012-10-17",
                "Statement": [{
                    "Action": "sts:AssumeRole",
                    "Effect": "Allow",
                    "Principal": {
                        "Service": "lambda.amazonaws.com"
                    },
                    "Sid": "",
                }],
            }
            role_1_description = "Test Role 1"
            role_1_max_session_duration = 3600
            role_1_arn = create_iam_role(
                name=role_1_name,
                assume_role_policy_doc=role_1_assume_role_policy_doc,
                description=role_1_description,
                max_session_duration=role_1_max_session_duration,
            )
            ## lambda
            lambda_function_1_name = "test_lambda_function_1"
            lambda_function_1_runtime = "python3.7"
            lambda_function_1_handler = "lambda_function.lambda_handler"
            lambda_function_1_description = "Test Lambda Function 1"
            lambda_function_1_timeout = 30
            lambda_function_1_memory_size = 256
            lambda_function_1_arn = create_lambda_function(
                name=lambda_function_1_name,
                runtime=lambda_function_1_runtime,
                role_name=role_1_arn,
                handler=lambda_function_1_handler,
                description=lambda_function_1_description,
                timeout=lambda_function_1_timeout,
                memory_size=lambda_function_1_memory_size,
                publish=False,
                region_name=resource_region_name,
            )
            # scan
            test_scan_id = "test_scan_id"
            aws_config = AWSConfig(
                artifact_path=temp_dir,
                pruner_max_age_min=4320,
                graph_name="alti",
                concurrency=ConcurrencyConfig(max_account_scan_threads=1,
                                              max_svc_scan_threads=1,
                                              max_account_scan_tries=2),
                scan=ScanConfig(
                    accounts=(),
                    regions=(),
                    scan_sub_accounts=False,
                    preferred_account_scan_regions=(
                        "us-west-1",
                        "us-west-2",
                        "us-east-1",
                        "us-east-2",
                    ),
                ),
                accessor=Accessor(
                    credentials_cache=AWSCredentialsCache(cache={}),
                    multi_hop_accessors=[],
                    cache_creds=True,
                ),
                write_master_json=True,
            )
            resource_spec_classes = (
                # DynamoDbTableResourceSpec, TODO moto
                EBSVolumeResourceSpec,
                FlowLogResourceSpec,
                IAMPolicyResourceSpec,
                IAMRoleResourceSpec,
                LambdaFunctionResourceSpec,
                S3BucketResourceSpec,
                SubnetResourceSpec,
                VPCResourceSpec,
            )
            muxer = LocalAWSScanMuxer(
                scan_id=test_scan_id,
                config=aws_config,
                resource_spec_classes=resource_spec_classes,
            )
            with unittest.mock.patch(
                    "altimeter.aws.scan.account_scanner.get_all_enabled_regions"
            ) as mock_get_all_enabled_regions:
                mock_get_all_enabled_regions.return_value = enabled_region_names
                aws2n_result = aws2n(
                    scan_id=test_scan_id,
                    config=aws_config,
                    muxer=muxer,
                    load_neptune=False,
                )
                graph_set = GraphSet.from_json_file(
                    Path(aws2n_result.json_path))
                self.assertEqual(len(graph_set.errors), 0)
                self.assertEqual(graph_set.name, "alti")
                self.assertEqual(graph_set.version, "2")
                # now check each resource type
                self.maxDiff = None
                ## Accounts
                expected_account_resources = [
                    Resource(
                        resource_id=f"arn:aws::::account/{account_id}",
                        type="aws:account",
                        link_collection=LinkCollection(
                            simple_links=(SimpleLink(pred="account_id",
                                                     obj=account_id), ), ),
                    )
                ]
                account_resources = [
                    resource for resource in graph_set.resources
                    if resource.type == "aws:account"
                ]
                self.assertCountEqual(account_resources,
                                      expected_account_resources)
                ## Regions
                expected_region_resources = [
                    Resource(
                        resource_id=
                        f"arn:aws:::{account_id}:region/{region['RegionName']}",
                        type="aws:region",
                        link_collection=LinkCollection(
                            simple_links=(
                                SimpleLink(pred="name",
                                           obj=region["RegionName"]),
                                SimpleLink(pred="opt_in_status",
                                           obj=region["OptInStatus"]),
                            ),
                            resource_links=(ResourceLink(
                                pred="account",
                                obj=f"arn:aws::::account/{account_id}"), ),
                        ),
                    ) for region in all_regions
                ]
                region_resources = [
                    resource for resource in graph_set.resources
                    if resource.type == "aws:region"
                ]
                self.assertCountEqual(region_resources,
                                      expected_region_resources)
                ## IAM Policies
                expected_iam_policy_resources = [
                    Resource(
                        resource_id=policy_1_arn,
                        type="aws:iam:policy",
                        link_collection=LinkCollection(
                            simple_links=(
                                SimpleLink(pred="name", obj=policy_1_name),
                                SimpleLink(pred="policy_id", obj=policy_1_id),
                                SimpleLink(pred="default_version_id",
                                           obj="v1"),
                                SimpleLink(
                                    pred="default_version_policy_document_text",
                                    obj=
                                    '{"Statement": [{"Action": "logs:CreateLogGroup", "Effect": "Allow", "Resource": "*"}], "Version": "2012-10-17"}',
                                ),
                            ),
                            resource_links=(ResourceLink(
                                pred="account",
                                obj=f"arn:aws::::account/{account_id}"), ),
                        ),
                    )
                ]
                iam_policy_resources = [
                    resource for resource in graph_set.resources
                    if resource.type == "aws:iam:policy"
                ]
                self.assertCountEqual(iam_policy_resources,
                                      expected_iam_policy_resources)
                ## IAM Roles
                expected_iam_role_resources = [
                    Resource(
                        resource_id=role_1_arn,
                        type="aws:iam:role",
                        link_collection=LinkCollection(
                            simple_links=(
                                SimpleLink(pred="name", obj=role_1_name),
                                SimpleLink(pred="max_session_duration",
                                           obj=role_1_max_session_duration),
                                SimpleLink(pred="description",
                                           obj=role_1_description),
                                SimpleLink(
                                    pred="assume_role_policy_document_text",
                                    obj=policy_doc_dict_to_sorted_str(
                                        role_1_assume_role_policy_doc),
                                ),
                            ),
                            multi_links=(MultiLink(
                                pred="assume_role_policy_document",
                                obj=LinkCollection(
                                    simple_links=(SimpleLink(
                                        pred="version", obj="2012-10-17"), ),
                                    multi_links=(MultiLink(
                                        pred="statement",
                                        obj=LinkCollection(
                                            simple_links=(
                                                SimpleLink(pred="effect",
                                                           obj="Allow"),
                                                SimpleLink(
                                                    pred="action",
                                                    obj="sts:AssumeRole"),
                                            ),
                                            multi_links=(MultiLink(
                                                pred="principal",
                                                obj=LinkCollection(
                                                    simple_links=(SimpleLink(
                                                        pred="service",
                                                        obj=
                                                        "lambda.amazonaws.com",
                                                    ), )),
                                            ), ),
                                        ),
                                    ), ),
                                ),
                            ), ),
                            resource_links=(ResourceLink(
                                pred="account",
                                obj="arn:aws::::account/123456789012"), ),
                        ),
                    )
                ]
                iam_role_resources = [
                    resource for resource in graph_set.resources
                    if resource.type == "aws:iam:role"
                ]
                self.assertCountEqual(iam_role_resources,
                                      expected_iam_role_resources)

                ## Lambda functions
                expected_lambda_function_resources = [
                    Resource(
                        resource_id=lambda_function_1_arn,
                        type="aws:lambda:function",
                        link_collection=LinkCollection(
                            simple_links=(
                                SimpleLink(pred="function_name",
                                           obj=lambda_function_1_name),
                                SimpleLink(pred="runtime",
                                           obj=lambda_function_1_runtime),
                            ),
                            resource_links=(
                                ResourceLink(
                                    pred="account",
                                    obj=f"arn:aws::::account/{account_id}"),
                                ResourceLink(
                                    pred="region",
                                    obj=
                                    f"arn:aws:::{account_id}:region/{resource_region_name}",
                                ),
                            ),
                            transient_resource_links=(ResourceLink(
                                pred="role",
                                obj="arn:aws:iam::123456789012:role/test_role_1"
                            ), ),
                        ),
                    ),
                ]
                lambda_function_resources = [
                    resource for resource in graph_set.resources
                    if resource.type == "aws:lambda:function"
                ]
                self.assertCountEqual(lambda_function_resources,
                                      expected_lambda_function_resources)
                ## EC2 VPCs
                expected_ec2_vpc_resources = [
                    Resource(
                        resource_id=vpc_1_arn,
                        type="aws:ec2:vpc",
                        link_collection=LinkCollection(
                            simple_links=(
                                SimpleLink(pred="is_default", obj=True),
                                SimpleLink(pred="cidr_block", obj=vpc_1_cidr),
                                SimpleLink(pred="state", obj="available"),
                            ),
                            resource_links=(
                                ResourceLink(
                                    pred="account",
                                    obj=f"arn:aws::::account/{account_id}"),
                                ResourceLink(
                                    pred="region",
                                    obj=
                                    f"arn:aws:::{account_id}:region/{resource_region_name}",
                                ),
                            ),
                        ),
                    )
                ]
                ec2_vpc_resources = [
                    resource for resource in graph_set.resources
                    if resource.type == "aws:ec2:vpc"
                ]
                self.assertCountEqual(ec2_vpc_resources,
                                      expected_ec2_vpc_resources)
                ## EC2 VPC Flow Logs
                expected_ec2_vpc_flow_log_resources = [
                    Resource(
                        resource_id=flow_log_1_arn,
                        type="aws:ec2:flow-log",
                        link_collection=LinkCollection(
                            simple_links=(
                                SimpleLink(
                                    pred="creation_time",
                                    obj=flow_log_1_creation_time.replace(
                                        tzinfo=datetime.timezone.utc).
                                    isoformat(),
                                ),
                                SimpleLink(pred="deliver_logs_status",
                                           obj="SUCCESS"),
                                SimpleLink(pred="flow_log_status",
                                           obj="ACTIVE"),
                                SimpleLink(pred="traffic_type", obj="ALL"),
                                SimpleLink(pred="log_destination_type",
                                           obj="s3"),
                                SimpleLink(pred="log_destination",
                                           obj=fixed_bucket_1_arn),
                                SimpleLink(
                                    pred="log_format",
                                    obj=
                                    "${version} ${account-id} ${interface-id} ${srcaddr} ${dstaddr} ${srcport} ${dstport} ${protocol} ${packets} ${bytes} ${start} ${end} ${action} ${log-status}",
                                ),
                            ),
                            resource_links=(
                                ResourceLink(
                                    pred="account",
                                    obj=f"arn:aws::::account/{account_id}"),
                                ResourceLink(
                                    pred="region",
                                    obj=
                                    f"arn:aws:::{account_id}:region/{resource_region_name}",
                                ),
                            ),
                            transient_resource_links=(TransientResourceLink(
                                pred="vpc",
                                obj=vpc_1_arn,
                            ), ),
                        ),
                    )
                ]
                ec2_vpc_flow_log_resources = [
                    resource for resource in graph_set.resources
                    if resource.type == "aws:ec2:flow-log"
                ]
                self.assertCountEqual(ec2_vpc_flow_log_resources,
                                      expected_ec2_vpc_flow_log_resources)
                ## EC2 Subnets
                expected_ec2_subnet_resources = [
                    Resource(
                        resource_id=subnet_1_arn,
                        type="aws:ec2:subnet",
                        link_collection=LinkCollection(
                            simple_links=(
                                SimpleLink(pred="cidr_block",
                                           obj=subnet_1_cidr),
                                SimpleLink(pred="first_ip",
                                           obj=subnet_1_first_ip),
                                SimpleLink(pred="last_ip",
                                           obj=subnet_1_last_ip),
                                SimpleLink(pred="state", obj="available"),
                            ),
                            resource_links=(
                                ResourceLink(pred="vpc", obj=vpc_1_arn),
                                ResourceLink(
                                    pred="account",
                                    obj=f"arn:aws::::account/{account_id}"),
                                ResourceLink(
                                    pred="region",
                                    obj=
                                    f"arn:aws:::{account_id}:region/{resource_region_name}",
                                ),
                            ),
                        ),
                    )
                ]
                ec2_subnet_resources = [
                    resource for resource in graph_set.resources
                    if resource.type == "aws:ec2:subnet"
                ]
                self.assertCountEqual(ec2_subnet_resources,
                                      expected_ec2_subnet_resources)
                ## EC2 EBS Volumes
                expected_ec2_ebs_volume_resources = [
                    Resource(
                        resource_id=ebs_volume_1_arn,
                        type="aws:ec2:volume",
                        link_collection=LinkCollection(
                            simple_links=(
                                SimpleLink(pred="availability_zone",
                                           obj=ebs_volume_1_az),
                                SimpleLink(
                                    pred="create_time",
                                    obj=ebs_volume_1_create_time.replace(
                                        tzinfo=datetime.timezone.utc).
                                    isoformat(),
                                ),
                                SimpleLink(pred="size", obj=ebs_volume_1_size),
                                SimpleLink(pred="state", obj="available"),
                                SimpleLink(pred="volume_type", obj="gp2"),
                                SimpleLink(pred="encrypted", obj=False),
                            ),
                            resource_links=(
                                ResourceLink(
                                    pred="account",
                                    obj=f"arn:aws::::account/{account_id}"),
                                ResourceLink(
                                    pred="region",
                                    obj=
                                    f"arn:aws:::{account_id}:region/{resource_region_name}",
                                ),
                            ),
                        ),
                    )
                ]
                ec2_ebs_volume_resources = [
                    resource for resource in graph_set.resources
                    if resource.type == "aws:ec2:volume"
                ]
                self.assertCountEqual(ec2_ebs_volume_resources,
                                      expected_ec2_ebs_volume_resources)
                ## S3 Buckets
                expected_s3_bucket_resources = [
                    Resource(
                        resource_id=bucket_1_arn,
                        type="aws:s3:bucket",
                        link_collection=LinkCollection(
                            simple_links=(
                                SimpleLink(pred="name", obj=bucket_1_name),
                                SimpleLink(
                                    pred="creation_date",
                                    obj=bucket_1_creation_date.replace(
                                        tzinfo=datetime.timezone.utc).
                                    isoformat(),
                                ),
                            ),
                            resource_links=(
                                ResourceLink(
                                    pred="account",
                                    obj=f"arn:aws::::account/{account_id}"),
                                ResourceLink(
                                    pred="region",
                                    obj=
                                    f"arn:aws:::{account_id}:region/{resource_region_name}",
                                ),
                            ),
                        ),
                    )
                ]
                s3_bucket_resources = [
                    resource for resource in graph_set.resources
                    if resource.type == "aws:s3:bucket"
                ]
                self.assertCountEqual(s3_bucket_resources,
                                      expected_s3_bucket_resources)

                expected_num_graph_set_resources = (
                    0 + len(expected_account_resources) +
                    len(expected_region_resources) +
                    len(expected_iam_policy_resources) +
                    len(expected_iam_role_resources) +
                    len(expected_lambda_function_resources) +
                    len(expected_ec2_ebs_volume_resources) +
                    len(expected_ec2_subnet_resources) +
                    len(expected_ec2_vpc_resources) +
                    len(expected_ec2_vpc_flow_log_resources) +
                    len(expected_s3_bucket_resources))
                self.assertEqual(len(graph_set.resources),
                                 expected_num_graph_set_resources)
Ejemplo n.º 12
0
    def test_schema_parse(self):
        resource_arn = "arn:aws:ec2:us-east-2:111122223333:route-table/rtb-099c7b032f2bbddda"
        aws_resource_dict = {
            "Associations": [
                {
                    "Main": False,
                    "RouteTableAssociationId": "rtbassoc-069d59127bf10a728",
                    "RouteTableId": "rtb-099c7b032f2bbddda",
                    "SubnetId": "subnet-00f9fe55b9d7ca4fb",
                },
                {
                    "Main": False,
                    "RouteTableAssociationId": "rtbassoc-07bfd170c4ece33c8",
                    "RouteTableId": "rtb-099c7b032f2bbddda",
                    "SubnetId": "subnet-0b98092b454c882cf",
                },
            ],
            "PropagatingVgws": [],
            "RouteTableId":
            "rtb-099c7b032f2bbddda",
            "Routes": [
                {
                    "DestinationCidrBlock": "172.31.0.0/16",
                    "GatewayId": "local",
                    "Origin": "CreateRouteTable",
                    "State": "active",
                },
                {
                    "DestinationCidrBlock": "0.0.0.0/0",
                    "GatewayId": "igw-092e5ec1685fd0c0b",
                    "Origin": "CreateRoute",
                    "State": "active",
                },
                {
                    "DestinationPrefixListId": "pl-68a54001",
                    "GatewayId": "vpce-0678bce2b63b8ad0f",
                    "Origin": "CreateRoute",
                    "State": "active",
                },
            ],
            "VpcId":
            "vpc-03c33051f57d21ff0",
            "OwnerId":
            "210554966933",
        }

        link_collection = EC2RouteTableResourceSpec.schema.parse(
            data=aws_resource_dict,
            context={
                "account_id": "111122223333",
                "region": "us-west-2"
            })
        resource = Resource(
            resource_id=resource_arn,
            type=EC2RouteTableResourceSpec.type_name,
            link_collection=link_collection,
        )

        expected_alti_resource = Resource(
            resource_id=
            "arn:aws:ec2:us-east-2:111122223333:route-table/rtb-099c7b032f2bbddda",
            type="route-table",
            link_collection=LinkCollection(
                simple_links=(
                    SimpleLink(pred="route_table_id",
                               obj="rtb-099c7b032f2bbddda"),
                    SimpleLink(pred="owner_id", obj="210554966933"),
                ),
                multi_links=(
                    MultiLink(
                        pred="route",
                        obj=LinkCollection(simple_links=(
                            SimpleLink(pred="destination_cidr_block",
                                       obj="172.31.0.0/16"),
                            SimpleLink(pred="gateway_id", obj="local"),
                            SimpleLink(pred="origin", obj="CreateRouteTable"),
                            SimpleLink(pred="state", obj="active"),
                        ), ),
                    ),
                    MultiLink(
                        pred="route",
                        obj=LinkCollection(simple_links=(
                            SimpleLink(pred="destination_cidr_block",
                                       obj="0.0.0.0/0"),
                            SimpleLink(pred="gateway_id",
                                       obj="igw-092e5ec1685fd0c0b"),
                            SimpleLink(pred="origin", obj="CreateRoute"),
                            SimpleLink(pred="state", obj="active"),
                        ), ),
                    ),
                    MultiLink(
                        pred="route",
                        obj=LinkCollection(simple_links=(
                            SimpleLink(pred="destination_prefix_list_id",
                                       obj="pl-68a54001"),
                            SimpleLink(pred="gateway_id",
                                       obj="vpce-0678bce2b63b8ad0f"),
                            SimpleLink(pred="origin", obj="CreateRoute"),
                            SimpleLink(pred="state", obj="active"),
                        ), ),
                    ),
                    MultiLink(
                        pred="association",
                        obj=LinkCollection(simple_links=(
                            SimpleLink(pred="main", obj=False),
                            SimpleLink(
                                pred="route_table_association_id",
                                obj="rtbassoc-069d59127bf10a728",
                            ),
                            SimpleLink(pred="route_table_id",
                                       obj="rtb-099c7b032f2bbddda"),
                            SimpleLink(pred="subnet_id",
                                       obj="subnet-00f9fe55b9d7ca4fb"),
                        ), ),
                    ),
                    MultiLink(
                        pred="association",
                        obj=LinkCollection(simple_links=(
                            SimpleLink(pred="main", obj=False),
                            SimpleLink(
                                pred="route_table_association_id",
                                obj="rtbassoc-07bfd170c4ece33c8",
                            ),
                            SimpleLink(pred="route_table_id",
                                       obj="rtb-099c7b032f2bbddda"),
                            SimpleLink(pred="subnet_id",
                                       obj="subnet-0b98092b454c882cf"),
                        ), ),
                    ),
                ),
                resource_links=(ResourceLink(
                    pred="vpc",
                    obj=
                    "arn:aws:ec2:us-west-2:111122223333:vpc/vpc-03c33051f57d21ff0",
                ), ),
            ),
        )

        self.assertEqual(resource, expected_alti_resource)