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
feat(encryption): enhance key rotation process with acknowledgment feature
Victor Hugo dos Santos04bce48667bb
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.tsindex 0fe5ed28e6..facbfc29b1 100644--- a/backend/src/lib/api-docs/constants.ts+++ b/backend/src/lib/api-docs/constants.ts@@ -3979,6 +3979,8 @@ export const ENCRYPTION_KEY_ROTATION = {COMPLETE: {description:"Remove the previous encryption key now that every instance is using the new one. This cannot be undone: after it, losing the new key means losing access to all stored secrets.",- rotationId: "ID of the previous key to remove."+ rotationId: "ID of the previous key to remove.",+ acknowledged:+ "Remove the key even though an instance started on it recently. Any instance still using that key will fail its next restart until it is given the new one."}};diff --git a/backend/src/lib/telemetry/metrics.ts b/backend/src/lib/telemetry/metrics.tsindex ea0a0ba35e..ad95b4f426 100644--- a/backend/src/lib/telemetry/metrics.ts+++ b/backend/src/lib/telemetry/metrics.ts@@ -812,9 +812,11 @@ export const recordLegacyRootKeyUsageMetric = (params: {operation: "encrypt" | "decrypt";surface: LegacyRootKeySurface;}) => {- if (!isTelemetryEnabled()) return;- legacyRootKeyUsageCounter.add(1, {- "legacy_key.operation": params.operation,- "legacy_key.surface": params.surface+ safely(() => {+ if (!isTelemetryEnabled()) return;+ legacyRootKeyUsageCounter.add(1, {+ "legacy_key.operation": params.operation,+ "legacy_key.surface": params.surface+ });});};diff --git a/backend/src/server/routes/v1/admin-router.ts b/backend/src/server/routes/v1/admin-router.tsindex 743c3d0623..13efdc96ec 100644--- a/backend/src/server/routes/v1/admin-router.ts+++ b/backend/src/server/routes/v1/admin-router.ts@@ -801,7 +801,13 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => {description: ENCRYPTION_KEY_ROTATION.COMPLETE.description,params: z.object({rotationId: z.string().uuid().describe(ENCRYPTION_KEY_ROTATION.COMPLETE.rotationId)- })+ }),+ // Defaults to absent, so a scripted caller that says nothing still gets the guard.+ body: z+ .object({+ acknowledged: z.boolean().optional().describe(ENCRYPTION_KEY_ROTATION.COMPLETE.acknowledged)+ })+ .nullish()},onRequest: (req, res, done) => {verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN])(req, res, () => {@@ -809,9 +815,10 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => {});},handler: async (req, res) => {- const { retiredFingerprint } = await server.services.encryptionKeyRotation.completeRotation(- req.params.rotationId- );+ const { retiredFingerprint } = await server.services.encryptionKeyRotation.completeRotation({+ rotationId: req.params.rotationId,+ acknowledged: req.body?.acknowledged+ });await server.services.auditLog.createAuditLog({...req.auditLogInfo,diff --git a/backend/src/services/encryption-key-rotation/encryption-key-rotation-service.ts b/backend/src/services/encryption-key-rotation/encryption-key-rotation-service.tsindex d8e5233d16..c43d60aa8b 100644--- a/backend/src/services/encryption-key-rotation/encryption-key-rotation-service.ts+++ b/backend/src/services/encryption-key-rotation/encryption-key-rotation-service.ts@@ -12,6 +12,7 @@ import { RootKeyEncryptionStrategy } from "@app/services/kms/kms-types";import { generateRootEncryptionKey, resolveKekBuffer } from "./encryption-key-rotation-fns";import {RotationBlocker,+ TCompleteRotationDTO,TCreatedRotation,TCreateRotationDTO,TEncryptionStatus@@ -19,9 +20,9 @@ import {const ABANDONED_ROTATION_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000; // 30 days-// How recent a superseded-key boot has to be for `complete` to treat it as a live straggler and refuse.-// Shorter than the GC's window on purpose: the GC is unattended and conservative, whereas an operator-// calling `complete` is asserting their rollout is done, so only fresh evidence should override them.+// How recent a superseded-key boot has to be for `complete` to treat it as a live straggler and ask the+// operator to confirm. Shorter than the GC's window on purpose: the GC is unattended and has nobody to+// make that judgement, whereas an operator calling `complete` can look at their fleet.const STRAGGLER_EVIDENCE_WINDOW_MS = 60 * 60 * 1000; // 1 hourtype TEncryptionKeyRotationServiceFactoryDep = {@@ -136,21 +137,30 @@ export const encryptionKeyRotationServiceFactory = ({logger.info(`Encryption key rotation discarded [rotationId=${rotationId}]`);};- const completeRotation = async (rotationId: string) => {+ const completeRotation = async ({ rotationId, acknowledged }: TCompleteRotationDTO) => {const row = await kmsRootConfigDAL.findById(rotationId);if (!row || row.id === KMS_ROOT_CONFIG_UUID || !row.supersededAt) {throw new NotFoundError({ message: `Retained encryption key with ID '${rotationId}' not found` });}- // Only fresh evidence overrides the operator. An instance that started on this key within the hour- // is very likely still running, and removing the key would break its next restart.+ // An instance that started on this key within the hour is very likely still running, and removing the+ // key would break its next restart. Nothing reports whether it has since rolled forward, so this asks+ // the operator rather than refusing outright.const lastResolvedAt = row.lastResolvedAt ? new Date(row.lastResolvedAt).getTime() : null;if (lastResolvedAt && Date.now() - lastResolvedAt < STRAGGLER_EVIDENCE_WINDOW_MS) {- throw new BadRequestError({- message: `An instance started on the previous encryption key at ${new Date(+ if (!acknowledged) {+ throw new BadRequestError({+ message: `An instance started on the previous encryption key at ${new Date(+ lastResolvedAt+ ).toISOString()} and may still be running. It would fail to restart if the key were removed now. Roll that instance onto the current key first, or acknowledge this to remove the key anyway.`+ });+ }++ logger.warn(+ `Removing an encryption key an instance started on at ${new Date(lastResolvedAt- ).toISOString()} and is likely still running. It would fail to restart if the key were removed now. Roll that instance onto the current key first.`- });+ ).toISOString()}, acknowledged by the caller [retainedKeyId=${rotationId}]`+ );}await kmsRootConfigDAL.deleteById(rotationId);diff --git a/backend/src/services/encryption-key-rotation/encryption-key-rotation-types.ts b/backend/src/services/encryption-key-rotation/encryption-key-rotation-types.tsindex 67dc967ee9..0a9be32635 100644--- a/backend/src/services/encryption-key-rotation/encryption-key-rotation-types.ts+++ b/backend/src/services/encryption-key-rotation/encryption-key-rotation-types.ts@@ -21,6 +21,11 @@ export type TCreateRotationDTO = {supersede?: boolean;};+export type TCompleteRotationDTO = {+ rotationId: string;+ acknowledged?: boolean;+};+export type TCreatedRotation = {id: string;fingerprint: string;diff --git a/docs/self-hosting/guides/rotating-the-encryption-key.mdx b/docs/self-hosting/guides/rotating-the-encryption-key.mdxindex 56566db345..2e4b2a0c47 100644--- a/docs/self-hosting/guides/rotating-the-encryption-key.mdx+++ b/docs/self-hosting/guides/rotating-the-encryption-key.mdx@@ -59,10 +59,11 @@ one, think about it, and discard it with no consequences.<Note>- Infisical can only see instances that have *started* on the previous key, so it will refuse to remove- a key one started on recently. It cannot see an instance that has been running since before the- rotation and has not restarted. If such an instance restarts after the key is gone, it fails to start- with an error naming the key it needs; setting that key again brings it back.+ Infisical can only see instances that have *started* on the previous key. If one did so recently, it+ asks you to confirm before removing the key, since you are the only one who can tell whether that+ instance has since moved onto the new key. It cannot see an instance that has been running since+ before the rotation and has not restarted. If such an instance restarts after the key is gone, it+ fails to start with an error naming the key it needs; setting that key again brings it back.</Note></Step></Steps>diff --git a/frontend/src/hooks/api/admin/mutation.ts b/frontend/src/hooks/api/admin/mutation.tsindex 14d180fdb2..364595209b 100644--- a/frontend/src/hooks/api/admin/mutation.ts+++ b/frontend/src/hooks/api/admin/mutation.ts@@ -10,6 +10,7 @@ import {AdminEmailDomain,RootKeyEncryptionStrategy,TAdminCreateEmailDomainDTO,+ TCompleteEncryptionKeyRotationDTO,TCreateAdminUserDTO,TCreatedEncryptionKeyRotation,TCreateOrganizationDTO,@@ -223,8 +224,10 @@ export const useDiscardEncryptionKeyRotation = () => {export const useCompleteEncryptionKeyRotation = () => {const queryClient = useQueryClient();return useMutation({- mutationFn: async (rotationId: string) => {- await apiRequest.post(`/api/v1/admin/encryption/rotations/${rotationId}/complete`);+ mutationFn: async ({ rotationId, acknowledged }: TCompleteEncryptionKeyRotationDTO) => {+ await apiRequest.post(`/api/v1/admin/encryption/rotations/${rotationId}/complete`, {+ acknowledged+ });},onSuccess: () => {queryClient.invalidateQueries({ queryKey: adminQueryKeys.getEncryptionStatus() });diff --git a/frontend/src/hooks/api/admin/types.ts b/frontend/src/hooks/api/admin/types.tsindex dd3921c943..f80b4f2a80 100644--- a/frontend/src/hooks/api/admin/types.ts+++ b/frontend/src/hooks/api/admin/types.ts@@ -265,3 +265,8 @@ export type TCreatedEncryptionKeyRotation = {fingerprint: string;key: string;};++export type TCompleteEncryptionKeyRotationDTO = {+ rotationId: string;+ acknowledged: boolean;+};diff --git a/frontend/src/pages/admin/EncryptionPage/components/EncryptionKeyRotationSection.tsx b/frontend/src/pages/admin/EncryptionPage/components/EncryptionKeyRotationSection.tsxindex 06f574aa5d..a2dc551fa5 100644--- a/frontend/src/pages/admin/EncryptionPage/components/EncryptionKeyRotationSection.tsx+++ b/frontend/src/pages/admin/EncryptionPage/components/EncryptionKeyRotationSection.tsx@@ -76,7 +76,7 @@ export const EncryptionKeyRotationSection = () => {const handleComplete = async () => {if (!status.retainedKey) return;- await completeRotation(status.retainedKey.id);+ await completeRotation({ rotationId: status.retainedKey.id, acknowledged });setAcknowledged(false);createNotification({ type: "success", text: "Previous encryption key removed." });};