diff options
author | waleriya <waleriya@ydb.tech> | 2023-05-24 15:32:06 +0300 |
---|---|---|
committer | waleriya <waleriya@ydb.tech> | 2023-05-24 15:32:06 +0300 |
commit | 2b9c07dca3d2ea84dcdf7f07f5b3d765bb380f6c (patch) | |
tree | dd40fd32d26f6c76ba98a159e97a0cf464f52448 | |
parent | 62c6e4f8386b134ad3504aa1b3c72d75077f388a (diff) | |
download | ydb-2b9c07dca3d2ea84dcdf7f07f5b3d765bb380f6c.tar.gz |
ydb docs - add recipe for token driver auth
19 files changed, 266 insertions, 33 deletions
diff --git a/ydb/docs/_includes/python/async/auth-access-token.md b/ydb/docs/_includes/python/async/auth-access-token.md new file mode 100644 index 0000000000..22c7f3ba03 --- /dev/null +++ b/ydb/docs/_includes/python/async/auth-access-token.md @@ -0,0 +1,16 @@ +```python +import os +import ydb +import asyncio + +async def ydb_init(): + async with ydb.aio.Driver( + endpoint=os.environ["YDB_ENDPOINT"], + database=os.environ["YDB_DATABASE"], + credentials=ydb.credentials.AccessTokenCredentials(os.environ["YDB_TOKEN"]), + ) as driver: + await driver.wait() + ... + +asyncio.run(ydb_init()) +``` diff --git a/ydb/docs/_includes/python/async/auth-anonymous.md b/ydb/docs/_includes/python/async/auth-anonymous.md new file mode 100644 index 0000000000..1e2b118a34 --- /dev/null +++ b/ydb/docs/_includes/python/async/auth-anonymous.md @@ -0,0 +1,16 @@ +```python +import os +import ydb +import asyncio + +async def ydb_init(): + async with ydb.aio.Driver( + endpoint=os.environ["YDB_ENDPOINT"], + database=os.environ["YDB_DATABASE"], + credentials=ydb.credentials.AnonymousCredentials(), + ) as driver: + await driver.wait() + ... + +asyncio.run(ydb_init()) +``` diff --git a/ydb/docs/_includes/python/async/auth-env.md b/ydb/docs/_includes/python/async/auth-env.md new file mode 100644 index 0000000000..9d3e9141e4 --- /dev/null +++ b/ydb/docs/_includes/python/async/auth-env.md @@ -0,0 +1,16 @@ +```python +import os +import ydb +import asyncio + +async def ydb_init(): + async with ydb.aio.Driver( + endpoint=os.environ["YDB_ENDPOINT"], + database=os.environ["YDB_DATABASE"], + credentials=ydb.credentials_from_env_variables(), + ) as driver: + await driver.wait() + ... + +asyncio.run(ydb_init()) +``` diff --git a/ydb/docs/_includes/python/async/auth-metadata.md b/ydb/docs/_includes/python/async/auth-metadata.md new file mode 100644 index 0000000000..21e89e6090 --- /dev/null +++ b/ydb/docs/_includes/python/async/auth-metadata.md @@ -0,0 +1,17 @@ +```python +import os +import ydb +import ydb.iam +import asyncio + +async def ydb_init(): + async with ydb.aio.Driver( + endpoint=os.environ["YDB_ENDPOINT"], + database=os.environ["YDB_DATABASE"], + credentials=ydb.iam.MetadataUrlCredentials(), + ) as driver: + await driver.wait() + ... + +asyncio.run(ydb_init()) +``` diff --git a/ydb/docs/_includes/python/async/auth-service-account.md b/ydb/docs/_includes/python/async/auth-service-account.md new file mode 100644 index 0000000000..6c814bdbf3 --- /dev/null +++ b/ydb/docs/_includes/python/async/auth-service-account.md @@ -0,0 +1,19 @@ +```python +import os +import asyncio +import ydb +import ydb.iam + +async def ydb_init(): + async with ydb.aio.Driver( + endpoint=os.environ["YDB_ENDPOINT"], + database=os.environ["YDB_DATABASE"], + # service account key should be in the local file, + # and SA_KEY_FILE environment variable should point to it + credentials=ydb.iam.ServiceAccountCredentials.from_file(os.environ["SA_KEY_FILE"]), + ) as driver: + await driver.wait() + ... + +asyncio.run(ydb_init()) +``` diff --git a/ydb/docs/_includes/python/async/auth-static.md b/ydb/docs/_includes/python/async/auth-static.md new file mode 100644 index 0000000000..a21744d9ae --- /dev/null +++ b/ydb/docs/_includes/python/async/auth-static.md @@ -0,0 +1,23 @@ +```python +import os +import ydb +import asyncio + +config = ydb.DriverConfig( + endpoint=os.environ["YDB_ENDPOINT"], + database=os.environ["YDB_DATABASE"], +) + +credentials = ydb.StaticCredentials( + driver_config=config, + user=os.environ["YDB_USER"], + password=os.environ["YDB_PASSWORD"], +) + +async def ydb_init(): + async with ydb.aio.Driver(driver_config=config, credentials=credentials) as driver: + await driver.wait() + ... + +asyncio.run(ydb_init()) +``` diff --git a/ydb/docs/_includes/python/auth-access-token.md b/ydb/docs/_includes/python/auth-access-token.md new file mode 100644 index 0000000000..51840722fd --- /dev/null +++ b/ydb/docs/_includes/python/auth-access-token.md @@ -0,0 +1,11 @@ +```python +import os +import ydb + +with ydb.Driver( + connection_string=os.environ["YDB_CONNECTION_STRING"], + credentials=ydb.credentials.AccessTokenCredentials(os.environ["YDB_TOKEN"]), +) as driver: + driver.wait(timeout=5) + ... +``` diff --git a/ydb/docs/_includes/python/auth-anonymous.md b/ydb/docs/_includes/python/auth-anonymous.md new file mode 100644 index 0000000000..80cd59fe0d --- /dev/null +++ b/ydb/docs/_includes/python/auth-anonymous.md @@ -0,0 +1,11 @@ +```python +import os +import ydb + +with ydb.Driver( + connection_string=os.environ["YDB_CONNECTION_STRING"], + credentials=ydb.credentials.AnonymousCredentials(), +) as driver: + driver.wait(timeout=5) + ... +``` diff --git a/ydb/docs/_includes/python/auth-env.md b/ydb/docs/_includes/python/auth-env.md new file mode 100644 index 0000000000..94dd0945c1 --- /dev/null +++ b/ydb/docs/_includes/python/auth-env.md @@ -0,0 +1,11 @@ +```python +import os +import ydb + +with ydb.Driver( + connection_string=os.environ["YDB_CONNECTION_STRING"], + credentials=ydb.credentials_from_env_variables(), +) as driver: + driver.wait(timeout=5) + ... +``` diff --git a/ydb/docs/_includes/python/auth-metadata.md b/ydb/docs/_includes/python/auth-metadata.md new file mode 100644 index 0000000000..48c7e7d9c6 --- /dev/null +++ b/ydb/docs/_includes/python/auth-metadata.md @@ -0,0 +1,12 @@ +```python +import os +import ydb +import ydb.iam + +with ydb.Driver( + connection_string=os.environ["YDB_CONNECTION_STRING"], + credentials=ydb.iam.MetadataUrlCredentials(), +) as driver: + driver.wait(timeout=5) + ... +``` diff --git a/ydb/docs/_includes/python/auth-service-account.md b/ydb/docs/_includes/python/auth-service-account.md new file mode 100644 index 0000000000..f2f32fdcdb --- /dev/null +++ b/ydb/docs/_includes/python/auth-service-account.md @@ -0,0 +1,14 @@ +```python +import os +import ydb +import ydb.iam + +with ydb.Driver( + connection_string=os.environ["YDB_CONNECTION_STRING"], + # service account key should be in the local file, + # and SA_KEY_FILE environment variable should point to it + credentials=ydb.iam.ServiceAccountCredentials.from_file(os.environ["SA_KEY_FILE"]), +) as driver: + driver.wait(timeout=5) + ... +``` diff --git a/ydb/docs/_includes/python/auth-static.md b/ydb/docs/_includes/python/auth-static.md new file mode 100644 index 0000000000..c486d9c4b5 --- /dev/null +++ b/ydb/docs/_includes/python/auth-static.md @@ -0,0 +1,19 @@ +```python +import os +import ydb + +config = ydb.DriverConfig( + endpoint=os.environ["YDB_ENDPOINT"], + database=os.environ["YDB_DATABASE"], +) + +credentials = ydb.StaticCredentials( + driver_config=config, + user=os.environ["YDB_USER"], + password=os.environ["YDB_PASSWORD"] +) + +with ydb.Driver(driver_config=config, credentials=credentials) as driver: + driver.wait(timeout=5) + ... +``` diff --git a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-access-token.md b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-access-token.md index 42948fb674..5d29a59f8b 100644 --- a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-access-token.md +++ b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-access-token.md @@ -14,7 +14,7 @@ import ( "context" "os" - + "github.com/ydb-platform/ydb-go-sdk/v3" ) @@ -44,7 +44,7 @@ "context" "database/sql" "os" - + "github.com/ydb-platform/ydb-go-sdk/v3" ) @@ -58,7 +58,7 @@ if err != nil { panic(err) } - defer nativeDriver.Close(ctx) + defer nativeDriver.Close(ctx) connector, err := ydb.Connector(nativeDriver) if err != nil { panic(err) @@ -80,7 +80,7 @@ "context" "database/sql" "os" - + _ "github.com/ydb-platform/ydb-go-sdk/v3" ) @@ -95,7 +95,7 @@ ``` {% endcut %} - + - Java @@ -106,7 +106,7 @@ GrpcTransport transport = GrpcTransport.forConnectionString(connectionString) .withAuthProvider(authProvider) .build()); - + TableClient tableClient = TableClient.newClient(transport).build(); doWork(tableClient); @@ -120,4 +120,12 @@ {% include [auth-access-token](../../../../_includes/nodejs/auth-access-token.md) %} +- Python + + {% include [auth-access-token](../../../../_includes/python/auth-access-token.md) %} + +- Python (asyncio) + + {% include [auth-access-token](../../../../_includes/python/async/auth-access-token.md) %} + {% endlist %} diff --git a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-anonymous.md b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-anonymous.md index b9bd734ca8..e7f0b41eac 100644 --- a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-anonymous.md +++ b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-anonymous.md @@ -8,14 +8,14 @@ - Go (native) - Анонимная аутентификация является аутентификацией по умолчанию. + Анонимная аутентификация является аутентификацией по умолчанию. Явным образом анонимную аутентификацию можно включить так: ```go package main import ( "context" - + "github.com/ydb-platform/ydb-go-sdk/v3" ) @@ -36,7 +36,7 @@ - Go (database/sql) - Анонимная аутентификация является аутентификацией по умолчанию. + Анонимная аутентификация является аутентификацией по умолчанию. Явным образом анонимную аутентификацию можно включить так: ```go package main @@ -45,7 +45,7 @@ "context" "database/sql" "os" - + "github.com/ydb-platform/ydb-go-sdk/v3" ) @@ -59,7 +59,7 @@ if err != nil { panic(err) } - defer nativeDriver.Close(ctx) + defer nativeDriver.Close(ctx) connector, err := ydb.Connector(nativeDriver) if err != nil { panic(err) @@ -79,7 +79,7 @@ GrpcTransport transport = GrpcTransport.forConnectionString(connectionString) .withAuthProvider(authProvider) .build()); - + TableClient tableClient = TableClient.newClient(transport).build(); doWork(tableClient); @@ -93,4 +93,12 @@ {% include [auth-anonymous](../../../../_includes/nodejs/auth-anonymous.md) %} -{% endlist %} +- Python + + {% include [auth-anonymous](../../../../_includes/python/auth-anonymous.md) %} + +- Python (asyncio) + + {% include [auth-anonymous](../../../../_includes/python/async/auth-anonymous.md) %} + +- {% endlist %} diff --git a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-env.md b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-env.md index 9de102ef9a..488518a0ef 100644 --- a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-env.md +++ b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-env.md @@ -7,7 +7,7 @@ description: "В разделе приведены примеры кода ау {% include [work in progress message](_includes/addition.md) %} -При использовании данного метода режим аутентификации и его параметры будут определены окружением, в котором запускается приложение, в [описанном здесь порядке](../auth.md#env). +При использовании данного метода режим аутентификации и его параметры будут определены окружением, в котором запускается приложение, в [описанном здесь порядке](../auth.md#env). Установив одну из следующих переменных окружения, можно управлять способом аутентификации: @@ -28,7 +28,7 @@ description: "В разделе приведены примеры кода ау import ( "context" "os" - + environ "github.com/ydb-platform/ydb-go-sdk-auth-environ" "github.com/ydb-platform/ydb-go-sdk/v3" ) @@ -57,7 +57,7 @@ description: "В разделе приведены примеры кода ау "context" "database/sql" "os" - + environ "github.com/ydb-platform/ydb-go-sdk-auth-environ" "github.com/ydb-platform/ydb-go-sdk/v3" ) @@ -72,7 +72,7 @@ description: "В разделе приведены примеры кода ау if err != nil { panic(err) } - defer nativeDriver.Close(ctx) + defer nativeDriver.Close(ctx) connector, err := ydb.Connector(nativeDriver) if err != nil { panic(err) @@ -88,11 +88,11 @@ description: "В разделе приведены примеры кода ау ```java public void work(String connectionString) { AuthProvider authProvider = CloudAuthHelper.getAuthProviderFromEnviron(); - + GrpcTransport transport = GrpcTransport.forConnectionString(connectionString) .withAuthProvider(authProvider) .build()); - + TableClient tableClient = TableClient.newClient(transport).build(); doWork(tableClient); @@ -106,4 +106,12 @@ description: "В разделе приведены примеры кода ау {% include [auth-env](../../../../_includes/nodejs/auth-env.md) %} +- Python + + {% include [auth-env](../../../../_includes/python/auth-env.md) %} + +- Python (asyncio) + + {% include [auth-env](../../../../_includes/python/async/auth-env.md) %} + {% endlist %} diff --git a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-metadata.md b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-metadata.md index 34c247699c..da140ee762 100644 --- a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-metadata.md +++ b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-metadata.md @@ -30,7 +30,7 @@ if err != nil { panic(err) } - defer db.Close(ctx) + defer db.Close(ctx) ... } ``` @@ -60,7 +60,7 @@ if err != nil { panic(err) } - defer nativeDriver.Close(ctx) + defer nativeDriver.Close(ctx) connector, err := ydb.Connector(nativeDriver) if err != nil { panic(err) @@ -75,12 +75,12 @@ ```java public void work(String connectionString) { - AuthProvider authProvider = CloudAuthHelper.getMetadataAuthProvider(); + AuthProvider authProvider = CloudAuthHelper.getMetadataAuthProvider(); GrpcTransport transport = GrpcTransport.forConnectionString(connectionString) .withAuthProvider(authProvider) .build()); - + TableClient tableClient = TableClient.newClient(transport).build(); doWork(tableClient); @@ -94,4 +94,12 @@ {% include [auth-metadata](../../../../_includes/nodejs/auth-metadata.md) %} +- Python + + {% include [auth-metadata](../../../../_includes/python/auth-metadata.md) %} + +- Python (asyncio) + + {% include [auth-metadata](../../../../_includes/python/async/auth-metadata.md) %} + {% endlist %} diff --git a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-service-account.md b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-service-account.md index aaf73f5001..a9d603e541 100644 --- a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-service-account.md +++ b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-service-account.md @@ -32,7 +32,7 @@ if err != nil { panic(err) } - defer db.Close(ctx) + defer db.Close(ctx) ... } ``` @@ -64,7 +64,7 @@ if err != nil { panic(err) } - defer nativeDriver.Close(ctx) + defer nativeDriver.Close(ctx) connector, err := ydb.Connector(nativeDriver) if err != nil { panic(err) @@ -84,7 +84,7 @@ GrpcTransport transport = GrpcTransport.forConnectionString(connectionString) .withAuthProvider(authProvider) .build()); - + TableClient tableClient = TableClient.newClient(transport).build(); doWork(tableClient); @@ -95,7 +95,7 @@ ``` - Node.js - + Загрузка данных сервисного аккаунта из файла: {% include [auth-sa-file](../../../../_includes/nodejs/auth-sa-file.md) %} @@ -104,4 +104,12 @@ {% include [auth-sa-data](../../../../_includes/nodejs/auth-sa-data.md) %} +- Python + + {% include [auth-sa-data](../../../../_includes/python/auth-service-account.md) %} + +- Python (asyncio) + + {% include [auth-sa-data](../../../../_includes/python/async/auth-service-account.md) %} + {% endlist %} diff --git a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-static.md b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-static.md index ac4c726158..c716177ae4 100644 --- a/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-static.md +++ b/ydb/docs/ru/core/reference/ydb-sdk/recipes/auth-static.md @@ -51,7 +51,7 @@ GrpcTransport transport = GrpcTransport.forConnectionString(connectionString) .withAuthProvider(authProvider) .build()); - + TableClient tableClient = TableClient.newClient(transport).build(); doWork(tableClient); @@ -65,4 +65,12 @@ {% include [auth-static](../../../../_includes/nodejs/auth-static.md) %} +- Python + + {% include [auth-static](../../../../_includes/python/auth-static.md) %} + +- Python (asyncio) + + {% include [auth-static](../../../../_includes/python/async/auth-static.md) %} + {% endlist %} diff --git a/ydb/docs/ru/core/reference/ydb-sdk/recipes/init.md b/ydb/docs/ru/core/reference/ydb-sdk/recipes/init.md index cdcd363b6f..85d13a48e3 100644 --- a/ydb/docs/ru/core/reference/ydb-sdk/recipes/init.md +++ b/ydb/docs/ru/core/reference/ydb-sdk/recipes/init.md @@ -103,8 +103,8 @@ description: "В статье приведены примеры кода под import ydb with ydb.Driver(connection_string="grpc://localhost:2136?database=/local") as driver: - driver.wait(timeout=5, fail_fast=True) - ... + driver.wait(timeout=5) + ... ``` - Python (asyncio) @@ -114,9 +114,9 @@ description: "В статье приведены примеры кода под import asyncio async def ydb_init(): - async with ydb.aio.Driver(endpoint="grpc://localhost:2136", database="/local") as driver: - await driver.wait(fail_fast=True) - ... + async with ydb.aio.Driver(endpoint="grpc://localhost:2136", database="/local") as driver: + await driver.wait() + ... asyncio.run(ydb_init()) ``` |