Skip to content

Commit

Permalink
fix(route53): value is too long error for TXT records (#9984)
Browse files Browse the repository at this point in the history
The solution is to split the record strings over 255 characters into
multiple text strings within the same record.

See https://aws.amazon.com/premiumsupport/knowledge-center/route53-resolve-dkim-text-record-error/

Fixes #8244


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jogold committed Aug 27, 2020
1 parent 352d25a commit fd4be21
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/@aws-cdk/aws-route53/lib/record-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,31 @@ export class TxtRecord extends RecordSet {
super(scope, id, {
...props,
recordType: RecordType.TXT,
target: RecordTarget.fromValues(...props.values.map(v => JSON.stringify(v))),
target: RecordTarget.fromValues(...props.values.map(v => formatTxt(v))),
});
}
}

/**
* Formats a text value for use in a TXT record
*
* Use `JSON.stringify` to correctly escape and enclose in double quotes ("").
*
* DNS TXT records can contain up to 255 characters in a single string. TXT
* record strings over 255 characters must be split into multiple text strings
* within the same record.
*
* @see https://aws.amazon.com/premiumsupport/knowledge-center/route53-resolve-dkim-text-record-error/
*/
function formatTxt(string: string): string {
const result = [];
let idx = 0;
while (idx < string.length) {
result.push(string.slice(idx, idx += 255)); // chunks of 255 characters long
}
return result.map(r => JSON.stringify(r)).join('');
}

/**
* Properties for a SRV record value.
*/
Expand Down
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-route53/test/integ.route53.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@
],
"TTL": "1800"
}
},
"TXT0D5C5ACF": {
"Type": "AWS::Route53::RecordSet",
"Properties": {
"Name": "cdk.test.",
"Type": "TXT",
"HostedZoneId": {
"Ref": "PublicZone2E1C4E34"
},
"ResourceRecords": [
"\"this is a very long stringthis is a very long stringthis is a very long stringthis is a very long stringthis is a very long stringthis is a very long stringthis is a very long stringthis is a very long stringthis is a very long stringthis is a very long s\"\"tring\""
],
"TTL": "1800"
}
}
},
"Outputs": {
Expand Down
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-route53/test/integ.route53.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ new CaaAmazonRecord(stack, 'CaaAmazon', {
zone: publicZone,
});

new TxtRecord(stack, 'TXT', {
zone: publicZone,
values: [
'this is a very long string'.repeat(10),
],
});

new cdk.CfnOutput(stack, 'PrivateZoneId', { value: privateZone.hostedZoneId });
new cdk.CfnOutput(stack, 'PublicZoneId', { value: publicZone.hostedZoneId });

Expand Down
30 changes: 30 additions & 0 deletions packages/@aws-cdk/aws-route53/test/test.record-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,36 @@ export = {
test.done();
},

'TXT record with value longer than 255 chars'(test: Test) {
// GIVEN
const stack = new Stack();

const zone = new route53.HostedZone(stack, 'HostedZone', {
zoneName: 'myzone',
});

// WHEN
new route53.TxtRecord(stack, 'TXT', {
zone,
recordName: 'www',
values: ['hello'.repeat(52)],
});

// THEN
expect(stack).to(haveResource('AWS::Route53::RecordSet', {
Name: 'www.myzone.',
Type: 'TXT',
HostedZoneId: {
Ref: 'HostedZoneDB99F866',
},
ResourceRecords: [
'"hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello""hello"',
],
TTL: '1800',
}));
test.done();
},

'SRV record'(test: Test) {
// GIVEN
const stack = new Stack();
Expand Down

0 comments on commit fd4be21

Please sign in to comment.