Skip to content

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

4.0 KB
Raw Blame
import { customAlphabet } from "nanoid";

import { BadRequestError } from "@app/lib/errors";
import { ms } from "@app/lib/ms";
import { CertKeyAlgorithm, CertSubjectAlternativeNameType } from "@app/services/certificate/certificate-types";

import { AWS_ACM_CERTIFICATE_VALIDITY_DAYS } from "./aws-acm-public-ca-certificate-authority-enums";

export const ACM_ALLOWED_KEY_ALGORITHMS = new Set<string>([
  CertKeyAlgorithm.RSA_2048,
  CertKeyAlgorithm.ECDSA_P256,
  CertKeyAlgorithm.ECDSA_P384
]);

export const ACM_FIXED_VALIDITY_MS = AWS_ACM_CERTIFICATE_VALIDITY_DAYS * 24 * 60 * 60 * 1000;

/**
 * Pre-flight validator for ACM issuance inputs. Called both by the async fns
 * (defense in depth) and synchronously by the certificate order API before
 * enqueuing, so the user gets a 400 on submit rather than a FAILED request
 * row a moment later.
 */
export const validateAcmIssuanceInputs = ({
  csr,
  keyAlgorithm,
  altNames,
  ttl,
  notBefore,
  notAfter,
  organization,
  organizationalUnit,
  country,
  state,
  locality,
  isRenewal
}: {
  csr?: string;
  keyAlgorithm?: string;
  altNames?: Array<{ type: CertSubjectAlternativeNameType; value: string }>;
  ttl?: string;
  notBefore?: Date | string;
  notAfter?: Date | string;
  organization?: string;
  organizationalUnit?: string;
  country?: string;
  state?: string;
  locality?: string;
  isRenewal?: boolean;
}) => {
  if (csr) {
    throw new BadRequestError({
      message: "AWS Certificate Manager does not support CSR-based issuance"
    });
  }
  if (keyAlgorithm && !ACM_ALLOWED_KEY_ALGORITHMS.has(keyAlgorithm)) {
    throw new BadRequestError({
      message: `AWS ACM only supports RSA_2048, EC_prime256v1, and EC_secp384r1 key algorithms. Received: ${keyAlgorithm}`
    });
  }
  if (organization || organizationalUnit || country || state || locality) {
    throw new BadRequestError({
      message: "AWS Certificate Manager does not support subject fields (O, OU, C, ST, L)"
    });
  }
  if (altNames) {
    for (const san of altNames) {
      if (san.type !== CertSubjectAlternativeNameType.DNS_NAME) {
        throw new BadRequestError({
          message: `AWS Certificate Manager only supports DNS SANs. Unsupported SAN type: ${san.type}`
        });
      }
    }
  }
  // On renewal, ACM handles validity itself — we don't pass a TTL to AWS, and the
  // TTL derived from the original cert may round down (e.g., 197.999d → "197d"),
  // so skip the exact-match check.
  if (!isRenewal) {
    if (!ttl) {
      throw new BadRequestError({
        message: `AWS Certificate Manager issues certificates with a fixed validity of ${AWS_ACM_CERTIFICATE_VALIDITY_DAYS} days.`
      });
    }
    let ttlMs: number;
    try {
      ttlMs = ms(ttl);
    } catch {
      throw new BadRequestError({
        message: `Invalid TTL format: ${ttl}`
      });
    }
    if (ttlMs !== ACM_FIXED_VALIDITY_MS) {
      throw new BadRequestError({
        message: `AWS Certificate Manager issues certificates with a fixed validity of ${AWS_ACM_CERTIFICATE_VALIDITY_DAYS} days.`
      });
    }
    if (notBefore || notAfter) {
      throw new BadRequestError({
        message: `AWS Certificate Manager does not support notBefore or notAfter — validity is fixed at ${AWS_ACM_CERTIFICATE_VALIDITY_DAYS} days from issuance.`
      });
    }
  }
};

export const mapCertKeyAlgorithmToAcm = (keyAlgorithm: CertKeyAlgorithm) => {
  switch (keyAlgorithm) {
    case CertKeyAlgorithm.RSA_2048:
      return "RSA_2048";
    case CertKeyAlgorithm.ECDSA_P256:
      return "EC_prime256v1";
    case CertKeyAlgorithm.ECDSA_P384:
      return "EC_secp384r1";
    default:
      throw new BadRequestError({
        message: `AWS ACM only supports RSA_2048, EC_prime256v1, and EC_secp384r1 key algorithms. Received: ${keyAlgorithm as string}`
      });
  }
};

// ACM's ExportCertificate passphrase must be 4-128 chars and cannot contain #, $, or %.
const generateAcmPassphraseInternal = customAlphabet(
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  32
);
export const generateAcmPassphrase = (): string => generateAcmPassphraseInternal();