Merge branch 'master' into delete-setup-page
This commit is contained in:
commit
a40e1e939a
34 changed files with 1301 additions and 1583 deletions
|
@ -357,7 +357,11 @@ def set_image_metadata(docker_image_id, namespace_name, repository_name, created
|
|||
|
||||
def get_image(repo, docker_image_id):
|
||||
try:
|
||||
return Image.get(Image.docker_image_id == docker_image_id, Image.repository == repo)
|
||||
return (Image
|
||||
.select(Image, ImageStorage)
|
||||
.join(ImageStorage)
|
||||
.where(Image.docker_image_id == docker_image_id, Image.repository == repo)
|
||||
.get())
|
||||
except Image.DoesNotExist:
|
||||
return None
|
||||
|
||||
|
|
|
@ -11,9 +11,6 @@ from util.validation import is_json
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Label which marks a manifest with its source build ID.
|
||||
INTERNAL_LABEL_BUILD_UUID = 'quay.build.uuid'
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_label_source_types():
|
||||
|
|
|
@ -208,8 +208,9 @@ def list_active_repo_tags(repo):
|
|||
and (if present), their manifest.
|
||||
"""
|
||||
query = _tag_alive(RepositoryTag
|
||||
.select(RepositoryTag, Image, TagManifest.digest)
|
||||
.select(RepositoryTag, Image, ImageStorage, TagManifest.digest)
|
||||
.join(Image)
|
||||
.join(ImageStorage)
|
||||
.where(RepositoryTag.repository == repo, RepositoryTag.hidden == False)
|
||||
.switch(RepositoryTag)
|
||||
.join(TagManifest, JOIN.LEFT_OUTER))
|
||||
|
@ -470,8 +471,9 @@ def get_tag_image(namespace_name, repository_name, tag_name, include_storage=Fal
|
|||
|
||||
def list_repository_tag_history(repo_obj, page=1, size=100, specific_tag=None):
|
||||
query = (RepositoryTag
|
||||
.select(RepositoryTag, Image)
|
||||
.select(RepositoryTag, Image, ImageStorage)
|
||||
.join(Image)
|
||||
.join(ImageStorage)
|
||||
.switch(RepositoryTag)
|
||||
.where(RepositoryTag.repository == repo_obj)
|
||||
.where(RepositoryTag.hidden == False)
|
||||
|
@ -515,7 +517,7 @@ def restore_tag_to_manifest(repo_obj, tag_name, manifest_digest):
|
|||
|
||||
# Change the tag manifest to point to the updated image.
|
||||
docker_image_id = tag_manifest.tag.image.docker_image_id
|
||||
updated_tag = create_or_update_tag_for_repo(repo_obj.id, tag_name, docker_image_id,
|
||||
updated_tag = create_or_update_tag_for_repo(repo_obj, tag_name, docker_image_id,
|
||||
reversion=True)
|
||||
tag_manifest.tag = updated_tag
|
||||
tag_manifest.save()
|
||||
|
@ -544,8 +546,7 @@ def restore_tag_to_image(repo_obj, tag_name, docker_image_id):
|
|||
except DataModelException:
|
||||
existing_image = None
|
||||
|
||||
create_or_update_tag(repo_obj.namespace_user.username, repo_obj.name, tag_name,
|
||||
docker_image_id, reversion=True)
|
||||
create_or_update_tag_for_repo(repo_obj, tag_name, docker_image_id, reversion=True)
|
||||
return existing_image
|
||||
|
||||
|
||||
|
@ -589,6 +590,16 @@ def get_active_tag(namespace, repo_name, tag_name):
|
|||
.where(RepositoryTag.name == tag_name, Repository.name == repo_name,
|
||||
Namespace.username == namespace)).get()
|
||||
|
||||
def get_active_tag_for_repo(repo, tag_name):
|
||||
try:
|
||||
return _tag_alive(RepositoryTag
|
||||
.select(RepositoryTag, Image, ImageStorage)
|
||||
.join(Image)
|
||||
.join(ImageStorage)
|
||||
.where(RepositoryTag.name == tag_name,
|
||||
RepositoryTag.repository == repo)).get()
|
||||
except RepositoryTag.DoesNotExist:
|
||||
return None
|
||||
|
||||
def get_possibly_expired_tag(namespace, repo_name, tag_name):
|
||||
return (RepositoryTag
|
||||
|
@ -641,6 +652,13 @@ def populate_manifest(repository, manifest, legacy_image, storage_ids):
|
|||
return manifest_row
|
||||
|
||||
|
||||
def get_tag_manifest(tag):
|
||||
try:
|
||||
return TagManifest.get(tag=tag)
|
||||
except TagManifest.DoesNotExist:
|
||||
return None
|
||||
|
||||
|
||||
def load_tag_manifest(namespace, repo_name, tag_name):
|
||||
try:
|
||||
return (_load_repo_manifests(namespace, repo_name)
|
||||
|
|
49
data/registry_model/datatype.py
Normal file
49
data/registry_model/datatype.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
# pylint: disable=protected-access
|
||||
|
||||
from functools import wraps, total_ordering
|
||||
|
||||
def datatype(name, static_fields):
|
||||
""" Defines a base class for a datatype that will represent a row from the database,
|
||||
in an abstracted form.
|
||||
"""
|
||||
@total_ordering
|
||||
class DataType(object):
|
||||
__name__ = name
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self._db_id = kwargs.pop('db_id', None)
|
||||
self._inputs = kwargs.pop('inputs', None)
|
||||
self._fields = kwargs
|
||||
|
||||
for name in static_fields:
|
||||
assert name in self._fields, 'Missing field %s' % name
|
||||
|
||||
def __eq__(self, other):
|
||||
return self._db_id == other._db_id
|
||||
|
||||
def __lt__(self, other):
|
||||
return self._db_id < other._db_id
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name in static_fields:
|
||||
return self._fields[name]
|
||||
|
||||
raise AttributeError('Unknown field `%s`' % name)
|
||||
|
||||
return DataType
|
||||
|
||||
|
||||
def requiresinput(input_name):
|
||||
""" Marks a property on the data type as requiring an input to be invoked. """
|
||||
def inner(func):
|
||||
@wraps(func)
|
||||
def wrapper(self, *args, **kwargs):
|
||||
if self._inputs.get(input_name) is None:
|
||||
raise Exception('Cannot invoke function with missing input `%s`' % input_name)
|
||||
|
||||
kwargs[input_name] = self._inputs[input_name]
|
||||
result = func(self, *args, **kwargs)
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
return inner
|
|
@ -1,20 +1,120 @@
|
|||
from collections import namedtuple
|
||||
from enum import Enum, unique
|
||||
|
||||
class RepositoryReference(object):
|
||||
from data.registry_model.datatype import datatype, requiresinput
|
||||
|
||||
class RepositoryReference(datatype('Repository', [])):
|
||||
""" RepositoryReference is a reference to a repository, passed to registry interface methods. """
|
||||
def __init__(self, repo_id):
|
||||
self.repo_id = repo_id
|
||||
|
||||
@classmethod
|
||||
def for_repo_obj(cls, repo_obj):
|
||||
return RepositoryReference(repo_obj.id)
|
||||
if repo_obj is None:
|
||||
return None
|
||||
|
||||
return RepositoryReference(db_id=repo_obj.id)
|
||||
|
||||
|
||||
class Tag(namedtuple('Tag', ['id', 'name'])):
|
||||
class Label(datatype('Label', ['key', 'value', 'uuid', 'source_type_name', 'media_type_name'])):
|
||||
""" Label represents a label on a manifest. """
|
||||
@classmethod
|
||||
def for_label(cls, label):
|
||||
if label is None:
|
||||
return None
|
||||
|
||||
return Label(db_id=label.id, key=label.key, value=label.value,
|
||||
uuid=label.uuid, media_type_name=label.media_type.name,
|
||||
source_type_name=label.source_type.name)
|
||||
|
||||
|
||||
class Tag(datatype('Tag', ['name', 'reversion', 'manifest_digest', 'lifetime_start_ts',
|
||||
'lifetime_end_ts'])):
|
||||
""" Tag represents a tag in a repository, which points to a manifest or image. """
|
||||
@classmethod
|
||||
def for_repository_tag(cls, repository_tag):
|
||||
def for_repository_tag(cls, repository_tag, manifest_digest=None, legacy_image=None):
|
||||
if repository_tag is None:
|
||||
return None
|
||||
|
||||
return Tag(id=repository_tag.id, name=repository_tag.name)
|
||||
return Tag(db_id=repository_tag.id,
|
||||
name=repository_tag.name,
|
||||
reversion=repository_tag.reversion,
|
||||
lifetime_start_ts=repository_tag.lifetime_start_ts,
|
||||
lifetime_end_ts=repository_tag.lifetime_end_ts,
|
||||
manifest_digest=manifest_digest,
|
||||
inputs=dict(legacy_image=legacy_image))
|
||||
|
||||
@property
|
||||
@requiresinput('legacy_image')
|
||||
def legacy_image(self, legacy_image):
|
||||
""" Returns the legacy Docker V1-style image for this tag. Note that this
|
||||
will be None for tags whose manifests point to other manifests instead of images.
|
||||
"""
|
||||
return legacy_image
|
||||
|
||||
|
||||
class Manifest(datatype('Manifest', ['digest', 'manifest_bytes'])):
|
||||
""" Manifest represents a manifest in a repository. """
|
||||
@classmethod
|
||||
def for_tag_manifest(cls, tag_manifest, legacy_image=None):
|
||||
if tag_manifest is None:
|
||||
return None
|
||||
|
||||
return Manifest(db_id=tag_manifest.id, digest=tag_manifest.digest,
|
||||
manifest_bytes=tag_manifest.json_data,
|
||||
inputs=dict(legacy_image=legacy_image))
|
||||
|
||||
@property
|
||||
@requiresinput('legacy_image')
|
||||
def legacy_image(self, legacy_image):
|
||||
""" Returns the legacy Docker V1-style image for this manifest. Note that this
|
||||
will be None for manifests that point to other manifests instead of images.
|
||||
"""
|
||||
return legacy_image
|
||||
|
||||
|
||||
class LegacyImage(datatype('LegacyImage', ['docker_image_id', 'created', 'comment', 'command',
|
||||
'image_size', 'aggregate_size', 'uploading'])):
|
||||
""" LegacyImage represents a Docker V1-style image found in a repository. """
|
||||
@classmethod
|
||||
def for_image(cls, image, images_map=None, tags_map=None):
|
||||
if image is None:
|
||||
return None
|
||||
|
||||
return LegacyImage(db_id=image.id,
|
||||
inputs=dict(images_map=images_map, tags_map=tags_map,
|
||||
ancestor_id_list=image.ancestor_id_list()),
|
||||
docker_image_id=image.docker_image_id,
|
||||
created=image.created,
|
||||
comment=image.comment,
|
||||
command=image.command,
|
||||
image_size=image.storage.image_size,
|
||||
aggregate_size=image.aggregate_size,
|
||||
uploading=image.storage.uploading)
|
||||
|
||||
@property
|
||||
@requiresinput('images_map')
|
||||
@requiresinput('ancestor_id_list')
|
||||
def parents(self, images_map, ancestor_id_list):
|
||||
""" Returns the parent images for this image. Raises an exception if the parents have
|
||||
not been loaded before this property is invoked.
|
||||
"""
|
||||
return [LegacyImage.for_image(images_map[ancestor_id], images_map=images_map)
|
||||
for ancestor_id in reversed(ancestor_id_list)
|
||||
if images_map.get(ancestor_id)]
|
||||
|
||||
@property
|
||||
@requiresinput('tags_map')
|
||||
def tags(self, tags_map):
|
||||
""" Returns the tags pointing to this image. Raises an exception if the tags have
|
||||
not been loaded before this property is invoked.
|
||||
"""
|
||||
tags = tags_map.get(self._db_id)
|
||||
if not tags:
|
||||
return []
|
||||
|
||||
return [Tag.for_repository_tag(tag) for tag in tags]
|
||||
|
||||
|
||||
@unique
|
||||
class SecurityScanStatus(Enum):
|
||||
""" Security scan status enum """
|
||||
SCANNED = 'scanned'
|
||||
FAILED = 'failed'
|
||||
QUEUED = 'queued'
|
||||
|
|
|
@ -19,3 +19,106 @@ class RegistryDataInterface(object):
|
|||
""" Returns the most recently pushed alive tag in the repository, if any. If none, returns
|
||||
None.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def lookup_repository(self, namespace_name, repo_name, kind_filter=None):
|
||||
""" Looks up and returns a reference to the repository with the given namespace and name,
|
||||
or None if none. """
|
||||
|
||||
@abstractmethod
|
||||
def get_manifest_for_tag(self, tag):
|
||||
""" Returns the manifest associated with the given tag. """
|
||||
|
||||
@abstractmethod
|
||||
def lookup_manifest_by_digest(self, repository_ref, manifest_digest, allow_dead=False):
|
||||
""" Looks up the manifest with the given digest under the given repository and returns it
|
||||
or None if none. """
|
||||
|
||||
@abstractmethod
|
||||
def get_legacy_images(self, repository_ref):
|
||||
"""
|
||||
Returns an iterator of all the LegacyImage's defined in the matching repository.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_legacy_image(self, repository_ref, docker_image_id, include_parents=False):
|
||||
"""
|
||||
Returns the matching LegacyImages under the matching repository, if any. If none,
|
||||
returns None.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def create_manifest_label(self, manifest, key, value, source_type_name, media_type_name=None):
|
||||
""" Creates a label on the manifest with the given key and value.
|
||||
|
||||
Can raise InvalidLabelKeyException or InvalidMediaTypeException depending
|
||||
on the validation errors.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def list_manifest_labels(self, manifest, key_prefix=None):
|
||||
""" Returns all labels found on the manifest. If specified, the key_prefix will filter the
|
||||
labels returned to those keys that start with the given prefix.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_manifest_label(self, manifest, label_uuid):
|
||||
""" Returns the label with the specified UUID on the manifest or None if none. """
|
||||
|
||||
@abstractmethod
|
||||
def delete_manifest_label(self, manifest, label_uuid):
|
||||
""" Delete the label with the specified UUID on the manifest. Returns the label deleted
|
||||
or None if none.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def list_repository_tags(self, repository_ref, include_legacy_images=False):
|
||||
"""
|
||||
Returns a list of all the active tags in the repository. Note that this can be a *heavy*
|
||||
operation on repositories with a lot of tags, and should be avoided for more targetted
|
||||
operations wherever possible.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def list_repository_tag_history(self, repository_ref, page=1, size=100, specific_tag_name=None):
|
||||
"""
|
||||
Returns the history of all tags in the repository (unless filtered). This includes tags that
|
||||
have been made in-active due to newer versions of those tags coming into service.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_repo_tag(self, repository_ref, tag_name, include_legacy_image=False):
|
||||
"""
|
||||
Returns the latest, *active* tag found in the repository, with the matching name
|
||||
or None if none.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def retarget_tag(self, repository_ref, tag_name, manifest_or_legacy_image,
|
||||
is_reversion=False):
|
||||
"""
|
||||
Creates, updates or moves a tag to a new entry in history, pointing to the manifest or
|
||||
legacy image specified. If is_reversion is set to True, this operation is considered a
|
||||
reversion over a previous tag move operation. Returns the updated Tag or None on error.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def delete_tag(self, repository_ref, tag_name):
|
||||
"""
|
||||
Deletes the latest, *active* tag with the given name in the repository.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def change_repository_tag_expiration(self, tag, expiration_date):
|
||||
""" Sets the expiration date of the tag under the matching repository to that given. If the
|
||||
expiration date is None, then the tag will not expire. Returns a tuple of the previous
|
||||
expiration timestamp in seconds (if any), and whether the operation succeeded.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_legacy_images_owned_by_tag(self, tag):
|
||||
""" Returns all legacy images *solely owned and used* by the given tag. """
|
||||
|
||||
@abstractmethod
|
||||
def get_security_status(self, manifest_or_legacy_image):
|
||||
""" Returns the security status for the given manifest or legacy image or None if none. """
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
# pylint: disable=protected-access
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
from data import database
|
||||
from data import model
|
||||
from data.registry_model.interface import RegistryDataInterface
|
||||
from data.registry_model.datatypes import Tag
|
||||
from data.registry_model.datatypes import (Tag, RepositoryReference, Manifest, LegacyImage, Label,
|
||||
SecurityScanStatus)
|
||||
|
||||
|
||||
class PreOCIModel(RegistryDataInterface):
|
||||
|
@ -13,15 +19,275 @@ class PreOCIModel(RegistryDataInterface):
|
|||
""" Finds an alive tag in the repository matching one of the given tag names and returns it
|
||||
or None if none.
|
||||
"""
|
||||
found_tag = model.tag.find_matching_tag(repository_ref.repo_id, tag_names)
|
||||
found_tag = model.tag.find_matching_tag(repository_ref._db_id, tag_names)
|
||||
return Tag.for_repository_tag(found_tag)
|
||||
|
||||
def get_most_recent_tag(self, repository_ref):
|
||||
""" Returns the most recently pushed alive tag in the repository, if any. If none, returns
|
||||
None.
|
||||
"""
|
||||
found_tag = model.tag.get_most_recent_tag(repository_ref.repo_id)
|
||||
found_tag = model.tag.get_most_recent_tag(repository_ref._db_id)
|
||||
return Tag.for_repository_tag(found_tag)
|
||||
|
||||
def lookup_repository(self, namespace_name, repo_name, kind_filter=None):
|
||||
""" Looks up and returns a reference to the repository with the given namespace and name,
|
||||
or None if none. """
|
||||
repo = model.repository.get_repository(namespace_name, repo_name, kind_filter=kind_filter)
|
||||
return RepositoryReference.for_repo_obj(repo)
|
||||
|
||||
def get_manifest_for_tag(self, tag):
|
||||
""" Returns the manifest associated with the given tag. """
|
||||
try:
|
||||
tag_manifest = database.TagManifest.get(tag_id=tag._db_id)
|
||||
except database.TagManifest.DoesNotExist:
|
||||
return
|
||||
|
||||
return Manifest.for_tag_manifest(tag_manifest)
|
||||
|
||||
def lookup_manifest_by_digest(self, repository_ref, manifest_digest, allow_dead=False,
|
||||
include_legacy_image=False):
|
||||
""" Looks up the manifest with the given digest under the given repository and returns it
|
||||
or None if none. """
|
||||
repo = model.repository.lookup_repository(repository_ref._db_id)
|
||||
if repo is None:
|
||||
return None
|
||||
|
||||
try:
|
||||
tag_manifest = model.tag.load_manifest_by_digest(repo.namespace_user.username,
|
||||
repo.name,
|
||||
manifest_digest,
|
||||
allow_dead=allow_dead)
|
||||
except model.tag.InvalidManifestException:
|
||||
return None
|
||||
|
||||
legacy_image = None
|
||||
if include_legacy_image:
|
||||
legacy_image = self.get_legacy_image(repository_ref, tag_manifest.tag.image.docker_image_id,
|
||||
include_parents=True)
|
||||
|
||||
return Manifest.for_tag_manifest(tag_manifest, legacy_image)
|
||||
|
||||
def get_legacy_images(self, repository_ref):
|
||||
"""
|
||||
Returns an iterator of all the LegacyImage's defined in the matching repository.
|
||||
"""
|
||||
repo = model.repository.lookup_repository(repository_ref._db_id)
|
||||
if repo is None:
|
||||
return None
|
||||
|
||||
all_images = model.image.get_repository_images_without_placements(repo)
|
||||
all_images_map = {image.id: image for image in all_images}
|
||||
|
||||
all_tags = model.tag.list_repository_tags(repo.namespace_user.username, repo.name)
|
||||
tags_by_image_id = defaultdict(list)
|
||||
for tag in all_tags:
|
||||
tags_by_image_id[tag.image_id].append(tag)
|
||||
|
||||
return [LegacyImage.for_image(image, images_map=all_images_map, tags_map=tags_by_image_id)
|
||||
for image in all_images]
|
||||
|
||||
def get_legacy_image(self, repository_ref, docker_image_id, include_parents=False):
|
||||
"""
|
||||
Returns the matching LegacyImages under the matching repository, if any. If none,
|
||||
returns None.
|
||||
"""
|
||||
repo = model.repository.lookup_repository(repository_ref._db_id)
|
||||
if repo is None:
|
||||
return None
|
||||
|
||||
image = model.image.get_image(repository_ref._db_id, docker_image_id)
|
||||
if image is None:
|
||||
return None
|
||||
|
||||
parent_images_map = None
|
||||
if include_parents:
|
||||
parent_images = model.image.get_parent_images(repo.namespace_user.username, repo.name, image)
|
||||
parent_images_map = {image.id: image for image in parent_images}
|
||||
|
||||
return LegacyImage.for_image(image, images_map=parent_images_map)
|
||||
|
||||
def create_manifest_label(self, manifest, key, value, source_type_name, media_type_name=None):
|
||||
""" Creates a label on the manifest with the given key and value. """
|
||||
try:
|
||||
tag_manifest = database.TagManifest.get(id=manifest._db_id)
|
||||
except database.TagManifest.DoesNotExist:
|
||||
return None
|
||||
|
||||
label = model.label.create_manifest_label(tag_manifest, key, value, source_type_name,
|
||||
media_type_name)
|
||||
return Label.for_label(label)
|
||||
|
||||
def list_manifest_labels(self, manifest, key_prefix=None):
|
||||
""" Returns all labels found on the manifest. If specified, the key_prefix will filter the
|
||||
labels returned to those keys that start with the given prefix.
|
||||
"""
|
||||
labels = model.label.list_manifest_labels(manifest._db_id, prefix_filter=key_prefix)
|
||||
return [Label.for_label(l) for l in labels]
|
||||
|
||||
def get_manifest_label(self, manifest, label_uuid):
|
||||
""" Returns the label with the specified UUID on the manifest or None if none. """
|
||||
return Label.for_label(model.label.get_manifest_label(label_uuid, manifest._db_id))
|
||||
|
||||
def delete_manifest_label(self, manifest, label_uuid):
|
||||
""" Delete the label with the specified UUID on the manifest. Returns the label deleted
|
||||
or None if none.
|
||||
"""
|
||||
return Label.for_label(model.label.delete_manifest_label(label_uuid, manifest._db_id))
|
||||
|
||||
def list_repository_tags(self, repository_ref, include_legacy_images=False):
|
||||
"""
|
||||
Returns a list of all the active tags in the repository. Note that this can be a *heavy*
|
||||
operation on repositories with a lot of tags, and should be avoided for more targetted
|
||||
operations wherever possible.
|
||||
"""
|
||||
# NOTE: include_legacy_images isn't used here because `list_active_repo_tags` includes the
|
||||
# information already, so we might as well just use it. However, the new model classes will
|
||||
# *not* include it by default, so we make it a parameter now.
|
||||
tags = model.tag.list_active_repo_tags(repository_ref._db_id)
|
||||
return [Tag.for_repository_tag(tag,
|
||||
legacy_image=LegacyImage.for_image(tag.image),
|
||||
manifest_digest=(tag.tagmanifest.digest
|
||||
if hasattr(tag, 'tagmanifest')
|
||||
else None))
|
||||
for tag in tags]
|
||||
|
||||
def list_repository_tag_history(self, repository_ref, page=1, size=100, specific_tag_name=None):
|
||||
"""
|
||||
Returns the history of all tags in the repository (unless filtered). This includes tags that
|
||||
have been made in-active due to newer versions of those tags coming into service.
|
||||
"""
|
||||
tags, manifest_map, has_more = model.tag.list_repository_tag_history(repository_ref._db_id,
|
||||
page, size,
|
||||
specific_tag_name)
|
||||
return [Tag.for_repository_tag(tag, manifest_map.get(tag.id),
|
||||
legacy_image=LegacyImage.for_image(tag.image))
|
||||
for tag in tags], has_more
|
||||
|
||||
def get_repo_tag(self, repository_ref, tag_name, include_legacy_image=False):
|
||||
"""
|
||||
Returns the latest, *active* tag found in the repository, with the matching name
|
||||
or None if none.
|
||||
"""
|
||||
tag = model.tag.get_active_tag_for_repo(repository_ref._db_id, tag_name)
|
||||
if tag is None:
|
||||
return None
|
||||
|
||||
legacy_image = LegacyImage.for_image(tag.image) if include_legacy_image else None
|
||||
tag_manifest = model.tag.get_tag_manifest(tag)
|
||||
manifest_digest = tag_manifest.digest if tag_manifest else None
|
||||
return Tag.for_repository_tag(tag, legacy_image=legacy_image, manifest_digest=manifest_digest)
|
||||
|
||||
def retarget_tag(self, repository_ref, tag_name, manifest_or_legacy_image,
|
||||
is_reversion=False):
|
||||
"""
|
||||
Creates, updates or moves a tag to a new entry in history, pointing to the manifest or
|
||||
legacy image specified. If is_reversion is set to True, this operation is considered a
|
||||
reversion over a previous tag move operation. Returns the updated Tag or None on error.
|
||||
"""
|
||||
# TODO: unify this.
|
||||
if not is_reversion:
|
||||
if isinstance(manifest_or_legacy_image, Manifest):
|
||||
raise NotImplementedError('Not yet implemented')
|
||||
else:
|
||||
model.tag.create_or_update_tag_for_repo(repository_ref._db_id, tag_name,
|
||||
manifest_or_legacy_image.docker_image_id)
|
||||
else:
|
||||
if isinstance(manifest_or_legacy_image, Manifest):
|
||||
image = model.tag.restore_tag_to_manifest(repository_ref._db_id, tag_name,
|
||||
manifest_or_legacy_image.digest)
|
||||
if image is None:
|
||||
return None
|
||||
else:
|
||||
image = model.tag.restore_tag_to_image(repository_ref._db_id, tag_name,
|
||||
manifest_or_legacy_image.docker_image_id)
|
||||
if image is None:
|
||||
return None
|
||||
|
||||
return self.get_repo_tag(repository_ref, tag_name, include_legacy_image=True)
|
||||
|
||||
def delete_tag(self, repository_ref, tag_name):
|
||||
"""
|
||||
Deletes the latest, *active* tag with the given name in the repository.
|
||||
"""
|
||||
repo = model.repository.lookup_repository(repository_ref._db_id)
|
||||
if repo is None:
|
||||
return None
|
||||
|
||||
deleted_tag = model.tag.delete_tag(repo.namespace_user.username, repo.name, tag_name)
|
||||
return Tag.for_repository_tag(deleted_tag)
|
||||
|
||||
def change_repository_tag_expiration(self, tag, expiration_date):
|
||||
""" Sets the expiration date of the tag under the matching repository to that given. If the
|
||||
expiration date is None, then the tag will not expire. Returns a tuple of the previous
|
||||
expiration timestamp in seconds (if any), and whether the operation succeeded.
|
||||
"""
|
||||
try:
|
||||
tag_obj = database.RepositoryTag.get(id=tag._db_id)
|
||||
except database.RepositoryTag.DoesNotExist:
|
||||
return (None, False)
|
||||
|
||||
return model.tag.change_tag_expiration(tag_obj, expiration_date)
|
||||
|
||||
def get_legacy_images_owned_by_tag(self, tag):
|
||||
""" Returns all legacy images *solely owned and used* by the given tag. """
|
||||
try:
|
||||
tag_obj = database.RepositoryTag.get(id=tag._db_id)
|
||||
except database.RepositoryTag.DoesNotExist:
|
||||
return None
|
||||
|
||||
# Collect the IDs of all images that the tag uses.
|
||||
tag_image_ids = set()
|
||||
tag_image_ids.add(tag_obj.image.id)
|
||||
tag_image_ids.update(tag_obj.image.ancestor_id_list())
|
||||
|
||||
# Remove any images shared by other tags.
|
||||
for current_tag in model.tag.list_active_repo_tags(tag_obj.repository_id):
|
||||
if current_tag == tag_obj:
|
||||
continue
|
||||
|
||||
tag_image_ids.discard(current_tag.image.id)
|
||||
tag_image_ids = tag_image_ids.difference(current_tag.image.ancestor_id_list())
|
||||
if not tag_image_ids:
|
||||
return []
|
||||
|
||||
if not tag_image_ids:
|
||||
return []
|
||||
|
||||
# Load the images we need to return.
|
||||
images = database.Image.select().where(database.Image.id << list(tag_image_ids))
|
||||
all_image_ids = set()
|
||||
for image in images:
|
||||
all_image_ids.add(image.id)
|
||||
all_image_ids.update(image.ancestor_id_list())
|
||||
|
||||
# Build a map of all the images and their parents.
|
||||
images_map = {}
|
||||
all_images = database.Image.select().where(database.Image.id << list(all_image_ids))
|
||||
for image in all_images:
|
||||
images_map[image.id] = image
|
||||
|
||||
return [LegacyImage.for_image(image, images_map=images_map) for image in images]
|
||||
|
||||
def get_security_status(self, manifest_or_legacy_image):
|
||||
""" Returns the security status for the given manifest or legacy image or None if none. """
|
||||
image = None
|
||||
|
||||
if isinstance(manifest_or_legacy_image, Manifest):
|
||||
try:
|
||||
tag_manifest = database.TagManifest.get(id=manifest_or_legacy_image._db_id)
|
||||
image = tag_manifest.tag.image
|
||||
except database.TagManifest.DoesNotExist:
|
||||
return None
|
||||
else:
|
||||
try:
|
||||
image = database.Image.get(id=manifest_or_legacy_image._db_id)
|
||||
except database.Image.DoesNotExist:
|
||||
return None
|
||||
|
||||
if image.security_indexed_engine is not None and image.security_indexed_engine >= 0:
|
||||
return SecurityScanStatus.SCANNED if image.security_indexed else SecurityScanStatus.FAILED
|
||||
|
||||
return SecurityScanStatus.QUEUED
|
||||
|
||||
|
||||
pre_oci_model = PreOCIModel()
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
from datetime import datetime, timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
from playhouse.test_utils import assert_query_count
|
||||
|
||||
from data import model
|
||||
from data.registry_model.registry_pre_oci_model import PreOCIModel
|
||||
from data.registry_model.datatypes import RepositoryReference
|
||||
|
||||
from test.fixtures import *
|
||||
|
||||
@pytest.fixture()
|
||||
|
@ -28,7 +33,7 @@ def test_find_matching_tag(names, expected, pre_oci_model):
|
|||
|
||||
|
||||
@pytest.mark.parametrize('repo_namespace, repo_name, expected', [
|
||||
('devtable', 'simple', {'latest'}),
|
||||
('devtable', 'simple', {'latest', 'prod'}),
|
||||
('buynlarge', 'orgrepo', {'latest', 'prod'}),
|
||||
])
|
||||
def test_get_most_recent_tag(repo_namespace, repo_name, expected, pre_oci_model):
|
||||
|
@ -39,3 +44,274 @@ def test_get_most_recent_tag(repo_namespace, repo_name, expected, pre_oci_model)
|
|||
assert found is None
|
||||
else:
|
||||
assert found.name in expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('repo_namespace, repo_name, expected', [
|
||||
('devtable', 'simple', True),
|
||||
('buynlarge', 'orgrepo', True),
|
||||
('buynlarge', 'unknownrepo', False),
|
||||
])
|
||||
def test_lookup_repository(repo_namespace, repo_name, expected, pre_oci_model):
|
||||
repo_ref = pre_oci_model.lookup_repository(repo_namespace, repo_name)
|
||||
if expected:
|
||||
assert repo_ref
|
||||
else:
|
||||
assert repo_ref is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('repo_namespace, repo_name', [
|
||||
('devtable', 'simple'),
|
||||
('buynlarge', 'orgrepo'),
|
||||
])
|
||||
def test_lookup_manifests(repo_namespace, repo_name, pre_oci_model):
|
||||
repo = model.repository.get_repository(repo_namespace, repo_name)
|
||||
repository_ref = RepositoryReference.for_repo_obj(repo)
|
||||
found_tag = pre_oci_model.find_matching_tag(repository_ref, ['latest'])
|
||||
found_manifest = pre_oci_model.get_manifest_for_tag(found_tag)
|
||||
found = pre_oci_model.lookup_manifest_by_digest(repository_ref, found_manifest.digest,
|
||||
include_legacy_image=True)
|
||||
assert found._db_id == found_manifest._db_id
|
||||
assert found.digest == found_manifest.digest
|
||||
assert found.legacy_image
|
||||
|
||||
|
||||
def test_lookup_unknown_manifest(pre_oci_model):
|
||||
repo = model.repository.get_repository('devtable', 'simple')
|
||||
repository_ref = RepositoryReference.for_repo_obj(repo)
|
||||
found = pre_oci_model.lookup_manifest_by_digest(repository_ref, 'sha256:deadbeef')
|
||||
assert found is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('repo_namespace, repo_name', [
|
||||
('devtable', 'simple'),
|
||||
('devtable', 'complex'),
|
||||
('devtable', 'history'),
|
||||
('buynlarge', 'orgrepo'),
|
||||
])
|
||||
def test_legacy_images(repo_namespace, repo_name, pre_oci_model):
|
||||
repository_ref = pre_oci_model.lookup_repository(repo_namespace, repo_name)
|
||||
legacy_images = pre_oci_model.get_legacy_images(repository_ref)
|
||||
assert len(legacy_images)
|
||||
|
||||
found_tags = set()
|
||||
for image in legacy_images:
|
||||
found_image = pre_oci_model.get_legacy_image(repository_ref, image.docker_image_id,
|
||||
include_parents=True)
|
||||
|
||||
with assert_query_count(4 if found_image.parents else 3):
|
||||
found_image = pre_oci_model.get_legacy_image(repository_ref, image.docker_image_id,
|
||||
include_parents=True)
|
||||
assert found_image.docker_image_id == image.docker_image_id
|
||||
assert found_image.parents == image.parents
|
||||
|
||||
# Check that the tags list can be retrieved.
|
||||
assert image.tags is not None
|
||||
found_tags.update({tag.name for tag in image.tags})
|
||||
|
||||
# Check against the actual DB row.
|
||||
model_image = model.image.get_image(repository_ref._db_id, found_image.docker_image_id)
|
||||
assert model_image.id == found_image._db_id
|
||||
assert ([pid for pid in reversed(model_image.ancestor_id_list())] ==
|
||||
[p._db_id for p in found_image.parents])
|
||||
|
||||
# Try without parents and ensure it raises an exception.
|
||||
found_image = pre_oci_model.get_legacy_image(repository_ref, image.docker_image_id,
|
||||
include_parents=False)
|
||||
with pytest.raises(Exception):
|
||||
assert not found_image.parents
|
||||
|
||||
assert found_tags
|
||||
|
||||
unknown = pre_oci_model.get_legacy_image(repository_ref, 'unknown', include_parents=True)
|
||||
assert unknown is None
|
||||
|
||||
|
||||
def test_manifest_labels(pre_oci_model):
|
||||
repo = model.repository.get_repository('devtable', 'simple')
|
||||
repository_ref = RepositoryReference.for_repo_obj(repo)
|
||||
found_tag = pre_oci_model.find_matching_tag(repository_ref, ['latest'])
|
||||
found_manifest = pre_oci_model.get_manifest_for_tag(found_tag)
|
||||
|
||||
# Create a new label.
|
||||
created = pre_oci_model.create_manifest_label(found_manifest, 'foo', 'bar', 'api')
|
||||
assert created.key == 'foo'
|
||||
assert created.value == 'bar'
|
||||
assert created.source_type_name == 'api'
|
||||
assert created.media_type_name == 'text/plain'
|
||||
|
||||
# Ensure we can look it up.
|
||||
assert pre_oci_model.get_manifest_label(found_manifest, created.uuid) == created
|
||||
|
||||
# Ensure it is in our list of labels.
|
||||
assert created in pre_oci_model.list_manifest_labels(found_manifest)
|
||||
assert created in pre_oci_model.list_manifest_labels(found_manifest, key_prefix='fo')
|
||||
|
||||
# Ensure it is *not* in our filtered list.
|
||||
assert created not in pre_oci_model.list_manifest_labels(found_manifest, key_prefix='ba')
|
||||
|
||||
# Delete the label and ensure it is gone.
|
||||
assert pre_oci_model.delete_manifest_label(found_manifest, created.uuid)
|
||||
assert pre_oci_model.get_manifest_label(found_manifest, created.uuid) is None
|
||||
assert created not in pre_oci_model.list_manifest_labels(found_manifest)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('repo_namespace, repo_name', [
|
||||
('devtable', 'simple'),
|
||||
('devtable', 'complex'),
|
||||
('devtable', 'history'),
|
||||
('buynlarge', 'orgrepo'),
|
||||
])
|
||||
def test_repository_tags(repo_namespace, repo_name, pre_oci_model):
|
||||
repository_ref = pre_oci_model.lookup_repository(repo_namespace, repo_name)
|
||||
|
||||
with assert_query_count(1):
|
||||
tags = pre_oci_model.list_repository_tags(repository_ref, include_legacy_images=True)
|
||||
assert len(tags)
|
||||
|
||||
for tag in tags:
|
||||
with assert_query_count(2):
|
||||
found_tag = pre_oci_model.get_repo_tag(repository_ref, tag.name, include_legacy_image=True)
|
||||
assert found_tag == tag
|
||||
|
||||
if found_tag.legacy_image is None:
|
||||
continue
|
||||
|
||||
with assert_query_count(2):
|
||||
found_image = pre_oci_model.get_legacy_image(repository_ref,
|
||||
found_tag.legacy_image.docker_image_id)
|
||||
assert found_image == found_tag.legacy_image
|
||||
|
||||
|
||||
def test_repository_tag_history(pre_oci_model):
|
||||
repository_ref = pre_oci_model.lookup_repository('devtable', 'history')
|
||||
|
||||
with assert_query_count(2):
|
||||
history, has_more = pre_oci_model.list_repository_tag_history(repository_ref)
|
||||
assert not has_more
|
||||
assert len(history) == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize('repo_namespace, repo_name', [
|
||||
('devtable', 'simple'),
|
||||
('devtable', 'complex'),
|
||||
('devtable', 'history'),
|
||||
('buynlarge', 'orgrepo'),
|
||||
])
|
||||
def test_delete_tags(repo_namespace, repo_name, pre_oci_model):
|
||||
repository_ref = pre_oci_model.lookup_repository(repo_namespace, repo_name)
|
||||
tags = pre_oci_model.list_repository_tags(repository_ref)
|
||||
assert len(tags)
|
||||
|
||||
# Save history before the deletions.
|
||||
previous_history, _ = pre_oci_model.list_repository_tag_history(repository_ref, size=1000)
|
||||
assert len(previous_history) >= len(tags)
|
||||
|
||||
# Delete every tag in the repository.
|
||||
for tag in tags:
|
||||
assert pre_oci_model.delete_tag(repository_ref, tag.name)
|
||||
|
||||
# Make sure the tag is no longer found.
|
||||
with assert_query_count(1):
|
||||
found_tag = pre_oci_model.get_repo_tag(repository_ref, tag.name, include_legacy_image=True)
|
||||
assert found_tag is None
|
||||
|
||||
# Ensure all tags have been deleted.
|
||||
tags = pre_oci_model.list_repository_tags(repository_ref)
|
||||
assert not len(tags)
|
||||
|
||||
# Ensure that the tags all live in history.
|
||||
history, _ = pre_oci_model.list_repository_tag_history(repository_ref, size=1000)
|
||||
assert len(history) == len(previous_history)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('use_manifest', [
|
||||
True,
|
||||
False,
|
||||
])
|
||||
def test_retarget_tag_history(use_manifest, pre_oci_model):
|
||||
repository_ref = pre_oci_model.lookup_repository('devtable', 'history')
|
||||
history, _ = pre_oci_model.list_repository_tag_history(repository_ref)
|
||||
|
||||
if use_manifest:
|
||||
manifest_or_legacy_image = pre_oci_model.lookup_manifest_by_digest(repository_ref,
|
||||
history[1].manifest_digest,
|
||||
allow_dead=True)
|
||||
else:
|
||||
manifest_or_legacy_image = history[1].legacy_image
|
||||
|
||||
# Retarget the tag.
|
||||
assert manifest_or_legacy_image
|
||||
updated_tag = pre_oci_model.retarget_tag(repository_ref, 'latest', manifest_or_legacy_image,
|
||||
is_reversion=True)
|
||||
|
||||
# Ensure the tag has changed targets.
|
||||
if use_manifest:
|
||||
assert updated_tag.manifest_digest == manifest_or_legacy_image.digest
|
||||
else:
|
||||
assert updated_tag.legacy_image == manifest_or_legacy_image
|
||||
|
||||
# Ensure history has been updated.
|
||||
new_history, _ = pre_oci_model.list_repository_tag_history(repository_ref)
|
||||
assert len(new_history) == len(history) + 1
|
||||
|
||||
|
||||
def test_retarget_tag(pre_oci_model):
|
||||
repository_ref = pre_oci_model.lookup_repository('devtable', 'complex')
|
||||
history, _ = pre_oci_model.list_repository_tag_history(repository_ref)
|
||||
|
||||
prod_tag = pre_oci_model.get_repo_tag(repository_ref, 'prod', include_legacy_image=True)
|
||||
|
||||
# Retarget the tag.
|
||||
updated_tag = pre_oci_model.retarget_tag(repository_ref, 'latest', prod_tag.legacy_image)
|
||||
|
||||
# Ensure the tag has changed targets.
|
||||
assert updated_tag.legacy_image == prod_tag.legacy_image
|
||||
|
||||
# Ensure history has been updated.
|
||||
new_history, _ = pre_oci_model.list_repository_tag_history(repository_ref)
|
||||
assert len(new_history) == len(history) + 1
|
||||
|
||||
|
||||
def test_change_repository_tag_expiration(pre_oci_model):
|
||||
repository_ref = pre_oci_model.lookup_repository('devtable', 'simple')
|
||||
tag = pre_oci_model.get_repo_tag(repository_ref, 'latest')
|
||||
assert tag.lifetime_end_ts is None
|
||||
|
||||
new_datetime = datetime.utcnow() + timedelta(days=2)
|
||||
previous, okay = pre_oci_model.change_repository_tag_expiration(tag, new_datetime)
|
||||
|
||||
assert okay
|
||||
assert previous is None
|
||||
|
||||
tag = pre_oci_model.get_repo_tag(repository_ref, 'latest')
|
||||
assert tag.lifetime_end_ts is not None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('repo_namespace, repo_name, expected_non_empty', [
|
||||
('devtable', 'simple', []),
|
||||
('devtable', 'complex', ['prod', 'v2.0']),
|
||||
('devtable', 'history', ['latest']),
|
||||
('buynlarge', 'orgrepo', []),
|
||||
('devtable', 'gargantuan', ['v2.0', 'v3.0', 'v4.0', 'v5.0', 'v6.0']),
|
||||
])
|
||||
def test_get_legacy_images_owned_by_tag(repo_namespace, repo_name, expected_non_empty,
|
||||
pre_oci_model):
|
||||
repository_ref = pre_oci_model.lookup_repository(repo_namespace, repo_name)
|
||||
tags = pre_oci_model.list_repository_tags(repository_ref)
|
||||
assert len(tags)
|
||||
|
||||
non_empty = set()
|
||||
for tag in tags:
|
||||
if pre_oci_model.get_legacy_images_owned_by_tag(tag):
|
||||
non_empty.add(tag.name)
|
||||
|
||||
assert non_empty == set(expected_non_empty)
|
||||
|
||||
|
||||
def test_get_security_status(pre_oci_model):
|
||||
repository_ref = pre_oci_model.lookup_repository('devtable', 'simple')
|
||||
tags = pre_oci_model.list_repository_tags(repository_ref, include_legacy_images=True)
|
||||
assert len(tags)
|
||||
|
||||
for tag in tags:
|
||||
assert pre_oci_model.get_security_status(tag.legacy_image)
|
||||
|
|
Reference in a new issue