-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.py
More file actions
555 lines (480 loc) · 19.8 KB
/
registry.py
File metadata and controls
555 lines (480 loc) · 19.8 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import base64
import copy
import hashlib
import json
import logging
import os
import sys
import tarfile
import uuid
from enum import Enum, auto
from platform import architecture
from typing import Optional, Tuple
import jsonschema
import oras.auth
import oras.client
import oras.defaults
import oras.oci
import oras.provider
import oras.utils
from parse_features import get_oci_metadata_from_fileset
import requests
from oras.container import Container as OrasContainer
from oras.decorator import ensure_container
from oras.provider import Registry
from oras.schemas import manifest as oras_manifest_schema
from crypto import (
calculate_sha256,
verify_sha256,
)
from schemas import (
EmptyIndex,
EmptyManifestMetadata,
EmptyPlatform,
)
from schemas import index as indexSchema
class ManifestState(Enum):
Incomplete = auto()
Complete = auto()
Final = auto()
logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
annotation_signature_key = "io.gardenlinux.oci.signature"
annotation_signed_string_key = "io.gardenlinux.oci.signed-string"
def get_image_state(manifest: dict) -> str:
if "annotations" not in manifest:
logger.warning("No annotations set for manifest.")
return "UNDEFINED"
if "image_state" not in manifest["annotations"]:
logger.warning("No image_state set for manifest.")
return "UNDEFINED"
return manifest["annotations"]["image_state"]
def NewPlatform(architecture: str, version: str) -> dict:
platform = copy.deepcopy(EmptyPlatform)
platform["architecture"] = architecture
platform["os.version"] = version
return platform
def NewManifestMetadata(
digest: str, size: int, annotations: dict, platform_data: dict
) -> dict:
manifest_meta_data = copy.deepcopy(EmptyManifestMetadata)
manifest_meta_data["mediaType"] = "application/vnd.oci.image.manifest.v1+json"
manifest_meta_data["digest"] = digest
manifest_meta_data["size"] = size
manifest_meta_data["annotations"] = annotations
manifest_meta_data["platform"] = platform_data
return manifest_meta_data
def NewIndex() -> dict:
index = copy.deepcopy(EmptyIndex)
index["mediaType"] = "application/vnd.oci.image.index.v1+json"
return index
def create_config_from_dict(conf: dict, annotations: dict) -> Tuple[dict, str]:
"""
Write a new OCI configuration to file, and generate oci metadata for it
For reference see https://github.com/opencontainers/image-spec/blob/main/config.md
annotations, mediatype, size, digest are not part of digest and size calculation,
and therefore must be attached to the output dict and not written to the file.
:param conf: dict with custom configuration (the payload of the configuration)
:param annotations: dict with custom annotations to be attached to metadata part of config
"""
config_path = os.path.join(os.path.curdir, str(uuid.uuid4()))
with open(config_path, "w") as fp:
json.dump(conf, fp)
conf["annotations"] = annotations
conf["mediaType"] = oras.defaults.unknown_config_media_type
conf["size"] = oras.utils.get_size(config_path)
conf["digest"] = f"sha256:{oras.utils.get_file_hash(config_path)}"
return conf, config_path
def construct_manifest_entry_signed_data_string(
cname: str, version: str, new_manifest_metadata: dict, architecture: str
) -> str:
data_to_sign = (
f"version:{version} cname:{cname} architecture:{architecture} manifest-size"
f":{new_manifest_metadata['size']} manifest-digest:{new_manifest_metadata['digest']}"
)
return data_to_sign
def construct_layer_signed_data_string(
cname: str, version: str, architecture: str, media_type: str, checksum_sha256: str
) -> str:
data_to_sign = f"version:{version} cname:{cname} architecture:{architecture} media_type:{media_type} digest:{checksum_sha256}"
return data_to_sign
class GlociRegistry(Registry):
def __init__(
self,
container_name: str,
insecure: bool = False,
token: Optional[str] = None,
config_path: Optional[str] = None,
):
super().__init__(auth_backend="token", insecure=insecure)
self.container = OrasContainer(container_name)
self.container_name = container_name
self.registry_url = self.container.registry
self.config_path = config_path
if not token:
logger.info("No Token provided.")
else:
self.token = base64.b64encode(token.encode("utf-8")).decode("utf-8")
self.auth.set_token_auth(self.token)
@ensure_container
def get_manifest_json(
self, container: OrasContainer, allowed_media_type: Optional[list[str]] = None
):
if not allowed_media_type:
default_image_index_media_type = (
"application/vnd.oci.image.manifest.v1+json"
)
allowed_media_type = [default_image_index_media_type]
# self.load_configs(container)
headers = {"Accept": ";".join(allowed_media_type)}
headers.update(self.headers)
get_manifest = f"{self.prefix}://{container.manifest_url()}"
response = self.do_request(get_manifest, "GET", headers=headers)
self._check_200_response(response)
return response
@ensure_container
def get_manifest_size(
self, container: OrasContainer, allowed_media_type: Optional[list[str]] = None
):
response = self.get_manifest_json(container, allowed_media_type)
if response is None:
return 0
return len(response.content)
@ensure_container
def get_digest(
self, container: OrasContainer, allowed_media_type: Optional[list[str]] = None
):
response = self.get_manifest_json(container, allowed_media_type)
if response is None:
return ""
return f"sha256:{hashlib.sha256(response.content).hexdigest()}"
def get_index(self, allowed_media_type: Optional[list[str]] = None):
"""
Returns the manifest for a cname+arch combination of a container
Will return None if no result was found
TODO: refactor: use get_manifest_json and call it with index mediatype.
"""
if not allowed_media_type:
default_image_index_media_type = "application/vnd.oci.image.index.v1+json"
allowed_media_type = [default_image_index_media_type]
headers = {"Accept": ";".join(allowed_media_type)}
manifest_url = f"{self.prefix}://{self.container.manifest_url()}"
response = self.do_request(manifest_url, "GET", headers=headers)
try:
self._check_200_response(response)
index = response.json()
return index
except ValueError:
logger.info("Index not found, creating new Index!")
return NewIndex()
@ensure_container
def get_manifest_meta_data_by_cname(
self,
container: OrasContainer,
cname: str,
version: str,
arch: str,
allowed_media_type: Optional[list[str]] = None,
):
"""
Returns the manifest for a cname+arch combination of a container
Will return None if no result was found
"""
index = self.get_index(allowed_media_type=allowed_media_type)
if "manifests" not in index:
logger.debug("Index is empty")
return None
for manifest_meta in index["manifests"]:
# Annotations are optional:
# https://github.com/opencontainers/image-spec/blob/v1.0.1/descriptor.md#properties
if "annotations" in manifest_meta:
if (
"cname" in manifest_meta["annotations"]
and "architecture" in manifest_meta["annotations"]
and "os.version" in manifest_meta["platform"]
and manifest_meta["annotations"]["cname"] == cname
and manifest_meta["annotations"]["architecture"] == arch
and manifest_meta["platform"]["os.version"] == version
):
return manifest_meta
return None
@ensure_container
def get_manifest_by_digest(
self,
container: OrasContainer,
digest: str,
allowed_media_type: Optional[list[str]] = None,
):
if not allowed_media_type:
default_image_manifest_media_type = (
"application/vnd.oci.image.manifest.v1+json"
)
allowed_media_type = [default_image_manifest_media_type]
manifest_url = f"{self.prefix}://{container.get_blob_url(digest)}".replace(
"/blobs/", "/manifests/"
)
headers = {"Accept": ";".join(allowed_media_type)}
response = self.do_request(manifest_url, "GET", headers=headers, stream=False)
self._check_200_response(response)
manifest = response.json()
verify_sha256(digest, response.content)
jsonschema.validate(manifest, schema=oras_manifest_schema)
return manifest
@ensure_container
def get_manifest_by_cname(
self,
container: OrasContainer,
cname: str,
version: str,
arch: str,
allowed_media_type: Optional[list[str]] = None,
):
"""
Returns the manifest for a cname+arch combination of a container
Will return None if no result was found
"""
if not allowed_media_type:
default_image_manifest_media_type = (
"application/vnd.oci.image.manifest.v1+json"
)
allowed_media_type = [default_image_manifest_media_type]
manifest_meta = self.get_manifest_meta_data_by_cname(
container, cname, version, arch
)
if manifest_meta is None:
logger.error(f"No manifest found for {cname}-{arch}")
return None
if "digest" not in manifest_meta:
logger.error("No digest found in metadata!")
manifest_digest = manifest_meta["digest"]
return self.get_manifest_by_digest(
container, manifest_digest, allowed_media_type=allowed_media_type
)
def change_state(self, cname: str, version: str, architecture: str, new_state: str):
manifest_container = OrasContainer(
f"{self.container_name}-{cname}-{architecture}"
)
manifest = self.get_manifest_by_cname(
manifest_container, cname, version, architecture
)
if "annotations" not in manifest:
logger.warning("No annotations found in manifest, init annotations now.")
manifest["annotations"] = {}
@ensure_container
def remove_container(self, container: OrasContainer):
self.delete_tag(container.manifest_url())
def status_all(self):
"""
Validate if container is valid
- all manifests require a info.yaml in the layers
- info.yaml needs to be signed (TODO)
- all layers listed in info.yaml must exist
- all mediatypes of layers listed in info.yaml must be set correctly
"""
index = self.get_index()
if "manifests" not in index:
logger.info("No manifests in index")
return
for manifest_meta in index["manifests"]:
manifest_digest = manifest_meta["digest"]
manifest = self.get_manifest_by_digest(self.container, manifest_digest)
image_state = get_image_state(manifest)
print(f"{manifest_digest}:\t{image_state}")
def upload_index(self, index: dict) -> requests.Response:
jsonschema.validate(index, schema=indexSchema)
headers = {
"Content-Type": "application/vnd.oci.image.index.v1+json",
"Content-Length": str(len(index)),
}
tag = self.container.digest or self.container.tag
index_url = (
f"{self.container.registry}/v2/{self.container.api_prefix}/manifests/{tag}"
)
response = self.do_request(
f"{self.prefix}://{index_url}", # noqa
"PUT",
headers=headers,
json=index,
)
return response
def push_image_manifest(
self,
architecture: str,
cname: str,
version: str,
build_artifacts_dir: str,
oci_metadata: list,
feature_set: str,
manifest_file: str,
):
"""
creates and pushes an image manifest
:param oci_metadata: a list of filenames and their OCI metadata, can be constructed with
parse_features.get_oci_metadata or parse_features.get_oci_metadata_from_fileset
:param str architecture: target architecture of the image
:param str cname: canonical name of the target image
:param str build_artifacts_dir: directory where the build artifacts are located
:param str feature_set: the expanded list of the included features of this manifest. It will be set in the
manifest itself and in the index entry for this manifest
:returns the digest of the pushed manifest
"""
# TODO: construct oci_artifacts default data
manifest_image = oras.oci.NewManifest()
total_size = 0
# For each file, create sign, attach and push a layer
for artifact in oci_metadata:
annotations_input = artifact["annotations"]
media_type = artifact["media_type"]
file_path = os.path.join(build_artifacts_dir, artifact["file_name"])
if not os.path.exists(file_path):
raise ValueError(f"{file_path} does not exist.")
cleanup_blob = False
if os.path.isdir(file_path):
file_path = oras.utils.make_targz(file_path)
cleanup_blob = True
# Create and sign layer information
layer = self.create_layer(
file_path, cname, version, architecture, media_type
)
total_size += int(layer["size"])
if annotations_input:
layer["annotations"].update(annotations_input)
# Attach this layer to the manifest that is currently created (and pushed later)
manifest_image["layers"].append(layer)
logger.debug(f"Layer: {layer}")
# Push
response = self.upload_blob(file_path, self.container, layer)
self._check_200_response(response)
logger.info(f"Pushed {artifact["file_name"]} {layer["digest"]}")
if cleanup_blob and os.path.exists(file_path):
os.remove(file_path)
# This ends up in the manifest
manifest_image["annotations"] = {}
manifest_image["annotations"]["version"] = version
manifest_image["annotations"]["cname"] = cname
manifest_image["annotations"]["architecture"] = architecture
manifest_image["annotations"]["feature_set"] = feature_set
description = (
f"Image: {cname} "
f"Architecture: {architecture} "
f"Features: {feature_set}"
)
manifest_image["annotations"][
"org.opencontainers.image.description"
] = description
config_annotations = {"cname": cname, "architecture": architecture}
conf, config_file = create_config_from_dict(dict(), config_annotations)
response = self.upload_blob(config_file, self.container, conf)
os.remove(config_file)
self._check_200_response(response)
manifest_image["config"] = conf
manifest_container = OrasContainer(
f"{self.container_name}-{cname}-{architecture}"
)
local_digest = f"sha256:{hashlib.sha256(json.dumps(manifest_image).encode('utf-8')).hexdigest()}"
self._check_200_response(
self.upload_manifest(manifest_image, manifest_container)
)
logger.info(f"Successfully pushed {self.container} {local_digest}")
# This ends up in the index-entry for the manifest
metadata_annotations = {"cname": cname, "architecture": architecture}
metadata_annotations["feature_set"] = feature_set
manifest_digest = self.get_digest(manifest_container)
if manifest_digest != local_digest:
raise ValueError("local and remotely calculated digests do not match")
manifest_index_metadata = NewManifestMetadata(
manifest_digest,
self.get_manifest_size(manifest_container),
metadata_annotations,
NewPlatform(architecture, version),
)
print(json.dumps(manifest_index_metadata), file=open(manifest_file, "w"))
logger.info(f"Index entry written to {manifest_file}")
return local_digest
def update_index(self, manifest_folder):
index = self.get_index()
new_entries = 0
for file in os.listdir(manifest_folder):
manifest_metadata = json.loads(
open(manifest_folder + "/" + file, "r").read()
)
# Skip if manifest with same digest already exists
found = False
for entry in index["manifests"]:
if entry["digest"] == manifest_metadata["digest"]:
found = True
break
if found:
logger.info(
f"Skipping manifest with digest {manifest_metadata["digest"]} - already exists"
)
continue
index["manifests"].append(manifest_metadata)
logger.info(
f"Index appended locally {manifest_metadata["annotations"]["cname"]}"
)
new_entries += 1
self._check_200_response(self.upload_index(index))
logger.info(f"Index pushed with {new_entries} new entries")
def create_layer(
self,
file_path: str,
cname: str,
version: str,
architecture: str,
media_type: str,
):
checksum_sha256 = calculate_sha256(file_path)
layer = oras.oci.NewLayer(file_path, media_type, is_dir=False)
layer["annotations"] = {
oras.defaults.annotation_title: os.path.basename(file_path),
}
return layer
def push_from_dir(
self,
architecture: str,
version: str,
cname: str,
directory: str,
manifest_file: str,
):
# Step 1 scan and extract nested artifacts:
for file in os.listdir(directory):
try:
if file.endswith(".pxe.tar.gz"):
logger.info(f"Found nested artifact {file}")
nested_tar_obj = tarfile.open(f"{directory}/{file}")
nested_tar_obj.extractall(filter="data", path=directory)
nested_tar_obj.close()
except (OSError, tarfile.FilterError, tarfile.TarError) as e:
print(f"Failed to extract nested artifact {file}", e)
exit(1)
try:
oci_metadata = get_oci_metadata_from_fileset(
os.listdir(directory), architecture
)
features = ""
for artifact in oci_metadata:
if artifact["media_type"] == "application/io.gardenlinux.release":
file = open(f"{directory}/{artifact["file_name"]}", "r")
lines = file.readlines()
for line in lines:
if line.strip().startswith("GARDENLINUX_FEATURES="):
features = line.strip().removeprefix(
"GARDENLINUX_FEATURES="
)
break
file.close()
digest = self.push_image_manifest(
architecture,
cname,
version,
directory,
oci_metadata,
features,
manifest_file,
)
except Exception as e:
print("Error: ", e)
exit(1)
return digest