Authentication in the SDK
As described in the article about connecting to the YDB server, the client must send an authentication token with each request. The authentication token is verified by the server, and if authentication succeeds, the request is authorized and executed; otherwise, an Unauthenticated error is returned.
The YDB SDK uses an object responsible for generating such tokens. The SDK provides built-in ways to obtain such an object:
- Methods with explicit parameter passing, each of which implements one of the authentication modes.
- A method for determining the authentication mode and required parameters from environment variables.
Typically, the token generation object is created before initializing the YDB driver and passed as a parameter to its constructor. The C++ and Go SDKs additionally allow working with multiple databases and token generation objects through a single driver.
If the token generation object is not defined, the driver will not add any authentication information to requests. This approach allows successfully connecting to locally deployed YDB clusters without mandatory authentication configured. If mandatory authentication is configured, requests to the database without an authentication token will be rejected with an authentication error.
Methods for creating a token generation object
You can click the methods below to go to the source code of the relevant example in the repository. You can also check out the Authentication code recipes.
| Mode | Method |
|---|---|
| Anonymous | ydb.AnonymousCredentials() |
| Access Token | ydb.AccessTokenCredentials(token) |
| Metadata | ydb.iam.MetadataUrlCredentials() |
| Service Account Key | ydb.iam.ServiceAccountCredentials.from_file( key_file, iam_endpoint=None, iam_channel_credentials=None) |
| Static Credentials | ydb.StaticCredentials.from_user_password(user, password) |
| OAuth 2.0 token exchange | ydb.oauth2_token_exchange.Oauth2TokenExchangeCredentials(), ydb.oauth2_token_exchange.Oauth2TokenExchangeCredentials.from_file(cfg_file, iam_endpoint=None) |
| Determined by environment variables | ydb.credentials_from_env_variables() |
| Mode | Package | Method |
|---|---|---|
| Anonymous | ydb-go-sdk/v3 | ydb.WithAnonymousCredentials() |
| Access Token | ydb-go-sdk/v3 | ydb.WithAccessTokenCredentials(token) |
| Metadata | ydb-go-yc | yc.WithMetadataCredentials(ctx) |
| Service Account Key | ydb-go-yc | yc.WithServiceAccountKeyFileCredentials(key_file) |
| Static Credentials | ydb-go-sdk/v3 | ydb.WithStaticCredentials(user, password) |
| OAuth 2.0 token exchange | ydb-go-sdk/v3 | ydb.WithOauth2TokenExchangeCredentials(options...), ydb.WithOauth2TokenExchangeCredentialsFile(configFilePath) |
| Determined by environment variables | ydb-go-sdk-auth-environ | environ.WithEnvironCredentials(ctx) |
| Mode | Method |
|---|---|
| Anonymous | tech.ydb.core.auth.NopAuthProvider.INSTANCE |
| Access Token | new tech.ydb.core.auth.TokenAuthProvider(accessToken); |
| Metadata | tech.ydb.auth.iam.CloudAuthHelper.getMetadataAuthProvider(); |
| Service Account Key | tech.ydb.auth.iam.CloudAuthHelper.getServiceAccountFileAuthProvider(saKeyFile); |
| OAuth 2.0 token exchange | tech.ydb.auth.OAuth2TokenExchangeProvider.fromFile(cfgFile); |
| Determined by environment variables | tech.ydb.auth.iam.CloudAuthHelper.getAuthProviderFromEnviron(); |
| Mode | Method |
|---|---|
| Anonymous | Nothing needs to be passed for this mode. |
| Access Token | new TokenProvider(accessToken) |
| Metadata | new Ydb.Sdk.Auth.MetadataProvider() |
| Service Account Key | new Ydb.Sdk.Auth.ServiceAccountProvider(saKeyFile); |
| OAuth 2.0 token exchange | Not supported |
| Determined by environment variables | Not supported |
| Mode | Method |
|---|---|
| Anonymous | AnonymousCredentialsProvider() |
| Access Token | AccessTokenCredentialsProvider({ token }) |
| Metadata | MetadataCredentialsProvider() |
| Service Account Key | ServiceAccountCredentialsProvider.fromFile(saKeyFile) |
| Static Credentials | StaticCredentialsProvider({ username, password }, endpoint) |
| Determined by environment variables | EnvironCredentialsProvider(connectionString) |
| Mode | Method |
|---|---|
| Anonymous | ydb::AnonymousCredentials or ydb::AccessTokenCredentials::from("") |
| Access Token | ydb::AccessTokenCredentials::from("token") |
| Metadata | ydb::MetadataUrlCredentials |
| Service Account Key | ydb::ServiceAccountCredentials |
| Static Credentials | ydb::StaticCredentialsAuth |
| Determined by environment variables | ydb::FromEnvCredentials |
| Executing an external command | ydb::CommandLineCredentials (for example, for authorization using an IAM token Yandex.Cloud from a developer's computer ydb::CommandLineCredentials.from_cmd("yc iam create-token")) |
| Mode | Method |
|---|---|
| Anonymous | AnonymousAuthentication() |
| Access Token | AccessTokenAuthentication($accessToken) |
| Oauth Token | OAuthTokenAuthentication($oauthToken) |
| Metadata | MetadataAuthentication() |
| Service Account Key | JwtWithJsonAuthentication(key_id, $service_account_id, $privateKeyFile) |
| Determined by environment variables | EnvironCredentials() |
| Static Credentials | StaticAuthentication($user, $password) |
Order of determining the authentication mode and parameters from the environment
The following algorithm is executed, which is the same for all SDKs:
- If the environment variable
YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALSis set, the System Account Key authentication mode is used, and the key is loaded from the file specified in this variable. - Otherwise, if the environment variable
YDB_ANONYMOUS_CREDENTIALSis set to 1, the anonymous authentication mode is used. - Otherwise, if the environment variable
YDB_METADATA_CREDENTIALSis set to 1, the Metadata authentication mode is used. - Otherwise, if the environment variable
YDB_ACCESS_TOKEN_CREDENTIALSis set, the Access token authentication mode is used, and the value of this variable is passed to it. - Otherwise, if the environment variable
YDB_OAUTH2_KEY_FILEis set, the OAuth 2.0 token exchange authentication mode is used, and the settings are loaded from the JSON file specified in this variable. - Otherwise, the Metadata authentication mode is used.
The presence of Metadata as the last item in the mode selection algorithm allows you to deploy a working application on virtual machines and in Cloud Functions Yandex.Cloud without setting any environment variables.
Format of the settings file for the OAuth 2.0 token exchange authentication mode
Description of the fields of the JSON settings file for the OAuth 2.0 token exchange authentication method. The set of fields depends on the type of the source token, JWT or FIXED.
In the table below, creds_json denotes JSON with parameters for the source token exchanged for an access token.
Fields not described in this table are ignored.
| Field | Type | Description | Default value / optionality |
|---|---|---|---|
grant-type |
string | Grant type | urn:ietf:params:oauth:grant-type:token-exchange |
res |
string | list of strings | Resource | optional |
aud |
string | list of strings | Audience option for the token exchange request | optional |
scope |
string | list of strings | Scope | optional |
requested-token-type |
string | Type of the token to be received | urn:ietf:params:oauth:token-type:access_token |
subject-credentials |
creds_json | Subject credentials | optional |
actor-credentials |
creds_json | Actor credentials | optional |
token-endpoint |
string | Token endpoint. For YDB CLI, it is overridden by the --iam-endpoint option. |
optional |
Description of creds_json fields (JWT) |
|||
type |
string | Token source type. Set the constant JWT. |
|
alg |
string | JWT signing algorithm. The following algorithms are supported: ES256, ES384, ES512, HS256, HS384, HS512, PS256, PS384, PS512, RS256, RS384, RS512 | |
private-key |
string | (Private) key in PEM format (for the ES*, PS*, and RS* algorithms) or Base64 (for the HS* algorithms) for signing |
|
kid |
string | Standard JWT field kid (key id) |
optional |
iss |
string | Standard JWT field iss (issuer) |
optional |
sub |
string | Standard JWT field sub (subject) |
optional |
aud |
string | Standard JWT field aud (audience) |
optional |
jti |
string | Standard JWT field jti (JWT id) |
optional |
ttl |
string | JWT token lifetime | 1h |
Description of creds_json fields (FIXED) |
|||
type |
string | Token source type. Set the constant FIXED. |
|
token |
string | Token value | |
token-type |
string | Token type value. This value will be passed to the subject_token_type/actor_token_type parameter in the token exchange request. |
Example
Example of JWT token exchange
{
"subject-credentials": {
"type": "JWT",
"alg": "RS256",
"private-key": "-----BEGIN RSA PRIVATE KEY-----\n...-----END RSA PRIVATE KEY-----\n",
"kid": "my_key_id",
"sub": "account_id"
}
}
Features of YDB Python SDK v2 (legacy version)
Warning
The behavior of YDB Python SDK v2 (legacy version) differs from the one described above.
-
How the
construct_credentials_from_environ()function of YDB Python SDK v2 works:- If the environment variable
USE_METADATA_CREDENTIALSis set to 1, the Metadata authentication mode is used. - Otherwise, if the environment variable
YDB_TOKENis set, the Access Token authentication mode is used, and the value of this variable is passed to it. - Otherwise, if the environment variable
SA_KEY_FILEis set, the System Account Key authentication mode is used, and the key is loaded from the file whose name is specified in this variable. - Otherwise, no authentication information will be added to requests.
- If the environment variable
-
If no object responsible for token generation is passed during driver initialization, the general procedure for reading environment variable values is applied.