Repository: StackStorm/st2

Issue:
Pack install verification call fails with -t flag, as it uses older token in verification(GET) call

ST2_AUTH_TOKEN = "wrong TOKEN"


```
$ st2 --debug pack install twitter -t 20f8a6077a7c4fddb5bb45e13f474cda
# -------- begin 140086401321616 request ----------
curl -X POST -H  'Connection: keep-alive' -H  'Accept-Encoding: gzip, deflate' -H  'Accept: */*' -H  'User-Agent: python-requests/2.11.1' -H  'content-type: application/json' -H  'X-Auth-Token: 20f8a6077a7c4fddb5bb45e13f474cda' -H  'Content-Length: 38' --data-binary '{"force": false, "packs": ["twitter"]}' http://127.0.0.1:9101/v1/packs/install
# -------- begin 140086401321616 response ----------
{"execution_id": "589e193555fc8c1fa446ed7a"}
# -------- end 140086401321616 response ------------


	[ succeeded ] download pack
	[ succeeded ] make a prerun
	[ succeeded ] install pack dependencies
	[ succeeded ] register pack

# -------- begin 140086401206288 request ----------

```
Pack installs successfully, but GET call uses older token or ENV var.

```
curl -X GET -H  'Connection: keep-alive' -H  'Accept-Encoding: gzip, deflate' -H  'Accept: */*' -H  'User-Agent: python-requests/2.11.1' -H  'X-Auth-Token: Wrong TOKEN' http://127.0.0.1:9101/v1/executions/589e193555fc8c1fa446ed7a
# -------- begin 140086401206288 response ----------
{
    "faultstring": "Unauthorized"
}
# -------- end 140086401206288 response ------------

ERROR: 401 Client Error: Unauthorized
MESSAGE: Unauthorized for url: http://127.0.0.1:9101/v1/executions/589e193555fc8c1fa446ed7a

CLI settings:
----------------
Config file path: /home/vagrant/.st2/config
Client settings:
----------------
ST2_BASE_URL: http://127.0.0.1
ST2_AUTH_URL: http://127.0.0.1:9100
ST2_API_URL: http://127.0.0.1:9101/v1
ST2_AUTH_TOKEN: Wrong TOKEN

Proxy settings:
---------------
HTTP_PROXY:
HTTPS_PROXY:

Traceback (most recent call last):
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/shell.py", line 278, in run
    args.func(args)
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/commands/pack.py", line 196, in run_and_print
    instance = super(PackInstallCommand, self).run_and_print(args, **kwargs)
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/commands/resource.py", line 44, in decorate
    return func(*args, **kwargs)
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/commands/pack.py", line 144, in run_and_print
    return self.app.client.managers['LiveAction'].get_by_id(parent_id)
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/models/core.py", line 39, in decorate
    return func(*args, **kwargs)
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/models/core.py", line 204, in get_by_id
    self.handle_error(response)
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/models/core.py", line 165, in handle_error
    response.raise_for_status()
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/requests/models.py", line 862, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
HTTPError: 401 Client Error: Unauthorized
MESSAGE: Unauthorized for url: http://127.0.0.1:9101/v1/executions/589e193555fc8c1fa446ed7a
```

st2api.log

