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
|
|
|
|
from endpoints.api import (require_repo_read, NotFound, DownstreamIssue, path_param,
|
|
|
|
RepositoryParamResource, resource, nickname, show_if, parse_args,
|
2016-02-17 19:48:50 +00:00
|
|
|
query_param, truthy_bool)
|
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):
|
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
|
|
|
|
|
|
|
|
|
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-02-17 19:48:50 +00:00
|
|
|
""" Fetches the features and vulnerabilities (if any) for a repository tag. """
|
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()
|
|
|
|
|
2015-11-11 20:52:30 +00:00
|
|
|
if not repo_image.security_indexed:
|
|
|
|
logger.debug('Image %s under repository %s/%s not security indexed',
|
|
|
|
repo_image.docker_image_id, namespace, repository)
|
2015-10-23 19:20:28 +00:00
|
|
|
return {
|
2015-11-12 20:42:45 +00:00
|
|
|
'status': _get_status(repo_image),
|
2015-10-23 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 21:01:27 +00:00
|
|
|
try:
|
|
|
|
if parsed_args.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:
|
2016-02-25 22:45:49 +00:00
|
|
|
raise DownstreamIssue({'message': arf.message})
|
2016-02-24 21:01:27 +00:00
|
|
|
|
|
|
|
if data is None:
|
|
|
|
raise NotFound()
|
2015-10-23 19:20:28 +00:00
|
|
|
|
|
|
|
return {
|
2015-11-12 20:42:45 +00:00
|
|
|
'status': _get_status(repo_image),
|
2015-10-23 19:20:28 +00:00
|
|
|
'data': data,
|
|
|
|
}
|