Skip to content

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

fix(pam): bootstrap PAM project on UI entry via GET /pam/project

bernie-g6378c078d745
diff --git a/backend/src/ee/routes/v1/pam-routers/index.ts b/backend/src/ee/routes/v1/pam-routers/index.ts
index 8294ee96b5..7acc04f40f 100644
--- a/backend/src/ee/routes/v1/pam-routers/index.ts
+++ b/backend/src/ee/routes/v1/pam-routers/index.ts
@@ -1,6 +1,7 @@
import { registerPamAccountRouter } from "./pam-account-router";
import { registerPamAccountTemplateRouter } from "./pam-account-template-router";
import { registerPamFolderRouter } from "./pam-folder-router";
+import { registerPamProjectRouter } from "./pam-project-router";
import {
registerPamAccountMembershipRouter,
registerPamFolderMembershipRouter,
@@ -11,6 +12,7 @@ import { registerPamSessionChunkRouter } from "./pam-session-chunk-router";
import { registerPamSessionRouter, registerPamWebAccessRouter } from "./pam-session-router";
export const registerPamRouters = async (server: FastifyZodProvider) => {
+ await server.register(registerPamProjectRouter, { prefix: "/project" });
await server.register(registerPamAccountTemplateRouter, { prefix: "/account-templates" });
await server.register(registerPamFolderRouter, { prefix: "/folders" });
await server.register(registerPamFolderMembershipRouter, { prefix: "/folders" });
diff --git a/backend/src/ee/routes/v1/pam-routers/pam-project-router.ts b/backend/src/ee/routes/v1/pam-routers/pam-project-router.ts
new file mode 100644
index 0000000000..c6f41636b1
--- /dev/null
+++ b/backend/src/ee/routes/v1/pam-routers/pam-project-router.ts
@@ -0,0 +1,28 @@
+import z from "zod";
+
+import { ApiDocsTags } from "@app/lib/api-docs/constants";
+import { readLimit } from "@app/server/config/rateLimiter";
+import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
+import { AuthMode } from "@app/services/auth/auth-type";
+
+export const registerPamProjectRouter = async (server: FastifyZodProvider) => {
+ server.route({
+ method: "GET",
+ url: "/",
+ schema: {
+ operationId: "getPamProject",
+ description: "Resolve the organization's Privileged Access Manager project, creating it on first access",
+ tags: [ApiDocsTags.PamFolders],
+ response: {
+ 200: z.object({
+ projectId: z.string()
+ })
+ }
+ },
+ config: { rateLimit: readLimit },
+ onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
+ // injectPamProjectId (preValidation) resolves and lazily bootstraps the project, so by the time
+ // this handler runs internalPamProjectId is always set.
+ handler: async (req) => ({ projectId: req.internalPamProjectId })
+ });
+};
diff --git a/frontend/src/hooks/api/pam/queries.tsx b/frontend/src/hooks/api/pam/queries.tsx
index d806116955..f3ea1665b1 100644
--- a/frontend/src/hooks/api/pam/queries.tsx
+++ b/frontend/src/hooks/api/pam/queries.tsx
@@ -30,6 +30,12 @@ import {
TPamSession
} from "./types";
+// Resolves the org's PAM project, creating it on first access (lazy bootstrap on the backend).
+export const fetchPamProjectId = async () => {
+ const { data } = await apiRequest.get<{ projectId: string }>("/api/v1/pam/project");
+ return data.projectId;
+};
+
export const pamKeys = {
all: ["pam"] as const,
account: () => [...pamKeys.all, "account"] as const,
diff --git a/frontend/src/pages/pam/layout.tsx b/frontend/src/pages/pam/layout.tsx
index 259b9d5d90..d78cae3f7c 100644
--- a/frontend/src/pages/pam/layout.tsx
+++ b/frontend/src/pages/pam/layout.tsx
@@ -2,8 +2,9 @@ import { createFileRoute, redirect } from "@tanstack/react-router";
import { BreadcrumbTypes } from "@app/components/v2";
import { projectKeys } from "@app/hooks/api";
-import { organizationKeys } from "@app/hooks/api/organization/queries";
+import { fetchOrganizationById, organizationKeys } from "@app/hooks/api/organization/queries";
import { Organization } from "@app/hooks/api/organization/types";
+import { fetchPamProjectId } from "@app/hooks/api/pam/queries";
import { fetchProjectById } from "@app/hooks/api/projects/queries";
import { fetchUserProjectPermissions, roleQueryKeys } from "@app/hooks/api/roles/queries";
import { PamLayout } from "@app/layouts/PamLayout";
@@ -18,7 +19,19 @@ export const Route = createFileRoute(
organizationKeys.getOrgById(params.orgId)
);
- const pamProjectId = org?.pamProjectId;
+ let pamProjectId = org?.pamProjectId;
+ if (!pamProjectId) {
+ // The org has no consolidated PAM project yet (lazy creation). Hitting the PAM endpoint
+ // bootstraps it server-side; refetch the org so pamProjectId is populated for the contexts
+ // that read it (ProjectContext / ProjectPermissionContext).
+ pamProjectId = await fetchPamProjectId();
+ const refreshedOrg = await context.queryClient.fetchQuery({
+ queryKey: organizationKeys.getOrgById(params.orgId),
+ queryFn: () => fetchOrganizationById(params.orgId)
+ });
+ pamProjectId = refreshedOrg?.pamProjectId ?? pamProjectId;
+ }
+
if (!pamProjectId) {
throw redirect({
to: "/organizations/$orgId/projects",