2015-10-05 17:35:01 +00:00
|
|
|
import logging
|
2015-10-27 21:38:48 +00:00
|
|
|
import logging.config
|
|
|
|
|
2015-10-05 17:35:01 +00:00
|
|
|
import features
|
|
|
|
import time
|
|
|
|
|
2016-02-17 19:44:49 +00:00
|
|
|
from peewee import fn
|
|
|
|
|
2016-05-04 19:20:27 +00:00
|
|
|
from app import app, secscan_api
|
2015-10-05 17:35:01 +00:00
|
|
|
from workers.worker import Worker
|
2016-02-24 21:01:27 +00:00
|
|
|
from data.database import Image, UseThenDisconnect
|
|
|
|
from data.model.image import get_image_with_storage_and_parent_base
|
2015-11-11 20:41:46 +00:00
|
|
|
from util.secscan.api import SecurityConfigValidator
|
2016-02-24 21:01:27 +00:00
|
|
|
from util.secscan.analyzer import LayerAnalyzer
|
2016-02-17 19:44:49 +00:00
|
|
|
from util.migrate.allocator import yield_random_entries
|
2016-05-04 21:40:09 +00:00
|
|
|
from endpoints.v2 import v2_bp
|
2015-10-05 17:35:01 +00:00
|
|
|
|
2016-02-10 02:25:07 +00:00
|
|
|
BATCH_SIZE = 50
|
|
|
|
INDEXING_INTERVAL = 30
|
2015-10-05 17:35:01 +00:00
|
|
|
|
2016-02-17 19:44:49 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-10-05 17:35:01 +00:00
|
|
|
|
|
|
|
class SecurityWorker(Worker):
|
|
|
|
def __init__(self):
|
|
|
|
super(SecurityWorker, self).__init__()
|
2016-05-04 19:20:27 +00:00
|
|
|
validator = SecurityConfigValidator(app.config)
|
2015-11-11 20:41:46 +00:00
|
|
|
if validator.valid():
|
2016-05-04 19:20:27 +00:00
|
|
|
self._target_version = app.config.get('SECURITY_SCANNER_ENGINE_VERSION_TARGET', 2)
|
2016-02-24 21:01:27 +00:00
|
|
|
self._analyzer = LayerAnalyzer(app.config, secscan_api)
|
2015-11-11 20:41:46 +00:00
|
|
|
|
2016-03-04 17:11:40 +00:00
|
|
|
# Get the ID of the first image we want to analyze.
|
|
|
|
self._min_id = (Image
|
|
|
|
.select(fn.Min(Image.id))
|
|
|
|
.where(Image.security_indexed_engine < self._target_version)
|
|
|
|
.scalar())
|
|
|
|
|
2015-10-05 17:35:01 +00:00
|
|
|
self.add_operation(self._index_images, INDEXING_INTERVAL)
|
2015-11-17 22:42:52 +00:00
|
|
|
else:
|
|
|
|
logger.warning('Failed to validate security scan configuration')
|
2015-10-05 17:35:01 +00:00
|
|
|
|
2016-02-17 19:44:49 +00:00
|
|
|
def _index_images(self):
|
|
|
|
def batch_query():
|
|
|
|
base_query = get_image_with_storage_and_parent_base()
|
|
|
|
return base_query.where(Image.security_indexed_engine < self._target_version)
|
|
|
|
|
2016-05-04 19:20:27 +00:00
|
|
|
# Get the ID of the last image we can analyze. Will be None if there are no images in the
|
|
|
|
# database.
|
2016-02-17 19:44:49 +00:00
|
|
|
max_id = Image.select(fn.Max(Image.id)).scalar()
|
2016-05-04 19:20:27 +00:00
|
|
|
if max_id is None:
|
|
|
|
return
|
2016-02-17 19:44:49 +00:00
|
|
|
|
|
|
|
with UseThenDisconnect(app.config):
|
2016-03-04 17:11:40 +00:00
|
|
|
for candidate, abt in yield_random_entries(batch_query, Image.id, BATCH_SIZE, max_id,
|
|
|
|
self._min_id):
|
2016-02-24 21:01:27 +00:00
|
|
|
_, continue_batch = self._analyzer.analyze_recursively(candidate)
|
2016-02-17 19:44:49 +00:00
|
|
|
if not continue_batch:
|
|
|
|
logger.info('Another worker pre-empted us for layer: %s', candidate.id)
|
|
|
|
abt.set()
|
|
|
|
|
2016-03-04 17:11:40 +00:00
|
|
|
# If we reach this point, we analyzed every images up to max_id, next time the worker runs,
|
|
|
|
# we want to start from the next image.
|
|
|
|
self._min_id = max_id + 1
|
2015-10-28 20:32:46 +00:00
|
|
|
|
2015-10-05 17:35:01 +00:00
|
|
|
if __name__ == '__main__':
|
2016-05-04 21:40:09 +00:00
|
|
|
app.register_blueprint(v2_bp, url_prefix='/v2')
|
|
|
|
|
2015-10-05 17:35:01 +00:00
|
|
|
if not features.SECURITY_SCANNER:
|
2015-11-10 18:07:47 +00:00
|
|
|
logger.debug('Security scanner disabled; skipping SecurityWorker')
|
2015-10-05 17:35:01 +00:00
|
|
|
while True:
|
|
|
|
time.sleep(100000)
|
|
|
|
|
2015-10-27 21:38:48 +00:00
|
|
|
logging.config.fileConfig('conf/logging_debug.conf', disable_existing_loggers=False)
|
2015-10-05 17:35:01 +00:00
|
|
|
worker = SecurityWorker()
|
|
|
|
worker.start()
|