Skip to content

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

move ssh ca info to encrypted internal metadata

x0322051f8081083505
diff --git a/backend/src/db/migrations/20260603030053_pam-revamp.ts b/backend/src/db/migrations/20260603030053_pam-revamp.ts
index 35d90b0a52..0f755b6d87 100644
--- a/backend/src/db/migrations/20260603030053_pam-revamp.ts
+++ b/backend/src/db/migrations/20260603030053_pam-revamp.ts
@@ -402,10 +402,6 @@ export async function up(knex: Knex): Promise<void> {
t.uuid("templateId").notNullable().alter();
t.binary("encryptedConnectionDetails").notNullable().alter();
t.unique(["folderId", "name"]);
- // SSH CA for certificate-based auth
- t.binary("encryptedCaPrivateKey").nullable();
- t.text("caPublicKey").nullable();
- t.string("caKeyAlgorithm").nullable();
// Make retained old columns nullable so new accounts can be created without them
t.string("projectId", 36).nullable().alter();
t.uuid("resourceId").nullable().alter();
diff --git a/backend/src/db/schemas/pam-accounts.ts b/backend/src/db/schemas/pam-accounts.ts
index 9f52974121..1a8ae71a19 100644
--- a/backend/src/db/schemas/pam-accounts.ts
+++ b/backend/src/db/schemas/pam-accounts.ts
@@ -32,10 +32,7 @@ export const PamAccountsSchema = z.object({
encryptedInternalMetadata: zodBuffer.nullable().optional(),
gatewayId: z.string().uuid().nullable().optional(),
gatewayPoolId: z.string().uuid().nullable().optional(),
- recordingConnectionId: z.string().uuid().nullable().optional(),
- encryptedCaPrivateKey: zodBuffer.nullable().optional(),
- caPublicKey: z.string().nullable().optional(),
- caKeyAlgorithm: z.string().nullable().optional()
+ recordingConnectionId: z.string().uuid().nullable().optional()
});
export type TPamAccounts = z.infer<typeof PamAccountsSchema>;
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 7fec731a1d..657c013abf 100644
--- a/backend/src/ee/services/pam-account/pam-account-schemas.ts
+++ b/backend/src/ee/services/pam-account/pam-account-schemas.ts
@@ -39,9 +39,19 @@ const ACCOUNT_TYPE_CONFIGS = {
privateKey: z.string().trim().min(1).max(5000)
}),
z.object({ authMethod: z.literal("certificate"), username: z.string().trim().min(1) })
- ])
+ ]),
+ internalMetadata: z.object({
+ caPrivateKey: z.string(),
+ caPublicKey: z.string(),
+ caKeyAlgorithm: z.string()
+ })
}
-} as const satisfies Partial<Record<PamAccountType, { connectionDetails: z.ZodTypeAny; credentials: z.ZodTypeAny }>>;
+} as const satisfies Partial<
+ Record<
+ PamAccountType,
+ { connectionDetails: z.ZodTypeAny; credentials: z.ZodTypeAny; internalMetadata?: z.ZodTypeAny }
+ >
+>;
type TSupportedAccountType = keyof typeof ACCOUNT_TYPE_CONFIGS;
@@ -66,3 +76,13 @@ export const validateCredentials = (accountType: PamAccountType, data: unknown)
};
export { ACCOUNT_TYPE_CONFIGS };
+
+export type TSshInternalMetadata = z.infer<(typeof ACCOUNT_TYPE_CONFIGS)[PamAccountType.SSH]["internalMetadata"]>;
+
+export const parseInternalMetadata = (accountType: PamAccountType, data: unknown): TSshInternalMetadata | null => {
+ if (accountType === PamAccountType.SSH) {
+ const result = ACCOUNT_TYPE_CONFIGS[PamAccountType.SSH].internalMetadata.safeParse(data);
+ return result.success ? result.data : null;
+ }
+ return null;
+};
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 17a0d72ee5..4c980cbf71 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -18,6 +18,7 @@ import { KmsDataKey } from "@app/services/kms/kms-types";
import { TMembershipDALFactory } from "@app/services/membership/membership-dal";
import { TMembershipRoleDALFactory } from "@app/services/membership/membership-role-dal";
+import { PamAccountType } from "../pam/pam-enums";
import {
checkAccountAccess,
checkFolderPermission,
@@ -29,7 +30,12 @@ import { validateGatewayAttachment, validateRecordingConnection } from "../pam/p
import { TPamAccountTemplateDALFactory } from "../pam-account-template/pam-account-template-dal";
import { TPamFolderDALFactory } from "../pam-folder/pam-folder-dal";
import { TPamAccountDALFactory } from "./pam-account-dal";
-import { validateConnectionDetails, validateCredentials } from "./pam-account-schemas";
+import {
+ parseInternalMetadata,
+ type TSshInternalMetadata,
+ validateConnectionDetails,
+ validateCredentials
+} from "./pam-account-schemas";
import {
TCreatePamAccountDTO,
TDeletePamAccountDTO,
@@ -402,6 +408,11 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
};
};
+ const decryptInternalMetadata = async (projectId: string, blob: Buffer | null | undefined) => {
+ if (!blob) return null;
+ return decrypt(projectId, blob);
+ };
+
const getOrCreateSshCa = async ({ accountId, projectId, ...ctx }: TGetPamAccountDTO & TActorContext) => {
const account = await pamAccountDAL.findByIdWithDetails(accountId);
if (!account || account.projectId !== projectId) {
@@ -410,20 +421,26 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
await checkAccount(accountId, account.folderId, projectId, ResourcePermissionPamResourceActions.EditAccounts, ctx);
- if (account.caPublicKey) {
- return { publicKey: account.caPublicKey, created: false };
+ const existing = parseInternalMetadata(
+ account.accountType as PamAccountType,
+ await decryptInternalMetadata(projectId, account.encryptedInternalMetadata)
+ );
+
+ if (existing?.caPublicKey) {
+ return { publicKey: existing.caPublicKey, created: false };
}
const keyAlgorithm = SshCertKeyAlgorithm.ED25519;
const { publicKey, privateKey } = await createSshKeyPair(keyAlgorithm);
- const { encryptor } = await getProjectCipher(projectId);
- const encryptedCaPrivateKey = encryptor({ plainText: Buffer.from(privateKey) }).cipherTextBlob;
+ const metadata: TSshInternalMetadata = {
+ caPublicKey: publicKey,
+ caKeyAlgorithm: keyAlgorithm,
+ caPrivateKey: privateKey
+ };
await pamAccountDAL.updateById(accountId, {
- encryptedCaPrivateKey,
- caPublicKey: publicKey,
- caKeyAlgorithm: keyAlgorithm
+ encryptedInternalMetadata: await encrypt(projectId, metadata)
});
return { publicKey, created: true, keyAlgorithm };
@@ -434,10 +451,16 @@ export const pamAccountServiceFactory = (deps: TPamAccountServiceFactoryDep) =>
if (!account) {
throw new NotFoundError({ message: `Account with ID '${accountId}' not found` });
}
- if (!account.caPublicKey) {
+
+ const metadata = parseInternalMetadata(
+ account.accountType as PamAccountType,
+ await decryptInternalMetadata(account.projectId!, account.encryptedInternalMetadata)
+ );
+
+ if (!metadata?.caPublicKey) {
throw new BadRequestError({ message: "SSH CA has not been configured for this account" });
}
- return { publicKey: account.caPublicKey };
+ return { publicKey: metadata.caPublicKey };
};
return { list, listAccessible, getById, create, update, deleteAccount, getOrCreateSshCa, getSshCaPublicKey };
diff --git a/backend/src/ee/services/pam-session/pam-session-service.ts b/backend/src/ee/services/pam-session/pam-session-service.ts
index 8b7933de2b..84d878d505 100644
--- a/backend/src/ee/services/pam-session/pam-session-service.ts
+++ b/backend/src/ee/services/pam-session/pam-session-service.ts
@@ -8,7 +8,7 @@ import { KmsDataKey } from "@app/services/kms/kms-types";
import { TMembershipDALFactory } from "@app/services/membership/membership-dal";
import { TMembershipRoleDALFactory } from "@app/services/membership/membership-role-dal";
-import { PamSessionStatus } from "../pam/pam-enums";
+import { PamAccountType, PamSessionStatus } from "../pam/pam-enums";
import {
checkAccountAccess,
getViewSessionsResourceIds,
@@ -16,6 +16,7 @@ import {
verifyProductMembership
} from "../pam/pam-permission";
import { TPamAccountDALFactory } from "../pam-account/pam-account-dal";
+import { parseInternalMetadata } from "../pam-account/pam-account-schemas";
import { PamRecordingStorageBackend } from "../pam-session-recording/pam-recording-enums";
import { generateSessionRecordingSecrets } from "../pam-session-recording/pam-recording-secrets";
import { ResourcePermissionPamResourceActions } from "../permission/resource-permission";
@@ -120,30 +121,29 @@ export const pamSessionServiceFactory = ({
const connectionDetails = await decrypt(session.projectId, account.encryptedConnectionDetails);
const credentials = await decrypt(session.projectId, account.encryptedCredentials);
- if (credentials.authMethod === "certificate" && account.encryptedCaPrivateKey) {
- const { decryptor } = await kmsService.createCipherPairWithDataKey({
- type: KmsDataKey.SecretManager,
- projectId: session.projectId
- });
- const caPrivateKey = decryptor({
- cipherTextBlob: account.encryptedCaPrivateKey
- }).toString("utf-8");
-
- const keyAlgorithm = (account.caKeyAlgorithm as SshCertKeyAlgorithm) || SshCertKeyAlgorithm.ED25519;
- const { publicKey: clientPublicKey, privateKey: clientPrivateKey } = await createSshKeyPair(keyAlgorithm);
-
- const username = credentials.username as string;
- const { signedPublicKey } = await createSshCert({
- caPrivateKey,
- clientPublicKey,
- keyId: `pam-session-${session.id}`,
- principals: [username],
- requestedTtl: "8h",
- certType: SshCertType.USER
- });
+ if (credentials.authMethod === "certificate" && account.encryptedInternalMetadata) {
+ const internalMetadata = parseInternalMetadata(
+ account.accountType as PamAccountType,
+ await decrypt(session.projectId, account.encryptedInternalMetadata)
+ );
- credentials.privateKey = clientPrivateKey;
- credentials.certificate = signedPublicKey;
+ if (internalMetadata?.caPrivateKey) {
+ const keyAlgorithm = (internalMetadata.caKeyAlgorithm as SshCertKeyAlgorithm) || SshCertKeyAlgorithm.ED25519;
+ const { publicKey: clientPublicKey, privateKey: clientPrivateKey } = await createSshKeyPair(keyAlgorithm);
+
+ const username = credentials.username as string;
+ const { signedPublicKey } = await createSshCert({
+ caPrivateKey: internalMetadata.caPrivateKey,
+ clientPublicKey,
+ keyId: `pam-session-${session.id}`,
+ principals: [username],
+ requestedTtl: "8h",
+ certType: SshCertType.USER
+ });
+
+ credentials.privateKey = clientPrivateKey;
+ credentials.certificate = signedPublicKey;
+ }
}
const sessionStarted = session.status === PamSessionStatus.Starting;