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
feat(pam): auto-heal delegated rotation on credential drift, split deferral errors
bernie-g738a1560e4ed
diff --git a/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.test.ts b/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.test.tsindex 989b26c31d..0533ad216a 100644--- a/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.test.ts+++ b/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.test.ts@@ -136,13 +136,16 @@ describe("rotateScheduledAccount recovery probe", () => {);});- test("defers when neither the pending nor the current credential authenticates", async () => {+ test("self-rotation fails with an actionable error when neither the pending nor the current credential works", async () => {+ // Definitive rejection of both (not a transport blip): a self-rotating account has no other way in, so it+ // surfaces an actionable "update the stored password" error rather than a transient defer-and-retry.const { service, applyPasswordChange } = buildService(() => false);const result = await service.rotateScheduledAccount("acc-1");expect(result?.rotationStatus).toBe(ROTATION_STATUS.Failed);- expect(result?.message).toContain("deferred");+ expect(result?.message).toContain("stored password no longer works");+ expect(result?.message).not.toContain("deferred");expect(applyPasswordChange).not.toHaveBeenCalled();});@@ -239,6 +242,39 @@ describe("rotateScheduledAccount recovery probe", () => {expect(applyArgs.targetUsername).toBe("app");});+ test("delegated target auto-heals a drifted stored credential instead of deferring", async () => {+ // The account keeps a stored current password, but it no longer matches the target (drift), and there is a+ // stale pending. Because the rotator resets unconditionally, rotation must proceed and heal the drift, unlike+ // self-rotation which would defer.+ const account = { ...buildAccount(), rotationAccountId: "rot-1" };+ const rotator = {+ id: "rot-1",+ projectId: "proj-1",+ accountType: PamAccountType.Postgres,+ encryptedCredentials: blobOf({ username: "rotuser", password: "rot-pw" }),+ encryptedConnectionDetails: blobOf(connectionDetails),+ gatewayId: "gw-rot",+ gatewayPoolId: null,+ templateGatewayId: null,+ templateGatewayPoolId: null+ };+ // Both the pending and the stored current password are rejected; only the freshly-generated password verifies.+ const { service, applyPasswordChange, updateById } = buildService(+ (password) => password !== PENDING_PASSWORD && password !== CURRENT_PASSWORD,+ { account, rotator }+ );++ const result = await service.rotateScheduledAccount("acc-1");++ expect(result?.rotationStatus).toBe(ROTATION_STATUS.Success);+ // It rotated (applied a new password) rather than deferring on the drifted current credential.+ expect(applyPasswordChange).toHaveBeenCalledTimes(1);+ expect(updateById).toHaveBeenCalledWith(+ "acc-1",+ expect.objectContaining({ encryptedPendingCredentials: null, rotationStatus: ROTATION_STATUS.Success })+ );+ });+test("aborts a delegated rotation when target and rotator are no longer the same resource, sending no credential", async () => {// The target's host was changed after binding, so it no longer matches the rotator's resource.const account = {diff --git a/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.ts b/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.tsindex a9e624f886..71544b1717 100644--- a/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.ts+++ b/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.ts@@ -509,8 +509,8 @@ export const pamAccountRotationServiceFactory = (deps: TPamAccountRotationServic// A delegated local account can't be logged in as to verify, so validate via the admin rotator instead.const verifyVia = isDelegated && accountType === PamAccountType.Windows ? auth : undefined;- // Recovery from an interrupted rotation: probe the candidate then the current credential, acting only on a- // definitive answer so a transient failure never discards a possibly-live credential.+ // Recovery from an interrupted rotation. A live pending credential means a prior rotation actually succeeded+ // before it could be recorded, so promote it (both self and delegated). Past that, the two paths diverge.if (account.encryptedPendingCredentials) {const pending = await decryptSqlCredentials(projectId, accountType, account.encryptedPendingCredentials);// Verify uses the rotator's gateway, not the target's: a delegated target often has no gateway of its own@@ -540,30 +540,44 @@ export const pamAccountRotationServiceFactory = (deps: TPamAccountRotationServicreturn null;}- // Pending did not authenticate. A successful current-credential probe proves the target is reachable,- // telling a dead pending from a transport blip; without that proof we defer rather than discard pending.- const currentPassword = targetCredentials.password;- let currentWorks = false;- if (currentPassword) {- try {- currentWorks = await probe(currentPassword);- } catch {+ if (isDelegated) {+ // The rotator resets the target unconditionally, so a stale pending (or a stored credential that no longer+ // matches the target) never blocks rotation: discard the dead pending and re-rotate below, which heals the+ // drift. The self-rotation deferral does not apply, because the rotator, not the target credential, is the+ // way in. A genuinely unreachable target surfaces as an error when the rotator's own apply fails.+ await pamAccountDAL.updateById(account.id, { encryptedPendingCredentials: null });+ } else {+ // Self-rotation can only change the password by authenticating as the account itself, so if neither the+ // pending nor the current credential works it cannot proceed. A successful current-credential probe also+ // proves the target is reachable, telling a dead pending from a transport blip.+ const currentPassword = targetCredentials.password;+ let currentWorks = false;+ if (currentPassword) {+ try {+ currentWorks = await probe(currentPassword);+ } catch {+ throw new BadRequestError({+ message: "Could not reach the target to verify its credentials; rotation deferred and will retry"+ });+ }+ }+ if (pendingThrew && !currentWorks) {+ // Pending was inconclusive (transport) and the current credential didn't prove reachability: can't tell a+ // dead pending from an unreachable target, so defer rather than risk discarding a live pending.throw new BadRequestError({- message: `Could not verify ${pendingThrew ? "pending" : "current"} credential; rotation deferred`+ message: "Could not reach the target to verify its credentials; rotation deferred and will retry"});}+ if (!currentWorks && currentPassword) {+ // Both credentials are definitively rejected. A self-rotating account has no other way to authenticate,+ // so it cannot rotate until its stored password is corrected.+ throw new BadRequestError({+ message:+ "This account's stored password no longer works on the target, and it rotates its own password, so it cannot authenticate to rotate. Update the stored password to the account's current one, or assign a delegated rotation account, then try again."+ });+ }+ await pamAccountDAL.updateById(account.id, { encryptedPendingCredentials: null });}- if (pendingThrew && !currentWorks) {- // Pending was inconclusive and the current credential didn't prove reachability: can't distinguish a- // dead pending from an unreachable target, so defer rather than risk discarding a live pending.- throw new BadRequestError({ message: "Could not verify pending credential; rotation deferred" });- }- if (!currentWorks && currentPassword) {- // Pending definitively failed and the current credential also failed: neither authenticates, defer. A- // delegated target (no stored current password) instead falls through, since its rotator re-sets below.- throw new BadRequestError({ message: "Could not verify account credentials; rotation deferred" });- }- await pamAccountDAL.updateById(account.id, { encryptedPendingCredentials: null });}const requirements = getPasswordRequirements(account.templateSettings);