Skip to content

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

swap to resource metadata model

x03220598883be65762
diff --git a/backend/src/db/migrations/20251211120000_pam-resource-metadata.ts b/backend/src/db/migrations/20251211120000_pam-resource-metadata.ts
new file mode 100644
index 0000000000..f8e17cb7d9
--- /dev/null
+++ b/backend/src/db/migrations/20251211120000_pam-resource-metadata.ts
@@ -0,0 +1,19 @@
+import { Knex } from "knex";
+
+import { TableName } from "../schemas";
+
+export async function up(knex: Knex): Promise<void> {
+ if (!(await knex.schema.hasColumn(TableName.PamResource, "encryptedResourceMetadata"))) {
+ await knex.schema.alterTable(TableName.PamResource, (t) => {
+ t.binary("encryptedResourceMetadata").nullable();
+ });
+ }
+}
+
+export async function down(knex: Knex): Promise<void> {
+ if (await knex.schema.hasColumn(TableName.PamResource, "encryptedResourceMetadata")) {
+ await knex.schema.alterTable(TableName.PamResource, (t) => {
+ t.dropColumn("encryptedResourceMetadata");
+ });
+ }
+}
diff --git a/backend/src/db/migrations/20251211120000_pam-ssh-resource-ca-keypair.ts b/backend/src/db/migrations/20251211120000_pam-ssh-resource-ca-keypair.ts
deleted file mode 100644
index a3ea5e1df5..0000000000
--- a/backend/src/db/migrations/20251211120000_pam-ssh-resource-ca-keypair.ts
+++ /dev/null
@@ -1,39 +0,0 @@
-import { Knex } from "knex";
-
-import { TableName } from "../schemas";
-
-export async function up(knex: Knex): Promise<void> {
- if (!(await knex.schema.hasColumn(TableName.PamResource, "encryptedCaPrivateKey"))) {
- await knex.schema.alterTable(TableName.PamResource, (t) => {
- t.binary("encryptedCaPrivateKey").nullable();
- });
- }
- if (!(await knex.schema.hasColumn(TableName.PamResource, "caPublicKey"))) {
- await knex.schema.alterTable(TableName.PamResource, (t) => {
- t.text("caPublicKey").nullable();
- });
- }
- if (!(await knex.schema.hasColumn(TableName.PamResource, "caKeyAlgorithm"))) {
- await knex.schema.alterTable(TableName.PamResource, (t) => {
- t.string("caKeyAlgorithm").nullable();
- });
- }
-}
-
-export async function down(knex: Knex): Promise<void> {
- if (await knex.schema.hasColumn(TableName.PamResource, "encryptedCaPrivateKey")) {
- await knex.schema.alterTable(TableName.PamResource, (t) => {
- t.dropColumn("encryptedCaPrivateKey");
- });
- }
- if (await knex.schema.hasColumn(TableName.PamResource, "caPublicKey")) {
- await knex.schema.alterTable(TableName.PamResource, (t) => {
- t.dropColumn("caPublicKey");
- });
- }
- if (await knex.schema.hasColumn(TableName.PamResource, "caKeyAlgorithm")) {
- await knex.schema.alterTable(TableName.PamResource, (t) => {
- t.dropColumn("caKeyAlgorithm");
- });
- }
-}
diff --git a/backend/src/db/schemas/pam-resources.ts b/backend/src/db/schemas/pam-resources.ts
index 94311823e8..bbd974e2f9 100644
--- a/backend/src/db/schemas/pam-resources.ts
+++ b/backend/src/db/schemas/pam-resources.ts
@@ -19,9 +19,7 @@ export const PamResourcesSchema = z.object({
createdAt: z.date(),
updatedAt: z.date(),
encryptedRotationAccountCredentials: zodBuffer.nullable().optional(),
- encryptedCaPrivateKey: zodBuffer.nullable().optional(),
- caPublicKey: z.string().nullable().optional(),
- caKeyAlgorithm: z.string().nullable().optional()
+ encryptedResourceMetadata: zodBuffer.nullable().optional()
});
export type TPamResources = z.infer<typeof PamResourcesSchema>;
diff --git a/backend/src/ee/routes/v1/pam-resource-routers/pam-resource-endpoints.ts b/backend/src/ee/routes/v1/pam-resource-routers/pam-resource-endpoints.ts
index 511cead3ff..d948a60200 100644
--- a/backend/src/ee/routes/v1/pam-resource-routers/pam-resource-endpoints.ts
+++ b/backend/src/ee/routes/v1/pam-resource-routers/pam-resource-endpoints.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
+import { PamResource } from "@app/ee/services/pam-resource/pam-resource-enums";
import { TPamResource } from "@app/ee/services/pam-resource/pam-resource-types";
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
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 8f62b07200..babfef7980 100644
--- a/backend/src/ee/services/pam-account/pam-account-service.ts
+++ b/backend/src/ee/services/pam-account/pam-account-service.ts
@@ -9,7 +9,11 @@ import {
TAwsIamAccountCredentials
} from "@app/ee/services/pam-resource/aws-iam";
import { PAM_RESOURCE_FACTORY_MAP } from "@app/ee/services/pam-resource/pam-resource-factory";
-import { decryptResource, decryptResourceConnectionDetails } from "@app/ee/services/pam-resource/pam-resource-fns";
+import {
+ decryptResource,
+ decryptResourceConnectionDetails,
+ decryptResourceMetadata
+} from "@app/ee/services/pam-resource/pam-resource-fns";
import { SSHAuthMethod } from "@app/ee/services/pam-resource/ssh/ssh-resource-enums";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import {
@@ -50,7 +54,7 @@ import { TPamResourceDALFactory } from "../pam-resource/pam-resource-dal";
import { PamResource } from "../pam-resource/pam-resource-enums";
import { TPamAccountCredentials } from "../pam-resource/pam-resource-types";
import { TSqlAccountCredentials, TSqlResourceConnectionDetails } from "../pam-resource/shared/sql/sql-resource-types";
-import { TSSHAccountCredentials } from "../pam-resource/ssh/ssh-resource-types";
+import { TSSHAccountCredentials, TSSHResourceMetadata } from "../pam-resource/ssh/ssh-resource-types";
import { TPamSessionDALFactory } from "../pam-session/pam-session-dal";
import { PamSessionStatus } from "../pam-session/pam-session-enums";
import { OrgPermissionGatewayActions, OrgPermissionSubjects } from "../permission/org-permission";
@@ -866,23 +870,22 @@ export const pamAccountServiceFactory = ({
const accountCredentials = decryptedAccount.credentials as TSSHAccountCredentials;
if (accountCredentials.authMethod === SSHAuthMethod.Certificate) {
- if (!resource.encryptedCaPrivateKey) {
+ if (!resource.encryptedResourceMetadata) {
throw new BadRequestError({
message: "SSH resource does not have a CA configured for certificate-based authentication"
});
}
- const { decryptor } = await kmsService.createCipherPairWithDataKey({
- type: KmsDataKey.SecretManager,
- projectId: session.projectId
+ const metadata = await decryptResourceMetadata<TSSHResourceMetadata>({
+ encryptedMetadata: resource.encryptedResourceMetadata,
+ projectId: session.projectId,
+ kmsService
});
- const caPrivateKey = decryptor({
- cipherTextBlob: resource.encryptedCaPrivateKey
- }).toString("utf8");
+ const { caPrivateKey, caKeyAlgorithm } = metadata;
// Generate a new key pair for the user
- const keyAlgorithm = (resource.caKeyAlgorithm as SshCertKeyAlgorithm) || SshCertKeyAlgorithm.ED25519;
+ const keyAlgorithm = (caKeyAlgorithm as SshCertKeyAlgorithm) || SshCertKeyAlgorithm.ED25519;
const { publicKey, privateKey } = await createSshKeyPair(keyAlgorithm);
// Calculate TTL from session expiry
diff --git a/backend/src/ee/services/pam-resource/pam-resource-fns.ts b/backend/src/ee/services/pam-resource/pam-resource-fns.ts
index 581f3425ac..d193d14e50 100644
--- a/backend/src/ee/services/pam-resource/pam-resource-fns.ts
+++ b/backend/src/ee/services/pam-resource/pam-resource-fns.ts
@@ -6,7 +6,7 @@ import { decryptAccountCredentials } from "../pam-account/pam-account-fns";
import { getAwsIamResourceListItem } from "./aws-iam/aws-iam-resource-fns";
import { getKubernetesResourceListItem } from "./kubernetes/kubernetes-resource-fns";
import { getMySQLResourceListItem } from "./mysql/mysql-resource-fns";
-import { TPamResource, TPamResourceConnectionDetails } from "./pam-resource-types";
+import { TPamResource, TPamResourceConnectionDetails, TPamResourceMetadata } from "./pam-resource-types";
import { getPostgresResourceListItem } from "./postgres/postgres-resource-fns";
export const listResourceOptions = () => {
@@ -61,6 +61,49 @@ export const decryptResourceConnectionDetails = async ({
return JSON.parse(decryptedPlainTextBlob.toString()) as TPamResourceConnectionDetails;
};
+// Resource Metadata
+export const encryptResourceMetadata = async ({
+ projectId,
+ metadata,
+ kmsService
+}: {
+ projectId: string;
+ metadata: TPamResourceMetadata;
+ kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
+}) => {
+ const { encryptor } = await kmsService.createCipherPairWithDataKey({
+ type: KmsDataKey.SecretManager,
+ projectId
+ });
+
+ const { cipherTextBlob } = encryptor({
+ plainText: Buffer.from(JSON.stringify(metadata))
+ });
+
+ return cipherTextBlob;
+};
+
+export const decryptResourceMetadata = async <T extends TPamResourceMetadata>({
+ projectId,
+ encryptedMetadata,
+ kmsService
+}: {
+ projectId: string;
+ encryptedMetadata: Buffer;
+ kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
+}) => {
+ const { decryptor } = await kmsService.createCipherPairWithDataKey({
+ type: KmsDataKey.SecretManager,
+ projectId
+ });
+
+ const decryptedPlainTextBlob = decryptor({
+ cipherTextBlob: encryptedMetadata
+ });
+
+ return JSON.parse(decryptedPlainTextBlob.toString()) as T;
+};
+
export const decryptResource = async (
resource: TPamResources,
projectId: string,
diff --git a/backend/src/ee/services/pam-resource/pam-resource-service.ts b/backend/src/ee/services/pam-resource/pam-resource-service.ts
index 279c3813fc..9e6a888e41 100644
--- a/backend/src/ee/services/pam-resource/pam-resource-service.ts
+++ b/backend/src/ee/services/pam-resource/pam-resource-service.ts
@@ -9,7 +9,6 @@ import { DatabaseErrorCode } from "@app/lib/error-codes";
import { BadRequestError, DatabaseError, NotFoundError } from "@app/lib/errors";
import { OrgServiceActor } from "@app/lib/types";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
-import { KmsDataKey } from "@app/services/kms/kms-types";
import { TGatewayV2ServiceFactory } from "../gateway-v2/gateway-v2-service";
import { TLicenseServiceFactory } from "../license/license-service";
@@ -20,10 +19,13 @@ import { PAM_RESOURCE_FACTORY_MAP } from "./pam-resource-factory";
import {
decryptResource,
decryptResourceConnectionDetails,
+ decryptResourceMetadata,
encryptResourceConnectionDetails,
+ encryptResourceMetadata,
listResourceOptions
} from "./pam-resource-fns";
import { TCreateResourceDTO, TListResourcesDTO, TUpdateResourceDTO } from "./pam-resource-types";
+import { TSSHResourceMetadata } from "./ssh/ssh-resource-types";
type TPamResourceServiceFactoryDep = {
pamResourceDAL: TPamResourceDALFactory;
@@ -312,29 +314,37 @@ export const pamResourceServiceFactory = ({
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Edit, ProjectPermissionSub.PamResources);
- if (resource.caPublicKey) {
- return { caPublicKey: resource.caPublicKey };
+ // Check if metadata already exists with CA
+ if (resource.encryptedResourceMetadata) {
+ const metadata = await decryptResourceMetadata<TSSHResourceMetadata>({
+ encryptedMetadata: resource.encryptedResourceMetadata,
+ projectId: resource.projectId,
+ kmsService
+ });
+ return { caPublicKey: metadata.caPublicKey };
}
// Generate new CA key pair
const keyAlgorithm = SshCertKeyAlgorithm.ED25519;
const { publicKey, privateKey } = await createSshKeyPair(keyAlgorithm);
- const { encryptor } = await kmsService.createCipherPairWithDataKey({
- type: KmsDataKey.SecretManager,
- projectId: resource.projectId
- });
+ const metadata: TSSHResourceMetadata = {
+ caPrivateKey: privateKey,
+ caPublicKey: publicKey.trim(),
+ caKeyAlgorithm: keyAlgorithm
+ };
- const encryptedCaPrivateKey = encryptor({ plainText: Buffer.from(privateKey, "utf8") }).cipherTextBlob;
- const caPublicKey = publicKey.trim();
+ const encryptedResourceMetadata = await encryptResourceMetadata({
+ metadata,
+ projectId: resource.projectId,
+ kmsService
+ });
await pamResourceDAL.updateById(resourceId, {
- encryptedCaPrivateKey,
- caPublicKey,
- caKeyAlgorithm: keyAlgorithm
+ encryptedResourceMetadata
});
- return { caPublicKey };
+ return { caPublicKey: metadata.caPublicKey };
};
return {
diff --git a/backend/src/ee/services/pam-resource/pam-resource-types.ts b/backend/src/ee/services/pam-resource/pam-resource-types.ts
index 5291e044ac..01788e5542 100644
--- a/backend/src/ee/services/pam-resource/pam-resource-types.ts
+++ b/backend/src/ee/services/pam-resource/pam-resource-types.ts
@@ -30,7 +30,8 @@ import {
TSSHAccount,
TSSHAccountCredentials,
TSSHResource,
- TSSHResourceConnectionDetails
+ TSSHResourceConnectionDetails,
+ TSSHResourceMetadata
} from "./ssh/ssh-resource-types";
// Resource types
@@ -41,6 +42,7 @@ export type TPamResourceConnectionDetails =
| TSSHResourceConnectionDetails
| TKubernetesResourceConnectionDetails
| TAwsIamResourceConnectionDetails;
+export type TPamResourceMetadata = TSSHResourceMetadata;
// Account types
export type TPamAccount = TPostgresAccount | TMySQLAccount | TSSHAccount | TAwsIamAccount | TKubernetesAccount;
diff --git a/backend/src/ee/services/pam-resource/ssh/ssh-resource-schemas.ts b/backend/src/ee/services/pam-resource/ssh/ssh-resource-schemas.ts
index ef79f864e0..192c1a2bc6 100644
--- a/backend/src/ee/services/pam-resource/ssh/ssh-resource-schemas.ts
+++ b/backend/src/ee/services/pam-resource/ssh/ssh-resource-schemas.ts
@@ -93,6 +93,13 @@ export const UpdateSSHResourceSchema = BaseUpdateGatewayPamResourceSchema.extend
rotationAccountCredentials: SSHAccountCredentialsSchema.nullable().optional()
});
+// Resource Metadata
+export const SSHResourceMetadataSchema = z.object({
+ caPrivateKey: z.string(),
+ caPublicKey: z.string(),
+ caKeyAlgorithm: z.string()
+});
+
// Accounts
export const SSHAccountSchema = BasePamAccountSchema.extend({
credentials: SSHAccountCredentialsSchema
diff --git a/backend/src/ee/services/pam-resource/ssh/ssh-resource-types.ts b/backend/src/ee/services/pam-resource/ssh/ssh-resource-types.ts
index 920dc42746..f24ac31844 100644
--- a/backend/src/ee/services/pam-resource/ssh/ssh-resource-types.ts
+++ b/backend/src/ee/services/pam-resource/ssh/ssh-resource-types.ts
@@ -4,12 +4,14 @@ import {
SSHAccountCredentialsSchema,
SSHAccountSchema,
SSHResourceConnectionDetailsSchema,
+ SSHResourceMetadataSchema,
SSHResourceSchema
} from "./ssh-resource-schemas";
// Resources
export type TSSHResource = z.infer<typeof SSHResourceSchema>;
export type TSSHResourceConnectionDetails = z.infer<typeof SSHResourceConnectionDetailsSchema>;
+export type TSSHResourceMetadata = z.infer<typeof SSHResourceMetadataSchema>;
// Accounts
export type TSSHAccount = z.infer<typeof SSHAccountSchema>;