Skip to content

Commit

Permalink
Merge pull request #204 from guardian/jl/https-clb
Browse files Browse the repository at this point in the history
Add GuHttpsClassicLoadBalancer
  • Loading branch information
Jamie Lynch authored Jan 26, 2021
2 parents c40f390 + 6607b23 commit 9dc3b05
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 1 deletion.
97 changes: 97 additions & 0 deletions src/constructs/loadbalancing/clb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Stack } from "@aws-cdk/core";
import { simpleGuStackForTesting } from "../../../test/utils/simple-gu-stack";
import type { SynthedStack } from "../../../test/utils/synthed-stack";
import { GuClassicLoadBalancer } from "../loadbalancing";
import { GuHttpsClassicLoadBalancer } from "./clb";

describe("The GuClassicLoadBalancer class", () => {
const vpc = Vpc.fromVpcAttributes(new Stack(), "VPC", {
Expand Down Expand Up @@ -116,3 +117,99 @@ describe("The GuClassicLoadBalancer class", () => {
});
});
});

describe("The GuHttpsClassicLoadBalancer class", () => {
const vpc = Vpc.fromVpcAttributes(new Stack(), "VPC", {
vpcId: "test",
availabilityZones: [""],
publicSubnetIds: [""],
privateSubnetIds: [""],
});

test("uses default listener values", () => {
const stack = simpleGuStackForTesting();
new GuHttpsClassicLoadBalancer(stack, "HttpsClassicLoadBalancer", {
vpc,
});

expect(stack).toHaveResource("AWS::ElasticLoadBalancing::LoadBalancer", {
Listeners: [
{
InstancePort: "9000",
InstanceProtocol: "http",
LoadBalancerPort: "443",
Protocol: "https",
SSLCertificateId: {
Ref: "CertificateARN",
},
},
],
});
});

test("adds the CertificateARN parameter if no value provided", () => {
const stack = simpleGuStackForTesting();
new GuHttpsClassicLoadBalancer(stack, "HttpsClassicLoadBalancer", {
vpc,
});

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(json.Parameters.CertificateARN).toEqual({
AllowedPattern: "arn:aws:[a-z0-9]*:[a-z0-9\\-]*:[0-9]{12}:.*",
Description: "Certificate ARN for ELB",
ConstraintDescription: "Must be a valid ARN, eg: arn:partition:service:region:account-id:resource-id",
Type: "String",
});
});

test("uses the certificate id provided", () => {
const stack = simpleGuStackForTesting();
new GuHttpsClassicLoadBalancer(stack, "HttpsClassicLoadBalancer", {
vpc,
listener: {
sslCertificateId: "certificateId",
},
});

expect(stack).toHaveResource("AWS::ElasticLoadBalancing::LoadBalancer", {
Listeners: [
{
InstancePort: "9000",
InstanceProtocol: "http",
LoadBalancerPort: "443",
Protocol: "https",
SSLCertificateId: "certificateId",
},
],
});

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(Object.keys(json.Parameters)).not.toContain("CertificateARN");
});

test("merges any listener values provided", () => {
const stack = simpleGuStackForTesting();
new GuHttpsClassicLoadBalancer(stack, "HttpsClassicLoadBalancer", {
vpc,
listener: {
internalPort: 3000,
},
});

expect(stack).toHaveResource("AWS::ElasticLoadBalancing::LoadBalancer", {
Listeners: [
{
InstancePort: "3000",
InstanceProtocol: "http",
LoadBalancerPort: "443",
Protocol: "https",
SSLCertificateId: {
Ref: "CertificateARN",
},
},
],
});
});
});
39 changes: 38 additions & 1 deletion src/constructs/loadbalancing/clb.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import type { CfnLoadBalancer, HealthCheck, LoadBalancerProps } from "@aws-cdk/aws-elasticloadbalancing";
import type {
CfnLoadBalancer,
HealthCheck,
LoadBalancerListener,
LoadBalancerProps,
} from "@aws-cdk/aws-elasticloadbalancing";
import { LoadBalancer, LoadBalancingProtocol } from "@aws-cdk/aws-elasticloadbalancing";
import { Duration } from "@aws-cdk/core";
import type { GuStack } from "../core";
import { GuArnParameter } from "../core";

enum RemoveableProperties {
SCHEME = "Scheme",
Expand Down Expand Up @@ -48,3 +54,34 @@ export class GuClassicLoadBalancer extends LoadBalancer {
Object.entries(mergedProps.propertiesToOverride).forEach(([key, value]) => cfnLb.addPropertyOverride(key, value));
}
}

interface GuHttpsClassicLoadBalancerProps extends Omit<GuClassicLoadBalancerProps, "listeners"> {
listener?: Partial<LoadBalancerListener>;
}

export class GuHttpsClassicLoadBalancer extends GuClassicLoadBalancer {
static DefaultListener: LoadBalancerListener = {
internalPort: 9000,
externalPort: 443,
internalProtocol: LoadBalancingProtocol.HTTP,
externalProtocol: LoadBalancingProtocol.HTTPS,
};

constructor(scope: GuStack, id: string, props: GuHttpsClassicLoadBalancerProps) {
const listenerProps = { ...GuHttpsClassicLoadBalancer.DefaultListener, ...props.listener };

if (!listenerProps.sslCertificateId) {
const certificateId = new GuArnParameter(scope, "CertificateARN", {
description: "Certificate ARN for ELB",
});
listenerProps.sslCertificateId = certificateId.valueAsString;
}

const mergedProps = {
...props,
listeners: [listenerProps],
};

super(scope, id, mergedProps);
}
}

0 comments on commit 9dc3b05

Please sign in to comment.