def test_csv_import(): auth_client = AuthClient( constants.HOST, verify_certificate=False, username='******', password='******', ) token_data = auth_client.token.new(expiration=300) token = token_data['token'] auth_client.set_token(token) client = ConfdClient( constants.HOST, verify_certificate=False, token=token, ) start = datetime.now() result = upload_csv(client, token_data['metadata']['tenant_uuid']) stop = datetime.now() assert 'created' in result, 'Result should contains the created users:\n{}'.format(result) assert len(result['created']) == 100, 'Should have created 100 users\n{}'.format(result) assert stop - start <= MAX_TIME, "CSV import exceeded max time ({})".format(MAX_TIME) # NOTE(fblackburn): wait until pjsip reload complete before starting next test time.sleep(5)
def _add_client(self, source_config): logger.debug('Instanciating a new confd client for %s', source_config['uuid']) auth_config = dict(source_config['auth']) if auth_config.get('key_file'): # File must be readable by wazo-dird key_file = parse_config_file(auth_config.pop('key_file')) if not key_file: logger.info('failed to load key file for source %s', source_config['name']) return auth_config['username'] = key_file['service_id'] auth_config['password'] = key_file['service_key'] auth_client = AuthClient(**auth_config) token_renewer = TokenRenewer(auth_client) confd_config = source_config['confd'] logger.debug('confd config %s', confd_config) client = ConfdClient(**confd_config) client.set_tenant(source_config['tenant_uuid']) token_renewer.subscribe_to_token_change(client.set_token) token_renewer.start() self._clients[source_config['uuid']] = RegisteredClient( client, token_renewer)
def get_nestbox_token( self, nestbox_host, nestbox_port, nestbox_verify_certificate, service_id, service_key, ): auth = AuthClient( nestbox_host, port=nestbox_port, username=service_id, password=service_key, prefix="/api/auth", verify_certificate=nestbox_verify_certificate, ) try: token_data = auth.token.new('wazo_user', expiration=600) except HTTPError: raise SetupError( message='Failed to create authorization token', error_id='setup-token-failed', details={ 'auth_host': nestbox_host, 'auth_port': nestbox_port, 'service_key': service_id, }, ) return token_data['token']
def do_migration(config): with closing(psycopg2.connect(config['db_uri'])) as conn: tenants = _build_tenant_bodies_from_entities(conn.cursor()) auth_client = AuthClient(**config['auth']) token = auth_client.token.new(expiration=36000)['token'] auth_client.set_token(token) existing_tenants = _get_existing_tenants(auth_client) for tenant in tenants: if tenant['name'] in existing_tenants.keys(): continue tenant = auth_client.tenants.new(**tenant) existing_tenants[tenant['name']] = tenant['uuid'] with closing(psycopg2.connect(config['db_uri'])) as conn: cursor = conn.cursor() for name, tenant_uuid in existing_tenants.items(): if not name: # wazo-auth allow tenants without names but those do not map to entities continue _upsert_tenant(cursor, tenant_uuid) conn.commit() _update_entity_tenant_uuid(cursor, name, tenant_uuid) conn.commit() auth_client.token.revoke(token)
def migrate_tenants(): config = _load_config() auth_client = AuthClient(**config['auth']) token = auth_client.token.new('wazo_user', expiration=5 * 60)['token'] auth_client.set_token(token) body = [ {"owner_user_uuid": user['uuid'], "owner_tenant_uuid": user['tenant_uuid']} for user in auth_client.users.list(recurse=True)["items"] ] with _migration_plugin(config['webhookd']): url = 'https://{host}:{port}/{version}/tenant-migration'.format(**config['webhookd']) result = requests.post( url, data=json.dumps(body), headers={'X-Auth-Token': token, 'Content-Type': 'application/json'}, verify=False, ) if result.status_code != 200: print( 'webhookd tenant migration failed, status-code {}:\n' '{}\ncheck /var/log/wazo-webhookd.log for more info' .format(result.status_code, result.text) ) sys.exit(2)
def load(self, dependencies): api = dependencies['api'] ari = dependencies['ari'] bus_consumer = dependencies['bus_consumer'] bus_publisher = dependencies['bus_publisher'] config = dependencies['config'] token_changed_subscribe = dependencies['token_changed_subscribe'] amid_client = AmidClient(**config['amid']) auth_client = AuthClient(**config['auth']) confd_client = ConfdClient(**config['confd']) token_changed_subscribe(amid_client.set_token) token_changed_subscribe(confd_client.set_token) notifier = FaxesNotifier(bus_publisher) fax_service = FaxesService(amid_client, ari.client, confd_client, notifier) bus_event_handler = FaxesBusEventHandler(notifier) bus_event_handler.subscribe(bus_consumer) api.add_resource(FaxesResource, '/faxes', resource_class_args=[fax_service]) api.add_resource(UserFaxesResource, '/users/me/faxes', resource_class_args=[auth_client, fax_service])
def load(self, dependencies): api = dependencies['api'] ari = dependencies['ari'] bus_consumer = dependencies['bus_consumer'] bus_publisher = dependencies['bus_publisher'] collectd = dependencies['collectd'] token_changed_subscribe = dependencies['token_changed_subscribe'] config = dependencies['config'] amid_client = AmidClient(**config['amid']) token_changed_subscribe(amid_client.set_token) auth_client = AuthClient(**config['auth']) confd_client = ConfdClient(**config['confd']) token_changed_subscribe(confd_client.set_token) dial_echo_manager = DialEchoManager() calls_service = CallsService(amid_client, config['ari']['connection'], ari.client, confd_client, dial_echo_manager) ari.register_application(DEFAULT_APPLICATION_NAME) calls_stasis = CallsStasis(ari.client, collectd, bus_publisher, calls_service, config['uuid'], amid_client) calls_stasis.subscribe() calls_bus_event_handler = CallsBusEventHandler(amid_client, ari.client, collectd, bus_publisher, calls_service, config['uuid'], dial_echo_manager) calls_bus_event_handler.subscribe(bus_consumer) api.add_resource(CallsResource, '/calls', resource_class_args=[calls_service]) api.add_resource(MyCallsResource, '/users/me/calls', resource_class_args=[auth_client, calls_service]) api.add_resource(CallResource, '/calls/<call_id>', resource_class_args=[calls_service]) api.add_resource(MyCallResource, '/users/me/calls/<call_id>', resource_class_args=[auth_client, calls_service]) api.add_resource(ConnectCallToUserResource, '/calls/<call_id>/user/<user_uuid>', resource_class_args=[calls_service])
def migrate_tenants(): config = _load_config() auth_client = AuthClient(**config['auth']) token = auth_client.token.new('wazo_user', expiration=5 * 60)['token'] auth_client.set_token(token) auth_tenants = auth_client.tenants.list() body = [{ 'uuid': t['uuid'], 'name': t['name'] } for t in auth_tenants['items']] with _migration_plugin(config['dird']): url = 'https://{host}:{port}/{version}/phonebook_move_tenant'.format( **config['dird']) result = requests.post( url, data=json.dumps(body), headers={ 'X-Auth-Token': token, 'Content-Type': 'application/json' }, verify=False, ) if result.status_code != 200: print( 'dird tenant migration failed, check /var/log/wazo-dird.log for more info' ) sys.exit(2)
def migrate_tenants(): config = _load_config() auth_client = AuthClient(**config['auth']) token = auth_client.token.new(expiration=5 * 60)['token'] auth_client.set_token(token) confd = ConfdClient(token=token, **config['confd']) body = { 'contexts': [ {"context": context['name'], "tenant_uuid": context['tenant_uuid']} for context in confd.contexts.list(recurse=True)["items"] ] } with _migration_plugin(config['call_logd']): url = 'https://{host}:{port}/{version}/tenant-migration'.format(**config['call_logd']) result = requests.post( url, data=json.dumps(body), headers={'X-Auth-Token': token, 'Content-Type': 'application/json'}, verify=False, ) if result.status_code != 200: print( 'call_logd tenant migration failed, status-code {}:\n' '{}\ncheck /var/log/wazo-call-logd.log for more info' .format(result.status_code, result.text) ) sys.exit(2)
def load(self, dependencies): app = dependencies['app'] dird_client = DirdClient(**dependencies['config']['dird']) auth_client = AuthClient(**dependencies['config']['auth']) token_changed_subscribe = dependencies['token_changed_subscribe'] token_changed_subscribe(dird_client.set_token) token_changed_subscribe(auth_client.set_token) dependencies['phone_plugins'].append(self) class_kwargs = { 'vendor': self.vendor, 'dird_client': dird_client, 'auth_client': auth_client, } api = create_blueprint_api( app, '{}_plugin'.format(self.vendor), self.import_name ) self.menu_url = self.menu_url_fmt.format(vendor=self.vendor) self.input_url = self.input_url_fmt.format(vendor=self.vendor) self.lookup_url = self.lookup_url_fmt.format(vendor=self.vendor) self.directories_menu_url = self.directories_menu_url_fmt.format( vendor=self.vendor ) self.directories_input_url = self.directories_input_url_fmt.format( vendor=self.vendor ) self.directories_lookup_url = self.directories_lookup_url_fmt.format( vendor=self.vendor ) self._add_resources(api, class_kwargs)
def setup_auth_token(context): context.auth_client = AuthClient(username='******', password='******', **context.wazo_config['auth']) context.token = auth.new_auth_token(context) context.auth_client.set_token(context.token) context.token_pubsub.subscribe('new-token-id', context.auth_client.set_token)
def new_auth(cls, **kwargs): return AuthClient( '127.0.0.1', cls.service_port(9497, 'auth'), prefix=None, https=False, **kwargs, )
def main(): args = parse_args() verify_certificate = _extract_verify_certificate(args.verify_certificate) password = os.getenv('PORTAL_PASSWORD', None) if not password: password = getpass() kwargs = {} if args.timeout: kwargs['timeout'] = args.timeout auth_client = AuthClient(args.host, port=443, prefix='/api/auth', verify_certificate=verify_certificate, username=args.username, password=password, **kwargs) token = auth_client.token.new()['token'] auth_client.set_token(token) confd_client = ConfdClient(args.host, port=443, prefix='/api/confd', verify_certificate=verify_certificate, token=token, **kwargs) rcl = [] if args.rcl: rcl = detect_desync_rcl(auth_client, confd_client) users = [] if args.users: users = detect_desync_users(auth_client, confd_client) if args.delete_orphan_users: delete_desync_users(confd_client, users) if not rcl and not users: sys.exit(0) if args.output: output_file = open(args.output, "w") else: output_file = sys.stdout fieldnames = ['uuid', 'type', 'name'] writer = csv.DictWriter(output_file, fieldnames=fieldnames) writer.writeheader() for row in rcl + users: writer.writerow(row) if args.output: output_file.close() sys.exit(1)
def main(): args = parse_args() verify_certificate = _extract_verify_certificate(args.verify_certificate) auth_client = AuthClient( args.host, port=443, prefix='/api/auth', verify_certificate=verify_certificate, username=args.username, password=args.password, ) token = auth_client.token.new()['token'] confd_client = ConfdClient( args.host, port=443, prefix='/api/confd', verify_certificate=verify_certificate, token=token, ) resellers = confd_client.resellers.list()['items'] result = io.StringIO() fieldnames = [ 'reseller_name', 'reseller_uuid', 'customer_name', 'customer_uuid', 'user_uuid', 'tenant_uuid', 'subscription', 'created_at', 'modified_at', ] writer = csv.DictWriter(result, fieldnames=fieldnames) writer.writeheader() for reseller in resellers: accounts = confd_client.accounts.list(tenant_uuid=reseller['uuid'])['items'] for account in accounts: account['reseller_name'] = reseller['name'] account['reseller_uuid'] = reseller['uuid'] account['user_uuid'] = account.pop('uuid') customer = account.pop('customer') if customer is not None: account['customer_name'] = customer['name'] account['customer_uuid'] = customer['uuid'] writer.writerow(account) if args.output: with open(args.output, 'w') as fobj: result.seek(0) shutil.copyfileobj(result, fobj) else: print(result.getvalue())
def test_csv_import(): auth_client = AuthClient(constants.HOST, verify_certificate=False, username='******', password='******') dird_client = DirdClient(constants.HOST, https=True, verify_certificate=False, timeout=MAX_TIME) token_data = auth_client.token.new(expiration=300) token = token_data['token'] auth_client.set_token(token) try: auth_client.users.new( username=USERNAME, password=PASSWORD, tenant_uuid=token_data['metadata']['tenant_uuid'], ) except requests.HTTPError as e: if e.response.status_code == 409: pass else: raise user_auth_client = AuthClient( constants.HOST, verify_certificate=False, username=USERNAME, password=PASSWORD, ) token = user_auth_client.token.new('wazo_user', expiration=300)['token'] result, time_to_complete = upload_csv(dird_client, token) assert 'created' in result, 'The result does not contain created contacts' assert len(result['created'] ) == 1000, 'expected 1000 created contacts: {}'.format( len(result['created'])) assert time_to_complete < MAX_TIME, 'The import took too long {}s > {}s'.format( time_to_complete, MAX_TIME)
def _generate_call_logs(): parser = argparse.ArgumentParser(description='Call logs generator') options = parse_args(parser) file_config = { key: value for key, value in read_config_file_hierarchy(DEFAULT_CONFIG).items() if key in ('confd', 'bus', 'auth', 'db_uri', 'cel_db_uri') } key_config = {} auth_username = file_config['auth'].get('username') auth_password = file_config['auth'].get('password') if not (auth_username and auth_password): key_config = load_key_file(ChainMap(file_config, DEFAULT_CONFIG)) config = ChainMap(key_config, file_config, DEFAULT_CONFIG) set_xivo_uuid(config, logger) init_db_from_config({'db_uri': config['cel_db_uri']}) DBSession = new_db_session(config['db_uri']) CELDBSession = new_db_session(config['cel_db_uri']) dao = DAO(DBSession, CELDBSession) auth_client = AuthClient(**config['auth']) confd_client = ConfdClient(**config['confd']) token_renewer = TokenRenewer(auth_client) token_renewer.subscribe_to_token_change(confd_client.set_token) generator = CallLogsGenerator( confd_client, [ LocalOriginateCELInterpretor(), DispatchCELInterpretor(CallerCELInterpretor(), CalleeCELInterpretor()), ], ) token_renewer.subscribe_to_next_token_details_change( generator.set_default_tenant_uuid) writer = CallLogsWriter(dao) publisher = BusPublisher(service_uuid=config['uuid'], **config['bus']) manager = CallLogsManager(dao, generator, writer, publisher) options = vars(options) with token_renewer: if options.get('action') == 'delete': if options.get('all'): manager.delete_all() elif options.get('days'): manager.delete_from_days(options['days']) else: if options.get('days'): manager.generate_from_days(days=options['days']) else: manager.generate_from_count(cel_count=options['cel_count'])