#14114·hyperswitch

[BUG] user_auth_methods: private_config DEK is never registered with the key manager, and has no transfer API

Author: ThisIsManiCreated Sep 8, 2026Updated Sep 29, 2026
LabelsC-bugS-designA-users

Bug Description

user_authentication_methods.private_config is encrypted through the same crypto_operation path as merchant and user data, and it passes Identifier::UserAuth(<id>) to the key manager — but no key is ever created or transferred to the key manager for a UserAuth identifier. Every key-manager encrypt/decrypt for a user auth method therefore references a key that does not exist, fails, and silently falls back to application encryption.

There is also no transfer API for these keys, unlike merchants (POST /accounts/transfer) and users (POST /user/key/transfer), so existing rows cannot be migrated onto the key manager at all.

Expected Behavior

A UserAuth DEK is registered with the key manager the way merchant and user DEKs are, so that data/encrypt and data/decrypt calls carrying Identifier::UserAuth(id) resolve to a real key, and ENCRYPTION_API_FAILURES stays at zero for this path.

Actual Behavior

transfer_key_to_key_manager / create_key_in_key_manager are only ever called with Identifier::Merchant or Identifier::User:

  • crates/router/src/core/admin.rs:336 — merchant, on merchant create
  • crates/router/src/core/encryption.rs:32 — merchant, bulk (POST /accounts/transfer)
  • crates/router/src/types/domain/user.rs:1270 — user, on key-store create
  • crates/router/src/core/encryption.rs:49 — user, bulk (POST /user/key/transfer)

create_key_in_key_manager (crates/common_utils/src/keymanager.rs:386) has no call sites at all.

Meanwhile both user-auth crypto call sites do pass a per-row Identifier::UserAuth(uuid):

  • encrypt — crates/router/src/utils/user.rs:163 (construct_public_and_private_db_configs)
  • decrypt — crates/router/src/utils/user.rs:215 (decrypt_oidc_private_config)

The key they actually use is a single static hex key read from config and shared by every row:

  • config/config.example.toml:1401 — [user_auth_methods] encryption_key
  • crates/router/src/core/user.rs:2744 — decoded on create
  • crates/router/src/utils/user.rs:220 — decoded on every decrypt

So with encryption_service enabled (KeyManagerState::is_encryption_service_enabled, crates/common_utils/src/types/keymanager.rs:100), the key-manager call fails, ENCRYPTION_API_FAILURES is incremented, an error is logged, and encrypt_via_api / decrypt_via_api (crates/hyperswitch_domain_models/src/type_encryption.rs:118) fall back to application encryption with the config key.

Steps To Reproduce

  1. Build the router with the encryption_service feature and set [key_manager] enabled = true.
  2. Set [user_auth_methods] encryption_key to a valid hex DEK.
  3. Create an OpenID Connect auth method: POST /user/auth with an OpenIdConnect auth method body — this reaches create_user_authentication_method (crates/router/src/core/user.rs:2740).
  4. Watch the router logs and the ENCRYPTION_API_FAILURES metric: the data/encrypt call for Identifier::UserAuth(<uuid>) fails, logs Encryption error ... followed by Fall back to Application Encryption, and the row is written under the shared config DEK.
  5. Any subsequent SSO sign-in that reads the config (decrypt_oidc_private_config) repeats the same failed round trip on the decrypt path.

Context For The Bug

Functionally nothing is broken, because the fallback works. The costs are:

  • every SSO config read/write is a wasted key-manager round trip plus a failure metric, which is noise on the encryption-failure dashboards and makes real key-manager failures harder to spot
  • OIDC configs stay under one shared, config-supplied DEK rather than key-manager-held keys, so there is no per-record isolation and rotation means a config change plus a re-encrypt of every row
  • the Identifier::UserAuth variant and the per-row id threaded through the crypto calls imply key-manager management that does not exist, which is misleading to anyone reading this code

Two ways to fix, in increasing order of correctness:

  1. Register the existing shared DEK. On auth-method create, transfer the config key to the key manager under Identifier::UserAuth(id), and add a backfill API (POST .../key/transfer-shaped, admin-authed) for existing rows. Cheap, but keeps one key for everything, so it does not buy per-record isolation.
  2. Per-auth-method DEK. Generate a key per auth method, transfer it to the key manager, and re-encrypt private_config under it. Needs a migration that reads each row with the old config key and rewrites it, plus a dual-read window. This is the version that actually gets user-auth data onto key-manager-held keys.

Either way the transfer API is the missing piece. Labelled S-design because the choice between the two should be made before implementation starts.

Environment

Not environment-specific — this is a code-level gap on main, reproducible on any deployment built with the encryption_service feature and a key manager configured.

  1. Operating System or Linux distribution: any
  2. App version: main at be1d9bae08

Have you spent some time checking if this bug has been raised before?

  • I checked and didn't find a similar issue

Have you read the Contributing Guidelines?

Are you willing to submit a PR?

No, I don't have time to work on this right now