-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathtest_sbom.py
More file actions
287 lines (244 loc) · 9.86 KB
/
Copy pathtest_sbom.py
File metadata and controls
287 lines (244 loc) · 9.86 KB
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
import json
import pathlib
import typing
from conftest import make_sbom_ctx
from packaging.requirements import Requirement
from packaging.version import Version
from spdx_tools.spdx.parser.jsonlikedict.json_like_dict_parser import (
JsonLikeDictParser,
)
from spdx_tools.spdx.validation.document_validator import validate_full_spdx_document
from fromager import sbom
from fromager.packagesettings import SbomSettings
def _validate_spdx(doc: dict[str, typing.Any]) -> None:
"""Validate an SBOM dict against the SPDX 2.3 spec using spdx-tools."""
parsed = JsonLikeDictParser().parse(doc)
errors = validate_full_spdx_document(parsed, spdx_version="SPDX-2.3")
assert not errors, "\n".join(e.validation_message for e in errors)
def test_generate_sbom_structure(tmp_path: pathlib.Path) -> None:
"""Verify the generated SBOM has the required SPDX 2.3 fields."""
ctx = make_sbom_ctx(tmp_path, sbom_settings=SbomSettings())
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("example-pkg==1.2.3"),
version=Version("1.2.3"),
)
assert doc["spdxVersion"] == "SPDX-2.3"
assert doc["dataLicense"] == "CC0-1.0"
assert doc["SPDXID"] == "SPDXRef-DOCUMENT"
assert doc["name"] == "example-pkg-1.2.3"
assert "documentNamespace" in doc
assert "creationInfo" in doc
assert doc["creationInfo"]["created"]
assert any("fromager" in c for c in doc["creationInfo"]["creators"])
_validate_spdx(doc)
def test_generate_sbom_default_purls(tmp_path: pathlib.Path) -> None:
"""Verify default purls use pkg:pypi without qualifiers."""
ctx = make_sbom_ctx(tmp_path, sbom_settings=SbomSettings())
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("my-package==2.0.0"),
version=Version("2.0.0"),
)
wheel = doc["packages"][0]
upstream = doc["packages"][1]
assert wheel["externalRefs"][0]["referenceLocator"] == "pkg:pypi/my-package@2.0.0"
assert (
upstream["externalRefs"][0]["referenceLocator"] == "pkg:pypi/my-package@2.0.0"
)
_validate_spdx(doc)
def test_generate_sbom_repository_url_qualifier(tmp_path: pathlib.Path) -> None:
"""Verify global repository_url adds qualifier to downstream but not upstream."""
settings = SbomSettings(
repository_url="https://packages.redhat.com", # type: ignore[arg-type]
)
ctx = make_sbom_ctx(tmp_path, sbom_settings=settings)
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("numpy==1.26.0"),
version=Version("1.26.0"),
)
wheel = doc["packages"][0]
upstream = doc["packages"][1]
assert wheel["externalRefs"][0]["referenceLocator"] == (
"pkg:pypi/numpy@1.26.0?repository_url=https://packages.redhat.com"
)
assert upstream["externalRefs"][0]["referenceLocator"] == "pkg:pypi/numpy@1.26.0"
_validate_spdx(doc)
def test_generate_sbom_custom_settings(tmp_path: pathlib.Path) -> None:
"""Verify custom supplier, namespace, and creators are used."""
settings = SbomSettings(
supplier="Organization: ExampleCo",
namespace="https://www.example.com", # type: ignore[arg-type]
creators=["Organization: ExampleCo"],
)
ctx = make_sbom_ctx(tmp_path, sbom_settings=settings)
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("my-package==2.0.0"),
version=Version("2.0.0"),
)
wheel = doc["packages"][0]
assert wheel["supplier"] == "Organization: ExampleCo"
assert doc["documentNamespace"] == (
"https://www.example.com/my-package-2.0.0.spdx.json"
)
creators = doc["creationInfo"]["creators"]
assert "Organization: ExampleCo" in creators
assert any("fromager" in c for c in creators)
_validate_spdx(doc)
def test_generate_sbom_purl_field_overrides(tmp_path: pathlib.Path) -> None:
"""Verify individual purl field overrides work."""
ctx = make_sbom_ctx(
tmp_path,
sbom_settings=SbomSettings(),
package_overrides={"purl": {"type": "generic", "name": "custom-name"}},
)
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("test-pkg==1.0.0"),
version=Version("1.0.0"),
)
wheel = doc["packages"][0]
upstream = doc["packages"][1]
assert wheel["externalRefs"][0]["referenceLocator"] == (
"pkg:generic/custom-name@1.0.0"
)
# Field overrides carry through to upstream (without qualifiers)
assert upstream["externalRefs"][0]["referenceLocator"] == (
"pkg:generic/custom-name@1.0.0"
)
_validate_spdx(doc)
def test_generate_sbom_package_repository_url_override(tmp_path: pathlib.Path) -> None:
"""Verify per-package repository_url overrides the global value."""
ctx = make_sbom_ctx(
tmp_path,
sbom_settings=SbomSettings(
repository_url="https://packages.redhat.com", # type: ignore[arg-type]
),
package_overrides={
"purl": {"repository_url": "https://mirror.example.com/simple"},
},
)
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("test-pkg==1.0.0"),
version=Version("1.0.0"),
)
wheel = doc["packages"][0]
upstream = doc["packages"][1]
assert wheel["externalRefs"][0]["referenceLocator"] == (
"pkg:pypi/test-pkg@1.0.0?repository_url=https://mirror.example.com/simple"
)
# Upstream never gets repository_url
assert upstream["externalRefs"][0]["referenceLocator"] == "pkg:pypi/test-pkg@1.0.0"
_validate_spdx(doc)
def test_generate_sbom_upstream_purl_override(tmp_path: pathlib.Path) -> None:
"""Verify upstream purl override for GitHub-sourced packages."""
ctx = make_sbom_ctx(
tmp_path,
sbom_settings=SbomSettings(
repository_url="https://packages.redhat.com", # type: ignore[arg-type]
),
package_overrides={
"purl": {"upstream": "pkg:github/vllm-project/bart-plugin@v0.2.0"},
},
)
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("test-pkg==0.2.0"),
version=Version("0.2.0"),
)
wheel = doc["packages"][0]
upstream = doc["packages"][1]
# Downstream has repository_url qualifier
assert (
"repository_url=https://packages.redhat.com"
in (wheel["externalRefs"][0]["referenceLocator"])
)
# Upstream identity comes from the override purl
assert upstream["name"] == "bart-plugin"
assert upstream["versionInfo"] == "v0.2.0"
assert upstream["externalRefs"][0]["referenceLocator"] == (
"pkg:github/vllm-project/bart-plugin@v0.2.0"
)
_validate_spdx(doc)
def test_generate_sbom_canonicalizes_name(tmp_path: pathlib.Path) -> None:
"""Verify package name is canonicalized per PEP 503."""
ctx = make_sbom_ctx(tmp_path, sbom_settings=SbomSettings())
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("My_Package==1.0.0"),
version=Version("1.0.0"),
)
wheel = doc["packages"][0]
assert wheel["name"] == "my-package"
assert doc["name"] == "my-package-1.0.0"
assert "pkg:pypi/my-package@1.0.0" in (wheel["externalRefs"][0]["referenceLocator"])
_validate_spdx(doc)
def test_generate_sbom_relationships(tmp_path: pathlib.Path) -> None:
"""Verify DESCRIBES and GENERATED_FROM relationships."""
ctx = make_sbom_ctx(tmp_path, sbom_settings=SbomSettings())
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("test==0.1.0"),
version=Version("0.1.0"),
)
rels = doc["relationships"]
assert len(rels) == 2
assert rels[0]["spdxElementId"] == "SPDXRef-DOCUMENT"
assert rels[0]["relationshipType"] == "DESCRIBES"
assert rels[0]["relatedSpdxElement"] == "SPDXRef-wheel"
assert rels[1]["spdxElementId"] == "SPDXRef-wheel"
assert rels[1]["relationshipType"] == "GENERATED_FROM"
assert rels[1]["relatedSpdxElement"] == "SPDXRef-upstream"
_validate_spdx(doc)
def test_generate_sbom_upstream_supplier(tmp_path: pathlib.Path) -> None:
"""Verify upstream package always has supplier NOASSERTION."""
settings = SbomSettings(supplier="Organization: Red Hat")
ctx = make_sbom_ctx(tmp_path, sbom_settings=settings)
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("numpy==1.26.0"),
version=Version("1.26.0"),
)
wheel = doc["packages"][0]
upstream = doc["packages"][1]
assert wheel["supplier"] == "Organization: Red Hat"
assert upstream["supplier"] == "NOASSERTION"
_validate_spdx(doc)
def test_write_sbom_creates_file(tmp_path: pathlib.Path) -> None:
"""Verify write_sbom creates sboms/ dir and writes valid JSON."""
dist_info_dir = tmp_path / "pkg-1.0.dist-info"
dist_info_dir.mkdir()
ctx = make_sbom_ctx(tmp_path, sbom_settings=SbomSettings())
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("pkg==1.0"),
version=Version("1.0"),
)
result = sbom.write_sbom(sbom=doc, dist_info_dir=dist_info_dir)
assert result == dist_info_dir / "sboms" / "fromager.spdx.json"
assert result.exists()
content = json.loads(result.read_text())
assert content["spdxVersion"] == "SPDX-2.3"
_validate_spdx(content)
def test_write_sbom_preserves_existing_files(tmp_path: pathlib.Path) -> None:
"""Verify write_sbom does not overwrite existing SBOM files."""
dist_info_dir = tmp_path / "pkg-1.0.dist-info"
sboms_dir = dist_info_dir / "sboms"
sboms_dir.mkdir(parents=True)
existing = sboms_dir / "cyclonedx.json"
existing.write_text('{"bomFormat": "CycloneDX"}')
ctx = make_sbom_ctx(tmp_path, sbom_settings=SbomSettings())
doc = sbom.generate_sbom(
ctx=ctx,
req=Requirement("pkg==1.0"),
version=Version("1.0"),
)
sbom.write_sbom(sbom=doc, dist_info_dir=dist_info_dir)
# Existing file should be untouched
assert existing.exists()
assert json.loads(existing.read_text())["bomFormat"] == "CycloneDX"
# New file should also exist
assert (sboms_dir / "fromager.spdx.json").exists()