@@ -48,11 +48,11 @@ class ModuleUtils():
4848
4949 # List of private fields and regular expressions to hide them
5050 PRIVATE_FIELDS = {
51- "token" : "[A-Za-z0-9]+ " ,
52- "auth" : "[A-Za-z0-9]+ " ,
53- "sessionid" : "[A-Za-z0-9]+ " ,
54- "password" : "[^' \" ]+ " ,
55- "result" : "(?!(zabbix_export|[0-9.]{5}))[ A-Za-z0-9]+ " ,
51+ "token" : r"^.+$ " ,
52+ "auth" : r"^.+$ " ,
53+ "sessionid" : r"^.+$ " ,
54+ "password" : r"^.+$ " ,
55+ "result" : r"^[ A-Za-z0-9]{32}$ " ,
5656 }
5757
5858 @classmethod
@@ -96,27 +96,60 @@ def mask_secret(cls, string: str, show_len: int = 4) -> str:
9696 return f"{ string [:show_len ]} { cls .HIDING_MASK } { string [- show_len :]} "
9797
9898 @classmethod
99- def hide_private (cls , message : str , fields : dict = None ) -> str :
99+ def hide_private (cls , input_data : dict , fields : dict = None ) -> dict :
100100 """Hide private data Zabbix info (e.g. token, password)
101101
102102 Args:
103- message (str ): Message text with private data .
104- fields (dict): Dictionary of private fields and their seeking regexps.
103+ input_data (dict ): Input dictionary with private fields .
104+ fields (dict): Dictionary of private fields and their filtering regexps.
105105
106106 Returns:
107- str: Message text without private data.
107+ dict: Result dictionary without private data.
108108 """
109109
110110 private_fields = fields if fields else cls .PRIVATE_FIELDS
111111
112+ if not isinstance (input_data , dict ):
113+ raise TypeError (f"Unsupported data type '{ type (input_data ).__name__ } ', \
114+ only 'dict' is expected" )
115+
112116 def gen_repl (match : Match ):
113117 return cls .mask_secret (match .group (0 ))
114118
115- pattern = re .compile (
116- r"|" .join ([rf"(?<=\"{ f } \":\s\"){ r } " for f , r in private_fields .items ()])
117- )
118-
119- return re .sub (pattern , gen_repl , message )
119+ def hide_str (k , v ):
120+ return re .sub (private_fields [k ], gen_repl , v )
121+
122+ def hide_dict (v ):
123+ return cls .hide_private (v )
124+
125+ def hide_list (v ):
126+ result = []
127+ for item in v :
128+ if isinstance (item , dict ):
129+ result .append (hide_dict (item ))
130+ continue
131+ if isinstance (item , list ):
132+ result .append (hide_list (item ))
133+ continue
134+ if isinstance (item , str ):
135+ if 'result' in private_fields :
136+ result .append (hide_str ('result' , item ))
137+ continue
138+ result .append (item )
139+ return result
140+
141+ result_data = input_data .copy ()
142+
143+ for key , value in result_data .items ():
144+ if isinstance (value , str ):
145+ if key in private_fields :
146+ result_data [key ] = hide_str (key , value )
147+ if isinstance (value , dict ):
148+ result_data [key ] = hide_dict (value )
149+ if isinstance (value , list ):
150+ result_data [key ] = hide_list (value )
151+
152+ return result_data
120153
121154
122155class ZabbixProtocol ():
@@ -126,22 +159,22 @@ class ZabbixProtocol():
126159 HEADER_SIZE = 13
127160
128161 @classmethod
129- def __prepare_request (cls , data : Union [bytes , str , dict ]) -> bytes :
162+ def __prepare_request (cls , data : Union [bytes , str , list , dict ]) -> bytes :
130163 if isinstance (data , bytes ):
131164 return data
132165 if isinstance (data , str ):
133166 return data .encode ("utf-8" )
134167 if isinstance (data , list ) or isinstance (data , dict ):
135168 return json .dumps (data , ensure_ascii = False ).encode ("utf-8" )
136- raise TypeError ("Unsupported data type, only 'bytes', 'str' or 'dict' is expected" )
169+ raise TypeError ("Unsupported data type, only 'bytes', 'str', 'list' or 'dict' is expected" )
137170
138171 @classmethod
139- def create_packet (cls , payload : Union [bytes , str , dict ],
172+ def create_packet (cls , payload : Union [bytes , str , list , dict ],
140173 log : Logger , compression : bool = False ) -> bytes :
141174 """Create a packet for sending via the Zabbix protocol.
142175
143176 Args:
144- payload (Union[bytes, str, dict]): Payload of the future packet
177+ payload (Union[bytes, str, list, dict]): Payload of the future packet
145178 log (Logger): Logger object
146179 compression (bool, optional): Compression use flag. Defaults to `False`.
147180
0 commit comments