Skip to content

Commit fc20d6a

Browse files
committed
Retain complete readiness outside the bounded terminal receipt
1 parent 949d38b commit fc20d6a

4 files changed

Lines changed: 94 additions & 4 deletions

File tree

‎scripts/ci/activity_acceptance_output.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
from __future__ import annotations
44

5+
import hashlib
56
import json
67
import os
78
import stat
89
from pathlib import Path
910

1011
RESULT_NAME = "result.json"
12+
RESULT_LIMIT = 128 * 1024
1113

1214

1315
class Output:
@@ -83,9 +85,25 @@ def finish(self, report):
8385
except ValueError:
8486
report["output_failure"] = "identity_changed"
8587
report["complete"] = False
86-
encoded = json.dumps(report, indent=2) + "\n"
87-
if len(encoded.encode()) > 128 * 1024:
88-
raise ValueError("terminal result exceeded its bound")
88+
encoded = (json.dumps(report, indent=2) + "\n").encode()
89+
if len(encoded) > RESULT_LIMIT:
90+
report["complete"] = False
91+
receipt = {
92+
"complete": False, "output_failure": "terminal_result_exceeded_bound",
93+
"oversized_report_bytes": len(encoded), "oversized_report_sha256": hashlib.sha256(encoded).hexdigest(),
94+
}
95+
if "output_failure" in report:
96+
receipt["output_boundary_failure"] = str(report["output_failure"])[:1024]
97+
for name in ("scenario", "artifact_source", "source_commit", "started_at", "finished_at",
98+
"runtime_id", "consumer_id", "network_id"):
99+
value = report.get(name)
100+
if isinstance(value, str) and len(value) <= 1024:
101+
receipt[name] = value
102+
cleanup = json.dumps(report.get("cleanup"), sort_keys=True).encode()
103+
receipt["cleanup"] = report.get("cleanup") if len(cleanup) <= RESULT_LIMIT // 2 else {
104+
"retained": False, "bytes": len(cleanup), "sha256": hashlib.sha256(cleanup).hexdigest(),
105+
}
106+
encoded = (json.dumps(receipt, sort_keys=True, separators=(",", ":")) + "\n").encode()
89107
self._write(self.files[RESULT_NAME], encoded)
90108

91109
def close(self):

‎scripts/ci/published-activity-acceptance.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ def packet_file(data):
148148
return {"base64": base64.b64encode(data).decode(), "sha256": hashlib.sha256(data).hexdigest()}
149149

150150

151+
def retain_consumer_ready(files, report, ready):
152+
# The complete environment inventory can exceed the terminal receipt bound.
153+
# Keep its bytes independently and bind them rather than duplicating them.
154+
data = json.dumps({key: value for key, value in ready.items() if key != "sdk_inventory"}, indent=2).encode()
155+
name = "consumer-ready.json"
156+
files.write(name, data)
157+
report["consumer_ready"] = {"file": name, "bytes": len(data), "sha256": hashlib.sha256(data).hexdigest()}
158+
159+
151160
def bind_bootstrap(captured):
152161
source = captured["bootstrap.py"].decode()
153162
for name, filename in (("CAPTURED_VERIFIER", VERIFIER_FILENAME), ("CAPTURED_ENTRY", "entry.py")):
@@ -525,7 +534,7 @@ def run_case(owned, selected, images, packet, bootstrap, report):
525534
name = "identity" if SCENARIO == "activity-identity" else SCENARIO
526535
owned.files.write(name + ".log", result["case_output"])
527536
owned.files.write(name + ".xml", result["junit"])
528-
report["consumer_ready"] = {key: value for key, value in ready.items() if key != "sdk_inventory"}
537+
retain_consumer_ready(owned.files, report, ready)
529538
session.send({"release": True})
530539
code = session.finish()
531540
report["consumer_exit"] = code

