Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
renew the oidc issuer rotation lock and stop writing once it is lost
DimitrisStaratzis06c34540bb0b
diff --git a/backend/AGENTS.md b/backend/AGENTS.mdindex 4b16338361..e27126526d 100644--- a/backend/AGENTS.md+++ b/backend/AGENTS.md@@ -1159,7 +1159,7 @@ The issuer URL and each consumer's `sub` format are one-way doors: customers cop- **Keys are created on the first sign, never on a JWKS fetch**, so existing orgs need no backfill.- **The second newest key signs.** The newest is published a full 24h period before it takes over, androtation keeps at most three keys. It runs under a Redis lock, so no database connection is held while- RSA keys are generated.+ RSA keys are generated; the lock is renewed while it runs, and nothing is written once it is lost.- **The JWKS is cached in Redis for 10 minutes**, because building it decrypts every key on an anonymousroute. The 24h lead makes a stale set safe. Empty sets are never cached, in Redis or over HTTP.- **Fresh keys are read on the primary.** Signing and the JWKS a rotation caches pass a `tx` todiff --git a/backend/src/services/oidc-issuer/oidc-issuer-service.ts b/backend/src/services/oidc-issuer/oidc-issuer-service.tsindex 1b962d084e..4c751553ac 100644--- a/backend/src/services/oidc-issuer/oidc-issuer-service.ts+++ b/backend/src/services/oidc-issuer/oidc-issuer-service.ts@@ -5,7 +5,6 @@ import { getConfig } from "@app/lib/config/env";import { crypto } from "@app/lib/crypto/cryptography";import { AsymmetricKeyAlgorithm, SigningAlgorithm } from "@app/lib/crypto/sign/types";import { BadRequestError, InternalServerError, NotFoundError } from "@app/lib/errors";-import { logger } from "@app/lib/logger";import { alphaNumericNanoId } from "@app/lib/nanoid";import { TKmsKeyDALFactory } from "@app/services/kms/kms-key-dal";import { TKmsServiceFactory } from "@app/services/kms/kms-service";@@ -23,6 +22,7 @@ import { TSignOidcIssuerTokenDTO } from "./oidc-issuer-types";const TOKEN_TTL_SECONDS = 5 * 60;const KEY_ROTATION_LOCK_TTL_MS = 30_000;+const KEY_ROTATION_MAX_MS = 2 * 60_000;type TOidcIssuerServiceFactoryDep = {kmsDAL: Pick<TKmsKeyDALFactory, "find" | "delete" | "transaction">;@@ -82,14 +82,31 @@ export const oidcIssuerServiceFactory = ({ kmsDAL, kmsService, keyStore }: TOidc// A Redis lock rather than an advisory lock, so no database connection is held while RSA keys are generatedconst $rotateKeys = async (orgId: string, waitForLock: boolean) => {- const lock = await keyStore+ const acquired = await keyStore.acquireLock([KeyStorePrefixes.OidcIssuerKeyRotationLock(orgId)],KEY_ROTATION_LOCK_TTL_MS,waitForLock ? { retryCount: 20, retryDelay: 500 } : { retryCount: 0 }).catch(() => null);- if (!lock) return null;+ if (!acquired) return null;++ // Generating keys can outlast the lock: renew it for up to KEY_ROTATION_MAX_MS and write nothing once it is lost+ let lock = acquired;+ let isLockLost = false;+ const deadline = Date.now() + KEY_ROTATION_MAX_MS;+ const renewal = setInterval(() => {+ if (Date.now() > deadline) isLockLost = true;+ if (isLockLost) return;+ void lock.extend?.(KEY_ROTATION_LOCK_TTL_MS).then(+ (renewed) => {+ lock = renewed;+ },+ () => {+ isLockLost = true;+ }+ );+ }, KEY_ROTATION_LOCK_TTL_MS / 3);try {// A transaction reads the primary, so a rotation another request just finished is not missed on a replica@@ -97,7 +114,7 @@ export const oidcIssuerServiceFactory = ({ kmsDAL, kmsService, keyStore }: TOidcconst { keysToCreate, keyIdsToDelete } = getKeyRotationPlan(keys);// Sequential, so createdAt orders the new keys- for (let i = 0; i < keysToCreate; i += 1) {+ for (let i = 0; i < keysToCreate && !isLockLost; i += 1) {keys.push(// eslint-disable-next-line no-await-in-loopawait kmsService.generateKmsKey({@@ -110,6 +127,7 @@ export const oidcIssuerServiceFactory = ({ kmsDAL, kmsService, keyStore }: TOidc}));}+ if (isLockLost) return null;if (keyIdsToDelete.length) await kmsDAL.delete({ $in: { id: keyIdsToDelete } });@@ -119,9 +137,8 @@ export const oidcIssuerServiceFactory = ({ kmsDAL, kmsService, keyStore }: TOidcreturn activeKeys;} finally {- await lock- .release()- .catch((err) => logger.warn(err, `OIDC issuer key rotation lock release failed [orgId=${orgId}]`));+ clearInterval(renewal);+ await lock.release();}};