-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_github_security_overview.py
More file actions
165 lines (132 loc) · 5.53 KB
/
collect_github_security_overview.py
File metadata and controls
165 lines (132 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
"""Collect counts-only GitHub security alert totals for one profile."""
from __future__ import annotations
import argparse
import json
import subprocess
from collections import Counter
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Callable
try:
from scripts.run_report_only import SUPPORTED_ALERT_CLASSES, _load_yaml_subset, _repo_alert_classes
except ModuleNotFoundError: # pragma: no cover - used when running from scripts/
from run_report_only import SUPPORTED_ALERT_CLASSES, _load_yaml_subset, _repo_alert_classes
CountClient = Callable[[str, str, str], tuple[int, bool]]
@dataclass(frozen=True)
class RepositoryScope:
repo: str
alert_classes: tuple[str, ...]
@dataclass(frozen=True)
class ProfileScope:
owner: str
repositories: tuple[RepositoryScope, ...]
def load_profile_scope(path: str | Path) -> ProfileScope:
data = _load_yaml_subset(Path(path))
profile = data.get("profile") if isinstance(data, dict) else None
if not isinstance(profile, dict):
raise ValueError("profile section is required")
owner = profile.get("owner")
if not owner:
raise ValueError("profile.owner is required")
defaults = profile.get("defaults") if isinstance(profile.get("defaults"), dict) else {}
default_mode = str(defaults.get("automation_mode") or "active")
repositories: list[RepositoryScope] = []
for entry in data.get("repositories") or []:
if not isinstance(entry, dict) or not entry.get("repo"):
continue
mode = str(entry.get("automation_mode") or default_mode)
if mode == "ignored":
continue
alert_classes = _repo_alert_classes(entry)
if alert_classes:
repositories.append(RepositoryScope(repo=str(entry["repo"]), alert_classes=alert_classes))
return ProfileScope(owner=str(owner), repositories=tuple(repositories))
def collect_security_overview(
profile: ProfileScope,
*,
count_client: CountClient,
now: datetime | None = None,
) -> dict[str, Any]:
counts: Counter[str] = Counter()
unavailable: Counter[str] = Counter()
for repo in profile.repositories:
for alert_class in repo.alert_classes:
count, failed = count_client(profile.owner, repo.repo, alert_class)
counts[alert_class] += count
if failed:
unavailable[alert_class] += 1
open_counts = {alert_class: counts[alert_class] for alert_class in SUPPORTED_ALERT_CLASSES}
unavailable_counts = {alert_class: unavailable[alert_class] for alert_class in SUPPORTED_ALERT_CLASSES}
return {
"schema_version": 1,
"generated_at": _format_utc(now or datetime.now(timezone.utc)),
"source": "github_profile_security_overview",
"owner": profile.owner,
"repo_scope": "profile",
"open_alert_counts": {**open_counts, "total": sum(open_counts.values())},
"unavailable_queries": {**unavailable_counts, "total": sum(unavailable_counts.values())},
}
def fetch_open_alert_count(owner: str, repo: str, alert_class: str) -> tuple[int, bool]:
endpoint = _alert_endpoint(owner, repo, alert_class)
if not endpoint:
return 0, False
result = subprocess.run(
["gh", "api", "--paginate", endpoint],
check=False,
capture_output=True,
text=True,
)
if result.returncode != 0:
return 0, True
return sum(_page_length(page) for page in decode_paginated_json(result.stdout)), False
def decode_paginated_json(text: str) -> list[Any]:
decoder = json.JSONDecoder()
index = 0
values: list[Any] = []
while index < len(text):
while index < len(text) and text[index].isspace():
index += 1
if index >= len(text):
break
value, index = decoder.raw_decode(text, index)
values.append(value)
return values
def _alert_endpoint(owner: str, repo: str, alert_class: str) -> str:
if alert_class == "dependabot":
return f"/repos/{owner}/{repo}/dependabot/alerts?state=open&per_page=100"
if alert_class == "code_scanning":
return f"/repos/{owner}/{repo}/code-scanning/alerts?state=open&per_page=100"
if alert_class == "secret_scanning":
return f"/repos/{owner}/{repo}/secret-scanning/alerts?state=open&per_page=100"
return ""
def _page_length(value: Any) -> int:
if isinstance(value, list):
return len(value)
if isinstance(value, dict):
return 1
return 0
def _format_utc(value: datetime) -> str:
if value.tzinfo is None:
value = value.replace(tzinfo=timezone.utc)
value = value.astimezone(timezone.utc).replace(microsecond=0)
return value.isoformat().replace("+00:00", "Z")
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--profile", required=True, help="Path to the selected profile.yaml")
parser.add_argument("--output", type=Path, help="Write sanitized count JSON to this path")
return parser
def main() -> int:
args = build_parser().parse_args()
profile = load_profile_scope(args.profile)
data = collect_security_overview(profile, count_client=fetch_open_alert_count)
rendered = json.dumps(data, indent=2, sort_keys=True) + "\n"
if args.output:
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(rendered, encoding="utf-8")
else:
print(rendered, end="")
return 0
if __name__ == "__main__":
raise SystemExit(main())