Beispiel #1
0
 def _list_from_aws_result_to_resources(
     cls: Type["AWSResourceSpec"],
     list_from_aws_result: ListFromAWSResult,
     context: Dict[str, str],
 ) -> List[Resource]:
     resources: List[Resource] = []
     for arn, resource_dict in list_from_aws_result.resources.items():
         try:
             links = cls.schema.parse(resource_dict, context)
         except Exception as ex:
             raise SchemaParseException(
                 (
                     f"Error parsing {cls.__name__} : "
                     f"{arn}:\n\nData: {resource_dict}\n\nError: {ex}"
                 )
             )
         resource_account_id = arn.split(":")[4]
         if resource_account_id:
             if resource_account_id != "aws":
                 account_link = ResourceLinkLink(
                     pred="account", obj=f"arn:aws::::account/{resource_account_id}"
                 )
                 links.append(account_link)
                 resource_region_name = arn.split(":")[3]
                 if resource_region_name:
                     region_link = ResourceLinkLink(
                         pred="region",
                         obj=f"arn:aws:::{resource_account_id}:region/{resource_region_name}",
                     )
                     links.append(region_link)
         resource = Resource(resource_id=arn, type_name=cls.get_full_type_name(), links=links)
         resources.append(resource)
     return resources
Beispiel #2
0
 def testToJson(self):
     pred = "test-pred"
     obj = "test-obj"
     link = ResourceLinkLink(pred=pred, obj=obj)
     self.assertDictEqual(link.to_dict(), {
         "type": "resource_link",
         "pred": pred,
         "obj": obj
     })
Beispiel #3
0
 def testToLpg(self):
     pred = "test-pred"
     obj = "test-obj"
     link = ResourceLinkLink(pred=pred, obj=obj)
     parent = {"~id": "123"}
     vertices = []
     edges = []
     link.to_lpg(parent, vertices, edges, "")
     self.assertEqual(1, len(edges))
     self.assertEqual("resource_link", edges[0]["~label"])
     self.assertEqual(parent["~id"], edges[0]["~from"])
     self.assertEqual("test-obj", edges[0]["~to"])
Beispiel #4
0
    def parse(self, data: str, context: Dict[str, Any]) -> List[Link]:
        """Parse this field and return a list of Links.

        Args:
            data: data to parse
            context: contains data from higher level parsing code.

        Returns:
            List of Link objects. At most one link will be returned.
        """
        if isinstance(self._resource_spec_class, str):
            resource_spec_class: Type[
                ResourceSpec] = ResourceSpec.get_by_class_name(
                    self._resource_spec_class)
        else:
            resource_spec_class = self._resource_spec_class
        if not self.alti_key:
            self.alti_key = resource_spec_class.type_name

        short_resource_id = data
        if self.value_is_id:
            resource_id = short_resource_id
        else:
            resource_id = resource_spec_class.generate_id(
                short_resource_id, context)
        return [ResourceLinkLink(pred=self.alti_key, obj=resource_id)]
