Hi,
this patch enables logging json dumps of request and response, using
the --log-payload switch in ipa cli. RFC tag is to ensure that I
handled the --log-payload switch correctly in ipa cli. Be careful, it
only logs, so --log-payload without -v switch doesn't make the dump
visible in command line, -v does!

https://fedorahosted.org/freeipa/ticket/4233

Thanks
Adam
>From f2230d5200feeb6fa413f4b248736b38ba66d317 Mon Sep 17 00:00:00 2001
From: Adam Misnyovszki <amisn...@redhat.com>
Date: Wed, 16 Apr 2014 14:58:18 +0200
Subject: [PATCH] Log pretty-printed request and response

With the --log-payload option, every request/response is
logged with json.dumps.

https://fedorahosted.org/freeipa/ticket/4233
---
 ipalib/constants.py |  1 +
 ipalib/plugable.py  |  7 +++++--
 ipalib/rpc.py       | 14 +++++++++++++-
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/ipalib/constants.py b/ipalib/constants.py
index 6cc50eacf44678840ad0048a1ef60c05736879cb..6acd7cef549d8b06366ee07adcbeb0a4d1b411d2 100644
--- a/ipalib/constants.py
+++ b/ipalib/constants.py
@@ -158,6 +158,7 @@ DEFAULT_CONFIG = (
     ('interactive', True),
     ('fallback', True),
     ('delegate', False),
+    ('log_payload', False),
 
     # Enable certain optional plugins:
     ('enable_ra', False),
diff --git a/ipalib/plugable.py b/ipalib/plugable.py
index 216f9c08a8b5d22bdb1e7853013967e8fe3f88b0..47e52b662f1421f0476fd7b301cd62043448a50d 100644
--- a/ipalib/plugable.py
+++ b/ipalib/plugable.py
@@ -597,7 +597,10 @@ class API(DictProxy):
             parser.add_option('-f', '--no-fallback', action='store_false',
                 dest='fallback',
                 help='Only use the server configured in /etc/ipa/default.conf'
-            )
+                )
+            parser.add_option('--log-payload', action='store_true',
+                help='Logs formatted json payload',
+                )
 
         return parser
 
@@ -617,7 +620,7 @@ class API(DictProxy):
                     pass
                 overrides[str(key.strip())] = value.strip()
         for key in ('conf', 'debug', 'verbose', 'prompt_all', 'interactive',
-            'fallback', 'delegate'):
+            'fallback', 'delegate', 'log_payload'):
             value = getattr(options, key, None)
             if value is not None:
                 overrides[key] = value
diff --git a/ipalib/rpc.py b/ipalib/rpc.py
index 2b47d1c0e25bbeec0dde38089f444e0399e1670e..fa13e5519de51a2a2e341fb94ca452f71087d102 100644
--- a/ipalib/rpc.py
+++ b/ipalib/rpc.py
@@ -738,6 +738,8 @@ class RPCClient(Connectible):
         for url in urls:
             kw = dict(allow_none=True, encoding='UTF-8')
             kw['verbose'] = verbose
+            if self.server_proxy_class == JSONServerProxy:
+                kw['log_payload'] = self.env.log_payload
             if url.startswith('https://'):
                 if delegate:
                     transport_class = DelegatedKerbTransport
@@ -783,6 +785,7 @@ class RPCClient(Connectible):
                     except Exception, e:
                         # This shouldn't happen if we have a session but it isn't fatal.
                         pass
+
                     return self.create_connection(ccache, verbose, fallback, delegate)
                 if not fallback:
                     raise
@@ -900,7 +903,8 @@ class xmlclient(RPCClient):
 
 
 class JSONServerProxy(object):
-    def __init__(self, uri, transport, encoding, verbose, allow_none):
+    def __init__(self, uri, transport, encoding, verbose, allow_none,
+                log_payload):
         type, uri = urllib.splittype(uri)
         if type not in ("http", "https"):
             raise IOError("unsupported XML-RPC protocol")
@@ -910,6 +914,7 @@ class JSONServerProxy(object):
         assert encoding == 'UTF-8'
         assert allow_none
         self.__verbose = verbose
+        self.__log_payload = log_payload
 
         # FIXME: Some of our code requires ServerProxy internals.
         # But, xmlrpclib.ServerProxy's _ServerProxy__transport can be accessed
@@ -919,6 +924,10 @@ class JSONServerProxy(object):
     def __request(self, name, args):
         payload = {'method': unicode(name), 'params': args, 'id': 0}
 
+        if self.__log_payload:
+            root_logger.info('Request: %s', json.dumps(payload, sort_keys=True,
+                                                       indent=4))
+
         response = self.__transport.request(
             self.__host,
             self.__handler,
@@ -931,6 +940,9 @@ class JSONServerProxy(object):
         except ValueError, e:
             raise JSONError(str(e))
 
+        if self.__log_payload:
+            root_logger.info('Response: %s', json.dumps(response, sort_keys=True,
+                                                        indent=4))
         error = response.get('error')
         if error:
             try:
-- 
1.9.0

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to