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

Fix aio auth credentials #540

Merged
merged 1 commit into from
Dec 23, 2024
Merged
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
3 changes: 2 additions & 1 deletion tests/aio/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ async def test_yandex_service_account_credentials():
tests.auth.test_credentials.PRIVATE_KEY,
server.get_endpoint(),
)
t = (await credentials.auth_metadata())[0][1]
t = await credentials.get_auth_token()
assert t == "test_token"
assert credentials.get_expire_time() <= 42

server.stop()


Expand Down
5 changes: 4 additions & 1 deletion ydb/_topic_reader/topic_reader_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,10 @@ async def _read_messages_loop(self):
async def _update_token_loop(self):
while True:
await asyncio.sleep(self._update_token_interval)
await self._update_token(token=self._get_token_function())
token = self._get_token_function()
if asyncio.iscoroutine(token):
token = await token
await self._update_token(token=token)

async def _update_token(self, token: str):
await self._update_token_event.wait()
Expand Down
5 changes: 4 additions & 1 deletion ydb/_topic_writer/topic_writer_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,10 @@ def write(self, messages: List[InternalMessage]):
async def _update_token_loop(self):
while True:
await asyncio.sleep(self._update_token_interval)
await self._update_token(token=self._get_token_function())
token = self._get_token_function()
if asyncio.iscoroutine(token):
token = await token
await self._update_token(token=token)

async def _update_token(self, token: str):
await self._update_token_event.wait()
Expand Down
14 changes: 11 additions & 3 deletions ydb/aio/credentials.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import time

import abc
import asyncio
import logging
from ydb import issues, credentials
import time

from ydb import credentials
from ydb import issues

logger = logging.getLogger(__name__)
YDB_AUTH_TICKET_HEADER = "x-ydb-auth-ticket"


class _OneToManyValue(object):
Expand Down Expand Up @@ -64,6 +66,12 @@ def __init__(self):
async def _make_token_request(self):
pass

async def get_auth_token(self) -> str:
for header, token in await self.auth_metadata():
if header == YDB_AUTH_TICKET_HEADER:
return token
return ""

async def _refresh(self):
current_time = time.time()
self._log_refresh_start(current_time)
Expand Down
Loading