Skip to content

Commit aaf60a8

Browse files
committed
add example
1 parent 78c3a5c commit aaf60a8

File tree

4 files changed

+105
-7
lines changed

4 files changed

+105
-7
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
import argparse
3+
from example import run
4+
5+
6+
if __name__ == "__main__":
7+
parser = argparse.ArgumentParser(
8+
formatter_class=argparse.RawDescriptionHelpFormatter,
9+
description="""\033[92mStatic Credentials example.\x1b[0m\n""",
10+
)
11+
parser.add_argument("-e", "--endpoint", help="Endpoint url to use", default="grpc://localhost:2136")
12+
parser.add_argument("-d", "--database", help="Name of the database to use", default="/local")
13+
parser.add_argument("-u", "--user", help="User to auth with", default="root")
14+
parser.add_argument("-p", "--password", help="Password from user to auth with", default="1234")
15+
16+
args = parser.parse_args()
17+
18+
run(
19+
endpoint=args.endpoint,
20+
database=args.database,
21+
user=args.user,
22+
password=args.password,
23+
)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import ydb
2+
3+
4+
def test_driver_works(driver: ydb.Driver):
5+
driver.wait(5)
6+
pool = ydb.QuerySessionPool(driver)
7+
pool.execute_with_retries("SELECT 1")
8+
print("everything is fine")
9+
10+
11+
def auth_with_static_credentials_old(endpoint: str, database: str, user: str, password: str):
12+
driver_config_temp = ydb.DriverConfig(
13+
endpoint=endpoint,
14+
database=database,
15+
)
16+
creds = ydb.StaticCredentials(
17+
driver_config=driver_config_temp,
18+
user=user,
19+
password=password,
20+
)
21+
22+
driver_config = ydb.DriverConfig(
23+
endpoint=endpoint,
24+
database=database,
25+
credentials=creds,
26+
)
27+
28+
with ydb.Driver(driver_config=driver_config) as driver:
29+
test_driver_works(driver)
30+
31+
32+
def auth_with_static_credentials_new(endpoint: str, database: str, user: str, password: str):
33+
driver_config = ydb.DriverConfig(
34+
endpoint,
35+
database,
36+
)
37+
creds = ydb.StaticCredentials(
38+
driver_config,
39+
user,
40+
password,
41+
)
42+
43+
with ydb.Driver(driver_config=driver_config, credentials=creds) as driver:
44+
test_driver_works(driver)
45+
46+
47+
def auth_with_user_password_credentials(endpoint: str, database: str, user: str, password: str):
48+
driver_config = ydb.DriverConfig(
49+
endpoint=endpoint,
50+
database=database,
51+
credentials=ydb.UserPasswordCredentials(
52+
user=user,
53+
password=password,
54+
endpoint=endpoint,
55+
database=database,
56+
),
57+
)
58+
59+
with ydb.Driver(driver_config=driver_config) as driver:
60+
test_driver_works(driver)
61+
62+
63+
def run(endpoint: str, database: str, user: str, password: str):
64+
auth_with_static_credentials_old(endpoint, database, user, password)
65+
auth_with_static_credentials_new(endpoint, database, user, password)
66+
auth_with_user_password_credentials(endpoint, database, user, password)

ydb/credentials.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def _make_token_request(self):
215215

216216
class UserPasswordCredentials(AbstractExpiringTokenCredentials):
217217
def __init__(self, user, password, endpoint, database, root_certificates=None, tracer=None):
218-
super(UserPasswordCredentials).__init__(tracer)
218+
super(UserPasswordCredentials, self).__init__(tracer)
219219

220220
from .driver import DriverConfig # to prevent circular dependencies
221221

@@ -224,6 +224,7 @@ def __init__(self, user, password, endpoint, database, root_certificates=None, t
224224
database=database,
225225
root_certificates=root_certificates,
226226
)
227+
227228
self.user = user
228229
self.password = password
229230
self.request_timeout = 10

ydb/driver.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# -*- coding: utf-8 -*-
2+
import grpc
3+
import logging
4+
import os
5+
from typing import Any # noqa
6+
27
from . import credentials as credentials_impl, table, scheme, pool
38
from . import tracing
4-
import os
5-
import grpc
69
from . import iam
710
from . import _utilities
811

9-
from typing import Any # noqa
12+
13+
logger = logging.getLogger(__name__)
1014

1115

1216
class RPCCompression:
@@ -190,9 +194,13 @@ def set_grpc_keep_alive_timeout(self, timeout):
190194
self.grpc_keep_alive_timeout = timeout
191195
return self
192196

193-
def _update_attrs_by_kwargs(self, kwargs: dict):
197+
def _update_attrs_by_kwargs(self, **kwargs):
194198
for key, value in kwargs.items():
195-
if getattr(self, key) is None:
199+
if value is not None:
200+
if getattr(self, key) is not None:
201+
logger.warning(
202+
f"Arg {key} was used in both DriverConfig and Driver. Value from Driver will be used."
203+
)
196204
setattr(self, key, value)
197205

198206

@@ -225,7 +233,7 @@ def get_config(
225233
kwargs["root_certificates"] = root_certificates
226234
kwargs["credentials"] = credentials
227235

228-
driver_config._update_attrs_by_kwargs(kwargs)
236+
driver_config._update_attrs_by_kwargs(**kwargs)
229237

230238
return driver_config
231239

0 commit comments

Comments
 (0)