From dc52124845113ad4d28724d1efb65cf196ce6fdb Mon Sep 17 00:00:00 2001 From: Tobias Wolf Date: Mon, 16 Nov 2026 11:29:13 +0100 Subject: [PATCH] Ensure commit IDs are always 8 characters long for OCI manifests and S3 Signed-off-by: Tobias Wolf On-behalf-of: SAP --- .github/actions/setup/action.yml | 2 +- poetry.lock | 38 +++++++++---------- pyproject.toml | 12 +++--- src/gardenlinux/constants.py | 3 ++ src/gardenlinux/features/__main__.py | 7 ++-- .../features/artifact_base_name.py | 12 +++--- src/gardenlinux/oci/manifest.py | 9 ++++- src/gardenlinux/s3/s3_artifacts.py | 15 ++++---- tests/constants.py | 5 ++- tests/oci/test_image_manifest.py | 20 ++++++++++ tests/s3/conftest.py | 4 +- tests/s3/constants.py | 4 +- tests/s3/test_main.py | 2 +- 13 files changed, 85 insertions(+), 48 deletions(-) diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml index 46cbf635..89348535 100644 --- a/.github/actions/setup/action.yml +++ b/.github/actions/setup/action.yml @@ -4,7 +4,7 @@ description: Installs the given GardenLinux Python library inputs: version: description: GardenLinux Python library version - default: "1.0.3" + default: "1.0.4" python_version: description: Python version to setup default: "3.14" diff --git a/poetry.lock b/poetry.lock index 54f27f9d..d18013c1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -144,18 +144,18 @@ yaml = ["PyYAML"] [[package]] name = "boto3" -version = "1.44.2" +version = "1.44.3" description = "The AWS SDK for Python (Boto3)" optional = false python-versions = ">=3.10" groups = ["main", "dev"] files = [ - {file = "boto3-1.44.2-py3-none-any.whl", hash = "sha256:d62ff8a9eb06cab9f0b4242d7a2907a8fff9cca630e27ef736660f9d53e5b18e"}, - {file = "boto3-1.44.2.tar.gz", hash = "sha256:70c02826640da17c15067033af01667059dde5e86057c2d1227cd8523c2f24dd"}, + {file = "boto3-1.44.3-py3-none-any.whl", hash = "sha256:8c9c4844c7ce184494b102983696d6cdb09f5307ec7f238ca4ad5849fb14e26b"}, + {file = "boto3-1.44.3.tar.gz", hash = "sha256:bba566b5cba78db05c5f6c44d219c8f6fb2cd2b923106f767289e4cd0b87b093"}, ] [package.dependencies] -botocore = ">=1.44.2,<1.45.0" +botocore = ">=1.44.3,<1.45.0" jmespath = ">=0.7.1,<2.0.0" s3transfer = ">=0.19.0,<0.20.0" @@ -164,14 +164,14 @@ crt = ["botocore[crt] (>=1.21.0,<2.0a0)"] [[package]] name = "boto3-stubs" -version = "1.44.2" -description = "Type annotations for boto3 1.44.2 generated with mypy-boto3-builder 8.12.0" +version = "1.44.3" +description = "Type annotations for boto3 1.44.3 generated with mypy-boto3-builder 8.12.0" optional = false python-versions = ">=3.9" groups = ["dev"] files = [ - {file = "boto3_stubs-1.44.2-py3-none-any.whl", hash = "sha256:df1b4c547844035730f9d6b35539aaf33171239baf1b542e14743c0d04f79c75"}, - {file = "boto3_stubs-1.44.2.tar.gz", hash = "sha256:f8f87bb12525ec822211d5efa1fed17d95116377eeba9440db38d68ada30159d"}, + {file = "boto3_stubs-1.44.3-py3-none-any.whl", hash = "sha256:7187b84a4b6e6dd9d09339a62788ab61a1d6c60d11f717a8e8f672feecf6ca71"}, + {file = "boto3_stubs-1.44.3.tar.gz", hash = "sha256:acef615112ad17c90570e7d265d60d2bf39feb1cf948b66ad823ec3e986c7086"}, ] [package.dependencies] @@ -236,7 +236,7 @@ bedrock-data-automation-runtime = ["mypy-boto3-bedrock-data-automation-runtime ( bedrock-runtime = ["mypy-boto3-bedrock-runtime (>=1.44.0,<1.45.0)"] billing = ["mypy-boto3-billing (>=1.44.0,<1.45.0)"] billingconductor = ["mypy-boto3-billingconductor (>=1.44.0,<1.45.0)"] -boto3 = ["boto3 (==1.44.2)"] +boto3 = ["boto3 (==1.44.3)"] braket = ["mypy-boto3-braket (>=1.44.0,<1.45.0)"] budgets = ["mypy-boto3-budgets (>=1.44.0,<1.45.0)"] ce = ["mypy-boto3-ce (>=1.44.0,<1.45.0)"] @@ -622,14 +622,14 @@ xray = ["mypy-boto3-xray (>=1.44.0,<1.45.0)"] [[package]] name = "botocore" -version = "1.44.2" +version = "1.44.3" description = "Low-level, data-driven core of boto 3." optional = false python-versions = ">=3.10" groups = ["main", "dev"] files = [ - {file = "botocore-1.44.2-py3-none-any.whl", hash = "sha256:f255849e8adc413812162e100cf53c4d096d07d9fd6f1773d90fa6034f7d8980"}, - {file = "botocore-1.44.2.tar.gz", hash = "sha256:39da314d7bab66e9da9af5531445d237beaeeab1fce0ed034bb6315d480b85ce"}, + {file = "botocore-1.44.3-py3-none-any.whl", hash = "sha256:793eac2a7ca167e9376b136cf000e1675e9ffe8d7ec609160cc1ee00b93ace28"}, + {file = "botocore-1.44.3.tar.gz", hash = "sha256:44d86fff55ce2d014bd72183ebc164b21b36598253b8234d4f59e8600e09b8b3"}, ] [package.dependencies] @@ -2648,14 +2648,14 @@ crt = ["botocore[crt] (>=1.37.4,<2.0a.0)"] [[package]] name = "semver" -version = "3.1.1" +version = "3.1.2" description = "Python helper for Semantic Versioning (https://semver.org)" optional = false python-versions = ">=3.10" groups = ["main"] files = [ - {file = "semver-3.1.1-py3-none-any.whl", hash = "sha256:c00cf7dac88a352bc6a4a997d6ac0bf95ba835c589bc0c54f7b76f9e2a45123a"}, - {file = "semver-3.1.1.tar.gz", hash = "sha256:149b26cabada6f63212f97e94505d21578cb1d280cf714827f328d5e70765fea"}, + {file = "semver-3.1.2-py3-none-any.whl", hash = "sha256:45d107c3fa880910441dfab9d555046cba2829a25feeadfe4a1566439635c510"}, + {file = "semver-3.1.2.tar.gz", hash = "sha256:bea193552413194252d33ff26ce0962b76669bd1c6fb17e8e203ac37f072feaf"}, ] [package.extras] @@ -2920,14 +2920,14 @@ widechars = ["wcwidth"] [[package]] name = "types-pyyaml" -version = "6.0.12.20261111" +version = "6.0.12.20261113" description = "Typing stubs for PyYAML" optional = false python-versions = ">=3.10" groups = ["dev"] files = [ - {file = "types_pyyaml-6.0.12.20261111-py3-none-any.whl", hash = "sha256:b7e46cbc847dc235305e95e3e3570792d72e6ccf64bf9b831306f48da490322d"}, - {file = "types_pyyaml-6.0.12.20261111.tar.gz", hash = "sha256:c511f563eaf820b948c4ff6f43fe0b9d48b227be442a69058d9f68f139cae1fb"}, + {file = "types_pyyaml-6.0.12.20261113-py3-none-any.whl", hash = "sha256:3600749e613748afdc4327f0b17c3296cf880cdbc7335f320007abd9dd815f67"}, + {file = "types_pyyaml-6.0.12.20261113.tar.gz", hash = "sha256:0828be2580fd94151182364f4d0158e4de62fdbeeb8d4296f7f7d2b6c22bb640"}, ] [[package]] @@ -3042,4 +3042,4 @@ test = ["pytest", "pytest-cov"] [metadata] lock-version = "2.1" python-versions = ">=3.13,!=3.14.1" -content-hash = "be2bdfcbae6ab5f32335029c99d0310e5a076a9ba9a961c4199b5177aba6925e" +content-hash = "bc8f964ea5d355f76f1667e93487186551ef9cf8bc30c10aab0896d3f32133f5" diff --git a/pyproject.toml b/pyproject.toml index 8dbef871..07832b33 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "gardenlinux" -version = "1.0.3" +version = "1.0.4" description = "Contains tools to work with the features directory of gardenlinux, for example deducting dependencies from feature sets or validating cnames" authors = ["Garden Linux Maintainers "] license = "Apache-2.0" @@ -10,7 +10,7 @@ packages = [{ include = "gardenlinux", from = "src" }] [tool.poetry.dependencies] python = ">=3.13,!=3.14.1" apt-repo = "^0.5" -boto3 = "^1.44.2" +boto3 = "^1.44.3" cryptography = "^49.0.0" jsonschema = "^4.26.0" networkx = "^3.7" @@ -20,21 +20,21 @@ pygit2 = "^1.21.0" pygments = "^2.21.0" PyGithub = "^2.10.0" PyYAML = "^6.0.3" -semver = "^3.1.1" +semver = "^3.1.2" [tool.poetry.group.dev.dependencies] bandit = "^1.9.4" -boto3-stubs = { extras = ["s3"], version = "^1.44.2" } +boto3-stubs = { extras = ["s3"], version = "^1.44.3" } isort = "^8.0.1" moto = "^5.2.5" -mypy = "2.4.1" +mypy = "2.1.0" pre-commit = "^4.7.0" python-dotenv = "^1.2.2" pytest = "^9.2.0" pytest-cov = "^7.1.0" requests-mock = "^1.12.1" sphinx = "9.2.0" -types-pyyaml = "^6.0.12.20261111" +types-pyyaml = "^6.0.12.20261113" types-requests = "^2.33.0.20261030" [tool.poetry.group.docs.dependencies] diff --git a/src/gardenlinux/constants.py b/src/gardenlinux/constants.py index d61101e6..d4f7b101 100644 --- a/src/gardenlinux/constants.py +++ b/src/gardenlinux/constants.py @@ -11,6 +11,9 @@ "type": "element", } +COMMIT_ID_LENGTH = 8 +COMMIT_HASH_LENGTH = 40 + # GardenLinux flavors schema for validation GL_FLAVORS_SCHEMA = { "type": "object", diff --git a/src/gardenlinux/features/__main__.py b/src/gardenlinux/features/__main__.py index c0095b41..69afacbb 100644 --- a/src/gardenlinux/features/__main__.py +++ b/src/gardenlinux/features/__main__.py @@ -11,6 +11,7 @@ from os import path from typing import Any, Set +from ..constants import COMMIT_ID_LENGTH from .artifact_base_name import ArtifactBaseName from .parser import Parser @@ -201,13 +202,13 @@ def main() -> None: else: print_output_from_abn_object(args.type, abn_object) elif args.type == "commit-id": - print(commit_id_or_hash[:8]) + print(commit_id_or_hash[:COMMIT_ID_LENGTH]) elif args.type == "container-tag": - print(re.sub("\\W+", "-", f"{version}-{commit_id_or_hash[:8]}")) + print(re.sub("\\W+", "-", f"{version}-{commit_id_or_hash[:COMMIT_ID_LENGTH]}")) elif args.type == "version": print(version) elif args.type == "version_and_commit-id": - print(f"{version}-{commit_id_or_hash[:8]}") + print(f"{version}-{commit_id_or_hash[:COMMIT_ID_LENGTH]}") def graph_as_mermaid_markup(cname: str | None, graph: Any) -> str: diff --git a/src/gardenlinux/features/artifact_base_name.py b/src/gardenlinux/features/artifact_base_name.py index 43a0ed85..6baa69b5 100644 --- a/src/gardenlinux/features/artifact_base_name.py +++ b/src/gardenlinux/features/artifact_base_name.py @@ -12,6 +12,8 @@ from typing import Optional, Self from ..constants import ( + COMMIT_HASH_LENGTH, + COMMIT_ID_LENGTH, GL_BUG_REPORT_URL, GL_COMMIT_SPECIAL_VALUES, GL_DISTRIBUTION_NAME, @@ -60,11 +62,11 @@ def __init__( commit_id_or_hash = re_match[5] - self._commit_id = commit_id_or_hash[:8] + self._commit_id = commit_id_or_hash[:COMMIT_ID_LENGTH] self._commit_hash = None if ( - len(commit_id_or_hash) == 40 + len(commit_id_or_hash) == COMMIT_HASH_LENGTH or commit_id_or_hash in GL_COMMIT_SPECIAL_VALUES ): # sha1 hex self._commit_hash = commit_id_or_hash @@ -95,7 +97,7 @@ def commit_hash(self, commit_hash: str) -> None: if self._commit_id is not None and not commit_hash.startswith(self._commit_id): raise RuntimeError("Commit hash given differs from commit ID already set") - self._commit_id = commit_hash[:8] + self._commit_id = commit_hash[:COMMIT_ID_LENGTH] self._commit_hash = commit_hash @property @@ -357,7 +359,7 @@ def get_version_and_commit_id_from_files( if os.access(gardenlinux_root.joinpath("COMMIT"), os.R_OK): with gardenlinux_root.joinpath("COMMIT").open("r") as fp: - commit_hash = fp.read().strip()[:8] + commit_hash = fp.read().strip()[:COMMIT_ID_LENGTH] if os.access(gardenlinux_root.joinpath("VERSION"), os.R_OK): with gardenlinux_root.joinpath("VERSION").open("r") as fp: @@ -434,7 +436,7 @@ def new_instance( raise RuntimeError("Version does not match the given argument") if commit_id_or_hash: - commit_id = commit_id_or_hash[:8] + commit_id = commit_id_or_hash[:COMMIT_ID_LENGTH] if abn_object.commit_hash and abn_object.commit_hash != commit_id_or_hash: raise RuntimeError("Commit hash does not match the given argument") diff --git a/src/gardenlinux/oci/manifest.py b/src/gardenlinux/oci/manifest.py index fe27be99..c8c406c3 100644 --- a/src/gardenlinux/oci/manifest.py +++ b/src/gardenlinux/oci/manifest.py @@ -13,6 +13,8 @@ from oras.defaults import unknown_config_media_type as UNKNOWN_CONFIG_MEDIA_TYPE from oras.oci import EmptyManifest +from ..constants import COMMIT_HASH_LENGTH, COMMIT_ID_LENGTH + class Manifest(dict): # type: ignore[type-arg] """ @@ -81,7 +83,12 @@ def commit(self) -> str: f"Unexpected manifest with missing config annotation '{annotation_commit_key}' found" ) - return self["annotations"][annotation_commit_key] # type: ignore[no-any-return] + commit_id: str = self["annotations"][annotation_commit_key] + + if len(commit_id) == COMMIT_HASH_LENGTH: + commit_id = commit_id[:COMMIT_ID_LENGTH] + + return commit_id @commit.setter def commit(self, value: str) -> None: diff --git a/src/gardenlinux/s3/s3_artifacts.py b/src/gardenlinux/s3/s3_artifacts.py index 6dd082d5..c950fe1c 100644 --- a/src/gardenlinux/s3/s3_artifacts.py +++ b/src/gardenlinux/s3/s3_artifacts.py @@ -217,7 +217,7 @@ def upload_from_directory( arch = re_object.sub("+", arch) commit_id_or_hash = abn_object.commit_hash - if commit_id_or_hash is None: + if not commit_id_or_hash: commit_id_or_hash = abn_object.commit_id metadata = { @@ -231,7 +231,7 @@ def upload_from_directory( "secureboot": secureboot, "tpm2": tpm2, "s3_bucket": self._bucket.name, - "s3_key": f"meta/singles/{base_name}", + "s3_key": f"meta/singles/{abn_object}", "test_result": None, "version": abn_object.version, "paths": [], @@ -254,7 +254,8 @@ def upload_from_directory( if not artifact.match(f"{base_name}*"): continue - s3_key = f"objects/{base_name}/{artifact.name}" + s3_suffix = artifact.name[base_name_length:] + s3_key = f"objects/{abn_object}/{abn_object}{s3_suffix}" with artifact.open("rb") as fp: md5sum = file_digest(fp, "md5").hexdigest() @@ -262,10 +263,10 @@ def upload_from_directory( sha256sum = file_digest(fp, "sha256").hexdigest() artifact_metadata = { - "name": artifact.name, + "name": f"{abn_object}{s3_suffix}", "s3_bucket_name": self._bucket.name, "s3_key": s3_key, - "suffix": re_object.sub("+", artifact.name[base_name_length:]), + "suffix": re_object.sub("+", s3_suffix), "md5sum": md5sum, "sha256sum": sha256sum, } @@ -296,7 +297,7 @@ def upload_from_directory( else: if delete_before_push: self._bucket.delete_objects( - Delete={"Objects": [{"Key": f"meta/singles/{base_name}"}]} + Delete={"Objects": [{"Key": f"meta/singles/{abn_object}"}]} ) with TemporaryFile(mode="wb+") as fp: @@ -305,6 +306,6 @@ def upload_from_directory( self._bucket.upload_fileobj( fp, - f"meta/singles/{base_name}", + f"meta/singles/{abn_object}", ExtraArgs={"ContentType": "text/yaml"}, ) diff --git a/tests/constants.py b/tests/constants.py index 28a260b2..43aa8307 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -1,6 +1,7 @@ import os from pathlib import Path +from gardenlinux.constants import COMMIT_ID_LENGTH from gardenlinux.git import Repository TEST_DATA_DIR = "test-data" @@ -18,14 +19,14 @@ TEST_ARCHITECTURES = ["arm64", "amd64"] TEST_FEATURE_STRINGS_SHORT = ["gardener_prod"] TEST_FEATURE_SET = "_slim,base,container" -TEST_COMMIT = Repository(GL_ROOT_DIR).commit_id[:8] +TEST_COMMIT = Repository(GL_ROOT_DIR).commit_id[:COMMIT_ID_LENGTH] TEST_VERSION = "1000.0" TEST_VERSION_STABLE = "1000" TEST_GARDENLINUX_RELEASE_MAJOR = "2150.0.0" TEST_GARDENLINUX_RELEASE_MINOR = "1877.3" TEST_GARDENLINUX_COMMIT = "75df9f401a842914563f312899ec3ce34b24515c" -TEST_GARDENLINUX_COMMIT_SHORT = TEST_GARDENLINUX_COMMIT[:8] +TEST_GARDENLINUX_COMMIT_SHORT = TEST_GARDENLINUX_COMMIT[:COMMIT_ID_LENGTH] TEST_GARDENLINUX_RELEASE_BUCKET_NAME = "test__gardenlinux__releases" RELEASE_NOTES_TEST_DATA_DIR = ( diff --git a/tests/oci/test_image_manifest.py b/tests/oci/test_image_manifest.py index f0bde72e..7fd6842b 100644 --- a/tests/oci/test_image_manifest.py +++ b/tests/oci/test_image_manifest.py @@ -4,6 +4,8 @@ from gardenlinux.oci import ImageManifest, Layer +from ..constants import TEST_GARDENLINUX_COMMIT, TEST_GARDENLINUX_COMMIT_SHORT + def test_ImageManifest_artifact_base_name() -> None: # Arrange @@ -48,6 +50,24 @@ def test_ImageManifest_arch() -> None: assert manifest.arch == "amd64" +def test_ImageManifest_commit() -> None: + # Arrange + empty_manifest = ImageManifest() + + manifest = ImageManifest( + annotations={ImageManifest.ANNOTATION_COMMIT_KEY: TEST_GARDENLINUX_COMMIT} + ) + + # Assert + with pytest.raises(RuntimeError): + assert empty_manifest.commit == TEST_GARDENLINUX_COMMIT_SHORT + + empty_manifest.commit = TEST_GARDENLINUX_COMMIT_SHORT + assert empty_manifest.commit == TEST_GARDENLINUX_COMMIT_SHORT + + assert manifest.commit == TEST_GARDENLINUX_COMMIT_SHORT + + def test_ImageManifest_cname() -> None: # Arrange arch = "amd64" diff --git a/tests/s3/conftest.py b/tests/s3/conftest.py index 9219fd06..86ac4038 100644 --- a/tests/s3/conftest.py +++ b/tests/s3/conftest.py @@ -11,6 +11,8 @@ from moto import mock_aws from mypy_boto3_s3.service_resource import S3ServiceResource +from gardenlinux.constants import COMMIT_ID_LENGTH + BUCKET_NAME = "test-bucket" REGION = "us-east-1" @@ -32,7 +34,7 @@ def make_artifact_base_name( """ Helper function to build cname. Can be used to customized the cname. """ - return f"{flavor}-{arch}-{version}-{commit[:8]}" + return f"{flavor}-{arch}-{version}-{commit[:COMMIT_ID_LENGTH]}" # Helpers to compute digests for fake files diff --git a/tests/s3/constants.py b/tests/s3/constants.py index d63680fe..92de12ca 100644 --- a/tests/s3/constants.py +++ b/tests/s3/constants.py @@ -5,7 +5,7 @@ GARDENLINUX_CNAME="container_trustedboot_usi" GARDENLINUX_VERSION=1234.1 GARDENLINUX_COMMIT_ID="abc123lo" -GARDENLINUX_COMMIT_ID_LONG="abc123long" +GARDENLINUX_COMMIT_ID_LONG="abc123long000000000000000000000000000040" GARDENLINUX_PLATFORM="container" GARDENLINUX_FEATURES="_usi,_trustedboot" GARDENLINUX_FEATURES_ELEMENTS= @@ -16,7 +16,7 @@ S3_METADATA = """ platform: container architecture: amd64 -build_committish: abc123lo +build_committish: abc123long000000000000000000000000000040 build_timestamp: {build_timestamp} logs: null modifiers: diff --git a/tests/s3/test_main.py b/tests/s3/test_main.py index 9051e084..98d7b54b 100644 --- a/tests/s3/test_main.py +++ b/tests/s3/test_main.py @@ -106,4 +106,4 @@ def test_main_with_expected_result( "^(.*)(md5sum|sha256sum)\\: .+$", "\\1\\2: {\\2}", result, flags=re.M ) - assert S3_METADATA == result + assert result == S3_METADATA