Commit df14ef26 authored by Clement Bois's avatar Clement Bois
Browse files

Merge branch 'findings-e2e-test' into 'main'

Findings e2e test

See merge request to-be-continuous/tools/dt-sbom-scanner!12
parents 840769a2 acab321d
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ INSECURE_SSL_CTX.verify_mode = ssl.CERT_NONE

MIME_APPLICATION_JSON = "application/json"

SEVERITY_COLOR = [AnsiColors.HRED, AnsiColors.RED, AnsiColors.YELLOW, AnsiColors.GREEN, AnsiColors.RESET, AnsiColors.PURPLE]

class DtPermission(str, Enum):
    """Dependency Track permissions.
@@ -203,6 +204,7 @@ class Scanner:
        merge_output: str = None,
        verify_ssl: bool = True,
        show_findings: bool = False,
        **_: None,
    ):
        self.base_api_url = base_api_url
        self.api_key = api_key
@@ -248,6 +250,12 @@ class Scanner:

        return self._purl_max_len

    @property
    @cache
    def event_token_path(self) -> str:
        """Determines the DT bom/token or event/token path depending on the DT server version."""
        return "bom/token" if self.dt_version < Version("4.11.0") else "event/token"

    @cache
    def get_permissions(self) -> list[DtPermission]:
        return [
@@ -545,11 +553,12 @@ class Scanner:
        for o in findings:
            vuln = o.get("vulnerability", {})
            component = o.get("component", {})
            color = SEVERITY_COLOR[vuln.get("severityRank", 5)]
            cwes = (cwe["name"] for cwe in vuln.get("cwes", []))
            print(
                f'[{vuln.get("severity", "?")}] {" ".join(cwes)} - {component.get("group","")}:{component.get("name")}:{component.get("version","")}'
                f'  - {vuln["vulnId"]} {color}{vuln.get("severity", "?").capitalize()}{AnsiColors.RESET}: {component.get("group","")}:{component.get("name")}:{component.get("version","")} - {" ".join(cwes)}'
            )
            print(vuln.get("description", "").strip())
            print(re.sub('\n+', '\n', vuln.get("description", "").strip()))
            print()
        print(
            f"- scan {AnsiColors.HGREEN}succeeded{AnsiColors.RESET}: {len(findings)} vulnerabilities found{AnsiColors.RESET}"
@@ -559,7 +568,7 @@ class Scanner:
        for n in range(8):  # ~5 minutes
            sleep(2**n)
            resp = requests.get(
                f"{self.base_api_url}/v1/event/token/{event_id}",
                f"{self.base_api_url}/v1/{self.event_token_path}/{event_id}",
                headers={"X-API-Key": self.api_key, "accept": MIME_APPLICATION_JSON},
                verify=self.verify_ssl,
            )