Skip to content

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

greptile feedback

x03220575906c4257a3
diff --git a/backend/src/ee/routes/v1/pam-account-routers/pam-account-router.ts b/backend/src/ee/routes/v1/pam-account-routers/pam-account-router.ts
index f0a45fd236..af8bcfc5b5 100644
--- a/backend/src/ee/routes/v1/pam-account-routers/pam-account-router.ts
+++ b/backend/src/ee/routes/v1/pam-account-routers/pam-account-router.ts
@@ -164,33 +164,17 @@ export const registerPamAccountRouter = async (server: FastifyZodProvider) => {
accountId: z.string().uuid()
}),
response: {
- 200: z.object({
+ 202: z.object({
success: z.boolean(),
accountId: z.string().uuid()
})
}
},
onRequest: verifyAuth([AuthMode.JWT]),
- handler: async (req) => {
- const account = await server.services.pamAccount.triggerManualRotation(req.params.accountId, req.permission);
-
- if (account) {
- await server.services.auditLog.createAuditLog({
- ...req.auditLogInfo,
- orgId: req.permission.orgId,
- projectId: account.projectId,
- event: {
- type: EventType.PAM_ACCOUNT_CREDENTIAL_ROTATION,
- metadata: {
- accountId: req.params.accountId,
- accountName: account.name,
- resourceId: account.resourceId,
- resourceType: "unknown"
- }
- }
- });
- }
+ handler: async (req, res) => {
+ await server.services.pamAccount.triggerManualRotation(req.params.accountId, req.permission);
+ void res.status(202);
return { success: true, accountId: req.params.accountId };
}
});
diff --git a/backend/src/ee/services/pam-account/pam-account-service.ts b/backend/src/ee/services/pam-account/pam-account-service.ts
index 719150ea1e..5ad7946447 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -1099,7 +1099,14 @@ export const pamAccountServiceFactory = ({
const rotateAccount = async (account: TPamAccounts) => {
let logResourceType = "unknown";
try {
- // Read resource and mark account as rotating
+ // Atomically claim rotation lock, only proceeds if not already rotating
+ const [claimed] = await pamAccountDAL.update(
+ { id: account.id, $notEqual: { rotationStatus: PamAccountRotationStatus.Rotating } },
+ { rotationStatus: PamAccountRotationStatus.Rotating }
+ );
+ if (!claimed) return;
+
+ // Read resource
const resource = await pamResourceDAL.findById(account.resourceId);
if (!resource || !resource.encryptedRotationAccountCredentials) return;
logResourceType = resource.resourceType;
@@ -1111,8 +1118,6 @@ export const pamAccountServiceFactory = ({
);
if (!rotationAccountCredentials) return;
- await pamAccountDAL.updateById(account.id, { rotationStatus: PamAccountRotationStatus.Rotating });
-
// Perform rotation
const accountCredentials = await decryptAccountCredentials({
encryptedCredentials: account.encryptedCredentials,
@@ -1177,37 +1182,48 @@ export const pamAccountServiceFactory = ({
const errorMessage = error instanceof Error ? error.message : "An unknown error occurred";
- const { encryptor } = await kmsService.createCipherPairWithDataKey({
- type: KmsDataKey.SecretManager,
- projectId: account.projectId
- });
+ try {
+ const { encryptor } = await kmsService.createCipherPairWithDataKey({
+ type: KmsDataKey.SecretManager,
+ projectId: account.projectId
+ });
- const { cipherTextBlob: encryptedMessage } = encryptor({
- plainText: Buffer.from(errorMessage)
- });
+ const { cipherTextBlob: encryptedMessage } = encryptor({
+ plainText: Buffer.from(errorMessage)
+ });
- await pamAccountDAL.updateById(account.id, {
- rotationStatus: PamAccountRotationStatus.Failed,
- encryptedLastRotationMessage: encryptedMessage
- });
+ await pamAccountDAL.updateById(account.id, {
+ rotationStatus: PamAccountRotationStatus.Failed,
+ encryptedLastRotationMessage: encryptedMessage
+ });
+ } catch (encryptErr) {
+ logger.error(encryptErr, `Failed to encrypt rotation error for account [accountId=${account.id}]`);
+ await pamAccountDAL.updateById(account.id, {
+ rotationStatus: PamAccountRotationStatus.Failed
+ });
+ }
- await auditLogService.createAuditLog({
- projectId: account.projectId,
- actor: {
- type: ActorType.PLATFORM,
- metadata: {}
- },
- event: {
- type: EventType.PAM_ACCOUNT_CREDENTIAL_ROTATION_FAILED,
- metadata: {
- accountId: account.id,
- accountName: account.name,
- resourceId: account.resourceId,
- resourceType: logResourceType,
- errorMessage
+ try {
+ await auditLogService.createAuditLog({
+ projectId: account.projectId,
+ actor: {
+ type: ActorType.PLATFORM,
+ metadata: {}
+ },
+ event: {
+ type: EventType.PAM_ACCOUNT_CREDENTIAL_ROTATION_FAILED,
+ metadata: {
+ accountId: account.id,
+ accountName: account.name,
+ resourceId: account.resourceId,
+ resourceType: logResourceType,
+ errorMessage
+ }
}
- }
- });
+ });
+ } catch (auditErr) {
+ logger.error(auditErr, `Failed to create audit log for rotation failure [accountId=${account.id}]`);
+ }
}
};
@@ -1253,9 +1269,11 @@ export const pamAccountServiceFactory = ({
throw new BadRequestError({ message: "Account is already being rotated" });
}
- await rotateAccount(account);
+ void rotateAccount(account).catch((err) => {
+ logger.error(err, `Background rotation failed for account [accountId=${accountId}]`);
+ });
- return pamAccountDAL.findById(accountId);
+ return { accountId };
};
return {
diff --git a/backend/src/ee/services/pam-resource/shared/dependency-sync-fns.ts b/backend/src/ee/services/pam-resource/shared/dependency-sync-fns.ts
index 9a6f3ffec6..30e441e4f3 100644
--- a/backend/src/ee/services/pam-resource/shared/dependency-sync-fns.ts
+++ b/backend/src/ee/services/pam-resource/shared/dependency-sync-fns.ts
@@ -1,3 +1,4 @@
+import RE2 from "re2";
import { runPowershell } from "winrm-client";
import { TPamAccountDependencies, TPamResources } from "@app/db/schemas";
@@ -16,7 +17,7 @@ import { TPostRotateContext } from "../pam-resource-types";
export const WINRM_PORT = 5985;
// Escape a string for use inside PowerShell single-quoted strings (doubles single quotes)
-export const escapePowershellSingleQuote = (value: string) => value.replace(/'/g, "''");
+export const escapePowershellSingleQuote = (value: string) => new RE2("'", "g").replace(value, "''");
export const buildDependencySyncScript = (
dep: TPamAccountDependencies,