```
2017-02-10 19:49:24,141 139865107110800 INFO hooks [-] a11dcfd2-6621-4c38-910c-2df70764742a - POST /v1/packs/register result={"sensors": 2, "aliases": 0, "actions": 3, "runners": 13, "triggers": 0} (result='{"sensors": 2, "aliases": 0, "actions": 3, "run│
ners": 13, "triggers": 0}',request_id='a11dcfd2-6621-4c38-910c-2df70764742a',status_code='200 OK',remote_addr='127.0.0.1',path='/v1/packs/register',method='POST')                                                                                            │
2017-02-10 19:49:25,784 139865107110800 INFO hooks [-] d204e648-23a3-4ad1-9910-0381b65abdba -  GET /v1/executions/589e193555fc8c1fa446ed7a with filters={} (remote_addr='127.0.0.1',method='GET',filters={},request_id='d204e648-23a3-4ad1-9910-0381b65abdba',│
path='/v1/executions/589e193555fc8c1fa446ed7a')                                                                                                                                                                                                               │
2017-02-10 19:49:25,785 139865107110800 AUDIT auth [-] Token provided in headers                                                                                                                                                                              │
2017-02-10 19:49:25,787 139865107110800 ERROR hooks [-] Token is not found.                                                                                                                                                                                   │
Traceback (most recent call last):                                                                                                                                                                                                                            │
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/pecan/core.py", line 630, in __call__                                                                                                                                                           │
    controller, args, kwargs = self.find_controller(state)                                                                                                                                                                                                    │
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/pecan/core.py", line 786, in find_controller                                                                                                                                                    │
    controller, args, kw = super(Pecan, self).find_controller(_state)                                                                                                                                                                                         │
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/pecan/core.py", line 516, in find_controller                                                                                                                                                    │
    self.handle_hooks(self.determine_hooks(controller), 'before', state)                                                                                                                                                                                      │
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/pecan/core.py", line 793, in handle_hooks                                                                                                                                                       │
    return super(Pecan, self).handle_hooks(hooks, *args, **kw)                                                                                                                                                                                                │
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/pecan/core.py", line 320, in handle_hooks                                                                                                                                                       │
    result = getattr(hook, hook_type)(*args)                                                                                                                                                                                                                  │
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2common/hooks.py", line 127, in before                                                                                                                                                        │
    user_db = self._validate_creds_and_get_user(request=state.request)                                                                                                                                                                                        │
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2common/hooks.py", line 217, in _validate_creds_and_get_user                                                                                                                                  │
    token_in_query_params=token_in_query_params)                                                                                                                                                                                                              │
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2common/util/auth.py", line 83, in validate_token_and_source                                                                                                                                  │
    return validate_token(token_in_headers or token_in_query_params)                                                                                                                                                                                          │
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2common/util/auth.py", line 48, in validate_token                                                                                                                                             │
    token = Token.get(token_string)                                                                                                                                                                                                                           │
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2common/persistence/auth.py", line 79, in get                                                                                                                                                 │
    raise TokenNotFoundError()                                                                                                                                                                                                                                │
TokenNotFoundError                                                                                                                                                                                                                                            │
2017-02-10 19:49:25,793 139865107110800 INFO hooks [-] d204e648-23a3-4ad1-9910-0381b65abdba - GET /v1/executions/589e193555fc8c1fa446ed7a result={                                                                                                            │
    "faultstring": "Unauthorized"                                                                                                                                                                                                                             │
} (result='{\n    "faultstring": "Unauthorized"\n}',request_id='d204e648-23a3-4ad1-9910-0381b65abdba',status_code='401 Unauthorized',remote_addr='127.0.0.1',path='/v1/executions/589e193555fc8c1fa446ed7a',method='GET')
```


Discussion:

Resolving patch:
diff --git a/st2client/st2client/commands/pack.py b/st2client/st2client/commands/pack.py
--- a/st2client/st2client/commands/pack.py
+++ b/st2client/st2client/commands/pack.py
@@ -22,6 +22,7 @@
 from st2client.models import Pack
 from st2client.models import LiveAction
 from st2client.commands import resource
+from st2client.commands.resource import add_auth_token_to_kwargs_from_cli
 from st2client.commands.action import ActionRunCommandMixin
 from st2client.formatters import table
 from st2client.exceptions.operations import OperationFailureException
@@ -100,7 +101,7 @@ def __init__(self, *args, **kwargs):
         detail_arg_grp.add_argument('-d', '--detail', action='store_true',
                                     help='Display full detail of the execution in table format.')
 
-    @resource.add_auth_token_to_kwargs_from_cli
+    @add_auth_token_to_kwargs_from_cli
     def run_and_print(self, args, **kwargs):
         instance = self.run(args, **kwargs)
         if not instance:
@@ -141,7 +142,7 @@ def run_and_print(self, args, **kwargs):
             self._print_execution_details(execution=execution, args=args, **kwargs)
             sys.exit(1)
 
-        return self.app.client.managers['LiveAction'].get_by_id(parent_id)
+        return self.app.client.managers['LiveAction'].get_by_id(parent_id, **kwargs)
 
 
 class PackListCommand(resource.ResourceListCommand):
@@ -167,7 +168,7 @@ def __init__(self, resource, *args, **kwargs):
                                  help='Name of the %s to show.' %
                                  resource.get_display_name().lower())
 
-    @resource.add_auth_token_to_kwargs_from_cli
+    @add_auth_token_to_kwargs_from_cli
     def run(self, args, **kwargs):
         return self.manager.search(args, **kwargs)
 