Beispiel #5
0
 def testToRdf(self):
     pred = "test-pred"
     obj = "test-obj"
     link = ResourceLinkLink(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 ?s ?p ?o where {?s ?p ?o}")
     obj_bnode = node_cache["test-obj"]
     self.assertEqual(1, len(results))
     for result in results:
         s, p, o = result
         self.assertEqual(s, bnode)
         self.assertEqual(str(p), "test:test-pred")
         self.assertEqual(o, obj_bnode)
Beispiel #6
0
 def test_orphaned_ref(self):
     resource_a1 = Resource(resource_id="123",
                            type_name="test:a",
                            links=[SimpleLink(pred="has-foo", obj="goo")])
     resource_b1 = Resource(
         resource_id="abc",
         type_name="test:b",
         links=[ResourceLinkLink(pred="has-a", obj="456")])
     resources = [resource_a1, resource_b1]
     graph_set = GraphSet(
         name="test-name",
         version="1",
         start_time=1234,
         end_time=4567,
         resources=resources,
         errors=["test err 1", "test err 2"],
         stats=MultilevelCounter(),
     )
     with self.assertRaises(GraphSetOrphanedReferencesException):
         graph_set.validate()
Beispiel #7
0
 def setUp(self):
     resource_a1 = Resource(resource_id="123",
                            type_name="test:a",
                            links=[SimpleLink(pred="has-foo", obj="goo")])
     resource_a2 = Resource(resource_id="456", type_name="test:a")
     resource_b1 = Resource(
         resource_id="abc",
         type_name="test:b",
         links=[ResourceLinkLink(pred="has-a", obj="123")])
     resource_b2 = Resource(resource_id="def",
                            type_name="test:b",
                            links=[SimpleLink(pred="name", obj="sue")])
     resources = [resource_a1, resource_a2, resource_b1, resource_b2]
     self.graph_set = GraphSet(
         name="test-name",
         version="1",
         start_time=1234,
         end_time=4567,
         resources=resources,
         errors=["test err 1", "test err 2"],
         stats=MultilevelCounter(),
     )
Beispiel #8
0
    def parse(self, data: Dict[str, Any], context: Dict[str,
                                                        Any]) -> List[Link]:
        """Parse this field and return a list of Links.

        Args:
            data: data to parse
            context: contains data from higher level parsing code.

        Returns:
             List of Link objects. At most one link will be returned.
        """
        if isinstance(self._resource_spec_class, str):
            resource_spec_class: Type[
                ResourceSpec] = ResourceSpec.get_by_class_name(
                    self._resource_spec_class)
        else:
            resource_spec_class = self._resource_spec_class
        if not self.alti_key:
            self.alti_key = resource_spec_class.type_name
        short_resource_id = data.get(self.source_key)
        if not short_resource_id:
            if self.optional:
                return []
            raise ResourceLinkFieldSourceKeyNotFoundException(
                f"Expected key '{self.source_key}' with non-empty/zero value in {data}"
            )
        if not isinstance(short_resource_id, str):
            raise ResourceLinkFieldValueNotAStringException((
                f"ResourceLinkField for {self.source_key} expected a string but got a "
                f"{type(short_resource_id)} : {short_resource_id}"))
        if self.value_is_id:
            resource_id = short_resource_id
        else:
            resource_id = resource_spec_class.generate_id(
                short_resource_id, context)
        return [ResourceLinkLink(pred=self.alti_key, obj=resource_id)]
Beispiel #9
0
    def test_valid_merge(self):
        resource_a1 = Resource(resource_id="123",
                               type_name="test:a",
                               links=[SimpleLink(pred="has-foo", obj="goo")])
        resource_a2 = Resource(resource_id="456", type_name="test:a")
        resource_b1 = Resource(
            resource_id="abc",
            type_name="test:b",
            links=[ResourceLinkLink(pred="has-a", obj="123")])
        resource_b2 = Resource(resource_id="def",
                               type_name="test:b",
                               links=[SimpleLink(pred="name", obj="sue")])
        graph_set_1 = GraphSet(
            name="graph-1",
            version="1",
            start_time=10,
            end_time=20,
            resources=[resource_a1, resource_a2],
            errors=["errora1", "errora2"],
            stats=MultilevelCounter(),
        )
        graph_set_2 = GraphSet(
            name="graph-1",
            version="1",
            start_time=15,
            end_time=25,
            resources=[resource_b1, resource_b2],
            errors=["errorb1", "errorb2"],
            stats=MultilevelCounter(),
        )
        graph_set_1.merge(graph_set_2)

        self.assertEqual(graph_set_1.name, "graph-1")
        self.assertEqual(graph_set_1.version, "1")
        self.assertEqual(graph_set_1.start_time, 10)
        self.assertEqual(graph_set_1.end_time, 25)
        self.assertCountEqual(graph_set_1.errors,
                              ["errora1", "errora2", "errorb1", "errorb2"])
        expected_resource_dicts = [
            {
                "type": "test:a",
                "links": [{
                    "pred": "has-foo",
                    "obj": "goo",
                    "type": "simple"
                }]
            },
            {
                "type": "test:a"
            },
            {
                "type": "test:b",
                "links": [{
                    "pred": "has-a",
                    "obj": "123",
                    "type": "resource_link"
                }]
            },
            {
                "type": "test:b",
                "links": [{
                    "pred": "name",
                    "obj": "sue",
                    "type": "simple"
                }]
            },
        ]
        resource_dicts = [
            resource.to_dict() for resource in graph_set_1.resources
        ]
        self.assertCountEqual(expected_resource_dicts, resource_dicts)