Skip to content

Commit 72c3fa9

Browse files
committed
Verify release-plan supersession approval against GitHub authority
1 parent 34d5eed commit 72c3fa9

2 files changed

Lines changed: 99 additions & 5 deletions

File tree

‎scripts/ci/component-release-recovery.py‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,9 @@ def validate_supersession_record(
11401140
validate_environment_approval_evidence(authorization["environment_approval"], authorization)
11411141

11421142

1143-
def protected_environment_evidence(client: PublicClient) -> dict[str, Any]:
1143+
def protected_environment_evidence(
1144+
client: PublicClient,
1145+
) -> tuple[dict[str, Any], set[tuple[int, str]]]:
11441146
encoded = urllib.parse.quote(SUPERSESSION_ENVIRONMENT, safe="")
11451147
environment = client.json(
11461148
f"https://api.github.com/repos/{CONTROL_REPOSITORY}/environments/{encoded}",
@@ -1164,6 +1166,23 @@ def protected_environment_evidence(client: PublicClient) -> dict[str, Any]:
11641166
and rule["id"] > 0
11651167
)
11661168
)
1169+
required_reviewers = {
1170+
(reviewer["reviewer"]["id"], reviewer["reviewer"]["login"])
1171+
for rule in rules
1172+
if isinstance(rule, dict) and rule.get("type") == "required_reviewers"
1173+
for reviewer in (
1174+
rule.get("reviewers") if isinstance(rule.get("reviewers"), list) else []
1175+
)
1176+
if (
1177+
isinstance(reviewer, dict)
1178+
and reviewer.get("type") == "User"
1179+
and isinstance(reviewer.get("reviewer"), dict)
1180+
and type(reviewer["reviewer"].get("id")) is int
1181+
and reviewer["reviewer"]["id"] > 0
1182+
and isinstance(reviewer["reviewer"].get("login"), str)
1183+
and re.fullmatch(r"[A-Za-z0-9-]{1,39}", reviewer["reviewer"]["login"])
1184+
)
1185+
}
11671186
environment_id = environment.get("id")
11681187
branch_policy = environment.get("deployment_branch_policy")
11691188
if (
@@ -1207,13 +1226,14 @@ def protected_environment_evidence(client: PublicClient) -> dict[str, Any]:
12071226
"required_reviewer_rule_ids": reviewer_rule_ids,
12081227
}
12091228
validate_environment_protection_evidence(evidence)
1210-
return evidence
1229+
return evidence, required_reviewers
12111230

12121231