‎tests/test_activity_acceptance_authority.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,45 @@ def test_replaced_result_cannot_become_a_successful_terminal_receipt(tmp_path):
7878
assert (output.path / "result.json").read_text() == "foreign"
7979
finally:
8080
output.close()
81+
82+
83+
def test_oversized_report_retains_bounded_nonpass_and_exact_cleanup_ids(tmp_path):
84+
output = files.Output(tmp_path / "result")
85+
cleanup = {"a" * 64: {"absent": True, "oom_killed": False}, "remaining-owned-labels": {"absent": True}}
86+
report = {"complete": True, "scenario": "pagination", "runtime_id": "a" * 64,
87+
"consumer_id": "b" * 64, "network_id": "c" * 64, "cleanup": cleanup,
88+
"large_inventory": "x" * (files.RESULT_LIMIT + 1)}
89+
try:
90+
output.finish(report)
91+
data = (output.path / "result.json").read_bytes()
92+
receipt = json.loads(data)
93+
assert len(data) <= files.RESULT_LIMIT
94+
assert receipt["complete"] is report["complete"] is False
95+
assert receipt["output_failure"] == "terminal_result_exceeded_bound"
96+
assert receipt["runtime_id"] == "a" * 64
97+
assert receipt["consumer_id"] == "b" * 64
98+
assert receipt["network_id"] == "c" * 64
99+
assert receipt["cleanup"] == cleanup
100+
assert receipt["oversized_report_bytes"] > files.RESULT_LIMIT
101+
finally:
102+
output.close()
103+
104+
105+
def test_oversized_unbounded_cleanup_is_explicitly_unqualified_and_cannot_overwrite_foreign_path(tmp_path):
106+
output = files.Output(tmp_path / "result")
107+
original = tmp_path / "retained"
108+
output.path.rename(original)
109+
output.path.mkdir()
110+
(output.path / "result.json").write_text("foreign")
111+
report = {"complete": True, "cleanup": {"untrusted": "x" * (files.RESULT_LIMIT * 2)}}
112+
try:
113+
output.finish(report)
114+
data = (original / "result.json").read_bytes()
115+
receipt = json.loads(data)
116+
assert len(data) <= files.RESULT_LIMIT
117+
assert receipt["complete"] is False
118+
assert receipt["output_boundary_failure"] == "identity_changed"
119+
assert receipt["cleanup"]["retained"] is False
120+
assert (output.path / "result.json").read_text() == "foreign"
121+
finally:
122+
output.close()

‎tests/test_published_activity_acceptance.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import hashlib
34
import importlib.util
45
import json
56
from copy import deepcopy
@@ -17,6 +18,26 @@
1718
runtime = runner.runtime
1819

1920

21+
def test_large_readiness_inventory_is_retained_and_hash_bound_without_terminal_duplication(tmp_path):
22+
ready = {"distributions": "fixed locked wheels", "environment_inventory": {"entry": "x" * 200_000},
23+
"sdk_inventory": {"already": "retained separately"}}
24+
report = {"complete": True}
25+
files = runner.output_files.Output(tmp_path / "result")
26+
try:
27+
runner.retain_consumer_ready(files, report, ready)
28+
files.finish(report)
29+
data = (files.path / "consumer-ready.json").read_bytes()
30+
assert json.loads(data) == {key: value for key, value in ready.items() if key != "sdk_inventory"}
31+
assert report["consumer_ready"] == {
32+
"file": "consumer-ready.json", "bytes": len(data), "sha256": hashlib.sha256(data).hexdigest(),
33+
}
34+
result = (files.path / "result.json").read_bytes()
35+
assert len(result) < runner.output_files.RESULT_LIMIT
36+
assert json.loads(result)["complete"] is True
37+
finally:
38+
files.close()
39+
40+
2041
def configuration(role="runtime"):
2142
image = {
2243
"Id": "sha256:" + "f" * 64,

0 commit comments

Comments
 (0)