Skip to content

Ability to pass credentials by string #11

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 1 commit into from
Nov 15, 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
17 changes: 12 additions & 5 deletions ydb_dbapi/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .errors import NotSupportedError
from .utils import handle_ydb_errors
from .utils import maybe_get_current_trace_id
from .utils import prepare_credentials


class IsolationLevel(str, Enum):
Expand Down Expand Up @@ -69,13 +70,15 @@ def __init__(
port: str = "",
database: str = "",
ydb_table_path_prefix: str = "",
credentials: ydb.AbstractCredentials | None = None,
protocol: str | None = None,
credentials: ydb.Credentials | dict | str | None = None,
ydb_session_pool: SessionPool | AsyncSessionPool | None = None,
**kwargs: dict,
) -> None:
self.endpoint = f"grpc://{host}:{port}"
protocol = protocol if protocol else "grpc"
self.endpoint = f"{protocol}://{host}:{port}"
self.credentials = prepare_credentials(credentials)
self.database = database
self.credentials = credentials
self.table_path_prefix = ydb_table_path_prefix

self.connection_kwargs: dict = kwargs
Expand Down Expand Up @@ -170,7 +173,8 @@ def __init__(
port: str = "",
database: str = "",
ydb_table_path_prefix: str = "",
credentials: ydb.AbstractCredentials | None = None,
protocol: str | None = None,
credentials: ydb.Credentials | None = None,
ydb_session_pool: SessionPool | AsyncSessionPool | None = None,
**kwargs: dict,
) -> None:
Expand All @@ -179,6 +183,7 @@ def __init__(
port=port,
database=database,
ydb_table_path_prefix=ydb_table_path_prefix,
protocol=protocol,
credentials=credentials,
ydb_session_pool=ydb_session_pool,
**kwargs,
Expand Down Expand Up @@ -333,7 +338,8 @@ def __init__(
port: str = "",
database: str = "",
ydb_table_path_prefix: str = "",
credentials: ydb.AbstractCredentials | None = None,
protocol: str | None = None,
credentials: ydb.Credentials | None = None,
ydb_session_pool: SessionPool | AsyncSessionPool | None = None,
**kwargs: dict,
) -> None:
Expand All @@ -342,6 +348,7 @@ def __init__(
port=port,
database=database,
ydb_table_path_prefix=ydb_table_path_prefix,
protocol=protocol,
credentials=credentials,
ydb_session_pool=ydb_session_pool,
**kwargs,
Expand Down
28 changes: 28 additions & 0 deletions ydb_dbapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import functools
import importlib.util
import json
from enum import Enum
from inspect import iscoroutinefunction
from typing import Any
Expand Down Expand Up @@ -117,3 +118,30 @@ def maybe_get_current_trace_id() -> str | None:

# Return None if OpenTelemetry is not available or trace ID is invalid
return None


def prepare_credentials(
credentials: ydb.Credentials | dict | str | None,
) -> ydb.Credentials | None:
if not credentials:
return None

if isinstance(credentials, ydb.Credentials):
return credentials

if isinstance(credentials, str):
credentials = json.loads(credentials)

if isinstance(credentials, dict):
credentials = credentials or {}
token = credentials.get("token")
if token:
return ydb.AccessTokenCredentials(token)

service_account_json = credentials.get("service_account_json")
if service_account_json:
return ydb.iam.ServiceAccountCredentials.from_content(
json.dumps(service_account_json)
)

return ydb.AnonymousCredentials()
Loading