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
fix pki signer checkup
Thiago Araujo da Silva8162786f9568
diff --git a/backend/e2e-test/routes/v1/org-access-reconcile.spec.ts b/backend/e2e-test/routes/v1/org-access-reconcile.spec.tsindex 5dd31ee696..e157ec0db7 100644--- a/backend/e2e-test/routes/v1/org-access-reconcile.spec.ts+++ b/backend/e2e-test/routes/v1/org-access-reconcile.spec.ts@@ -6,7 +6,9 @@ import {AccessScope,OrgMembershipRole,ProjectMembershipRole,+ ProjectType,RESOURCE_SCOPE,+ ResourceMembershipRole,ResourceType,TableName} from "@app/db/schemas";@@ -373,6 +375,61 @@ describe("Org access reconcile", () => {expect(await projectRows(agentVaultProjectId, { actorUserId: userId })).toHaveLength(1);});+ test("Certificate Manager applications and signers take a user and identity that reach the sub-org only through a group", async () => {+ const groupId = await createLinkedGroup("pki-grp");+ const { userId, username } = await createUser("pki-user");+ createdUserIds.push(userId);+ await addUserMembership({ userId, orgId: rootOrgId, role: OrgMembershipRole.Member });+ await testDb(TableName.UserGroupMembership).insert({ userId, groupId });+ const { identityId } = await createIdentityActor({ orgId: rootOrgId, authToken: rootToken });+ createdIdentityIds.push(identityId);+ await testDb(TableName.IdentityGroupMembership).insert({ identityId, groupId });++ const projectRes = await asSubOrg("POST", "/api/v1/projects", {+ projectName: `pki-${randomUUID().slice(0, 8)}`,+ type: ProjectType.CertificateManager+ });+ expect(projectRes.statusCode).toBe(200);+ const certProjectId = projectRes.json().project.id as string;+ const [application] = await testDb(TableName.PkiApplication)+ .insert({ projectId: certProjectId, name: `pki-app-${randomUUID().slice(0, 8)}` })+ .returning("id");+ const [signer] = await testDb(TableName.PkiSigners)+ .insert({ projectId: certProjectId, name: `pki-signer-${randomUUID().slice(0, 8)}` })+ .returning("id");++ const userRes = await asSubOrg("POST", "/api/v1/cert-manager/access/users", { usernames: [username] });+ expect(userRes.statusCode).toBe(200);+ const identityRes = await asSubOrg("POST", `/api/v1/cert-manager/access/identities/${identityId}`, {});+ expect(identityRes.statusCode).toBe(200);++ const appUserRes = await asSubOrg("POST", `/api/v1/cert-manager/applications/${application.id}/users`, {+ userIds: [userId]+ });+ expect(appUserRes.statusCode).toBe(200);+ expect(appUserRes.json().unresolved).toEqual([]);+ const appIdentityRes = await asSubOrg(+ "POST",+ `/api/v1/cert-manager/applications/${application.id}/identities/${identityId}`,+ { role: ResourceMembershipRole.Operator }+ );+ expect(appIdentityRes.statusCode).toBe(200);++ const signerUserRes = await asSubOrg("POST", `/api/v1/cert-manager/signers/${signer.id}/users`, {+ userIds: [userId]+ });+ expect(signerUserRes.statusCode).toBe(200);+ const signerIdentityRes = await asSubOrg("POST", `/api/v1/cert-manager/signers/${signer.id}/identities`, {+ identityId+ });+ expect(signerIdentityRes.statusCode).toBe(200);++ const resourceRows = (actor: { actorUserId?: string; actorIdentityId?: string }) =>+ testDb(TableName.Membership).where({ scope: RESOURCE_SCOPE, scopeProjectId: certProjectId, ...actor });+ expect(await resourceRows({ actorUserId: userId })).toHaveLength(2);+ expect(await resourceRows({ actorIdentityId: identityId })).toHaveLength(2);+ });+test("removing a member from the instance admin panel tears down their access like org member removal", async () => {const groupId = await createLinkedGroup("reconcile-admin-panel");const { userId } = await createRootUserWithProjects("reconcile-admin-panel-user", [groupId]);diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.tsindex d999d117ee..e14d0b8053 100644--- a/backend/src/server/routes/index.ts+++ b/backend/src/server/routes/index.ts@@ -4498,7 +4498,8 @@ export const registerRoutes = async (membershipDAL,membershipRoleDAL,hsmConnectorService,- licenseService+ licenseService,+ orgDAL});const signerAutoRenewalQueue = signerAutoRenewalQueueFactory({diff --git a/backend/src/services/org/org-access-reconcile-consumer.ts b/backend/src/services/org/org-access-reconcile-consumer.tsindex b6934dcb77..d9559a6f6a 100644--- a/backend/src/services/org/org-access-reconcile-consumer.ts+++ b/backend/src/services/org/org-access-reconcile-consumer.ts@@ -169,7 +169,7 @@ export const orgAccessReconcileConsumerFactory = ({try {await $reconcileOrg(orgId, scope);} catch (error) {- logger.error(error, `org-access-reconcile: failed for org ${orgId} (event ${id})`);+ logger.error(error, `org-access-reconcile: failed [orgId=${orgId}] [eventId=${id}]`);errors.push(`org ${orgId}: ${error instanceof Error ? error.message : "Unknown error"}`);}}diff --git a/backend/src/services/pki-application/pki-application-membership-service.ts b/backend/src/services/pki-application/pki-application-membership-service.tsindex 2d9172fcea..9144dd718d 100644--- a/backend/src/services/pki-application/pki-application-membership-service.ts+++ b/backend/src/services/pki-application/pki-application-membership-service.ts@@ -168,18 +168,26 @@ export const pkiApplicationMembershipServiceFactory = ({else if (actor.identityId) where.actorIdentityId = actor.identityId;else if (actor.groupId) where.actorGroupId = actor.groupId;- const orgMembership = await membershipDAL.find(where);- if (!orgMembership.length) {+ const [orgMembership] = await membershipDAL.find(where);+ if (orgMembership && !orgMembership.isActive) {throw new BadRequestError({- message: "The actor is not a member of this organization. Invite them to the organization first."+ message: "The actor is deactivated in this organization. Reactivate them before granting them access."});}- if (!orgMembership[0].isActive) {- throw new BadRequestError({- message: "The actor is deactivated in this organization. Reactivate them before granting them access."+ if (orgMembership) return;++ if (actor.userId || actor.identityId) {+ const withAccess = await orgDAL.findActorsWithActiveOrgAccess({+ orgId,+ userIds: actor.userId ? [actor.userId] : [],+ identityIds: actor.identityId ? [actor.identityId] : []});+ if (withAccess.userIds.size || withAccess.identityIds.size) return;}- return orgMembership[0];++ throw new BadRequestError({+ message: "The actor is not a member of this organization. Invite them to the organization first."+ });};const $loadResourcePermission = (@@ -231,7 +239,7 @@ export const pkiApplicationMembershipServiceFactory = ({});}- const orgMembership = await $assertActorPresentInOrg(actorOrgId, { userId, identityId, groupId });+ await $assertActorPresentInOrg(actorOrgId, { userId, identityId, groupId });const membership = await membershipDAL.transaction(async (tx) => {const existingAppMembershipFilter: Record<string, unknown> = {@@ -287,7 +295,7 @@ export const pkiApplicationMembershipServiceFactory = ({const newMembership = await membershipDAL.create({scope: RESOURCE_SCOPE,- scopeOrgId: orgMembership.scopeOrgId,+ scopeOrgId: actorOrgId,scopeProjectId: projectId,actorUserId: userId ?? null,actorIdentityId: identityId ?? null,diff --git a/backend/src/services/signer-membership/signer-membership-service.ts b/backend/src/services/signer-membership/signer-membership-service.tsindex 1caf00b867..4e73ad1544 100644--- a/backend/src/services/signer-membership/signer-membership-service.ts+++ b/backend/src/services/signer-membership/signer-membership-service.ts@@ -139,18 +139,26 @@ export const signerMembershipServiceFactory = ({else if (actor.identityId) where.actorIdentityId = actor.identityId;else if (actor.groupId) where.actorGroupId = actor.groupId;- const orgMembership = await membershipDAL.find(where);- if (!orgMembership.length) {+ const [orgMembership] = await membershipDAL.find(where);+ if (orgMembership && !orgMembership.isActive) {throw new BadRequestError({- message: "The actor is not a member of this organization. Invite them to the organization first."+ message: "The actor is deactivated in this organization. Reactivate them before granting them access."});}- if (!orgMembership[0].isActive) {- throw new BadRequestError({- message: "The actor is deactivated in this organization. Reactivate them before granting them access."+ if (orgMembership) return;++ if (actor.userId || actor.identityId) {+ const withAccess = await orgDAL.findActorsWithActiveOrgAccess({+ orgId,+ userIds: actor.userId ? [actor.userId] : [],+ identityIds: actor.identityId ? [actor.identityId] : []});+ if (withAccess.userIds.size || withAccess.identityIds.size) return;}- return orgMembership[0];++ throw new BadRequestError({+ message: "The actor is not a member of this organization. Invite them to the organization first."+ });};const $loadResourcePermission = (@@ -203,7 +211,7 @@ export const signerMembershipServiceFactory = ({throw new BadRequestError({ message: unknownSignerRoleMessage(role) });}- const orgMembership = await $assertActorPresentInOrg(actorOrgId, { userId, identityId, groupId });+ await $assertActorPresentInOrg(actorOrgId, { userId, identityId, groupId });const membership = await membershipDAL.transaction(async (tx) => {const existingSignerMembershipFilter: Record<string, unknown> = {@@ -249,7 +257,7 @@ export const signerMembershipServiceFactory = ({const newMembership = await membershipDAL.create({scope: RESOURCE_SCOPE,- scopeOrgId: orgMembership.scopeOrgId,+ scopeOrgId: actorOrgId,scopeProjectId: projectId,actorUserId: userId ?? null,actorIdentityId: identityId ?? null,diff --git a/backend/src/services/signer/signer-service.ts b/backend/src/services/signer/signer-service.tsindex ffa73b9cda..fada7f54c7 100644--- a/backend/src/services/signer/signer-service.ts+++ b/backend/src/services/signer/signer-service.ts@@ -35,6 +35,8 @@ import { DatabaseErrorCode } from "@app/lib/error-codes";import { BadRequestError, ConflictError, DatabaseError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";import { logger } from "@app/lib/logger";import { THsmConnectorServiceFactory } from "@app/services/hsm-connector/hsm-connector-service";+import { lockAndAssertActiveOrgAccess } from "@app/services/org/org-access-fns";+import { TOrgDALFactory } from "@app/services/org/org-dal";import {TApprovalPolicyDALFactory,@@ -198,6 +200,7 @@ type TSignerServiceFactoryDep = {"create" | "find" | "delete" | "transaction" | "findResourceMembershipsForActor">;membershipRoleDAL: Pick<TMembershipRoleDALFactory, "create" | "delete">;+ orgDAL: Pick<TOrgDALFactory, "lockActorsForOrgAccess" | "findActorsWithActiveOrgAccess">;licenseService: Pick<TLicenseServiceFactory, "getPlan">;};@@ -370,7 +373,8 @@ export const signerServiceFactory = ({membershipDAL,membershipRoleDAL,hsmConnectorService,- licenseService+ licenseService,+ orgDAL}: TSignerServiceFactoryDep) => {const hsmCertIssuanceDeps = {certificateAuthorityDAL,@@ -916,6 +920,11 @@ export const signerServiceFactory = ({});}}++ await lockAndAssertActiveOrgAccess(+ { orgDAL, orgId: dto.actorOrgId, userIds: pendingUserIds, identityIds: pendingIdentityIds },+ tx+ );}for (const member of pendingMembers) {