Skip to content

Commit e3a2f98

Browse files
committed
new way to create static credentials
1 parent f24ae46 commit e3a2f98

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

examples/static-credentials/example.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ def auth_with_static_credentials_new(endpoint: str, database: str, user: str, pa
4444
test_driver_works(driver)
4545

4646

47+
def auth_with_static_credentials_new2(endpoint: str, database: str, user: str, password: str):
48+
driver_config = ydb.DriverConfig(
49+
endpoint=endpoint,
50+
database=database,
51+
credentials=ydb.StaticCredentials.from_user_password(user, password),
52+
)
53+
54+
with ydb.Driver(driver_config=driver_config) as driver:
55+
test_driver_works(driver)
56+
57+
4758
def auth_with_user_password_credentials(endpoint: str, database: str, user: str, password: str):
4859
driver_config = ydb.DriverConfig(
4960
endpoint=endpoint,
@@ -63,4 +74,5 @@ def auth_with_user_password_credentials(endpoint: str, database: str, user: str,
6374
def run(endpoint: str, database: str, user: str, password: str):
6475
auth_with_static_credentials_old(endpoint, database, user, password)
6576
auth_with_static_credentials_new(endpoint, database, user, password)
77+
auth_with_static_credentials_new2(endpoint, database, user, password)
6678
auth_with_user_password_credentials(endpoint, database, user, password)

ydb/credentials.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,20 @@ def __init__(self, driver_config, user, password="", tracer=None):
188188

189189
from .driver import DriverConfig
190190

191-
self.driver_config = DriverConfig(
192-
endpoint=driver_config.endpoint,
193-
database=driver_config.database,
194-
root_certificates=driver_config.root_certificates,
195-
)
191+
if driver_config is not None:
192+
self.driver_config = DriverConfig(
193+
endpoint=driver_config.endpoint,
194+
database=driver_config.database,
195+
root_certificates=driver_config.root_certificates,
196+
)
196197
self.user = user
197198
self.password = password
198199
self.request_timeout = 10
199200

201+
@classmethod
202+
def from_user_password(cls, user: str, password: str, tracer=None):
203+
return cls(None, user, password, tracer)
204+
200205
def _make_token_request(self):
201206
conn = connection.Connection.ready_factory(self.driver_config.endpoint, self.driver_config)
202207
assert conn is not None, "Failed to establish connection in to %s" % self.driver_config.endpoint
@@ -212,6 +217,15 @@ def _make_token_request(self):
212217
conn.close()
213218
return {"expires_in": 30 * 60, "access_token": result.token}
214219

220+
def _update_driver_config(self, driver_config):
221+
from .driver import DriverConfig
222+
223+
self.driver_config = DriverConfig(
224+
endpoint=driver_config.endpoint,
225+
database=driver_config.database,
226+
root_certificates=driver_config.root_certificates,
227+
)
228+
215229

216230
class UserPasswordCredentials(AbstractExpiringTokenCredentials):
217231
def __init__(self, user, password, endpoint, database, root_certificates=None, tracer=None):

ydb/driver.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,16 @@ def get_config(
226226
driver_config = config_class.default_from_endpoint_and_database(
227227
endpoint, database, root_certificates, credentials, **kwargs
228228
)
229-
return driver_config
229+
else:
230+
kwargs["endpoint"] = endpoint
231+
kwargs["database"] = database
232+
kwargs["root_certificates"] = root_certificates
233+
kwargs["credentials"] = credentials
230234

231-
kwargs["endpoint"] = endpoint
232-
kwargs["database"] = database
233-
kwargs["root_certificates"] = root_certificates
234-
kwargs["credentials"] = credentials
235+
driver_config._update_attrs_by_kwargs(**kwargs)
235236

236-
driver_config._update_attrs_by_kwargs(**kwargs)
237+
if isinstance(driver_config.credentials, credentials_impl.StaticCredentials):
238+
driver_config.credentials._update_driver_config(driver_config)
237239

238240
return driver_config
239241

0 commit comments

Comments
 (0)