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"]), )
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})
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" )
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"), )
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"
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
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})
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"])
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"])
def display_detector(detector: str, offset: int) -> str: """Return the formatted detector line.""" return " " * offset + format_text(detector, STYLE["detector"])
def display_match_value(match_value: str) -> str: """Return the formatted match value.""" return format_text(match_value, STYLE["secret"])
def display_patch(patch: str) -> str: """Return the formatted patch.""" return format_text(patch, STYLE["patch"])