Skip to content

Commit ddfbc16

Browse files
committed
session: Use Authorization header for RH bugzilla
See https://bugzilla.redhat.com/show_bug.cgi?id=1833585 bugzilla.redhat.com has added support for non-standard 'Authorization: Bearer $APIKEY' header for authenticating. Other auth methods may eventually be removed. So let's start using this for bugzilla.redhat.com One caveat is that we need to stop sending token/apikey values as query parameters when this header is used Signed-off-by: Cole Robinson <[email protected]>
1 parent 6573d90 commit ddfbc16

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

bugzilla/_session.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ class _BugzillaSession(object):
1818
Class to handle the backend agnostic 'requests' setup
1919
"""
2020
def __init__(self, url, user_agent,
21-
sslverify, cert,
22-
tokencache, api_key, requests_session=None):
21+
sslverify, cert, tokencache, api_key,
22+
is_redhat_bugzilla,
23+
requests_session=None):
2324
self._url = url
2425
self._user_agent = user_agent
2526
self._scheme = urllib.parse.urlparse(url)[0]
2627
self._tokencache = tokencache
2728
self._api_key = api_key
2829
self._is_xmlrpc = False
30+
self._use_auth_bearer = False
2931

3032
if self._scheme not in ["http", "https"]:
3133
raise Exception("Invalid URL scheme: %s (%s)" % (
@@ -41,6 +43,11 @@ def __init__(self, url, user_agent,
4143
self._session.verify = False
4244
self._session.headers["User-Agent"] = self._user_agent
4345

46+
if is_redhat_bugzilla and self._api_key:
47+
self._use_auth_bearer = True
48+
self._session.headers["Authorization"] = (
49+
"Bearer %s" % self._api_key)
50+
4451
def _get_timeout(self):
4552
# Default to 5 minutes. This is longer than bugzilla.redhat.com's
4653
# apparent 3 minute timeout so shouldn't affect legitimate usage,
@@ -63,6 +70,11 @@ def set_token_value(self, value):
6370
self._tokencache.set_value(self._url, value)
6471

6572
def get_auth_params(self):
73+
# bugzilla.redhat.com will error if there's auth bits in params
74+
# when Authorization header is used
75+
if self._use_auth_bearer:
76+
return {}
77+
6678
# Don't add a token to the params list if an API key is set.
6779
# Keeping API key solo means bugzilla will definitely fail
6880
# if the key expires. Passing in a token could hide that

bugzilla/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,11 +504,15 @@ def connect(self, url=None):
504504
# we've changed URLs - reload config
505505
self.readconfig(overwrite=False)
506506

507+
# Detect if connecting to redhat bugzilla
508+
self._init_class_from_url()
509+
507510
self._session = _BugzillaSession(self.url, self.user_agent,
508511
sslverify=self._sslverify,
509512
cert=self.cert,
510513
tokencache=self._tokencache,
511514
api_key=self.api_key,
515+
is_redhat_bugzilla=self._is_redhat_bugzilla,
512516
requests_session=self._user_requests_session)
513517
self._backend = backendclass(self.url, self._session)
514518

@@ -522,7 +526,6 @@ def connect(self, url=None):
522526
version = self._backend.bugzilla_version()["version"]
523527
log.debug("Bugzilla version string: %s", version)
524528
self._set_bz_version(version)
525-
self._init_class_from_url()
526529

527530

528531
@property

0 commit comments

Comments
 (0)