Skip to content

Commit 8da4fe2

Browse files
committed
Remove warning exception, check recursively.
1 parent a7fbe6c commit 8da4fe2

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

garak/_config.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ def _nested_dict():
117117
# placeholder
118118
# generator, probe, detector, buff = {}, {}, {}, {}
119119

120+
def _key_exists(d: dict, key: str) -> bool:
121+
# Check for the presence of a key in a nested dict.
122+
if not isinstance(d, dict):
123+
return False
124+
if key in d.keys():
125+
return True
126+
else:
127+
return any([_key_exists(val, key) for val in d.values()])
128+
120129

121130
def _set_settings(config_obj, settings_obj: dict):
122131
for k, v in settings_obj.items():
@@ -143,10 +152,11 @@ def _load_yaml_config(settings_filenames) -> dict:
143152
with open(settings_filename, encoding="utf-8") as settings_file:
144153
settings = yaml.safe_load(settings_file)
145154
if settings is not None:
146-
if "api_key" in settings.keys():
155+
if _key_exists(settings, "api_key"):
156+
logging.info(f"API key found in {settings_filename}. Checking readability...")
147157
res = os.stat(settings_filename)
148158
if res.st_mode & stat.S_IROTH or res.st_mode & stat.S_IRGRP:
149-
raise ConfigSecretWarning(f"A possibly secret value (`api_key`) was detected in {settings_filename}, which is readable by users other than yourself.")
159+
logging.warn(f"A possibly secret value (`api_key`) was detected in {settings_filename}, which is readable by users other than yourself.")
150160
config = _combine_into(settings, config)
151161
return config
152162

garak/exception.py

-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,3 @@ class ConfigFailure(GarakException):
3636

3737
class PayloadFailure(GarakException):
3838
"""Problem instantiating/using payloads"""
39-
40-
41-
class ConfigSecretWarning(Warning):
42-
"""Raised when a secret value is detected in a config"""

0 commit comments

Comments
 (0)