Exemplo n.º 1
0
    def testImageImport(self):
        log.setLevel(logging.ERROR)
        log.setLevel(logging.INFO)

        for f in test_env.image_exports():
            log.info('Testing image export loading into the db')

            with open(f[1]) as infile:
                json_data = json.load(infile)
                image_id = json_data[0]['image']['imagedata']['image_report'][
                    'meta']['imageId'] if type(
                        json_data
                    ) == list else json_data['image_report']['meta']['imageId']
                log.info('Using image id: ' + image_id)

            t = time.time()
            task = ImageLoadTask(user_id='0',
                                 image_id=image_id,
                                 url='file:///' + f[1],
                                 force_reload=True)
            load_result = task.execute()
            load_duration = time.time() - t
            log.info(
                'Load complete for {}. Took: {} sec for db load. Result: {}'.
                format(f, load_duration, load_result))
Exemplo n.º 2
0
def _load_image(name, test_env):
    logger.info('Loading image: {}'.format(name))
    imgs = test_env.get_images_named(name)
    t = ImageLoadTask(user_id='0', image_id=imgs[0][0])
    t.fetch_url = 'file://' + os.path.join(test_env.images_dir,
                                           imgs[0][1]['path'])
    t.execute()
Exemplo n.º 3
0
def test_image_load(test_data_env):
    for f in test_data_env.image_exports():
        logger.info('Testing image export loading into the db')

        with open(f[1]) as infile:
            json_data = json.load(infile)
            image_id = json_data[0]['image']['imagedata']['image_report'][
                'meta']['imageId'] if type(
                    json_data
                ) == list else json_data['image_report']['meta']['imageId']
            logger.info('Using image id: ' + image_id)

        t = time.time()
        task = ImageLoadTask(user_id='0',
                             image_id=image_id,
                             url='file://' + f[1],
                             force_reload=True)
        load_result = task.execute()
        load_duration = time.time() - t
        logger.info(
            'Load complete for {}. Took: {} sec for db load. Result: {}'.
            format(f, load_duration, load_result))

        with session_scope() as db:
            assert db.query(Image).filter_by(
                id=image_id, user_id='0').one_or_none() is not None
Exemplo n.º 4
0
def test_image_load(test_data_env):
    for f in test_data_env.image_exports():
        logger.info("Testing image export loading into the db")

        with open(f[1]) as infile:
            json_data = json.load(infile)
            image_id = (json_data[0]["image"]["imagedata"]["image_report"]
                        ["meta"]["imageId"] if type(json_data) == list else
                        json_data["image_report"]["meta"]["imageId"])
            logger.info("Using image id: " + image_id)

        t = time.time()
        task = ImageLoadTask(user_id="0",
                             image_id=image_id,
                             url="file://" + f[1],
                             force_reload=True)
        load_result = task.execute()
        load_duration = time.time() - t
        logger.info(
            "Load complete for {}. Took: {} sec for db load. Result: {}".
            format(f, load_duration, load_result))

        with session_scope() as db:
            assert (db.query(Image).filter_by(id=image_id,
                                              user_id="0").one_or_none()
                    is not None)
Exemplo n.º 5
0
 def load_images_from_env(cls):
     for image_id, path in cls.test_env.image_exports():
         print(('Ensuring loaded: image id: {} from file: {}'.format(
             image_id, path)))
         t = ImageLoadTask(image_id=image_id,
                           user_id='0',
                           url='file://' + path)
         t.execute()
Exemplo n.º 6
0
def load_images(request):
    for image_id, path in request.cls.test_env.image_exports():
        logger.info('Ensuring loaded: image id: {} from file: {}'.format(
            image_id, path))
        t = ImageLoadTask(image_id=image_id, user_id='0', url='file://' + path)
        t.execute()

    db = get_thread_scoped_session()
    test_image = db.query(Image).get((request.cls.test_env.get_images_named(
        request.cls.__default_image__)[0][0], '0'))
    request.cls.test_image = test_image
    db.rollback()
Exemplo n.º 7
0
def cls_fully_loaded_test_env(cls_test_data_env2, request):
    """
    Load the test env, including a feed sync and image analysis. Places the env in the class's test_env and test_image vars

    :param cls_test_data_env:
    :param request:
    :return:
    """
    _init_distro_mappings()

    from anchore_engine.services.policy_engine.engine.tasks import FeedsUpdateTask
    t = FeedsUpdateTask(feeds_to_sync=[
        'vulnerabilities', 'packages', 'nvdv2', 'nvd', 'vulndb'
    ])
    t.execute()

    for image_id, path in request.cls.test_env.image_exports():
        logger.info(('Ensuring loaded: image id: {} from file: {}'.format(
            image_id, path)))
        t = ImageLoadTask(image_id=image_id, user_id='0', url='file://' + path)
        t.execute()

    db = get_thread_scoped_session()
    test_image = db.query(Image).get((request.cls.test_env.get_images_named(
        request.cls.__default_image__)[0][0], '0'))
    request.cls.test_image = test_image
    db.rollback()
def cls_no_feeds_test_env(cls_test_data_env2, request):
    """
    Same as fully_loaded_test_env but does not sync feeds

    :param cls_test_data_env:
    :param request:
    :return:
    """
    _init_distro_mappings()

    for image_id, path in request.cls.test_env.image_exports():
        logger.info(('Ensuring loaded: image id: {} from file: {}'.format(image_id, path)))
        t = ImageLoadTask(image_id=image_id, user_id='0', url='file://' + path)
        t.execute()

    db = get_thread_scoped_session()
    test_image = db.query(Image).get((request.cls.test_env.get_images_named(request.cls.__default_image__)[0][0], '0'))
    request.cls.test_image = test_image
    db.rollback()
Exemplo n.º 9
0
def ingress_image(ingress_request):
    """
    :param ingress_request json object specifying the identity of the image to sync
    :return: status result for image load
    """
    if not connexion.request.is_json:
        abort(400)

    req = ImageIngressRequest.from_dict(ingress_request)
    try:
        # Try this synchronously for now to see how slow it really is
        t = ImageLoadTask(req.user_id, req.image_id, url=req.fetch_url)
        result = t.execute()
        resp = ImageIngressResponse()
        if not result:
            resp.status = 'loaded'
        else:
            # We're doing a sync call above, so just send loaded. It should be 'accepted' once async works.
            resp.status = 'loaded'
        return resp.to_dict(), 200
    except Exception as e:
        abort(500, 'Internal error processing image analysis import')
def _load_images(test_env):
    logger.info('Loading images')
    image_results = []
    try:
        for img_id, path in test_env.image_exports():
            logger.info('Loading {}'.format(img_id))
            file_url = 'file://' + path
            i = ImageLoadTask(user_id='0', image_id=img_id, url=file_url).execute()
            if not i:
                logger.info('Could not load image {}, already in system, ot an exception'.format(img_id))
        logger.info('Load complete')
    finally:
        end_session()
def ingress_image(ingress_request):
    """
    :param ingress_request json object specifying the identity of the image to sync
    :return: status result for image load
    """

    req = ImageIngressRequest.from_json(ingress_request)
    if not req.user_id:
        raise ValueError("user_id")
    if not req.image_id:
        raise ValueError("image_id")

    try:
        # Try this synchronously for now to see how slow it really is
        conn_timeout = ApiRequestContextProxy.get_service().configuration.get(
            "catalog_client_conn_timeout", DEFAULT_CACHE_CONN_TIMEOUT
        )
        read_timeout = ApiRequestContextProxy.get_service().configuration.get(
            "catalog_client_read_timeout", DEFAULT_CACHE_READ_TIMEOUT
        )
        t = ImageLoadTask(
            req.user_id,
            req.image_id,
            url=req.fetch_url,
            content_conn_timeout=conn_timeout,
            content_read_timeout=read_timeout,
        )
        result = t.execute()
        resp = ImageIngressResponse()
        if not result:
            resp.status = "loaded"
        else:
            # We're doing a sync call above, so just send loaded. It should be 'accepted' once async works.
            resp.status = "loaded"
        resp.problems = list()
        return resp.to_json(), 200
    except Exception as e:
        log.exception("Error loading image into policy engine")
        return make_response_error(e, in_httpcode=500), 500
Exemplo n.º 12
0
def _load_image(name, test_env):
    logger.info("Loading image: {}".format(name))
    imgs = test_env.get_images_named(name)
    t = ImageLoadTask(user_id="0", image_id=imgs[0][0])
    t.fetch_url = "file://" + os.path.join(test_env.images_dir, imgs[0][1]["path"])
    t.execute()
Exemplo n.º 13
0
def _load_images(test_env):
    for img_id, path in test_env.image_exports():
        t = ImageLoadTask(user_id="0", image_id=img_id)
        t.fetch_url = "file://" + path
        t.execute()
Exemplo n.º 14
0
 def load_images(self):
     self.test_env.image_exports()
     for img_id, path in self.test_env.image_exports():
         t = ImageLoadTask(user_id='0', image_id=img_id)
         t.fetch_url = 'file://' + path 
         t.execute()
Exemplo n.º 15
0
 def load_images(self):
     img = self.test_env.get_images_named('node')[0][0]
     t = ImageLoadTask(user_id='0', image_id=img)
     t.execute()
Exemplo n.º 16
0
 def load_images(self):
     for img in self.test_image_ids.values():
         t = ImageLoadTask(user_id='0', image_id=img)
         t.execute()