Skip to content

Commit 68b14e4

Browse files
askbCopilot
andcommitted
fix: Set browser User-Agent for Jenkins requests
Jenkins servers behind a WAF/CDN such as Cloudflare reject the default python-requests/python-jenkins User-Agent as suspected bot traffic, causing commands like "lftools jenkins get-credentials" to fail with a bot-detection challenge. Set a browser-like User-Agent on the underlying requests session after creating the python-jenkins client. The value defaults to a Firefox UA and can be overridden via the LFTOOLS_JENKINS_USER_AGENT environment variable. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Change-Id: Id76e2348d330aff12fdfa1ecea77daf306954242 Signed-off-by: Anil Belur <abelur@linuxfoundation.org>
1 parent e733825 commit 68b14e4

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

lftools/jenkins/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def jjb_ini() -> str | None:
4141

4242
JJB_INI: str | None = jjb_ini()
4343

44+
# Some Jenkins servers sit behind a WAF/CDN (e.g. Cloudflare) that blocks the
45+
# default python-requests/python-jenkins User-Agent as suspected bot traffic.
46+
# Sending a browser-like User-Agent avoids these bot-detection challenges.
47+
# The value can be overridden via the LFTOOLS_JENKINS_USER_AGENT env variable.
48+
DEFAULT_USER_AGENT: str = "Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0"
49+
4450

4551
class Jenkins:
4652
"""lftools Jenkins object."""
@@ -70,4 +76,17 @@ def __init__(
7076

7177
self.server: jenkins.Jenkins = jenkins.Jenkins(server, username=user, password=password) # type: ignore
7278

79+
# Override the default User-Agent so requests are not flagged as bot
80+
# traffic by WAF/CDN layers (e.g. Cloudflare) in front of Jenkins.
81+
# Use `or DEFAULT_USER_AGENT` so an empty env value still applies a
82+
# browser-like UA rather than falling back to the requests default.
83+
user_agent: str = os.environ.get("LFTOOLS_JENKINS_USER_AGENT") or DEFAULT_USER_AGENT
84+
# `_session` is a python-jenkins internal; guard against it being
85+
# renamed/removed in a future release so init never hard-fails.
86+
session = getattr(self.server, "_session", None)
87+
if session is not None:
88+
session.headers["User-Agent"] = user_agent
89+
else:
90+
log.debug("python-jenkins session unavailable; skipping User-Agent override.")
91+
7392
self.url: str = server

0 commit comments

Comments
 (0)