Skip to content

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

Address greptile comments

Carlos Monastyrski815fc9bc3562
diff --git a/backend/src/db/migrations/20260618143217_pki-hsm-connector.ts b/backend/src/db/migrations/20260618143217_pki-hsm-connector.ts
index 381f311bf2..72efc697a6 100644
--- a/backend/src/db/migrations/20260618143217_pki-hsm-connector.ts
+++ b/backend/src/db/migrations/20260618143217_pki-hsm-connector.ts
@@ -107,7 +107,7 @@ export async function up(knex: Knex): Promise<void> {
if (await knex.schema.hasTable(TableName.GatewayV2)) {
if (!(await knex.schema.hasColumn(TableName.GatewayV2, "capabilities"))) {
await knex.schema.alterTable(TableName.GatewayV2, (t) => {
- t.jsonb("capabilities").notNullable().defaultTo(knex.raw(`'{}'::jsonb`));
+ t.jsonb("capabilities");
});
}
if (!(await indexExists(knex, GATEWAYS_CAPABILITIES_GIN_IDX))) {
diff --git a/backend/src/db/schemas/gateways-v2.ts b/backend/src/db/schemas/gateways-v2.ts
index e4941300c4..88e73d6608 100644
--- a/backend/src/db/schemas/gateways-v2.ts
+++ b/backend/src/db/schemas/gateways-v2.ts
@@ -22,7 +22,7 @@ export const GatewaysV2Schema = z.object({
healthAlertedAt: z.date().nullable().optional(),
tokenVersion: z.number().default(0),
heartbeatTTL: z.number().nullable().optional(),
- capabilities: z.unknown()
+ capabilities: z.unknown().nullable().optional()
});
export type TGatewaysV2 = z.infer<typeof GatewaysV2Schema>;
diff --git a/backend/src/lib/csr/build-csr-with-external-signer.ts b/backend/src/lib/csr/build-csr-with-external-signer.ts
index 39ac97ed64..5c808e768a 100644
--- a/backend/src/lib/csr/build-csr-with-external-signer.ts
+++ b/backend/src/lib/csr/build-csr-with-external-signer.ts
@@ -159,7 +159,7 @@ export const buildCsrWithExternalSigner = async (input: BuildCsrInput): Promise<
const spki = AsnConvert.parse(bufferToArrayBuffer(input.publicKeySpkiDer), SubjectPublicKeyInfo);
- const nameDer = new x509.Name(`CN=${input.subjectCommonName}`).toArrayBuffer();
+ const nameDer = new x509.Name([{ CN: [input.subjectCommonName] }]).toArrayBuffer();
const subject = AsnConvert.parse(nameDer, Name);
// extensionRequest (RFC 2985 OID 1.2.840.113549.1.9.14) carries EKU + SAN + KU + BC.
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index 72448ebdd2..7997bb016f 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -2634,6 +2634,8 @@ export const registerRoutes = async (
kmsService,
gatewayV2Service,
gatewayPoolService,
+ gatewayV2DAL,
+ gatewayPoolDAL,
licenseService
});
diff --git a/backend/src/services/hsm-connector/hsm-connector-service.ts b/backend/src/services/hsm-connector/hsm-connector-service.ts
index f430031500..3601247bb2 100644
--- a/backend/src/services/hsm-connector/hsm-connector-service.ts
+++ b/backend/src/services/hsm-connector/hsm-connector-service.ts
@@ -1,7 +1,9 @@
import { ForbiddenError } from "@casl/ability";
import { ActionProjectType } from "@app/db/schemas";
+import { TGatewayPoolDALFactory } from "@app/ee/services/gateway-pool/gateway-pool-dal";
import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
+import { TGatewayV2DALFactory } from "@app/ee/services/gateway-v2/gateway-v2-dal";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
@@ -40,6 +42,8 @@ export type THsmConnectorServiceFactoryDep = {
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
gatewayPoolService: Pick<TGatewayPoolServiceFactory, "listHealthyGateways">;
+ gatewayV2DAL: Pick<TGatewayV2DALFactory, "findById">;
+ gatewayPoolDAL: Pick<TGatewayPoolDALFactory, "findById">;
licenseService: Pick<TLicenseServiceFactory, "getPlan">;
};
@@ -51,10 +55,31 @@ export const hsmConnectorServiceFactory = ({
kmsService,
gatewayV2Service,
gatewayPoolService,
+ gatewayV2DAL,
+ gatewayPoolDAL,
licenseService
}: THsmConnectorServiceFactoryDep) => {
const routing = hsmConnectorRoutingFactory({ gatewayV2Service, gatewayPoolService });
+ const assertGatewayBelongsToOrg = async (
+ actorOrgId: string,
+ gatewayId: string | null | undefined,
+ gatewayPoolId: string | null | undefined
+ ) => {
+ if (gatewayId) {
+ const gw = await gatewayV2DAL.findById(gatewayId);
+ if (!gw || gw.orgId !== actorOrgId) {
+ throw new NotFoundError({ message: `Gateway ${gatewayId} not found.` });
+ }
+ }
+ if (gatewayPoolId) {
+ const pool = await gatewayPoolDAL.findById(gatewayPoolId);
+ if (!pool || pool.orgId !== actorOrgId) {
+ throw new NotFoundError({ message: `Gateway pool ${gatewayPoolId} not found.` });
+ }
+ }
+ };
+
const assertLicense = async (orgId: string) => {
const plan = await licenseService.getPlan(orgId);
if (!plan.pkiHsm) {
@@ -93,6 +118,8 @@ export const hsmConnectorServiceFactory = ({
});
}
+ await assertGatewayBelongsToOrg(actor.orgId, dto.gatewayId, dto.gatewayPoolId);
+
const parsedCreds = HsmConnectorCredentialsSchema.safeParse(dto.credentials);
if (!parsedCreds.success) {
throw new BadRequestError({ message: `Invalid credentials: ${parsedCreds.error.message}` });
@@ -205,6 +232,10 @@ export const hsmConnectorServiceFactory = ({
});
}
+ if (routingChanged) {
+ await assertGatewayBelongsToOrg(actor.orgId, nextGatewayId, nextGatewayPoolId);
+ }
+
const currentCreds = await decryptHsmConnectorCredentials({
projectId: row.projectId,
encryptedCredentials: row.encryptedCredentials,
@@ -331,15 +362,17 @@ export const hsmConnectorServiceFactory = ({
projectId: string;
keyLabel: string;
keyAlgorithm: HsmKeyAlgorithm;
- }): Promise<Buffer> => {
+ }): Promise<{ publicKeySpkiDer: Buffer; keyLabel: string }> => {
const { row, credentials } = await $loadConnector(args.connectorId, args.projectId);
+ const prefix = credentials.keyNamePrefix ?? "";
+ const fullLabel = `${prefix}${args.keyLabel}`;
const result = await routing.dispatchPkcs11<{ publicKey: string }>({
connector: { gatewayId: row.gatewayId, gatewayPoolId: row.gatewayPoolId },
credentials,
endpoint: "/v1/generate-key-pair",
- params: { keyLabel: args.keyLabel, keyAlgorithm: args.keyAlgorithm }
+ params: { keyLabel: fullLabel, keyAlgorithm: args.keyAlgorithm }
});
- return Buffer.from(result.publicKey, "base64");
+ return { publicKeySpkiDer: Buffer.from(result.publicKey, "base64"), keyLabel: fullLabel };
};
const sign = async (args: {
diff --git a/backend/src/services/signer/signer-issuance-fns.ts b/backend/src/services/signer/signer-issuance-fns.ts
index 47d921ec08..348de07ef3 100644
--- a/backend/src/services/signer/signer-issuance-fns.ts
+++ b/backend/src/services/signer/signer-issuance-fns.ts
@@ -251,12 +251,12 @@ export const issueHsmBackedSignerCertificate = async (
});
}
- const keyLabel = `infisical-signer-${crypto.nativeCrypto.randomUUID()}`;
+ const keyLabelSuffix = `signer-${crypto.nativeCrypto.randomUUID()}`;
- const publicKeySpkiDer = await deps.hsmConnectorService.generateKeyPair({
+ const { publicKeySpkiDer, keyLabel } = await deps.hsmConnectorService.generateKeyPair({
connectorId: input.hsmConnectorId,
projectId: input.projectId,
- keyLabel,
+ keyLabel: keyLabelSuffix,
keyAlgorithm: input.hsmKeyAlgorithm
});
const built = await buildCsrWithExternalSigner({
diff --git a/backend/src/services/signer/signer-issuance-service.ts b/backend/src/services/signer/signer-issuance-service.ts
index 759f119f64..ae2a3dd5d2 100644
--- a/backend/src/services/signer/signer-issuance-service.ts
+++ b/backend/src/services/signer/signer-issuance-service.ts
@@ -142,17 +142,22 @@ export const signerIssuanceServiceFactory = ({
await hsmConnectorService.assertAttachPermission(input.hsm.actor, input.hsm.hsmConnectorId, input.projectId);
}
const reuseExistingKey = Boolean(input.hsm.hsmKeyLabel && input.hsm.hsmPublicKeySpki);
- const keyLabel = reuseExistingKey
- ? (input.hsm.hsmKeyLabel as string)
- : `infisical-signer-${crypto.nativeCrypto.randomUUID()}`;
- const publicKeySpkiDer = reuseExistingKey
- ? Buffer.from(input.hsm.hsmPublicKeySpki as Buffer)
- : await hsmConnectorService.generateKeyPair({
- connectorId: input.hsm.hsmConnectorId,
- projectId: input.projectId,
- keyLabel,
- keyAlgorithm: input.hsm.hsmKeyAlgorithm
- });
+ let keyLabel: string;
+ let publicKeySpkiDer: Buffer;
+ if (reuseExistingKey) {
+ keyLabel = input.hsm.hsmKeyLabel as string;
+ publicKeySpkiDer = Buffer.from(input.hsm.hsmPublicKeySpki as Buffer);
+ } else {
+ const keyLabelSuffix = `signer-${crypto.nativeCrypto.randomUUID()}`;
+ const generated = await hsmConnectorService.generateKeyPair({
+ connectorId: input.hsm.hsmConnectorId,
+ projectId: input.projectId,
+ keyLabel: keyLabelSuffix,
+ keyAlgorithm: input.hsm.hsmKeyAlgorithm
+ });
+ keyLabel = generated.keyLabel;
+ publicKeySpkiDer = generated.publicKeySpkiDer;
+ }
const built = await buildCsrWithExternalSigner({
publicKeySpkiDer,
keyAlgorithm: input.hsm.hsmKeyAlgorithm,
diff --git a/backend/src/services/signer/signer-service.ts b/backend/src/services/signer/signer-service.ts
index 2789f43924..800a7a3ac4 100644
--- a/backend/src/services/signer/signer-service.ts
+++ b/backend/src/services/signer/signer-service.ts
@@ -300,6 +300,13 @@ export const signerServiceFactory = ({
const isExternalCa = resolvedCaType !== null && resolvedCaType !== CaType.INTERNAL;
+ if (resolvedCaType === CaType.AZURE_AD_CS && dto.certificate?.keySource === CertKeySource.Hsm) {
+ throw new BadRequestError({
+ message:
+ "HSM-backed signers are not supported with Azure AD CS yet. Use AWS Private CA, an Internal CA, or switch the signer's key source to Infisical-managed."
+ });
+ }
+
const keyAlgorithm: CertKeyAlgorithm = (dto.keyAlgorithm as CertKeyAlgorithm) ?? CertKeyAlgorithm.RSA_2048;
let issuedCertificateId: string | null = null;