Skip to content

Commit 0405604

Browse files
authored
Merge pull request #11 from advanced-security/select-secret-output
Updated list secret scanning alerts to give all default and generic by default, allow switching off either, and listing a custom set of types if needed
2 parents 39ad44e + 227d9eb commit 0405604

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ A note on common arguments: generally, the date in `--since` can be specified as
2929
This script retrieves secret scanning alerts from GitHub repositories, organizations, or Enterprises and outputs them in CSV or JSON format. It supports filtering by state, date, and push protection bypass status. Use this to audit, analyze, or export secret scanning data for compliance or security purposes.
3030

3131
```text
32-
usage: list_secret_scanning_alerts.py [-h] [--scope {ent,org,repo}] [--generic] [--bypassed] [--state {open,resolved}]
33-
[--no-include-secret] [--include-locations] [--include-commit] [--since SINCE]
34-
[--json] [--raw] [--quote-all] [--hostname HOSTNAME]
32+
usage: list_secret_scanning_alerts.py [-h] [--scope {ent,org,repo}] [--no-generic] [--no-default] [--include-types INCLUDE_TYPES [INCLUDE_TYPES ...]] [--bypassed] [--state {open,resolved}]
33+
[--no-include-secret] [--include-locations] [--include-commit] [--since SINCE] [--json] [--raw] [--quote-all] [--hostname HOSTNAME]
3534
[--ca-cert-bundle CA_CERT_BUNDLE] [--no-verify-tls] [--quiet] [--debug]
3635
name
3736
@@ -44,24 +43,24 @@ options:
4443
-h, --help show this help message and exit
4544
--scope {ent,org,repo}
4645
Scope of the query
47-
--generic, -g Include generic secret types (not just vendor secret types/custom patterns, which is the
48-
default)
46+
--no-generic Exclude generic secret types from the output
47+
--no-default Exclude default secret types from the output
48+
--include-types INCLUDE_TYPES [INCLUDE_TYPES ...]
49+
Include specific secret types in the output (adds to any generic/default secrets that are output, so use --no-generic and --no-default to exclude those if required)
4950
--bypassed, -b Only show alerts where push protection was bypassed
50-
--state {open,resolved}, -s {open,resolved}
51+
--state, -s {open,resolved}
5152
State of the alerts to query
5253
--no-include-secret, -n
5354
Do not include the secret in the output
5455
--include-locations, -l
5556
Include locations in the output
5657
--include-commit, -c Include commit date and committer in the output
57-
--since SINCE, -S SINCE
58-
Only show alerts created after this date/time - ISO 8601 format, e.g. 2024-10-08 or
59-
2024-10-08T12:00; or Nd format, e.g. 7d for 7 days ago
58+
--since, -S SINCE Only show alerts created after this date/time - ISO 8601 format, e.g. 2024-10-08 or 2024-10-08T12:00; or Nd format, e.g. 7d for 7 days ago
6059
--json Output in JSON format (otherwise CSV)
6160
--raw, -r Output the raw data from the GitHub API
6261
--quote-all, -Q Quote all fields in CSV output
6362
--hostname HOSTNAME GitHub Enterprise hostname (defaults to github.com)
64-
--ca-cert-bundle CA_CERT_BUNDLE, -C CA_CERT_BUNDLE
63+
--ca-cert-bundle, -C CA_CERT_BUNDLE
6564
Path to CA certificate bundle in PEM format (e.g. for self-signed server certificates)
6665
--no-verify-tls Do not verify TLS connection certificates (warning: insecure)
6766
--quiet, -q Suppress non-error log messages

githubapi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ def list_secret_scanning_alerts(
430430
scope: str = "org",
431431
bypassed: bool = False,
432432
generic: bool = False,
433+
secret_types: list[str] | None = None,
433434
progress: bool = True,
434435
) -> Generator[dict, None, None]:
435436
"""List secret scanning alerts for a GitHub repository, organization or Enterprise."""
@@ -438,6 +439,9 @@ def list_secret_scanning_alerts(
438439
if generic:
439440
query["secret_type"] = GENERIC_SECRET_TYPES
440441

442+
if secret_types:
443+
query["secret_type"] = ",".join(secret_types)
444+
441445
alerts = self.query(
442446
scope,
443447
name,

list_secret_scanning_alerts.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import logging
88
import datetime
99
import json
10-
from typing import Generator, Any
10+
from typing import Generator, Any, Iterable
1111
from defusedcsv import csv # type: ignore
1212
from githubapi import GitHub, parse_date
1313
from requests.exceptions import HTTPError
@@ -142,7 +142,7 @@ def output_csv(results: list[dict], quote_all: bool) -> None:
142142
LOG.info("Stopped by user")
143143
return
144144

145-
def decorate_alerts(g: GitHub, alerts: Generator[dict[str, Any], None, None], include_locations: bool = False, include_commit: bool = False) -> Generator[dict[str, Any], None, None]:
145+
def decorate_alerts(g: GitHub, alerts: Iterable[dict[str, Any]], include_locations: bool = False, include_commit: bool = False) -> Generator[dict[str, Any], None, None]:
146146
"""Decorate alerts with additional information, for both the raw and make_result outputs.
147147
148148
Resolve locations and commit information, if that was asked for.
@@ -225,6 +225,8 @@ def list_secret_scanning_alerts(
225225
bypassed: bool = False,
226226
raw: bool = False,
227227
generic: bool = False,
228+
default: bool = False,
229+
specific: list[str] | None = None,
228230
verify: bool | str = True,
229231
progress: bool = True,
230232
) -> Generator[dict[str, Any], None, None] | None:
@@ -235,9 +237,28 @@ def list_secret_scanning_alerts(
235237
Output either the raw alert data, or flattened results.
236238
"""
237239
g = GitHub(hostname=hostname, verify=verify)
238-
alerts = g.list_secret_scanning_alerts(
239-
name, state=state, since=since, scope=scope, bypassed=bypassed, generic=generic, progress=progress
240-
)
240+
241+
alerts = []
242+
243+
if default:
244+
default_alerts = g.list_secret_scanning_alerts(
245+
name, state=state, since=since, scope=scope, bypassed=bypassed, generic=False, progress=progress
246+
)
247+
248+
alerts.append(default_alerts)
249+
250+
if generic:
251+
generic_alerts = g.list_secret_scanning_alerts(
252+
name, state=state, since=since, scope=scope, bypassed=bypassed, generic=True, progress=progress
253+
)
254+
255+
alerts.append(generic_alerts)
256+
257+
if specific:
258+
specific_alerts = g.list_secret_scanning_alerts(
259+
name, state=state, since=since, scope=scope, bypassed=bypassed, secret_types=specific, progress=progress
260+
)
261+
alerts.append(specific_alerts)
241262

242263
alerts = decorate_alerts(g, alerts, include_locations=include_locations, include_commit=include_commit)
243264

@@ -268,10 +289,20 @@ def add_args(parser: argparse.ArgumentParser) -> None:
268289
help="Scope of the query",
269290
)
270291
parser.add_argument(
271-
"--generic",
272-
"-g",
292+
"--no-generic",
273293
action="store_true",
274-
help="Include generic secret types (not just vendor secret types/custom patterns, which is the default)",
294+
help="Exclude generic secret types from the output",
295+
)
296+
parser.add_argument(
297+
"--no-default",
298+
action="store_true",
299+
help="Exclude default secret types from the output",
300+
)
301+
parser.add_argument(
302+
"--include-types",
303+
type=str,
304+
nargs="+",
305+
help="Include specific secret types in the output (adds to any generic/default secrets that are output, so use --no-generic and --no-default to exclude those if required)",
275306
)
276307
parser.add_argument(
277308
"--bypassed",
@@ -374,7 +405,9 @@ def main() -> None:
374405
include_locations = args.include_locations
375406
include_commit = args.include_commit
376407
bypassed = args.bypassed
377-
generic = args.generic
408+
generic = not args.no_generic
409+
default = not args.no_default
410+
specific = args.include_types
378411
verify = True
379412

380413
if args.ca_cert_bundle:
@@ -402,6 +435,8 @@ def main() -> None:
402435
bypassed=bypassed,
403436
raw=args.raw,
404437
generic=generic,
438+
default=default,
439+
specific=specific,
405440
verify=verify
406441
)
407442

0 commit comments

Comments
 (0)