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
remove wait
Dimitris Staratzis17255bb4ce8c
diff --git a/backend/src/ee/services/secret-rotation-v2/gcp-service-account-key/gcp-service-account-key-rotation-fns.test.ts b/backend/src/ee/services/secret-rotation-v2/gcp-service-account-key/gcp-service-account-key-rotation-fns.test.tsindex de611fa90d..10d5c778eb 100644--- a/backend/src/ee/services/secret-rotation-v2/gcp-service-account-key/gcp-service-account-key-rotation-fns.test.ts+++ b/backend/src/ee/services/secret-rotation-v2/gcp-service-account-key/gcp-service-account-key-rotation-fns.test.ts@@ -3,11 +3,10 @@ import { beforeEach, describe, expect, it, vi } from "vitest";import { TSecretRotationV2Raw } from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-types";-const { postMock, deleteMock, authorizeMock, delayMock } = vi.hoisted(() => ({+const { postMock, deleteMock, authorizeMock } = vi.hoisted(() => ({postMock: vi.fn<(url: string, body?: unknown, config?: unknown) => Promise<unknown>>(),deleteMock: vi.fn<(url: string, config?: unknown) => Promise<unknown>>(),- authorizeMock: vi.fn<() => Promise<unknown>>(),- delayMock: vi.fn<(ms: number) => Promise<void>>()+ authorizeMock: vi.fn<() => Promise<unknown>>()}));vi.mock("@app/lib/config/request", () => ({@@ -16,18 +15,12 @@ vi.mock("@app/lib/config/request", () => ({vi.mock("@app/services/app-connection/gcp", () => ({getGcpConnectionAuthToken: vi.fn(async () => "access-token")}));-// Stands in for GCP's token endpoint, which is where a new key is accepted or rejected.+// Stands in for GCP's token endpoint, which is where a key is accepted or rejected.vi.mock("google-auth-library", () => ({JWT: class {authorize = authorizeMock;}}));-vi.mock("@app/lib/delay", () => ({- delay: delayMock-}));-vi.mock("@app/lib/logger", () => ({- logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn(), debug: vi.fn() }-}));// eslint-disable-next-line import/firstimport { gcpServiceAccountKeyRotationFactory } from "./gcp-service-account-key-rotation-fns";@@ -133,7 +126,7 @@ describe("gcpServiceAccountKeyRotationFactory", () => {});describe("issueCredentials", () => {- it("creates a key and hands the decoded key file to the callback without waiting for GCP", async () => {+ it("creates a key and hands the decoded key file to the callback", async () => {mockGcp();const result = await makeFactory().issueCredentials(commit);@@ -168,30 +161,6 @@ describe("gcpServiceAccountKeyRotationFactory", () => {expect(commit).toHaveBeenCalledWith(NEW_KEY);});- it("waits until GCP accepts the new key when running as a background job", async () => {- mockGcp();- authorizeMock.mockImplementationOnce(async () => {- calls.push("authorize");- throw new Error("invalid_grant: Invalid JWT Signature.");- });-- await makeFactory().rotateCredentials(OLD_KEY, commit, NEW_KEY, { isBackgroundJob: true });-- expect(calls).toEqual(["create", "authorize", "authorize", "delete:old-key-id", "commit"]);- });-- it("deletes the new key and keeps the previous one when GCP never accepts the new key", async () => {- mockGcp();- authorizeMock.mockRejectedValue(new Error("invalid_grant: Invalid JWT Signature."));-- await expect(- makeFactory().rotateCredentials(OLD_KEY, commit, NEW_KEY, { isBackgroundJob: true })- ).rejects.toThrow("GCP did not accept the new key");-- expect(calls).toEqual(["create", "delete:new-key-id"]);- expect(commit).not.toHaveBeenCalled();- });-it("deletes the new key and fails when deleting the previous key fails", async () => {mockGcp({deleteKey: async (keyId) => {diff --git a/backend/src/ee/services/secret-rotation-v2/gcp-service-account-key/gcp-service-account-key-rotation-fns.ts b/backend/src/ee/services/secret-rotation-v2/gcp-service-account-key/gcp-service-account-key-rotation-fns.tsindex 7bd57031f6..97bb015fad 100644--- a/backend/src/ee/services/secret-rotation-v2/gcp-service-account-key/gcp-service-account-key-rotation-fns.ts+++ b/backend/src/ee/services/secret-rotation-v2/gcp-service-account-key/gcp-service-account-key-rotation-fns.ts@@ -10,9 +10,7 @@ import {TRotationFactoryRotateCredentials} from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-types";import { request } from "@app/lib/config/request";-import { delay } from "@app/lib/delay";import { BadRequestError } from "@app/lib/errors";-import { logger } from "@app/lib/logger";import { getGcpConnectionAuthToken } from "@app/services/app-connection/gcp";import { IntegrationUrls } from "@app/services/integration-auth/integration-list";@@ -23,11 +21,6 @@ import {TGcpServiceAccountKeyRotationWithConnection} from "./gcp-service-account-key-rotation-types";-// GCP documents that a new key can take 60 seconds or more before it is accepted. Every check has to-// finish inside this rotation's lock, which SECRET_ROTATION_LOCK_TTL_MS sets to 5 minutes.-const KEY_VERIFICATION_MAX_ATTEMPTS = 5;-const KEY_VERIFICATION_INTERVAL_MS = 60 * 1000;-type TGoogleApiError = {error?: {status?: string;@@ -151,37 +144,7 @@ export const gcpServiceAccountKeyRotationFactory: TRotationFactory<}).authorize();};- // Apps read the mapped secret as soon as it changes, so a scheduled rotation only publishes a key once GCP- // accepts it. Rotations run from an HTTP request skip this, so the wait never holds a request open.- const $waitUntilKeyAccepted = async (keyId: string, serviceAccountKey: string) => {- let lastError: unknown;-- for (let attempt = 1; attempt <= KEY_VERIFICATION_MAX_ATTEMPTS; attempt += 1) {- try {- // eslint-disable-next-line no-await-in-loop- await $authenticateWithKey(serviceAccountKey);-- if (attempt > 1) {- logger.info(- `secretRotation: GCP accepted new service account key [serviceAccountEmail=${serviceAccountEmail}] [keyId=${keyId}] [attempt=${attempt}]`- );- }-- return;- } catch (error) {- lastError = error;- }-- // eslint-disable-next-line no-await-in-loop- if (attempt < KEY_VERIFICATION_MAX_ATTEMPTS) await delay(KEY_VERIFICATION_INTERVAL_MS);- }-- throw new BadRequestError({- message: `GCP did not accept the new key "${keyId}" for service account "${serviceAccountEmail}" in time: ${getErrorMessage(lastError)}`- });- };-- const $createKey = async (accessToken: string, shouldWaitUntilAccepted: boolean) => {+ const $createKey = async (accessToken: string) => {let data: TGcpServiceAccountKeyCreateResponse;try {@@ -205,11 +168,7 @@ export const gcpServiceAccountKeyRotationFactory: TRotationFactory<});}- const serviceAccountKey = Buffer.from(data.privateKeyData, "base64").toString("utf8");-- if (shouldWaitUntilAccepted) await $waitUntilKeyAccepted(keyId, serviceAccountKey);-- return { keyId, serviceAccountKey };+ return { keyId, serviceAccountKey: Buffer.from(data.privateKeyData, "base64").toString("utf8") };});};@@ -218,8 +177,7 @@ export const gcpServiceAccountKeyRotationFactory: TRotationFactory<) => {const accessToken = await getGcpConnectionAuthToken(connection);- // creating a rotation always runs inside an HTTP request- const credentials = await $createKey(accessToken, false);+ const credentials = await $createKey(accessToken);return $deleteOnFailure(accessToken, credentials.keyId, () => callback(credentials));};@@ -248,13 +206,13 @@ export const gcpServiceAccountKeyRotationFactory: TRotationFactory<const rotateCredentials: TRotationFactoryRotateCredentials<TGcpServiceAccountKeyRotationGeneratedCredentials- > = async (credentialsToRevoke, callback, _activeCredentials, options) => {+ > = async (credentialsToRevoke, callback) => {const accessToken = await getGcpConnectionAuthToken(connection);- // The new key is created before the previous one is deleted, so a failed create or a key GCP never- // accepts can't leave the service account with fewer working keys than before. The cost is room for a- // third key under GCP's 10 key limit while the rotation runs.- const newCredentials = await $createKey(accessToken, Boolean(options?.isBackgroundJob));+ // The new key is created before the previous one is deleted, so a failed create can't leave the service+ // account with fewer working keys than before. The cost is room for a third key under GCP's 10 key limit+ // while the rotation runs.+ const newCredentials = await $createKey(accessToken);// Delete before committing, so a failure leaves GCP and the rotation agreeing with each other and the// new key is cleaned up rather than left untracked across retries.diff --git a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-maps.ts b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-maps.tsindex 1c8bfb6b64..0e0a14ce69 100644--- a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-maps.ts+++ b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-maps.ts@@ -66,11 +66,3 @@ export const SECRET_ROTATION_CONNECTION_MAP: Record<SecretRotation, AppConnectio[SecretRotation.StripeApiKey]: AppConnection.Stripe,[SecretRotation.GcpServiceAccountKey]: AppConnection.GCP};--export const DEFAULT_SECRET_ROTATION_LOCK_TTL_MS = 60 * 1000;--// For rotations that wait on the provider after creating a credential, so a rotation still running can't-// have its lock expire and another rotation start on the same credentials.-export const SECRET_ROTATION_LOCK_TTL_MS: Partial<Record<SecretRotation, number>> = {- [SecretRotation.GcpServiceAccountKey]: 5 * 60 * 1000-};diff --git a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.tsindex a8b20d68d1..fcc8d1255d 100644--- a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts+++ b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts@@ -38,9 +38,7 @@ import {throwOnImmutableParameterUpdate} from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-fns";import {- DEFAULT_SECRET_ROTATION_LOCK_TTL_MS,SECRET_ROTATION_CONNECTION_MAP,- SECRET_ROTATION_LOCK_TTL_MS,SECRET_ROTATION_NAME_MAP} from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-maps";import {@@ -1320,10 +1318,7 @@ export const secretRotationV2ServiceFactory = ({try {try {- lock = await keyStore.acquireLock(- [KeyStorePrefixes.SecretRotationLock(rotationId)],- SECRET_ROTATION_LOCK_TTL_MS[type as SecretRotation] ?? DEFAULT_SECRET_ROTATION_LOCK_TTL_MS- );+ lock = await keyStore.acquireLock([KeyStorePrefixes.SecretRotationLock(rotationId)], 60 * 1000);} catch (e) {throw new InternalServerError({message: "Failed to acquire rotation lock."@@ -1444,8 +1439,7 @@ export const secretRotationV2ServiceFactory = ({);});},- activeCredentials,- { isBackgroundJob: Boolean(jobId) }+ activeCredentials);await auditLogService.createAuditLog({diff --git a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.tsindex c7406c399b..98493d8ca8 100644--- a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts+++ b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts@@ -517,15 +517,10 @@ export type TRotationFactoryRevokeCredentials<T extends TSecretRotationV2Generatcallback: () => Promise<TSecretRotationV2Raw>) => Promise<TSecretRotationV2Raw>;-// isBackgroundJob is true when a queue job runs the rotation rather than an HTTP request, so a factory-// can do slow work, such as waiting for the provider to accept new credentials, without holding a request open.-export type TRotationFactoryRotateCredentialsOptions = { isBackgroundJob: boolean };-export type TRotationFactoryRotateCredentials<T extends TSecretRotationV2GeneratedCredentials> = (credentialsToRevoke: T[number] | undefined,callback: (newCredentials: T[number]) => Promise<TSecretRotationV2Raw>,- activeCredentials: T[number],- options?: TRotationFactoryRotateCredentialsOptions+ activeCredentials: T[number]) => Promise<TSecretRotationV2Raw>;export type TRotationFactoryGetSecretsPayload<T extends TSecretRotationV2GeneratedCredentials> = (diff --git a/docs/documentation/platform/secret-rotation/gcp-service-account-key.mdx b/docs/documentation/platform/secret-rotation/gcp-service-account-key.mdxindex bc4e52cd11..a8fcb2a01a 100644--- a/docs/documentation/platform/secret-rotation/gcp-service-account-key.mdx+++ b/docs/documentation/platform/secret-rotation/gcp-service-account-key.mdx@@ -18,9 +18,9 @@ Rotate the JSON keys of a Google Cloud service account on a schedule. On each ro## Prerequisites- A [project with secrets configured](/documentation/platform/secrets-mgmt/quick-starts/deliver-first-secret)-- A GCP service account that your applications authenticate as with a key-- A role that lets you grant roles on that service account, such as **Service Account Admin**-- The `iam.disableServiceAccountKeyCreation` organization policy not enforced on the service account's project (if it's enforced, nobody can create keys in that project)+- A [GCP service account](https://docs.cloud.google.com/iam/docs/service-accounts-create) that your applications authenticate as with a key+- A role that lets you [grant roles on that service account](https://docs.cloud.google.com/iam/docs/manage-access-service-accounts), such as **Service Account Admin**+- The `iam.disableServiceAccountKeyCreation` [organization policy](https://docs.cloud.google.com/organization-policy/restrict-service-accounts) not enforced on the service account's project (if it's enforced, nobody can create keys in that project)## Step 1: Configure the service account for Infisical@@ -152,10 +152,10 @@ Infisical needs a GCP connection whose service account has the **Service AccountInfisical doesn't change or delete keys that it didn't create. After your applications read the key from Infisical, delete any keys you created by hand so they stop working.</Accordion>- <Accordion title="Why does a scheduled rotation take longer than a manual one?">- GCP can take a minute or more before it accepts a new key. A scheduled rotation waits until GCP accepts the new key before it updates the secret, so applications don't read a key that GCP still rejects. If GCP doesn't accept the key within about 4 minutes, Infisical deletes the new key and the rotation fails. The current key stays in the secret and keeps working.+ <Accordion title="Can a new key fail right after a rotation?">+ Yes, for a short time. GCP usually accepts a new key within seconds, but Google documents that it can take a minute or more. Infisical updates the secret as soon as GCP creates the key, so an application that reads the secret right after a rotation can briefly fail to authenticate.- If you rotate the key manually with **Rotate Secret**, or create a new rotation, Infisical updates the secret without waiting. For a short time after that, applications that read the new key can fail to authenticate. After a manual rotation, the previous key keeps working during that time.+ The previous key keeps working until the next rotation, so applications that still hold it aren't affected. If your application reads the secret right after it changes, retry authentication for a short time.</Accordion><Accordion title="Can I use this rotation with a key expiry policy?">Yes. If your Google Cloud organization enforces the `iam.serviceAccountKeyExpiryHours` policy, every key expires after the policy's duration, including the keys Infisical creates. Each key stays valid for two rotation intervals (first as the current key, then as the previous key), so set the rotation interval to at most half of the expiry. For example, with a 90-day expiry, rotate every 45 days or less.