Esempio n. 1
0
    def makeService(self, options):
        slist = []

        try:
            configfile = os.path.join(options['config'], 'config.yaml')
            config = localconfig.read_config(configfile=configfile)
        except Exception as err:
            log.err("cannot load local configuration: " + str(err))
            raise err

        log_level = config.get('log_level', 'INFO')
        log_to_db = config.get('log_to_db', False)

        slist = self.servicenames

        try:
            config_services = config['services']

            isEnabled = False
            for sname in slist:
                if 'log_level' in config_services[sname]:
                    log_level = config_services[sname]['log_level']

                if config_services[sname]['enabled']:
                    isEnabled = True
                    break

            if not isEnabled:
                log.err("no services in list (" + str(self.servicenames) +
                        ") are enabled in configuration file: shutting down")
                sys.exit(0)

        except Exception as err:
            log.err(
                "error checking for enabled services, check config file - exception: "
                + str(err))
            raise Exception(
                "error checking for enabled services, check config file - exception: "
                + str(err))

        try:
            logger.set_log_level(log_level, log_to_db=log_to_db)
        except Exception as err:
            log.err("exception while initializing logger - exception: " +
                    str(err))
            logger.set_log_level('INFO')

        logger.enable_bootstrap_logging(name_prefix='policy_engine')
        r = anchore_engine.services.common.makeService(slist,
                                                       options,
                                                       bootstrap_db=True)
        logger.disable_bootstrap_logging()
        return r
import datetime
import unittest

from anchore_engine.db import db_locks, initialize, session_scope, Lease, db_queue
from anchore_engine.subsys import logger, simplequeue
from anchore_engine.subsys.logger import enable_bootstrap_logging

enable_bootstrap_logging()

conn_str = 'postgres+pg8000://postgres:postgres@localhost:54320/postgres'


def init():
    config = {'credentials': {'database': {'db_connect': conn_str}}}
    initialize(localconfig=config)


class TestSimpleQueue(unittest.TestCase):
    singleton_queue = 'testq1'
    multi_queue = 'testq2'
    std_queue = 'testq3'

    @classmethod
    def setUpClass(cls):
        init()
        simplequeue.create_queue(cls.singleton_queue,
                                 max_outstanding_msgs=1,
                                 visibility_timeout=10)
        simplequeue.create_queue(cls.multi_queue,
                                 max_outstanding_msgs=5,
                                 visibility_timeout=10)
"""
Tests for the archive subsystem. Tests for each driver are here.
"""
import unittest
import os

from anchore_engine.subsys.object_store.drivers.filesystem import FilesystemObjectStorageDriver
from anchore_engine.subsys.object_store.drivers.rdbms import DbDriver
from anchore_engine.subsys.object_store.drivers.s3 import S3ObjectStorageDriver
from anchore_engine.subsys.object_store.drivers.swift import SwiftObjectStorageDriver
from anchore_engine.subsys import logger

logger.set_log_level('INFO')
logger.enable_bootstrap_logging()

test_s3_key = os.getenv('TEST_ACCESS_KEY')
test_s3_secret_key = os.getenv('TEST_SECRET_KEY')


class TestArchiveDriverMixin(object):
    """
    Expected to be mixed into a unittest.TestCase object
    """
    driver_cls = None
    driver_config = None

    test_obj1 = 'testingdata123'
    test_json_obj1 = {'key1': 'value1'}
    test_user1 = 'testuser1'
    test_user2 = 'testuser2'
    test_bucket1 = 'testbucket1'
Esempio n. 4
0
def makeService(snames,
                options,
                db_connect=True,
                require_system_user_auth=True,
                module_name="anchore_engine.services",
                validate_params={}):

    try:
        logger.enable_bootstrap_logging(service_name=','.join(snames))

        try:
            # config and init
            configfile = configdir = None
            if options['config']:
                configdir = options['config']
                configfile = os.path.join(options['config'], 'config.yaml')

            anchore_engine.configuration.localconfig.load_config(
                configdir=configdir,
                configfile=configfile,
                validate_params=validate_params)
            localconfig = anchore_engine.configuration.localconfig.get_config()
            localconfig['myservices'] = []
            logger.spew("localconfig=" +
                        json.dumps(localconfig, indent=4, sort_keys=True))
        except Exception as err:
            logger.error("cannot load configuration: exception - " + str(err))
            raise err

        # get versions of things
        try:
            versions = anchore_engine.configuration.localconfig.get_versions()
        except Exception as err:
            logger.error("cannot detect versions of service: exception - " +
                         str(err))
            raise err

        if db_connect:
            logger.info("initializing database")

            # connect to DB
            try:
                db.initialize(localconfig=localconfig, versions=versions)
            except Exception as err:
                logger.error("cannot connect to configured DB: exception - " +
                             str(err))
                raise err

            #credential bootstrap
            localconfig['system_user_auth'] = (None, None)
            if require_system_user_auth:
                gotauth = False
                max_retries = 60
                for count in range(1, max_retries):
                    if gotauth:
                        continue
                    try:
                        with session_scope() as dbsession:
                            localconfig[
                                'system_user_auth'] = get_system_user_auth(
                                    session=dbsession)
                        if localconfig['system_user_auth'] != (None, None):
                            gotauth = True
                        else:
                            logger.error(
                                "cannot get system user auth credentials yet, retrying ("
                                + str(count) + " / " + str(max_retries) + ")")
                            time.sleep(5)
                    except Exception as err:
                        logger.error(
                            "cannot get system-user auth credentials - service may not have system level access"
                        )
                        localconfig['system_user_auth'] = (None, None)

                if not gotauth:
                    raise Exception(
                        "service requires system user auth to start")

        # application object
        application = service.Application("multi-service-" + '-'.join(snames))

        #multi-service
        retservice = service.MultiService()
        retservice.setServiceParent(application)

        success = False
        try:
            scount = 0
            for sname in snames:
                if sname in localconfig['services'] and localconfig[
                        'services'][sname]['enabled']:

                    smodule = importlib.import_module(module_name + "." +
                                                      sname)

                    s = smodule.createService(sname, localconfig)
                    s.setServiceParent(retservice)

                    rc = smodule.initializeService(sname, localconfig)
                    if not rc:
                        raise Exception("failed to initialize service")

                    rc = smodule.registerService(sname, localconfig)
                    if not rc:
                        raise Exception("failed to register service")

                    logger.debug("starting service: " + sname)
                    success = True
                    scount += 1
                    localconfig['myservices'].append(sname)
                else:
                    logger.error(
                        "service not enabled in config, not starting service: "
                        + sname)

            if scount == 0:
                logger.error(
                    "no services/subservices were enabled/started on this host"
                )
                success = False
        except Exception as err:
            logger.error("cannot create/init/register service: " + sname +
                         " - exception: " + str(err))
            success = False

        if not success:
            logger.error("cannot start service (see above for information)")
            traceback.print_exc('Service init failure')
            raise Exception("cannot start service (see above for information)")

        return (retservice)
    finally:
        logger.disable_bootstrap_logging()
Esempio n. 5
0
import unittest
from anchore_engine.subsys import archive
from anchore_engine.subsys.archive import migration
from anchore_engine.subsys import logger

logger.enable_bootstrap_logging('testing')


class TestArchiveMigrations(unittest.TestCase):
    """
    Tests for the archive subsys. With each configured driver.
    """
    configs = {'fs': {}, 'db_legacy': {}, 'db': {}, 'swift': {}, 's3': {}}

    document_1 = '{"document": {"user_id": "admin", "final_action_reason": "policy_evaluation", "matched_whitelisted_images_rule": "matched_blacklisted_images_rule": false}}'
    document_json = {
        "user_id": "admin",
        "final_action_reason": "policy_evaluation",
        "matched_whitelisted_images_rule": False,
        "created_at": 1522454550,
        "evaluation_problems": [],
        "last_modified": 1522454550,
        "final_action": "stop",
        "matched_mapping_rule": {
            "name": "default",
            "repository": "*",
            "image": {
                "type": "tag",
                "value": "*"
            },
            "whitelist_ids": ["37fd763e-1765-11e8-add4-3b16c029ac5c"],