2727from __future__ import absolute_import
2828
2929import io
30+ import json
3031import logging
3132import socket
3233import ssl
6061 "HTTPError"
6162]
6263
64+ SENSITIVE_KEYS = ["password" , "token" , "Authorization" ]
6365# If you change these, update the docstring
6466# on _authority as well.
6567DEFAULT_HOST = "localhost"
6668DEFAULT_PORT = "8089"
6769DEFAULT_SCHEME = "https"
6870
71+
6972def _log_duration (f ):
7073 @wraps (f )
7174 def new_f (* args , ** kwargs ):
@@ -77,6 +80,27 @@ def new_f(*args, **kwargs):
7780 return new_f
7881
7982
83+ def _get_masked_data (data ):
84+ '''
85+ Masked sensitive fields data for logging purpose
86+ '''
87+ if not isinstance (data , dict ):
88+ try :
89+ data = json .loads (data )
90+ except Exception as ex :
91+ return data
92+
93+ if not isinstance (data , dict ):
94+ return data
95+ mdata = {}
96+ for k , v in data .items ():
97+ if k in SENSITIVE_KEYS :
98+ mdata [k ] = "******"
99+ else :
100+ mdata [k ] = _get_masked_data (v )
101+ return mdata
102+
103+
80104def _parse_cookies (cookie_str , dictionary ):
81105 """Tries to parse any key-value pairs of cookies in a string,
82106 then updates the the dictionary with any key-value pairs found.
@@ -630,7 +654,7 @@ def delete(self, path_segment, owner=None, app=None, sharing=None, **query):
630654 """
631655 path = self .authority + self ._abspath (path_segment , owner = owner ,
632656 app = app , sharing = sharing )
633- logger .debug ("DELETE request to %s (body: %s)" , path , repr (query ))
657+ logger .debug ("DELETE request to %s (body: %s)" , path , _get_masked_data (query ))
634658 response = self .http .delete (path , self ._auth_headers , ** query )
635659 return response
636660
@@ -693,7 +717,7 @@ def get(self, path_segment, owner=None, app=None, headers=None, sharing=None, **
693717
694718 path = self .authority + self ._abspath (path_segment , owner = owner ,
695719 app = app , sharing = sharing )
696- logger .debug ("GET request to %s (body: %s)" , path , repr (query ))
720+ logger .debug ("GET request to %s (body: %s)" , path , _get_masked_data (query ))
697721 all_headers = headers + self .additional_headers + self ._auth_headers
698722 response = self .http .get (path , all_headers , ** query )
699723 return response
@@ -772,12 +796,7 @@ def post(self, path_segment, owner=None, app=None, sharing=None, headers=None, *
772796
773797 path = self .authority + self ._abspath (path_segment , owner = owner , app = app , sharing = sharing )
774798
775- # To avoid writing sensitive data in debug logs
776- endpoint_having_sensitive_data = ["/storage/passwords" ]
777- if any (endpoint in path for endpoint in endpoint_having_sensitive_data ):
778- logger .debug ("POST request to %s " , path )
779- else :
780- logger .debug ("POST request to %s (body: %s)" , path , repr (query ))
799+ logger .debug ("POST request to %s (body: %s)" , path , _get_masked_data (query ))
781800 all_headers = headers + self .additional_headers + self ._auth_headers
782801 response = self .http .post (path , all_headers , ** query )
783802 return response
@@ -844,7 +863,7 @@ def request(self, path_segment, method="GET", headers=None, body={},
844863
845864 all_headers = headers + self .additional_headers + self ._auth_headers
846865 logger .debug ("%s request to %s (headers: %s, body: %s)" ,
847- method , path , str (all_headers ), repr (body ))
866+ method , path , str (all_headers ), _get_masked_data (body ))
848867
849868 if body :
850869 body = _encode (** body )
0 commit comments