-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest_github_control_plane.py
More file actions
192 lines (169 loc) · 8.85 KB
/
test_github_control_plane.py
File metadata and controls
192 lines (169 loc) · 8.85 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from __future__ import annotations
import importlib.util
import json
import sys
from pathlib import Path
def _load_module() -> object:
script_path = Path(__file__).resolve().parents[3] / "scripts" / "check_github_control_plane.py"
spec = importlib.util.spec_from_file_location("cortexpilot_check_github_control_plane", script_path)
module = importlib.util.module_from_spec(spec)
assert spec and spec.loader
spec.loader.exec_module(module)
return module
def _policy_payload() -> dict:
return {
"owner": "example",
"repo": "repo",
"default_branch": "main",
"required_actions_permissions": {
"enabled": True,
"allowed_actions": "all",
"sha_pinning_required": True,
},
"required_environments": ["owner-approved-sensitive"],
"required_checks": ["Quick Feedback"],
"branch_protection_required": True,
"platform_evidence": {
"private_vulnerability_reporting": {"required": True, "mode": "api"},
"vulnerability_alerts": {"required": True, "mode": "api"},
"secret_scanning": {"required": True, "mode": "api"},
"secret_scanning_push_protection": {"required": True, "mode": "api"},
"secret_scanning_non_provider_patterns": {"required": True, "mode": "api"},
"secret_scanning_validity_checks": {"required": True, "mode": "api"},
"org_code_security_configuration": {
"required": True,
"mode": "api",
"configuration_id": 240284,
"required_repository_status": "enforced",
},
"dependabot_config": {"required": True, "mode": "repo_file", "path": ".github/dependabot.yml"},
"codeql_workflow": {"required": True, "mode": "repo_file", "path": ".github/workflows/codeql.yml"},
"codeql_config": {"required": True, "mode": "repo_file", "path": ".github/codeql/codeql-config.yml"},
},
}
def _fake_gh_json(secret_non_provider: str, validity_checks: str):
def _impl(path: str) -> tuple[int, dict]:
if path == "repos/example/repo":
return (
0,
{
"id": 123,
"default_branch": "main",
"security_and_analysis": {
"secret_scanning": {"status": "enabled"},
"secret_scanning_push_protection": {"status": "enabled"},
"secret_scanning_non_provider_patterns": {"status": secret_non_provider},
"secret_scanning_validity_checks": {"status": validity_checks},
},
},
)
if path == "repos/example/repo/actions/permissions":
return 0, {"enabled": True, "allowed_actions": "all", "sha_pinning_required": True}
if path == "repos/example/repo/branches/main/protection":
return 0, {"required_status_checks": {"contexts": ["Quick Feedback"]}}
if path == "repos/example/repo/environments":
return 0, {"environments": [{"name": "owner-approved-sensitive"}]}
if path == "repos/example/repo/private-vulnerability-reporting":
return 0, {"enabled": True}
if path == "repos/example/repo/vulnerability-alerts":
return 0, {}
if path == "repos/example/repo/code-scanning/default-setup":
return 0, {}
if path == "repos/example/repo/dependabot/alerts?per_page=1":
return 0, []
if path == "orgs/example/code-security/configurations/240284":
return 0, {
"id": 240284,
"secret_scanning": "enabled",
"secret_scanning_push_protection": "enabled",
"secret_scanning_non_provider_patterns": "enabled",
"secret_scanning_validity_checks": "enabled",
}
if path == "orgs/example/code-security/configurations/240284/repositories":
return 0, [{"status": "enforced", "repository": {"id": 123}}]
raise AssertionError(path)
return _impl
def _fake_gh_json_without_org_enforcement(secret_non_provider: str, validity_checks: str):
def _impl(path: str) -> tuple[int, dict]:
if path == "repos/example/repo":
return (
0,
{
"id": 123,
"default_branch": "main",
"security_and_analysis": {
"secret_scanning": {"status": "enabled"},
"secret_scanning_push_protection": {"status": "enabled"},
"secret_scanning_non_provider_patterns": {"status": secret_non_provider},
"secret_scanning_validity_checks": {"status": validity_checks},
},
},
)
if path == "repos/example/repo/actions/permissions":
return 0, {"enabled": True, "allowed_actions": "all", "sha_pinning_required": True}
if path == "repos/example/repo/branches/main/protection":
return 0, {"required_status_checks": {"contexts": ["Quick Feedback"]}}
if path == "repos/example/repo/environments":
return 0, {"environments": [{"name": "owner-approved-sensitive"}]}
if path == "repos/example/repo/private-vulnerability-reporting":
return 0, {"enabled": True}
if path == "repos/example/repo/vulnerability-alerts":
return 0, {}
if path == "repos/example/repo/code-scanning/default-setup":
return 0, {}
if path == "repos/example/repo/dependabot/alerts?per_page=1":
return 0, []
if path == "orgs/example/code-security/configurations/240284":
return 0, {
"id": 240284,
"secret_scanning": "enabled",
"secret_scanning_push_protection": "enabled",
"secret_scanning_non_provider_patterns": "enabled",
"secret_scanning_validity_checks": "enabled",
}
if path == "orgs/example/code-security/configurations/240284/repositories":
return 0, []
raise AssertionError(path)
return _impl
def test_github_control_plane_requires_secret_scanning_subfeatures(tmp_path: Path, monkeypatch) -> None:
module = _load_module()
policy_path = tmp_path / "policy.json"
output_path = tmp_path / "report.json"
policy_path.write_text(json.dumps(_policy_payload(), ensure_ascii=False, indent=2), encoding="utf-8")
monkeypatch.setattr(module, "ROOT", tmp_path)
monkeypatch.setattr(module, "_gh_json", _fake_gh_json_without_org_enforcement("disabled", "disabled"))
monkeypatch.setattr(module, "_repo_path_exists", lambda _path: True)
monkeypatch.setattr(sys, "argv", ["check_github_control_plane.py", "--policy", str(policy_path), "--output", str(output_path)])
rc = module.main()
report = json.loads(output_path.read_text(encoding="utf-8"))
assert rc == 1
assert any("secret_scanning_non_provider_patterns drift" in item for item in report["errors"])
assert any("secret_scanning_validity_checks drift" in item for item in report["errors"])
def test_github_control_plane_accepts_enabled_secret_scanning_subfeatures(tmp_path: Path, monkeypatch) -> None:
module = _load_module()
policy_path = tmp_path / "policy.json"
output_path = tmp_path / "report.json"
policy_path.write_text(json.dumps(_policy_payload(), ensure_ascii=False, indent=2), encoding="utf-8")
monkeypatch.setattr(module, "ROOT", tmp_path)
monkeypatch.setattr(module, "_gh_json", _fake_gh_json("enabled", "enabled"))
monkeypatch.setattr(module, "_repo_path_exists", lambda _path: True)
monkeypatch.setattr(sys, "argv", ["check_github_control_plane.py", "--policy", str(policy_path), "--output", str(output_path)])
rc = module.main()
report = json.loads(output_path.read_text(encoding="utf-8"))
assert rc == 0
assert report["errors"] == []
assert report["security_and_analysis"]["secret_scanning_non_provider_patterns"]["status"] == "enabled"
def test_github_control_plane_accepts_enforced_org_config_fallback(tmp_path: Path, monkeypatch) -> None:
module = _load_module()
policy_path = tmp_path / "policy.json"
output_path = tmp_path / "report.json"
policy_path.write_text(json.dumps(_policy_payload(), ensure_ascii=False, indent=2), encoding="utf-8")
monkeypatch.setattr(module, "ROOT", tmp_path)
monkeypatch.setattr(module, "_gh_json", _fake_gh_json("disabled", "disabled"))
monkeypatch.setattr(module, "_repo_path_exists", lambda _path: True)
monkeypatch.setattr(sys, "argv", ["check_github_control_plane.py", "--policy", str(policy_path), "--output", str(output_path)])
rc = module.main()
report = json.loads(output_path.read_text(encoding="utf-8"))
assert rc == 0
assert report["errors"] == []
assert report["org_code_security_configuration"]["id"] == 240284