Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
Add DNS config on ACME external CAs and use it on ACME challenge
Carlos Monastyrski591e062ee8e7
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.tsindex a022ee23f0..cad0984945 100644--- a/backend/src/lib/api-docs/constants.ts+++ b/backend/src/lib/api-docs/constants.ts@@ -2440,7 +2440,8 @@ export const CertificateAuthorities = {provider: `The DNS provider for the ACME Certificate Authority.`,hostedZoneId: `The hosted zone ID for the ACME Certificate Authority.`,eabKid: `The External Account Binding (EAB) Key ID for the ACME Certificate Authority. Required if the ACME provider uses EAB.`,- eabHmacKey: `The External Account Binding (EAB) HMAC key for the ACME Certificate Authority. Required if the ACME provider uses EAB.`+ eabHmacKey: `The External Account Binding (EAB) HMAC key for the ACME Certificate Authority. Required if the ACME provider uses EAB.`,+ dnsResolver: `An optional custom DNS resolver IP address or hostname to use for verifying DNS propagation during ACME challenges. When not set, the system default DNS resolver is used.`},AWS_PCA: {appConnectionId: `The ID of the AWS App Connection to use for authenticating with AWS Private Certificate Authority (PCA). This connection must have permissions to issue, get, and revoke certificates from the specified PCA.`,diff --git a/backend/src/services/certificate-authority/acme/acme-certificate-authority-fns.ts b/backend/src/services/certificate-authority/acme/acme-certificate-authority-fns.tsindex 66c82e2116..20f3b6a8ec 100644--- a/backend/src/services/certificate-authority/acme/acme-certificate-authority-fns.ts+++ b/backend/src/services/certificate-authority/acme/acme-certificate-authority-fns.ts@@ -2,10 +2,10 @@ import * as x509 from "@peculiar/x509";import acme, { CsrBuffer } from "acme-client";import dns from "dns";import { Knex } from "knex";+import net from "net";import RE2 from "re2";import { TableName } from "@app/db/schemas";-import { getConfig } from "@app/lib/config/env";import { crypto } from "@app/lib/crypto/cryptography";import { delay } from "@app/lib/delay";import { BadRequestError, CryptographyError, NotFoundError } from "@app/lib/errors";@@ -54,6 +54,26 @@ import { cloudflareDeleteTxtRecord, cloudflareInsertTxtRecord } from "./dns-provimport { dnsMadeEasyDeleteTxtRecord, dnsMadeEasyInsertTxtRecord } from "./dns-providers/dns-made-easy";import { route53DeleteTxtRecord, route53InsertTxtRecord } from "./dns-providers/route54";+const validateDnsResolver = async (resolver: string): Promise<void> => {+ if (net.isIP(resolver)) {+ return;+ }++ // Must be a valid hostname format+ const HOSTNAME_REGEX = new RE2("^[a-zA-Z0-9.-]+$");+ if (!HOSTNAME_REGEX.test(resolver)) {+ throw new BadRequestError({ message: `Invalid DNS resolver address: ${resolver}` });+ }++ try {+ await dns.promises.resolve(resolver);+ } catch {+ throw new BadRequestError({+ message: `DNS resolver "${resolver}" could not be resolved. Please provide a valid IP address or resolvable hostname.`+ });+ }+};+const parseTtlToDays = (ttl: string): number => {const match = ttl.match(new RE2("^(\\d+)([dhm])$"));if (!match) {@@ -152,6 +172,7 @@ type DBConfigurationColumn = {hostedZoneId: string;eabKid?: string;eabHmacKey?: string;+ dnsResolver?: string;};export const castDbEntryToAcmeCertificateAuthority = (@@ -179,7 +200,8 @@ export const castDbEntryToAcmeCertificateAuthority = (directoryUrl: dbConfigurationCol.directoryUrl,accountEmail: dbConfigurationCol.accountEmail,eabKid: dbConfigurationCol.eabKid,- eabHmacKey: dbConfigurationCol.eabHmacKey+ eabHmacKey: dbConfigurationCol.eabHmacKey,+ dnsResolver: dbConfigurationCol.dnsResolver},status: ca.status as CaStatus};@@ -212,14 +234,23 @@ const resolveAcmeChallengeCname = async (recordName: string): Promise<string> =>return current;};-const waitForDnsPropagation = async (lookupName: string, expectedValue: string): Promise<void> => {+const waitForDnsPropagation = async (+ lookupName: string,+ expectedValue: string,+ dnsResolver?: string+): Promise<void> => {const unquotedExpected = expectedValue.replace(new RE2('^"|"$', "g"), "");let attempts = 0;+ const resolver = new dns.promises.Resolver();+ if (dnsResolver) {+ resolver.setServers([dnsResolver]);+ }+while (attempts < DNS_PROPAGATION_MAX_RETRIES) {attempts += 1;- const found = await dns.promises // eslint-disable-line no-await-in-loop+ const found = await resolver // eslint-disable-line no-await-in-loop.resolveTxt(lookupName).then((records) => records.some((chunks) => chunks.join("") === unquotedExpected)).catch(() => false);@@ -377,9 +408,9 @@ export const orderCertificate = async (csr,email: acmeCa.configuration.accountEmail,challengePriority: ["dns-01"],- // For ACME development mode, we mock the DNS challenge API calls. So, no real DNS records are created.- // We need to disable the challenge verification to avoid errors.- skipChallengeVerification: getConfig().isAcmeDevelopmentMode && getConfig().ACME_SKIP_UPSTREAM_VALIDATION,+ // We skip the built-in challenge verification and rely on our challengeCreateFn/challengeRemoveFn+ // to handle DNS record management.+ skipChallengeVerification: true,termsOfServiceAgreed: true,challengeCreateFn: async (authz, challenge, keyAuthorization) => {@@ -440,7 +471,7 @@ export const orderCertificate = async (acmeCa.configuration.dnsProviderConfig.provider === AcmeDnsProvider.AzureDNS? recordName: `_acme-challenge.${authz.identifier.value}`;- await waitForDnsPropagation(lookupName, recordValue);+ await waitForDnsPropagation(lookupName, recordValue, acmeCa.configuration.dnsResolver);},challengeRemoveFn: async (authz, challenge, keyAuthorization) => {const { recordName, recordValue } = await getAcmeChallengeRecord(@@ -616,7 +647,8 @@ export const AcmeCertificateAuthorityFns = ({});}- const { dnsAppConnectionId, directoryUrl, accountEmail, dnsProviderConfig, eabKid, eabHmacKey } = configuration;+ const { dnsAppConnectionId, directoryUrl, accountEmail, dnsProviderConfig, eabKid, eabHmacKey, dnsResolver } =+ configuration;const appConnection = await appConnectionDAL.findById(dnsAppConnectionId);if (!appConnection) {@@ -647,6 +679,10 @@ export const AcmeCertificateAuthorityFns = ({});}+ if (dnsResolver) {+ await validateDnsResolver(dnsResolver);+ }+// validates permission to connectawait appConnectionService.validateAppConnectionUsageById(appConnection.app as AppConnection,@@ -677,7 +713,8 @@ export const AcmeCertificateAuthorityFns = ({dnsProvider: dnsProviderConfig.provider,hostedZoneId: dnsProviderConfig.hostedZoneId,eabKid,- eabHmacKey+ eabHmacKey,+ dnsResolver}},tx@@ -718,7 +755,8 @@ export const AcmeCertificateAuthorityFns = ({}) => {const updatedCa = await certificateAuthorityDAL.transaction(async (tx) => {if (configuration) {- const { dnsAppConnectionId, directoryUrl, accountEmail, dnsProviderConfig, eabKid, eabHmacKey } = configuration;+ const { dnsAppConnectionId, directoryUrl, accountEmail, dnsProviderConfig, eabKid, eabHmacKey, dnsResolver } =+ configuration;const appConnection = await appConnectionDAL.findById(dnsAppConnectionId);if (!appConnection) {@@ -755,6 +793,10 @@ export const AcmeCertificateAuthorityFns = ({});}+ if (dnsResolver) {+ await validateDnsResolver(dnsResolver);+ }+const ca = await certificateAuthorityDAL.findById(id);if (!ca) {@@ -781,7 +823,8 @@ export const AcmeCertificateAuthorityFns = ({dnsProvider: dnsProviderConfig.provider,hostedZoneId: dnsProviderConfig.hostedZoneId,eabKid,- eabHmacKey+ eabHmacKey,+ dnsResolver}},txdiff --git a/backend/src/services/certificate-authority/acme/acme-certificate-authority-schemas.ts b/backend/src/services/certificate-authority/acme/acme-certificate-authority-schemas.tsindex 70b0cb0e1d..0ec7e42b58 100644--- a/backend/src/services/certificate-authority/acme/acme-certificate-authority-schemas.ts+++ b/backend/src/services/certificate-authority/acme/acme-certificate-authority-schemas.ts@@ -20,7 +20,13 @@ export const AcmeCertificateAuthorityConfigurationSchema = z.object({directoryUrl: z.string().url().trim().min(1).describe(CertificateAuthorities.CONFIGURATIONS.ACME.directoryUrl),accountEmail: z.string().trim().min(1).describe(CertificateAuthorities.CONFIGURATIONS.ACME.accountEmail),eabKid: z.string().trim().max(64).optional().describe(CertificateAuthorities.CONFIGURATIONS.ACME.eabKid),- eabHmacKey: z.string().trim().max(512).optional().describe(CertificateAuthorities.CONFIGURATIONS.ACME.eabHmacKey)+ eabHmacKey: z.string().trim().max(512).optional().describe(CertificateAuthorities.CONFIGURATIONS.ACME.eabHmacKey),+ dnsResolver: z+ .string()+ .trim()+ .max(255)+ .optional()+ .describe(CertificateAuthorities.CONFIGURATIONS.ACME.dnsResolver)});export const AcmeCertificateAuthorityCredentialsSchema = z.object({diff --git a/frontend/src/hooks/api/ca/types.ts b/frontend/src/hooks/api/ca/types.tsindex 8a0efed6de..5614d403b0 100644--- a/frontend/src/hooks/api/ca/types.ts+++ b/frontend/src/hooks/api/ca/types.ts@@ -18,6 +18,7 @@ export type TAcmeCertificateAuthority = {accountEmail: string;eabKid?: string;eabHmacKey?: string;+ dnsResolver?: string;};};diff --git a/frontend/src/pages/cert-manager/CertificateAuthoritiesPage/components/ExternalCaModal.tsx b/frontend/src/pages/cert-manager/CertificateAuthoritiesPage/components/ExternalCaModal.tsxindex 9a63225c52..a25f068dc9 100644--- a/frontend/src/pages/cert-manager/CertificateAuthoritiesPage/components/ExternalCaModal.tsx+++ b/frontend/src/pages/cert-manager/CertificateAuthoritiesPage/components/ExternalCaModal.tsx@@ -83,7 +83,8 @@ const acmeConfigurationSchema = zdirectoryUrl: z.string(),accountEmail: z.string(),eabKid: z.string().optional(),- eabHmacKey: z.string().optional()+ eabHmacKey: z.string().optional(),+ dnsResolver: z.string().optional()}).superRefine((data, ctx) => {if (REQUIRED_EAB_DIRECTORIES.includes(data.directoryUrl)) {@@ -226,7 +227,8 @@ export const ExternalCaModal = ({ popUp, handlePopUpToggle }: Props) => {directoryUrl: "",accountEmail: "",eabKid: "",- eabHmacKey: ""+ eabHmacKey: "",+ dnsResolver: ""}});}@@ -341,7 +343,8 @@ export const ExternalCaModal = ({ popUp, handlePopUpToggle }: Props) => {directoryUrl: ca.configuration.directoryUrl,accountEmail: ca.configuration.accountEmail,eabKid: ca.configuration.eabKid,- eabHmacKey: ca.configuration.eabHmacKey+ eabHmacKey: ca.configuration.eabHmacKey,+ dnsResolver: ca.configuration.dnsResolver || ""}});} else if (ca.type === CaType.AZURE_AD_CS && availableConnections?.length) {@@ -399,7 +402,8 @@ export const ExternalCaModal = ({ popUp, handlePopUpToggle }: Props) => {accountEmail: formConfiguration.accountEmail,dnsAppConnectionId: formConfiguration.dnsAppConnection.id,eabKid: formConfiguration.eabKid,- eabHmacKey: formConfiguration.eabHmacKey+ eabHmacKey: formConfiguration.eabHmacKey,+ dnsResolver: formConfiguration.dnsResolver || undefined};} else if (type === CaType.AZURE_AD_CS && "azureAdcsConnection" in formConfiguration) {configPayload = {@@ -718,6 +722,22 @@ export const ExternalCaModal = ({ popUp, handlePopUpToggle }: Props) => {</FormControl>)}/>+ <Controller+ control={control}+ defaultValue=""+ name="configuration.dnsResolver"+ render={({ field, fieldState: { error } }) => (+ <FormControl+ label="DNS Resolver"+ isError={Boolean(error)}+ errorText={error?.message}+ isOptional+ tooltipText="A custom DNS resolver IP or hostname used to verify DNS propagation during ACME challenges. Leave empty to use the system default."+ >+ <Input {...field} placeholder="8.8.8.8" />+ </FormControl>+ )}+ /></>)}{caType === CaType.AZURE_AD_CS && (