Пример #1
0
    def test_two_tags_filter_resource(self):
        resources = filter_resources(
            [
                Resource(
                    digest=ResourceDigest(id="1", type="type"),
                    name="name",
                    tags=[ResourceTag(key="key", value="value1")],
                ),
                Resource(
                    digest=ResourceDigest(id="2", type="type"),
                    name="name",
                    tags=[ResourceTag(key="key", value="value2")],
                ),
                Resource(
                    digest=ResourceDigest(id="3", type="type"),
                    name="name",
                    tags=[ResourceTag(key="key", value="wrong")],
                ),
            ],
            [
                ResourceTag(key="key", value="value1"),
                ResourceTag(key="key", value="value2"),
            ],
        )

        assert_that(resources).is_length(2).extracting(0).contains(
            ResourceDigest(id="1", type="type"),
            ResourceDigest(id="2", type="type"))
Пример #2
0
    def test_no_filters_resource(self):
        resources = filter_resources(
            [
                Resource(
                    digest=ResourceDigest(id="1", type="type"),
                    name="name",
                    tags=[ResourceTag(key="key", value="value")],
                )
            ],
            [],
        )

        assert_that(resources).is_length(1)
        assert_that(resources[0].digest).is_equal_to(
            ResourceDigest(id="1", type="type"))
Пример #3
0
    def test_one_type_filter_resource(self):
        resources = filter_resources(
            [
                Resource(
                    digest=ResourceDigest(id="1", type="type1"),
                    name="name",
                    tags=[ResourceTag(key="key", value="value")],
                ),
                Resource(
                    digest=ResourceDigest(id="2", type="type2"),
                    name="name",
                    tags=[ResourceTag(key="key", value="wrong")],
                ),
            ],
            [ResourceType(type="type1")],
        )

        assert_that(resources).is_length(1)
        assert_that(resources[0].digest).is_equal_to(
            ResourceDigest(id="1", type="type1"))
Пример #4
0
    def run(
        self,
        provider: str,
        options: BaseAwsOptions,
        diagram_builder: BaseDiagram,
        title: str,
        filename: str,
    ):
        """
        Executes a command.

        The project's development pattern is a file with the respective name of the parent
        resource (e.g. compute, network), classes of child resources inside this file and run() method to execute
        respective check. So it makes sense to load dynamically.
        """
        # Iterate to get all modules
        message_handler("\nInspecting resources", "HEADER")
        providers = []
        for name in os.listdir(
            dirname(__file__) + "/../provider/" + provider + "/resource"
        ):
            if name.endswith(".py"):
                # strip the extension
                module = name[:-3]

                # Load and call all run check
                for nameclass, cls in inspect.getmembers(
                    importlib.import_module(
                        "provider." + provider + ".resource." + module
                    ),
                    inspect.isclass,
                ):
                    if (
                        issubclass(cls, ResourceProvider)
                        and cls is not ResourceProvider
                    ):
                        providers.append((nameclass, cls))
        providers.sort(key=lambda x: x[0])

        all_resources: List[Resource] = []
        resource_relations: List[ResourceEdge] = []

        with ThreadPoolExecutor(15) as executor:
            provider_results = executor.map(
                lambda data: execute_provider(options, data), providers
            )

        for provider_results in provider_results:
            if provider_results[0] is not None:
                all_resources.extend(provider_results[0])
            if provider_results[1] is not None:
                resource_relations.extend(provider_results[1])

        unique_resources_dict: Dict[ResourceDigest, Resource] = dict()
        for resource in all_resources:
            unique_resources_dict[resource.digest] = resource

        unique_resources = list(unique_resources_dict.values())

        unique_resources.sort(key=lambda x: x.group + x.digest.type + x.name)
        resource_relations.sort(
            key=lambda x: x.from_node.type
            + x.from_node.id
            + x.to_node.type
            + x.to_node.id
        )

        # Resource filtering and sorting
        filtered_resources = filter_resources(unique_resources, self.filters)
        filtered_resources.sort(key=lambda x: x.group + x.digest.type + x.name)

        # Relationships filtering and sorting
        filtered_relations = filter_relations(filtered_resources, resource_relations)
        filtered_relations.sort(
            key=lambda x: x.from_node.type
            + x.from_node.id
            + x.to_node.type
            + x.to_node.id
        )

        # Diagram integration
        diagram_builder.build(
            resources=filtered_resources,
            resource_relations=filtered_relations,
            title=title,
            filename=filename,
        )

        # TODO: Generate reports in json/csv/pdf/xls
        report = Report()
        report.general_report(
            resources=filtered_resources, resource_relations=filtered_relations
        ),
        report.html_report(
            resources=filtered_resources,
            resource_relations=filtered_relations,
            title=title,
            filename=filename,
        )