Skip to content

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

fix(pam): stop the edit form seeding defaults over stored details

bernie-g29d7dc4460c8
diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
index e333c441dc..55f97f6130 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
@@ -1070,6 +1070,7 @@ export const gatewayV2ServiceFactory = ({
capabilities?: {
pkcs11?: boolean;
sessionLogMaskingBuiltInDetection?: boolean;
+ clickhouseNativeProtocol?: boolean;
supported_account_types?: string[];
};
}) => {
diff --git a/backend/src/ee/services/pam-account-heartbeat/pam-account-heartbeat-service.test.ts b/backend/src/ee/services/pam-account-heartbeat/pam-account-heartbeat-service.test.ts
index 64ef29c32a..a32a0d56d4 100644
--- a/backend/src/ee/services/pam-account-heartbeat/pam-account-heartbeat-service.test.ts
+++ b/backend/src/ee/services/pam-account-heartbeat/pam-account-heartbeat-service.test.ts
@@ -23,13 +23,13 @@ vi.mock("@app/ee/services/gateway-v2/gateway-v2-fns", () => ({
testBuiltConnectionWithGateway: (...args: unknown[]) => gatewayBuiltTestConnection(...args)
}));
-// The heartbeat takes the built test whole; unwrap it so the existing per-argument expectations still apply.
-gatewayBuiltTestConnection.mockImplementation(
- (built: unknown, gatewayId: unknown, service: unknown, timeoutMs: unknown, signal: unknown) => {
- const t = built as { host: string; port: number; request: unknown; additionalPorts?: number[] };
- return gatewayTestConnection(t.host, t.port, gatewayId, service, t.request, timeoutMs, signal, t.additionalPorts);
- }
-);
+const builtTest = (call = 0) =>
+ gatewayBuiltTestConnection.mock.calls[call][0] as {
+ host: string;
+ port: number;
+ request: Record<string, unknown>;
+ additionalPorts?: number[];
+ };
const blobOf = (data: Record<string, unknown>) => Buffer.from(JSON.stringify(data));
@@ -105,7 +105,7 @@ describe("heartbeat: Windows", () => {
const result = await service.checkScheduledAccount("acc-win");
expect(testCredential).toHaveBeenCalledTimes(1);
- expect(gatewayTestConnection).not.toHaveBeenCalled();
+ expect(gatewayBuiltTestConnection).not.toHaveBeenCalled();
expect(result?.status).toBe(PamHeartbeatStatus.Healthy);
expect(updateById).toHaveBeenCalledWith(
"acc-win",
@@ -199,8 +199,8 @@ describe("heartbeat: SSH certificate", () => {
});
test("mints a short-lived certificate and logs in with it", async () => {
- gatewayTestConnection.mockReset();
- gatewayTestConnection.mockResolvedValue({ ok: true });
+ gatewayBuiltTestConnection.mockReset();
+ gatewayBuiltTestConnection.mockResolvedValue({ ok: true });
const { SshCertKeyAlgorithm, createSshKeyPair } = await import("@app/lib/ssh");
const { publicKey, privateKey } = await createSshKeyPair(SshCertKeyAlgorithm.ED25519);
@@ -231,7 +231,7 @@ describe("heartbeat: SSH certificate", () => {
const result = await service.checkScheduledAccount("acc-ssh");
expect(result?.status).toBe(PamHeartbeatStatus.Healthy);
- const [, , , , request] = gatewayTestConnection.mock.calls[0];
+ const { request } = builtTest();
expect(request).toMatchObject({ mode: "ssh", authMethod: PamSshAuthMethod.Certificate, username: "ubuntu" });
expect((request as { certificate?: string }).certificate).toBeTruthy();
});
diff --git a/backend/src/ee/services/pam-account/pam-account-connection-test.test.ts b/backend/src/ee/services/pam-account/pam-account-connection-test.test.ts
index d35721dc14..75b2f02b68 100644
--- a/backend/src/ee/services/pam-account/pam-account-connection-test.test.ts
+++ b/backend/src/ee/services/pam-account/pam-account-connection-test.test.ts
@@ -272,6 +272,9 @@ describe("buildGatewayConnectionTest: ClickHouse", () => {
sslEnabled: false
});
expect(result?.port).toBe(8123);
+ // These are the ports the gateway's certificate authorises, so a probe it is asked to run is refused
+ // without them.
+ expect(result?.additionalPorts).toEqual([8123, 9000]);
});
test("a native-only account targets the native port and sends no HTTP port", async () => {
@@ -291,6 +294,7 @@ describe("buildGatewayConnectionTest: ClickHouse", () => {
expect(result?.request.mode).toBe(TestConnectionMode.ClickHouse);
expect(result?.port).toBe(9440);
expect(result?.request).toMatchObject({ nativePort: 9440, sslEnabled: true });
+ expect(result?.additionalPorts).toEqual([9440]);
expect(wireKeys(result!.request)).not.toContain("httpPort");
});
@@ -304,6 +308,7 @@ describe("buildGatewayConnectionTest: ClickHouse", () => {
expect(result?.request.mode).toBe(TestConnectionMode.ClickHouse);
expect(result?.request).toMatchObject({ httpPort: 8123 });
+ expect(result?.additionalPorts).toEqual([8123]);
expect(wireKeys(result!.request)).not.toContain("nativePort");
});
});
diff --git a/backend/src/ee/services/pam-account/pam-account-schemas.ts b/backend/src/ee/services/pam-account/pam-account-schemas.ts
index 37dfaa2d4b..8e7d8692a5 100644
--- a/backend/src/ee/services/pam-account/pam-account-schemas.ts
+++ b/backend/src/ee/services/pam-account/pam-account-schemas.ts
@@ -616,6 +616,10 @@ export const ACCOUNT_TYPE_CONFIGS = {
.refine((v) => v.port !== undefined || v.nativePort !== undefined, {
message: "Set the HTTP port, the native port, or both",
path: ["port"]
+ })
+ .refine((v) => v.port === undefined || v.port !== v.nativePort, {
+ message: "The HTTP and native ports must differ",
+ path: ["nativePort"]
}),
credentials: z.object({
username: z.string().trim().min(1).max(255),
@@ -631,8 +635,7 @@ export const ACCOUNT_TYPE_CONFIGS = {
},
nativePort: {
label: "Native Port",
- // No default: the edit form seeds defaults over stored details, so every existing HTTP-only
- // account would silently gain a native port on its next save.
+ // No default: a native port is the server operator's choice, not something a new account assumes.
tooltip:
"The native TCP port, usually 9000 for plain TCP or 9440 with SSL. clickhouse-client and other native drivers need it. Leave it empty if the server only serves HTTP."
},
diff --git a/backend/src/ee/services/pam-web-access/pam-web-access-service.ts b/backend/src/ee/services/pam-web-access/pam-web-access-service.ts
index f915d95cae..3f4ef4c302 100644
--- a/backend/src/ee/services/pam-web-access/pam-web-access-service.ts
+++ b/backend/src/ee/services/pam-web-access/pam-web-access-service.ts
@@ -186,14 +186,6 @@ export const pamWebAccessServiceFactory = ({
throw new BadRequestError({ message: "Web access is not supported for this account type" });
}
- const webAccessBlocked = webAccessUnavailableReason(
- account.accountType as PamAccountType,
- await decrypt(account.projectId, account.encryptedConnectionDetails)
- );
- if (webAccessBlocked) {
- throw new BadRequestError({ message: webAccessBlocked });
- }
-
const policy = resolveAccessControls(account.templatePolicies);
const { requiresApproval } = policy;
@@ -238,6 +230,12 @@ export const pamWebAccessServiceFactory = ({
enforceRecordingConfig(account);
const connectionDetails = await decrypt(projectId, account.encryptedConnectionDetails);
+
+ const webAccessBlocked = webAccessUnavailableReason(account.accountType as PamAccountType, connectionDetails);
+ if (webAccessBlocked) {
+ throw new BadRequestError({ message: webAccessBlocked });
+ }
+
const resolvedHost = resolveSelectedHost(account.accountType as PamAccountType, connectionDetails, selectedHost);
const trimmedReason = reason?.trim() || null;
diff --git a/backend/src/ee/services/pam/CLAUDE.md b/backend/src/ee/services/pam/CLAUDE.md
index c43cc64410..287be02f7f 100644
--- a/backend/src/ee/services/pam/CLAUDE.md
+++ b/backend/src/ee/services/pam/CLAUDE.md
@@ -138,11 +138,17 @@ live in a process-wide map keyed by the driver's request id rather than on the p
test compares the login's `sessionInfo` against what was asked for, because Snowflake accepts a warehouse
or role the credential can't use and silently leaves it unset.
-**ClickHouse is brokered over its HTTP interface (8123/8443), never the native protocol on 9000**, so the
-gateway can read the statement as text to block and record it (CLI `packages/pam/handlers/clickhouse/`).
-Only the first megabyte of a request body is inspected, so an account carrying a command-blocking policy
-refuses a body longer than that rather than forward the remainder unread. Databases stand in for schemas, and the explorer grid is read-only for every table
-(`supportsRowEditing`): `is_in_primary_key` is a sorting key, not a unique constraint.
+**ClickHouse is brokered over both its interfaces, HTTP (8123/8443) and the native TCP protocol
+(9000/9440)**, and an account may carry either port or both. One local port serves both: the gateway
+routes a connection by its first byte (CLI `packages/pam/handlers/clickhouse/`). Over HTTP the statement
+is read as text, and only the first megabyte of a request body is inspected, so an account carrying a
+command-blocking policy refuses a longer body rather than forward the remainder unread. Over native the
+statement arrives in its own Query packet; a packet the gateway cannot parse ends the session rather than
+being relayed uninspected. Two consequences worth knowing: **Web Access needs the HTTP port**, because the
+browser client speaks HTTP and nothing translates for it (`webAccessUnavailableReason`), and a native port
+may only be saved against a gateway advertising the `clickhouseNativeProtocol` capability. Databases stand
+in for schemas, and the explorer grid is read-only for every table (`supportsRowEditing`):
+`is_in_primary_key` is a sorting key, not a unique constraint.
## Policies & Settings
diff --git a/docs/documentation/platform/pam/accounts/clickhouse.mdx b/docs/documentation/platform/pam/accounts/clickhouse.mdx
index cd0f0a71ca..2cd7da4fa9 100644
--- a/docs/documentation/platform/pam/accounts/clickhouse.mdx
+++ b/docs/documentation/platform/pam/accounts/clickhouse.mdx
@@ -15,7 +15,7 @@ Either interface can be turned off on the server. Set whichever ports it serves
- A ClickHouse user for Infisical to connect as.
- At least one of the two interfaces enabled on the target. Both are on by default in a stock ClickHouse configuration: `http_port` is 8123 and `tcp_port` is 9000. The TLS ports are `https_port` 8443 and `tcp_port_secure` 9440, and ClickHouse Cloud exposes both.
- Both interfaces share the same `<openSSL><server>` certificate, so one SSL setting covers them. A server with TLS on one interface and not the other isn't supported.
-- A [gateway](/documentation/platform/gateways/overview) that can reach those ports. A gateway that predates ClickHouse support refuses the session with a version error.
+- A [gateway](/documentation/platform/gateways/overview) that can reach those ports. Saving the account fails if the gateway predates ClickHouse support, and setting a **Native Port** fails if it predates native-protocol support. Update the gateway, or leave the native port empty to use HTTP only.
## Creating an account
@@ -60,7 +60,7 @@ GRANT SELECT ON analytics.* TO pam_service;
Every session opens on the account's **Database**, whatever a client asks for. That is a starting point, not a limit: a session can still read any database the user has a grant on, and Web Access lists them all. Scope the grants, not the **Database** field.
-Sessions connect with the user's default roles. The gateway drops the `role` URL parameter, which would otherwise activate a role with no statement to record, so use `SET ROLE` instead and it's recorded like any other statement.
+Sessions connect with the user's default roles. Over HTTP the gateway drops the `role` URL parameter, which would otherwise activate a role with no statement to record, so use `SET ROLE` instead and it's recorded like any other statement.
Command blocking matches the text of each statement, so a statement that builds SQL at runtime can run something a pattern would otherwise reject. Use ClickHouse grants for restrictions that have to hold.
@@ -133,7 +133,7 @@ A server that serves only the native protocol works with native clients. Set the
`clickhouse-client`, Python's `clickhouse-driver` and `clickhouse-go` speak to the session's port directly and need nothing special.
-**Everything that speaks HTTP needs the server to have an HTTP port.** That includes Web Access, JDBC tools such as DBeaver and DataGrip, `clickhouse-connect` and ODBC. Web Access is greyed out on such an account, and the other clients fail when they connect. Give the account an HTTP port if you need any of them.
+Nothing translates HTTP into the native protocol, so every HTTP client needs the server to have an HTTP port. That includes Web Access, JDBC tools such as DBeaver and DataGrip, `clickhouse-connect` and ODBC. Web Access is greyed out on such an account, and the other clients fail when they connect. Give the account an HTTP port if you need any of them.
## Next steps
diff --git a/frontend/src/pages/pam/PamAccountsPage/components/EditAccountForm.tsx b/frontend/src/pages/pam/PamAccountsPage/components/EditAccountForm.tsx
index 609e168c33..d9dcc3198f 100644
--- a/frontend/src/pages/pam/PamAccountsPage/components/EditAccountForm.tsx
+++ b/frontend/src/pages/pam/PamAccountsPage/components/EditAccountForm.tsx
@@ -28,9 +28,10 @@ import { SheetSaveBar } from "../../components/SheetSaveBar";
import {
accountFormSchema,
applyServerValidationErrors,
- buildDefaultFieldValues,
buildEditCredentialValues,
+ buildEditFieldValues,
getMissingRequiredFields,
+ omitUnsetAbsentFields,
TAccountFormValues
} from "./accountFormSchema";
import { ConnectionDetailsForm } from "./ConnectionDetailsForm";
@@ -109,10 +110,10 @@ export const EditAccountForm = ({ accountId, onDirtyChange }: Props) => {
description: account.description ?? "",
folderId: account.folderId,
templateId: account.templateId,
- connectionDetails: {
- ...buildDefaultFieldValues(metadata.connectionFields),
- ...account.connectionDetails
- },
+ connectionDetails: buildEditFieldValues(
+ metadata.connectionFields,
+ account.connectionDetails
+ ),
credentials: buildEditCredentialValues(metadata.credentialFields, account.credentials)
});
}
@@ -166,7 +167,10 @@ export const EditAccountForm = ({ accountId, onDirtyChange }: Props) => {
description: values.description || null,
folderId: values.folderId,
templateId: values.templateId,
- connectionDetails: values.connectionDetails,
+ connectionDetails: omitUnsetAbsentFields(
+ values.connectionDetails,
+ account.connectionDetails
+ ),
...(filteredCredentials ? { credentials: filteredCredentials } : {})
},
{
diff --git a/frontend/src/pages/pam/PamAccountsPage/components/accountFormSchema.test.ts b/frontend/src/pages/pam/PamAccountsPage/components/accountFormSchema.test.ts
new file mode 100644
index 0000000000..0637f5ca36
--- /dev/null
+++ b/frontend/src/pages/pam/PamAccountsPage/components/accountFormSchema.test.ts
@@ -0,0 +1,48 @@
+import { describe, expect, it } from "vitest";
+
+import { PamFieldWidget, TPamFieldDescriptor } from "@app/hooks/api/pam";
+
+import {
+ buildDefaultFieldValues,
+ buildEditFieldValues,
+ omitUnsetAbsentFields
+} from "./accountFormSchema";
+
+const field = (overrides: Partial<TPamFieldDescriptor> & { key: string }): TPamFieldDescriptor =>
+ ({ label: overrides.key, widget: PamFieldWidget.Text, ...overrides }) as TPamFieldDescriptor;
+
+describe("buildEditFieldValues", () => {
+ it("keeps a stored value rather than the descriptor default", () => {
+ const fields = [field({ key: "port", widget: PamFieldWidget.Number, defaultValue: 8123 })];
+ expect(buildEditFieldValues(fields, { port: 9000 })).toEqual({ port: 9000 });
+ });
+
+ it("leaves an absent field empty rather than seeding the descriptor default", () => {
+ const fields = [field({ key: "port", widget: PamFieldWidget.Number, defaultValue: 8123 })];
+ expect(buildEditFieldValues(fields, { nativePort: 9000 })).toEqual({ port: "" });
+ });
+
+ it("still seeds descriptor defaults on the create path", () => {
+ const fields = [field({ key: "port", widget: PamFieldWidget.Number, defaultValue: 8123 })];
+ expect(buildDefaultFieldValues(fields)).toEqual({ port: 8123 });
+ });
+});
+
+describe("omitUnsetAbsentFields", () => {
+ it("omits a field the account never stored so a schema default can apply", () => {
+ expect(omitUnsetAbsentFields({ host: "h", winrmPort: "" }, { host: "h" })).toEqual({
+ host: "h"
+ });
+ });
+
+ it("sends an empty string for a stored field the user cleared", () => {
+ expect(omitUnsetAbsentFields({ port: "" }, { port: 8123 })).toEqual({ port: "" });
+ });
+
+ it("leaves non-empty values alone", () => {
+ expect(omitUnsetAbsentFields({ port: 9000, ssl: false }, {})).toEqual({
+ port: 9000,
+ ssl: false
+ });
+ });
+});
diff --git a/frontend/src/pages/pam/PamAccountsPage/components/accountFormSchema.ts b/frontend/src/pages/pam/PamAccountsPage/components/accountFormSchema.ts
index d8eada7e40..b07d22d040 100644
--- a/frontend/src/pages/pam/PamAccountsPage/components/accountFormSchema.ts
+++ b/frontend/src/pages/pam/PamAccountsPage/components/accountFormSchema.ts
@@ -24,16 +24,38 @@ export const accountFormSchema = z.object({
export type TAccountFormValues = z.infer<typeof accountFormSchema>;
-const defaultForField = (field: TPamFieldDescriptor): unknown => {
- if (field.defaultValue !== undefined) return field.defaultValue;
+const emptyForField = (field: TPamFieldDescriptor): unknown => {
if (field.widget === PamFieldWidget.Boolean) return false;
if (field.widget === PamFieldWidget.Select) return field.options?.[0]?.value ?? "";
return "";
};
+const defaultForField = (field: TPamFieldDescriptor): unknown =>
+ field.defaultValue !== undefined ? field.defaultValue : emptyForField(field);
+
export const buildDefaultFieldValues = (fields: TPamFieldDescriptor[]): Record<string, unknown> =>
Object.fromEntries(fields.map((field) => [field.key, defaultForField(field)]));
+// A metadata default is a new-account convenience. The edit form submits every field, so seeding
+// one over a key the account does not store would silently give it a value it never had.
+export const buildEditFieldValues = (
+ fields: TPamFieldDescriptor[],
+ existing: Record<string, unknown> = {}
+): Record<string, unknown> =>
+ Object.fromEntries(
+ fields.map((field) => [field.key, existing[field.key] ?? emptyForField(field)])
+ );
+
+// A field the account never stored must reach the backend as absent, not as "". Zod's `.default()` fires on
+// undefined and rejects an empty string, so submitting "" blocks the save on a field the user never touched.
+export const omitUnsetAbsentFields = (
+ values: Record<string, unknown>,
+ existing: Record<string, unknown> = {}
+): Record<string, unknown> =>
+ Object.fromEntries(
+ Object.entries(values).filter(([key, value]) => value !== "" || existing[key] !== undefined)
+ );
+
export const buildEditCredentialValues = (
fields: TPamFieldDescriptor[],
existing: Record<string, unknown> = {}
@@ -41,7 +63,7 @@ export const buildEditCredentialValues = (
Object.fromEntries(
fields.map((field) => {
if (field.secret) return [field.key, UNCHANGED_PASSWORD_SENTINEL];
- return [field.key, existing[field.key] ?? defaultForField(field)];
+ return [field.key, existing[field.key] ?? emptyForField(field)];
})
);
diff --git a/frontend/src/pages/pam/components/LaunchSessionSheet.tsx b/frontend/src/pages/pam/components/LaunchSessionSheet.tsx
index b354a10cff..fe760f0d32 100644
--- a/frontend/src/pages/pam/components/LaunchSessionSheet.tsx
+++ b/frontend/src/pages/pam/components/LaunchSessionSheet.tsx
@@ -51,9 +51,9 @@ const LaunchTab = ({
}) => {
const { currentOrg } = useOrganization();
const browserDisabled = Boolean(webAccessUnavailableReason);
- const [method, setMethod] = useState<LaunchMethod>(
- supportsWebAccess && !browserDisabled ? "browser" : "cli"
- );
+ const [method, setMethod] = useState<LaunchMethod>(supportsWebAccess ? "browser" : "cli");
+ // The reason arrives with the account, after this state is seeded, so the render has to re-decide.
+ const effectiveMethod: LaunchMethod = browserDisabled ? "cli" : method;
const needsHost = hosts.length > 1;
const [selectedHost, setSelectedHost] = useState<string | undefined>(
@@ -100,15 +100,11 @@ const LaunchTab = ({
<div>
<p className="mb-3 text-sm font-medium text-foreground">Launch method</p>
<RadioGroup
- value={method}
+ value={effectiveMethod}
onValueChange={(value) => setMethod(value as LaunchMethod)}
className="grid-cols-2 gap-3"
>
- <FieldLabel
- htmlFor="launch-browser"
- variant="pam"
- className={browserDisabled ? "cursor-not-allowed opacity-50" : undefined}
- >
+ <FieldLabel htmlFor="launch-browser" variant="pam">
<Field orientation="horizontal" className="items-center gap-3">
<Globe className="size-5 shrink-0 text-foreground" />
<div className="flex-1 text-left">
@@ -141,7 +137,7 @@ const LaunchTab = ({
</div>
)}
- {method === "browser" && (
+ {effectiveMethod === "browser" && (
<div>
<p className="mb-2.5 text-sm text-foreground">Launch browser client</p>
{hostMissing ? (
@@ -169,7 +165,7 @@ const LaunchTab = ({
</div>
)}
- {method === "cli" && (
+ {effectiveMethod === "cli" && (
<div>
<p className="mb-2.5 text-sm text-foreground">Run this command</p>
<div className="flex items-center justify-between gap-3 rounded-md border border-border bg-container px-4 py-3">