Allow multiple tokens per cloud configuration
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 28 Jun 2013 10:57:32 +0000 (13:57 +0300)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 28 Jun 2013 10:57:32 +0000 (13:57 +0300)
Refs: #3632

kamaki/cli/__init__.py
kamaki/cli/commands/astakos.py

index 8c24ad2..8310e9a 100644 (file)
@@ -41,6 +41,8 @@ from kamaki.cli.history import History
 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
@@ -285,7 +287,7 @@ def _init_session(arguments, is_non_API=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(
@@ -296,9 +298,23 @@ def _init_session(arguments, is_non_API=False):
                     '  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
index f8788ca..020e95d 100644 (file)
@@ -45,6 +45,14 @@ _commands = [user_cmds]
 
 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
@@ -54,6 +62,7 @@ class _user_init(_command_init):
             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:
@@ -91,9 +100,7 @@ class user_authenticate(_user_init, _optional_json):
             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
@@ -156,6 +163,9 @@ class user_set(_user_init, _optional_json):
                 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,