Skip to content

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

add credential validation for sql rotations

adilson92ddae67cd37
diff --git a/backend/src/ee/routes/v2/secret-rotation-v2-routers/secret-rotation-v2-endpoints.ts b/backend/src/ee/routes/v2/secret-rotation-v2-routers/secret-rotation-v2-endpoints.ts
index 08a305576b..a3f881aa9f 100644
--- a/backend/src/ee/routes/v2/secret-rotation-v2-routers/secret-rotation-v2-endpoints.ts
+++ b/backend/src/ee/routes/v2/secret-rotation-v2-routers/secret-rotation-v2-endpoints.ts
@@ -557,4 +557,39 @@ export const registerSecretRotationEndpoints = <
return { secretRotation };
}
});
+
+ server.route({
+ method: "POST",
+ url: "/:rotationId/check-credentials",
+ config: {
+ rateLimit: writeLimit
+ },
+ schema: {
+ hide: false,
+ operationId: `check${rotationTypeId}RotationCredentials`,
+ tags: [ApiDocsTags.SecretRotations],
+ description: `Check whether the active credentials for the specified ${rotationType} Rotation are still valid against the upstream system.`,
+ params: z.object({
+ rotationId: z.string().uuid().describe(SecretRotations.CHECK_CREDENTIALS(type).rotationId)
+ }),
+ response: {
+ 204: z.string().length(0)
+ }
+ },
+ onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
+ handler: async (req, res) => {
+ const { rotationId } = req.params;
+
+ await server.services.secretRotationV2.checkSecretRotationCredentials(
+ {
+ rotationId,
+ type,
+ auditLogInfo: req.auditLogInfo
+ },
+ req.permission
+ );
+
+ return res.status(204).send();
+ }
+ });
};
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.ts
index 42e28007ab..a1b1ad4de7 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -549,6 +549,7 @@ export enum EventType {
MOVE_SECRET_ROTATION = "move-secret-rotation",
SECRET_ROTATION_ROTATE_SECRETS = "secret-rotation-rotate-secrets",
RECONCILE_SECRET_ROTATION = "reconcile-secret-rotation",
+ SECRET_ROTATION_CHECK_CREDENTIALS = "secret-rotation-check-credentials",
PROJECT_ACCESS_REQUEST = "project-access-request",
@@ -4446,6 +4447,18 @@ interface ReconcileSecretRotationEvent {
};
}
+interface CheckSecretRotationCredentialsEvent {
+ type: EventType.SECRET_ROTATION_CHECK_CREDENTIALS;
+ metadata: {
+ type: string;
+ rotationId: string;
+ connectionId: string;
+ folderId: string;
+ success: boolean;
+ errorMessage?: string;
+ };
+}
+
interface MicrosoftTeamsWorkflowIntegrationCreateEvent {
type: EventType.MICROSOFT_TEAMS_WORKFLOW_INTEGRATION_CREATE;
metadata: {
@@ -6775,6 +6788,7 @@ export type Event =
| MoveSecretRotationEvent
| RotateSecretRotationEvent
| ReconcileSecretRotationEvent
+ | CheckSecretRotationCredentialsEvent
| MicrosoftTeamsWorkflowIntegrationCreateEvent
| MicrosoftTeamsWorkflowIntegrationDeleteEvent
| MicrosoftTeamsWorkflowIntegrationCheckInstallationStatusEvent
diff --git a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts
index 2508742402..2336119c87 100644
--- a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts
+++ b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-service.ts
@@ -35,6 +35,7 @@ import {
SECRET_ROTATION_NAME_MAP
} from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-maps";
import {
+ TCheckSecretRotationV2Credentials,
TCreateSecretRotationV2DTO,
TDeleteSecretRotationV2DTO,
TFindSecretRotationV2ByIdDTO,
@@ -1477,6 +1478,112 @@ export const secretRotationV2ServiceFactory = ({
}
};
+ const checkSecretRotationCredentials = async (
+ { rotationId, type, auditLogInfo }: TCheckSecretRotationV2Credentials,
+ actor: OrgServiceActor
+ ) => {
+ const plan = await licenseService.getPlan(actor.orgId);
+
+ if (!plan.secretRotation)
+ throw new BadRequestError({
+ message:
+ "Failed to check secret rotation credentials due to plan restriction. Upgrade plan to check secret rotation credentials."
+ });
+
+ const secretRotation = await secretRotationV2DAL.findById(rotationId);
+
+ if (!secretRotation)
+ throw new NotFoundError({
+ message: `Could not find ${SECRET_ROTATION_NAME_MAP[type]} Rotation with ID "${rotationId}"`
+ });
+
+ const { projectId, connection, encryptedGeneratedCredentials, activeIndex, folderId } = secretRotation;
+
+ const { permission } = await permissionService.getProjectPermission({
+ actor: actor.type,
+ actorId: actor.id,
+ actorAuthMethod: actor.authMethod,
+ actorOrgId: actor.orgId,
+ actionProjectType: ActionProjectType.SecretManager,
+ projectId
+ });
+
+ ForbiddenError.from(permission).throwUnlessCan(
+ ProjectPermissionSecretRotationActions.ReadGeneratedCredentials,
+ getSecretRotationSubject(secretRotation)
+ );
+
+ if (connection.app !== SECRET_ROTATION_CONNECTION_MAP[type])
+ throw new BadRequestError({
+ message: `Secret Rotation with ID "${rotationId}" is not configured for ${SECRET_ROTATION_NAME_MAP[type]}`
+ });
+
+ const isRotationOccurring = Boolean(await keyStore.getItem(KeyStorePrefixes.SecretRotationLock(secretRotation.id)));
+
+ if (isRotationOccurring)
+ throw new BadRequestError({ message: `A rotation is in progress. Please try again shortly.` });
+
+ const appConnection = await decryptAppConnection(connection, kmsService);
+
+ const generatedCredentials = await decryptSecretRotationCredentials({
+ projectId,
+ encryptedGeneratedCredentials,
+ kmsService
+ });
+
+ const activeCredentials = generatedCredentials?.[activeIndex];
+
+ if (!activeCredentials)
+ throw new BadRequestError({ message: "No active credentials are available to check for this rotation." });
+
+ const rotationFactory = SECRET_ROTATION_FACTORY_MAP[type as SecretRotation](
+ {
+ ...secretRotation,
+ connection: appConnection
+ } as TSecretRotationV2WithConnection,
+ appConnectionDAL,
+ kmsService,
+ gatewayService,
+ gatewayV2Service
+ );
+
+ if (!rotationFactory.checkActiveCredentials)
+ throw new BadRequestError({
+ message: `Credential check is not yet supported for ${SECRET_ROTATION_NAME_MAP[type]} Rotations.`
+ });
+
+ let success = true;
+ let errorMessage: string | undefined;
+
+ try {
+ await rotationFactory.checkActiveCredentials(activeCredentials);
+ } catch (err) {
+ success = false;
+ errorMessage = (err as Error).message;
+ }
+
+ await auditLogService.createAuditLog({
+ ...(auditLogInfo ?? { actor: { type: ActorType.PLATFORM, metadata: {} } }),
+ projectId,
+ event: {
+ type: EventType.SECRET_ROTATION_CHECK_CREDENTIALS,
+ metadata: {
+ type,
+ rotationId,
+ connectionId: connection.id,
+ folderId,
+ success,
+ errorMessage
+ }
+ }
+ });
+
+ if (!success)
+ throw new BadRequestError({
+ message: errorMessage ?? "Active credentials are no longer valid."
+ });
+ };
+
const getDashboardSecretRotationCount = async (
{ projectId, environments, secretPath, search }: TGetDashboardSecretRotationV2Count,
actor: OrgServiceActor
@@ -1912,6 +2019,7 @@ export const secretRotationV2ServiceFactory = ({
getDashboardSecretRotationCount,
getDashboardSecretRotations,
getQuickSearchSecretRotations,
- reconcileLocalAccountRotation
+ reconcileLocalAccountRotation,
+ checkSecretRotationCredentials
};
};
diff --git a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts
index e9a67ff84a..b12fd0bb61 100644
--- a/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts
+++ b/backend/src/ee/services/secret-rotation-v2/secret-rotation-v2-types.ts
@@ -248,6 +248,8 @@ export type TFindSecretRotationV2ByIdDTO = {
export type TRotateSecretRotationV2 = TFindSecretRotationV2ByIdDTO & { auditLogInfo: AuditLogInfo };
+export type TCheckSecretRotationV2Credentials = TFindSecretRotationV2ByIdDTO & { auditLogInfo: AuditLogInfo };
+
export type TRotateAtUtc = { hours: number; minutes: number };
export type TFindSecretRotationV2ByNameDTO = {
@@ -379,6 +381,10 @@ export type TRotationFactoryGetSecretsPayload<T extends TSecretRotationV2Generat
generatedCredentials: T[number]
) => { key: string; value: string }[];
+export type TRotationFactoryCheckActiveCredentials<T extends TSecretRotationV2GeneratedCredentials> = (
+ activeCredentials: T[number]
+) => Promise<void>;
+
export type TRotationFactory<
T extends TSecretRotationV2WithConnection,
C extends TSecretRotationV2GeneratedCredentials,
@@ -394,4 +400,5 @@ export type TRotationFactory<
revokeCredentials: TRotationFactoryRevokeCredentials<C>;
rotateCredentials: TRotationFactoryRotateCredentials<C>;
getSecretsPayload: TRotationFactoryGetSecretsPayload<C>;
+ checkActiveCredentials?: TRotationFactoryCheckActiveCredentials<C>;
};
diff --git a/backend/src/ee/services/secret-rotation-v2/shared/sql-credentials/sql-credentials-rotation-fns.ts b/backend/src/ee/services/secret-rotation-v2/shared/sql-credentials/sql-credentials-rotation-fns.ts
index 5becb191f0..8e859b204c 100644
--- a/backend/src/ee/services/secret-rotation-v2/shared/sql-credentials/sql-credentials-rotation-fns.ts
+++ b/backend/src/ee/services/secret-rotation-v2/shared/sql-credentials/sql-credentials-rotation-fns.ts
@@ -3,6 +3,7 @@ import { Knex } from "knex";
import {
TRotationFactory,
+ TRotationFactoryCheckActiveCredentials,
TRotationFactoryGetSecretsPayload,
TRotationFactoryIssueCredentials,
TRotationFactoryRevokeCredentials,
@@ -210,10 +211,17 @@ export const sqlCredentialsRotationFactory: TRotationFactory<
return secrets;
};
+ const checkActiveCredentials: TRotationFactoryCheckActiveCredentials<
+ TSqlCredentialsRotationGeneratedCredentials
+ > = async (activeCredentials) => {
+ await $validateCredentials(activeCredentials);
+ };
+
return {
issueCredentials,
revokeCredentials,
rotateCredentials,
- getSecretsPayload
+ getSecretsPayload,
+ checkActiveCredentials
};
};
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index ecf161488d..59ca7b0e54 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -3216,6 +3216,9 @@ export const SecretRotations = {
RECONCILE: {
rotationId: "The ID of the SSH Password Rotation to reconcile credentials for."
},
+ CHECK_CREDENTIALS: (type: SecretRotation) => ({
+ rotationId: `The ID of the ${SECRET_ROTATION_NAME_MAP[type]} Rotation to check active credentials for.`
+ }),
PARAMETERS: {
SQL_CREDENTIALS: {
username1: