forked from pypi/warehouse
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_services.py
342 lines (286 loc) · 11.9 KB
/
test_services.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import pretend
import pytest
from pydantic import TypeAdapter
from pypi_attestations import (
Attestation,
AttestationType,
GitHubPublisher,
GitLabPublisher,
Provenance,
VerificationError,
)
from sigstore.verify import Verifier
from zope.interface.verify import verifyClass
from tests.common.db.oidc import GitHubPublisherFactory, GitLabPublisherFactory
from tests.common.db.packaging import FileFactory
from warehouse.attestations import IIntegrityService, services
from warehouse.attestations.errors import AttestationUploadError
from warehouse.attestations.models import Provenance as DatabaseProvenance
class TestNullIntegrityService:
def test_interface_matches(self):
assert verifyClass(IIntegrityService, services.NullIntegrityService)
def test_build_provenance(self, db_request, dummy_attestation):
db_request.oidc_publisher = pretend.stub(
publisher_name="GitHub",
repository="fake/fake",
workflow_filename="fake.yml",
environment="fake",
)
file = FileFactory.create()
service = services.NullIntegrityService.create_service(None, db_request)
provenance = service.build_provenance(db_request, file, [dummy_attestation])
assert isinstance(provenance, DatabaseProvenance)
assert provenance.file == file
assert file.provenance == provenance
def test_parse_attestations(self, db_request, monkeypatch):
service = services.NullIntegrityService.create_service(None, db_request)
extract_attestations_from_request = pretend.call_recorder(lambda r: None)
monkeypatch.setattr(
services,
"_extract_attestations_from_request",
extract_attestations_from_request,
)
service.parse_attestations(db_request, pretend.stub())
assert extract_attestations_from_request.calls == [pretend.call(db_request)]
class TestIntegrityService:
def test_interface_matches(self):
assert verifyClass(IIntegrityService, services.IntegrityService)
def test_create_service(self, db_request):
service = services.IntegrityService.create_service(None, db_request)
assert isinstance(service, services.IntegrityService)
def test_parse_attestations_fails_no_publisher(self, db_request):
integrity_service = services.IntegrityService(
metrics=pretend.stub(),
session=db_request.db,
)
db_request.oidc_publisher = None
with pytest.raises(
AttestationUploadError,
match="Attestations are only supported when using Trusted Publishing",
):
integrity_service.parse_attestations(db_request, pretend.stub())
def test_parse_attestations_fails_unsupported_publisher(self, db_request):
integrity_service = services.IntegrityService(
metrics=pretend.stub(), session=db_request.db
)
db_request.oidc_publisher = pretend.stub(
supports_attestations=False, publisher_name="fake"
)
with pytest.raises(
AttestationUploadError,
match="Attestations are not currently supported with fake publishers",
):
integrity_service.parse_attestations(db_request, pretend.stub())
def test_parse_attestations_fails_malformed_attestation(self, metrics, db_request):
integrity_service = services.IntegrityService(
metrics=metrics,
session=db_request.db,
)
db_request.oidc_publisher = pretend.stub(supports_attestations=True)
db_request.POST["attestations"] = "{'malformed-attestation'}"
with pytest.raises(
AttestationUploadError,
match="Malformed attestations",
):
integrity_service.parse_attestations(db_request, pretend.stub())
assert (
pretend.call("warehouse.upload.attestations.malformed")
in metrics.increment.calls
)
def test_parse_attestations_fails_multiple_attestations(
self, metrics, db_request, dummy_attestation
):
integrity_service = services.IntegrityService(
metrics=metrics,
session=db_request.db,
)
db_request.oidc_publisher = pretend.stub(supports_attestations=True)
db_request.POST["attestations"] = TypeAdapter(list[Attestation]).dump_json(
[dummy_attestation, dummy_attestation]
)
with pytest.raises(
AttestationUploadError, match="Only a single attestation per file"
):
integrity_service.parse_attestations(
db_request,
pretend.stub(),
)
assert (
pretend.call("warehouse.upload.attestations.failed_multiple_attestations")
in metrics.increment.calls
)
@pytest.mark.parametrize(
("verify_exception", "expected_message"),
[
(
VerificationError,
"Could not verify the uploaded",
),
(
ValueError,
"Unknown error while",
),
],
)
def test_parse_attestations_fails_verification(
self,
metrics,
monkeypatch,
db_request,
dummy_attestation,
verify_exception,
expected_message,
):
integrity_service = services.IntegrityService(
metrics=metrics,
session=db_request.db,
)
db_request.oidc_publisher = pretend.stub(
supports_attestations=True,
publisher_verification_policy=pretend.call_recorder(lambda c: None),
)
db_request.oidc_claims = {"sha": "somesha"}
db_request.POST["attestations"] = TypeAdapter(list[Attestation]).dump_json(
[dummy_attestation]
)
def failing_verify(_self, _verifier, _policy, _dist):
raise verify_exception("error")
monkeypatch.setattr(Verifier, "production", lambda: pretend.stub())
monkeypatch.setattr(Attestation, "verify", failing_verify)
with pytest.raises(AttestationUploadError, match=expected_message):
integrity_service.parse_attestations(
db_request,
pretend.stub(),
)
def test_parse_attestations_fails_wrong_predicate(
self,
metrics,
monkeypatch,
db_request,
dummy_attestation,
):
integrity_service = services.IntegrityService(
metrics=metrics,
session=db_request.db,
)
db_request.oidc_publisher = pretend.stub(
supports_attestations=True,
publisher_verification_policy=pretend.call_recorder(lambda c: None),
)
db_request.oidc_claims = {"sha": "somesha"}
db_request.POST["attestations"] = TypeAdapter(list[Attestation]).dump_json(
[dummy_attestation]
)
monkeypatch.setattr(Verifier, "production", lambda: pretend.stub())
monkeypatch.setattr(
Attestation, "verify", lambda *args: ("wrong-predicate", {})
)
with pytest.raises(
AttestationUploadError, match="Attestation with unsupported predicate"
):
integrity_service.parse_attestations(
db_request,
pretend.stub(),
)
assert (
pretend.call(
"warehouse.upload.attestations.failed_unsupported_predicate_type"
)
in metrics.increment.calls
)
def test_parse_attestations_succeeds(
self, metrics, monkeypatch, db_request, dummy_attestation
):
integrity_service = services.IntegrityService(
metrics=metrics,
session=db_request.db,
)
db_request.oidc_publisher = pretend.stub(
supports_attestations=True,
publisher_verification_policy=pretend.call_recorder(lambda c: None),
)
db_request.oidc_claims = {"sha": "somesha"}
db_request.POST["attestations"] = TypeAdapter(list[Attestation]).dump_json(
[dummy_attestation]
)
monkeypatch.setattr(Verifier, "production", lambda: pretend.stub())
monkeypatch.setattr(
Attestation, "verify", lambda *args: (AttestationType.PYPI_PUBLISH_V1, {})
)
attestations = integrity_service.parse_attestations(
db_request,
pretend.stub(),
)
assert attestations == [dummy_attestation]
def test_build_provenance_fails_unsupported_publisher(
self, db_request, dummy_attestation
):
integrity_service = services.IntegrityService(
metrics=pretend.stub(),
session=db_request.db,
)
db_request.oidc_publisher = pretend.stub(publisher_name="not-existing")
file = FileFactory.create()
with pytest.raises(AttestationUploadError, match="Unsupported publisher"):
integrity_service.build_provenance(db_request, file, [dummy_attestation])
# If building provenance fails, nothing is stored or associated with the file
assert not file.provenance
@pytest.mark.parametrize(
"publisher_factory",
[
GitHubPublisherFactory,
GitLabPublisherFactory,
],
)
def test_build_provenance_succeeds(
self, db_request, publisher_factory, dummy_attestation
):
db_request.oidc_publisher = publisher_factory.create()
integrity_service = services.IntegrityService(
metrics=pretend.stub(),
session=db_request.db,
)
file = FileFactory.create()
assert file.provenance is None
provenance = integrity_service.build_provenance(
db_request, file, [dummy_attestation]
)
assert file.provenance == provenance
model = Provenance.model_validate(provenance.provenance)
assert model.attestation_bundles[0].attestations == [dummy_attestation]
def test_publisher_from_oidc_publisher_succeeds_github(db_request):
publisher = GitHubPublisherFactory.create()
attestation_publisher = services._publisher_from_oidc_publisher(publisher)
assert isinstance(attestation_publisher, GitHubPublisher)
assert attestation_publisher.repository == publisher.repository
assert attestation_publisher.workflow == publisher.workflow_filename
assert attestation_publisher.environment == publisher.environment
def test_publisher_from_oidc_publisher_succeeds_gitlab(db_request):
publisher = GitLabPublisherFactory.create()
attestation_publisher = services._publisher_from_oidc_publisher(publisher)
assert isinstance(attestation_publisher, GitLabPublisher)
assert attestation_publisher.repository == publisher.project_path
assert attestation_publisher.environment == publisher.environment
def test_publisher_from_oidc_publisher_fails_unsupported():
publisher = pretend.stub(publisher_name="not-existing")
with pytest.raises(AttestationUploadError):
services._publisher_from_oidc_publisher(publisher)
def test_extract_attestations_from_request_empty_list(db_request):
db_request.oidc_publisher = GitHubPublisherFactory.create()
db_request.POST = {"attestations": json.dumps([])}
with pytest.raises(
AttestationUploadError, match="an empty attestation set is not permitted"
):
services._extract_attestations_from_request(db_request)