Skip to content

Commit

Permalink
Get mediaType from the request's content-type
Browse files Browse the repository at this point in the history
closes #883

(cherry picked from commit 1b1b5a7)
  • Loading branch information
lubosmj authored and ipanova committed Aug 2, 2022
1 parent c1c7da3 commit cfcce30
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES/883.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed an error that was raised when an OCI manifest did not contain ``mediaType``.
29 changes: 16 additions & 13 deletions pulp_container/app/tasks/sync_stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
ManifestSignature,
Tag,
)
from pulp_container.app.utils import extract_data_from_signature, urlpath_sanitize
from pulp_container.app.utils import (
extract_data_from_signature,
urlpath_sanitize,
determine_media_type,
)

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -129,14 +133,16 @@ async def run(self):
tag_dc = DeclarativeContent(Tag(name=tag_name))

content_data = json.loads(raw_data)
media_type = content_data.get("mediaType")

media_type = determine_media_type(content_data, dl_res)

if media_type in (MEDIA_TYPE.MANIFEST_LIST, MEDIA_TYPE.INDEX_OCI):
list_dc = self.create_tagged_manifest_list(
tag_name, saved_artifact, content_data
tag_name, saved_artifact, content_data, media_type
)
for listed_manifest_task in asyncio.as_completed(
[
self.create_listed_manifest(list_dc, manifest_data)
self.create_listed_manifest(manifest_data)
for manifest_data in content_data.get("manifests")
]
):
Expand Down Expand Up @@ -176,7 +182,7 @@ async def run(self):
else:
# Simple tagged manifest
man_dc = self.create_tagged_manifest(
tag_name, saved_artifact, content_data, raw_data
tag_name, saved_artifact, content_data, raw_data, media_type
)
if signature_source is not None:
man_sig_dcs = await self.create_signatures(man_dc, signature_source)
Expand Down Expand Up @@ -315,21 +321,20 @@ async def handle_blobs(self, manifest_dc, content_data):
manifest_dc.extra_data["config_blob_dc"] = blob_dc
await self.put(blob_dc)

def create_tagged_manifest_list(self, tag_name, saved_artifact, manifest_list_data):
def create_tagged_manifest_list(self, tag_name, saved_artifact, manifest_list_data, media_type):
"""
Create a ManifestList.
Args:
tag_name (str): A name of a tag
saved_artifact (pulpcore.plugin.models.Artifact): A saved manifest's Artifact
manifest_list_data (dict): Data about a ManifestList
media_type (str): The type of a manifest
"""
digest = f"sha256:{saved_artifact.sha256}"
manifest_list = Manifest(
digest=digest,
schema_version=manifest_list_data["schemaVersion"],
media_type=manifest_list_data["mediaType"],
digest=digest, schema_version=manifest_list_data["schemaVersion"], media_type=media_type
)

manifest_list_dc = self._create_manifest_declarative_content(
Expand All @@ -338,7 +343,7 @@ def create_tagged_manifest_list(self, tag_name, saved_artifact, manifest_list_da
manifest_list_dc.extra_data["listed_manifests"] = []
return manifest_list_dc

def create_tagged_manifest(self, tag_name, saved_artifact, manifest_data, raw_data):
def create_tagged_manifest(self, tag_name, saved_artifact, manifest_data, raw_data, media_type):
"""
Create an Image Manifest.
Expand All @@ -349,7 +354,6 @@ def create_tagged_manifest(self, tag_name, saved_artifact, manifest_data, raw_da
raw_data: (str): The raw JSON representation of the ImageManifest.
"""
media_type = manifest_data.get("mediaType", MEDIA_TYPE.MANIFEST_V1)
if media_type in (MEDIA_TYPE.MANIFEST_V2, MEDIA_TYPE.MANIFEST_OCI):
digest = f"sha256:{saved_artifact.sha256}"
else:
Expand Down Expand Up @@ -407,12 +411,11 @@ def _create_signature_declarative_content(
)
return sig_dc

async def create_listed_manifest(self, list_dc, manifest_data):
async def create_listed_manifest(self, manifest_data):
"""
Create an Image Manifest from manifest data in a ManifestList.
Args:
list_dc (pulpcore.plugin.stages.DeclarativeContent): dc for a ManifestList
manifest_data (dict): Data about a single new ImageManifest.
"""
Expand Down
25 changes: 25 additions & 0 deletions pulp_container/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pulpcore.plugin.models import Task

from pulp_container.constants import MEDIA_TYPE
from pulp_container.app.json_schemas import SIGNATURE_SCHEMA


Expand Down Expand Up @@ -122,3 +123,27 @@ def has_task_completed(dispatched_task):
task.delete()
raise Exception(str(error))
raise Throttled()


def determine_media_type(content_data, response):
"""Determine the media type of a manifest either from the JSON data or the response object."""
if media_type := content_data.get("mediaType"):
return media_type
elif media_type := response.headers.get("content-type"):
return media_type
elif manifests := content_data.get("manifests"):
if len(manifests):
if manifests[0].get("mediaType") in (MEDIA_TYPE.MANIFEST_V2, MEDIA_TYPE.MANIFEST_V1):
return MEDIA_TYPE.MANIFEST_LIST
elif manifests[0].get("mediaType") in (MEDIA_TYPE.MANIFEST_OCI, MEDIA_TYPE.INDEX_OCI):
return MEDIA_TYPE.INDEX_OCI
return MEDIA_TYPE.MANIFEST_LIST
else:
if config := content_data.get("config"):
config_media_type = config.get("mediaType")
if config_media_type == MEDIA_TYPE.CONFIG_BLOB_OCI:
return MEDIA_TYPE.MANIFEST_OCI
else:
return MEDIA_TYPE.MANIFEST_V2
else:
return MEDIA_TYPE.MANIFEST_V1

0 comments on commit cfcce30

Please sign in to comment.