Skip to content

Commit efca223

Browse files
committed
adding regex for aws key and apikey values
1 parent c24abca commit efca223

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/scrapy_settings_log/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ def spider_closed(self, spider):
5151
regex = settings.get("SETTINGS_LOGGING_REGEX")
5252
if regex is not None:
5353
settings = {k: v for k, v in settings.items() if re.search(regex, k)}
54+
default_regexes = [
55+
"(?i)(api[\W_]*key)", # apikey and possible variations
56+
"(?i)(AWS[\W_]*SECRET[\W_]*ACCESS[\W_]*KEY)" # AWS_SECRET_ACCESS_KEY and possible variations
57+
]
58+
if settings.get("MASKED_SENSITIVE_SETTINGS_ENABLED", True):
59+
regex_list = settings.get("MASKED_SENSITIVE_SETTINGS_REGEX_LIST", default_regexes)
60+
for reg in regex_list:
61+
updated_settings = {k: '*'*len(v) for k, v in settings.items() if re.match(reg, k)}
62+
settings = {**settings, **updated_settings}
5463

5564
self.output_settings(settings, spider)
5665

tests/test_code.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,60 @@ class CustomClass:
103103
logger.spider_closed(spider)
104104

105105
assert '{"DUMMY_CUSTOM_CLASS": {"CustomClass": "/foo/bar"}}' in caplog.text
106+
107+
108+
def test_log_all_should_not_return_apikey_value_by_default(caplog):
109+
settings = {
110+
"SETTINGS_LOGGING_ENABLED": True,
111+
"APIKEY": 'apikey_value1',
112+
"apikey": 'apikey_value2',
113+
"api_key": 'apikey_value3',
114+
}
115+
116+
spider = MockSpider(settings)
117+
logger = SpiderSettingsLogging()
118+
with caplog.at_level(logging.INFO):
119+
logger.spider_closed(spider)
120+
121+
# won't check specifics here as the default settings
122+
# can vary with scrapy versions - presence is enough
123+
assert '"APIKEY": "*************"' in caplog.text
124+
assert '"apikey": "*************"' in caplog.text
125+
assert '"api_key": "*************"' in caplog.text
126+
assert 'apikey_value' not in caplog.text
127+
128+
129+
def test_log_all_should_return_apikey_value_if_MASKED_SENSITIVE_SETTINGS_ENABLED_is_false(caplog):
130+
settings = {
131+
"SETTINGS_LOGGING_ENABLED": True,
132+
"APIKEY": 'apikey_value',
133+
"MASKED_SENSITIVE_SETTINGS_ENABLED": False,
134+
}
135+
136+
spider = MockSpider(settings)
137+
logger = SpiderSettingsLogging()
138+
with caplog.at_level(logging.INFO):
139+
logger.spider_closed(spider)
140+
141+
# won't check specifics here as the default settings
142+
# can vary with scrapy versions - presence is enough
143+
assert '"APIKEY": "apikey_value"' in caplog.text
144+
145+
146+
def test_log_all_should_not_return_aws_secret_key_value_by_default(caplog):
147+
settings = {
148+
"SETTINGS_LOGGING_ENABLED": True,
149+
"AWS_SECRET_ACCESS_KEY": 'secret_value1',
150+
"aws_secret_access_key": 'secret_value2',
151+
}
152+
153+
spider = MockSpider(settings)
154+
logger = SpiderSettingsLogging()
155+
with caplog.at_level(logging.INFO):
156+
logger.spider_closed(spider)
157+
158+
# won't check specifics here as the default settings
159+
# can vary with scrapy versions - presence is enough
160+
assert '"AWS_SECRET_ACCESS_KEY": "*************"' in caplog.text
161+
assert '"aws_secret_access_key": "*************"' in caplog.text
162+
assert 'secret_value' not in caplog.text

0 commit comments

Comments
 (0)