Skip to content
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

fix(ec2): private DNS for custom endpoints has incorrect default #5987

Merged
111 changes: 111 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions packages/@aws-cdk/aws-ec2/lib/vpc-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ export interface IInterfaceVpcEndpointService {
* The port of the service.
*/
readonly port: number;

/**
* Whether Private DNS is supported by default.
*/
readonly privateDnsDefault?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made this optional for backwards compatibility -- any existing CDK code specifying a service like:

  const interfaceVpcEndpoint = new ec2.InterfaceVpcEndpoint(stack, 'InterfaceEndpoint', {
    vpc,
    service: {
      name: 'com.amazonaws.us-west-2.workspaces',
      port: 80
    }
  });

Will get compilation errors if I don't make the field optional:

Property 'privateDnsDefault' is missing in type '{ name: string; port: number; }' 
but required in type 'IInterfaceVpcEndpointService'.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. That's not how you're supposed to supply that value though, but I get it.

}

/**
Expand All @@ -218,6 +223,11 @@ export class InterfaceVpcEndpointService implements IInterfaceVpcEndpointService
*/
public readonly port: number;

/**
* Whether Private DNS is supported by default.
*/
public readonly privateDnsDefault?: boolean = false;

constructor(name: string, port?: number) {
this.name = name;
this.port = port || 443;
Expand Down Expand Up @@ -279,6 +289,11 @@ export class InterfaceVpcEndpointAwsService implements IInterfaceVpcEndpointServ
*/
public readonly port: number;

/**
* Whether Private DNS is supported by default.
*/
public readonly privateDnsDefault?: boolean = true;

constructor(name: string, prefix?: string, port?: number) {
this.name = `${prefix || 'com.amazonaws'}.${Aws.REGION}.${name}`;
this.port = port || 443;
Expand All @@ -298,7 +313,8 @@ export interface InterfaceVpcEndpointOptions {
* Whether to associate a private hosted zone with the specified VPC. This
* allows you to make requests to the service using its default DNS hostname.
*
* @default true
* @default set by the instance of IInterfaceVpcEndpointService, or true if
* not defined by the instance of IInterfaceVpcEndpointService
*/
readonly privateDnsEnabled?: boolean;

Expand Down Expand Up @@ -429,7 +445,7 @@ export class InterfaceVpcEndpoint extends VpcEndpoint implements IInterfaceVpcEn
const subnetIds = subnets.subnetIds;

const endpoint = new CfnVPCEndpoint(this, 'Resource', {
privateDnsEnabled: props.privateDnsEnabled !== undefined ? props.privateDnsEnabled : true,
privateDnsEnabled: props.privateDnsEnabled ?? props.service.privateDnsDefault ?? true,
policyDocument: Lazy.anyValue({ produce: () => this.policyDocument }),
securityGroupIds: securityGroups.map(s => s.securityGroupId),
serviceName: props.service.name,
Expand Down
23 changes: 22 additions & 1 deletion packages/@aws-cdk/aws-ec2/test/test.vpc-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,31 @@ export = {

// THEN
expect(stack).to(haveResource('AWS::EC2::VPCEndpoint', {
ServiceName: "com.amazonaws.vpce.us-east-1.vpce-svc-uuddlrlrbastrtsvc"
ServiceName: "com.amazonaws.vpce.us-east-1.vpce-svc-uuddlrlrbastrtsvc",
PrivateDnsEnabled: false
}));

test.done();
},
'marketplace partner service interface endpoint'(test: Test) {
// GIVEN
const stack = new Stack();
const vpc = new Vpc(stack, 'VpcNetwork');

// WHEN
vpc.addInterfaceEndpoint('YourService', {
service: {name: "com.amazonaws.vpce.us-east-1.vpce-svc-mktplacesvcwprdns",
port: 443,
privateDnsDefault: true}
});

// THEN
expect(stack).to(haveResource('AWS::EC2::VPCEndpoint', {
ServiceName: "com.amazonaws.vpce.us-east-1.vpce-svc-mktplacesvcwprdns",
PrivateDnsEnabled: true
}));

test.done();
}
}
};