Skip to content

Commit 6a1058d

Browse files
committed
Add tests
1 parent 58535d8 commit 6a1058d

6 files changed

Lines changed: 326 additions & 2 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQDLYCPSdloA94Te
3+
+68CLqtHJ3qTKttyWyM1uHkcb+AEAExtQGLoKtysmE2QQz8xwpiBKsByAlPTAHZZ
4+
pYIGzof45xEwiRzBqKxE4um1Uhtq+WCth96UeUkjz2G0xMxkFycjHDdpQQ92Hlg+
5+
mvU8VOrs4Xi7PvG5E+lTY7zez89QQ2ZY3OcbFSHVkC1b0COjYqJ7m5TmVWaRyNbW
6+
Nn+00/uZ+hFzcsxAuKa9B9s4ngWELLA1TnWp5yi4q2Pkold4prI3PwF7IfGWtrfo
7+
TgyzCKji12QLLjbd5KiKecGqx9zaV8xs7GnCif47JmOFAEDW5dtUstepH8ysy8wC
8+
IXoZYvOSOsOv2mVz99JikyaWeV0rjNk7p9UPrhiaGKOfzfn+g6bzCJYBlo425pmJ
9+
S/d1EoBoqgeCGniyLkFByQ9zDmzr1NlCkNKtNII1qNoRSMyqJY0qKPAd2bNMHUfT
10+
FB3Eb/AvcOjb4fa5kkxl4gMQwasNaFmDgusqt1JjHK4XcL8UIcvzGPj8LsDrx5iT
11+
HCFSF1gJvAXiLG0o6KFgrHfTBw2V3mxaKZNGr2NA5IMTs2UeGud590VmSvsKhr6R
12+
tekHHGLCrZT9kPxbicBq6OZOhSb0usSpclSvwMzMSIOFNzACXq33nPYqlY0r1vGN
13+
5Gi2P9W2CxAYbnOGgqpzHbt4YoLVrQIDAQABAoICAA2SR3xZPtL8XCGFKhM7NLLK
14+
2k3NI60TPTDt3nxx+sD0RCVbkT4XniI7skauNh6xSFFWSQFSpnADgoz46R8LKTJd
15+
jl1uyOcjb7DTyO9l9e5tixtetbuysZly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ivOe
47+
5/OVKsDgKOdfCI3kP2I+/h//4aiW6JyHtXJkPojD9QLwPC887cmcc2A0726nhmoj
48+
VV5vZpJyTFPktxNc/snVID3uKT0uk4xfBAUNejRjlFFpC3fyxk3rB/0cG2UJWb0A
49+
VapDSXTg+S3kySS4uZbwW19jofDMqFfsVwlIFlMRSkE4jj5+Qs6/P1xnpAceIJVJ
50+
orG/DhLTqAwcRSjmNw/FpKNUbrFZ8NtUVU4XIEzvWmoqDMKrkiHMGOvfzU9jza1f
51+
Y3acysRQ2KI9Ero+Ga3hDfaohueVFg==
52+
-----END PRIVATE KEY-----

‎.ci/assets/keys/test-key.pem‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAy2Aj0nZaAPeE3vuvAi6r
3+
Ryd6kyrbclsjNbh5HG/gBABMbUBi6CrcrJhNkEM/McKYgSrAcgJT0wB2WaWCBs6H
4+
+OcRMIkcwaisROLptVIbavlgrYfelHlJI89htMTMZBcnIxw3aUEPdh5YPpr1PFTq
5+
7OF4uz7xuRPpU2O83s/PUENmWNznGxUh1ZAtW9Ajo2Kie5uU5lVmkcjW1jZ/tNP7
6+
mfoRc3LMQLimvQfbOJ4FhCywNU51qecouKtj5KJXeKayNz8BeyHxlra36E4Mswio
7+
4tdkCy423eSoinnBqsfc2lfMbOxpwon+OyZjhQBA1uXbVLLXqR/MrMvMAiF6GWLz
8+
kjrDr9plc/fSYpMmlnldK4zZO6fVD64Ymhijn835/oOm8wiWAZaONuaZiUv3dRKA
9+
aKoHghp4si5BQckPcw5s69TZQpDSrTSCNajaEUjMqiWNKijwHdmzTB1H0xQdxG/w
10+
L3Do2+H2uZJMZeIDEMGrDWhZg4LrKrdSYxyuF3C/FCHL8xj4/C7A68eYkxwhUhdY
11+
CbwF4ixtKOihYKx30wcNld5sWimTRq9jQOSDE7NlHhrnefdFZkr7Coa+kbXpBxxi
12+
wq2U/ZD8W4nAaujmToUm9LrEqXJUr8DMzEiDhTcwAl6t95z2KpWNK9bxjeRotj/V
13+
tgsQGG5zhoKqcx27eGKC1a0CAwEAAQ==
14+
-----END PUBLIC KEY-----

‎.github/workflows/scripts/before_install.sh‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ legacy_component_name: "pulp_python"
5050
component_name: "python"
5151
component_version: "${COMPONENT_VERSION}"
5252
pulp_env: {}
53-
pulp_settings: {"allowed_export_paths": "/tmp", "allowed_import_paths": "/tmp", "api_root": "/pulp/", "orphan_protection_time": 0, "pypi_api_hostname": "https://pulp:443"}
53+
pulp_settings: {"allowed_export_paths": "/tmp", "allowed_import_paths": "/tmp", "api_root": "/pulp/", "attestation_verification_key": "/etc/pki/attestation/test-key.pem", "orphan_protection_time": 0, "pypi_api_hostname": "https://pulp:443"}
5454
pulp_scheme: "https"
5555
image:
5656
name: "pulp"
@@ -64,6 +64,10 @@ image:
6464
extra_files:
6565
- origin: "pulp_python"
6666
destination: "pulp_python"
67+
- origin: "pulp_python/.ci/assets/keys/test-key.pem"
68+
destination: "/etc/pki/attestation/test-key.pem"
69+
- origin: "pulp_python/.ci/assets/keys/test-key-private.pem"
70+
destination: "/etc/pki/attestation/test-key-private.pem"
6771
services:
6872
- name: "pulp"
6973
image: "pulp:ci_build"
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
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"

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ filename = "./pyproject.toml"
125125
search = "version = \"{current_version}\""
126126
replace = "version = \"{new_version}\""
127127

128+
128129
[tool.black]
129130
line-length = 100
130131

‎template_config.yml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ deploy_client_to_rubygems: true
2020
deploy_to_pypi: true
2121
disabled_redis_runners: []
2222
docker_fixtures: false
23-
extra_files: []
23+
extra_files:
24+
- origin: "pulp_python/.ci/assets/keys/test-key.pem"
25+
destination: "/etc/pki/attestation/test-key.pem"
26+
- origin: "pulp_python/.ci/assets/keys/test-key-private.pem"
27+
destination: "/etc/pki/attestation/test-key-private.pem"
2428
github_org: "pulp"
2529
latest_release_branch: "3.33"
2630
lint_ignore: []
@@ -43,6 +47,7 @@ pulp_settings:
4347
allowed_import_paths: "/tmp"
4448
api_root: "/pulp/"
4549
orphan_protection_time: 0
50+
attestation_verification_key: "/etc/pki/attestation/test-key.pem"
4651
pypi_api_hostname: "https://pulp:443"
4752
pulp_settings_azure:
4853
MEDIA_ROOT: ""

0 commit comments

Comments
 (0)