Skip to content

Commit 38e855b

Browse files
committed
fix(dns): preserve every value of a Route53 record set
Route53 returns a record set as a list of values, but listing joined them into one string and writing sent that string back as a single ResourceRecord. Editing a multi-value NS, MX or TXT set therefore either failed validation or collapsed the set into one bogus value, and creating a record for a name that already had values replaced them silently. Values are now newline separated end to end: listing joins with a newline, writing splits back into one ResourceRecord per line, and creating merges into the existing set instead of replacing it. Unquoted TXT values get the quotes Route53 requires. The record panel shows a textarea for Route53 and validates every line.
1 parent 130be40 commit 38e855b

3 files changed

Lines changed: 162 additions & 16 deletions

File tree

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

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,15 @@ describe("route53Client.listRecords", () => {
142142

143143
const records = await route53Client.listRecords(config, "Z123");
144144

145-
expect(records[0]?.content).toBe("ns1.example.com, ns2.example.com");
145+
expect(records[0]?.content).toBe("ns1.example.com\nns2.example.com");
146146
});
147147
});
148148

149149
describe("route53Client.upsertRecord", () => {
150-
it("sends a single UPSERT change", async () => {
151-
send.mockResolvedValueOnce({});
150+
it("sends a single UPSERT change when nothing exists yet", async () => {
151+
send
152+
.mockResolvedValueOnce({ ResourceRecordSets: [] })
153+
.mockResolvedValueOnce({});
152154

153155
const result = await route53Client.upsertRecord(config, {
154156
zoneId: "Z123",
@@ -158,7 +160,7 @@ describe("route53Client.upsertRecord", () => {
158160
});
159161

160162
expect(result).toEqual({ id: "A:app.example.com" });
161-
const command = send.mock.calls[0]?.[0] as HasInput;
163+
const command = send.mock.calls[1]?.[0] as HasInput;
162164
expect(command.input.HostedZoneId).toBe("Z123");
163165
expect(command.input.ChangeBatch.Changes).toEqual([
164166
{
@@ -172,6 +174,82 @@ describe("route53Client.upsertRecord", () => {
172174
},
173175
]);
174176
});
177+
178+
it("keeps the values already in the record set", async () => {
179+
send
180+
.mockResolvedValueOnce({
181+
ResourceRecordSets: [
182+
{
183+
Name: "app.example.com.",
184+
Type: "A",
185+
TTL: 300,
186+
ResourceRecords: [{ Value: "1.1.1.1" }, { Value: "2.2.2.2" }],
187+
},
188+
],
189+
})
190+
.mockResolvedValueOnce({});
191+
192+
await route53Client.upsertRecord(config, {
193+
zoneId: "Z123",
194+
type: "A",
195+
name: "app.example.com",
196+
content: "3.3.3.3",
197+
});
198+
199+
const command = send.mock.calls[1]?.[0] as HasInput;
200+
expect(
201+
command.input.ChangeBatch.Changes[0].ResourceRecordSet.ResourceRecords,
202+
).toEqual([
203+
{ Value: "1.1.1.1" },
204+
{ Value: "2.2.2.2" },
205+
{ Value: "3.3.3.3" },
206+
]);
207+
});
208+
209+
it("does not duplicate a value that is already in the record set", async () => {
210+
send
211+
.mockResolvedValueOnce({
212+
ResourceRecordSets: [
213+
{
214+
Name: "app.example.com.",
215+
Type: "A",
216+
TTL: 300,
217+
ResourceRecords: [{ Value: "1.1.1.1" }],
218+
},
219+
],
220+
})
221+
.mockResolvedValueOnce({});
222+
223+
await route53Client.upsertRecord(config, {
224+
zoneId: "Z123",
225+
type: "A",
226+
name: "app.example.com",
227+
content: "1.1.1.1",
228+
});
229+
230+
const command = send.mock.calls[1]?.[0] as HasInput;
231+
expect(
232+
command.input.ChangeBatch.Changes[0].ResourceRecordSet.ResourceRecords,
233+
).toEqual([{ Value: "1.1.1.1" }]);
234+
});
235+
236+
it("wraps unquoted TXT values in double quotes", async () => {
237+
send
238+
.mockResolvedValueOnce({ ResourceRecordSets: [] })
239+
.mockResolvedValueOnce({});
240+
241+
await route53Client.upsertRecord(config, {
242+
zoneId: "Z123",
243+
type: "TXT",
244+
name: "example.com",
245+
content: 'v=spf1 ~all\n"already quoted"',
246+
});
247+
248+
const command = send.mock.calls[1]?.[0] as HasInput;
249+
expect(
250+
command.input.ChangeBatch.Changes[0].ResourceRecordSet.ResourceRecords,
251+
).toEqual([{ Value: '"v=spf1 ~all"' }, { Value: '"already quoted"' }]);
252+
});
175253
});
176254

177255
describe("route53Client.updateRecord", () => {
@@ -222,6 +300,25 @@ describe("route53Client.updateRecord", () => {
222300
]);
223301
});
224302

303+
it("keeps every line of a multi-value record set", async () => {
304+
send.mockResolvedValueOnce({});
305+
306+
await route53Client.updateRecord(config, "Z123", "NS:example.com", {
307+
type: "NS",
308+
name: "example.com",
309+
content: "ns1.example.com\nns2.example.com\n\n ns3.example.com ",
310+
});
311+
312+
const command = send.mock.calls[0]?.[0] as HasInput;
313+
expect(
314+
command.input.ChangeBatch.Changes[0].ResourceRecordSet.ResourceRecords,
315+
).toEqual([
316+
{ Value: "ns1.example.com" },
317+
{ Value: "ns2.example.com" },
318+
{ Value: "ns3.example.com" },
319+
]);
320+
});
321+
225322
it("skips the DELETE when the old record no longer exists", async () => {
226323
send
227324
.mockResolvedValueOnce({ ResourceRecordSets: [] })

‎apps/dokploy/components/dashboard/settings/dns/dns-record-panel.tsx‎

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
SelectTrigger,
2323
SelectValue,
2424
} from "@/components/ui/select";
25+
import { Textarea } from "@/components/ui/textarea";
2526
import { cn } from "@/lib/utils";
2627
import { api } from "@/utils/api";
2728

@@ -82,8 +83,20 @@ const DnsRecordSchema = z
8283
proxied: z.boolean(),
8384
})
8485
.superRefine((data, ctx) => {
86+
const values = data.content
87+
.split("\n")
88+
.map((value) => value.trim())
89+
.filter(Boolean);
90+
if (!values.length) {
91+
ctx.addIssue({
92+
code: z.ZodIssueCode.custom,
93+
path: ["content"],
94+
message: "Content is required",
95+
});
96+
return;
97+
}
8598
const pattern = structuredValuePatterns[data.type];
86-
if (pattern && !pattern.test(data.content.trim())) {
99+
if (pattern && !values.every((value) => pattern.test(value))) {
87100
ctx.addIssue({
88101
code: z.ZodIssueCode.custom,
89102
path: ["content"],
@@ -161,6 +174,7 @@ export const DnsRecordPanel = ({
161174
const proxied = form.watch("proxied");
162175
const canProxy =
163176
provider?.providerType === "cloudflare" && PROXIABLE_TYPES.includes(type);
177+
const supportsMultipleValues = provider?.providerType === "route53";
164178
const usesAutomaticTtl = canProxy && proxied;
165179

166180
const onSubmit = async (data: DnsRecordForm) => {
@@ -286,13 +300,29 @@ export const DnsRecordPanel = ({
286300
<FormItem>
287301
<FormLabel>{valueFields[type].label}</FormLabel>
288302
<FormControl>
289-
<Input
290-
placeholder={valueFields[type].placeholder}
291-
{...field}
292-
/>
303+
{supportsMultipleValues ? (
304+
<Textarea
305+
className="min-h-[60px] font-mono text-xs"
306+
placeholder={valueFields[type].placeholder}
307+
{...field}
308+
/>
309+
) : (
310+
<Input
311+
placeholder={valueFields[type].placeholder}
312+
{...field}
313+
/>
314+
)}
293315
</FormControl>
294-
{valueFields[type].hint && (
295-
<FormDescription>{valueFields[type].hint}</FormDescription>
316+
{(supportsMultipleValues || valueFields[type].hint) && (
317+
<FormDescription>
318+
{[
319+
valueFields[type].hint,
320+
supportsMultipleValues &&
321+
"One value per line: every line belongs to the same record set.",
322+
]
323+
.filter(Boolean)
324+
.join(" ")}
325+
</FormDescription>
296326
)}
297327
<FormMessage />
298328
</FormItem>

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ const findExactRecordSet = async (
6868
return undefined;
6969
};
7070

71+
const formatValue = (type: DnsRecordType, value: string) =>
72+
type === "TXT" && !value.startsWith('"') ? JSON.stringify(value) : value;
73+
7174
const buildRecordSet = (record: {
7275
type: DnsRecordType;
7376
name: string;
@@ -77,7 +80,11 @@ const buildRecordSet = (record: {
7780
Name: ensureTrailingDot(record.name),
7881
Type: record.type as ResourceRecordSet["Type"],
7982
TTL: record.ttl ?? 300,
80-
ResourceRecords: [{ Value: record.content }],
83+
ResourceRecords: record.content
84+
.split("\n")
85+
.map((value) => value.trim())
86+
.filter(Boolean)
87+
.map((value) => ({ Value: formatValue(record.type, value) })),
8188
});
8289

8390
export const route53Client: DnsClient<Route53Config> = {
@@ -129,7 +136,7 @@ export const route53Client: DnsClient<Route53Config> = {
129136
id: buildRecordId(set.Type, set.Name),
130137
type: set.Type,
131138
name: stripTrailingDot(set.Name),
132-
content: set.ResourceRecords.map((r) => r.Value).join(", "),
139+
content: set.ResourceRecords.map((r) => r.Value).join("\n"),
133140
ttl: set.TTL ?? 300,
134141
});
135142
}
@@ -141,13 +148,25 @@ export const route53Client: DnsClient<Route53Config> = {
141148

142149
async upsertRecord(config, record) {
143150
const client = createClient(config);
151+
const existing = await findExactRecordSet(
152+
config,
153+
record.zoneId,
154+
record.type,
155+
record.name,
156+
);
157+
const recordSet = buildRecordSet(record);
158+
if (existing?.ResourceRecords?.length) {
159+
const values = new Set([
160+
...existing.ResourceRecords.map((r) => r.Value),
161+
...(recordSet.ResourceRecords ?? []).map((r) => r.Value),
162+
]);
163+
recordSet.ResourceRecords = [...values].map((Value) => ({ Value }));
164+
}
144165
await client.send(
145166
new ChangeResourceRecordSetsCommand({
146167
HostedZoneId: record.zoneId,
147168
ChangeBatch: {
148-
Changes: [
149-
{ Action: "UPSERT", ResourceRecordSet: buildRecordSet(record) },
150-
],
169+
Changes: [{ Action: "UPSERT", ResourceRecordSet: recordSet }],
151170
},
152171
}),
153172
);

0 commit comments

Comments
 (0)