Skip to content

Commit 0f5475a

Browse files
authored
Merge pull request #440 from getlift/empty-domain
Support empty strings in domains
2 parents 41ab5c3 + 10d0fc4 commit 0f5475a

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

src/constructs/aws/ServerSideWebsite.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@ export class ServerSideWebsite extends AwsConstruct {
9292
) {
9393
super(scope, id);
9494

95-
if (configuration.domain !== undefined && configuration.certificate === undefined) {
96-
throw new ServerlessError(
97-
`Invalid configuration in 'constructs.${id}.certificate': if a domain is configured, then a certificate ARN must be configured as well.`,
98-
"LIFT_INVALID_CONSTRUCT_CONFIGURATION"
99-
);
100-
}
10195
if (configuration.errorPage !== undefined && !configuration.errorPage.endsWith(".html")) {
10296
throw new ServerlessError(
10397
`Invalid configuration in 'constructs.${id}.errorPage': the custom error page must be a static HTML file. '${configuration.errorPage}' does not end with '.html'.`,
@@ -121,12 +115,24 @@ export class ServerSideWebsite extends AwsConstruct {
121115
const apiGatewayDomain = Fn.join(".", [Fn.ref(apiId), `execute-api.${this.provider.region}.amazonaws.com`]);
122116

123117
// Cast the domains to an array
124-
this.domains = configuration.domain !== undefined ? flatten([configuration.domain]) : undefined;
118+
// if configuration.domain is an empty array or an empty string, ignore it
119+
this.domains =
120+
configuration.domain !== undefined && configuration.domain.length > 0
121+
? flatten([configuration.domain])
122+
: undefined;
123+
// if configuration.certificate is an empty string, ignore it
125124
const certificate =
126-
configuration.certificate !== undefined
125+
configuration.certificate !== undefined && configuration.certificate !== ""
127126
? acm.Certificate.fromCertificateArn(this, "Certificate", configuration.certificate)
128127
: undefined;
129128

129+
if (this.domains !== undefined && certificate === undefined) {
130+
throw new ServerlessError(
131+
`Invalid configuration in 'constructs.${id}.certificate': if a domain is configured, then a certificate ARN must be configured as well.`,
132+
"LIFT_INVALID_CONSTRUCT_CONFIGURATION"
133+
);
134+
}
135+
130136
// Hide the stage in the URL in REST scenario
131137
const originPath = configuration.apiGateway === "rest" ? "/" + (provider.getStage() ?? "") : undefined;
132138

test/unit/serverSideWebsite.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,71 @@ describe("server-side website", () => {
610610
});
611611
});
612612

613+
it("should treat empty string domain and certificate as unconfigured", async () => {
614+
const { cfTemplate, computeLogicalId } = await runServerless({
615+
command: "package",
616+
config: Object.assign(baseConfig, {
617+
constructs: {
618+
backend: {
619+
type: "server-side-website",
620+
domain: "",
621+
certificate: "",
622+
},
623+
},
624+
}),
625+
});
626+
const cfDistributionLogicalId = computeLogicalId("backend", "CDN");
627+
// No Aliases or ViewerCertificate should be set
628+
expect(cfTemplate.Resources[cfDistributionLogicalId]).not.toHaveProperty(
629+
"Properties.DistributionConfig.Aliases"
630+
);
631+
expect(cfTemplate.Resources[cfDistributionLogicalId]).not.toHaveProperty(
632+
"Properties.DistributionConfig.ViewerCertificate"
633+
);
634+
});
635+
636+
it("should treat empty string domain with valid certificate as unconfigured", async () => {
637+
const { cfTemplate, computeLogicalId } = await runServerless({
638+
command: "package",
639+
config: Object.assign(baseConfig, {
640+
constructs: {
641+
backend: {
642+
type: "server-side-website",
643+
domain: "",
644+
certificate:
645+
"arn:aws:acm:us-east-1:123456615250:certificate/0a28e63d-d3a9-4578-9f8b-14347bfe8123",
646+
},
647+
},
648+
}),
649+
});
650+
const cfDistributionLogicalId = computeLogicalId("backend", "CDN");
651+
// No Aliases should be set (domain is empty)
652+
expect(cfTemplate.Resources[cfDistributionLogicalId]).not.toHaveProperty(
653+
"Properties.DistributionConfig.Aliases"
654+
);
655+
});
656+
657+
it("should treat empty array domain as unconfigured", async () => {
658+
const { cfTemplate, computeLogicalId } = await runServerless({
659+
command: "package",
660+
config: Object.assign(baseConfig, {
661+
constructs: {
662+
backend: {
663+
type: "server-side-website",
664+
domain: [],
665+
certificate:
666+
"arn:aws:acm:us-east-1:123456615250:certificate/0a28e63d-d3a9-4578-9f8b-14347bfe8123",
667+
},
668+
},
669+
}),
670+
});
671+
const cfDistributionLogicalId = computeLogicalId("backend", "CDN");
672+
// No Aliases should be set (domain is empty array)
673+
expect(cfTemplate.Resources[cfDistributionLogicalId]).not.toHaveProperty(
674+
"Properties.DistributionConfig.Aliases"
675+
);
676+
});
677+
613678
it("trims CloudFront function names to stay under the limit", async () => {
614679
const { cfTemplate, computeLogicalId } = await runServerless({
615680
command: "package",

0 commit comments

Comments
 (0)