Skip to content

Commit

Permalink
Return a legacy manifest when the conversion was not required
Browse files Browse the repository at this point in the history
closes #8350
https://pulp.plan.io/issues/8350

backports #7923

(cherry picked from commit 1289587)
  • Loading branch information
lubosmj authored and ipanova committed Mar 5, 2021
1 parent 9d491c3 commit 0944df0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
12 changes: 5 additions & 7 deletions pulp_container/app/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,16 @@ async def dispatch_converted_schema(tag, accepted_media_types, path):
"""
schema1_converter = Schema2toSchema1ConverterWrapper(tag, accepted_media_types, path)
try:
schema, converted, digest = schema1_converter.convert()
result = schema1_converter.convert()
except RuntimeError:
raise PathNotResolved(tag.name)

response_headers = {
"Docker-Content-Digest": digest,
"Content-Type": MEDIA_TYPE.MANIFEST_V1_SIGNED,
"Docker-Content-Digest": result.digest,
"Content-Type": result.content_type,
"Docker-Distribution-API-Version": "registry/2.0",
}
if not converted:
raise PathNotResolved(tag.name)

return web.Response(text=schema, headers=response_headers)
return web.Response(text=result.text, headers=response_headers)

async def get_by_digest(self, request):
"""
Expand Down
24 changes: 19 additions & 5 deletions pulp_container/app/schema_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
log = logging.getLogger(__name__)

FS_Layer = namedtuple("FS_Layer", "layer_id uncompressed_digest history")
ConversionResult = namedtuple("ConversionResult", "text digest content_type")


class Schema2toSchema1ConverterWrapper:
Expand All @@ -29,16 +30,29 @@ def __init__(self, tag, accepted_media_types, path):
self.name = path

def convert(self):
"""Convert a manifest to schema 1."""
"""Convert a manifest to schema 1.
Raises:
RuntimeError: If the conversion was not successful.
Returns:
ConversionResult: A converted manifest, corresponding digest, and content type.
"""
if self.tag.tagged_manifest.media_type == MEDIA_TYPE.MANIFEST_V2:
return self._convert_schema(self.tag.tagged_manifest)
schema_with_signature, digest = self._convert_schema(self.tag.tagged_manifest)
return ConversionResult(schema_with_signature, digest, MEDIA_TYPE.MANIFEST_V1_SIGNED)
elif self.tag.tagged_manifest.media_type == MEDIA_TYPE.MANIFEST_LIST:
legacy = self._get_legacy_manifest()
if legacy.media_type in self.accepted_media_types:
# return legacy without conversion
return legacy, False, legacy.digest
legacy_schema = _jsonDumps(_get_manifest_dict(legacy))
return ConversionResult(legacy_schema, legacy.digest, legacy.media_type)
elif legacy.media_type == MEDIA_TYPE.MANIFEST_V2:
return self._convert_schema(legacy)
schema_with_signature, digest = self._convert_schema(legacy)
return ConversionResult(
schema_with_signature, digest, MEDIA_TYPE.MANIFEST_V1_SIGNED
)
else:
raise RuntimeError()

Expand All @@ -59,7 +73,7 @@ def _convert_schema(self, manifest):
# the digest header is deduced from the manifest body without the signature content.
# Therefore, the digest is computed from the formatted and converted manifest here.
digest = compute_digest(converted_schema)
return schema_with_signature, True, digest
return schema_with_signature, digest

def _get_legacy_manifest(self):
ml = self.tag.tagged_manifest.listed_manifests.all()
Expand Down

0 comments on commit 0944df0

Please sign in to comment.