Skip to content

Commit

Permalink
decouple _config.run.user_agent from UA setter
Browse files Browse the repository at this point in the history
  • Loading branch information
leondz committed Nov 11, 2024
1 parent 44e5d30 commit f9ca2ea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 14 additions & 5 deletions garak/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,17 @@ def _store_config(settings_files) -> None:
reporting = _set_settings(reporting, settings["reporting"])


# not my favourite solution in this module, but if
# _config.set_http_lib_agents() to be predicated on a param instead of
# a _config.run value (i.e. user_agent) - which it needs to be if it can be
# used when the values are popped back to originals - then a separate way
# of passing the UA string to _garak_user_agent() needs to exist, outside of
# _config.run.user_agent
REQUESTS_AGENT = ""


def _garak_user_agent(dummy=None):
global run
if hasattr(run, "user_agent"):
return run.user_agent
else:
return "garak"
return str(REQUESTS_AGENT)


def set_all_http_lib_agents(agent_string):
Expand All @@ -170,9 +175,13 @@ def set_all_http_lib_agents(agent_string):


def set_http_lib_agents(agent_strings: dict):

global REQUESTS_AGENT

if "requests" in agent_strings:
from requests import utils

REQUESTS_AGENT = agent_strings["requests"]
utils.default_user_agent = _garak_user_agent
if "httpx" in agent_strings:
import httpx
Expand Down
3 changes: 3 additions & 0 deletions garak/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def main(arguments=None) -> None:

# load site config before loading CLI config
_cli_config_supplied = args.config is not None
prior_user_agents = _config.get_http_lib_agents()
_config.load_config(run_config_filename=args.config)

# extract what was actually passed on CLI; use a masking argparser
Expand Down Expand Up @@ -553,3 +554,5 @@ def main(arguments=None) -> None:
except (ValueError, GarakException) as e:
logging.exception(e)
print(e)

_config.set_http_lib_agents(prior_user_agents)

0 comments on commit f9ca2ea

Please sign in to comment.