Skip to content

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

change the order so we revoke first and then the callback is called

adilsone48c2d748425
diff --git a/backend/src/ee/services/secret-rotation-v2/cloudflare-api-token/cloudflare-api-token-rotation-fns.ts b/backend/src/ee/services/secret-rotation-v2/cloudflare-api-token/cloudflare-api-token-rotation-fns.ts
index 8a532f011e..3200e41b7c 100644
--- a/backend/src/ee/services/secret-rotation-v2/cloudflare-api-token/cloudflare-api-token-rotation-fns.ts
+++ b/backend/src/ee/services/secret-rotation-v2/cloudflare-api-token/cloudflare-api-token-rotation-fns.ts
@@ -13,7 +13,6 @@ import {
TCloudflareTokenResources,
verifyCloudflareToken
} from "@app/ee/services/secret-rotation-v2/shared/cloudflare-token";
-import { BadRequestError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { CloudflareApiTokenPolicyScope } from "./cloudflare-api-token-rotation-schemas";
@@ -95,31 +94,31 @@ export const cloudflareApiTokenRotationFactory: TRotationFactory<
return callback();
};
- // We persist the new token before revoking the old one so a revocation failure can never leave the
- // rotation without a usable credential.
+ // The new token is created first so a creation failure leaves the rotation untouched. The previous
+ // (inactive) token is then revoked *before* the new credentials are persisted: the callback
+ // overwrites the inactive slot, so a post-persist revocation failure would lose the token id and
+ // leave the token active with no way to retry its deletion. Revoking first keeps the DB state
+ // unchanged on failure, so the next attempt retries the same deletion (a 404 counts as success).
const rotateCredentials: TRotationFactoryRotateCredentials<TCloudflareApiTokenRotationGeneratedCredentials> = async (
credentialsToRevoke,
callback
) => {
const newCredentials = await $createToken();
- const result = await callback(newCredentials);
-
if (credentialsToRevoke?.tokenId) {
try {
await deleteCloudflareToken({ accountId, connectionApiToken, tokenId: credentialsToRevoke.tokenId });
} catch (error: unknown) {
logger.error(
error,
- `Failed to revoke previous token after rotation [rotationId=${rotationId}] [tokenId=${credentialsToRevoke.tokenId}]`
+ `Failed to revoke previous token during rotation [rotationId=${rotationId}] [tokenId=${credentialsToRevoke.tokenId}]`
);
- throw new BadRequestError({
- message: `Failed to revoke previous token after rotation [rotationId=${rotationId}] [tokenId=${credentialsToRevoke.tokenId}]`
- });
+ // deleteCloudflareToken already throws a descriptive, user-facing BadRequestError
+ throw error;
}
}
- return result;
+ return callback(newCredentials);
};
const getSecretsPayload: TRotationFactoryGetSecretsPayload<TCloudflareApiTokenRotationGeneratedCredentials> = (
diff --git a/backend/src/ee/services/secret-rotation-v2/cloudflare-r2-access-key/cloudflare-r2-access-key-rotation-fns.ts b/backend/src/ee/services/secret-rotation-v2/cloudflare-r2-access-key/cloudflare-r2-access-key-rotation-fns.ts
index 644ae814a5..ab1920554d 100644
--- a/backend/src/ee/services/secret-rotation-v2/cloudflare-r2-access-key/cloudflare-r2-access-key-rotation-fns.ts
+++ b/backend/src/ee/services/secret-rotation-v2/cloudflare-r2-access-key/cloudflare-r2-access-key-rotation-fns.ts
@@ -146,30 +146,30 @@ export const cloudflareR2AccessKeyRotationFactory: TRotationFactory<
return callback();
};
- // We persist the new access key before revoking the old one so a revocation failure can never leave
- // the rotation without a usable credential.
+ // The new access key is created first so a creation failure leaves the rotation untouched. The
+ // previous (inactive) key is then revoked *before* the new credentials are persisted: the callback
+ // overwrites the inactive slot, so a post-persist revocation failure would lose the key id and
+ // leave the underlying token active with no way to retry its deletion. Revoking first keeps the DB
+ // state unchanged on failure, so the next attempt retries the same deletion (a 404 counts as success).
const rotateCredentials: TRotationFactoryRotateCredentials<
TCloudflareR2AccessKeyRotationGeneratedCredentials
> = async (credentialsToRevoke, callback) => {
const newCredentials = await $createCredentials();
- const result = await callback(newCredentials);
-
if (credentialsToRevoke?.accessKeyId) {
try {
await deleteCloudflareToken({ accountId, connectionApiToken, tokenId: credentialsToRevoke.accessKeyId });
} catch (error) {
logger.error(
error,
- `Failed to revoke previous token after rotation [rotationId=${rotationId}] [tokenId=${credentialsToRevoke.accessKeyId}]`
+ `Failed to revoke previous token during rotation [rotationId=${rotationId}] [tokenId=${credentialsToRevoke.accessKeyId}]`
);
- throw new BadRequestError({
- message: `Failed to revoke previous token after rotation [rotationId=${rotationId}] [tokenId=${credentialsToRevoke.accessKeyId}]`
- });
+ // deleteCloudflareToken already throws a descriptive, user-facing BadRequestError
+ throw error;
}
}
- return result;
+ return callback(newCredentials);
};
const getSecretsPayload: TRotationFactoryGetSecretsPayload<TCloudflareR2AccessKeyRotationGeneratedCredentials> = (