Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mediaType, artifactType, and subject to Index per the specification #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions opencontainers/image/v1/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from opencontainers.struct import Struct
from opencontainers.digest import Digest
from opencontainers.mediatype import RFC6838


class Descriptor(Struct):
Expand All @@ -28,12 +29,11 @@ def __init__(
super().__init__()

# MediaType is the media type of the object this schema refers to.
regexp = "^[A-Za-z0-9][A-Za-z0-9!#$&-^_.+]{0,126}/[A-Za-z0-9][A-Za-z0-9!#$&-^_.+]{0,126}$"
self.newAttr(
name="MediaType",
attType=str,
jsonName="mediaType",
regexp=regexp,
regexp=RFC6838,
required=True,
)

Expand Down
39 changes: 35 additions & 4 deletions opencontainers/image/v1/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from opencontainers.struct import Struct
from opencontainers.image.specs import Versioned
from opencontainers.logger import bot
from opencontainers.mediatype import RFC6838
from .mediatype import MediaTypeImageIndex, MediaTypeImageManifest
from .descriptor import Descriptor
import re
Expand All @@ -20,22 +21,44 @@ class Index(Struct):
mediatype when marshalled to JSON.
"""

def __init__(self, manifests=None, schemaVersion=None, annotations=None):
def __init__(self, manifests=None, schemaVersion=None, annotations=None,
mediaType=None, artifactType=None, subject=None):
super().__init__()

self.newAttr(name="schemaVersion", attType=Versioned, required=True)

# MediaType must be "application/vnd.oci.image.index.v1+json" if given
self.newAttr(
name="MediaType",
attType=str,
jsonName="mediaType",
)

# ArtifactType must be a valid media type according to RFC6838
self.newAttr(
name="ArtifactType",
attType=str,
jsonName="artifactType",
regexp=RFC6838,
)

# Manifests references platform specific manifests.
self.newAttr(
name="Manifests", attType=[Descriptor], jsonName="manifests", required=True
)

# Subject is a descriptor of another manifest
self.newAttr(name="Subject", attType=Descriptor, jsonName="subject")

# Annotations contains arbitrary metadata for the image index.
self.newAttr(name="Annotations", attType=dict, jsonName="annotations")

self.add("schemaVersion", schemaVersion)
self.add("MediaType", mediaType)
self.add("ArtifactType", artifactType)
self.add("Manifests", manifests)
self.add("Subject", subject)
self.add("Annotations", annotations)
self.add("schemaVersion", schemaVersion)

def _validate(self):
"""
Expand All @@ -44,8 +67,16 @@ def _validate(self):
custom validation function to ensure that Manifests mediaTypes
are valid.
"""
valid = True

valid_types = [MediaTypeImageManifest, MediaTypeImageIndex]

mediaType = self.attrs.get("MediaType")
if mediaType.value and mediaType.value != MediaTypeImageIndex:
bot.error("%s must be %s" % (mediaType, MediaTypeImageIndex))
valid = False


manifests = self.attrs.get("Manifests").value
if manifests:
for manifest in manifests:
Expand All @@ -61,6 +92,6 @@ def _validate(self):
# Case 2: not valid and doesn't match regular expression
else:
bot.error("%s is not valid for index manifest." % mediaType)
return False
valid = False

return True
return valid
2 changes: 2 additions & 0 deletions opencontainers/mediatype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# RFC6838 specifies a regexp string for validating media types.
RFC6838 = "^[A-Za-z0-9][A-Za-z0-9!#$&-^_.+]{0,126}/[A-Za-z0-9][A-Za-z0-9!#$&-^_.+]{0,126}$"
31 changes: 31 additions & 0 deletions opencontainers/tests/test_imageindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,30 @@
],
}

root_mediatype_valid = {
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 7143,
"digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
}
],
}

root_mediatype_invalid = {
"schemaVersion": 2,
"mediaType": "something/else",
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": 7143,
"digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
}
],
}


def test_imageindex(tmp_path):
"""test creation of an opencontainers Index"""
Expand Down Expand Up @@ -162,3 +186,10 @@ def test_imageindex(tmp_path):

# valid image index, with customized media type of referenced manifest
index.load(index_with_custom)

# expected failure: invalid root media type
with pytest.raises(SystemExit):
index.load(root_mediatype_invalid)

# valid root media type
index.load(root_mediatype_valid)
Loading