2016-02-17 19:48:50 +00:00
|
|
|
""" List and manage repository vulnerabilities and other security information. """
|
2015-10-23 19:20:28 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import features
|
|
|
|
|
2015-11-10 20:01:33 +00:00
|
|
|
from app import secscan_api
|
2015-10-23 19:20:28 +00:00
|
|
|
from data import model
|
2016-04-11 20:20:11 +00:00
|
|
|
from endpoints.api import (require_repo_read, path_param,
|
2015-10-23 19:20:28 +00:00
|
|
|
RepositoryParamResource, resource, nickname, show_if, parse_args,
|
2016-02-17 19:48:50 +00:00
|
|
|
query_param, truthy_bool)
|
2016-04-11 20:20:11 +00:00
|
|
|
from endpoints.exception import NotFound, DownstreamIssue
|
2017-02-02 22:51:18 +00:00
|
|
|
from endpoints.api.manifest import MANIFEST_DIGEST_ROUTE
|
2016-02-24 21:01:27 +00:00
|
|
|
from util.secscan.api import APIRequestFailure
|
2015-10-23 19:20:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-10-26 19:13:58 +00:00
|
|
|
|
2015-11-12 20:42:45 +00:00
|
|
|
class SCAN_STATUS(object):
|
|
|
|
""" Security scan status enum """
|
|
|
|
SCANNED = 'scanned'
|
|
|
|
FAILED = 'failed'
|
|
|
|
QUEUED = 'queued'
|
|
|
|
|
|
|
|
|
|
|
|
def _get_status(repo_image):
|
2017-02-02 22:51:18 +00:00
|
|
|
""" Returns the SCAN_STATUS value for the given image. """
|
2015-11-13 06:06:18 +00:00
|
|
|
if repo_image.security_indexed_engine is not None and repo_image.security_indexed_engine >= 0:
|
2015-11-12 20:42:45 +00:00
|
|
|
return SCAN_STATUS.SCANNED if repo_image.security_indexed else SCAN_STATUS.FAILED
|
|
|
|
|
|
|
|
return SCAN_STATUS.QUEUED
|
|
|
|
|
2017-02-02 22:51:18 +00:00
|
|
|
def _security_status_for_image(namespace, repository, repo_image, include_vulnerabilities=True):
|
|
|
|
""" Returns a dict representing the result of a call to the security status API for the given
|
|
|
|
image.
|
|
|
|
"""
|
|
|
|
if not repo_image.security_indexed:
|
|
|
|
logger.debug('Image %s under repository %s/%s not security indexed',
|
|
|
|
repo_image.docker_image_id, namespace, repository)
|
|
|
|
return {
|
|
|
|
'status': _get_status(repo_image),
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
if include_vulnerabilities:
|
|
|
|
data = secscan_api.get_layer_data(repo_image, include_vulnerabilities=True)
|
|
|
|
else:
|
|
|
|
data = secscan_api.get_layer_data(repo_image, include_features=True)
|
|
|
|
except APIRequestFailure as arf:
|
|
|
|
raise DownstreamIssue({'message': arf.message})
|
|
|
|
|
|
|
|
if data is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
return {
|
|
|
|
'status': _get_status(repo_image),
|
|
|
|
'data': data,
|
|
|
|
}
|
|
|
|
|
2015-11-12 20:42:45 +00:00
|
|
|
|
2015-10-23 19:20:28 +00:00
|
|
|
@show_if(features.SECURITY_SCANNER)
|
2016-02-17 19:48:50 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/image/<imageid>/security')
|
2015-10-23 19:20:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
2015-11-11 20:52:30 +00:00
|
|
|
@path_param('imageid', 'The image ID')
|
2016-02-17 19:48:50 +00:00
|
|
|
class RepositoryImageSecurity(RepositoryParamResource):
|
2015-11-11 20:52:30 +00:00
|
|
|
""" Operations for managing the vulnerabilities in a repository image. """
|
2015-10-23 19:20:28 +00:00
|
|
|
|
|
|
|
@require_repo_read
|
2016-02-17 19:48:50 +00:00
|
|
|
@nickname('getRepoImageSecurity')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2016-02-17 19:48:50 +00:00
|
|
|
@query_param('vulnerabilities', 'Include vulnerabilities informations', type=truthy_bool,
|
|
|
|
default=False)
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, namespace, repository, imageid, parsed_args):
|
2016-12-07 16:50:22 +00:00
|
|
|
""" Fetches the features and vulnerabilities (if any) for a repository image. """
|
2015-11-11 20:52:30 +00:00
|
|
|
repo_image = model.image.get_repo_image(namespace, repository, imageid)
|
|
|
|
if repo_image is None:
|
2015-10-23 19:20:28 +00:00
|
|
|
raise NotFound()
|
|
|
|
|
2017-02-02 22:51:18 +00:00
|
|
|
return _security_status_for_image(namespace, repository, repo_image,
|
|
|
|
parsed_args.vulnerabilities)
|
|
|
|
|
|
|
|
@show_if(features.SECURITY_SCANNER)
|
|
|
|
@resource(MANIFEST_DIGEST_ROUTE + '/security')
|
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('manifestref', 'The digest of the manifest')
|
|
|
|
class RepositoryManifestSecurity(RepositoryParamResource):
|
|
|
|
""" Operations for managing the vulnerabilities in a repository manifest. """
|
2015-10-23 19:20:28 +00:00
|
|
|
|
2017-02-02 22:51:18 +00:00
|
|
|
@require_repo_read
|
|
|
|
@nickname('getRepoManifestSecurity')
|
|
|
|
@parse_args()
|
|
|
|
@query_param('vulnerabilities', 'Include vulnerabilities informations', type=truthy_bool,
|
|
|
|
default=False)
|
|
|
|
def get(self, namespace, repository, manifestref, parsed_args):
|
2016-02-24 21:01:27 +00:00
|
|
|
try:
|
2017-02-02 22:51:18 +00:00
|
|
|
tag_manifest = model.tag.load_manifest_by_digest(namespace, repository, manifestref)
|
|
|
|
except model.DataModelException:
|
2016-02-24 21:01:27 +00:00
|
|
|
raise NotFound()
|
2015-10-23 19:20:28 +00:00
|
|
|
|
2017-02-02 22:51:18 +00:00
|
|
|
repo_image = tag_manifest.tag.image
|
|
|
|
|
|
|
|
return _security_status_for_image(namespace, repository, repo_image,
|
|
|
|
parsed_args.vulnerabilities)
|