Skip to content

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

fix(secret-rotation): retire orphaned Stripe keys on every post-create failure

Calvina068fae38bc6
diff --git a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.test.ts b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.test.ts
index 05a86145e7..45cb161310 100644
--- a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.test.ts
+++ b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.test.ts
@@ -93,14 +93,43 @@ describe("stripeApiKeyRotationFactory", () => {
expect(expireCalls("mk_new")).toHaveLength(1);
});
- it("retires the previous key before committing the new one", async () => {
- mockStripe({ createIds: ["mk_new"] });
+ it("expires the new key when reading the returned secret fails", async () => {
+ postMock.mockImplementation(async (url: string) => {
+ if (isCreate(url)) return { data: { id: "mk_new", secret_key: {} } };
+ if (isExpire(url)) return { data: {} };
+ throw new Error(`unexpected request to ${url}`);
+ });
const callback = vi.fn(async (credentials: unknown) => credentials);
+ await expect(makeFactory().issueCredentials(callback as any)).rejects.toThrow(
+ "Stripe returned an API key without a secret"
+ );
+
+ expect(expireCalls("mk_new")).toHaveLength(1);
+ expect(callback).not.toHaveBeenCalled();
+ });
+
+ it("retires the previous key before committing the new one", async () => {
+ const order: string[] = [];
+ postMock.mockImplementation(async (url: string) => {
+ if (isCreate(url)) {
+ order.push("create");
+ return { data: { id: "mk_new", secret_key: { token: "rk_test_mk_new" } } };
+ }
+ if (isExpire(url)) {
+ order.push(url.split("/").slice(-2)[0]);
+ return { data: {} };
+ }
+ throw new Error(`unexpected request to ${url}`);
+ });
+ const callback = vi.fn(async (credentials: unknown) => {
+ order.push("commit");
+ return credentials;
+ });
+
await makeFactory().rotateCredentials({ keyId: "mk_old", apiKey: "rk_old" } as any, callback as any, {} as any);
- const order = postMock.mock.calls.map(([url]) => (isCreate(url) ? "create" : url.split("/").slice(-2)[0]));
- expect(order).toEqual(["create", "mk_old"]);
+ expect(order).toEqual(["create", "mk_old", "commit"]);
expect(callback).toHaveBeenCalledWith({ keyId: "mk_new", apiKey: "rk_test_mk_new" });
});
@@ -129,9 +158,15 @@ describe("stripeApiKeyRotationFactory", () => {
});
const callback = vi.fn(async (credentials: unknown) => credentials);
- await expect(
- makeFactory().rotateCredentials({ keyId: "mk_old" } as any, callback as any, {} as any)
- ).rejects.toThrow(/mk_new/);
+ let caughtMessage = "";
+ try {
+ await makeFactory().rotateCredentials({ keyId: "mk_old" } as any, callback as any, {} as any);
+ } catch (error) {
+ caughtMessage = (error as Error).message;
+ }
+
+ expect(caughtMessage).toMatch(/mk_old/);
+ expect(caughtMessage).toMatch(/mk_new/);
});
it("treats a 404 on expire as already gone", async () => {
@@ -148,6 +183,56 @@ describe("stripeApiKeyRotationFactory", () => {
).resolves.toBeDefined();
});
+ it("revokeCredentials returns early without contacting Stripe when there are no credentials", async () => {
+ const callback = vi.fn(async () => "done");
+
+ const result = await makeFactory().revokeCredentials([] as any, callback as any);
+
+ expect(result).toBe("done");
+ expect(postMock).not.toHaveBeenCalled();
+ });
+
+ it("revokeCredentials expires every key it is given", async () => {
+ mockStripe();
+ const callback = vi.fn(async () => "done");
+
+ const result = await makeFactory().revokeCredentials(
+ [
+ { keyId: "mk_one", apiKey: "rk_one" },
+ { keyId: "mk_two", apiKey: "rk_two" }
+ ] as any,
+ callback as any
+ );
+
+ expect(expireCalls("mk_one")).toHaveLength(1);
+ expect(expireCalls("mk_two")).toHaveLength(1);
+ expect(result).toBe("done");
+ });
+
+ it("revokeCredentials stops at the first failure and never calls back", async () => {
+ mockStripe({
+ expire: async (keyId) => {
+ if (keyId === "mk_two") throw httpError(500, "Stripe is down");
+ return { data: {} };
+ }
+ });
+ const callback = vi.fn(async () => "done");
+
+ await expect(
+ makeFactory().revokeCredentials(
+ [
+ { keyId: "mk_one", apiKey: "rk_one" },
+ { keyId: "mk_two", apiKey: "rk_two" }
+ ] as any,
+ callback as any
+ )
+ ).rejects.toThrow("Stripe is down");
+
+ expect(expireCalls("mk_one")).toHaveLength(1);
+ expect(expireCalls("mk_two")).toHaveLength(1);
+ expect(callback).not.toHaveBeenCalled();
+ });
+
it("maps only the API key into the secrets payload", () => {
expect(makeFactory().getSecretsPayload({ keyId: "mk_new", apiKey: "rk_test" } as any)).toEqual([
{ key: "STRIPE_API_KEY", value: "rk_test" }
diff --git a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.ts b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.ts
index 5e292b889d..7c359999f5 100644
--- a/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.ts
+++ b/backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.ts
@@ -46,6 +46,53 @@ export const stripeApiKeyRotationFactory: TRotationFactory<
// It is what makes a key stranded by a timed-out create identifiable in the Stripe dashboard.
const $keyName = () => `infisical-${secretsMapping.apiKey}-${Date.now()}`.slice(0, STRIPE_KEY_NAME_MAX_LENGTH);
+ /** A 404 means the key is already gone; anything else is handed back for the caller to report. */
+ const $tryRetireKey = async (keyId: string): Promise<{ retired: true } | { retired: false; error: unknown }> => {
+ try {
+ await request.post(
+ `${STRIPE_API_KEYS_URL}/${keyId}/expire`,
+ {},
+ withIdempotencyKey(getStripePlatformRequestConfig(accountId))
+ );
+ return { retired: true };
+ } catch (error) {
+ if (getStripeErrorStatus(error) === 404) return { retired: true };
+
+ return { retired: false, error };
+ }
+ };
+
+ /**
+ * Expire is the whole retirement. Stripe decides when it takes effect, so nothing here claims the
+ * key is dead. Narrowing the key's permissions first would stop it sooner, and was measured
+ * working, but is deliberately not in v1. See the design doc.
+ */
+ const $retireKey = async (keyId: string) => {
+ const result = await $tryRetireKey(keyId);
+
+ if (!result.retired) throwStripeApiKeyManagementError(accountId, result.error);
+ };
+
+ /**
+ * A key exists in Stripe before the row does, so anything that fails after create, whether that is
+ * decrypting the secret Stripe returned or committing the row, has to take the key with it.
+ */
+ const $retireOnFailure = async <T>(keyId: string, action: () => Promise<T>): Promise<T> => {
+ try {
+ return await action();
+ } catch (actionError) {
+ try {
+ await $retireKey(keyId);
+ } catch (cleanupError) {
+ throw new BadRequestError({
+ message: `${getErrorMessage(actionError)} The newly created Stripe API key ${keyId} could not be expired and may need to be removed manually: ${getErrorMessage(cleanupError)}`
+ });
+ }
+
+ throw actionError;
+ }
+ };
+
const $createApiKey = async () => {
const { publicKey, privateKey } = await generateStripeEncryptionKeyPair();
@@ -71,46 +118,12 @@ export const stripeApiKeyRotationFactory: TRotationFactory<
throw new BadRequestError({ message: "Stripe did not return an ID for the created API key." });
}
- return { keyId: data.id, apiKey: readStripeSecret(data.secret_key, privateKey) };
- };
-
- /**
- * Expire is the whole retirement. Stripe decides when it takes effect, so nothing here claims the
- * key is dead. Narrowing the key's permissions first would stop it sooner, and was measured
- * working, but is deliberately not in v1. See the design doc.
- */
- const $retireKey = async (keyId: string) => {
- try {
- await request.post(
- `${STRIPE_API_KEYS_URL}/${keyId}/expire`,
- {},
- withIdempotencyKey(getStripePlatformRequestConfig(accountId))
- );
- } catch (error) {
- if (getStripeErrorStatus(error) === 404) return;
-
- throwStripeApiKeyManagementError(accountId, error);
- }
- };
-
- /** A key exists in Stripe before the row does, so a failed commit has to take the key with it. */
- const $commitOrCleanUp = async <T>(
- credentials: { keyId: string; apiKey: string },
- callback: (credentials: { keyId: string; apiKey: string }) => Promise<T>
- ): Promise<T> => {
- try {
- return await callback(credentials);
- } catch (commitError) {
- try {
- await $retireKey(credentials.keyId);
- } catch (cleanupError) {
- throw new BadRequestError({
- message: `${getErrorMessage(commitError)} The newly created Stripe API key ${credentials.keyId} could not be expired and may need to be removed manually: ${getErrorMessage(cleanupError)}`
- });
- }
+ const { id: keyId } = data;
- throw commitError;
- }
+ // readStripeSecret can throw (a malformed JWE, an unsupported algorithm, a decrypt failure). The
+ // key already exists in Stripe by that point, so that throw needs the same cleanup as a failed
+ // commit, not a bare rethrow.
+ return $retireOnFailure(keyId, async () => ({ keyId, apiKey: readStripeSecret(data.secret_key, privateKey) }));
};
const issueCredentials: TRotationFactoryIssueCredentials<TStripeApiKeyRotationGeneratedCredentials> = async (
@@ -118,7 +131,7 @@ export const stripeApiKeyRotationFactory: TRotationFactory<
) => {
const credentials = await $createApiKey();
- return $commitOrCleanUp(credentials, callback);
+ return $retireOnFailure(credentials.keyId, () => callback(credentials));
};
const revokeCredentials: TRotationFactoryRevokeCredentials<TStripeApiKeyRotationGeneratedCredentials> = async (
@@ -144,22 +157,22 @@ export const stripeApiKeyRotationFactory: TRotationFactory<
// Retire before committing, so a failure leaves Postgres and Stripe agreeing with each other and
// the key we just minted gets cleaned up rather than orphaned across BullMQ's retries.
if (credentialsToRevoke?.keyId) {
- try {
- await $retireKey(credentialsToRevoke.keyId);
- } catch (retireError) {
- try {
- await $retireKey(newCredentials.keyId);
- } catch (cleanupError) {
+ const retireResult = await $tryRetireKey(credentialsToRevoke.keyId);
+
+ if (!retireResult.retired) {
+ const cleanupResult = await $tryRetireKey(newCredentials.keyId);
+
+ if (!cleanupResult.retired) {
throw new BadRequestError({
- message: `${getErrorMessage(retireError)} The newly created Stripe API key ${newCredentials.keyId} could not be expired and may need to be removed manually: ${getErrorMessage(cleanupError)}`
+ message: `Stripe API key ${credentialsToRevoke.keyId} could not be retired (${getStripeErrorMessage(retireResult.error)}), and the newly created key ${newCredentials.keyId} could not be cleaned up either (${getStripeErrorMessage(cleanupResult.error)}). Both may need to be removed manually from the Stripe dashboard.`
});
}
- throw retireError;
+ throwStripeApiKeyManagementError(accountId, retireResult.error);
}
}
- return $commitOrCleanUp(newCredentials, callback);
+ return $retireOnFailure(newCredentials.keyId, () => callback(newCredentials));
};
const getSecretsPayload: TRotationFactoryGetSecretsPayload<TStripeApiKeyRotationGeneratedCredentials> = ({