Skip to content

Commit e74466b

Browse files
Nisha Krnjudge
authored andcommitted
Fix retrieving image digest
The config object is the incorrect method of retrieving an image digest as the config and layers itself creates a manifest which is hashed to get the image digest. Instead, we will use skopeo to retireve it. For this, we introduce a function in tern/load/skopeo to do the remote inspection, and parse out the image digest type and digest. Fixes #1101 Signed-off-by: Nisha K <[email protected]>
1 parent 16db01a commit e74466b

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

tern/classes/oci_image.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from tern.classes.image import Image
1212
from tern.utils.constants import manifest_file
1313
from tern.classes.image_layer import ImageLayer
14+
from tern.load import skopeo
1415

1516

1617
class OCIImage(Image):
@@ -100,12 +101,11 @@ def load_image(self, load_until_layer=0):
100101
self.__history = self.get_image_history(self._config)
101102
layer_paths = self.get_image_layers(self._manifest)
102103
layer_diffs = self.get_diff_ids(self._config)
103-
# if the digest isn't in the repotag, get it from the config
104+
# if the digest isn't in the repotag, get it using skopeo
104105
if not self.checksum:
105-
repo_dict = general.parse_image_string(
106-
self._config.get("config").get("Image"))
107-
self.set_checksum(repo_dict.get("digest_type"),
108-
repo_dict.get("digest"))
106+
digest_type, digest = skopeo.get_image_digest(self.repotag)
107+
if digest_type and digest:
108+
self.set_checksum(digest_type, digest)
109109
layer_checksum_type = self.get_diff_checksum_type(self._config)
110110
layer_count = 1
111111
while layer_diffs and layer_paths:

tern/load/skopeo.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Interactions with remote container images using skopeo
88
"""
99

10+
import json
1011
import logging
1112
import sys
1213
import shutil
@@ -40,3 +41,23 @@ def pull_image(image_tag_string):
4041
logger.error("Error when downloading image: \"%s\"", error)
4142
return None
4243
return result
44+
45+
46+
def get_image_digest(image_tag_string):
47+
"""Use skopeo to get the remote image's digest"""
48+
# check if skopeo is set up
49+
check_skopeo_setup()
50+
remote = f'docker://{image_tag_string}'
51+
logger.debug("Inspecting remote image \"%s\"", image_tag_string)
52+
result, error = rootfs.shell_command(
53+
False, ['skopeo', 'inspect', remote])
54+
if error or not result:
55+
logger.error("Unable to retrieve image digest")
56+
return None, None
57+
result_string = json.loads(result)
58+
digest_string = result_string.get("Digest")
59+
if not digest_string:
60+
logger.error("No image digest available")
61+
return None, None
62+
digest_list = digest_string.split(":")
63+
return digest_list[0], digest_list[1]

0 commit comments

Comments
 (0)