forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 15
/
test_secrets_masker.py
377 lines (315 loc) · 13 KB
/
test_secrets_masker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import contextlib
import inspect
import io
import logging
import logging.config
import os
import sys
import textwrap
import pytest
from airflow import settings
from airflow.utils.log.secrets_masker import RedactedIO, SecretsMasker, should_hide_value_for_key
from tests.test_utils.config import conf_vars
settings.MASK_SECRETS_IN_LOGS = True
p = "password"
@pytest.fixture
def logger(caplog):
logging.config.dictConfig(
{
'version': 1,
'handlers': {
__name__: {
# Reset later
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout',
}
},
'loggers': {
__name__: {
'handlers': [__name__],
'level': logging.INFO,
'propagate': False,
}
},
'disable_existing_loggers': False,
}
)
formatter = ShortExcFormatter("%(levelname)s %(message)s")
logger = logging.getLogger(__name__)
caplog.handler.setFormatter(formatter)
logger.handlers = [caplog.handler]
filt = SecretsMasker()
logger.addFilter(filt)
filt.add_mask('password')
return logger
class TestSecretsMasker:
def test_message(self, logger, caplog):
logger.info("XpasswordY")
assert caplog.text == "INFO X***Y\n"
def test_args(self, logger, caplog):
logger.info("Cannot connect to %s", "user:password")
assert caplog.text == "INFO Cannot connect to user:***\n"
def test_extra(self, logger, caplog):
logger.handlers[0].formatter = ShortExcFormatter("%(levelname)s %(message)s %(conn)s")
logger.info("Cannot connect", extra={'conn': "user:password"})
assert caplog.text == "INFO Cannot connect user:***\n"
def test_exception(self, logger, caplog):
try:
conn = "user:password"
raise RuntimeError("Cannot connect to " + conn)
except RuntimeError:
logger.exception("Err")
line = lineno() - 4
assert caplog.text == textwrap.dedent(
f"""\
ERROR Err
Traceback (most recent call last):
File ".../test_secrets_masker.py", line {line}, in test_exception
raise RuntimeError("Cannot connect to " + conn)
RuntimeError: Cannot connect to user:***
"""
)
def test_exception_not_raised(self, logger, caplog):
"""
Test that when ``logger.exception`` is called when there is no current exception we still log.
(This is a "bug" in user code, but we shouldn't die because of it!)
"""
logger.exception("Err")
assert caplog.text == textwrap.dedent(
"""\
ERROR Err
NoneType: None
"""
)
def test_exc_tb(self, logger, caplog):
"""
Show it is not possible to filter secrets in the source.
It is not possible to (regularly/reliably) filter out secrets that
appear directly in the source code. This is because the formatting of
exc_info is not done in the filter, it is done after the filter is
called, and fixing this "properly" is hard/impossible.
(It would likely need to construct a custom traceback that changed the
source. I have no idead if that is even possible)
This test illustrates that, but ix marked xfail in case someone wants to
fix this later.
"""
try:
raise RuntimeError("Cannot connect to user:password")
except RuntimeError:
logger.exception("Err")
line = lineno() - 4
assert caplog.text == textwrap.dedent(
f"""\
ERROR Err
Traceback (most recent call last):
File ".../test_secrets_masker.py", line {line}, in test_exc_tb
raise RuntimeError("Cannot connect to user:password")
RuntimeError: Cannot connect to user:***
"""
)
def test_masking_in_implicit_context_exceptions(self, logger, caplog):
"""
Show that redacting password works in context exceptions.
"""
try:
try:
try:
raise RuntimeError(f"Cannot connect to user:{p}")
except RuntimeError as ex1:
raise RuntimeError(f'Exception: {ex1}')
except RuntimeError as ex2:
raise RuntimeError(f'Exception: {ex2}')
except RuntimeError:
logger.exception("Err")
line = lineno() - 8
assert caplog.text == textwrap.dedent(
f"""\
ERROR Err
Traceback (most recent call last):
File ".../test_secrets_masker.py", line {line}, in test_masking_in_implicit_context_exceptions
raise RuntimeError(f"Cannot connect to user:{{p}}")
RuntimeError: Cannot connect to user:***
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".../test_secrets_masker.py", line {line+2}, in test_masking_in_implicit_context_exceptions
raise RuntimeError(f'Exception: {{ex1}}')
RuntimeError: Exception: Cannot connect to user:***
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".../test_secrets_masker.py", line {line+4}, in test_masking_in_implicit_context_exceptions
raise RuntimeError(f'Exception: {{ex2}}')
RuntimeError: Exception: Exception: Cannot connect to user:***
"""
)
def test_masking_in_explicit_context_exceptions(self, logger, caplog):
"""
Show that redacting password works in context exceptions.
"""
exception = None
try:
raise RuntimeError(f"Cannot connect to user:{p}")
except RuntimeError as ex:
exception = ex
try:
raise RuntimeError(f'Exception: {exception}') from exception
except RuntimeError:
logger.exception("Err")
line = lineno() - 8
assert caplog.text == textwrap.dedent(
f"""\
ERROR Err
Traceback (most recent call last):
File ".../test_secrets_masker.py", line {line}, in test_masking_in_explicit_context_exceptions
raise RuntimeError(f"Cannot connect to user:{{p}}")
RuntimeError: Cannot connect to user:***
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File ".../test_secrets_masker.py", line {line+4}, in test_masking_in_explicit_context_exceptions
raise RuntimeError(f'Exception: {{exception}}') from exception
RuntimeError: Exception: Cannot connect to user:***
"""
)
@pytest.mark.parametrize(
("name", "value", "expected_mask"),
[
(None, "secret", {"secret"}),
("apikey", "secret", {"secret"}),
# the value for "apikey", and "password" should end up masked
(None, {"apikey": "secret", "other": {"val": "innocent", "password": "foo"}}, {"secret", "foo"}),
(None, ["secret", "other"], {"secret", "other"}),
# When the "sensitive value" is a dict, don't mask anything
# (Or should this be mask _everything_ under it ?
("api_key", {"other": "innoent"}, set()),
(None, {"password": ""}, set()),
(None, "", set()),
],
)
def test_mask_secret(self, name, value, expected_mask):
filt = SecretsMasker()
filt.add_mask(value, name)
assert filt.patterns == expected_mask
@pytest.mark.parametrize(
("patterns", "name", "value", "expected"),
[
({"secret"}, None, "secret", "***"),
(
{"secret", "foo"},
None,
{"apikey": "secret", "other": {"val": "innocent", "password": "foo"}},
{"apikey": "***", "other": {"val": "innocent", "password": "***"}},
),
({"secret", "other"}, None, ["secret", "other"], ["***", "***"]),
# We don't mask dict _keys_.
({"secret", "other"}, None, {"data": {"secret": "secret"}}, {"data": {"secret": "***"}}),
# Non string dict keys
({"secret", "other"}, None, {1: {"secret": "secret"}}, {1: {"secret": "***"}}),
(
# Since this is a sensitive name, all the values should be redacted!
{"secret"},
"api_key",
{"other": "innoent", "nested": ["x", "y"]},
{"other": "***", "nested": ["***", "***"]},
),
(
# Test that masking still works based on name even when no patterns given
set(),
'env',
{'api_key': 'masked based on key name', 'other': 'foo'},
{'api_key': '***', 'other': 'foo'},
),
],
)
def test_redact(self, patterns, name, value, expected):
filt = SecretsMasker()
for val in patterns:
filt.add_mask(val)
assert filt.redact(value, name) == expected
def test_redact_filehandles(self, caplog):
filt = SecretsMasker()
with open("/dev/null", "w") as handle:
assert filt.redact(handle, None) == handle
# We shouldn't have logged a warning here
assert caplog.messages == []
class TestShouldHideValueForKey:
@pytest.mark.parametrize(
("key", "expected_result"),
[
('', False),
(None, False),
("key", False),
("google_api_key", True),
("GOOGLE_API_KEY", True),
("GOOGLE_APIKEY", True),
(1, False),
],
)
def test_hiding_defaults(self, key, expected_result):
assert expected_result == should_hide_value_for_key(key)
@pytest.mark.parametrize(
("sensitive_variable_fields", "key", "expected_result"),
[
('key', 'TRELLO_KEY', True),
('key', 'TRELLO_API_KEY', True),
('key', 'GITHUB_APIKEY', True),
('key, token', 'TRELLO_TOKEN', True),
('mysecretword, mysensitivekey', 'GITHUB_mysecretword', True),
(None, 'TRELLO_API', False),
('token', 'TRELLO_KEY', False),
('token, mysecretword', 'TRELLO_KEY', False),
],
)
def test_hiding_config(self, sensitive_variable_fields, key, expected_result):
from airflow.utils.log.secrets_masker import get_sensitive_variables_fields
with conf_vars({('core', 'sensitive_var_conn_names'): str(sensitive_variable_fields)}):
get_sensitive_variables_fields.cache_clear()
assert expected_result == should_hide_value_for_key(key)
get_sensitive_variables_fields.cache_clear()
class ShortExcFormatter(logging.Formatter):
"""Don't include full path in exc_info messages"""
def formatException(self, exc_info):
formatted = super().formatException(exc_info)
return formatted.replace(__file__, ".../" + os.path.basename(__file__))
def lineno():
"""Returns the current line number in our program."""
return inspect.currentframe().f_back.f_lineno
class TestRedactedIO:
def test_redacts_from_print(self, capsys):
# Without redacting, password is printed.
print(p)
stdout = capsys.readouterr().out
assert stdout == f"{p}\n"
assert "***" not in stdout
# With context manager, password is redacted.
with contextlib.redirect_stdout(RedactedIO()):
print(p)
stdout = capsys.readouterr().out
assert stdout == "***\n"
def test_write(self, capsys):
RedactedIO().write(p)
stdout = capsys.readouterr().out
assert stdout == "***"
def test_input_builtin(self, monkeypatch):
"""
Test that when redirect is inplace the `input()` builtin works.
This is used by debuggers!
"""
monkeypatch.setattr(sys, 'stdin', io.StringIO("a\n"))
with contextlib.redirect_stdout(RedactedIO()):
assert input() == "a"