Skip to content

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

Merge branch 'calvin/eng-5685-add-stripe-api-key-secret-rotation-integration' of https://github.com/Infisical/infisical into calvin/eng-5685-add-stripe-api-key-secret-rotation-integration

varonix48aa03c7965d
diff --cc backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.test.ts
index b9005d01b6,48e0c59200..e25d0b8996
--- 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
@@@ -161,20 -169,31 +168,45 @@@ describe("stripeApiKeyRotationFactory"
expect(createBody?.public_key.pem_key.data).toContain("BEGIN PUBLIC KEY");
});
+ // The old and new key are both live after a rotation, so a fixed name alone would leave two keys
+ // in the Stripe dashboard that cannot be told apart.
+ it.each([
+ { source: "the default name", keyName: undefined, expected: "^infisical-managed-[0-9]{13}$" },
+ { source: "the configured key name", keyName: "payments-service", expected: "^payments-service-[0-9]{13}$" }
+ ])("names the key after $source with a timestamp suffix", async ({ keyName, expected }) => {
+ mockStripe();
+
+ await makeFactory({ keyName }).issueCredentials(commit);
+
+ const name = createBody?.name ?? "";
+ expect(new RE2(expected).test(name), `expected ${expected}, got "${name}"`).toBe(true);
+ });
+
+ it("confirms the connection is still authorized before acting on the account with the app key", async () => {
+ mockStripe();
+
+ await makeFactory().issueCredentials(commit);
+
+ const { headers } = postMock.mock.calls[0][2] as { headers: Record<string, string> };
+
+ expect(authorizeMock).toHaveBeenCalledWith("connection-id", {}, {});
+ expect(authorizeMock.mock.invocationCallOrder[0]).toBeLessThan(postMock.mock.invocationCallOrder[0]);
+ expect(headers.Authorization).toBe("Bearer sk_test_app");
+ expect(headers["Stripe-Context"]).toBe("acct_123");
+ });
+
+ it.each([
+ { operation: "issue", run: () => makeFactory().issueCredentials(commit) },
+ { operation: "rotate", run: () => makeFactory().rotateCredentials(OLD_KEY, commit, OLD_KEY) },
+ { operation: "revoke", run: () => makeFactory().revokeCredentials([OLD_KEY], commitWithoutCredentials) }
+ ])("does not touch Stripe keys on $operation once the app was uninstalled", async ({ run }) => {
+ mockStripe();
+ authorizeMock.mockRejectedValue(new Error("Infisical is no longer authorized"));
+
+ await expect(run()).rejects.toThrow("Infisical is no longer authorized");
+ expect(calls).toEqual([]);
+ });
+
// The key exists in Stripe before the row does, so both failure points after create have to
// take it with them.
it.each([
diff --cc backend/src/ee/services/secret-rotation-v2/stripe-api-key/stripe-api-key-rotation-fns.ts
index b46c3cd1fb,13e497e4f1..990fa2d1b9
--- 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
@@@ -31,20 -32,23 +32,23 @@@ const STRIPE_KEY_NAME_MAX_LENGTH = 100
export const stripeApiKeyRotationFactory: TRotationFactory<
TStripeApiKeyRotationWithConnection,
TStripeApiKeyRotationGeneratedCredentials
- > = (secretRotation) => {
+ > = (secretRotation, appConnectionDAL, kmsService) => {
const {
connection,
- parameters: { permissions, connectPermissions },
+ parameters: { permissions, keyName },
secretsMapping
} = secretRotation;
const { accountId } = connection.credentials;
+ // The default name leaves out the mapped secret's name, which would otherwise show in the Stripe
+ // dashboard. The timestamp keeps the old and new key apart while both are live, and is what makes
+ // a key stranded by a timed-out create identifiable there.
+ const $keyName = () => `${keyName ?? `infisical-managed`}-${Date.now()}`.slice(0, STRIPE_KEY_NAME_MAX_LENGTH);
+ // Runs once at the start of each operation, not per request, so the cleanup a failed operation
+ // does (expiring a key it just created) never fails on authorization half way through.
+ const $assertAuthorized = () => assertStripeConnectionAuthorized(connection.id, appConnectionDAL, kmsService);
- // The factory is built without an id at create time, so the name comes from the mapped secret.
- // 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);
-
/** No 404 on a double expire has ever been observed, so a non-404 failure on retiring a key that
* was already expired can't be told apart from a real failure by status code alone. This is only
* ever consulted after the expire call itself has failed, to check whether the key was already