diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a8302ae..89b1dcb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* Fix automatic refresh of short-lived login tokens by using their JWT expiration time. + ## 3.35.0 ## * Include streaming queries in scheme directory listings and path descriptions * Add timeout to the token exchange request so a stalled token endpoint cannot hang credential refresh diff --git a/tests/aio/test_credentials.py b/tests/aio/test_credentials.py index e73f3522..956ce250 100644 --- a/tests/aio/test_credentials.py +++ b/tests/aio/test_credentials.py @@ -29,6 +29,41 @@ def get_expire_time(self): return self._expires_in - time.time() +@pytest.mark.asyncio +async def test_short_lived_token_is_cached_and_refreshed(): + credentials = ServiceAccountCredentialsForTest( + tests.auth.test_credentials.SERVICE_ACCOUNT_ID, + tests.auth.test_credentials.ACCESS_KEY_ID, + tests.auth.test_credentials.PRIVATE_KEY, + "localhost:0", + ) + credentials._make_token_request = AsyncMock( + side_effect=[ + {"access_token": "token_v1", "expires_in": 15}, + {"access_token": "token_v2", "expires_in": 15}, + {"access_token": "token_v3", "expires_in": 15}, + ] + ) + background_calls = [] + credentials._tp.submit = background_calls.append + + with patch("time.time", return_value=1000) as clock: + assert await credentials.get_auth_token() == "token_v1" + assert await credentials.get_auth_token() == "token_v1" + assert credentials._make_token_request.call_count == 1 + + clock.return_value = 1002 + assert await credentials.get_auth_token() == "token_v1" + assert len(background_calls) == 1 + await background_calls.pop()() + assert await credentials.get_auth_token() == "token_v2" + assert credentials._make_token_request.call_count == 2 + + clock.return_value = 1018 + assert await credentials.get_auth_token() == "token_v3" + assert credentials._make_token_request.call_count == 3 + + @pytest.mark.asyncio async def test_yandex_service_account_credentials(): server = tests.auth.test_credentials.IamTokenServiceTestServer() diff --git a/tests/auth/test_static_credentials.py b/tests/auth/test_static_credentials.py index f88878ee..266608e4 100644 --- a/tests/auth/test_static_credentials.py +++ b/tests/auth/test_static_credentials.py @@ -1,3 +1,6 @@ +import base64 +import json + import pytest import ydb from unittest.mock import patch, MagicMock @@ -6,6 +9,67 @@ PASSWORD = "1234" +def make_login_token(expires_at): + payload = base64.urlsafe_b64encode(json.dumps({"exp": expires_at}).encode()).decode().rstrip("=") + return "eyJhbGciOiJSUzI1NiJ9.{}.signature".format(payload) + + +@pytest.mark.parametrize("lifetime", [15, 60, 12 * 60 * 60]) +def test_static_credentials_use_jwt_expiration(lifetime): + credentials = ydb.StaticCredentials(ydb.DriverConfig("localhost:2135", "/local"), USERNAME, PASSWORD) + token = make_login_token(1000 + lifetime) + conn = MagicMock(return_value=MagicMock(token=token)) + + with patch("ydb.credentials.connection.Connection.ready_factory", return_value=conn): + with patch("ydb.credentials.time.time", return_value=1000): + response = credentials._make_token_request() + + assert response == {"access_token": token, "expires_in": lifetime} + conn.close.assert_called_once_with() + + +def test_static_credentials_refresh_short_lived_jwt(): + credentials = ydb.StaticCredentials(ydb.DriverConfig("localhost:2135", "/local"), USERNAME, PASSWORD) + token1 = make_login_token(1015) + token2 = make_login_token(1017) + conn = MagicMock(side_effect=[MagicMock(token=token1), MagicMock(token=token2)]) + background_calls = [] + credentials._tp.submit = background_calls.append + + with patch("ydb.credentials.connection.Connection.ready_factory", return_value=conn): + with patch("ydb.credentials.time.time", return_value=1000) as clock: + assert credentials.get_auth_token() == token1 + assert credentials.get_auth_token() == token1 + assert conn.call_count == 1 + + clock.return_value = 1002 + assert credentials.get_auth_token() == token1 + assert len(background_calls) == 1 + background_calls.pop()() + assert credentials.get_auth_token() == token2 + assert conn.call_count == 2 + + clock.return_value = 1015 + assert credentials.get_auth_token() == token2 + assert conn.call_count == 2 + + +def test_static_credentials_refresh_expired_jwt_synchronously(): + credentials = ydb.StaticCredentials(ydb.DriverConfig("localhost:2135", "/local"), USERNAME, PASSWORD) + token1 = make_login_token(1015) + token2 = make_login_token(1031) + conn = MagicMock(side_effect=[MagicMock(token=token1), MagicMock(token=token2)]) + credentials._tp.submit = MagicMock() + + with patch("ydb.credentials.connection.Connection.ready_factory", return_value=conn): + with patch("ydb.credentials.time.time", return_value=1000) as clock: + assert credentials.get_auth_token() == token1 + clock.return_value = 1016 + assert credentials.get_auth_token() == token2 + assert conn.call_count == 2 + credentials._tp.submit.assert_not_called() + + def check_driver_works(driver): driver.wait(timeout=15) pool = ydb.QuerySessionPool(driver) diff --git a/ydb/credentials.py b/ydb/credentials.py index 28ea8b52..a37bcf72 100644 --- a/ydb/credentials.py +++ b/ydb/credentials.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- import abc +import base64 +import json import typing from . import tracing, issues, connection @@ -101,7 +103,8 @@ def _should_refresh(self): def _update_token_info(self, token_response, current_time): self._refresh_in = current_time + min(self._hour / 2, token_response["expires_in"] / 10) - self._expires_in = current_time + token_response["expires_in"] - self._time_shift_protection_seconds + time_shift_protection = min(self._time_shift_protection_seconds, token_response["expires_in"] / 10) + self._expires_in = current_time + token_response["expires_in"] - time_shift_protection self._cached_token = token_response["access_token"] def _refresh_token(self, should_raise=False): @@ -190,7 +193,9 @@ def _make_token_request(self): ) finally: conn.close() - return {"expires_in": 30 * 60, "access_token": result.token} + payload = result.token.split(".")[1] + claims = json.loads(base64.urlsafe_b64decode(payload + "=" * (-len(payload) % 4))) + return {"expires_in": max(0, claims["exp"] - time.time()), "access_token": result.token} def _update_driver_config(self, driver_config): from .driver import DriverConfig