Skip to content

Commit 8409b8c

Browse files
mitc-gjugeclaude
andcommitted
perf(dns): filter Infomaniak records server-side when upserting
upsertRecord pulled every record of a zone just to find the one it was about to write. The API can filter, so ask it to. The match is still verified locally, because filter[source] matches substrings rather than exact values. Verified against a live account: filter[source]=autoconfig -> [autoconfig] filter[source]=auto -> [autoconfig, autodiscover] filter[source]=disco -> [autodiscover] Letting the filter decide would therefore have upserted "auto" onto the existing "autoconfig" record. The filter narrows what is transferred; the exact comparison still decides. Covered by a test that fails if the first candidate is trusted. The apex was the other thing worth checking, since Infomaniak writes it as ".": filter[source]=. does return the apex records, and an upsert on an existing apex TXT returns its id rather than creating a second one. Suggested by @narcisonunez on Dokploy#5257. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent d7884c7 commit 8409b8c

2 files changed

Lines changed: 78 additions & 6 deletions

File tree

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,58 @@ describe("infomaniakClient.upsertRecord", () => {
326326

327327
expect(lastBody().target).toBe('"token-value"');
328328
});
329+
330+
it("queries the API with a source and type filter instead of the whole zone", async () => {
331+
mockFetch
332+
.mockResolvedValueOnce(ikSuccess([]))
333+
.mockResolvedValueOnce(ikSuccess({ id: 50 }));
334+
335+
await infomaniakClient.upsertRecord(config, {
336+
zoneId: "example.com",
337+
type: "A",
338+
name: "app.example.com",
339+
content: "1.2.3.4",
340+
});
341+
342+
const [url] = mockFetch.mock.calls[0] as [string];
343+
expect(url).toContain("filter%5Bsource%5D=app");
344+
expect(url).toContain("filter%5Btypes%5D%5B%5D=A");
345+
});
346+
347+
it("ignores a partial filter hit rather than overwriting a different record", async () => {
348+
// filter[source] matches substrings: asking for "auto" also returns
349+
// "autoconfig" and "autodiscover". Trusting it would overwrite one of them.
350+
mockFetch
351+
.mockResolvedValueOnce(
352+
ikSuccess([
353+
{
354+
id: 61,
355+
type: "CNAME",
356+
source: "autoconfig",
357+
target: "a.example.net",
358+
ttl: 300,
359+
},
360+
{
361+
id: 62,
362+
type: "CNAME",
363+
source: "autodiscover",
364+
target: "b.example.net",
365+
ttl: 300,
366+
},
367+
]),
368+
)
369+
.mockResolvedValueOnce(ikSuccess({ id: 63 }));
370+
371+
const result = await infomaniakClient.upsertRecord(config, {
372+
zoneId: "example.com",
373+
type: "CNAME",
374+
name: "auto.example.com",
375+
content: "c.example.net",
376+
});
377+
378+
expect(result).toEqual({ id: "63" });
379+
expect(lastCall()[1].method).toBe("POST");
380+
});
329381
});
330382

331383
describe("infomaniakClient.updateRecord", () => {

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,31 @@ const listZoneRecords = async (config: InfomaniakConfig, zoneId: string) =>
148148
`/2/zones/${encodeURIComponent(zoneId)}/records?with=records_description`,
149149
);
150150

151+
// The API filters server-side, which avoids pulling a whole zone just to find
152+
// one record. The match is still checked here: filter[source] is documented with
153+
// a bare subdomain example, so nothing guarantees it compares exactly the way
154+
// toSource writes the apex, and a filter that silently over-matches would
155+
// otherwise turn an update into a duplicate.
156+
const findRecord = async (
157+
config: InfomaniakConfig,
158+
zoneId: string,
159+
type: string,
160+
source: string,
161+
) => {
162+
const query = new URLSearchParams({
163+
"filter[source]": source,
164+
"filter[types][]": type,
165+
});
166+
const candidates = await ikFetch<InfomaniakRecord[]>(
167+
config,
168+
`/2/zones/${encodeURIComponent(zoneId)}/records?${query}`,
169+
);
170+
return candidates.find(
171+
(candidate) =>
172+
candidate.type === type && normalizeSource(candidate.source) === source,
173+
);
174+
};
175+
151176
export const infomaniakClient: DnsClient<InfomaniakConfig> = {
152177
async listZones(config) {
153178
const domains = await listDomainProducts(config);
@@ -171,12 +196,7 @@ export const infomaniakClient: DnsClient<InfomaniakConfig> = {
171196

172197
async upsertRecord(config, record) {
173198
const source = toSource(record.name, record.zoneId);
174-
const existing = await listZoneRecords(config, record.zoneId);
175-
const match = existing.find(
176-
(candidate) =>
177-
candidate.type === record.type &&
178-
normalizeSource(candidate.source) === source,
179-
);
199+
const match = await findRecord(config, record.zoneId, record.type, source);
180200

181201
const body = JSON.stringify(recordPayload(record, record.zoneId));
182202
const zone = encodeURIComponent(record.zoneId);

0 commit comments

Comments
 (0)