from kamaki.cli.utils import print_dict, red, magenta, yellow
from kamaki.cli.errors import CLIError, CLICmdSpecError
from kamaki.cli import logger
+from kamaki.clients.astakos import AstakosClient as AuthCachedClient
+from kamaki.clients import ClientError
_help = False
_debug = False
for term in ('url', 'token'):
try:
auth_args[term] = _cnf.get_cloud(cloud, term)
- except KeyError:
+ except KeyError or IndexError:
auth_args[term] = ''
if not auth_args[term]:
raise CLIError(
' kamaki config set cloud.%s.%s <%s>' % (
cloud, term, term.upper())])
- from kamaki.clients.astakos import AstakosClient as AuthCachedClient
try:
- return AuthCachedClient(auth_args['url'], auth_args['token']), cloud
+ auth_base = None
+ for token in reversed(auth_args['token'].split()):
+ try:
+ if auth_base:
+ auth_base.authenticate(token)
+ else:
+ auth_base = AuthCachedClient(
+ auth_args['url'], auth_args['token'])
+ auth_base.authenticate(token)
+ except ClientError as ce:
+ if ce.status in (401, ):
+ kloger.warning(
+ 'WARNING: Failed to authorize token %s' % token)
+ else:
+ raise
+ return auth_base, cloud
except AssertionError as ae:
kloger.warning('WARNING: Failed to load authenticator [%s]' % ae)
return None, cloud
class _user_init(_command_init):
+ def _write_main_token(self, token):
+ tokens = self.config.get_cloud(self.cloud, 'token').split()
+ if token in tokens:
+ tokens.remove(token)
+ tokens.insert(0, token)
+ self.config.set_cloud(self.cloud, 'token', ' '.join(tokens))
+ self.config.write()
+
@errors.generic.all
@errors.user.load
@addLogSettings
if base_url:
token = self._custom_token('astakos')\
or self.config.get_cloud(self.cloud, 'token')
+ token = token.split()[0] if ' ' in token else token
self.client = AstakosClient(base_url=base_url, token=token)
return
else:
if (token_bu != self.client.token and
ask_user('Permanently save token as cloud.%s.token ?' % (
self.cloud))):
- self.config.set_cloud(
- self.cloud, 'token', self.client.token)
- self.config.write()
+ self._write_main_token(self.client.token)
except Exception:
#recover old token
self.client.token = token_bu
print('Session user set to %s (%s)' % (
self.client.user_term('name'),
self.client.user_term('id')))
+ if ask_user('Permanently make %s the main user?' % (
+ self.client.user_term('name'))):
+ self._write_main_token(self.client.token)
return
raise CLIError(
'User with UUID %s not authenticated in current session' % uuid,