-
Notifications
You must be signed in to change notification settings - Fork 40
Prevent race condition during JWT obtaining #329
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
753d4bf
Prevent race condition during JWT obtaining
IgorChvyrov-sm a9e313d
Testcov for an async Configuration
IgorChvyrov-sm 8674b27
Testcov for async ApiClientAdapter
IgorChvyrov-sm eaf4dfa
Added lock explanation comment
IgorChvyrov-sm 4b48625
Moved noqa directive for ruff
IgorChvyrov-sm 928bbb5
Run black
IgorChvyrov-sm f427c46
Merge branch 'main' into fix_extra_refresh_token_requests
IgorChvyrov-sm d81b16a
Apply formatting
IgorChvyrov-sm e0ed82f
Post merge fixes
IgorChvyrov-sm 353c138
Formatting
IgorChvyrov-sm 4f9ce11
Ruff fixes
IgorChvyrov-sm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| import asyncio | ||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| from conductor.asyncio_client.adapters import ApiClient | ||
| from conductor.asyncio_client.configuration.configuration import Configuration | ||
| from conductor.asyncio_client.http.rest import RESTResponse | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def api_client(): | ||
| configuration = Configuration( | ||
| server_url="http://localhost:8080/api", | ||
| auth_key="test_key", | ||
| auth_secret="test_secret", | ||
| ) | ||
| return ApiClient(configuration) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_success_response(mocker): | ||
| response = mocker.Mock(spec=RESTResponse) | ||
| response.status = 200 | ||
| response.data = b'{"token": "test_token"}' | ||
| response.read = mocker.Mock() | ||
| return response | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_401_response(mocker): | ||
| response = mocker.Mock(spec=RESTResponse) | ||
| response.status = 401 | ||
| response.data = b'{"message":"Token cannot be null or empty","error":"INVALID_TOKEN","timestamp":1758039192168}' | ||
| response.read = mocker.AsyncMock() | ||
| return response | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_refresh_authorization_token_called_on_invalid_token( | ||
| mocker, api_client, mock_401_response, mock_success_response | ||
| ): | ||
| api_client.configuration._http_config.api_key = {} | ||
|
|
||
| api_client.rest_client = mocker.AsyncMock() | ||
| api_client.rest_client.request.side_effect = [ | ||
| mock_401_response, | ||
| mock_success_response, | ||
| ] | ||
|
|
||
| mock_refresh = mocker.patch.object( | ||
| api_client, "refresh_authorization_token", new_callable=mocker.AsyncMock | ||
| ) | ||
| mock_refresh.return_value = "new_token" | ||
|
|
||
| mock_obtain = mocker.patch.object( | ||
| api_client, "obtain_new_token", new_callable=mocker.AsyncMock | ||
| ) | ||
| mock_obtain.return_value = {"token": "new_token"} | ||
|
|
||
| await api_client.call_api( | ||
| method="GET", url="http://localhost:8080/api/test", header_params={} | ||
| ) | ||
|
|
||
| mock_refresh.assert_called_once() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_refresh_authorization_token_called_on_expired_token( | ||
| mocker, api_client, mock_401_response, mock_success_response | ||
| ): | ||
| current_time = time.time() | ||
| api_client.configuration.token_update_time = current_time - 3600 | ||
| api_client.configuration.auth_token_ttl_sec = 1800 | ||
| api_client.configuration._http_config.api_key = {"api_key": "old_token"} | ||
|
|
||
| api_client.rest_client = mocker.AsyncMock() | ||
| api_client.rest_client.request.side_effect = [ | ||
| mock_401_response, | ||
| mock_success_response, | ||
| ] | ||
|
|
||
| mock_refresh = mocker.patch.object( | ||
| api_client, "refresh_authorization_token", new_callable=mocker.AsyncMock | ||
| ) | ||
| mock_refresh.return_value = "new_token" | ||
|
|
||
| mock_obtain = mocker.patch.object( | ||
| api_client, "obtain_new_token", new_callable=mocker.AsyncMock | ||
| ) | ||
| mock_obtain.return_value = {"token": "new_token"} | ||
|
|
||
| await api_client.call_api( | ||
| method="GET", url="http://localhost:8080/api/test", header_params={} | ||
| ) | ||
|
|
||
| mock_refresh.assert_called_once() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_token_lock_prevents_concurrent_refresh( | ||
| mocker, api_client, mock_401_response, mock_success_response | ||
| ): | ||
| api_client.configuration._http_config.api_key = {} | ||
|
|
||
| refresh_calls = [] | ||
|
|
||
| async def mock_refresh(): | ||
| refresh_calls.append(time.time()) | ||
| await asyncio.sleep(0.1) | ||
| return "new_token" | ||
|
|
||
| mocker.patch.object( | ||
| api_client, "refresh_authorization_token", side_effect=mock_refresh | ||
| ) | ||
|
|
||
| mock_obtain = mocker.patch.object( | ||
| api_client, "obtain_new_token", new_callable=mocker.AsyncMock | ||
| ) | ||
| mock_obtain.return_value = {"token": "new_token"} | ||
|
|
||
| api_client.rest_client = mocker.AsyncMock() | ||
| api_client.rest_client.request.side_effect = [ | ||
| mock_401_response, | ||
| mock_success_response, | ||
| mock_401_response, | ||
| mock_success_response, | ||
| ] | ||
|
|
||
| tasks = [ | ||
| api_client.call_api( | ||
| method="GET", | ||
| url="http://localhost:8080/api/test1", | ||
| header_params={}, | ||
| ), | ||
| api_client.call_api( | ||
| method="GET", | ||
| url="http://localhost:8080/api/test2", | ||
| header_params={}, | ||
| ), | ||
| ] | ||
|
|
||
| await asyncio.gather(*tasks) | ||
|
|
||
| assert len(refresh_calls) == 1 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_no_refresh_when_token_valid_and_not_expired( | ||
| mocker, api_client, mock_success_response | ||
| ): | ||
| current_time = time.time() | ||
| api_client.configuration.token_update_time = current_time - 100 | ||
| api_client.configuration.auth_token_ttl_sec = 1800 | ||
| api_client.configuration._http_config.api_key = {"api_key": "valid_token"} | ||
|
|
||
| api_client.rest_client = mocker.AsyncMock() | ||
| api_client.rest_client.request.return_value = mock_success_response | ||
|
|
||
| mock_refresh = mocker.patch.object( | ||
| api_client, "refresh_authorization_token", new_callable=mocker.AsyncMock | ||
| ) | ||
|
|
||
| await api_client.call_api( | ||
| method="GET", url="http://localhost:8080/api/test", header_params={} | ||
| ) | ||
|
|
||
| mock_refresh.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_no_refresh_for_token_endpoint(mocker, api_client, mock_401_response): | ||
| api_client.configuration._http_config.api_key = {} | ||
|
|
||
| api_client.rest_client = mocker.AsyncMock() | ||
| api_client.rest_client.request.return_value = mock_401_response | ||
|
|
||
| mock_refresh = mocker.patch.object( | ||
| api_client, "refresh_authorization_token", new_callable=mocker.AsyncMock | ||
| ) | ||
|
|
||
| await api_client.call_api( | ||
| method="POST", url="http://localhost:8080/api/token", header_params={} | ||
| ) | ||
|
|
||
| mock_refresh.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_401_response_triggers_retry_with_new_token( | ||
| mocker, api_client, mock_401_response, mock_success_response | ||
| ): | ||
| api_client.configuration._http_config.api_key = {} | ||
|
|
||
| api_client.rest_client = mocker.AsyncMock() | ||
| api_client.rest_client.request.side_effect = [ | ||
| mock_401_response, | ||
| mock_success_response, | ||
| ] | ||
|
|
||
| mock_refresh = mocker.patch.object( | ||
| api_client, "refresh_authorization_token", new_callable=mocker.AsyncMock | ||
| ) | ||
| mock_refresh.return_value = "new_token" | ||
|
|
||
| mock_obtain = mocker.patch.object( | ||
| api_client, "obtain_new_token", new_callable=mocker.AsyncMock | ||
| ) | ||
| mock_obtain.return_value = {"token": "new_token"} | ||
|
|
||
| header_params = {} | ||
| await api_client.call_api( | ||
| method="GET", | ||
| url="http://localhost:8080/api/test", | ||
| header_params=header_params, | ||
| ) | ||
|
|
||
| assert api_client.rest_client.request.call_count == 2 | ||
|
|
||
| second_call_args = api_client.rest_client.request.call_args_list[1] | ||
| assert second_call_args[1]["headers"]["X-Authorization"] == "new_token" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import pytest | ||
|
|
||
| from conductor.asyncio_client.configuration import Configuration | ||
|
|
||
|
|
||
| def test_initialization_default(monkeypatch): | ||
| monkeypatch.setenv("CONDUCTOR_SERVER_URL", "http://localhost:8080/api") | ||
| configuration = Configuration() | ||
| assert configuration.host == "http://localhost:8080/api" | ||
|
|
||
|
|
||
| def test_initialization_with_base_url(): | ||
| configuration = Configuration(server_url="https://play.orkes.io/api") | ||
| assert configuration.host == "https://play.orkes.io/api" | ||
|
|
||
|
|
||
| def test_missed_http_config(): | ||
| configuration = Configuration() | ||
| configuration._http_config = None | ||
| with pytest.raises(AttributeError) as ctx: | ||
| _ = configuration.api_key | ||
| assert ( | ||
| f"'{Configuration.__class__.__name__}' object has no attribute 'api_key'" | ||
| in ctx.value | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.