Repository navigation
Expand file tree
/
Copy pathtest_bom_projectors.py
More file actions
314 lines (280 loc) · 10.4 KB
/
Copy pathtest_bom_projectors.py
File metadata and controls
314 lines (280 loc) · 10.4 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
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
"""P2 out-of-loop projectors — ABI read, CAS GET, byte-stable re-run."""
from __future__ import annotations
import copy
import json
from unittest.mock import MagicMock
import pytest
from flask import Flask
from cats.network.bom import (
fru_purl,
project_bom,
project_data_lot,
project_function_source,
project_structure_runtime,
project_structure_source,
)
from cats.network.bom.documents import FIXED_CREATED
from cats.network.bom.project import MEDIA_CDX, MEDIA_DCAT, MEDIA_SPDX, parse_uv_lock
from cats.network.bom.read import invoice_uri_from_response, require_ni
from cats.network.cas import (
LocatorIndex,
build_manifest_entries,
from_ni,
is_ni_or_digest,
register_cas_routes,
set_ref,
sha256_hex,
to_ni,
)
from cats.network.content_mesh import ContentMesh
FORBIDDEN_BOM_ID = 'FORBIDDEN_BOM_CONTENT_ID'
_IMAGE_HEX = sha256_hex(b'p2-oci-image')
_IMAGE_REF = f'kindest/node@sha256:{_IMAGE_HEX}'
_UV_LOCK = b'''version = 1
[[package]]
name = "numpy"
version = "2.1.0"
'''
def _mesh(tmp_path, monkeypatch) -> ContentMesh:
client = ContentMesh(ipfsClient=MagicMock(), CATS_HOME=str(tmp_path))
monkeypatch.setattr(client, 'ensure_bootstrap_content_store', lambda: None)
return client
def _put_sorted(mesh: ContentMesh, obj: dict) -> str:
return mesh.put_bytes(
(json.dumps(obj, sort_keys=True) + '\n').encode('utf-8')
)
def _fixture_graph(mesh: ContentMesh) -> dict:
src_file = mesh.put_bytes(b'def process_0():\n return 0\n')
lock_id = mesh.put_bytes(_UV_LOCK)
proc_src = _put_sorted(
mesh,
build_manifest_entries(
{'callables.py': src_file, 'uv.lock': lock_id}
),
)
infra_src = _put_sorted(
mesh, build_manifest_entries({'actuator.py': src_file})
)
function = {}
set_ref(function, 'process_source', proc_src)
set_ref(function, 'infrafunction_source', infra_src)
set_ref(function, 'process', mesh.put_bytes(b'{"ingress_subproc_uri":"x"}\n'))
set_ref(function, 'infrafunction', mesh.put_bytes(b'{}\n'))
function_id = _put_sorted(mesh, function)
plant_tf = mesh.put_bytes(b'variable "node_image" {}\n')
plant_src = _put_sorted(mesh, build_manifest_entries({'main.tf': plant_tf}))
infra_tree = _put_sorted(
mesh, build_manifest_entries({'outputs.tf': plant_tf})
)
root_tree = _put_sorted(mesh, build_manifest_entries({'main.tf': plant_tf}))
structure = {}
set_ref(structure, 'root', root_tree)
set_ref(structure, 'plant', plant_src)
set_ref(structure, 'infrastructure', infra_tree)
structure_id = _put_sorted(mesh, structure)
plant_sae = _put_sorted(mesh, {'rebuilt': False, 'kind_cluster_name': 'c'})
infra_sae = _put_sorted(mesh, {'object_store_as_executed_uri': 'http://x'})
sae = {}
set_ref(sae, 'plant_as_executed', plant_sae)
set_ref(sae, 'infrastructure_as_executed', infra_sae)
sae_id = _put_sorted(mesh, sae)
egress = mesh.put_bytes(b'{"lot":"egress"}\n')
integ = mesh.put_bytes(b'{"lot":"integ"}\n')
ingres = mesh.put_bytes(b'{"lot":"in"}\n')
stages = {}
set_ref(stages, 'egressed_data', egress)
set_ref(stages, 'integrated_data', integ)
set_ref(stages, 'ingressed_data', ingres)
stages_id = _put_sorted(mesh, stages)
seed_id = _put_sorted(mesh, {'seed': 'abc', 'rng_seed': 1, 'num_partitions': 1})
input_lot = mesh.put_bytes(b'{"lot":"input"}\n')
input_invoice = {}
set_ref(input_invoice, 'data', input_lot)
input_invoice_id = _put_sorted(mesh, input_invoice)
order = {}
set_ref(order, 'function', function_id)
set_ref(order, 'structure', structure_id)
set_ref(order, 'invoice', input_invoice_id)
order_id = _put_sorted(mesh, order)
invoice = {}
set_ref(invoice, 'order', order_id)
set_ref(invoice, 'data', egress)
set_ref(invoice, 'data_stages', stages_id)
set_ref(invoice, 'seed', seed_id)
set_ref(invoice, 'structure_as_executed', sae_id)
invoice_id = _put_sorted(mesh, invoice)
log_id = _put_sorted(mesh, {'plant_rebuilt': False})
bom = {}
set_ref(bom, 'invoice', invoice_id)
set_ref(bom, 'log', log_id)
envelope = {
'content_id': FORBIDDEN_BOM_ID,
'invoice_uri': 'http://n/inv-resp-ignored',
'bom': bom,
}
return {
'envelope': envelope,
'invoice_id': invoice_id,
'function_id': function_id,
'structure_id': structure_id,
'sae_id': sae_id,
'order_id': order_id,
'input_lot_id': input_lot,
'function': json.loads(mesh.cat(function_id)),
'structure': json.loads(mesh.cat(structure_id)),
'invoice': json.loads(mesh.cat(invoice_id)),
}
def test_invoice_uri_ignores_top_level_execute_field():
record = {'invoice_uri': 'http://n/inv-rec'}
assert (
invoice_uri_from_response(
{'invoice_uri': 'http://n/inv-resp'}, record
)
== 'http://n/inv-rec'
)
assert (
invoice_uri_from_response(
{'bom': {'invoice_uri': 'http://n/inv-bom'}, 'invoice_uri': 'x'},
record,
)
== 'http://n/inv-bom'
)
def test_parse_uv_lock_packages():
assert parse_uv_lock(_UV_LOCK) == [('numpy', '2.1.0')]
assert parse_uv_lock(b'not toml {') == []
def test_project_bom_cas_get_stable_and_no_bom_id(tmp_path, monkeypatch):
mesh = _mesh(tmp_path, monkeypatch)
monkeypatch.setattr(
'cats.network.bom.syft.syft_binary', lambda explicit=None: None
)
monkeypatch.setattr(
'cats.network.bom.syft.docker_image_digest',
lambda ref, timeout=15: (
_IMAGE_HEX if ref == _IMAGE_REF else None
),
)
graph = _fixture_graph(mesh)
before = copy.deepcopy(graph['envelope'])
first = project_bom(
graph['envelope'],
mesh,
image_refs=[_IMAGE_REF],
created=FIXED_CREATED,
)
assert graph['envelope'] == before
second = project_bom(
graph['envelope'],
mesh,
image_refs=[_IMAGE_REF],
created=FIXED_CREATED,
)
for key in (
'function_spdx',
'structure_spdx',
'runtime_cdx',
'data_contract',
'data_dcat',
'data_spdx',
):
assert is_ni_or_digest(first[key])
assert first[key] == second[key]
assert first.get('runtime_syft') is None
app = Flask(__name__)
register_cas_routes(app, cats_home=str(tmp_path))
client = app.test_client()
loc = LocatorIndex(str(tmp_path))
media = {
'function_spdx': MEDIA_SPDX,
'structure_spdx': MEDIA_SPDX,
'runtime_cdx': MEDIA_CDX,
'data_contract': MEDIA_DCAT,
'data_dcat': MEDIA_DCAT,
'data_spdx': MEDIA_SPDX,
}
for key, media_type in media.items():
ni = first[key]
hex_digest = from_ni(ni)
resp = client.get(f'/ldp/cas/{hex_digest}')
assert resp.status_code == 200
stored = mesh.catObj(ni)
assert resp.data == stored
body = json.loads(resp.data.decode('utf-8'))
dumped = json.dumps(body)
assert FORBIDDEN_BOM_ID not in dumped
locators = loc.get(ni)
assert locators is not None
assert locators['locators'][0]['uri'].endswith(f'/ldp/cas/{hex_digest}')
assert locators['locators'][0].get('media_type') == media_type
function_doc = json.loads(mesh.cat(first['function_spdx']))
assert fru_purl('function', graph['function_id']) in json.dumps(function_doc)
assert fru_purl('pypi', name='numpy', version='2.1.0') in json.dumps(
function_doc
)
structure_doc = json.loads(mesh.cat(first['structure_spdx']))
assert fru_purl('structure', graph['structure_id']) in json.dumps(
structure_doc
)
cdx = json.loads(mesh.cat(first['runtime_cdx']))
assert cdx['bomFormat'] == 'CycloneDX'
assert cdx['specVersion'] == '1.6'
assert fru_purl('oci', _IMAGE_HEX, name='kindest/node') in json.dumps(cdx)
dcat = json.loads(mesh.cat(first['data_dcat']))
assert dcat['@type'] == 'dcat:Catalog'
contract = json.loads(mesh.cat(first['data_contract']))
assert contract['@type'] == 'odcs:DataContract'
assert dcat.get('dct:conformsTo') == contract['@id']
assert contract['cats:subject'] == require_ni(
graph['input_lot_id'], label='data_id'
)
dataset = json.loads(mesh.cat(first['data_spdx']))
assert 'dataset' in dataset['profileConformance']
def test_syft_missing_still_emits_cdx(tmp_path, monkeypatch):
mesh = _mesh(tmp_path, monkeypatch)
monkeypatch.setattr(
'cats.network.bom.syft.syft_binary', lambda explicit=None: None
)
monkeypatch.setattr(
'cats.network.bom.syft.docker_image_digest', lambda ref, timeout=15: None
)
sae_id = mesh.put_bytes(b'{"rebuilt":false}\n')
result = project_structure_runtime(
{},
mesh,
structure_as_executed_id=sae_id,
image_refs=['missing.example/img:latest'],
)
assert is_ni_or_digest(result['runtime_cdx'])
assert result.get('runtime_syft') is None
doc = json.loads(mesh.cat(result['runtime_cdx']))
assert doc['components'] == []
def test_individual_projectors_and_network_not_exported(tmp_path, monkeypatch):
mesh = _mesh(tmp_path, monkeypatch)
monkeypatch.setattr(
'cats.network.bom.syft.syft_binary', lambda explicit=None: None
)
graph = _fixture_graph(mesh)
fn = project_function_source(
graph['function'], mesh, function_id=graph['function_id']
)
st = project_structure_source(
graph['structure'], mesh, structure_id=graph['structure_id']
)
lots = project_data_lot(
graph['invoice'], mesh, invoice_id=graph['invoice_id']
)
assert is_ni_or_digest(fn)
assert is_ni_or_digest(st)
assert is_ni_or_digest(lots['data_dcat'])
assert is_ni_or_digest(lots['data_contract'])
import cats.network as network
assert not hasattr(network, 'project_bom')
assert not hasattr(network, 'fru_purl')
assert not hasattr(network, 'attach_runtime_sbom')
assert not hasattr(network, 'attach_ebom_stems')
assert not hasattr(network, 'project_input_data')
assert not hasattr(network, 'project_sysml_quantum')
assert not hasattr(network, 'project_allocate_view')
def test_require_signed_invoice_uri(tmp_path, monkeypatch):
mesh = _mesh(tmp_path, monkeypatch)
with pytest.raises(ValueError, match='invoice_uri'):
project_bom({'invoice_uri': 'http://n/inv-resp', 'bom': {}}, mesh)