@@ -188,10 +189,10 @@ def __init__(self, resource, *args, **kwargs):
                                  default=False,
                                  help='Force pack installation.')
 
-    @resource.add_auth_token_to_kwargs_from_cli
     def run(self, args, **kwargs):
         return self.manager.install(args.packs, force=args.force, **kwargs)
 
+    @add_auth_token_to_kwargs_from_cli
     def run_and_print(self, args, **kwargs):
         instance = super(PackInstallCommand, self).run_and_print(args, **kwargs)
 
@@ -199,12 +200,12 @@ def run_and_print(self, args, **kwargs):
         packs = instance.result['tasks'][1]['result']['result']
 
         if len(packs) == 1:
-            pack_instance = self.app.client.managers['Pack'].get_by_ref_or_id(packs[0])
+            pack_instance = self.app.client.managers['Pack'].get_by_ref_or_id(packs[0], **kwargs)
             self.print_output(pack_instance, table.PropertyValueTable,
                               attributes=args.attr, json=args.json, yaml=args.yaml,
                               attribute_display_order=self.attribute_display_order)
         else:
-            all_pack_instances = self.app.client.managers['Pack'].get_all()
+            all_pack_instances = self.app.client.managers['Pack'].get_all(**kwargs)
             pack_instances = []
 
             for pack in all_pack_instances:
@@ -228,19 +229,19 @@ def __init__(self, resource, *args, **kwargs):
                                  help='Name of the %s to remove.' %
                                  resource.get_plural_display_name().lower())
 
-    @resource.add_auth_token_to_kwargs_from_cli
     def run(self, args, **kwargs):
         return self.manager.remove(args.packs, **kwargs)
 
+    @add_auth_token_to_kwargs_from_cli
     def run_and_print(self, args, **kwargs):
-        all_pack_instances = self.app.client.managers['Pack'].get_all()
+        all_pack_instances = self.app.client.managers['Pack'].get_all(**kwargs)
 
         super(PackRemoveCommand, self).run_and_print(args, **kwargs)
 
         packs = args.packs
 
         if len(packs) == 1:
-            pack_instance = self.app.client.managers['Pack'].get_by_ref_or_id(packs[0])
+            pack_instance = self.app.client.managers['Pack'].get_by_ref_or_id(packs[0], **kwargs)
 
             if pack_instance:
                 raise OperationFailureException('Pack %s has not been removed properly', packs[0])
@@ -252,7 +253,7 @@ def run_and_print(self, args, **kwargs):
                               attributes=args.attr, json=args.json, yaml=args.yaml,
                               attribute_display_order=self.attribute_display_order)
         else:
-            remaining_pack_instances = self.app.client.managers['Pack'].get_all()
+            remaining_pack_instances = self.app.client.managers['Pack'].get_all(**kwargs)
             pack_instances = []
 
             for pack in all_pack_instances:
@@ -283,7 +284,7 @@ def __init__(self, resource, *args, **kwargs):
                                  nargs='+',
                                  help='Types of content to register.')
 
-    @resource.add_auth_token_to_kwargs_from_cli
+    @add_auth_token_to_kwargs_from_cli
     def run(self, args, **kwargs):
         return self.manager.register(args.packs, args.types, **kwargs)
 
@@ -302,7 +303,7 @@ def __init__(self, resource, *args, **kwargs):
         self.parser.add_argument('query',
                                  help='Search query.')
 
-    @resource.add_auth_token_to_kwargs_from_cli
+    @add_auth_token_to_kwargs_from_cli
     def run(self, args, **kwargs):
         return self.manager.search(args, **kwargs)
 
@@ -317,7 +318,7 @@ def __init__(self, resource, *args, **kwargs):
                                  help='Name of the %s(s) to configure.' %
                                       resource.get_display_name().lower())
 
-    @resource.add_auth_token_to_kwargs_from_cli
+    @add_auth_token_to_kwargs_from_cli
     def run(self, args, **kwargs):
         schema = self.app.client.managers['ConfigSchema'].get_by_ref_or_id(args.name, **kwargs)
 
@@ -344,7 +345,8 @@ def run(self, args, **kwargs):
         if save_dialog.read() == 'n':
             raise OperationFailureException('Interrupted')
 
-        result = self.app.client.managers['Config'].update(Config(pack=args.name, values=config))
+        config_item = Config(pack=args.name, values=config)
+        result = self.app.client.managers['Config'].update(config_item, **kwargs)
 
         return result
 


Test patch: