Example #1
0
def test_get_selected_feeds_to_sync():
    default_result = ["vulnerabilities", "nvdv2"]

    matrix = [
        ({}, default_result),
        ({
            "feeds": {}
        }, default_result),
        ({
            "feeds": None
        }, default_result),
        ({
            "feeds": {
                "selective_sync": {}
            }
        }, default_result),
        ({
            "feeds": {
                "selective_sync": None
            }
        }, default_result),
        ({
            "feeds": {
                "selective_sync": {
                    "enabled": False
                }
            }
        }, default_result),
    ]

    for input, output in matrix:
        assert get_selected_feeds_to_sync(input) == output
Example #2
0
def do_feed_sync(msg):
    if 'FeedsUpdateTask' not in locals():
        from anchore_engine.services.policy_engine.engine.tasks import FeedsUpdateTask

    if 'get_selected_feeds_to_sync' not in locals():
        from anchore_engine.services.policy_engine.engine.feeds.sync import get_selected_feeds_to_sync

    handler_success = False
    timer = time.time()
    logger.info("FIRING: feed syncer")
    try:
        feeds = get_selected_feeds_to_sync(localconfig.get_config())
        logger.info('Syncing configured feeds: {}'.format(feeds))
        result = FeedsUpdateTask.run_feeds_update(json_obj=msg.get('data'))

        if result is not None:
            handler_success = True
        else:
            logger.warn('Feed sync task marked as disabled, so skipping')
    except ValueError as e:
        logger.warn('Received msg of wrong type')
    except Exception as err:
        logger.warn("failure in feed sync handler - exception: " + str(err))

    if handler_success:
        anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function='do_feed_sync', status="success")
    else:
        anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function='do_feed_sync', status="fail")
Example #3
0
def test_get_selected_feeds_to_sync():
    default_result = ['vulnerabilities', 'nvdv2']

    matrix = [({}, default_result), ({
        'feeds': {}
    }, default_result), ({
        'feeds': None
    }, default_result), ({
        'feeds': {
            'selective_sync': {}
        }
    }, default_result), ({
        'feeds': {
            'selective_sync': None
        }
    }, default_result),
              ({
                  'feeds': {
                      'selective_sync': {
                          'enabled': False
                      }
                  }
              }, default_result)]

    for input, output in matrix:
        assert get_selected_feeds_to_sync(input) == output
Example #4
0
    def run_feeds_update(cls, json_obj=None, force_flush=False):
        """
        Creates a task and runs it, optionally with a thread if locking is enabled.

        :return:
        """
        feeds = None

        try:

            feeds = get_selected_feeds_to_sync(localconfig.get_config())
            if json_obj:
                task = cls.from_json(json_obj)
                if not task:
                    return None
                task.feeds = feeds
            else:
                task = FeedsUpdateTask(feeds_to_sync=feeds, flush=force_flush)

            result = []
            if cls.locking_enabled:
                run_target_with_lease(
                    account=None,
                    lease_id="feed_sync",
                    ttl=90,
                    target=lambda: result.append(task.execute()),
                )
                # A bit of work-around for the lambda def to get result from thread execution
                if result:
                    result = result[0]
            else:
                result = task.execute()

            return result
        except Exception:
            logger.exception("Error executing feeds update")
            raise