Skip to content

Commit 6a7f814

Browse files
committed
port OCI module back from python_gardenlinux_cli
1 parent 17d8a90 commit 6a7f814

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

poetry.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gardenlinux/features/__main__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,29 @@ def sort_subset(input_set, order_list):
212212
return [item for item in order_list if item in input_set]
213213

214214

215+
def get_flavor_from_cname(cname: str, get_arch: bool = True) -> str:
216+
"""
217+
Extracts the flavor from a canonical name.
218+
219+
:param str cname: Canonical name of an image
220+
:param bool get_arch: Whether to include the architecture in the flavor
221+
:return: Flavor string
222+
"""
223+
224+
# cname:
225+
# azure-gardener_prod_tpm2_trustedboot-amd64-1312.2-80ffcc87
226+
# transform to flavor:
227+
# azure-gardener_prod_tpm2_trustedboot-amd64
228+
229+
platform = cname.split("-")[0]
230+
features = cname.split("-")[1:-1]
231+
arch = cname.split("-")[-1]
232+
233+
if get_arch:
234+
return f"{platform}-{features}-{arch}"
235+
else:
236+
return f"{platform}-{features}"
237+
238+
215239
if __name__ == "__main__":
216240
main()

src/gardenlinux/oci/crypto.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import hashlib
2+
3+
4+
def verify_sha256(checksum: str, data: bytes):
5+
data_checksum = f"sha256:{hashlib.sha256(data).hexdigest()}"
6+
if checksum != data_checksum:
7+
raise ValueError(f"Invalid checksum. {checksum} != {data_checksum}")
8+
9+
10+
def calculate_sha256(file_path: str) -> str:
11+
"""Calculate the SHA256 checksum of a file."""
12+
sha256_hash = hashlib.sha256()
13+
with open(file_path, "rb") as f:
14+
for byte_block in iter(lambda: f.read(4096), b""):
15+
sha256_hash.update(byte_block)
16+
return sha256_hash.hexdigest()

src/gardenlinux/oci/defaults.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
annotation_signature_key = "io.gardenlinux.oci.signature"
2+
annotation_signed_string_key = "io.gardenlinux.oci.signed-string"

src/gardenlinux/oci/helper.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import json
2+
import os
3+
import re
4+
5+
6+
def write_dict_to_json_file(input, output_path):
7+
if os.path.exists(output_path):
8+
raise ValueError(f"{output_path} already exists")
9+
with open(output_path, "w") as fp:
10+
json.dump(input, fp)
11+
12+
13+
def get_uri_for_digest(uri, digest):
14+
"""
15+
Given a URI for an image, return a URI for the related digest.
16+
17+
URI may be in any of the following forms:
18+
19+
ghcr.io/homebrew/core/hello
20+
ghcr.io/homebrew/core/hello:2.10
21+
ghcr.io/homebrew/core/hello@sha256:ff81...47a
22+
"""
23+
base_uri = re.split(r"[@:]", uri, maxsplit=1)[0]
24+
return f"{base_uri}@{digest}"

0 commit comments

Comments
 (0)