Skip to content

Commit 80a6baf

Browse files
mitc-gjugeclaude
andcommitted
fix(dns): restore the original OVH record when a type change fails
Changing a record's type deletes the record then recreates it with the new type, because OVH's update payload carries no fieldType. If the creation failed the name was left with nothing and no rollback. The delete still has to come first, since OVH rejects a CNAME that would sit alongside other data on the same name. So on a failed creation the original record is put back from the copy already fetched before the delete, and the original error is rethrown. If the restore fails too, the error names the record that has to be recreated by hand. Reported by Greptile on Dokploy#5258. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent e629248 commit 80a6baf

2 files changed

Lines changed: 114 additions & 9 deletions

File tree

‎apps/dokploy/__test__/dns/ovh.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,72 @@ describe("ovhClient.updateRecord", () => {
365365
expect(JSON.parse(calls[2]?.[1].body as string).fieldType).toBe("CNAME");
366366
expect(calls[3]?.[0]).toContain("/refresh");
367367
});
368+
369+
it("restores the original record when the replacement fails", async () => {
370+
const cfg = freshConfig();
371+
const original = {
372+
id: 4,
373+
zone: "example.com",
374+
fieldType: "A",
375+
subDomain: "app",
376+
target: "1.1.1.1",
377+
ttl: 60,
378+
};
379+
mockApi(
380+
ovhSuccess(original),
381+
ovhSuccess(null),
382+
ovhError("Invalid target", 400),
383+
ovhSuccess({ id: 12 }),
384+
ovhSuccess(null),
385+
);
386+
387+
await expect(
388+
ovhClient.updateRecord(cfg, "example.com", "4", {
389+
type: "CNAME",
390+
name: "app.example.com",
391+
content: "not a valid target",
392+
}),
393+
).rejects.toThrow("Invalid target");
394+
395+
const calls = apiCalls();
396+
expect(calls[1]?.[1].method).toBe("DELETE");
397+
expect(calls[2]?.[1].method).toBe("POST");
398+
// The original record is put back with its own type, target and ttl.
399+
expect(JSON.parse(calls[3]?.[1].body as string)).toEqual({
400+
fieldType: "A",
401+
subDomain: "app",
402+
target: "1.1.1.1",
403+
ttl: 60,
404+
});
405+
expect(calls[4]?.[0]).toContain("/refresh");
406+
});
407+
408+
it("reports the lost record when the restore also fails", async () => {
409+
const cfg = freshConfig();
410+
mockApi(
411+
ovhSuccess({
412+
id: 4,
413+
zone: "example.com",
414+
fieldType: "A",
415+
subDomain: "app",
416+
target: "1.1.1.1",
417+
ttl: 60,
418+
}),
419+
ovhSuccess(null),
420+
ovhError("Invalid target", 400),
421+
ovhError("Service unavailable", 503),
422+
);
423+
424+
await expect(
425+
ovhClient.updateRecord(cfg, "example.com", "4", {
426+
type: "CNAME",
427+
name: "app.example.com",
428+
content: "not a valid target",
429+
}),
430+
).rejects.toThrow(
431+
/Recreate it manually: A app\.example\.com -> 1\.1\.1\.1/,
432+
);
433+
});
368434
});
369435

370436
describe("ovhClient.deleteRecord", () => {

‎packages/server/src/utils/dns/ovh.ts‎

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,34 @@ const refreshZone = async (config: OvhConfig, zone: string) => {
171171
});
172172
};
173173

174+
// Used to undo the delete half of a type change when the replacement fails.
175+
const restoreRecord = async (
176+
config: OvhConfig,
177+
zone: string,
178+
record: OvhRecord,
179+
cause: unknown,
180+
) => {
181+
try {
182+
await ovhFetch(config, `/domain/zone/${encodeURIComponent(zone)}/record`, {
183+
method: "POST",
184+
body: {
185+
fieldType: record.fieldType,
186+
subDomain: record.subDomain ?? "",
187+
target: record.target,
188+
...(record.ttl === null ? {} : { ttl: record.ttl }),
189+
},
190+
});
191+
await refreshZone(config, zone);
192+
} catch {
193+
const name = toFqdn(record.subDomain, zone);
194+
throw new Error(
195+
`OVH: could not replace the record and could not restore the original one, which has been deleted. Recreate it manually: ${record.fieldType} ${name} -> ${record.target}. Original failure: ${
196+
cause instanceof Error ? cause.message : String(cause)
197+
}`,
198+
);
199+
}
200+
};
201+
174202
const recordBody = (
175203
record: { name: string; content: string; ttl?: number },
176204
zone: string,
@@ -244,19 +272,30 @@ export const ovhClient: DnsClient<OvhConfig> = {
244272
);
245273

246274
// The update payload carries no fieldType, so switching a record's type
247-
// means replacing it.
275+
// means replacing it. The delete has to come first: OVH rejects a CNAME
276+
// that would sit alongside other data on the same name. If the creation
277+
// then fails, put the original record back rather than leaving the name
278+
// with nothing.
248279
if (existing.fieldType !== record.type) {
249280
await ovhFetch(config, `/domain/zone/${zone}/record/${recordId}`, {
250281
method: "DELETE",
251282
});
252-
const created = await ovhFetch<OvhRecord>(
253-
config,
254-
`/domain/zone/${zone}/record`,
255-
{
256-
method: "POST",
257-
body: { fieldType: record.type, ...recordBody(record, zoneId) },
258-
},
259-
);
283+
284+
let created: OvhRecord;
285+
try {
286+
created = await ovhFetch<OvhRecord>(
287+
config,
288+
`/domain/zone/${zone}/record`,
289+
{
290+
method: "POST",
291+
body: { fieldType: record.type, ...recordBody(record, zoneId) },
292+
},
293+
);
294+
} catch (error) {
295+
await restoreRecord(config, zoneId, existing, error);
296+
throw error;
297+
}
298+
260299
await refreshZone(config, zoneId);
261300
return { id: String(created.id) };
262301
}

0 commit comments

Comments
 (0)