mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 16:52:38 +00:00
Finish preliminary support for external key source for ssh cas
This commit is contained in:
@@ -322,6 +322,91 @@ const validateSshPublicKey = async (publicKey: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const getKeyAlgorithmFromFingerprintOutput = (output: string): CertKeyAlgorithm | undefined => {
|
||||
const parts = output.trim().split(" ");
|
||||
const bitsInt = parseInt(parts[0], 10);
|
||||
const keyTypeRaw = parts.at(-1)?.replace(/[()]/g, ""); // remove surrounding parentheses
|
||||
|
||||
if (keyTypeRaw === "RSA") {
|
||||
return bitsInt === 2048 ? CertKeyAlgorithm.RSA_2048 : CertKeyAlgorithm.RSA_4096;
|
||||
}
|
||||
|
||||
if (keyTypeRaw === "ECDSA") {
|
||||
return bitsInt === 256 ? CertKeyAlgorithm.ECDSA_P256 : CertKeyAlgorithm.ECDSA_P384;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
};
|
||||
|
||||
export const normalizeSshPrivateKey = (raw: string): string => {
|
||||
return `${raw
|
||||
.replace(/\r\n/g, "\n") // Windows CRLF → LF
|
||||
.replace(/\r/g, "\n") // Old Mac CR → LF
|
||||
.replace(/\\n/g, "\n") // Double-escaped \n
|
||||
.trim()}\n`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validate the format of the SSH private key
|
||||
*
|
||||
* Returns the SSH public key corresponding to the private key
|
||||
* and the key algorithm categorization.
|
||||
*/
|
||||
export const validateSshPrivateKey = async (privateKey: string) => {
|
||||
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "ssh-privkey-"));
|
||||
const privateKeyFile = path.join(tempDir, "id_key");
|
||||
|
||||
try {
|
||||
await fs.writeFile(privateKeyFile, privateKey, {
|
||||
encoding: "utf8",
|
||||
mode: 0o600
|
||||
});
|
||||
|
||||
// This will fail if the private key is malformed or unreadable
|
||||
const { stdout: publicKey } = await execFileAsync("ssh-keygen", ["-y", "-f", privateKeyFile], {
|
||||
timeout: EXEC_TIMEOUT_MS
|
||||
});
|
||||
|
||||
const { stdout: fingerprint } = await execFileAsync("ssh-keygen", ["-lf", privateKeyFile]);
|
||||
const keyAlgorithm = getKeyAlgorithmFromFingerprintOutput(fingerprint);
|
||||
|
||||
if (!keyAlgorithm) {
|
||||
throw new BadRequestError({
|
||||
message: "Failed to validate SSH private key format: The key algorithm is not supported."
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
publicKey,
|
||||
keyAlgorithm
|
||||
};
|
||||
} catch (err) {
|
||||
throw new BadRequestError({
|
||||
message: "Failed to validate SSH private key format: could not be parsed."
|
||||
});
|
||||
} finally {
|
||||
await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Validate that the provided public and private keys are valid and constitute
|
||||
* a matching SSH key pair.
|
||||
*/
|
||||
export const validateExternalSshCaKeyPair = async (publicKey: string, privateKey: string) => {
|
||||
await validateSshPublicKey(publicKey);
|
||||
|
||||
const { publicKey: derivedPublicKey, keyAlgorithm } = await validateSshPrivateKey(privateKey);
|
||||
|
||||
if (publicKey.trim() !== derivedPublicKey.trim()) {
|
||||
throw new BadRequestError({
|
||||
message: "Failed to validate matching SSH key pair."
|
||||
});
|
||||
}
|
||||
|
||||
return keyAlgorithm;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create an SSH certificate for a user or host.
|
||||
*/
|
||||
|
||||
@@ -5,5 +5,6 @@ export const sanitizedSshCa = SshCertificateAuthoritiesSchema.pick({
|
||||
projectId: true,
|
||||
friendlyName: true,
|
||||
status: true,
|
||||
keyAlgorithm: true
|
||||
keyAlgorithm: true,
|
||||
keySource: true
|
||||
});
|
||||
|
||||
@@ -9,12 +9,19 @@ import { TSshCertificateBodyDALFactory } from "@app/ee/services/ssh-certificate/
|
||||
import { TSshCertificateDALFactory } from "@app/ee/services/ssh-certificate/ssh-certificate-dal";
|
||||
import { TSshCertificateTemplateDALFactory } from "@app/ee/services/ssh-certificate-template/ssh-certificate-template-dal";
|
||||
import { BadRequestError, NotFoundError } from "@app/lib/errors";
|
||||
import { CertKeyAlgorithm } from "@app/services/certificate/certificate-types";
|
||||
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
|
||||
import { KmsDataKey } from "@app/services/kms/kms-types";
|
||||
|
||||
import { SshCertTemplateStatus } from "../ssh-certificate-template/ssh-certificate-template-types";
|
||||
import { createSshCert, createSshKeyPair, getSshPublicKey } from "./ssh-certificate-authority-fns";
|
||||
import {
|
||||
createSshCert,
|
||||
createSshKeyPair,
|
||||
getSshPublicKey,
|
||||
validateExternalSshCaKeyPair
|
||||
} from "./ssh-certificate-authority-fns";
|
||||
import {
|
||||
SshCaKeySource,
|
||||
SshCaStatus,
|
||||
TCreateSshCaDTO,
|
||||
TDeleteSshCaDTO,
|
||||
@@ -59,7 +66,10 @@ export const sshCertificateAuthorityServiceFactory = ({
|
||||
const createSshCa = async ({
|
||||
projectId,
|
||||
friendlyName,
|
||||
keyAlgorithm,
|
||||
keyAlgorithm: requestedKeyAlgorithm,
|
||||
publicKey: externalPk,
|
||||
privateKey: externalSk,
|
||||
keySource,
|
||||
actorId,
|
||||
actorAuthMethod,
|
||||
actor,
|
||||
@@ -80,18 +90,37 @@ export const sshCertificateAuthorityServiceFactory = ({
|
||||
);
|
||||
|
||||
const newCa = await sshCertificateAuthorityDAL.transaction(async (tx) => {
|
||||
let publicKey: string;
|
||||
let privateKey: string;
|
||||
let keyAlgorithm: CertKeyAlgorithm = requestedKeyAlgorithm;
|
||||
|
||||
if (keySource === SshCaKeySource.INTERNAL) {
|
||||
// generate SSH CA key pair internally
|
||||
({ publicKey, privateKey } = await createSshKeyPair(requestedKeyAlgorithm));
|
||||
} else {
|
||||
// use external SSH CA key pair
|
||||
if (!externalPk || !externalSk) {
|
||||
throw new BadRequestError({
|
||||
message: "Public and private keys are required if generateSigningKey is false"
|
||||
});
|
||||
}
|
||||
publicKey = externalPk;
|
||||
privateKey = externalSk;
|
||||
|
||||
keyAlgorithm = await validateExternalSshCaKeyPair(publicKey, privateKey);
|
||||
}
|
||||
|
||||
const ca = await sshCertificateAuthorityDAL.create(
|
||||
{
|
||||
projectId,
|
||||
friendlyName,
|
||||
status: SshCaStatus.ACTIVE,
|
||||
keyAlgorithm
|
||||
keyAlgorithm,
|
||||
keySource
|
||||
},
|
||||
tx
|
||||
);
|
||||
|
||||
const { publicKey, privateKey } = await createSshKeyPair(keyAlgorithm);
|
||||
|
||||
const { encryptor: secretManagerEncryptor } = await kmsService.createCipherPairWithDataKey({
|
||||
type: KmsDataKey.SecretManager,
|
||||
projectId
|
||||
|
||||
@@ -7,6 +7,11 @@ export enum SshCaStatus {
|
||||
DISABLED = "disabled"
|
||||
}
|
||||
|
||||
export enum SshCaKeySource {
|
||||
INTERNAL = "internal",
|
||||
EXTERNAL = "external"
|
||||
}
|
||||
|
||||
export enum SshCertType {
|
||||
USER = "user",
|
||||
HOST = "host"
|
||||
@@ -15,6 +20,9 @@ export enum SshCertType {
|
||||
export type TCreateSshCaDTO = {
|
||||
friendlyName: string;
|
||||
keyAlgorithm: CertKeyAlgorithm;
|
||||
publicKey?: string;
|
||||
privateKey?: string;
|
||||
keySource: SshCaKeySource;
|
||||
} & TProjectPermission;
|
||||
|
||||
export type TGetSshCaDTO = {
|
||||
|
||||
Reference in New Issue
Block a user