Skip to content

Commit aef5f66

Browse files
Add root_certificates option for ydb.DriverConfig (#525)
* Add root_certificates option for ydb.DriverConfig in example --------- Co-authored-by: Oleg Ovcharuk <[email protected]>
1 parent 10284db commit aef5f66

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

examples/static-credentials/example.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,42 @@
22

33

44
def test_driver_works(driver: ydb.Driver):
5+
"""Tests the functionality of the YDB driver.
6+
7+
Waits for the driver to become ready and executes a simple SQL query to verify that the driver works as expected.
8+
9+
Args:
10+
driver (ydb.Driver): The YDB driver instance to test.
11+
12+
Raises:
13+
AssertionError: If the SQL query does not return the expected result.
14+
"""
515
driver.wait(5)
616
pool = ydb.QuerySessionPool(driver)
717
result = pool.execute_with_retries("SELECT 1 as cnt")
818
assert result[0].rows[0].cnt == 1
919

1020

11-
def auth_with_static_credentials(endpoint: str, database: str, user: str, password: str):
21+
def auth_with_static_credentials(endpoint: str, database: str, user: str, password: str, ca_path: str):
22+
"""Authenticate using static credentials.
23+
24+
Args:
25+
endpoint (str): Accepts a string in the format `grpcs://<node-fqdn>:2136` or `grpcs://<node-ip>:2136`.
26+
database (str): Accepts a string, the database name in the format `/Root/<database-name>`.
27+
user (str): Username.
28+
password (str): User password.
29+
ca_path (str): Path to CA cert
30+
31+
Notes:
32+
The argument `root_certificates` of the function `ydb.DriverConfig` takes the content of the cluster's root certificate
33+
for connecting to cluster nodes via TLS.
34+
Note that the VM from which you are connecting must be in the cluster's domain for which the CA certificate is issued.
35+
"""
1236
driver_config = ydb.DriverConfig(
1337
endpoint=endpoint,
1438
database=database,
1539
credentials=ydb.StaticCredentials.from_user_password(user, password),
40+
root_certificates=ydb.load_ydb_root_certificate(ca_path),
1641
)
1742

1843
with ydb.Driver(driver_config=driver_config) as driver:

ydb/auth_helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# -*- coding: utf-8 -*-
22
import os
3+
from typing import Optional
34

45

56
def read_bytes(f):
67
with open(f, "rb") as fr:
78
return fr.read()
89

910

10-
def load_ydb_root_certificate():
11-
path = os.getenv("YDB_SSL_ROOT_CERTIFICATES_FILE", None)
11+
def load_ydb_root_certificate(path: Optional[str] = None):
12+
path = path if path is not None else os.getenv("YDB_SSL_ROOT_CERTIFICATES_FILE", None)
1213
if path is not None and os.path.exists(path):
1314
return read_bytes(path)
1415
return None

0 commit comments

Comments
 (0)