diff --git a/connaisseur/image.py b/connaisseur/image.py index b60bb9456..ed877790f 100644 --- a/connaisseur/image.py +++ b/connaisseur/image.py @@ -42,10 +42,23 @@ def __init__(self, image: str): raise InvalidImageFormatError(message=msg, image=image) name_tag = image.split("/")[-1] - search = re.search(tag_re, name_tag) - self.digest, self.tag = search.groups() if search else (None, "latest") - self.name = name_tag.removesuffix(":" + str(self.tag)).removesuffix( - "@sha256:" + str(self.digest) + + self.digest, self.tag = None, None + + # in case the image_tag contains both a tag and a digest, + # the tag_re matches twice. This handles all these matches. + for match in re.finditer(tag_re, name_tag): + (digest, tag) = match.groups() + if digest is not None: + self.digest = digest + if tag is not None: + self.tag = tag + + if self.tag is None and self.digest is None: + self.tag = "latest" + + self.name = name_tag.removesuffix("@sha256:" + str(self.digest)).removesuffix( + ":" + str(self.tag) ) first_comp = image.removesuffix(name_tag).split("/")[0] diff --git a/tests/test_image.py b/tests/test_image.py index a0916583f..bcc362515 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -113,6 +113,15 @@ "docker.io", fix.no_exc(), ), + ( + "ghcr.io/repo/test/image-with-tag-and-digest:v1.2.3@sha256:f8816ada742348e1adfcec5c2a180b675bf6e4a294e0feb68bd70179451e1242", + "image-with-tag-and-digest", + "v1.2.3", + "f8816ada742348e1adfcec5c2a180b675bf6e4a294e0feb68bd70179451e1242", + "repo/test", + "ghcr.io", + fix.no_exc(), + ), ], ) def test_image( @@ -148,6 +157,13 @@ def test_set_digest(image: str, digest: str): ), True, ), + ( + ( + "image:tag@sha256:859b5aada817b3eb53410222" + "e8fc232cf126c9e598390ae61895eb96f52ae46d" + ), + True + ), ], ) def test_has_digest(image: str, digest: bool): @@ -176,6 +192,16 @@ def test_has_digest(image: str, digest: bool): ), ), ("path/image", "docker.io/path/image:latest"), + ( + ( + "ghcr.io/repo/test/image-with-tag-and-digest:v1.2.3" + "@sha256:859b5aada817b3eb53410222e8fc232cf126c9e598390ae61895eb96f52ae46d" + ), + ( + "ghcr.io/repo/test/image-with-tag-and-digest" + "@sha256:859b5aada817b3eb53410222e8fc232cf126c9e598390ae61895eb96f52ae46d" + ) + ) ], ) def test_str(image: str, str_image: str):