From 9c0cbbf57c451983c673997568920b499ecb0b3a Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 23 Mar 2017 12:37:32 -0400 Subject: [PATCH] data.oci_model: sloppily rewrite digest format We expect digests to be in the form 'sha256:digest' --- data/interfaces/appr.py | 10 ++++++++-- data/oci_model/blob.py | 12 +++++++++--- data/oci_model/manifest.py | 10 ++++++++-- data/oci_model/manifest_list.py | 10 ++++++++-- data/oci_model/release.py | 9 ++++++++- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/data/interfaces/appr.py b/data/interfaces/appr.py index fcf42b2e6..04ebf1bdc 100644 --- a/data/interfaces/appr.py +++ b/data/interfaces/appr.py @@ -306,7 +306,8 @@ class OCIAppModel(AppRegistryDataInterface): tag, manifest, blob = oci_model.release.get_app_release(repo, release, media_type) created_at = _timestamp_to_iso(tag.lifetime_start) - blob_descriptor = BlobDescriptor(digest=blob.digest, mediaType=blob.media_type.name, + blob_descriptor = BlobDescriptor(digest=_strip_sha256_header(blob.digest), + mediaType=blob.media_type.name, size=blob.size, urls=[]) app_manifest = ApplicationManifest(digest=manifest.digest, mediaType=manifest.media_type.name, @@ -333,7 +334,9 @@ class OCIAppModel(AppRegistryDataInterface): cnrblob.size, content_media_type, locations) - return BlobDescriptor(mediaType=content_media_type, digest=db_blob.digest, size=db_blob.size, + return BlobDescriptor(mediaType=content_media_type, + digest=_strip_sha256_header(db_blob.digest), + size=db_blob.size, urls=[]) def create_release(self, package, user, visibility, force=False): @@ -425,4 +428,7 @@ class OCIAppModel(AppRegistryDataInterface): name=channel.name) +def _strip_sha256_header(digest): + return digest.lstrip('sha256:') + oci_app_model = OCIAppModel() diff --git a/data/oci_model/blob.py b/data/oci_model/blob.py index 521f072ba..dbcea422b 100644 --- a/data/oci_model/blob.py +++ b/data/oci_model/blob.py @@ -4,9 +4,15 @@ from data.model import db_transaction from data.database import Blob, BlobPlacementLocation, BlobPlacement +def _ensure_sha256_header(digest): + if digest.startswith('sha256:'): + return digest + return 'sha256:' + digest + + def get_blob(digest): """ Find a blob by its digest. """ - return Blob.select().where(Blob.digest == digest).get() + return Blob.select().where(Blob.digest == _ensure_sha256_header(digest)).get() def get_or_create_blob(digest, size, media_type_name, locations): @@ -15,7 +21,7 @@ def get_or_create_blob(digest, size, media_type_name, locations): try: blob = get_blob(digest) except Blob.DoesNotExist: - blob = Blob.create(digest=digest, + blob = Blob.create(digest=_ensure_sha256_header(digest), media_type_id=Blob.media_type.get_id(media_type_name), size=size) for location_name in locations: @@ -35,7 +41,7 @@ def get_blob_locations(digest): .select() .join(BlobPlacement) .join(Blob) - .where(Blob.digest == digest)] + .where(Blob.digest == _ensure_sha256_header(digest))] def ensure_blob_locations(*names): diff --git a/data/oci_model/manifest.py b/data/oci_model/manifest.py index 247324256..a1af680b5 100644 --- a/data/oci_model/manifest.py +++ b/data/oci_model/manifest.py @@ -11,12 +11,18 @@ from data.oci_model import tag as tag_model logger = logging.getLogger(__name__) +def _ensure_sha256_header(digest): + if digest.startswith('sha256:'): + return digest + return 'sha256:' + digest + + def _digest(manifestjson): - return hashlib.sha256(json.dumps(manifestjson, sort_keys=True)).hexdigest() + return _ensure_sha256_header(hashlib.sha256(json.dumps(manifestjson, sort_keys=True)).hexdigest()) def get_manifest_query(digest, media_type): - return Manifest.select().where(Manifest.digest == digest, + return Manifest.select().where(Manifest.digest == _ensure_sha256_header(digest), Manifest.media_type == Manifest.media_type.get_id(media_type)) diff --git a/data/oci_model/manifest_list.py b/data/oci_model/manifest_list.py index 68be2855f..c1bd35af5 100644 --- a/data/oci_model/manifest_list.py +++ b/data/oci_model/manifest_list.py @@ -8,12 +8,18 @@ from data.database import ManifestList, ManifestListManifest, db_transaction logger = logging.getLogger(__name__) +def _ensure_sha256_header(digest): + if digest.startswith('sha256:'): + return digest + return 'sha256:' + digest + + def _digest(manifestjson): - return hashlib.sha256(json.dumps(manifestjson, sort_keys=True)).hexdigest() + return _ensure_sha256_header(hashlib.sha256(json.dumps(manifestjson, sort_keys=True)).hexdigest()) def get_manifest_list(digest): - return ManifestList.select().where(ManifestList.digest == digest).get() + return ManifestList.select().where(ManifestList.digest == _ensure_sha256_header(digest)).get() def get_or_create_manifest_list(manifest_list_json, media_type_name, schema_version): diff --git a/data/oci_model/release.py b/data/oci_model/release.py index 8bdbbb16f..a343608d5 100644 --- a/data/oci_model/release.py +++ b/data/oci_model/release.py @@ -14,6 +14,12 @@ LIST_MEDIA_TYPE = 'application/vnd.cnr.manifest.list.v0.json' SCHEMA_VERSION = 'v0' +def _ensure_sha256_header(digest): + if digest.startswith('sha256:'): + return digest + return 'sha256:' + digest + + def get_app_release(repo, tag_name, media_type): """ Returns (tag, manifest, blob) given a repo object, tag_name, and media_type). """ tag = tag_model.get_tag(repo, tag_name, tag_kind='release') @@ -67,7 +73,8 @@ def create_app_release(repo, tag_name, manifest, digest): (ManifestBlob .select() .join(Blob) - .where(ManifestBlob.manifest == manifest, Blob.digest == blob_digest).get()) + .where(ManifestBlob.manifest == manifest, + Blob.digest == _ensure_sha256_header(blob_digest)).get()) except ManifestBlob.DoesNotExist: blob = blob_model.get_blob(blob_digest) ManifestBlob.create(manifest=manifest, blob=blob)