Skip to content

Commit 4d58f39

Browse files
committed
feat: add DNS provider integration (Cloudflare, AWS Route53)
Lets you connect a DNS provider and manage its records (create, update, delete) from Settings -> DNS Providers, instead of doing it by hand in Cloudflare/AWS. - dns_provider table, org-scoped, jsonb config as a discriminated union per provider type - Cloudflare adapter (REST, bearer token) - Route53 adapter (AWS SDK, SigV4); records are identified by type:name since Route53 has no native record id, so update handles renames as delete-old + upsert-new - listZones/listRecords/createRecord/updateRecord/deleteRecord/ testConnection wired through a shared DnsClient interface - Settings UI: provider management, zone browser, record CRUD, IP-fill dropdown for A records (panel IP + remote servers) - Access control: dnsProvider resource wired into custom roles - Unit tests for both adapters and the config mask/merge logic
1 parent 3848fa9 commit 4d58f39

29 files changed

Lines changed: 12233 additions & 18 deletions
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
const mockFetch = vi.fn();
4+
global.fetch = mockFetch as typeof fetch;
5+
6+
import { cloudflareClient } from "@dokploy/server/utils/dns/cloudflare";
7+
8+
const jsonResponse = (body: unknown, ok = true, status = 200) =>
9+
({
10+
ok,
11+
status,
12+
json: async () => body,
13+
}) as Response;
14+
15+
const cfSuccess = (result: unknown) =>
16+
jsonResponse({ success: true, errors: [], result });
17+
18+
const cfError = (message: string, status = 400) =>
19+
jsonResponse(
20+
{ success: false, errors: [{ code: 1, message }] },
21+
false,
22+
status,
23+
);
24+
25+
const config = { providerType: "cloudflare" as const, apiToken: "cf-token" };
26+
27+
beforeEach(() => {
28+
mockFetch.mockReset();
29+
});
30+
31+
describe("cloudflareClient.listZones", () => {
32+
it("returns a single page of zones", async () => {
33+
mockFetch.mockResolvedValue(cfSuccess([{ id: "z1", name: "example.com" }]));
34+
35+
const zones = await cloudflareClient.listZones(config);
36+
37+
expect(zones).toEqual([{ id: "z1", name: "example.com" }]);
38+
expect(mockFetch).toHaveBeenCalledTimes(1);
39+
const [url] = mockFetch.mock.calls[0] as [string];
40+
expect(url).toContain("/zones?per_page=50&page=1");
41+
});
42+
43+
it("paginates until a short page is returned", async () => {
44+
const page = (count: number) =>
45+
cfSuccess(
46+
Array.from({ length: count }, (_, i) => ({
47+
id: `z${i}`,
48+
name: `zone${i}.com`,
49+
})),
50+
);
51+
mockFetch.mockResolvedValueOnce(page(50)).mockResolvedValueOnce(page(3));
52+
53+
const zones = await cloudflareClient.listZones(config);
54+
55+
expect(zones).toHaveLength(53);
56+
expect(mockFetch).toHaveBeenCalledTimes(2);
57+
expect(mockFetch.mock.calls[1]?.[0]).toContain("page=2");
58+
});
59+
});
60+
61+
describe("cloudflareClient.listRecords", () => {
62+
it("lists records for a zone", async () => {
63+
mockFetch.mockResolvedValue(
64+
cfSuccess([
65+
{
66+
id: "r1",
67+
type: "A",
68+
name: "app.example.com",
69+
content: "1.2.3.4",
70+
ttl: 1,
71+
},
72+
]),
73+
);
74+
75+
const records = await cloudflareClient.listRecords(config, "zone-1");
76+
77+
expect(records).toEqual([
78+
{
79+
id: "r1",
80+
type: "A",
81+
name: "app.example.com",
82+
content: "1.2.3.4",
83+
ttl: 1,
84+
},
85+
]);
86+
expect(mockFetch.mock.calls[0]?.[0]).toContain("/zones/zone-1/dns_records");
87+
});
88+
});
89+
90+
describe("cloudflareClient.upsertRecord", () => {
91+
it("creates a record when none exists for the name/type", async () => {
92+
mockFetch
93+
.mockResolvedValueOnce(cfSuccess([]))
94+
.mockResolvedValueOnce(cfSuccess({ id: "new-1" }));
95+
96+
const result = await cloudflareClient.upsertRecord(config, {
97+
zoneId: "zone-1",
98+
type: "A",
99+
name: "app.example.com",
100+
content: "1.2.3.4",
101+
});
102+
103+
expect(result).toEqual({ id: "new-1" });
104+
const [, createInit] = mockFetch.mock.calls[1] as [string, RequestInit];
105+
expect(createInit.method).toBe("POST");
106+
});
107+
108+
it("updates the existing record instead of creating a duplicate", async () => {
109+
mockFetch
110+
.mockResolvedValueOnce(cfSuccess([{ id: "existing-1" }]))
111+
.mockResolvedValueOnce(cfSuccess({ id: "existing-1" }));
112+
113+
const result = await cloudflareClient.upsertRecord(config, {
114+
zoneId: "zone-1",
115+
type: "A",
116+
name: "app.example.com",
117+
content: "5.6.7.8",
118+
});
119+
120+
expect(result).toEqual({ id: "existing-1" });
121+
const [updateUrl, updateInit] = mockFetch.mock.calls[1] as [
122+
string,
123+
RequestInit,
124+
];
125+
expect(updateUrl).toContain("/dns_records/existing-1");
126+
expect(updateInit.method).toBe("PUT");
127+
});
128+
129+
it("defaults ttl to 1 (automatic) when not provided", async () => {
130+
mockFetch
131+
.mockResolvedValueOnce(cfSuccess([]))
132+
.mockResolvedValueOnce(cfSuccess({ id: "new-1" }));
133+
134+
await cloudflareClient.upsertRecord(config, {
135+
zoneId: "zone-1",
136+
type: "CNAME",
137+
name: "www.example.com",
138+
content: "example.com",
139+
});
140+
141+
const [, createInit] = mockFetch.mock.calls[1] as [string, RequestInit];
142+
const body = JSON.parse(createInit.body as string);
143+
expect(body.ttl).toBe(1);
144+
});
145+
});
146+
147+
describe("cloudflareClient.updateRecord", () => {
148+
it("PUTs directly to the given record id", async () => {
149+
mockFetch.mockResolvedValue(cfSuccess({ id: "r1" }));
150+
151+
const result = await cloudflareClient.updateRecord(config, "zone-1", "r1", {
152+
type: "A",
153+
name: "app.example.com",
154+
content: "9.9.9.9",
155+
ttl: 300,
156+
});
157+
158+
expect(result).toEqual({ id: "r1" });
159+
const [url, init] = mockFetch.mock.calls[0] as [string, RequestInit];
160+
expect(url).toContain("/zones/zone-1/dns_records/r1");
161+
expect(init.method).toBe("PUT");
162+
expect(JSON.parse(init.body as string)).toEqual({
163+
type: "A",
164+
name: "app.example.com",
165+
content: "9.9.9.9",
166+
ttl: 300,
167+
});
168+
});
169+
});
170+
171+
describe("cloudflareClient.deleteRecord", () => {
172+
it("DELETEs the given record id", async () => {
173+
mockFetch.mockResolvedValue(cfSuccess({}));
174+
175+
await cloudflareClient.deleteRecord(config, "zone-1", "r1");
176+
177+
const [url, init] = mockFetch.mock.calls[0] as [string, RequestInit];
178+
expect(url).toContain("/zones/zone-1/dns_records/r1");
179+
expect(init.method).toBe("DELETE");
180+
});
181+
});
182+
183+
describe("cloudflareClient.testConnection", () => {
184+
it("succeeds when the token can list zones", async () => {
185+
mockFetch.mockResolvedValue(cfSuccess([]));
186+
await expect(
187+
cloudflareClient.testConnection(config),
188+
).resolves.toBeUndefined();
189+
});
190+
191+
it("surfaces Cloudflare's error message on an invalid token", async () => {
192+
mockFetch.mockResolvedValue(cfError("Invalid API Token"));
193+
194+
await expect(cloudflareClient.testConnection(config)).rejects.toThrow(
195+
"Invalid API Token",
196+
);
197+
});
198+
});
199+
200+
describe("cloudflareClient auth header", () => {
201+
it("trims whitespace pasted into the token", async () => {
202+
mockFetch.mockResolvedValue(cfSuccess([]));
203+
204+
await cloudflareClient.testConnection({
205+
providerType: "cloudflare",
206+
apiToken: " cf-token\n",
207+
});
208+
209+
const [, init] = mockFetch.mock.calls[0] as [string, RequestInit];
210+
expect((init.headers as Record<string, string>).Authorization).toBe(
211+
"Bearer cf-token",
212+
);
213+
});
214+
});
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
vi.mock("@dokploy/server/db", () => ({
4+
db: {
5+
query: { dnsProvider: { findFirst: vi.fn(), findMany: vi.fn() } },
6+
insert: vi.fn(),
7+
update: vi.fn(),
8+
delete: vi.fn(),
9+
},
10+
}));
11+
12+
import {
13+
DNS_SECRET_MASK,
14+
maskDnsProviderConfig,
15+
mergeDnsProviderConfig,
16+
} from "@dokploy/server/services/dns-provider";
17+
18+
describe("maskDnsProviderConfig", () => {
19+
it("masks the apiToken for a cloudflare config", () => {
20+
const masked = maskDnsProviderConfig({
21+
providerType: "cloudflare",
22+
apiToken: "real-token",
23+
});
24+
25+
expect(masked).toEqual({
26+
providerType: "cloudflare",
27+
apiToken: DNS_SECRET_MASK,
28+
});
29+
});
30+
31+
it("masks only the secretAccessKey for a route53 config, keeping accessKeyId visible", () => {
32+
const masked = maskDnsProviderConfig({
33+
providerType: "route53",
34+
accessKeyId: "AKIA_VISIBLE",
35+
secretAccessKey: "shh",
36+
});
37+
38+
expect(masked).toEqual({
39+
providerType: "route53",
40+
accessKeyId: "AKIA_VISIBLE",
41+
secretAccessKey: DNS_SECRET_MASK,
42+
});
43+
});
44+
45+
it("leaves an empty sensitive field untouched instead of masking a blank value", () => {
46+
const masked = maskDnsProviderConfig({
47+
providerType: "cloudflare",
48+
apiToken: "",
49+
});
50+
51+
expect(masked).toEqual({ providerType: "cloudflare", apiToken: "" });
52+
});
53+
});
54+
55+
describe("mergeDnsProviderConfig", () => {
56+
it("restores the real secret when the incoming config still has the mask placeholder", () => {
57+
const existing = {
58+
providerType: "cloudflare" as const,
59+
apiToken: "real-token",
60+
};
61+
const incoming = {
62+
providerType: "cloudflare" as const,
63+
apiToken: DNS_SECRET_MASK,
64+
};
65+
66+
expect(mergeDnsProviderConfig(incoming, existing)).toEqual(existing);
67+
});
68+
69+
it("keeps a freshly entered secret instead of the stored one", () => {
70+
const existing = {
71+
providerType: "cloudflare" as const,
72+
apiToken: "old-token",
73+
};
74+
const incoming = {
75+
providerType: "cloudflare" as const,
76+
apiToken: "new-token",
77+
};
78+
79+
expect(mergeDnsProviderConfig(incoming, existing)).toEqual(incoming);
80+
});
81+
82+
it("throws when switching provider type while the field is still masked", () => {
83+
const existing = {
84+
providerType: "cloudflare" as const,
85+
apiToken: "real-token",
86+
};
87+
const incoming = {
88+
providerType: "route53" as const,
89+
accessKeyId: "AKIA",
90+
secretAccessKey: DNS_SECRET_MASK,
91+
};
92+
93+
expect(() => mergeDnsProviderConfig(incoming, existing)).toThrow(
94+
"Credentials must be re-entered",
95+
);
96+
});
97+
98+
it("does not require re-entry for fields that are not sensitive", () => {
99+
const existing = {
100+
providerType: "route53" as const,
101+
accessKeyId: "AKIA_OLD",
102+
secretAccessKey: "old-secret",
103+
};
104+
const incoming = {
105+
providerType: "route53" as const,
106+
accessKeyId: "AKIA_NEW",
107+
secretAccessKey: DNS_SECRET_MASK,
108+
};
109+
110+
expect(mergeDnsProviderConfig(incoming, existing)).toEqual({
111+
providerType: "route53",
112+
accessKeyId: "AKIA_NEW",
113+
secretAccessKey: "old-secret",
114+
});
115+
});
116+
});

0 commit comments

Comments
 (0)