Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AGENT-12890] Handle unexpected values for expires_in in OAuth Access response #19371

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions datadog_checks_base/changelog.d/19371.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle unexpected values for `expires_in` in OAuth Access response
8 changes: 7 additions & 1 deletion datadog_checks_base/datadog_checks/base/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,13 @@ def read(self, **request):

# https://www.rfc-editor.org/rfc/rfc6749#section-4.4.3
self._token = response['access_token']
self._expiration = get_timestamp() + response['expires_in']
self._expiration = get_timestamp()
try:
# According to https://www.rfc-editor.org/rfc/rfc6749#section-5.1, the `expires_in` field is optional
token_expiration = response.get('expires_in', 0)
self._expiration += token_expiration
except TypeError:
LOGGER.warning('OAuth2 included an `expires_in` value of unexpected type %s.', type(token_expiration))

return self._token

Expand Down
12 changes: 10 additions & 2 deletions datadog_checks_base/tests/base/utils/http/test_authtoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,15 @@ def fetch_token(self, *args, **kwargs):
with pytest.raises(Exception, match='OAuth2 client credentials grant error: unauthorized_client'):
http.get('https://www.google.com')

def test_success(self):
@pytest.mark.parametrize(
'token_response',
[
pytest.param({'access_token': 'foo', 'expires_in': 9000}, id='With expires_in'),
pytest.param({'access_token': 'foo'}, id='Without expires_in'),
pytest.param({'access_token': 'foo', 'expires_in': 'two minutes'}, id='With string expires_in'),
],
)
def test_success(self, token_response):
instance = {
'auth_token': {
'reader': {'type': 'oauth', 'url': 'foo', 'client_id': 'bar', 'client_secret': 'baz'},
Expand All @@ -487,7 +495,7 @@ def __init__(self, *args, **kwargs):
pass

def fetch_token(self, *args, **kwargs):
return {'access_token': 'foo', 'expires_in': 9000}
return token_response

with mock.patch('requests.get') as get, mock.patch('oauthlib.oauth2.BackendApplicationClient'), mock.patch(
'requests_oauthlib.OAuth2Session', side_effect=MockOAuth2Session
Expand Down
Loading