-
Notifications
You must be signed in to change notification settings - Fork 21
feat: add split horizon dns construct #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
IamFlowZ
wants to merge
7
commits into
open-constructs:main
Choose a base branch
from
IamFlowZ:feat-add-split-horizon-dns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
14cf292
getting everything imported
IamFlowZ cac54ee
adding interface
IamFlowZ e1e2777
unit tests for certificate
IamFlowZ 42ba5a9
verify records marked as private cant be created if disallowPrivate i…
IamFlowZ 8580a32
integ tests refusing to work
IamFlowZ 166b135
add factory methods for resource creation
IamFlowZ cf00407
fixing props for createdHostedZone
IamFlowZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Split Horizon DNS CDK Construct | ||
|
|
||
| ## Overview | ||
|
|
||
|
|
||
| ## Usage | ||
|
|
||
|
|
||
| ### Basic Example | ||
|
|
||
|
|
||
| ### Advanced Example |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './split-horizon-dns'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import * as cdk from 'aws-cdk-lib'; | ||
| import * as acm from 'aws-cdk-lib/aws-certificatemanager'; | ||
| import * as ec2 from 'aws-cdk-lib/aws-ec2'; | ||
| import * as route53 from 'aws-cdk-lib/aws-route53'; | ||
| import { Construct } from 'constructs'; | ||
|
|
||
| const isAliasRecordTarget = (value: route53.IAliasRecordTarget | Array<string>): value is route53.IAliasRecordTarget => { | ||
| return !('length' in value); | ||
| }; | ||
|
|
||
| type ARecordArray = Array<route53.ARecord>; | ||
|
|
||
| export interface AliasTarget { | ||
| readonly target: route53.IAliasRecordTarget | Array<string>; | ||
| readonly private?: boolean; | ||
| readonly public?: boolean; | ||
| readonly ttl?: cdk.Duration; | ||
| } | ||
|
|
||
| /** | ||
| * @param zoneName The DNS name of the zone to create | ||
| * @param existingPublicZone An existing public zone to use instead of creating a new one | ||
| * @param existingPrivateZone An existing private zone to use instead of creating a new one | ||
| * @param disallowPrivateZone Override the default behavior of creating a private zone. Will also block adding private records. | ||
| * @param includeCertificate Whether to create an ACM certificate for the zone | ||
| * @param certAlternateNames Alternate names to include in the certificate | ||
| * @param privateZoneVpcs VPCs to associate with the private zone | ||
| * @param targets Targets to create A records for | ||
| */ | ||
| export interface ISplitHorizonDnsProps { | ||
| readonly zoneName: string; | ||
| readonly recordName?: string; | ||
| readonly existingPublicZone?: route53.IHostedZone; | ||
| readonly existingPrivateZone?: route53.IHostedZone; | ||
| readonly disallowPrivateZone?: boolean; | ||
| readonly certAlternateNames?: Array<string>; | ||
| readonly privateZoneVpcs?: Array<ec2.Vpc>; | ||
| readonly targets: Array<AliasTarget>; | ||
| readonly includeCertificate?: boolean; | ||
| } | ||
|
|
||
| export interface ISplitHorizonDns { | ||
| publicZone: route53.IHostedZone; | ||
| privateZone?: route53.IHostedZone; | ||
| records: Array<ARecordArray>; | ||
| } | ||
|
|
||
| interface createHostedZoneProps { | ||
| zoneName: string; | ||
| vpcs?: Array<ec2.IVpc>; | ||
| } | ||
|
|
||
| interface createCertificateProps { | ||
|
|
||
| domainName: string; | ||
| subjectAlternativeNames?: Array<string>; | ||
| } | ||
| /** | ||
| * Creates a public and private zone for a given domain name, and creates A records for the given targets. | ||
| * @property publicZone The public zone created | ||
| * @property privateZone The private zone created | ||
| * @property records The A records created | ||
| */ | ||
| export class SplitHorizonDns extends Construct implements ISplitHorizonDns { | ||
| public publicZone: route53.IHostedZone; | ||
|
|
||
| public privateZone?: route53.IHostedZone; | ||
|
|
||
| public records: Array<ARecordArray>; | ||
|
|
||
| public certificate?: acm.ICertificate; | ||
|
|
||
| constructor(scope: Construct, id: string, private props: ISplitHorizonDnsProps) { | ||
| super(scope, id); | ||
|
|
||
| const { | ||
| zoneName, | ||
| existingPublicZone, | ||
| existingPrivateZone, | ||
| disallowPrivateZone, | ||
| includeCertificate, | ||
| certAlternateNames, | ||
| privateZoneVpcs, | ||
| targets, | ||
| } = this.props; | ||
|
|
||
| if (existingPublicZone) { | ||
| this.publicZone = existingPublicZone; | ||
| } else { | ||
| this.publicZone = this.createHostedZone('PublicZone', { | ||
| zoneName: zoneName, | ||
| }); | ||
| } | ||
|
|
||
| if (includeCertificate) { | ||
| this.certificate = this.createCertificate('Certificate', { | ||
| domainName: zoneName, | ||
| subjectAlternativeNames: certAlternateNames, | ||
| }); | ||
| } | ||
|
|
||
| if (disallowPrivateZone) { | ||
| console.log('Private zone creation is disallowed. Skipping...'); | ||
| } else if (existingPrivateZone) { | ||
| this.privateZone = existingPrivateZone; | ||
| } else { | ||
| this.privateZone = this.createHostedZone('PrivateZone', { | ||
| zoneName: zoneName, | ||
| vpcs: privateZoneVpcs, | ||
| }); | ||
| } | ||
|
|
||
| this.records = targets.reduce((accu: Array<ARecordArray>, curr: AliasTarget) => { | ||
| let target; | ||
|
|
||
| if (isAliasRecordTarget(curr.target)) { | ||
| target = route53.RecordTarget.fromAlias(curr.target); | ||
| } else { | ||
| target = route53.RecordTarget.fromValues(...curr.target); | ||
| } | ||
|
|
||
| if (!curr.private && !curr.public) { | ||
| console.error(`Neither public nor private was specified for ${JSON.stringify(curr)}. Omitting...`); | ||
| return accu; | ||
| } | ||
|
|
||
| const records = [] as ARecordArray; | ||
| if (curr.public) { | ||
| const publicARecord = new route53.ARecord(this, `${curr.target.toString()}PublicARecord`, { | ||
| zone: this.publicZone, | ||
| target: target, | ||
| ttl: curr.ttl, | ||
| recordName: props.recordName, | ||
| }); | ||
| records.push(publicARecord); | ||
| } | ||
|
|
||
| if (disallowPrivateZone) { | ||
| console.log('Private zone creation is disallowed. Skipping...'); | ||
| } else if (curr.private && this.privateZone) { | ||
| const privateARecord = new route53.ARecord(this, `${curr.target.toString()}PrivateARecord`, { | ||
| zone: this.privateZone, | ||
| target: target, | ||
| recordName: props.recordName, | ||
| }); | ||
| records.push(privateARecord); | ||
| } else if (curr.private && !this.privateZone) { | ||
| console.error(`Private zone was specified for ${curr}, but private zone was not created. Omitting...`); | ||
| } | ||
| accu.push(records); | ||
| return accu; | ||
| }, [] as Array<ARecordArray>); | ||
| } | ||
|
|
||
| protected createHostedZone(zoneId: string, props: createHostedZoneProps): route53.IHostedZone { | ||
| return new route53.HostedZone(this, zoneId, props); | ||
| } | ||
|
|
||
| protected createCertificate(certId: string, props: createCertificateProps): acm.ICertificate { | ||
| return new acm.Certificate(this, certId, props); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| // Export constructs here | ||
| export * as aws_cur from './aws-cur'; | ||
| export * as aws_ec2 from './aws-ec2'; | ||
| export * as aws_ec2 from './aws-ec2'; | ||
| export * as aws_route53 from './aws-route53'; |
19 changes: 19 additions & 0 deletions
19
....split-horizon-dns.ts.snapshot/SplitHorizonDnsDefaultTestDeployAssertB4E57D9C.assets.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "version": "36.0.0", | ||
| "files": { | ||
| "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { | ||
| "source": { | ||
| "path": "SplitHorizonDnsDefaultTestDeployAssertB4E57D9C.template.json", | ||
| "packaging": "file" | ||
| }, | ||
| "destinations": { | ||
| "current_account-current_region": { | ||
| "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", | ||
| "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", | ||
| "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "dockerImages": {} | ||
| } |
36 changes: 36 additions & 0 deletions
36
...plit-horizon-dns.ts.snapshot/SplitHorizonDnsDefaultTestDeployAssertB4E57D9C.template.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| "Parameters": { | ||
| "BootstrapVersion": { | ||
| "Type": "AWS::SSM::Parameter::Value<String>", | ||
| "Default": "/cdk-bootstrap/hnb659fds/version", | ||
| "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" | ||
| } | ||
| }, | ||
| "Rules": { | ||
| "CheckBootstrapVersion": { | ||
| "Assertions": [ | ||
| { | ||
| "Assert": { | ||
| "Fn::Not": [ | ||
| { | ||
| "Fn::Contains": [ | ||
| [ | ||
| "1", | ||
| "2", | ||
| "3", | ||
| "4", | ||
| "5" | ||
| ], | ||
| { | ||
| "Ref": "BootstrapVersion" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }, | ||
| "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
...oute53/cdk-integ.out.integ.split-horizon-dns.ts.snapshot/SplitHorizonDnsStack.assets.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| { | ||
| "version": "36.0.0", | ||
| "files": { | ||
| "dd5711540f04e06aa955d7f4862fc04e8cdea464cb590dae91ed2976bb78098e": { | ||
| "source": { | ||
| "path": "asset.dd5711540f04e06aa955d7f4862fc04e8cdea464cb590dae91ed2976bb78098e", | ||
| "packaging": "zip" | ||
| }, | ||
| "destinations": { | ||
| "697863601562-us-east-1": { | ||
| "bucketName": "cdk-hnb659fds-assets-697863601562-us-east-1", | ||
| "objectKey": "dd5711540f04e06aa955d7f4862fc04e8cdea464cb590dae91ed2976bb78098e.zip", | ||
| "region": "us-east-1", | ||
| "assumeRoleArn": "arn:${AWS::Partition}:iam::697863601562:role/cdk-hnb659fds-file-publishing-role-697863601562-us-east-1" | ||
| } | ||
| } | ||
| }, | ||
| "251d0dc1993c060b2439b54cb243f4ead92e308a2b7c7a5f0c5e0e3a565d6d51": { | ||
| "source": { | ||
| "path": "SplitHorizonDnsStack.template.json", | ||
| "packaging": "file" | ||
| }, | ||
| "destinations": { | ||
| "697863601562-us-east-1": { | ||
| "bucketName": "cdk-hnb659fds-assets-697863601562-us-east-1", | ||
| "objectKey": "251d0dc1993c060b2439b54cb243f4ead92e308a2b7c7a5f0c5e0e3a565d6d51.json", | ||
| "region": "us-east-1", | ||
| "assumeRoleArn": "arn:${AWS::Partition}:iam::697863601562:role/cdk-hnb659fds-file-publishing-role-697863601562-us-east-1" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "dockerImages": {} | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please make this protected so sub-classes can access this info
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I follow why a protected constructor is needed here?