示例#1
0
def file_info(filename: str, nb_secrets: int) -> str:
    """Return the formatted file info (number of secrets + filename)."""
    return "\n{} {} {} been found in file {}\n".format(
        _file_info_decoration(),
        format_text(str(nb_secrets), STYLE["nb_secrets"]),
        pluralize("incident has", nb_secrets, "incidents have"),
        format_text(filename, STYLE["filename"]),
    )
示例#2
0
def format_quota_color(remaining: int, limit: int) -> str:
    if limit == 0:
        return format_text(str(remaining), {"fg": "white"})

    available_percent = remaining / limit
    if available_percent < 0.25:
        color = "red"
    elif available_percent < 0.75:
        color = "yellow"
    else:
        color = "green"

    return format_text(str(remaining), {"fg": color})
示例#3
0
 def optional_header(self) -> str:
     """Return the formatted patch."""
     return (
         format_text(f"\ncommit {self.sha}\n", STYLE["commit_info"])
         + f"Author: {self.info.author} <{self.info.email}>\n"
         + f"Date: {self.info.date}\n"
     )
示例#4
0
def policy_break_header(issue_n: int, policy_breaks: List[PolicyBreak],
                        ignore_sha: str) -> str:
    """
    Build a header for the policy break.
    """
    validity_msg = (
        f" (Validity: {format_text(translate_validity(policy_breaks[0].validity), STYLE['nb_secrets'])}) "
        if policy_breaks[0].validity else "")

    return "\n{} Incident {}({}): {}{} (Ignore with SHA: {}) ({} {})\n".format(
        format_text(">>>", STYLE["detector_line_start"]),
        issue_n,
        format_text(policy_breaks[0].policy, STYLE["detector"]),
        format_text(policy_breaks[0].break_type, STYLE["detector"]),
        validity_msg,
        format_text(ignore_sha, STYLE["ignore_sha"]),
        len(policy_breaks),
        pluralize("occurrence", len(policy_breaks), "occurrences"),
    )
示例#5
0
def format_detector(match_type: str, index_start: int, index_end: int) -> str:
    """Return detector object to add in detector_line."""

    detector_size = len(match_type)
    secret_size = index_end - index_start

    display = ""
    if secret_size < MAX_SECRET_SIZE:
        before = "_" * max(1, int(((secret_size - detector_size) - 1) / 2))
        after = "_" * max(1, (secret_size - len(before) - detector_size) - 2)
        display = "|{}{}{}|".format(before, match_type, after)

    return " " * index_start + format_text(display, STYLE["detector"]) + "\n"
示例#6
0
def scan_commit_range(
    client: GGClient,
    cache: Cache,
    commit_list: List[str],
    output_handler: OutputHandler,
    verbose: bool,
    exclusion_regexes: Set[re.Pattern],
    matches_ignore: Iterable[IgnoredMatch],
    all_policies: bool,
    scan_id: str,
    mode_header: str,
    banlisted_detectors: Optional[Set[str]] = None,
) -> int:  # pragma: no cover
    """
    Scan every commit in a range.

    :param client: Public Scanning API client
    :param commit_list: List of commits sha to scan
    :param verbose: Display successfull scan's message
    """
    return_code = 0
    with concurrent.futures.ThreadPoolExecutor(
            max_workers=min(CPU_COUNT, 4)) as executor:
        future_to_process = [
            executor.submit(
                scan_commit,
                Commit(sha, exclusion_regexes),
                client,
                cache,
                verbose,
                matches_ignore,
                all_policies,
                mode_header,
                banlisted_detectors,
            ) for sha in commit_list
        ]

        scans: List[ScanCollection] = []
        with click.progressbar(
                iterable=concurrent.futures.as_completed(future_to_process),
                length=len(future_to_process),
                label=format_text("Scanning Commits", STYLE["progress"]),
        ) as completed_futures:
            for future in completed_futures:
                scans.append(future.result())

        return_code = output_handler.process_scan(
            ScanCollection(id=scan_id, type="commit-range", scans=scans))
    return return_code
示例#7
0
def format_healthcheck_status(health_check: HealthCheckResponse) -> str:
    (color, status) = (("red", f"unhealthy ({health_check.detail})")
                       if health_check.status_code != 200 else
                       ("green", "healthy"))

    return format_text(status, {"fg": color})
示例#8
0
def format_line_count_break(padding: int) -> str:
    """Return the line count break."""
    return format_text(" " * max(0, padding - len("...")) + "...\n",
                       STYLE["detector_line_start"])
示例#9
0
def no_leak_message() -> str:
    """
    Build a message if no secret is found.
    """
    return format_text("\nNo secrets have been found\n", STYLE["no_secret"])
示例#10
0
def display_detector(detector: str, offset: int) -> str:
    """Return the formatted detector line."""
    return " " * offset + format_text(detector, STYLE["detector"])
示例#11
0
def display_match_value(match_value: str) -> str:
    """Return the formatted match value."""
    return format_text(match_value, STYLE["secret"])
示例#12
0
def display_patch(patch: str) -> str:
    """Return the formatted patch."""
    return format_text(patch, STYLE["patch"])