Skip to content

Commit

Permalink
fix: handle image references with both a tag and digest present
Browse files Browse the repository at this point in the history
  • Loading branch information
chgl committed Aug 26, 2022
1 parent 365ac06 commit ca3e612
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
21 changes: 17 additions & 4 deletions connaisseur/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
26 changes: 26 additions & 0 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit ca3e612

Please sign in to comment.