Skip to content

Commit c839969

Browse files
authored
Merge pull request #11 from ydb-platform/creds_from_string
Ability to pass credentials by string
2 parents 492dab6 + 3548ac2 commit c839969

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

ydb_dbapi/connections.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .errors import NotSupportedError
2121
from .utils import handle_ydb_errors
2222
from .utils import maybe_get_current_trace_id
23+
from .utils import prepare_credentials
2324

2425

2526
class IsolationLevel(str, Enum):
@@ -69,13 +70,15 @@ def __init__(
6970
port: str = "",
7071
database: str = "",
7172
ydb_table_path_prefix: str = "",
72-
credentials: ydb.AbstractCredentials | None = None,
73+
protocol: str | None = None,
74+
credentials: ydb.Credentials | dict | str | None = None,
7375
ydb_session_pool: SessionPool | AsyncSessionPool | None = None,
7476
**kwargs: dict,
7577
) -> None:
76-
self.endpoint = f"grpc://{host}:{port}"
78+
protocol = protocol if protocol else "grpc"
79+
self.endpoint = f"{protocol}://{host}:{port}"
80+
self.credentials = prepare_credentials(credentials)
7781
self.database = database
78-
self.credentials = credentials
7982
self.table_path_prefix = ydb_table_path_prefix
8083

8184
self.connection_kwargs: dict = kwargs
@@ -170,7 +173,8 @@ def __init__(
170173
port: str = "",
171174
database: str = "",
172175
ydb_table_path_prefix: str = "",
173-
credentials: ydb.AbstractCredentials | None = None,
176+
protocol: str | None = None,
177+
credentials: ydb.Credentials | None = None,
174178
ydb_session_pool: SessionPool | AsyncSessionPool | None = None,
175179
**kwargs: dict,
176180
) -> None:
@@ -179,6 +183,7 @@ def __init__(
179183
port=port,
180184
database=database,
181185
ydb_table_path_prefix=ydb_table_path_prefix,
186+
protocol=protocol,
182187
credentials=credentials,
183188
ydb_session_pool=ydb_session_pool,
184189
**kwargs,
@@ -333,7 +338,8 @@ def __init__(
333338
port: str = "",
334339
database: str = "",
335340
ydb_table_path_prefix: str = "",
336-
credentials: ydb.AbstractCredentials | None = None,
341+
protocol: str | None = None,
342+
credentials: ydb.Credentials | None = None,
337343
ydb_session_pool: SessionPool | AsyncSessionPool | None = None,
338344
**kwargs: dict,
339345
) -> None:
@@ -342,6 +348,7 @@ def __init__(
342348
port=port,
343349
database=database,
344350
ydb_table_path_prefix=ydb_table_path_prefix,
351+
protocol=protocol,
345352
credentials=credentials,
346353
ydb_session_pool=ydb_session_pool,
347354
**kwargs,

ydb_dbapi/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import functools
44
import importlib.util
5+
import json
56
from enum import Enum
67
from inspect import iscoroutinefunction
78
from typing import Any
@@ -117,3 +118,30 @@ def maybe_get_current_trace_id() -> str | None:
117118

118119
# Return None if OpenTelemetry is not available or trace ID is invalid
119120
return None
121+
122+
123+
def prepare_credentials(
124+
credentials: ydb.Credentials | dict | str | None,
125+
) -> ydb.Credentials | None:
126+
if not credentials:
127+
return None
128+
129+
if isinstance(credentials, ydb.Credentials):
130+
return credentials
131+
132+
if isinstance(credentials, str):
133+
credentials = json.loads(credentials)
134+
135+
if isinstance(credentials, dict):
136+
credentials = credentials or {}
137+
token = credentials.get("token")
138+
if token:
139+
return ydb.AccessTokenCredentials(token)
140+
141+
service_account_json = credentials.get("service_account_json")
142+
if service_account_json:
143+
return ydb.iam.ServiceAccountCredentials.from_content(
144+
json.dumps(service_account_json)
145+
)
146+
147+
return ydb.AnonymousCredentials()

0 commit comments

Comments
 (0)