12131232
def protected_run_approval_evidence(
12141233
client: PublicClient,
12151234
authorization: dict[str, Any],
12161235
environment_protection: dict[str, Any],
1236+
required_reviewers: set[tuple[int, str]],
12171237
) -> dict[str, Any]:
12181238
run_id = authorization["run_id"]
12191239
run_attempt = authorization["run_attempt"]
@@ -1245,6 +1265,13 @@ def protected_run_approval_evidence(
12451265
"protected supersession workflow run evidence does not match GitHub",
12461266
"plan-discovery",
12471267
)
1268+
if run_attempt != 1:
1269+
# GitHub's approval-history response has no attempt identity. Attempt 1
1270+
# is the only attempt for which an approval cannot be stale.
1271+
raise RecoveryError(
1272+
"GitHub approval history cannot bind protected approval to a rerun attempt",
1273+
"plan-discovery",
1274+
)
12481275
history = client.json(
12491276
f"https://api.github.com/repos/{CONTROL_REPOSITORY}/actions/runs/{run_id}/approvals",
12501277
headers={"X-GitHub-Api-Version": SUPERSESSION_API_VERSION},
@@ -1301,18 +1328,28 @@ def protected_run_approval_evidence(
13011328
evidence,
13021329
{**authorization, "environment_protection": environment_protection},
13031330
)
1331+
if (evidence["user"]["id"], evidence["user"]["login"]) not in required_reviewers:
1332+
raise RecoveryError(
1333+
"protected supersession approving user is not authorized by the current reviewer policy",
1334+
"plan-discovery",
1335+
)
13041336
return evidence
13051337

13061338

13071339
def revalidate_supersession_authority(record: dict[str, Any], client: PublicClient) -> None:
13081340
authorization = record["authorization"]
1309-
protection = protected_environment_evidence(client)
1341+
protection, required_reviewers = protected_environment_evidence(client)
13101342
if protection != authorization["environment_protection"]:
13111343
raise RecoveryError(
13121344
"release plan failure protected environment policy no longer matches GitHub",
13131345
"plan-discovery",
13141346
)
1315-
approval = protected_run_approval_evidence(client, authorization, protection)
1347+
approval = protected_run_approval_evidence(
1348+
client,
1349+
authorization,
1350+
protection,
1351+
required_reviewers,
1352+
)
13161353
if approval != authorization["environment_approval"]:
13171354
raise RecoveryError(
13181355
"release plan failure approved deployment evidence no longer matches GitHub",

‎scripts/ci/test-component-release-recovery.py‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,17 @@ def captured_github_authority(module, record: dict[str, object]) -> list[object]
207207
{
208208
"id": protection["required_reviewer_rule_ids"][0],
209209
"type": "required_reviewers",
210-
"reviewers": [{"type": "User"}],
210+
"reviewers": [
211+
{
212+
"type": "User",
213+
"reviewer": {
214+
**approval["user"],
215+
"avatar_url": "https://avatars.githubusercontent.com/u/55?v=4",
216+
"site_admin": False,
217+
"type": "User",
218+
},
219+
}
220+
],
211221
}
212222
],
213223
"deployment_branch_policy": protection["deployment_branch_policy"],
@@ -913,6 +923,53 @@ def test_terminal_failure_normalizes_captured_github_approval_shape(self) -> Non
913923
with self.assertRaises(self.recovery.RecoveryError):
914924
self.recovery.revalidate_supersession_authority(record, client)
915925

926+
def test_terminal_failure_rejects_approval_history_for_a_rerun_attempt(self) -> None:
927+
failed = lifecycle_plan(self.recovery)
928+
successor = json.loads(json.dumps(failed))
929+
successor["plan"] = "successor-plan"
930+
successor["components"]["workflow"]["version"] = "2.0.0-alpha.2"
931+
record = supersession_record(self.recovery, failed, successor, "a" * 40)
932+
authorization = record["authorization"]
933+
authorization["run_attempt"] = 2
934+
authorization["environment_approval"]["run_attempt"] = 2
935+
client = mock.Mock()
936+
client.json.side_effect = captured_github_authority(self.recovery, record)
937+
938+
with self.assertRaisesRegex(
939+
self.recovery.RecoveryError,
940+
"approval history cannot bind.*rerun attempt",
941+
):
942+
self.recovery.revalidate_supersession_authority(record, client)
943+
944+
self.assertEqual(3, client.json.call_count)
945+
946+
def test_terminal_failure_rejects_approver_outside_current_policy(self) -> None:
947+
failed = lifecycle_plan(self.recovery)
948+
successor = json.loads(json.dumps(failed))
949+
successor["plan"] = "successor-plan"
950+
successor["components"]["workflow"]["version"] = "2.0.0-alpha.2"
951+
record = supersession_record(self.recovery, failed, successor, "a" * 40)
952+
responses = captured_github_authority(self.recovery, record)
953+
responses[0]["protection_rules"][0]["reviewers"][0]["reviewer"].update(
954+
{
955+
"html_url": "https://github.com/different-reviewer",
956+
"id": 77,
957+
"login": "different-reviewer",
958+
"node_id": "different-reviewer-node",
959+
"url": "https://api.github.com/users/different-reviewer",
960+
}
961+
)
962+
client = mock.Mock()
963+
client.json.side_effect = responses
964+
965+
with self.assertRaisesRegex(
966+
self.recovery.RecoveryError,
967+
"approving user is not authorized by the current reviewer policy",
968+
):
969+
self.recovery.revalidate_supersession_authority(record, client)
970+
971+
self.assertEqual(4, client.json.call_count)
972+
916973
def test_terminal_failure_rejects_incomplete_lifecycle_authority(self) -> None:
917974
failed = lifecycle_plan(self.recovery)
918975
failed["plan"] = "failed-plan"

0 commit comments

Comments
 (0)