Skip to content

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

cache the oidc issuer jwks in redis

DimitrisStaratzis1e4062ff9e52
diff --git a/backend/AGENTS.md b/backend/AGENTS.md
index b028fb7e9c..3bf6c11277 100644
--- a/backend/AGENTS.md
+++ b/backend/AGENTS.md
@@ -1159,6 +1159,9 @@ URL and the `sub` shape are one-way doors: customers copy both into their provid
- **Keys are created on the first sign, never on a JWKS fetch.** Those routes are unauthenticated, and
existing orgs need no backfill. An empty JWKS is sent `no-store` so the first token is never checked
against a cached empty set.
+- **The built JWKS is cached in Redis for 10 minutes**, because deriving it decrypts every key and the route
+ is anonymous. A stale set is harmless for the reason below; empty sets are never cached, and a rotation
+ clears the entry.
- **The second newest key signs.** The newest is published a full 24h period before it takes over, and
rotation keeps at most three keys. It runs under a Redis lock rather than an advisory lock, so no
database connection is held while RSA keys are generated, and it re-reads keys on the primary.
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.ts
index a7d04f3c29..f4b44a57b7 100644
--- a/backend/src/keystore/keystore.ts
+++ b/backend/src/keystore/keystore.ts
@@ -83,6 +83,7 @@ export const KeyStorePrefixes = {
StripeConnectionRefreshLock: (connectionId: string) => `stripe-connection-refresh-mutex-${connectionId}` as const,
PamAccountRotationLock: (accountId: string) => `pam-account-rotation-mutex-${accountId}` as const,
OidcIssuerKeyRotationLock: (orgId: string) => `oidc-issuer-key-rotation-mutex-${orgId}` as const,
+ OidcIssuerJwks: (orgId: string) => `oidc-issuer-jwks:${orgId}` as const,
SecretScanningFullScanLease: (resourceId: string) => `secret-scanning-v2-full-scan-lease-${resourceId}` as const,
SecretScanningDataSourceLock: (dataSourceId: string) =>
`secret-scanning-v2-data-source-mutex-${dataSourceId}` as const,
@@ -252,6 +253,7 @@ export const KeyStorePrefixes = {
};
export const KeyStoreTtls = {
+ OidcIssuerJwksInSeconds: 10 * 60,
LdapDirectoryMachinesInSeconds: 60,
LdapHostLoginInSeconds: 3600,
LdapGuessedHostLoginInSeconds: 30,
diff --git a/backend/src/services/oidc-issuer/oidc-issuer-service.ts b/backend/src/services/oidc-issuer/oidc-issuer-service.ts
index 16a3762dc8..b7661d1a4d 100644
--- a/backend/src/services/oidc-issuer/oidc-issuer-service.ts
+++ b/backend/src/services/oidc-issuer/oidc-issuer-service.ts
@@ -1,6 +1,6 @@
import { Knex } from "knex";
-import { KeyStorePrefixes, TKeyStoreFactory, TKeyStoreLock } from "@app/keystore/keystore";
+import { KeyStorePrefixes, KeyStoreTtls, TKeyStoreFactory, TKeyStoreLock } from "@app/keystore/keystore";
import { getConfig } from "@app/lib/config/env";
import { crypto } from "@app/lib/crypto/cryptography";
import { AsymmetricKeyAlgorithm, SigningAlgorithm } from "@app/lib/crypto/sign/types";
@@ -27,9 +27,11 @@ 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">;
+ keyStore: Pick<TKeyStoreFactory, "acquireLock" | "getItem" | "setItemWithExpiry" | "deleteItem">;
};
+type TJwks = { keys: Awaited<ReturnType<typeof publicKeyToJwk>>[] };
+
export type TOidcIssuerServiceFactory = ReturnType<typeof oidcIssuerServiceFactory>;
export const oidcIssuerServiceFactory = ({ kmsDAL, kmsService, keyStore }: TOidcIssuerServiceFactoryDep) => {
@@ -90,6 +92,7 @@ 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));
} finally {
@@ -165,7 +168,13 @@ export const oidcIssuerServiceFactory = ({ kmsDAL, kmsService, keyStore }: TOidc
};
};
- const getJwks = async (orgId: string) => {
+ // Cached so an anonymous caller cannot make every request decrypt keys. A stale set is harmless, because a key
+ // is published a full signing period before it signs and was already out of use when it is deleted.
+ const getJwks = async (orgId: string): Promise<TJwks> => {
+ const cacheKey = KeyStorePrefixes.OidcIssuerJwks(orgId);
+ const cached = await keyStore.getItem(cacheKey);
+ if (cached) return JSON.parse(cached) as TJwks;
+
const keys = await $listKeys(orgId);
const jwks = await Promise.all(
@@ -182,7 +191,14 @@ export const oidcIssuerServiceFactory = ({ kmsDAL, kmsService, keyStore }: TOidc
)
);
- return { keys: jwks.filter((jwk): jwk is NonNullable<typeof jwk> => jwk !== null) };
+ const jwkSet = { keys: jwks.filter((jwk): jwk is NonNullable<typeof jwk> => jwk !== null) };
+
+ // 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));
+ }
+
+ return jwkSet;
};
return {