From 08ec346817889c1e067879b2403a6e01ad97c646 Mon Sep 17 00:00:00 2001 From: rddimon Date: Fri, 1 Dec 2023 16:41:49 +0200 Subject: [PATCH 1/2] AT-10914: Fix hosted zone filtering https://amplify-education.atlassian.net/browse/AT-10914 --- CHANGELOG.md | 5 +++ package.json | 2 +- src/aws/acm-wrapper.ts | 6 ++-- src/aws/route53-wrapper.ts | 5 ++- src/index.ts | 2 -- .../debug/pr-example/serverless.yml | 2 +- test/unit-tests/aws/route53-wrapper.test.ts | 35 +++++++++++++++++++ 7 files changed, 50 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b980cfc..239a1ac9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [7.3.1] - 2023-12-01 + +### Fixed +- Fixed hosted zone filtering. + ## [7.3.0] - 2023-11-30 ### Fixed diff --git a/package.json b/package.json index ba6726ee..557e01c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-domain-manager", - "version": "7.3.0", + "version": "7.3.1", "engines": { "node": ">=14" }, diff --git a/src/aws/acm-wrapper.ts b/src/aws/acm-wrapper.ts index c7b8400e..eddb860a 100644 --- a/src/aws/acm-wrapper.ts +++ b/src/aws/acm-wrapper.ts @@ -9,6 +9,7 @@ import { import Globals from "../globals"; import DomainConfig = require("../models/domain-config"); import {getAWSPagedResults} from "../utils"; +import Logging from "../logging"; const certStatuses = [ CertificateStatus.PENDING_VALIDATION, @@ -47,8 +48,9 @@ class ACMWrapper { certificateArn = this.getCertArnByCertName(certificates, certificateName); } else { certificateName = domain.givenDomainName; - certificateArn = this.getCertArnByDomainName(certificates, certificateName); + certificateArn = ACMWrapper.getCertArnByDomainName(certificates, certificateName); } + Logging.logInfo(`Found a certificate ARN: '${certificateArn}'`); } catch (err) { throw Error(`Could not search certificates in Certificate Manager.\n${err.message}`); } @@ -66,7 +68,7 @@ class ACMWrapper { return null; } - private getCertArnByDomainName(certificates, domainName): string { + private static getCertArnByDomainName(certificates, domainName): string { // The more specific name will be the longest let nameLength = 0; let certificateArn; diff --git a/src/aws/route53-wrapper.ts b/src/aws/route53-wrapper.ts index 944fd185..f93f8173 100644 --- a/src/aws/route53-wrapper.ts +++ b/src/aws/route53-wrapper.ts @@ -56,17 +56,20 @@ class Route53Wrapper { "NextMarker", new ListHostedZonesCommand({}) ); + Logging.logInfo(`Founded hosted zones list: ${hostedZones.map(zone => zone.Name)}.`); } catch (err) { throw new Error(`Unable to list hosted zones in Route53.\n${err.message}`); } + // removing the first part of the domain name, api.test.com => test.com + const domainNameHost = domain.givenDomainName.substring(domain.givenDomainName.indexOf(".") + 1); const targetHostedZone = hostedZones .filter((hostedZone) => { return !isPrivateDefined || isHostedZonePrivate === hostedZone.Config.PrivateZone; }) .filter((hostedZone) => { const hostedZoneName = hostedZone.Name.replace(/\.$/, ""); - return domain.givenDomainName.endsWith(hostedZoneName); + return domainNameHost.endsWith(hostedZoneName); }) .sort((zone1, zone2) => zone2.Name.length - zone1.Name.length) .shift(); diff --git a/src/index.ts b/src/index.ts index b9784ac5..9b830036 100644 --- a/src/index.ts +++ b/src/index.ts @@ -261,8 +261,6 @@ class ServerlessCustomDomain { await this.s3Wrapper.assertTlsCertObjectExists(domain); } if (!domain.certificateArn) { - const searchName = domain.certificateName || domain.givenDomainName; - Logging.logInfo(`Searching for a certificate with the '${searchName}' domain`); domain.certificateArn = await acm.getCertArn(domain); } domain.domainInfo = await apiGateway.createCustomDomain(domain); diff --git a/test/integration-tests/debug/pr-example/serverless.yml b/test/integration-tests/debug/pr-example/serverless.yml index e61a5a1d..0ee2f9ff 100644 --- a/test/integration-tests/debug/pr-example/serverless.yml +++ b/test/integration-tests/debug/pr-example/serverless.yml @@ -22,8 +22,8 @@ custom: autoDomain: true basePath: "" domainName: ${env:PLUGIN_IDENTIFIER}-http-${env:RANDOM_STRING}.${env:TEST_DOMAIN} - stage: "dev" createRoute53Record: true + endpointType: REGIONAL package: patterns: diff --git a/test/unit-tests/aws/route53-wrapper.test.ts b/test/unit-tests/aws/route53-wrapper.test.ts index 2b238445..a4e542d2 100644 --- a/test/unit-tests/aws/route53-wrapper.test.ts +++ b/test/unit-tests/aws/route53-wrapper.test.ts @@ -217,6 +217,41 @@ describe("Route53 wrapper checks", () => { expect(commandCalls.length).to.equal(1); }); + it("get route53 hosted zones with overlaps", async () => { + const testId = "test_host_id" + const Route53Mock = mockClient(Route53Client); + Route53Mock.on(ListHostedZonesCommand).resolves({ + HostedZones: [ + { + CallerReference: "1", + Config: {PrivateZone: false}, + Id: "dummy_host_id", + Name: "dummy_domain", + }, { + CallerReference: "2", + Config: {PrivateZone: false}, + Id: "not_valid", + Name: "api.test_domain", + }, { + CallerReference: "3", + Config: {PrivateZone: false}, + Id: testId, + Name: "test_domain", + } + ] + }); + + const dc = new DomainConfig(getDomainConfig({ + domainName: "devapi.test_domain" + })); + + const actualId = await new Route53Wrapper().getRoute53HostedZoneId(dc); + expect(actualId).to.equal(testId); + + const commandCalls = Route53Mock.commandCalls(ListHostedZonesCommand, {}); + expect(commandCalls.length).to.equal(1); + }); + it("get route53 hosted zone id failure", async () => { const Route53Mock = mockClient(Route53Client); Route53Mock.on(ListHostedZonesCommand).rejects(null); From a856cea42521915061b7fd0d8537a3ab88ee68cf Mon Sep 17 00:00:00 2001 From: rddimon Date: Fri, 1 Dec 2023 16:48:41 +0200 Subject: [PATCH 2/2] AT-10914: Fix duplicates https://amplify-education.atlassian.net/browse/AT-10914 --- test/unit-tests/aws/route53-wrapper.test.ts | 48 ++++----------------- 1 file changed, 9 insertions(+), 39 deletions(-) diff --git a/test/unit-tests/aws/route53-wrapper.test.ts b/test/unit-tests/aws/route53-wrapper.test.ts index a4e542d2..0f9f3c4c 100644 --- a/test/unit-tests/aws/route53-wrapper.test.ts +++ b/test/unit-tests/aws/route53-wrapper.test.ts @@ -165,19 +165,24 @@ describe("Route53 wrapper checks", () => { { CallerReference: "", Config: {PrivateZone: false}, - Id: testId, - Name: "test_domain", + Id: "no_valid", + Name: "api.test_domain", }, { CallerReference: "", Config: {PrivateZone: true}, Id: "dummy_host_id", Name: "test_domain", - } + }, { + CallerReference: "", + Config: {PrivateZone: false}, + Id: testId, + Name: "test_domain", + }, ] }); const dc = new DomainConfig(getDomainConfig({ - domainName: "test_domain" + domainName: "devapi.test_domain" })); const actualId = await new Route53Wrapper().getRoute53HostedZoneId(dc, false); @@ -217,41 +222,6 @@ describe("Route53 wrapper checks", () => { expect(commandCalls.length).to.equal(1); }); - it("get route53 hosted zones with overlaps", async () => { - const testId = "test_host_id" - const Route53Mock = mockClient(Route53Client); - Route53Mock.on(ListHostedZonesCommand).resolves({ - HostedZones: [ - { - CallerReference: "1", - Config: {PrivateZone: false}, - Id: "dummy_host_id", - Name: "dummy_domain", - }, { - CallerReference: "2", - Config: {PrivateZone: false}, - Id: "not_valid", - Name: "api.test_domain", - }, { - CallerReference: "3", - Config: {PrivateZone: false}, - Id: testId, - Name: "test_domain", - } - ] - }); - - const dc = new DomainConfig(getDomainConfig({ - domainName: "devapi.test_domain" - })); - - const actualId = await new Route53Wrapper().getRoute53HostedZoneId(dc); - expect(actualId).to.equal(testId); - - const commandCalls = Route53Mock.commandCalls(ListHostedZonesCommand, {}); - expect(commandCalls.length).to.equal(1); - }); - it("get route53 hosted zone id failure", async () => { const Route53Mock = mockClient(Route53Client); Route53Mock.on(ListHostedZonesCommand).rejects(null);