diff options
author | Ilya Shevchenko <id.shev@yandex.ru> | 2022-12-21 08:02:35 +0000 |
---|---|---|
committer | iliashev <iliashev@yandex-team.com> | 2022-12-21 11:02:35 +0300 |
commit | 7a097007f40b87f60b2881c070a57cfc5b7614ac (patch) | |
tree | 1ab8493c594541ace18938778d048e6bccdf891a | |
parent | 29ccbbb4cd870a385fa20b944973515b65fd3b27 (diff) | |
download | ydb-7a097007f40b87f60b2881c070a57cfc5b7614ac.tar.gz |
Add recipes for nodejs sdk auth
I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/?lang=en.
Pull Request resolved: #138
23 files changed, 186 insertions, 4 deletions
diff --git a/ydb/docs/_includes/nodejs/recipes/auth/access-token.md b/ydb/docs/_includes/nodejs/recipes/auth/access-token.md new file mode 100644 index 0000000000..b3dcf825a8 --- /dev/null +++ b/ydb/docs/_includes/nodejs/recipes/auth/access-token.md @@ -0,0 +1,15 @@ +```typescript +import { Driver, TokenAuthService } from 'ydb-sdk'; + +export async function connect(endpoint: string, database: string, accessToken: string) { + const authService = new TokenAuthService(accessToken); + const driver = new Driver({endpoint, database, authService}); + const timeout = 10000; + if (!await driver.ready(timeout)) { + console.log(`Driver has not become ready in ${timeout}ms!`); + process.exit(1); + } + console.log('Driver connected') + return driver +} +``` diff --git a/ydb/docs/_includes/nodejs/recipes/auth/anonymous.md b/ydb/docs/_includes/nodejs/recipes/auth/anonymous.md new file mode 100644 index 0000000000..2741bd3a6d --- /dev/null +++ b/ydb/docs/_includes/nodejs/recipes/auth/anonymous.md @@ -0,0 +1,15 @@ +```typescript +import { Driver, AnonymousAuthService } from 'ydb-sdk'; + +export async function connect(endpoint: string, database: string) { + const authService = new AnonymousAuthService(); + const driver = new Driver({endpoint, database, authService}); + const timeout = 10000; + if (!await driver.ready(timeout)) { + console.log(`Driver has not become ready in ${timeout}ms!`); + process.exit(1); + } + console.log('Driver connected') + return driver +} +``` diff --git a/ydb/docs/_includes/nodejs/recipes/auth/env.md b/ydb/docs/_includes/nodejs/recipes/auth/env.md new file mode 100644 index 0000000000..ef828d85eb --- /dev/null +++ b/ydb/docs/_includes/nodejs/recipes/auth/env.md @@ -0,0 +1,15 @@ +```typescript +import { Driver, getCredentialsFromEnv } from 'ydb-sdk'; + +export async function connect(endpoint: string, database: string) { + const authService = getCredentialsFromEnv(); + const driver = new Driver({endpoint, database, authService}); + const timeout = 10000; + if (!await driver.ready(timeout)) { + console.log(`Driver has not become ready in ${timeout}ms!`); + process.exit(1); + } + console.log('Driver connected') + return driver +} +``` diff --git a/ydb/docs/_includes/nodejs/recipes/auth/metadata.md b/ydb/docs/_includes/nodejs/recipes/auth/metadata.md new file mode 100644 index 0000000000..49ad89741b --- /dev/null +++ b/ydb/docs/_includes/nodejs/recipes/auth/metadata.md @@ -0,0 +1,15 @@ +```typescript +import { Driver, MetadataAuthService } from 'ydb-sdk'; + +export async function connect(endpoint: string, database: string) { + const authService = new MetadataAuthService(); + const driver = new Driver({endpoint, database, authService}); + const timeout = 10000; + if (!await driver.ready(timeout)) { + console.log(`Driver has not become ready in ${timeout}ms!`); + process.exit(1); + } + console.log('Driver connected') + return driver +} +``` diff --git a/ydb/docs/_includes/nodejs/recipes/auth/service-account-data.md b/ydb/docs/_includes/nodejs/recipes/auth/service-account-data.md new file mode 100644 index 0000000000..c9a75197ed --- /dev/null +++ b/ydb/docs/_includes/nodejs/recipes/auth/service-account-data.md @@ -0,0 +1,22 @@ +```typescript +import { Driver, IamAuthService } from 'ydb-sdk'; +import { IIamCredentials } from 'ydb-sdk/build/cjs/src/credentials'; + +export async function connect(endpoint: string, database: string) { + const saCredentials: IIamCredentials = { + serviceAccountId: 'serviceAccountId', + accessKeyId: 'accessKeyId', + privateKey: Buffer.from('-----BEGIN PRIVATE KEY-----\nyJ1yFwJq...'), + iamEndpoint: 'iam.api.cloud.yandex.net:443', + }; + const authService = new IamAuthService(saCredentials); + const driver = new Driver({endpoint, database, authService}); + const timeout = 10000; + if (!await driver.ready(timeout)) { + console.log(`Driver has not become ready in ${timeout}ms!`); + process.exit(1); + } + console.log('Driver connected') + return driver +} +``` diff --git a/ydb/docs/_includes/nodejs/recipes/auth/service-account-file.md b/ydb/docs/_includes/nodejs/recipes/auth/service-account-file.md new file mode 100644 index 0000000000..2531dd0365 --- /dev/null +++ b/ydb/docs/_includes/nodejs/recipes/auth/service-account-file.md @@ -0,0 +1,16 @@ +```typescript +import { Driver, getSACredentialsFromJson, IamAuthService } from 'ydb-sdk'; + +export async function connect(endpoint: string, database: string, serviceAccountFilename: string) { + const saCredentials = getSACredentialsFromJson(serviceAccountFilename); + const authService = new IamAuthService(saCredentials); + const driver = new Driver({endpoint, database, authService}); + const timeout = 10000; + if (!await driver.ready(timeout)) { + console.log(`Driver has not become ready in ${timeout}ms!`); + process.exit(1); + } + console.log('Driver connected') + return driver +} +``` diff --git a/ydb/docs/_includes/nodejs/recipes/auth/static.md b/ydb/docs/_includes/nodejs/recipes/auth/static.md new file mode 100644 index 0000000000..932d2d2814 --- /dev/null +++ b/ydb/docs/_includes/nodejs/recipes/auth/static.md @@ -0,0 +1,17 @@ +```typescript +import { Driver, StaticCredentialsAuthService } from 'ydb-sdk'; + +export async function connect(endpoint: string, database: string, user: string, password: string) { + const authService = new StaticCredentialsAuthService(user, password, endpoint, { + tokenExpirationTimeout: 20000, + }) + const driver = new Driver({endpoint, database, authService}); + const timeout = 10000; + if (!await driver.ready(timeout)) { + console.log(`Driver has not become ready in ${timeout}ms!`); + process.exit(1); + } + console.log('Driver connected') + return driver +} +``` diff --git a/ydb/docs/en/core/reference/ydb-sdk/_includes/auth.md b/ydb/docs/en/core/reference/ydb-sdk/_includes/auth.md index 42450eed8b..1509030b79 100644 --- a/ydb/docs/en/core/reference/ydb-sdk/_includes/auth.md +++ b/ydb/docs/en/core/reference/ydb-sdk/_includes/auth.md @@ -61,6 +61,7 @@ You can click any of the methods below to go to the source code of an example in | Access Token | [TokenAuthService( accessToken, database )](https://github.com/ydb-platform/ydb-nodejs-sdk/tree/main/examples/auth/access-token-credentials) | | Metadata | [MetadataAuthService( database )](https://github.com/ydb-platform/ydb-nodejs-sdk/tree/main/examples/auth/metadata-credentials) | | Service Account Key | [getSACredentialsFromJson( saKeyFile )](https://github.com/ydb-platform/ydb-nodejs-sdk/tree/main/examples/auth/service-account-credentials) | + | User+Password | [StaticCredentialsAuthService( user, password, endpoint )](https://github.com/ydb-platform/ydb-nodejs-sdk/tree/main/examples/auth/static-credentials) | | Determined by environment variables | [getCredentialsFromEnv( entryPoint, database, logger )](https://github.com/ydb-platform/ydb-nodejs-sdk/tree/main/examples/auth/environ) | - Rust diff --git a/ydb/docs/en/core/reference/ydb-sdk/feature-parity.md b/ydb/docs/en/core/reference/ydb-sdk/feature-parity.md index 3aa7b2e6c3..fc3e22c59d 100644 --- a/ydb/docs/en/core/reference/ydb-sdk/feature-parity.md +++ b/ydb/docs/en/core/reference/ydb-sdk/feature-parity.md @@ -1,6 +1,6 @@ # Comparison of SDK features -| Feature | C\+\+ | Python | Go | Java | NodeJS | C# | Rust | PHP | +| Feature | C\+\+ | Python | Go | Java | Node.js | C# | Rust | PHP | |:---|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:| | SSL/TLS support (system certificates) | \+ | \+ | \+ | \+ | \+ | \+ | \+ | | SSL/TLS support (custom certificates) | \+ | \+ | \+ | \+ | \+ | \- | @@ -22,7 +22,7 @@ | Load balancing across all nodes of all local DCs | \+ | \+ | \+ | ? | \- | \- | \- | | **Credentials providers** | | Anonymous (default) | \+ | \+ | \+ | \+ | \+ | ? | \+ | -| Static (user - password) | \+ | \+ | \+ | \+ | \- | \- | \- | +| Static (user - password) | \+ | \+ | \+ | \+ | \+ | \- | \- | | Token: IAM, OAuth | \+ | \+ | \+ | \+ | \+ | \+ | \+ | | Service account (Yandex.Cloud specific) | \+ | \+ | \+ | \+ | \+ | \+ | \- | | Metadata (Yandex.Cloud specific) | \+ | \+ | \+ | \+ | \+ | \+ | \+ | @@ -129,7 +129,7 @@ | \* environ | ? | ? | \+ | \+ | \+ | \- | | \* metadata | ? | ? | \+ | \+ | \+ | \+ | | \* service_account | ? | ? | \+ | \+ | \+ | \- | -| \* static (username \+ password) | ? | ? | \+ | \+ | \- | \- | +| \* static (username \+ password) | ? | ? | \+ | \+ | \+ | \- | | Basic (series) | \+ | ? | \+ | \+ | \+ | \+ | \+ | | Bulk Upsert | \+/- | ? | \+ | \+ | \+ | \- | | Containers (Struct,Variant,List,Tuple) | \- | ? | \+ | \- | \- | \+ | diff --git a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-access-token.md b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-access-token.md index 7088a14035..5c5a17a375 100644 --- a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-access-token.md +++ b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-access-token.md @@ -121,4 +121,8 @@ Below are examples of the code for authentication using a token in different {{ } ``` +- Node.js + + {% include [access-token](../../../../_includes/nodejs/recipes/auth/access-token.md) %} + {% endlist %} diff --git a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-anonymous.md b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-anonymous.md index 1900802fd5..677aa0422e 100644 --- a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-anonymous.md +++ b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-anonymous.md @@ -90,4 +90,8 @@ Below are examples of the code for anonymous authentication in different {{ ydb- } ``` +- Node.js + + {% include [anonymous](../../../../_includes/nodejs/recipes/auth/anonymous.md) %} + {% endlist %} diff --git a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-env.md b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-env.md index b9355312d3..7e0b500490 100644 --- a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-env.md +++ b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-env.md @@ -98,4 +98,8 @@ Below are examples of the code for authentication using environment variables in } ``` +- Node.js + + {% include [env](../../../../_includes/nodejs/recipes/auth/env.md) %} + {% endlist %} diff --git a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-metadata.md b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-metadata.md index 5cc4e48de8..93f16d6953 100644 --- a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-metadata.md +++ b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-metadata.md @@ -90,4 +90,8 @@ Below are examples of the code for authentication using environment variables in } ``` +- Node.js + + {% include [metadata](../../../../_includes/nodejs/recipes/auth/metadata.md) %} + {% endlist %} diff --git a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-service-account.md b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-service-account.md index 0d2525c80c..9f1f40c065 100644 --- a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-service-account.md +++ b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-service-account.md @@ -94,4 +94,16 @@ Below are examples of the code for authentication using a service account file i } ``` +- Node.js + + ### Load service account data from file + + {% include [service-account-file](../../../../_includes/nodejs/recipes/auth/service-account-file.md) %} + + ### Load service account data from other source + + When you need to load service account data from the outside, for example, from the secrets vault, this method is suitable: + + {% include [service-account-data](../../../../_includes/nodejs/recipes/auth/service-account-data.md) %} + {% endlist %} diff --git a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-static.md b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-static.md index b5a89d5407..3ba566a1fe 100644 --- a/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-static.md +++ b/ydb/docs/en/core/reference/ydb-sdk/recipes/auth-static.md @@ -61,4 +61,8 @@ Below are examples of the code for authentication based on a username and token } ``` +- Node.js + + {% include [static](../../../../_includes/nodejs/recipes/auth/static.md) %} + {% endlist %} diff --git a/ydb/docs/ru/core/reference/ydb-sdk/_includes/auth.md b/ydb/docs/ru/core/reference/ydb-sdk/_includes/auth.md index b57e607309..809c9e019d 100644 --- a/ydb/docs/ru/core/reference/ydb-sdk/_includes/auth.md +++ b/ydb/docs/ru/core/reference/ydb-sdk/_includes/auth.md @@ -61,6 +61,7 @@ Access Token | [TokenAuthService( accessToken, database )](https://github.com/ydb-platform/ydb-nodejs-sdk/tree/main/examples/auth/access-token-credentials) Metadata | [MetadataAuthService( database )](https://github.com/ydb-platform/ydb-nodejs-sdk/tree/main/examples/auth/metadata-credentials) Service Account Key | [getSACredentialsFromJson( saKeyFile )](https://github.com/ydb-platform/ydb-nodejs-sdk/tree/main/examples/auth/service-account-credentials) + User+Password | [StaticCredentialsAuthService( user, password, endpoint )](https://github.com/ydb-platform/ydb-nodejs-sdk/tree/main/examples/auth/static-credentials) Определяется по переменным окружения | [getCredentialsFromEnv( entryPoint, database, logger )](https://github.com/ydb-platform/ydb-nodejs-sdk/tree/main/examples/auth/environ) - Rust diff --git a/ydb/docs/ru/core/reference/ydb-sdk/feature-parity.md b/ydb/docs/ru/core/reference/ydb-sdk/feature-parity.md index 8213297341..321e05243e 100644 --- a/ydb/docs/ru/core/reference/ydb-sdk/feature-parity.md +++ b/ydb/docs/ru/core/reference/ydb-sdk/feature-parity.md @@ -129,7 +129,7 @@ |\* environ|?|?|\+|\+|\+||\-|| |\* metadata|?|?|\+|\+|\+||\+|| |\* service_account|?|?|\+|\+|\+||\-|| -|\* static (username \+ password)|?|?|\+|\+|\-||\-|| +|\* static (username \+ password)|?|?|\+|\+|\+||\-|| |Basic (series)|\+|?|\+|\+|\+|\+|\+|| |Bulk Upsert|\+/-|?|\+|\+|\+||\-|| |Containers (Struct,Variant,List,Tuple)|\-|?|\+|\-|\-||\+|| 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 aa6d506fa4..76eb79eb78 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 @@ -116,4 +116,8 @@ } ``` +- Node.js + + {% include [access-token](../../../../_includes/nodejs/recipes/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 df8b4a9d67..c49297d323 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 @@ -89,4 +89,8 @@ } ``` +- Node.js + + {% include [anonymous](../../../../_includes/nodejs/recipes/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 3694b7d2f2..68118e6785 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 @@ -97,4 +97,8 @@ } ``` +- Node.js + + {% include [env](../../../../_includes/nodejs/recipes/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 9c39363045..d2f03990a6 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 @@ -90,4 +90,8 @@ } ``` +- Node.js + + {% include [metadata](../../../../_includes/nodejs/recipes/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 7b00b54abf..06ce170f9a 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 @@ -94,4 +94,17 @@ } ``` +- Node.js + + ### Загрузка данных сервисного аккаунта из файла + + {% include [service-account-file](../../../../_includes/nodejs/recipes/auth/service-account-file.md) %} + + ### Загрузка данных сервисного аккаунта из стороннего источника + + В случае, когда необходимо подгрузить данные сервисного аккаунта извне, например, из хранилища секретов, подходит данный способ: + + {% include [service-account-data](../../../../_includes/nodejs/recipes/auth/service-account-data.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 e438538fd1..9124361798 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 @@ -61,5 +61,9 @@ } ``` +- Node.js + + {% include [static](../../../../_includes/nodejs/recipes/auth/static.md) %} + {% endlist %} |