Skip to content

Commit

Permalink
fix(rds): fix unresolved endpoint socket address
Browse files Browse the repository at this point in the history
The port needs to be passed as a string to the `Endpoint` class otherwise the concatenation of
address and port does not resolve correctly (forced string conversion of number token).

Fixes aws#2711
  • Loading branch information
jogold committed Jun 12, 2019
1 parent b80ef04 commit 0ef110c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
15 changes: 7 additions & 8 deletions packages/@aws-cdk/aws-rds/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ export class DatabaseCluster extends DatabaseClusterBase {
});
public readonly clusterIdentifier = attrs.clusterIdentifier;
public readonly instanceIdentifiers: string[] = [];
public readonly clusterEndpoint = new Endpoint(attrs.clusterEndpointAddress, attrs.port);
public readonly clusterReadEndpoint = new Endpoint(attrs.readerEndpointAddress, attrs.port);
public readonly instanceEndpoints = attrs.instanceEndpointAddresses.map(a => new Endpoint(a, attrs.port));
public readonly clusterEndpoint = new Endpoint(attrs.clusterEndpointAddress, attrs.port.toString());
public readonly clusterReadEndpoint = new Endpoint(attrs.readerEndpointAddress, attrs.port.toString());
public readonly instanceEndpoints = attrs.instanceEndpointAddresses.map(a => new Endpoint(a, attrs.port.toString()));
public readonly securityGroupId = attrs.securityGroupId;
}

