def do_reset_cmd(config, args) :
    """subcommand to clear all entries from the database
    """

    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--create', help='create a new database if necessary', action='store_true')

    options = parser.parse_args(args)

    # load the database just to make sure the file exists
    _load_database_(config, create=options.create)
    eservice_db.clear_all_data()
    _save_database_(config)
Exemplo n.º 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)
Exemplo n.º 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')
Exemplo n.º 5
0
assert eservice_db.get_by_name(names[0]) is None, 'failed to remove by name'

einfo1 = eservice_db.get_by_name(names[1])
assert eservice_db.remove_by_enclave_id(
    einfo1.enclave_id), 'error removing by enclave id'
assert eservice_db.get_by_enclave_id(
    einfo1.enclave_id) is None, 'failed to remove by enclave id'

#save file after remove
eservice_db.save_database(options.eservice_db, overwrite=True)

# -----------------------------------------------------------------
# logger.info('test last verified and load merge')
# -----------------------------------------------------------------
# force data to empty
eservice_db.clear_all_data()
assert len(list(
    eservice_db.get_enclave_ids())) == 0, 'failed to clear database (ids)'
assert len(list(
    eservice_db.get_enclave_names())) == 0, 'failed to clear database (names)'

# add entry for e1
eservice_db.add_by_url(ledger_config, einfo0.url, name=einfo0.name)
eservice_db.add_by_url(ledger_config, einfo1.url, name=einfo1.name)
assert len(list(
    eservice_db.get_enclave_ids())) == 2, 'failed to add enclaves (ids)'
assert len(list(
    eservice_db.get_enclave_names())) == 2, 'failed to add enclaves (names)'

#merge with rest of the entries from file
eservice_db.load_database(options.eservice_db, merge=True)