Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cf-custom-resources/lib/custom-domain-app-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ exports.handler = async function (event, context) {
}),
});
appRunnerClient = new AWS.AppRunner();
appHostedZoneID = await domainHostedZoneID(appDNSName);
appHostedZoneID = props.RootHostedZoneId
if (!appHostedZoneID){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we pass RootHostedZoneId to custom resource, instead of looping through what we get from the listHostedZonesByName call and find the hosted zone that's not private?

Copy link
Copy Markdown
Contributor Author

@KollaAdithya KollaAdithya Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have 6 custom resources having listHostedZonesByName call. I need to replicate the same exact logic of PublicHostedZoneId which we already have in the route53 api call for all the six custom resources to filter for public hostedzones.

I am not sure which is better way whether to replicate same logic in all the custom resources or pass these hostedzoneId as parameters in custom resources.
So I went with second approach.

appHostedZoneID = await domainHostedZoneID(appDNSName);
}
switch (event.RequestType) {
case "Create":
case "Update":
Expand Down
33 changes: 26 additions & 7 deletions cf-custom-resources/lib/custom-domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ const writeCustomDomainRecord = async function (
accessDNS,
accessHostedZone,
aliasTypes,
action
action,
rootHostedZoneId,
appHostedZoneId,
envHostedZoneId
) {
const actions = [];
for (const alias of aliases) {
Expand All @@ -111,7 +114,8 @@ const writeCustomDomainRecord = async function (
accessDNS,
accessHostedZone,
aliasType.domain,
action
action,
envHostedZoneId
));
break;
case aliasTypes.AppDomainZone:
Expand All @@ -121,7 +125,8 @@ const writeCustomDomainRecord = async function (
accessDNS,
accessHostedZone,
aliasType.domain,
action
action,
appHostedZoneId
));
break;
case aliasTypes.RootDomainZone:
Expand All @@ -131,7 +136,8 @@ const writeCustomDomainRecord = async function (
accessDNS,
accessHostedZone,
aliasType.domain,
action
action,
rootHostedZoneId
));
break;
// We'll skip if it is the other alias type since it will be in another account's route53.
Expand All @@ -147,9 +153,10 @@ const writeARecord = async function (
accessDNS,
accessHostedZone,
domain,
action
action,
hostedZoneID
) {
let hostedZoneId = hostedZoneCache.get(domain);
let hostedZoneId = hostedZoneID || hostedZoneCache.get(domain);
if (!hostedZoneId) {
const hostedZones = await route53
.listHostedZonesByName({
Expand Down Expand Up @@ -233,6 +240,9 @@ exports.handler = async function (event, context) {
props.PublicAccessHostedZone,
aliasTypes,
changeRecordAction.Upsert,
props.RootHostedZoneId,
props.AppHostedZoneId,
props.EnvHostedZoneId
);
break;
case "Update":
Expand All @@ -244,6 +254,9 @@ exports.handler = async function (event, context) {
props.PublicAccessHostedZone,
aliasTypes,
changeRecordAction.Upsert,
props.RootHostedZoneId,
props.AppHostedZoneId,
props.EnvHostedZoneId
);
// After upserting new aliases, delete unused ones. For example: previously we have ["foo.com", "bar.com"],
// and now the aliases param is updated to just ["foo.com"] then we'll delete "bar.com".
Expand All @@ -261,6 +274,9 @@ exports.handler = async function (event, context) {
props.PublicAccessHostedZone,
aliasTypes,
changeRecordAction.Delete,
props.RootHostedZoneId,
props.AppHostedZoneId,
props.EnvHostedZoneId
);
break;
case "Delete":
Expand All @@ -271,7 +287,10 @@ exports.handler = async function (event, context) {
props.PublicAccessDNS,
props.PublicAccessHostedZone,
aliasTypes,
changeRecordAction.Delete
changeRecordAction.Delete,
props.RootHostedZoneId,
props.AppHostedZoneId,
props.EnvHostedZoneId
);
break;
default:
Expand Down
24 changes: 21 additions & 3 deletions cf-custom-resources/lib/dns-cert-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ const validateCertificate = async function(
options,
envRoute53,
appRoute53,
rootHostedZoneId,
appHostedZoneId,
envHostedZoneId,
certificateARN,
acm
Expand All @@ -221,6 +223,8 @@ const validateCertificate = async function(
options,
envRoute53,
appRoute53,
rootHostedZoneId,
appHostedZoneId,
envHostedZoneId
);

Expand All @@ -241,7 +245,9 @@ const updateHostedZoneRecords = async function (
options,
envRoute53,
appRoute53,
envHostedZoneId
rootHostedZoneId,
appHostedZoneId,
envHostedZoneId,
) {
const promises = [];
for (const option of options) {
Expand All @@ -265,6 +271,7 @@ const updateHostedZoneRecords = async function (
record: option.ResourceRecord,
action: action,
domainName: domainType.domain,
hostedZoneId: appHostedZoneId,
})
);
break;
Expand All @@ -275,6 +282,7 @@ const updateHostedZoneRecords = async function (
record: option.ResourceRecord,
action: action,
domainName: domainType.domain,
hostedZoneId: rootHostedZoneId,
})
);
break;
Expand All @@ -293,6 +301,8 @@ const deleteHostedZoneRecords = async function (
envRoute53,
appRoute53,
acm,
rootHostedZoneId,
appHostedZoneId,
envHostedZoneId
) {
let listCertificatesInput = {};
Expand Down Expand Up @@ -354,6 +364,8 @@ const deleteHostedZoneRecords = async function (
filteredRecordOption,
envRoute53,
appRoute53,
rootHostedZoneId,
appHostedZoneId,
envHostedZoneId
);
} catch (e) {
Expand Down Expand Up @@ -416,6 +428,8 @@ const deleteCertificate = async function (
arn,
certDomain,
region,
rootHostedZoneId,
appHostedZoneId,
envHostedZoneId,
rootDnsRole
) {
Expand Down Expand Up @@ -463,6 +477,8 @@ const deleteCertificate = async function (
envRoute53,
appRoute53,
acm,
rootHostedZoneId,
appHostedZoneId,
envHostedZoneId
);

Expand Down Expand Up @@ -626,7 +642,7 @@ exports.certificateRequestHandler = async function (event, context) {
);
responseData.Arn = physicalResourceId = response.CertificateArn; // Set physicalResourceId as soon as we can.
options = await waitForValidationOptionsToBeReady(response.CertificateArn, sansToUse, acm);
await validateCertificate(options, envRoute53, appRoute53, props.EnvHostedZoneId, response.CertificateArn, acm);
await validateCertificate(options, envRoute53, appRoute53, props.RootHostedZoneId, props.AppHostedZoneId, props.EnvHostedZoneId, response.CertificateArn, acm);
break;
case "Update":
// Exit early if cert doesn't change.
Expand All @@ -644,7 +660,7 @@ exports.certificateRequestHandler = async function (event, context) {
);
responseData.Arn = physicalResourceId = response.CertificateArn;
options = await waitForValidationOptionsToBeReady(response.CertificateArn, sansToUse, acm);
await validateCertificate(options, envRoute53, appRoute53, props.EnvHostedZoneId, response.CertificateArn, acm);
await validateCertificate(options, envRoute53, appRoute53, props.RootHostedZoneId, props.AppHostedZoneId, props.EnvHostedZoneId, response.CertificateArn, acm);
break;
case "Delete":
// If the resource didn't create correctly, the physical resource ID won't be the
Expand All @@ -654,6 +670,8 @@ exports.certificateRequestHandler = async function (event, context) {
physicalResourceId,
certDomain,
props.Region,
props.RootHostedZoneId,
props.AppHostedZoneId,
props.EnvHostedZoneId,
props.RootDNSRole
);
Expand Down
24 changes: 14 additions & 10 deletions cf-custom-resources/lib/dns-delegation.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ const createSubdomainInRoot = async function (
domainName,
subDomain,
nameServers,
rootDnsRole
rootDnsRole,
hostedZoneId
) {
const route53 = new aws.Route53({
credentials: new aws.ChainableTemporaryCredentials({
params: { RoleArn: rootDnsRole },
masterCredentials: new aws.EnvironmentCredentials("AWS"),
}),
});

if (!hostedZoneId) {
const hostedZones = await route53
.listHostedZonesByName({
DNSName: domainName,
Expand All @@ -115,8 +116,8 @@ const createSubdomainInRoot = async function (

// HostedZoneIDs are of the form /hostedzone/1234455, but the actual
// ID is after the last slash.
const hostedZoneId = domainHostedZone.Id.split("/").pop();

hostedZoneId = domainHostedZone.Id.split("/").pop();
}
const changeBatch = await route53
.changeResourceRecordSets({
ChangeBatch: {
Expand Down Expand Up @@ -158,15 +159,16 @@ const deleteSubdomainInRoot = async function (
requestId,
domainName,
subDomain,
rootDnsRole
rootDnsRole,
hostedZoneId
) {
const route53 = new aws.Route53({
credentials: new aws.ChainableTemporaryCredentials({
params: { RoleArn: rootDnsRole },
masterCredentials: new aws.EnvironmentCredentials("AWS"),
}),
});

if (!hostedZoneId) {
const hostedZones = await route53
.listHostedZonesByName({
DNSName: domainName,
Expand All @@ -183,8 +185,8 @@ const deleteSubdomainInRoot = async function (

// HostedZoneIDs are of the form /hostedzone/1234455, but the actual
// ID is after the last slash.
const hostedZoneId = domainHostedZone.Id.split("/").pop();

hostedZoneId = domainHostedZone.Id.split("/").pop();
}
// Find the recordsets for this subdomain, and then remove it
// from the hosted zone.
const recordSets = await route53
Expand Down Expand Up @@ -275,15 +277,17 @@ exports.domainDelegationHandler = async function (event, context) {
props.DomainName,
props.SubdomainName,
props.NameServers,
props.RootDNSRole
props.RootDNSRole,
props.RootHostedZoneId
);
break;
case "Delete":
await deleteSubdomainInRoot(
event.RequestId,
props.DomainName,
props.SubdomainName,
props.RootDNSRole
props.RootDNSRole,
props.RootHostedZoneId
);
break;
default:
Expand Down
14 changes: 11 additions & 3 deletions cf-custom-resources/lib/wkld-cert-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ATTEMPTS_CERTIFICATE_VALIDATED = 19;
const ATTEMPTS_CERTIFICATE_NOT_IN_USE = 12;
const DELAY_CERTIFICATE_VALIDATED_IN_S = 30;

let envHostedZoneID, appName, envName, serviceName, certificateDomain, domainTypes, rootDNSRole, domainName, isCloudFrontCert;
let rootHostedZoneID,appHostedZoneID,envHostedZoneID, appName, envName, serviceName, certificateDomain, domainTypes, rootDNSRole, domainName, isCloudFrontCert;
let defaultSleep = function (ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
};
Expand Down Expand Up @@ -168,6 +168,8 @@ exports.handler = async function (event, context) {
const aliases = new Set(props.Aliases);

// Initialize global variables.
rootHostedZoneID = props.RootHostedZoneId;
appHostedZoneID = props.AppHostedZoneId;
envHostedZoneID = props.EnvHostedZoneId;
envName = props.EnvName;
appName = props.AppName;
Expand Down Expand Up @@ -748,17 +750,23 @@ async function domainResources(alias) {
};
}
if (domainTypes.AppDomainZone.regex.test(alias)) {
if (!appHostedZoneID){
appHostedZoneID = await hostedZoneID.app()
}
return {
domain: domainTypes.AppDomainZone.domain,
route53Client: clients.app.route53(),
hostedZoneID: await hostedZoneID.app(),
hostedZoneID: appHostedZoneID,
};
}
if (domainTypes.RootDomainZone.regex.test(alias)) {
if (!rootHostedZoneID){
rootHostedZoneID = await hostedZoneID.root()
}
return {
domain: domainTypes.RootDomainZone.domain,
route53Client: clients.root.route53(),
hostedZoneID: await hostedZoneID.root(),
hostedZoneID: rootHostedZoneID,
};
}
throw new UnrecognizedDomainTypeError(`unrecognized domain type for ${alias}`);
Expand Down
14 changes: 11 additions & 3 deletions cf-custom-resources/lib/wkld-custom-domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const ATTEMPTS_VALIDATION_OPTIONS_READY = 10;
const ATTEMPTS_RECORD_SETS_CHANGE = 10;
const DELAY_RECORD_SETS_CHANGE_IN_S = 30;

let envHostedZoneID, appName, envName, serviceName, domainTypes, rootDNSRole, domainName;
let rootHostedZoneID,appHostedZoneID,envHostedZoneID, appName, envName, serviceName, domainTypes, rootDNSRole, domainName;
let defaultSleep = function (ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
};
Expand Down Expand Up @@ -157,6 +157,8 @@ exports.handler = async function (event, context) {
const aliases = new Set(props.Aliases);

// Initialize global variables.
rootHostedZoneID = props.RootHostedZoneId;
appHostedZoneID = props.AppHostedZoneId;
envHostedZoneID = props.EnvHostedZoneId;
envName = props.EnvName;
appName = props.AppName;
Expand Down Expand Up @@ -444,17 +446,23 @@ async function domainResources(alias) {
};
}
if (domainTypes.AppDomainZone.regex.test(alias)) {
if (!appHostedZoneID){
appHostedZoneID = await hostedZoneID.app()
}
return {
domain: domainTypes.AppDomainZone.domain,
route53Client: clients.app.route53(),
hostedZoneID: await hostedZoneID.app(),
hostedZoneID: appHostedZoneID,
};
}
if (domainTypes.RootDomainZone.regex.test(alias)) {
if (!rootHostedZoneID){
rootHostedZoneID = await hostedZoneID.root()
}
return {
domain: domainTypes.RootDomainZone.domain,
route53Client: clients.root.route53(),
hostedZoneID: await hostedZoneID.root(),
hostedZoneID: rootHostedZoneID,
};
}
throw new UnrecognizedDomainTypeError(`unrecognized domain type for ${alias}`);
Expand Down
Loading