Expand Down Expand Up @@ -328,9 +328,8 @@ export class DatabaseCluster extends DatabaseClusterBase {
this.clusterIdentifier = cluster.refAsString;

// create a number token that represents the port of the cluster
const portAttribute = Token.asNumber(cluster.dbClusterEndpointPort);
this.clusterEndpoint = new Endpoint(cluster.dbClusterEndpointAddress, portAttribute);
this.clusterReadEndpoint = new Endpoint(cluster.dbClusterReadEndpointAddress, portAttribute);
this.clusterEndpoint = new Endpoint(cluster.dbClusterEndpointAddress, cluster.dbClusterEndpointPort);
this.clusterReadEndpoint = new Endpoint(cluster.dbClusterReadEndpointAddress, cluster.dbClusterEndpointPort);

if (secret) {
this.secret = secret.addTargetAttachment('AttachedSecret', {
Expand Down Expand Up @@ -376,10 +375,10 @@ export class DatabaseCluster extends DatabaseClusterBase {
instance.node.addDependency(internetConnected);

this.instanceIdentifiers.push(instance.refAsString);
this.instanceEndpoints.push(new Endpoint(instance.dbInstanceEndpointAddress, portAttribute));
this.instanceEndpoints.push(new Endpoint(instance.dbInstanceEndpointAddress, instance.dbInstanceEndpointPort));
}

const defaultPortRange = new ec2.TcpPort(this.clusterEndpoint.port);
const defaultPortRange = new ec2.TcpPort(Token.asNumber(cluster.dbClusterEndpointPort));
this.connections = new ec2.Connections({ securityGroups: [securityGroup], defaultPortRange });
}

Expand Down
7 changes: 3 additions & 4 deletions packages/@aws-cdk/aws-rds/lib/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ export class Endpoint {
*/
public readonly socketAddress: string;

constructor(address: string, port: number) {
constructor(address: string, port: string) {
this.hostname = address;
this.port = port;
this.port = Token.asNumber(port);

const portDesc = Token.isUnresolved(port) ? '{IndirectPort}' : port;
this.socketAddress = `${address}:${portDesc}`;
this.socketAddress = `${address}:${port}`;
}
}
20 changes: 7 additions & 13 deletions packages/@aws-cdk/aws-rds/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export abstract class DatabaseInstanceBase extends Resource implements IDatabase
public readonly instanceIdentifier = attrs.instanceIdentifier;
public readonly dbInstanceEndpointAddress = attrs.instanceEndpointAddress;
public readonly dbInstanceEndpointPort = attrs.port.toString();
public readonly instanceEndpoint = new Endpoint(attrs.instanceEndpointAddress, attrs.port);
public readonly instanceEndpoint = new Endpoint(this.dbInstanceEndpointAddress, this.dbInstanceEndpointPort);
public readonly securityGroupId = attrs.securityGroupId;
}

Expand Down Expand Up @@ -743,9 +743,7 @@ export class DatabaseInstance extends DatabaseInstanceSource implements IDatabas
this.dbInstanceEndpointAddress = instance.dbInstanceEndpointAddress;
this.dbInstanceEndpointPort = instance.dbInstanceEndpointPort;

// create a number token that represents the port of the instance
const portAttribute = Token.asNumber(instance.dbInstanceEndpointPort);
this.instanceEndpoint = new Endpoint(instance.dbInstanceEndpointAddress, portAttribute);
this.instanceEndpoint = new Endpoint(instance.dbInstanceEndpointAddress, instance.dbInstanceEndpointPort);

const deleteReplacePolicy = props.deleteReplacePolicy || DeletionPolicy.Retain;
instance.options.deletionPolicy = deleteReplacePolicy;
Expand All @@ -759,7 +757,7 @@ export class DatabaseInstance extends DatabaseInstanceSource implements IDatabas

this.connections = new ec2.Connections({
securityGroups: [this.securityGroup],
defaultPortRange: new ec2.TcpPort(this.instanceEndpoint.port)
defaultPortRange: new ec2.TcpPort(Token.asNumber(instance.dbInstanceEndpointPort))
});

this.setLogRetention();
Expand Down Expand Up @@ -837,9 +835,7 @@ export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource impleme
this.dbInstanceEndpointAddress = instance.dbInstanceEndpointAddress;
this.dbInstanceEndpointPort = instance.dbInstanceEndpointPort;

// create a number token that represents the port of the instance
const portAttribute = Token.asNumber(instance.dbInstanceEndpointPort);
this.instanceEndpoint = new Endpoint(instance.dbInstanceEndpointAddress, portAttribute);
this.instanceEndpoint = new Endpoint(instance.dbInstanceEndpointAddress, instance.dbInstanceEndpointPort);

const deleteReplacePolicy = props.deleteReplacePolicy || DeletionPolicy.Retain;
instance.options.deletionPolicy = deleteReplacePolicy;
Expand All @@ -853,7 +849,7 @@ export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource impleme

this.connections = new ec2.Connections({
securityGroups: [this.securityGroup],
defaultPortRange: new ec2.TcpPort(this.instanceEndpoint.port)
defaultPortRange: new ec2.TcpPort(Token.asNumber(instance.dbInstanceEndpointPort))
});

this.setLogRetention();
Expand Down Expand Up @@ -914,17 +910,15 @@ export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements
this.dbInstanceEndpointAddress = instance.dbInstanceEndpointAddress;
this.dbInstanceEndpointPort = instance.dbInstanceEndpointPort;

// create a number token that represents the port of the instance
const portAttribute = Token.asNumber(instance.dbInstanceEndpointPort);
this.instanceEndpoint = new Endpoint(instance.dbInstanceEndpointAddress, portAttribute);
this.instanceEndpoint = new Endpoint(instance.dbInstanceEndpointAddress, instance.dbInstanceEndpointPort);

const deleteReplacePolicy = props.deleteReplacePolicy || DeletionPolicy.Retain;
instance.options.deletionPolicy = deleteReplacePolicy;
instance.options.updateReplacePolicy = deleteReplacePolicy;

this.connections = new ec2.Connections({
securityGroups: [this.securityGroup],
defaultPortRange: new ec2.TcpPort(this.instanceEndpoint.port)
defaultPortRange: new ec2.TcpPort(Token.asNumber(instance.dbInstanceEndpointPort))
});

this.setLogRetention();
Expand Down
31 changes: 31 additions & 0 deletions packages/@aws-cdk/aws-rds/test/test.instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,37 @@ export = {
statistic: 'Average'
});

test.done();
},

'can resolve endpoint port and socket address'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');

// WHEN
const instance = new rds.DatabaseInstance(stack, 'Instance', {
engine: rds.DatabaseInstanceEngine.Mysql,
instanceClass: new ec2.InstanceTypePair(ec2.InstanceClass.Burstable2, ec2.InstanceSize.Small),
masterUsername: 'admin',
vpc
});

test.deepEqual(stack.resolve(instance.instanceEndpoint.port), {
'Fn::GetAtt': ['InstanceC1063A87', 'Endpoint.Port']
});

test.deepEqual(stack.resolve(instance.instanceEndpoint.socketAddress), {
'Fn::Join': [
'',
[
{ 'Fn::GetAtt': ['InstanceC1063A87', 'Endpoint.Address'] },
':',
{ 'Fn::GetAtt': ['InstanceC1063A87', 'Endpoint.Port'] },
]
]
});

test.done();
}
};

0 comments on commit 0ef110c

Please sign in to comment.