Skip to content

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

pam validators

x0322059e119295fc21
diff --git a/backend/src/ee/services/pam-account-template/pam-account-template-service.ts b/backend/src/ee/services/pam-account-template/pam-account-template-service.ts
index b95de1c6cd..7825ee4e43 100644
--- a/backend/src/ee/services/pam-account-template/pam-account-template-service.ts
+++ b/backend/src/ee/services/pam-account-template/pam-account-template-service.ts
@@ -4,6 +4,7 @@ import { BadRequestError, DatabaseError, ForbiddenRequestError, NotFoundError }
import { PamProductRole } from "../pam/pam-enums";
import { TActorContext, verifyProductMembership } from "../pam/pam-permission";
+import { TPamValidatorDeps, validateGatewayAttachment, validateRecordingConnection } from "../pam/pam-validators";
import { TPamAccountTemplateDALFactory } from "./pam-account-template-dal";
import {
TCreatePamAccountTemplateDTO,
@@ -13,17 +14,16 @@ import {
TUpdatePamAccountTemplateDTO
} from "./pam-account-template-types";
-type TPamAccountTemplateServiceFactoryDep = {
+type TPamAccountTemplateServiceFactoryDep = TPamValidatorDeps & {
pamAccountTemplateDAL: TPamAccountTemplateDALFactory;
- permissionService: Pick<TPermissionServiceFactory, "getProjectPermission">;
+ permissionService: Pick<TPermissionServiceFactory, "getProjectPermission" | "getOrgPermission">;
};
export type TPamAccountTemplateServiceFactory = ReturnType<typeof pamAccountTemplateServiceFactory>;
-export const pamAccountTemplateServiceFactory = ({
- pamAccountTemplateDAL,
- permissionService
-}: TPamAccountTemplateServiceFactoryDep) => {
+export const pamAccountTemplateServiceFactory = (deps: TPamAccountTemplateServiceFactoryDep) => {
+ const { pamAccountTemplateDAL, permissionService } = deps;
+
const verifyMembership = (projectId: string, ctx: TActorContext) =>
verifyProductMembership(permissionService, projectId, ctx);
@@ -64,6 +64,8 @@ export const pamAccountTemplateServiceFactory = ({
...ctx
}: TCreatePamAccountTemplateDTO & TActorContext) => {
await verifyProductAdmin(projectId, ctx);
+ await validateGatewayAttachment(deps, gatewayId, gatewayPoolId, ctx);
+ await validateRecordingConnection(deps, recordingConnectionId, ctx);
try {
return await pamAccountTemplateDAL.create({
@@ -101,6 +103,8 @@ export const pamAccountTemplateServiceFactory = ({
...ctx
}: TUpdatePamAccountTemplateDTO & TActorContext) => {
await verifyProductAdmin(projectId, ctx);
+ await validateGatewayAttachment(deps, gatewayId, gatewayPoolId, ctx);
+ await validateRecordingConnection(deps, recordingConnectionId, ctx);
const existing = await pamAccountTemplateDAL.findById(templateId);
if (!existing || existing.projectId !== projectId) {
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 06b5b623f6..17a0d72ee5 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -1,9 +1,8 @@
import { ForbiddenError } from "@casl/ability";
-import { OrganizationActionScope, RESOURCE_SCOPE, ResourceType } from "@app/db/schemas";
+import { RESOURCE_SCOPE, ResourceType } from "@app/db/schemas";
import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
import { TGatewayV2DALFactory } from "@app/ee/services/gateway-v2/gateway-v2-dal";
-import { OrgPermissionGatewayActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import {
ResourcePermissionPamResourceActions,
@@ -13,6 +12,7 @@ import { createSshKeyPair } from "@app/ee/services/ssh/ssh-certificate-authority
import { SshCertKeyAlgorithm } from "@app/ee/services/ssh-certificate/ssh-certificate-types";
import { DatabaseErrorCode } from "@app/lib/error-codes";
import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors";
+import { TAppConnectionDALFactory } from "@app/services/app-connection/app-connection-dal";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { KmsDataKey } from "@app/services/kms/kms-types";
import { TMembershipDALFactory } from "@app/services/membership/membership-dal";
@@ -25,6 +25,7 @@ import {
TActorContext,
verifyProductMembership
} from "../pam/pam-permission";
+import { validateGatewayAttachment, validateRecordingConnection } from "../pam/pam-validators";
import { TPamAccountTemplateDALFactory } from "../pam-account-template/pam-account-template-dal";
import { TPamFolderDALFactory } from "../pam-folder/pam-folder-dal";
import { TPamAccountDALFactory } from "./pam-account-dal";
@@ -51,21 +52,22 @@ type TPamAccountServiceFactoryDep = {
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
gatewayV2DAL: Pick<TGatewayV2DALFactory, "findOne">;
gatewayPoolService: Pick<TGatewayPoolServiceFactory, "resolveAttachableGatewayFromPool">;
+ appConnectionDAL: Pick<TAppConnectionDALFactory, "findOne">;
};
export type TPamAccountServiceFactory = ReturnType<typeof pamAccountServiceFactory>;
-export const pamAccountServiceFactory = ({
- pamAccountDAL,
- pamFolderDAL,
- pamAccountTemplateDAL,
- membershipDAL,
- membershipRoleDAL,
- permissionService,
- kmsService,
- gatewayV2DAL,
- gatewayPoolService
-}: TPamAccountServiceFactoryDep) => {
+export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) => {
+ const {
+ pamAccountDAL,
+ pamFolderDAL,
+ pamAccountTemplateDAL,
+ membershipDAL,
+ membershipRoleDAL,
+ permissionService,
+ kmsService
+ } = deps;
+
const getProjectCipher = async (projectId: string) =>
kmsService.createCipherPairWithDataKey({ type: KmsDataKey.SecretManager, projectId });
@@ -85,39 +87,6 @@ export const pamAccountServiceFactory = ({
const checkFolder = (folderId: string, projectId: string, ctx: TActorContext) =>
checkFolderPermission(permissionService, folderId, projectId, ctx);
- const validateGatewayAttachment = async (
- gwId: string | null | undefined,
- poolId: string | null | undefined,
- ctx: TActorContext
- ) => {
- if (gwId) {
- const gw = await gatewayV2DAL.findOne({ id: gwId, orgId: ctx.actorOrgId });
- if (!gw) {
- throw new NotFoundError({ message: "Gateway not found in your organization" });
- }
-
- const { permission: orgPermission } = await permissionService.getOrgPermission({
- scope: OrganizationActionScope.Any,
- actor: ctx.actor,
- actorId: ctx.actorId,
- orgId: ctx.actorOrgId,
- actorAuthMethod: ctx.actorAuthMethod,
- actorOrgId: ctx.actorOrgId
- });
- ForbiddenError.from(orgPermission).throwUnlessCan(
- OrgPermissionGatewayActions.AttachGateways,
- OrgPermissionSubjects.Gateway
- );
- }
- if (poolId) {
- await gatewayPoolService.resolveAttachableGatewayFromPool({
- poolId,
- orgId: ctx.actorOrgId,
- actor: { type: ctx.actor, id: ctx.actorId, authMethod: ctx.actorAuthMethod, orgId: ctx.actorOrgId }
- });
- }
- };
-
const list = async ({ projectId, folderId, templateId, search, ...ctx }: TListPamAccountsDTO & TActorContext) => {
await verifyMembership(projectId, ctx);
@@ -222,7 +191,8 @@ export const pamAccountServiceFactory = ({
});
}
- await validateGatewayAttachment(gatewayId, gatewayPoolId, ctx);
+ await validateGatewayAttachment(deps, gatewayId, gatewayPoolId, ctx);
+ await validateRecordingConnection(deps, recordingConnectionId, ctx);
const validatedConnectionDetails = validateConnectionDetails(accountType, connectionDetails);
const validatedCredentials = validateCredentials(accountType, credentials);
@@ -324,7 +294,8 @@ export const pamAccountServiceFactory = ({
}
}
- await validateGatewayAttachment(gatewayId, gatewayPoolId, ctx);
+ await validateGatewayAttachment(deps, gatewayId, gatewayPoolId, ctx);
+ await validateRecordingConnection(deps, recordingConnectionId, ctx);
const updateData: Record<string, unknown> = {};
if (name !== undefined) updateData.name = name;
diff --git a/backend/src/ee/services/pam/pam-validators.ts b/backend/src/ee/services/pam/pam-validators.ts
new file mode 100644
index 0000000000..bd74df124b
--- /dev/null
+++ b/backend/src/ee/services/pam/pam-validators.ts
@@ -0,0 +1,69 @@
+import { ForbiddenError } from "@casl/ability";
+
+import { OrganizationActionScope } from "@app/db/schemas";
+import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
+import { TGatewayV2DALFactory } from "@app/ee/services/gateway-v2/gateway-v2-dal";
+import { OrgPermissionGatewayActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
+import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
+import { NotFoundError } from "@app/lib/errors";
+import { TAppConnectionDALFactory } from "@app/services/app-connection/app-connection-dal";
+
+import { TActorContext } from "./pam-permission";
+
+export type TPamValidatorDeps = {
+ permissionService: Pick<TPermissionServiceFactory, "getOrgPermission">;
+ gatewayV2DAL: Pick<TGatewayV2DALFactory, "findOne">;
+ gatewayPoolService: Pick<TGatewayPoolServiceFactory, "resolveAttachableGatewayFromPool">;
+ appConnectionDAL: Pick<TAppConnectionDALFactory, "findOne">;
+};
+
+export const validateGatewayAttachment = async (
+ {
+ permissionService,
+ gatewayV2DAL,
+ gatewayPoolService
+ }: Pick<TPamValidatorDeps, "permissionService" | "gatewayV2DAL" | "gatewayPoolService">,
+ gwId: string | null | undefined,
+ poolId: string | null | undefined,
+ ctx: TActorContext
+) => {
+ if (gwId) {
+ const gw = await gatewayV2DAL.findOne({ id: gwId, orgId: ctx.actorOrgId });
+ if (!gw) {
+ throw new NotFoundError({ message: "Gateway not found in your organization" });
+ }
+
+ const { permission: orgPermission } = await permissionService.getOrgPermission({
+ scope: OrganizationActionScope.Any,
+ actor: ctx.actor,
+ actorId: ctx.actorId,
+ orgId: ctx.actorOrgId,
+ actorAuthMethod: ctx.actorAuthMethod,
+ actorOrgId: ctx.actorOrgId
+ });
+ ForbiddenError.from(orgPermission).throwUnlessCan(
+ OrgPermissionGatewayActions.AttachGateways,
+ OrgPermissionSubjects.Gateway
+ );
+ }
+ if (poolId) {
+ await gatewayPoolService.resolveAttachableGatewayFromPool({
+ poolId,
+ orgId: ctx.actorOrgId,
+ actor: { type: ctx.actor, id: ctx.actorId, authMethod: ctx.actorAuthMethod, orgId: ctx.actorOrgId }
+ });
+ }
+};
+
+export const validateRecordingConnection = async (
+ { appConnectionDAL }: Pick<TPamValidatorDeps, "appConnectionDAL">,
+ connectionId: string | null | undefined,
+ ctx: TActorContext
+) => {
+ if (connectionId) {
+ const conn = await appConnectionDAL.findOne({ id: connectionId, orgId: ctx.actorOrgId });
+ if (!conn) {
+ throw new NotFoundError({ message: "Recording connection not found in your organization" });
+ }
+ }
+};
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index a3c763155a..519bde3a3f 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -1530,11 +1530,6 @@ export const registerRoutes = async (
const pamFolderDAL = pamFolderDALFactory(db);
const pamAccountDAL = pamAccountDALFactory(db);
- const pamAccountTemplateService = pamAccountTemplateServiceFactory({
- pamAccountTemplateDAL,
- permissionService
- });
-
const pamFolderService = pamFolderServiceFactory({
pamFolderDAL,
membershipDAL,
@@ -1670,6 +1665,14 @@ export const registerRoutes = async (
dynamicSecretDAL
});
+ const pamAccountTemplateService = pamAccountTemplateServiceFactory({
+ pamAccountTemplateDAL,
+ permissionService,
+ gatewayV2DAL,
+ gatewayPoolService,
+ appConnectionDAL
+ });
+
const pamAccountService = pamAccountServiceFactory({
pamAccountDAL,
pamFolderDAL,
@@ -1679,7 +1682,8 @@ export const registerRoutes = async (
permissionService,
kmsService,
gatewayV2DAL,
- gatewayPoolService
+ gatewayPoolService,
+ appConnectionDAL
});
const pamSessionDAL = pamSessionDALFactory(db);