Пример #1
0
def StartService(config):
    # load the eservice database
    try:
        dbfile_name = config['Service']['EnclaveServiceDatabaseFile']
    except KeyError:
        logger.error(
            'missing required configuration for enclave service database')
        sys.exit(-1)

    try:
        eservice_db.load_database(dbfile_name)
    except Exception as e:
        logger.error('error loading eservice database from file %s', str(e))
        sys.exit(-1)

    try:
        http_port = config['Service']['HttpPort']
        http_host = config['Service']['Host']
        worker_threads = config['Service'].get('WorkerThreads', 8)
        reactor_threads = config['Service'].get('ReactorThreads', 8)
    except KeyError as ke:
        logger.error('missing configuration for %s', str(ke))
        sys.exit(-1)

    logger.info('service started on host %s, port %s', http_host, http_port)

    thread_pool = ThreadPool(maxthreads=worker_threads)
    thread_pool.start()
    reactor.addSystemEventTrigger('before', 'shutdown', thread_pool.stop)

    flask_app = toxaway.views.register(config)
    flask_site = WSGIResource(reactor, reactor.getThreadPool(), flask_app)

    root = Resource()
    root.putChild(b'shutdown', ShutdownResource())
    root.putChild(b'flask', flask_site)

    files = config.get('StaticContent', {})
    logger.info('files=%s', files)
    for key, val in files.items():
        logger.info('map <%s> to <%s>', key, val)
        root.putChild(key.encode(), File(val.encode()))

    site = Site(root, timeout=60)
    site.displayTracebacks = True

    reactor.suggestThreadPoolSize(reactor_threads)

    endpoint = TCP4ServerEndpoint(reactor,
                                  http_port,
                                  backlog=32,
                                  interface=http_host)
    endpoint.listen(site)
Пример #2
0
def _load_database_(config, create=False):
    eservice_db.clear_all_data()
    database_file = config['Service']['EnclaveServiceDatabaseFile']

    if create and not os.path.exists(database_file):
        _save_database_(config)

    loaded = False
    try:
        loaded = eservice_db.load_database(database_file, merge=False)
    except Exception as e:
        pass

    if not loaded:
        logger.error('unable to load eservice data from %s', database_file)
        sys.exit(-1)
Пример #3
0
def command_eservice_db(state, bindings, pargs):
    """controller command to manage the enclave service database
    """

    parser = argparse.ArgumentParser(prog='eservice')

    subparsers = parser.add_subparsers(dest='command')

    add_parser = subparsers.add_parser(
        'add', description='add an eservice to the database')
    add_parser.add_argument('--url',
                            help='URL for the enclave service to add',
                            type=str,
                            required=True)
    add_parser.add_argument('--name',
                            help='Short name for the enclave service',
                            type=str,
                            required=True)

    clear_parser = subparsers.add_parser(
        'clear', description='remove all eservices in the database')
    list_parser = subparsers.add_parser(
        'list', description='list eservices in the database')

    load_parser = subparsers.add_parser(
        'load', description='load an eservice database')
    load_parser.add_argument('--database',
                             help='Name of the eservice database to use',
                             type=str,
                             required=True)
    merge_group = load_parser.add_mutually_exclusive_group(required=False)
    merge_group.add_argument('--merge',
                             help='Merge new database with current db',
                             dest='merge',
                             action='store_true')
    merge_group.add_argument('--no-merge',
                             help='Overwrite current db with new database',
                             dest='merge',
                             action='store_false')
    load_parser.set_defaults(merge=False)

    remove_parser = subparsers.add_parser(
        'remove', description='remove eservice from the database')
    remove_group = remove_parser.add_mutually_exclusive_group(required=True)
    remove_group.add_argument('--name',
                              help='Short name for enclave service to remove',
                              type=str)

    save_parser = subparsers.add_parser(
        'save', description='save the current eservice database')
    save_parser.add_argument('--database',
                             help='Name of the eservice database to use',
                             type=str,
                             required=True)

    options = parser.parse_args(pargs)

    default_database = state.get(['Service', 'EnclaveServiceDatabaseFile'])
    ledger_config = state.get(['Sawtooth'])

    if options.command == 'add':
        if not eservice_db.add_by_url(
                ledger_config, options.url, name=options.name, update=True):
            raise Exception(
                'failed to add eservice {0} to the database'.format(
                    options.name))
        return

    if options.command == 'clear':
        eservice_db.clear_all_data()
        return

    if options.command == 'list':
        enclave_names = list(eservice_db.get_enclave_names())
        enclave_names.sort()

        for enclave_name in enclave_names:
            enclave_info = eservice_db.get_by_name(enclave_name)
            enclave_short_id = _hashed_identity_(enclave_info.enclave_id)
            print("{0:<18} {1:<18} {2}".format(enclave_name, enclave_short_id,
                                               enclave_info.url))

    if options.command == 'load':
        eservice_db.load_database(options.database, options.merge)
        return

    if options.command == 'remove':
        eservice_db.remove_by_name(name=options.name)
        return

    if options.command == 'save':
        eservice_db.save_database(options.database, True)
        return

    raise Exception('unknown subcommand')
