Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

read fresh oidc issuer keys on the primary

DimitrisStaratzisa8ba94184a9b
diff --git a/backend/AGENTS.md b/backend/AGENTS.md
index 0e982c97e3..4b16338361 100644
--- a/backend/AGENTS.md
+++ b/backend/AGENTS.md
@@ -1161,8 +1161,9 @@ The issuer URL and each consumer's `sub` format are one-way doors: customers cop
rotation keeps at most three keys. It runs under a Redis lock, so no database connection is held while
RSA keys are generated.
- **The JWKS is cached in Redis for 10 minutes**, because building it decrypts every key on an anonymous
- route. The 24h lead makes a stale set safe. Empty sets are never cached, in Redis or over HTTP, and a
- rotation clears the entry.
+ route. 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` to
+ `getPublicKey` and `signWithKmsKey`, because a read replica may not have a key created moments earlier.
### Enterprise (EE) Features
diff --git a/backend/src/services/kms/kms-service.ts b/backend/src/services/kms/kms-service.ts
index 12b198deee..477fcd37c7 100644
--- a/backend/src/services/kms/kms-service.ts
+++ b/backend/src/services/kms/kms-service.ts
@@ -722,8 +722,8 @@ export const kmsServiceFactory = ({
return doc;
};
- const getPublicKey = async ({ kmsId }: TGetPublicKeyDTO) => {
- const kmsDoc = await kmsDAL.findByIdWithAssociatedKms(kmsId);
+ const getPublicKey = async ({ kmsId }: TGetPublicKeyDTO, tx?: Knex) => {
+ const kmsDoc = await kmsDAL.findByIdWithAssociatedKms(kmsId, tx);
if (!kmsDoc) {
throw new NotFoundError({ message: `KMS with ID '${kmsId}' not found` });
}
@@ -740,8 +740,8 @@ export const kmsServiceFactory = ({
return signingService(encryptionAlgorithm).getPublicKeyFromPrivateKey(kmsKey);
};
- const signWithKmsKey = async ({ kmsId }: Pick<TSignWithKmsDTO, "kmsId">) => {
- const kmsDoc = await kmsDAL.findByIdWithAssociatedKms(kmsId);
+ const signWithKmsKey = async ({ kmsId }: Pick<TSignWithKmsDTO, "kmsId">, tx?: Knex) => {
+ const kmsDoc = await kmsDAL.findByIdWithAssociatedKms(kmsId, tx);
if (!kmsDoc) {
throw new NotFoundError({ message: `KMS with ID '${kmsId}' not found` });
}
diff --git a/backend/src/services/oidc-issuer/oidc-issuer-service.ts b/backend/src/services/oidc-issuer/oidc-issuer-service.ts
index b8d4186121..1b962d084e 100644
--- a/backend/src/services/oidc-issuer/oidc-issuer-service.ts
+++ b/backend/src/services/oidc-issuer/oidc-issuer-service.ts
@@ -27,7 +27,7 @@ const KEY_ROTATION_LOCK_TTL_MS = 30_000;
type TOidcIssuerServiceFactoryDep = {
kmsDAL: Pick<TKmsKeyDALFactory, "find" | "delete" | "transaction">;
kmsService: Pick<TKmsServiceFactory, "generateKmsKey" | "getPublicKey" | "signWithKmsKey">;
- keyStore: Pick<TKeyStoreFactory, "acquireLock" | "getItem" | "setItemWithExpiry" | "deleteItem">;
+ keyStore: Pick<TKeyStoreFactory, "acquireLock" | "getItem" | "setItemWithExpiry">;
};
type TJwks = { keys: Awaited<ReturnType<typeof publicKeyToJwk>>[] };
@@ -56,6 +56,30 @@ export const oidcIssuerServiceFactory = ({ kmsDAL, kmsService, keyStore }: TOidc
return keys.filter(({ name }) => name.startsWith(OIDC_ISSUER_KEY_NAME_PREFIX));
};
+ const $buildJwks = async (keys: { id: string }[], tx?: Knex): Promise<TJwks> => {
+ const jwks = await Promise.all(
+ keys.map(({ id }) =>
+ kmsService
+ .getPublicKey({ kmsId: id }, tx)
+ .then(publicKeyToJwk)
+ .catch((err) => {
+ // A concurrent rotation can delete the oldest key, which no longer signs
+ if (err instanceof NotFoundError) return null;
+ throw err;
+ })
+ )
+ );
+
+ return { keys: jwks.filter((jwk): jwk is NonNullable<typeof jwk> => jwk !== null) };
+ };
+
+ const $cacheJwks = (orgId: string, jwks: TJwks) =>
+ keyStore.setItemWithExpiry(
+ KeyStorePrefixes.OidcIssuerJwks(orgId),
+ KeyStoreTtls.OidcIssuerJwksInSeconds,
+ JSON.stringify(jwks)
+ );
+
// A Redis lock rather than an advisory lock, so no database connection is held while RSA keys are generated
const $rotateKeys = async (orgId: string, waitForLock: boolean) => {
const lock = await keyStore
@@ -88,9 +112,12 @@ export const oidcIssuerServiceFactory = ({ kmsDAL, kmsService, keyStore }: TOidc
}
if (keyIdsToDelete.length) await kmsDAL.delete({ $in: { id: keyIdsToDelete } });
- if (keysToCreate) await keyStore.deleteItem(KeyStorePrefixes.OidcIssuerJwks(orgId));
- return keys.filter(({ id }) => !keyIdsToDelete.includes(id));
+ const activeKeys = keys.filter(({ id }) => !keyIdsToDelete.includes(id));
+ // Built on the primary, so the first fetch after a rotation cannot come from a replica missing the new keys
+ if (keysToCreate) await $cacheJwks(orgId, await kmsDAL.transaction((tx) => $buildJwks(activeKeys, tx)));
+
+ return activeKeys;
} finally {
await lock
.release()
@@ -120,10 +147,13 @@ export const oidcIssuerServiceFactory = ({ kmsDAL, kmsService, keyStore }: TOidc
const issuer = $getIssuerUrl(orgId);
const { id: kmsId } = await $getSigningKey(orgId);
- const [{ kid }, sign] = await Promise.all([
- kmsService.getPublicKey({ kmsId }).then(publicKeyToJwk),
- kmsService.signWithKmsKey({ kmsId })
- ]);
+ // Read on the primary, because this request may have just created the key
+ const [{ kid }, sign] = await kmsDAL.transaction((tx) =>
+ Promise.all([
+ kmsService.getPublicKey({ kmsId }, tx).then(publicKeyToJwk),
+ kmsService.signWithKmsKey({ kmsId }, tx)
+ ])
+ );
const iat = Math.floor(Date.now() / 1000);
@@ -161,33 +191,14 @@ export const oidcIssuerServiceFactory = ({ kmsDAL, kmsService, keyStore }: TOidc
// Cached so anonymous requests do not each decrypt every key. A stale set is safe: keys are published a period
// before they sign
const getJwks = async (orgId: string): Promise<TJwks> => {
- const cacheKey = KeyStorePrefixes.OidcIssuerJwks(orgId);
- const cached = await keyStore.getItem(cacheKey);
+ const cached = await keyStore.getItem(KeyStorePrefixes.OidcIssuerJwks(orgId));
if (cached) return JSON.parse(cached) as TJwks;
- const keys = await $listKeys(orgId);
-
- const jwks = await Promise.all(
- keys.map(({ id }) =>
- kmsService
- .getPublicKey({ kmsId: id })
- .then(publicKeyToJwk)
- .catch((err) => {
- // A concurrent rotation can delete the oldest key, which no longer signs
- if (err instanceof NotFoundError) return null;
- throw err;
- })
- )
- );
-
- const jwkSet = { keys: jwks.filter((jwk): jwk is NonNullable<typeof jwk> => jwk !== null) };
-
+ const jwks = await $buildJwks(await $listKeys(orgId));
// An empty set is never cached, or the first token signed for the org would be checked against it
- if (jwkSet.keys.length) {
- await keyStore.setItemWithExpiry(cacheKey, KeyStoreTtls.OidcIssuerJwksInSeconds, JSON.stringify(jwkSet));
- }
+ if (jwks.keys.length) await $cacheJwks(orgId, jwks);
- return jwkSet;
+ return jwks;
};
return {