@@ -48,11 +48,11 @@ class ModuleUtils():
48
48
49
49
# List of private fields and regular expressions to hide them
50
50
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}$ " ,
56
56
}
57
57
58
58
@classmethod
@@ -96,27 +96,60 @@ def mask_secret(cls, string: str, show_len: int = 4) -> str:
96
96
return f"{ string [:show_len ]} { cls .HIDING_MASK } { string [- show_len :]} "
97
97
98
98
@classmethod
99
- def hide_private (cls , message : str , fields : dict = None ) -> str :
99
+ def hide_private (cls , input_data : dict , fields : dict = None ) -> dict :
100
100
"""Hide private data Zabbix info (e.g. token, password)
101
101
102
102
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.
105
105
106
106
Returns:
107
- str: Message text without private data.
107
+ dict: Result dictionary without private data.
108
108
"""
109
109
110
110
private_fields = fields if fields else cls .PRIVATE_FIELDS
111
111
112
+ if not isinstance (input_data , dict ):
113
+ raise TypeError (f"Unsupported data type '{ type (input_data ).__name__ } ', \
114
+ only 'dict' is expected" )
115
+
112
116
def gen_repl (match : Match ):
113
117
return cls .mask_secret (match .group (0 ))
114
118
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
120
153
121
154
122
155
class ZabbixProtocol ():
@@ -126,22 +159,22 @@ class ZabbixProtocol():
126
159
HEADER_SIZE = 13
127
160
128
161
@classmethod
129
- def __prepare_request (cls , data : Union [bytes , str , dict ]) -> bytes :
162
+ def __prepare_request (cls , data : Union [bytes , str , list , dict ]) -> bytes :
130
163
if isinstance (data , bytes ):
131
164
return data
132
165
if isinstance (data , str ):
133
166
return data .encode ("utf-8" )
134
167
if isinstance (data , list ) or isinstance (data , dict ):
135
168
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" )
137
170
138
171
@classmethod
139
- def create_packet (cls , payload : Union [bytes , str , dict ],
172
+ def create_packet (cls , payload : Union [bytes , str , list , dict ],
140
173
log : Logger , compression : bool = False ) -> bytes :
141
174
"""Create a packet for sending via the Zabbix protocol.
142
175
143
176
Args:
144
- payload (Union[bytes, str, dict]): Payload of the future packet
177
+ payload (Union[bytes, str, list, dict]): Payload of the future packet
145
178
log (Logger): Logger object
146
179
compression (bool, optional): Compression use flag. Defaults to `False`.
147
180
0 commit comments