def command_eservice_db(state, bindings, pargs):
    """controller command to manage the enclave service database
    """

    parser = argparse.ArgumentParser(prog='eservice_db')
    parser.add_argument('-q',
                        '--quiet',
                        help='Do not print the result',
                        action='store_true')

    subparsers = parser.add_subparsers(dest='command')

    add_parser = subparsers.add_parser(
        'add', description='add an eservice to the database')
    add_parser.add_argument('--url',
                            help='URL for the enclave service to add',
                            type=str,
                            required=True)
    add_parser.add_argument('--name',
                            help='Short name for the enclave service',
                            type=str,
                            required=True)

    clear_parser = subparsers.add_parser(
        'clear', description='remove all eservices in the database')
    list_parser = subparsers.add_parser(
        'list', description='list eservices in the database')

    info_parser = subparsers.add_parser(
        'info', description='get information about a specific eservice')
    info_parser.add_argument('--name',
                             help='Short name for enclave service',
                             type=str,
                             required=True)
    info_parser.add_argument('-s',
                             '--symbol',
                             help='binding symbol for the result',
                             type=str)
    info_parser.add_argument('-f',
                             '--field',
                             help='field to display',
                             type=str)

    load_parser = subparsers.add_parser(
        'load', description='load an eservice database')
    load_parser.add_argument('--database',
                             help='Name of the eservice database to use',
                             type=str,
                             required=True)
    merge_group = load_parser.add_mutually_exclusive_group(required=False)
    merge_group.add_argument('--merge',
                             help='Merge new database with current db',
                             dest='merge',
                             action='store_true')
    merge_group.add_argument('--no-merge',
                             help='Overwrite current db with new database',
                             dest='merge',
                             action='store_false')
    load_parser.set_defaults(merge=False)

    remove_parser = subparsers.add_parser(
        'remove', description='remove eservice from the database')
    remove_group = remove_parser.add_mutually_exclusive_group(required=True)
    remove_group.add_argument('--name',
                              help='Short name for enclave service to remove',
                              type=str)

    save_parser = subparsers.add_parser(
        'save', description='save the current eservice database')
    save_parser.add_argument('--database',
                             help='Name of the eservice database to use',
                             type=str,
                             required=True)

    options = parser.parse_args(pargs)

    default_database = state.get(['Service', 'EnclaveServiceDatabaseFile'])
    ledger_config = state.get(['Ledger'])

    if options.command == 'add':
        if not eservice_db.add_by_url(
                ledger_config, options.url, name=options.name, update=True):
            raise Exception(
                'failed to add eservice {0} to the database'.format(
                    options.name))
        return

    if options.command == 'clear':
        eservice_db.clear_all_data()
        return

    if options.command == 'list':
        enclave_names = list(eservice_db.get_enclave_names())
        enclave_names.sort()

        for enclave_name in enclave_names:
            enclave_info = eservice_db.get_by_name(enclave_name)
            enclave_short_id = _hashed_identity_(enclave_info.enclave_id)
            print("{0:<18} {1:<18} {2}".format(enclave_name, enclave_short_id,
                                               enclave_info.url))

        return

    if options.command == 'info':
        enclave_info = eservice_db.get_by_name(options.name)
        enclave_info.verify(state.get(['Ledger']))

        enclave = {}
        enclave['short_name'] = options.name
        enclave['short_id'] = _hashed_identity_(enclave_info.enclave_id)
        enclave['enclave_id'] = enclave_info.enclave_id
        enclave['url'] = enclave_info.url
        enclave['last_verified_time'] = enclave_info.last_verified_time
        enclave['interpreter'] = enclave_info.client.interpreter
        enclave[
            'storage_service_url'] = enclave_info.client.storage_service_url
        enclave['verifying_key'] = enclave_info.client.verifying_key
        enclave['encryption_key'] = enclave_info.client.encryption_key

        result = enclave
        if options.field:
            result = enclave[options.field]

        if not options.quiet:
            print(json.dumps(result, indent=4, sort_keys=True))

        if options.symbol:
            bindings.bind(options.symbol, result)

        return

    if options.command == 'load':
        eservice_db.load_database(options.database, options.merge)
        return

    if options.command == 'remove':
        eservice_db.remove_by_name(name=options.name)
        return

    if options.command == 'save':
        eservice_db.save_database(options.database, True)
        return

    raise Exception('unknown subcommand')
Пример #5
0
    # this can probably be simplified to just a comparison of
    # the two objects
    assert einfo_by_name.name == e
    assert einfo_by_enclave_id.name == e
    assert einfo_by_name.name == einfo_by_enclave_id.name
    assert einfo_by_name.enclave_id == einfo_by_enclave_id.enclave_id

# -----------------------------------------------------------------
# logger.info('verify that database can be saved and loaded')
# -----------------------------------------------------------------
initial_hash = compute_database_hash()
eservice_db.save_database(options.eservice_db, overwrite=True)

#load db freshly and check
eservice_db.load_database(options.eservice_db, merge=False)
assert compute_database_hash(
) == initial_hash, 'reloaded database has changed, no merge'

#reload db again and check
eservice_db.load_database(options.eservice_db, merge=True)
assert compute_database_hash(
) == initial_hash, 'reloaded database has changed, merge'

# -----------------------------------------------------------------
# logger.info('test removing entries')
# -----------------------------------------------------------------
einfo0 = eservice_db.get_by_name(names[0])
assert eservice_db.remove_by_name(names[0]), 'error removing by name'
assert eservice_db.get_by_name(names[0]) is None, 'failed to remove by name'