def init_cached_authenticator(config_argument, cloud, logger): try: _cnf = config_argument.value url = _cnf.get_cloud(cloud, 'url') tokens = _cnf.get_cloud(cloud, 'token').split() astakos, failed, help_message = None, [], [] for token in tokens: try: if astakos: astakos.authenticate(token) else: tmp_base = CachedAstakosClient(url, token) from kamaki.cli.cmds import CommandInit fake_cmd = CommandInit(dict(config=config_argument)) fake_cmd.client = astakos fake_cmd._set_log_params() tmp_base.authenticate(token) astakos = tmp_base except ClientError as ce: if ce.status in (401, ): logger.warning('Cloud %s failed to authenticate token %s' % (cloud, token)) failed.append(token) else: raise if failed: if set(tokens) == set(failed): tlen = len(tokens) logger.warning( '%s token%s in cloud.%s.token failed to authenticate' % (('All %s' % tlen) if tlen > 1 else 'The only', 's' if tlen > 1 else '', cloud)) help_message += [ 'To replace with a new and valid token:', ' kamaki config set cloud.%s.token NEW_TOKEN' % (cloud) ] else: tlen = len(tokens) for token in failed: tokens.remove(token) logger.warning( '%s of %s tokens removed from cloud.%s.token list' % (len(failed), tlen, cloud)) _cnf.set_cloud(cloud, 'token', ' '.join(tokens)) _cnf.write() if tokens: return astakos, help_message logger.warning('cloud.%s.token is now empty' % cloud) help_message = [ 'To set a new token:', ' kamaki config set cloud.%s.token NEW_TOKEN' ] except AssertionError as ae: logger.warning('Failed to load authenticator [%s]' % ae) return None, help_message
def create_account(url, token): """Given a valid (URL, tokens) pair this method returns an Astakos client instance """ client = AstakosClient(url, token) try: client.authenticate() except ClientError: return None return client
def init_cached_authenticator(config_argument, cloud, logger): try: _cnf = config_argument.value url = _cnf.get_cloud(cloud, 'url') tokens = _cnf.get_cloud(cloud, 'token').split() astakos, failed, help_message = None, [], [] for token in tokens: try: if astakos: astakos.authenticate(token) else: tmp_base = CachedAstakosClient(url, token) from kamaki.cli.cmds import CommandInit fake_cmd = CommandInit(dict(config=config_argument)) fake_cmd.client = astakos fake_cmd._set_log_params() tmp_base.authenticate(token) astakos = tmp_base except ClientError as ce: if ce.status in (401, ): logger.warning( 'Cloud %s failed to authenticate token %s' % ( cloud, token)) failed.append(token) else: raise if failed: if set(tokens) == set(failed): tlen = len(tokens) logger.warning( '%s token%s in cloud.%s.token failed to authenticate' % ( ('All %s' % tlen) if tlen > 1 else 'The only', 's' if tlen > 1 else '', cloud)) help_message += [ 'To replace with a new and valid token:', ' kamaki config set cloud.%s.token NEW_TOKEN' % (cloud)] else: tlen = len(tokens) for token in failed: tokens.remove(token) logger.warning( '%s of %s tokens removed from cloud.%s.token list' % ( len(failed), tlen, cloud)) _cnf.set_cloud(cloud, 'token', ' '.join(tokens)) _cnf.write() if tokens: return astakos, help_message logger.warning('cloud.%s.token is now empty' % cloud) help_message = [ 'To set a new token:', ' kamaki config set cloud.%s.token NEW_TOKEN'] except AssertionError as ae: logger.warning('Failed to load authenticator [%s]' % ae) return None, help_message
def get_user_name(token): """Check kamaki and return user name / email from matching ~okeanos token""" cached = CachedAstakosClient(auth_url, token) uuid = get_user_id(token) try: logging.info(' Get the user_name') user_name = cached.uuids2usernames((uuid,), token).get(uuid,'') return user_name except ClientError: msg = 'Failed to get user_name from identity server' raise ClientError(msg)
def init_cached_authenticator(config_argument, cloud, logger): try: _cnf = config_argument.value url = _cnf.get_cloud(cloud, 'url') tokens = _cnf.get_cloud(cloud, 'token').split() auth_base, failed = None, [] for token in tokens: try: if auth_base: auth_base.authenticate(token) else: tmp_base = CachedAstakosClient(url, token) from kamaki.cli.commands import _command_init fake_cmd = _command_init(dict(config=config_argument)) fake_cmd.client = auth_base fake_cmd._set_log_params() tmp_base.authenticate(token) auth_base = tmp_base except ClientError as ce: if ce.status in (401, ): logger.warning( 'WARNING: Failed to authenticate token %s' % token) failed.append(token) else: raise for token in failed: r = raw_input( 'Token %s failed to authenticate. Remove it? [y/N]: ' % token) if r in ('y', 'Y'): tokens.remove(token) if set(failed).difference(tokens): _cnf.set_cloud(cloud, 'token', ' '.join(tokens)) _cnf.write() if tokens: return auth_base logger.warning('WARNING: cloud.%s.token is now empty' % cloud) except AssertionError as ae: logger.warning('WARNING: Failed to load authenticator [%s]' % ae) return None