pycontribs / jira

Python Jira library. Development chat available on https://matrix.to/#/#pycontribs:matrix.org
https://jira.readthedocs.io
BSD 2-Clause "Simplified" License
1.92k stars 857 forks source link

Logging=False is not honored in _create_oauth_session #1777

Open brooookemiller opened 6 months ago

brooookemiller commented 6 months ago

Bug summary

As a user setting Logging=False as a param on initialization of the JIRA class, I'd like for logging to be turned off entirely, so that I can structure my logs flexibly within my service wrapped around this class. However, I recently noticed (presumably following this change) the following issues that lead to this Logging = False preference not to necessarily be honored as I'd expect it to be.

Areas for Improvement:

  1. Tracebacks being printed due to using logging.exception in _create_oauth_session. This is notably the only place in the file where an exception is logged (here).
  2. Each attempt to try a different signature_method for the creds generates an exception currently, although it's intentional to attempt to iterate through other options until we reach the last fallback option.
  3. The exception log statement mentions "Consider specifying the signature via oauth['signature_method']" even if the signature_method was provided as a param on the initialization. (Actually recognizing whether a signature_method was used before recommending that would be an improvement.)

References:

Rough Suggestions:

method_provided = oauth.get("signature_method")
....
except JIRAError as error:
    if not method_provided:
        _logging.debug(
            f"Failed to create OAuth session with signature_method={sha_type}."
            + "Attempting fallback method(s)."
            + "Consider specifying the signature via oauth['signature_method']."
        )
    else:
        _logging.debug(
            f"Failed to create OAuth session with signature_method={sha_type}."
            + "Attempting fallback method(s)."
        )
    if sha_type is FALLBACK_SHA:
        raise error  # We have exhausted our options, bubble up exception

Is there an existing issue for this?

Jira Instance type

Jira Cloud (Hosted by Atlassian)

Jira instance version

9.7.1

jira-python version

3.5.2

Python Interpreter version

3.10

Which operating systems have you used?

Reproduction steps

# 1. Given a Jira client instance
jira: JIRA
# 2. When I initialize the Jira client instance and my OAuth credentials have expired and call the following function 
JIRA(options={"server": uri}, oauth=self.oauth, logging=False)

where self.oauth contains "signature_method": "RSA-SHA1",

Stack trace

2023-12-13 04:21:54.100228 [error    ] Failed to create OAuth session with signature_method=RSA-SHA1.
Attempting fallback method(s).Consider specifying the signature via oauth['signature_method']. 
Traceback (most recent call last):
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/client.py", line 3713, in _create_oauth_session
    self.myself()
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/client.py", line 3135, in myself
    return self._get_json("myself")
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/client.py", line 3853, in _get_json
    r = self._session.get(url, params=params)
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/requests/sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/resilientsession.py", line 246, in request
    elif raise_on_error(response, **processed_kwargs):
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/resilientsession.py", line 71, in raise_on_error
    raise JIRAError(
jira.exceptions.JIRAError: JiraError HTTP 401 url: ...
        text: oauth_problem=token_rejected

        response headers = {'Date': 'Wed, 13 Dec 2023 04:21:54 GMT', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Content-Length': '28', 'Connection': 'keep-alive', 'WWW-Authenticate': ...., oauth_problem="token_rejected"', 'Content-Security-Policy': 'sandbox'}
        response text = oauth_problem=token_rejected
2023-12-13 04:21:54.157748 [error    ] Failed to create OAuth session with signature_method=HMAC-SHA1.
Attempting fallback method(s).Consider specifying the signature via oauth['signature_method']. 
Traceback (most recent call last):
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/client.py", line 3713, in _create_oauth_session
    self.myself()
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/client.py", line 3135, in myself
    return self._get_json("myself")
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/client.py", line 3853, in _get_json
    r = self._session.get(url, params=params)
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/requests/sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/resilientsession.py", line 246, in request
    elif raise_on_error(response, **processed_kwargs):
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/resilientsession.py", line 71, in raise_on_error
    raise JIRAError(
jira.exceptions.JIRAError: JiraError HTTP 401 url: ...
        text: oauth_problem=token_rejected

        response headers = {'Date': 'Wed, 13 Dec 2023 04:21:54 GMT', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Content-Length': '28', 'Connection': 'keep-alive', 'WWW-Authenticate': ..., oauth_problem="token_rejected"', 'Content-Security-Policy': 'sandbox'}
        response text = oauth_problem=token_rejected
2023-12-13 04:21:54.197825 [error    ] Failed to create OAuth session with signature_method=RSA-SHA1.
Attempting fallback method(s).Consider specifying the signature via oauth['signature_method']. 
Traceback (most recent call last):
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/client.py", line 3713, in _create_oauth_session
    self.myself()
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/client.py", line 3135, in myself
    return self._get_json("myself")
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/client.py", line 3853, in _get_json
    r = self._session.get(url, params=params)
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/requests/sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/resilientsession.py", line 246, in request
    elif raise_on_error(response, **processed_kwargs):
  File "/Users/brookemiller/Library/Caches/pypoetry/virtualenvs/mothra-98Oa1KiW-py3.10/lib/python3.10/site-packages/jira/resilientsession.py", line 71, in raise_on_error
    raise JIRAError(
jira.exceptions.JIRAError: JiraError HTTP 401 url: ...
        text: oauth_problem=token_rejected

        response headers = {'Date': 'Wed, 13 Dec 2023 04:21:54 GMT', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Content-Length': '28', 'Connection': 'keep-alive', 'WWW-Authenticate': ..., oauth_problem="token_rejected"', 'Content-Security-Policy': 'sandbox'}
        response text = oauth_problem=token_rejected

Expected behaviour

no logs or tracebacks

Additional Context

No response

adehad commented 5 months ago

Thanks for the report, if anyone wants to take this on, the fix is to replace the calls to the global logging object _logging with the self.log object, which applies the log level accordingly.