|
| 1 | +"""Functional tests for Konflux-style attestation verification. |
| 2 | +
|
| 3 | +These tests exercise the attestation / provenance upload paths with |
| 4 | +attestations that carry an RSA signature instead of a Sigstore certificate, |
| 5 | +mirroring the format produced by Konflux / Calunga builds. |
| 6 | +
|
| 7 | +The test signing key is generated at image build time and the matching |
| 8 | +public key is configured as PULP_ATTESTATION_VERIFICATION_KEY so that |
| 9 | +signature verification is fully exercised end-to-end. |
| 10 | +""" |
| 11 | + |
| 12 | +import base64 |
| 13 | +import json |
| 14 | +import os |
| 15 | + |
| 16 | +import pytest |
| 17 | +from cryptography.hazmat.primitives import hashes, serialization |
| 18 | +from cryptography.hazmat.primitives.asymmetric import padding as crypto_padding |
| 19 | + |
| 20 | +from pulpcore.tests.functional.utils import PulpTaskError |
| 21 | + |
| 22 | +DSSE_PAYLOAD_TYPE = "application/vnd.in-toto+json" |
| 23 | + |
| 24 | +TEST_PRIVATE_KEY_PATH = "/etc/pki/attestation/test-key-private.pem" |
| 25 | +TEST_PUBLIC_KEY_PATH = "/etc/pki/attestation/test-key.pem" |
| 26 | + |
| 27 | + |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | +# Helpers |
| 30 | +# --------------------------------------------------------------------------- |
| 31 | + |
| 32 | + |
| 33 | +def _build_statement(filename, sha256): |
| 34 | + """Build a minimal in-toto statement for a Konflux attestation.""" |
| 35 | + return json.dumps( |
| 36 | + { |
| 37 | + "_type": "https://in-toto.io/Statement/v0.1", |
| 38 | + "predicateType": "https://slsa.dev/provenance/v0.2", |
| 39 | + "subject": [{"name": filename, "digest": {"sha256": sha256}}], |
| 40 | + "predicate": { |
| 41 | + "buildType": "https://konflux-ci.dev/PythonWheelBuild@v1", |
| 42 | + "builder": {"id": "https://konflux-ci.dev/calunga"}, |
| 43 | + }, |
| 44 | + } |
| 45 | + ).encode() |
| 46 | + |
| 47 | + |
| 48 | +def _pae(payload_type, payload): |
| 49 | + """Compute the DSSE Pre-Authentication Encoding.""" |
| 50 | + return b"DSSEv1 %d %b %d %b" % ( |
| 51 | + len(payload_type), |
| 52 | + payload_type.encode(), |
| 53 | + len(payload), |
| 54 | + payload, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def _sign(statement_bytes, private_key): |
| 59 | + pae = _pae(DSSE_PAYLOAD_TYPE, statement_bytes) |
| 60 | + return private_key.sign( |
| 61 | + pae, |
| 62 | + crypto_padding.PKCS1v15(), |
| 63 | + hashes.SHA256(), |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def _b64(data: bytes) -> str: |
| 68 | + return base64.b64encode(data).decode() |
| 69 | + |
| 70 | + |
| 71 | +def _make_attestation(statement_bytes, signature_bytes): |
| 72 | + """Return a single PEP-740 Attestation dict (Konflux flavour).""" |
| 73 | + return { |
| 74 | + "version": 1, |
| 75 | + "verification_material": None, |
| 76 | + "envelope": { |
| 77 | + "statement": _b64(statement_bytes), |
| 78 | + "signature": _b64(signature_bytes), |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +def _make_provenance(attestation): |
| 84 | + """Wrap an attestation into a full PEP-740 Provenance object.""" |
| 85 | + return { |
| 86 | + "version": 1, |
| 87 | + "attestation_bundles": [ |
| 88 | + { |
| 89 | + "publisher": {"kind": "Konflux", "builder": "calunga"}, |
| 90 | + "attestations": [attestation], |
| 91 | + } |
| 92 | + ], |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | +# --------------------------------------------------------------------------- |
| 97 | +# Fixtures |
| 98 | +# --------------------------------------------------------------------------- |
| 99 | + |
| 100 | + |
| 101 | +@pytest.fixture(scope="session") |
| 102 | +def test_private_key(pulp_settings): |
| 103 | + """Load the CI-generated test attestation signing key.""" |
| 104 | + if not os.path.exists(TEST_PRIVATE_KEY_PATH): |
| 105 | + pytest.skip( |
| 106 | + f"Test attestation private key not found at {TEST_PRIVATE_KEY_PATH} (not running in CI container?)" |
| 107 | + ) |
| 108 | + if pulp_settings.ATTESTATION_VERIFICATION_KEY != TEST_PUBLIC_KEY_PATH: |
| 109 | + pytest.fail(f"ATTESTATION_VERIFICATION_KEY is not set to {TEST_PUBLIC_KEY_PATH}") |
| 110 | + with open(TEST_PRIVATE_KEY_PATH, "rb") as f: |
| 111 | + return serialization.load_pem_private_key(f.read(), password=None) |
| 112 | + |
| 113 | + |
| 114 | +@pytest.fixture() |
| 115 | +def _provenance_file(tmp_path): |
| 116 | + """Return a helper that writes a provenance dict to a temp file.""" |
| 117 | + |
| 118 | + def _write(provenance_dict): |
| 119 | + path = tmp_path / "provenance.json" |
| 120 | + path.write_text(json.dumps(provenance_dict)) |
| 121 | + return str(path) |
| 122 | + |
| 123 | + return _write |
| 124 | + |
| 125 | + |
| 126 | +# --------------------------------------------------------------------------- |
| 127 | +# Tests |
| 128 | +# --------------------------------------------------------------------------- |
| 129 | + |
| 130 | + |
| 131 | +def test_konflux_provenance_stored( |
| 132 | + python_bindings, python_content_factory, monitor_task, test_private_key, _provenance_file |
| 133 | +): |
| 134 | + """A Konflux-style provenance is accepted and stored when verify=True.""" |
| 135 | + content = python_content_factory() |
| 136 | + |
| 137 | + stmt = _build_statement(content.filename, content.sha256) |
| 138 | + sig = _sign(stmt, test_private_key) |
| 139 | + att = _make_attestation(stmt, sig) |
| 140 | + prov = _make_provenance(att) |
| 141 | + |
| 142 | + task = python_bindings.ContentProvenanceApi.create( |
| 143 | + package=content.pulp_href, |
| 144 | + file=_provenance_file(prov), |
| 145 | + verify=True, |
| 146 | + ).task |
| 147 | + result = monitor_task(task) |
| 148 | + |
| 149 | + prov_obj = python_bindings.ContentProvenanceApi.read(result.created_resources[-1]) |
| 150 | + assert prov_obj.package == content.pulp_href |
| 151 | + stored_att = prov_obj.provenance["attestation_bundles"][0]["attestations"][0] |
| 152 | + assert stored_att["envelope"]["statement"] == _b64(stmt) |
| 153 | + publisher = prov_obj.provenance["attestation_bundles"][0]["publisher"] |
| 154 | + assert publisher["builder"] == "calunga" |
| 155 | + assert publisher["kind"] == "Konflux" |
| 156 | + |
| 157 | + |
| 158 | +def test_konflux_wrong_subject_name_rejected( |
| 159 | + python_bindings, python_content_factory, monitor_task, test_private_key, _provenance_file |
| 160 | +): |
| 161 | + """Verification rejects a Konflux attestation whose subject name does not match.""" |
| 162 | + content = python_content_factory() |
| 163 | + |
| 164 | + wrong_name = "wrong-package-0.1.tar.gz" |
| 165 | + stmt = _build_statement(wrong_name, content.sha256) |
| 166 | + sig = _sign(stmt, test_private_key) |
| 167 | + att = _make_attestation(stmt, sig) |
| 168 | + prov = _make_provenance(att) |
| 169 | + |
| 170 | + task = python_bindings.ContentProvenanceApi.create( |
| 171 | + package=content.pulp_href, |
| 172 | + file=_provenance_file(prov), |
| 173 | + verify=True, |
| 174 | + ).task |
| 175 | + with pytest.raises(PulpTaskError) as exc_info: |
| 176 | + monitor_task(task) |
| 177 | + assert "subject does not match distribution name" in exc_info.value.task.error["description"] |
| 178 | + |
| 179 | + |
| 180 | +def test_konflux_wrong_digest_rejected( |
| 181 | + python_bindings, python_content_factory, monitor_task, test_private_key, _provenance_file |
| 182 | +): |
| 183 | + """Verification rejects a Konflux attestation whose digest does not match.""" |
| 184 | + content = python_content_factory() |
| 185 | + |
| 186 | + bad_digest = "0" * 64 |
| 187 | + stmt = _build_statement(content.filename, bad_digest) |
| 188 | + sig = _sign(stmt, test_private_key) |
| 189 | + att = _make_attestation(stmt, sig) |
| 190 | + prov = _make_provenance(att) |
| 191 | + |
| 192 | + task = python_bindings.ContentProvenanceApi.create( |
| 193 | + package=content.pulp_href, |
| 194 | + file=_provenance_file(prov), |
| 195 | + verify=True, |
| 196 | + ).task |
| 197 | + with pytest.raises(PulpTaskError) as exc_info: |
| 198 | + monitor_task(task) |
| 199 | + assert "subject does not match distribution digest" in exc_info.value.task.error["description"] |
| 200 | + |
| 201 | + |
| 202 | +def test_konflux_bad_signature_rejected( |
| 203 | + python_bindings, python_content_factory, monitor_task, test_private_key, _provenance_file |
| 204 | +): |
| 205 | + """An attestation with a valid subject but tampered signature is rejected.""" |
| 206 | + content = python_content_factory() |
| 207 | + |
| 208 | + stmt = _build_statement(content.filename, content.sha256) |
| 209 | + sig = _sign(stmt, test_private_key) |
| 210 | + tampered_sig = bytes([b ^ 0xFF for b in sig[:32]]) + sig[32:] |
| 211 | + att = _make_attestation(stmt, tampered_sig) |
| 212 | + prov = _make_provenance(att) |
| 213 | + |
| 214 | + task = python_bindings.ContentProvenanceApi.create( |
| 215 | + package=content.pulp_href, |
| 216 | + file=_provenance_file(prov), |
| 217 | + verify=True, |
| 218 | + ).task |
| 219 | + with pytest.raises(PulpTaskError) as exc_info: |
| 220 | + monitor_task(task) |
| 221 | + assert "signature verification failed" in exc_info.value.task.error["description"] |
| 222 | + |
| 223 | + |
| 224 | +def test_konflux_attestation_via_content_upload( |
| 225 | + python_bindings, python_content_factory, monitor_task, test_private_key |
| 226 | +): |
| 227 | + """Konflux-style attestations can be uploaded alongside a package via the content API.""" |
| 228 | + content = python_content_factory() |
| 229 | + |
| 230 | + stmt = _build_statement(content.filename, content.sha256) |
| 231 | + sig = _sign(stmt, test_private_key) |
| 232 | + att = _make_attestation(stmt, sig) |
| 233 | + |
| 234 | + body = { |
| 235 | + "artifact": content.artifact, |
| 236 | + "relative_path": content.filename, |
| 237 | + "sha256": content.sha256, |
| 238 | + "attestations": json.dumps([att]), |
| 239 | + } |
| 240 | + task = python_bindings.ContentPackagesApi.create(**body).task |
| 241 | + result = monitor_task(task) |
| 242 | + |
| 243 | + assert len(result.created_resources) == 2 |
| 244 | + prov_obj = python_bindings.ContentProvenanceApi.read(result.created_resources[1]) |
| 245 | + publisher = prov_obj.provenance["attestation_bundles"][0]["publisher"] |
| 246 | + assert publisher["builder_id"] == "https://konflux-ci.dev/calunga" |
| 247 | + assert publisher["build_type"] == "https://konflux-ci.dev/PythonWheelBuild@v1" |
| 248 | + assert publisher["kind"] == "konflux-ci.dev" |
0 commit comments