Caching authentication results
During authentication, the user session receives an authentication token, which is sent with each request to the YDB cluster. Since YDB is a distributed system, requests are processed on different nodes. Upon receiving a request, each node independently verifies the authentication token and, upon successful verification, creates a user token.
The authentication token and the user token are different entities. The authentication token is sent by the client. The user token is created inside the node based on the verification result, contains the SID of the user and groups, and is used for authorization. It is stored only on a specific node and is not sent to the client.
The node stores the user token in the cache. The record key includes the authentication token and the verification context, such as the database, attributes, and requested access rights. A subsequent request uses the cached record only if the key matches.
The user token lifetime and other caching aspects are configured in the auth_config section of the YDB configuration.
Cache record lifetime
A record with a user token is removed from the cache if one of the following conditions is met:
- The record has not been used for
auth_config.life_time. Only a request that matches the cache key (authentication token and verification context) updates the last use time of the record. - The record has expired. For login and password tokens and external identity provider tokens, the record lifetime is determined by the authentication token lifetime. For requests signed with an access key,
auth_config.as_signature_expire_timeis used; for other types,auth_config.expire_timeis used.
An error is considered retryable if the verification can be repeated without changing the request. The type of error is determined by the corresponding authentication subsystem.
Warning
On a retryable update error, the node continues to use the stored user token. Therefore, deleting an account or changing the user's membership in groups may take effect on a specific node only after a successful update or removal of the record from the cache.
ACL are not cached in the user token, so ACL changes are applied at the next permission check.
Updating user tokens
For refreshable authentication tokens, the node periodically re-validates and replaces the user's token in the cache. Depending on the authentication method, validation is performed locally or requires contacting an external system. After successful validation, the user's SID and group SIDs are updated.
The auth_config.refresh_time parameter sets the maximum interval before the next validation. The specific moment is chosen in the range from refresh_time/2 to refresh_time.
After a retryable error, retries continue until a successful update, a permanent error, or the record is removed from the cache. The intervals between attempts are set by the min_error_refresh_time and max_error_refresh_time parameters. For login/password tokens and external identity provider tokens, attempts also stop after the authentication token expires.
Possible causes of errors when validating and updating cached results include, but are not limited to:
- Network errors and timeouts when contacting the external authentication system.
- Unavailability or overload of the external authentication system.
- Unavailability of the local security state required for validation.
- Overload of the YDB node or query processing timeout.