Skip to content

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

fix(pki): cloud dns retry on vanished record and clearer picker errors

bernie-g311628ad9375
diff --git a/backend/src/services/app-connection/gcp/gcp-connection-service.ts b/backend/src/services/app-connection/gcp/gcp-connection-service.ts
index fde662f8c3..084a9379bc 100644
--- a/backend/src/services/app-connection/gcp/gcp-connection-service.ts
+++ b/backend/src/services/app-connection/gcp/gcp-connection-service.ts
@@ -97,7 +97,7 @@ export const gcpConnectionService = (getAppConnection: TGetAppConnectionFunc) =>
try {
return await getGcpCloudDnsProjects(appConnection);
} catch (error) {
- logger.error(error, "Error listing GCP Cloud DNS projects");
+ logger.error(error, `Error listing GCP Cloud DNS projects [connectionId=${connectionId}]`);
throw error;
}
};
@@ -111,7 +111,10 @@ export const gcpConnectionService = (getAppConnection: TGetAppConnectionFunc) =>
try {
return await getGcpCloudDnsZones(gcpProjectId, appConnection);
} catch (error) {
- logger.error(error, "Error listing GCP Cloud DNS zones");
+ logger.error(
+ error,
+ `Error listing GCP Cloud DNS zones [connectionId=${connectionId}] [gcpProjectId=${gcpProjectId}]`
+ );
throw error;
}
};
diff --git a/backend/src/services/certificate-authority/acme/dns-providers/gcp-cloud-dns.test.ts b/backend/src/services/certificate-authority/acme/dns-providers/gcp-cloud-dns.test.ts
index 10d2175e2a..bf4437d391 100644
--- a/backend/src/services/certificate-authority/acme/dns-providers/gcp-cloud-dns.test.ts
+++ b/backend/src/services/certificate-authority/acme/dns-providers/gcp-cloud-dns.test.ts
@@ -86,6 +86,30 @@ describe("gcpCloudDnsInsertTxtRecord", () => {
);
});
+ it("retries as a fresh create when the record set was deleted between read and write", async () => {
+ getMock.mockResolvedValueOnce(existingRecordSet(['"token-a"'])).mockRejectedValueOnce(axiosError(404));
+ postMock.mockRejectedValueOnce(axiosError(404)).mockResolvedValueOnce({ data: {} });
+
+ await gcpCloudDnsInsertTxtRecord(connection, ZONE, RECORD, '"token-b"');
+
+ expect(postMock).toHaveBeenCalledTimes(2);
+ expect(postMock).toHaveBeenLastCalledWith(
+ `${ZONE_URL}/changes`,
+ { additions: [{ name: FQDN, type: "TXT", ttl: 60, rrdatas: ['"token-b"'] }] },
+ expect.anything()
+ );
+ });
+
+ it("does not retry a 404 on a plain create", async () => {
+ getMock.mockRejectedValueOnce(axiosError(404));
+ postMock.mockRejectedValueOnce(axiosError(404, "The managed zone does not exist."));
+
+ await expect(gcpCloudDnsInsertTxtRecord(connection, ZONE, RECORD, '"token-a"')).rejects.toThrow(
+ "Google Cloud DNS request failed: The managed zone does not exist."
+ );
+ expect(postMock).toHaveBeenCalledTimes(1);
+ });
+
it("does nothing when the value is already present", async () => {
getMock.mockResolvedValueOnce(existingRecordSet(["token-a"]));
diff --git a/backend/src/services/certificate-authority/acme/dns-providers/gcp-cloud-dns.ts b/backend/src/services/certificate-authority/acme/dns-providers/gcp-cloud-dns.ts
index 89249fb812..777dadcef8 100644
--- a/backend/src/services/certificate-authority/acme/dns-providers/gcp-cloud-dns.ts
+++ b/backend/src/services/certificate-authority/acme/dns-providers/gcp-cloud-dns.ts
@@ -46,7 +46,7 @@ const toGcpDnsError = (error: unknown, hostedZoneId: string) => {
if (error.response?.status === 403) {
const [, gcpProjectId, , zoneName] = hostedZoneId.split("/");
- if (message.includes("has not been used in project") || message.includes("is disabled")) {
+ if (message.includes("has not been used in project")) {
return new Error(
`The Cloud DNS API is not enabled on GCP project '${gcpProjectId}'. Enable dns.googleapis.com in the Google Cloud console and try again.`
);
@@ -61,9 +61,12 @@ const toGcpDnsError = (error: unknown, hostedZoneId: string) => {
return error;
};
-// 409 = record set was created concurrently, 412 = record set changed since it was read
-const isConflictError = (error: unknown) =>
- isAxiosError(error) && (error.response?.status === 409 || error.response?.status === 412);
+// 409 = created concurrently, 412 = changed since read, 404 on a deletion = removed since read
+const isConflictError = (error: unknown, changeHadDeletions: boolean) =>
+ isAxiosError(error) &&
+ (error.response?.status === 409 ||
+ error.response?.status === 412 ||
+ (error.response?.status === 404 && changeHadDeletions));
const getTxtRecordSet = async (zoneUrl: string, accessToken: string, fqdn: string) => {
try {
@@ -101,17 +104,19 @@ const applyTxtRecordChange = async (
const accessToken = await getGcpConnectionAuthToken(connection);
for (let attempt = 1; attempt <= MAX_CHANGE_ATTEMPTS; attempt += 1) {
+ let changeHadDeletions = false;
try {
// eslint-disable-next-line no-await-in-loop
const existing = await getTxtRecordSet(zoneUrl, accessToken, fqdn);
const change = buildChange(fqdn, existing);
if (!change) return;
+ changeHadDeletions = Boolean(change.deletions?.length);
// eslint-disable-next-line no-await-in-loop
await submitChange(zoneUrl, accessToken, change);
return;
} catch (error) {
- if (!isConflictError(error) || attempt === MAX_CHANGE_ATTEMPTS) {
+ if (!isConflictError(error, changeHadDeletions) || attempt === MAX_CHANGE_ATTEMPTS) {
throw toGcpDnsError(error, hostedZoneId);
}
// eslint-disable-next-line no-await-in-loop
@@ -152,7 +157,10 @@ export const gcpCloudDnsDeleteTxtRecord = async (
const remaining = existing?.rrdatas.filter((rrdata) => normalizeTxtValue(rrdata) !== normalizeTxtValue(value));
if (!existing || !remaining || remaining.length === existing.rrdatas.length) {
- logger.warn({ hostedZoneId, recordName: fqdn }, "TXT record not found for deletion");
+ logger.warn(
+ { hostedZoneId, recordName: fqdn },
+ `Google Cloud DNS TXT record not found for deletion [hostedZoneId=${hostedZoneId}] [recordName=${fqdn}]`
+ );
return null;
}
diff --git a/docs/integrations/app-connections/gcp.mdx b/docs/integrations/app-connections/gcp.mdx
index d8e35fb5bc..9088447de0 100644
--- a/docs/integrations/app-connections/gcp.mdx
+++ b/docs/integrations/app-connections/gcp.mdx
@@ -107,7 +107,7 @@ Infisical supports [service account impersonation](https://cloud.google.com/iam/
<Tab title="ACME DNS validation">
Add the required permissions for [ACME certificate authorities](/documentation/platform/pki/ca/acme-ca) that validate domains through Google Cloud DNS.
- Grant the **DNS Administrator** (`roles/dns.admin`) and **Service Usage Viewer** (`roles/serviceusage.serviceUsageViewer`) roles on the project that holds your public managed zone. DNS Administrator lets Infisical create and delete the challenge TXT records, and Service Usage Viewer lets Infisical list the projects where the Cloud DNS API is enabled.
+ Grant the **DNS Administrator** (`roles/dns.admin`) and **Service Usage Viewer** (`roles/serviceusage.serviceUsageViewer`) roles on the project that holds your public managed zone. DNS Administrator lets Infisical list your projects and zones and create and delete the challenge TXT records. Service Usage Viewer lets Infisical check which projects have the Cloud DNS API enabled.
Then enable the Google Cloud APIs that Infisical uses on that same project:
diff --git a/frontend/src/pages/cert-manager/CertificateAuthoritiesPage/components/ExternalCaFields/GcpCloudDnsZoneFields.tsx b/frontend/src/pages/cert-manager/CertificateAuthoritiesPage/components/ExternalCaFields/GcpCloudDnsZoneFields.tsx
index 13843385bb..595b66e458 100644
--- a/frontend/src/pages/cert-manager/CertificateAuthoritiesPage/components/ExternalCaFields/GcpCloudDnsZoneFields.tsx
+++ b/frontend/src/pages/cert-manager/CertificateAuthoritiesPage/components/ExternalCaFields/GcpCloudDnsZoneFields.tsx
@@ -20,6 +20,9 @@ type Props = {
const getProjectIdFromZone = (zoneResourceName?: string | null) =>
zoneResourceName?.split("/")[1] ?? "";
+const getApiErrorMessage = (apiError: unknown, fallback: string) =>
+ (apiError as AxiosError<{ message?: string }>)?.response?.data?.message ?? fallback;
+
const GcpCloudDnsZoneSelect = ({
connectionId,
gcpProjectId,
@@ -43,15 +46,11 @@ const GcpCloudDnsZoneSelect = ({
{ enabled: Boolean(connectionId) && Boolean(gcpProjectId) }
);
- const displayedError =
- error ??
- (isZonesError
- ? {
- message:
- (zonesError as AxiosError<{ message?: string }>)?.response?.data?.message ??
- "Failed to list Cloud DNS zones for this project."
- }
- : undefined);
+ const displayedError = isZonesError
+ ? {
+ message: getApiErrorMessage(zonesError, "Failed to list Cloud DNS zones for this project.")
+ }
+ : error;
return (
<Field className="mb-4">
@@ -85,8 +84,16 @@ const GcpCloudDnsZoneSelect = ({
export const GcpCloudDnsZoneFields = ({ control, connectionId }: Props) => {
const [selectedProjectId, setSelectedProjectId] = useState<string>();
- const { data: projects = [], isPending: isProjectsPending } =
- useGcpConnectionListCloudDnsProjects(connectionId, { enabled: Boolean(connectionId) });
+ const {
+ data: projects = [],
+ isPending: isProjectsPending,
+ isError: isProjectsError,
+ error: projectsError
+ } = useGcpConnectionListCloudDnsProjects(connectionId, { enabled: Boolean(connectionId) });
+
+ const projectsErrorMessage = isProjectsError
+ ? getApiErrorMessage(projectsError, "Failed to list GCP projects for this connection.")
+ : undefined;
return (
<Controller
@@ -117,11 +124,13 @@ export const GcpCloudDnsZoneFields = ({ control, connectionId }: Props) => {
noOptionsMessage={({ inputValue }) =>
inputValue
? "No matching projects"
- : "No projects found. Grant the connection's service account the DNS Administrator role on your project and enable the Cloud DNS API there."
+ : "No projects found. Grant the connection's service account the DNS Administrator and Service Usage Viewer roles on your project and enable the Cloud DNS API there."
}
getOptionLabel={(option) => option.name}
getOptionValue={(option) => option.id}
+ isError={Boolean(projectsErrorMessage)}
/>
+ {projectsErrorMessage && <FieldError>{projectsErrorMessage}</FieldError>}
</Field>
<GcpCloudDnsZoneSelect
connectionId={connectionId}