audit-labs/audit-tools
A collection of scripts, queries, and other goodies you can use in an audit.
clone: git clone https://gitbay.org/audit-labs/audit-tools.git
main: applications/github/collectors/deploy_keys.py · raw
1"""Collect deploy keys across all repositories in an org."""
2
3import sys
4
5import requests
6
7from .api import paginate
8
9
10def deploy_keys(org, cfg):
11 rows = []
12 for repo in paginate(f"https://api.github.com/orgs/{org}/repos", cfg):
13 name = repo["name"]
14 try:
15 keys = paginate(f"https://api.github.com/repos/{org}/{name}/keys", cfg)
16 except requests.HTTPError as e:
17 if e.response is not None and e.response.status_code in (403, 404):
18 print(
19 f" Skipping {name}: keys endpoint returned "
20 f"{e.response.status_code}",
21 file=sys.stderr,
22 )
23 continue
24 raise
25 for k in keys:
26 rows.append(
27 {
28 "repo": name,
29 "title": k.get("title", ""),
30 "read_only": k.get("read_only"),
31 "created_at": k.get("created_at", ""),
32 "last_used": k.get("last_used") or "",
33 "added_by": k.get("added_by") or "",
34 }
35 )
36 return rows