Bläddra i källkod

HUE-5076 [core] Unpack thrift handle guid, secret before adding to logs

krish 9 år sedan
förälder
incheckning
1c06e78

+ 25 - 3
desktop/core/src/desktop/lib/thrift_util.py

@@ -21,7 +21,9 @@ import logging
 import socket
 import threading
 import time
+import re
 import sasl
+import struct
 import sys
 
 from thrift.Thrift import TType, TApplicationException
@@ -424,15 +426,18 @@ class SuperClient(object):
           if not self.transport.isOpen():
             self.transport.open()
           st = time.time()
+
+          str_args = _unpack_guid_secret_in_handle(repr(args))
           logging.debug("Thrift call: %s.%s(args=%s, kwargs=%s)"
-            % (str(self.wrapped.__class__), attr, repr(args), repr(kwargs)))
+            % (str(self.wrapped.__class__), attr, str_args, repr(kwargs)))
+
           ret = res(*args, **kwargs)
-          log_msg = repr(ret)
+          log_msg = _unpack_guid_secret_in_handle(repr(ret))
 
           # Truncate log message, increase output in DEBUG mode
           log_limit = 2000 if settings.DEBUG else 1000
           log_msg = log_msg[:log_limit] + (log_msg[log_limit:] and '...')
-          
+
           duration = time.time() - st
 
           # Log the duration at different levels, depending on how long
@@ -478,6 +483,23 @@ class SuperClient(object):
       else:
         _grab_transport_from_wrapper(self.transport).setTimeout(None)
 
+def _unpack_guid_secret_in_handle(str_args):
+  if 'operationHandle' in str_args or 'sessionHandle' in str_args:
+    secret = re.search('secret=(\".*\"), guid', str_args) or re.search('secret=(\'.*\'), guid', str_args)
+    guid = re.search('guid=(\".*\")\)\)', str_args) or re.search('guid=(\'.*\')\)\)', str_args)
+
+    if secret and guid:
+      try:
+        encoded_secret = eval(secret.group(1))
+        encoded_guid = eval(guid.group(1))
+
+        str_args = str_args.replace(secret.group(1), "%x:%x" % struct.unpack(b"QQ", encoded_secret))
+        str_args = str_args.replace(guid.group(1), "%x:%x" % struct.unpack(b"QQ", encoded_guid))
+      except Exception:
+        logging.warn("Unable to unpack the secret and guid in Thrift Handle.")
+
+  return str_args
+
 def simpler_string(thrift_obj):
   """
   Strips out nulls and empty arrays from the string representation.

+ 8 - 1
desktop/core/src/desktop/lib/thrift_util_test.py

@@ -33,7 +33,7 @@ from djangothrift_test_gen import TestService
 
 import python_util
 import thrift_util
-from thrift_util import jsonable2thrift, thrift2json
+from thrift_util import jsonable2thrift, thrift2json, _unpack_guid_secret_in_handle
 
 from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
 from thrift.server import TServer
@@ -218,6 +218,13 @@ class ThriftUtilTest(unittest.TestCase):
     self.assertTrue(hasattr(struct1,"myenumAsString"))
     self.assertEquals(struct1.myenumAsString,'ENUM_ONE')
 
+  def test_unpack_guid_secret_in_handle(self):
+    hive_handle = """(TExecuteStatementReq(confOverlay={}, sessionHandle=TSessionHandle(sessionId=THandleIdentifier(secret=\'\x1aOYj\xf3\x86M\x95\xbb\xc8\xe9/;\xb0{9\', guid=\'\x86\xa6$\xb2\xb8\xdaF\xbd\xbd\xf5\xc5\xf4\xcb\x96\x03<\')), runAsync=True, statement="SELECT \'Hello World!\'"),)"""
+    self.assertEqual(_unpack_guid_secret_in_handle(hive_handle), """(TExecuteStatementReq(confOverlay={}, sessionHandle=TSessionHandle(sessionId=THandleIdentifier(secret=954d86f36a594f1a:397bb03b2fe9c8bb, guid=bd46dab8b224a686:3c0396cbf4c5f5bd)), runAsync=True, statement="SELECT \'Hello World!\'"),)""")
+
+    impala_handle = """(TGetTablesReq(schemaName=u\'default\', sessionHandle=TSessionHandle(sessionId=THandleIdentifier(secret=\'\x7f\x98\x97s\xe1\xa8G\xf4\x8a\x8a\\r\x0e6\xc2\xee\xf0\', guid=\'\xfa\xb0/\x04 \xfeDX\x99\xfcq\xff2\x07\x02\xfe\')), tableName=u\'customers\', tableTypes=None, catalogName=None),)"""
+    self.assertEqual(_unpack_guid_secret_in_handle(impala_handle), """(TGetTablesReq(schemaName=u\'default\', sessionHandle=TSessionHandle(sessionId=THandleIdentifier(secret=f447a8e17397987f:f0eec2360e0d8a8a, guid=5844fe20042fb0fa:fe020732ff71fc99)), tableName=u\'customers\', tableTypes=None, catalogName=None),)""")
+
 class TestJsonable2Thrift(unittest.TestCase):
   """
   Tests a handful of permutations of jsonable2thrift.