def add_distro_mapping(distro_mapping):
    """
    POST /distro_mappings

    :param distro_mapping: a DistroMapping object from request body
    :return: listing of all distro mappings
    """
    dist_map = DistroMapping.from_dict(distro_mapping)
    db = get_session()
    try:
        new_mapping = DbDistroMapping()
        new_mapping.created_at = datetime.datetime.utcnow()
        new_mapping.from_distro = dist_map.from_distro
        new_mapping.to_distro = dist_map.to_distro
        new_mapping.flavor = dist_map.flavor
        db.add(new_mapping)
        db.commit()
    except IntegrityError as e:
        log.warn('Insertion of existing mapping name')
        db.rollback()
        abort(409)
    except Exception as e:
        log.exception('Error inserting new distro mapping')
        db.rollback()
        abort(500)

    return list_distro_mappings()
示例#2
0
def _init_distro_mappings():
    from anchore_engine.db import session_scope, DistroMapping

    initial_mappings = [
        DistroMapping(from_distro='alpine', to_distro='alpine', flavor='ALPINE'),
        DistroMapping(from_distro='busybox', to_distro='busybox', flavor='BUSYB'),
        DistroMapping(from_distro='centos', to_distro='centos', flavor='RHEL'),
        DistroMapping(from_distro='debian', to_distro='debian', flavor='DEB'),
        DistroMapping(from_distro='fedora', to_distro='centos', flavor='RHEL'),
        DistroMapping(from_distro='ol', to_distro='ol', flavor='RHEL'),
        DistroMapping(from_distro='rhel', to_distro='centos', flavor='RHEL'),
        DistroMapping(from_distro='ubuntu', to_distro='ubuntu', flavor='DEB'),
        #DistroMapping(from_distro='java', to_distro='snyk', flavor='JAVA'),
        #DistroMapping(from_distro='gem', to_distro='snyk', flavor='RUBY'),
        #DistroMapping(from_distro='npm', to_distro='snyk', flavor='NODEJS'),
        #DistroMapping(from_distro='python', to_distro='snyk', flavor='PYTHON'),
    ]

    # set up any data necessary at system init
    try:
        logger.info('Checking policy engine db initialization. Checking initial set of distro mappings')
        with session_scope() as dbsession:
            distro_mappings = dbsession.query(DistroMapping).all()

            for i in initial_mappings:
                if not [x for x in distro_mappings if x.from_distro == i.from_distro]:
                    logger.info('Adding missing mapping: {}'.format(i))
                    dbsession.add(i)

        logger.info('Distro mapping initialization complete')
    except Exception as err:
        raise Exception("unable to initialize default distro mappings - exception: " + str(err))

    return True
示例#3
0
def init_distro_mappings():
    """
    Initializes the distro mappings, needed for lots of operation tests.
    :return:
    """

    from anchore_engine.db import session_scope, DistroMapping

    initial_mappings = [
        DistroMapping(from_distro="alpine", to_distro="alpine", flavor="ALPINE"),
        DistroMapping(from_distro="busybox", to_distro="busybox", flavor="BUSYB"),
        DistroMapping(from_distro="centos", to_distro="centos", flavor="RHEL"),
        DistroMapping(from_distro="debian", to_distro="debian", flavor="DEB"),
        DistroMapping(from_distro="fedora", to_distro="centos", flavor="RHEL"),
        DistroMapping(from_distro="ol", to_distro="ol", flavor="RHEL"),
        DistroMapping(from_distro="rhel", to_distro="centos", flavor="RHEL"),
        DistroMapping(from_distro="ubuntu", to_distro="ubuntu", flavor="DEB"),
    ]

    # set up any data necessary at system init
    try:
        with session_scope() as dbsession:
            distro_mappings = dbsession.query(DistroMapping).all()

            for i in initial_mappings:
                if not [x for x in distro_mappings if x.from_distro == i.from_distro]:
                    dbsession.add(i)
    except Exception as err:
        raise Exception(
            "unable to initialize default distro mappings - exception: " + str(err)
        )
示例#4
0
def init_distro_mappings():
    """
    Initializes the distro mappings, needed for lots of operation tests.
    :return:
    """

    from anchore_engine.db import session_scope, DistroMapping

    initial_mappings = [
        DistroMapping(from_distro='alpine', to_distro='alpine', flavor='ALPINE'),
        DistroMapping(from_distro='busybox', to_distro='busybox', flavor='BUSYB'),
        DistroMapping(from_distro='centos', to_distro='centos', flavor='RHEL'),
        DistroMapping(from_distro='debian', to_distro='debian', flavor='DEB'),
        DistroMapping(from_distro='fedora', to_distro='centos', flavor='RHEL'),
        DistroMapping(from_distro='ol', to_distro='ol', flavor='RHEL'),
        DistroMapping(from_distro='rhel', to_distro='centos', flavor='RHEL'),
        DistroMapping(from_distro='ubuntu', to_distro='ubuntu', flavor='DEB')
    ]

    # set up any data necessary at system init
    try:
        with session_scope() as dbsession:
            distro_mappings = dbsession.query(DistroMapping).all()

            for i in initial_mappings:
                if not filter(lambda x: x.from_distro == i.from_distro, distro_mappings):
                    dbsession.add(i)
    except Exception as err:
        raise Exception("unable to initialize default distro mappings - exception: " + str(err))
示例#5
0
def _init_distro_mappings():
    from anchore_engine.db import DistroMapping, session_scope

    initial_mappings = [
        DistroMapping(from_distro="alpine",
                      to_distro="alpine",
                      flavor="ALPINE"),
        DistroMapping(from_distro="busybox",
                      to_distro="busybox",
                      flavor="BUSYB"),
        DistroMapping(from_distro="centos", to_distro="rhel", flavor="RHEL"),
        DistroMapping(from_distro="debian", to_distro="debian", flavor="DEB"),
        DistroMapping(from_distro="fedora", to_distro="rhel", flavor="RHEL"),
        DistroMapping(from_distro="ol", to_distro="ol", flavor="RHEL"),
        DistroMapping(from_distro="rhel", to_distro="rhel", flavor="RHEL"),
        DistroMapping(from_distro="ubuntu", to_distro="ubuntu", flavor="DEB"),
        DistroMapping(from_distro="amzn", to_distro="amzn", flavor="RHEL"),
        DistroMapping(from_distro="redhat", to_distro="rhel", flavor="RHEL"),
    ]

    # set up any data necessary at system init
    try:
        logger.info(
            "Checking policy engine db initialization. Checking initial set of distro mappings"
        )

        with session_scope() as dbsession:
            distro_mappings = dbsession.query(DistroMapping).all()

            for i in initial_mappings:
                if not [
                        x for x in distro_mappings
                        if x.from_distro == i.from_distro
                ]:
                    logger.info("Adding missing mapping: {}".format(i))
                    dbsession.add(i)

        logger.info("Distro mapping initialization complete")
    except Exception as err:

        if isinstance(err, IntegrityError):
            logger.warn("another process has already initialized, continuing")
        else:
            raise Exception(
                "unable to initialize default distro mappings - exception: " +
                str(err))

    return True
示例#6
0
def _init_distro_mappings():
    from anchore_engine.db import session_scope, DistroMapping

    initial_mappings = [
        DistroMapping(from_distro='alpine',
                      to_distro='alpine',
                      flavor='ALPINE'),
        DistroMapping(from_distro='busybox',
                      to_distro='busybox',
                      flavor='BUSYB'),
        DistroMapping(from_distro='centos', to_distro='rhel', flavor='RHEL'),
        DistroMapping(from_distro='debian', to_distro='debian', flavor='DEB'),
        DistroMapping(from_distro='fedora', to_distro='rhel', flavor='RHEL'),
        DistroMapping(from_distro='ol', to_distro='ol', flavor='RHEL'),
        DistroMapping(from_distro='rhel', to_distro='rhel', flavor='RHEL'),
        DistroMapping(from_distro='ubuntu', to_distro='ubuntu', flavor='DEB'),
        DistroMapping(from_distro='amzn', to_distro='amzn', flavor='RHEL')
    ]

    # set up any data necessary at system init
    try:
        logger.info(
            'Checking policy engine db initialization. Checking initial set of distro mappings'
        )

        with session_scope() as dbsession:
            distro_mappings = dbsession.query(DistroMapping).all()

            for i in initial_mappings:
                if not [
                        x for x in distro_mappings
                        if x.from_distro == i.from_distro
                ]:
                    logger.info('Adding missing mapping: {}'.format(i))
                    dbsession.add(i)

        logger.info('Distro mapping initialization complete')
    except Exception as err:

        if isinstance(err, IntegrityError):
            logger.warn("another process has already initialized, continuing")
        else:
            raise Exception(
                "unable to initialize default distro mappings - exception: " +
                str(err))

    return True
示例#7
0
def test_distromappings(anchore_db):
    _init_distro_mappings()

    c7 = DistroNamespace(name='centos', version='7', like_distro='centos')
    assert c7.mapped_names() == []
    assert c7.like_namespace_names == ['rhel:7']

    r7 = DistroNamespace(name='rhel', version='7', like_distro='centos')
    assert set(r7.mapped_names()) == {'centos', 'fedora', 'rhel'}
    assert r7.like_namespace_names == ['rhel:7']

    assert sorted(DistroMapping.distros_mapped_to('rhel', '7')) == sorted([
        DistroTuple('rhel', '7', 'RHEL'),
        DistroTuple('centos', '7', 'RHEL'),
        DistroTuple('fedora', '7', 'RHEL')
    ])