-
Notifications
You must be signed in to change notification settings - Fork 4k
/
proxy.ts
466 lines (412 loc) · 13.6 KB
/
proxy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import * as cdk from '@aws-cdk/core';
import { IDatabaseCluster } from './cluster-ref';
import { IDatabaseInstance } from './instance';
import { CfnDBCluster, CfnDBInstance, CfnDBProxy, CfnDBProxyTargetGroup } from './rds.generated';
/**
* SessionPinningFilter
*
* @see https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/rds-proxy.html#rds-proxy-pinning
*/
export class SessionPinningFilter {
/**
* You can opt out of session pinning for the following kinds of application statements:
*
* - Setting session variables and configuration settings.
*/
public static readonly EXCLUDE_VARIABLE_SETS = new SessionPinningFilter('EXCLUDE_VARIABLE_SETS');
/**
* custom filter
*/
public static of(filterName: string): SessionPinningFilter {
return new SessionPinningFilter(filterName);
}
private constructor(
/**
* Filter name
*/
public readonly filterName: string,
) {}
}
/**
* Proxy target: Instance or Cluster
*
* A target group is a collection of databases that the proxy can connect to.
* Currently, you can specify only one RDS DB instance or Aurora DB cluster.
*/
export class ProxyTarget {
/**
* From instance
*
* @param instance RDS database instance
*/
public static fromInstance(instance: IDatabaseInstance): ProxyTarget {
return new ProxyTarget(instance);
}
/**
* From cluster
*
* @param cluster RDS database cluster
*/
public static fromCluster(cluster: IDatabaseCluster): ProxyTarget {
return new ProxyTarget(undefined, cluster);
}
private constructor(private readonly dbInstance?: IDatabaseInstance, private readonly dbCluster?: IDatabaseCluster) {}
/**
* Bind this target to the specified database proxy.
*/
public bind(_: DatabaseProxy): ProxyTargetConfig {
let engine: string | undefined;
if (this.dbCluster && this.dbInstance) {
throw new Error('Proxy cannot target both database cluster and database instance.');
} else if (this.dbCluster) {
engine = (this.dbCluster.node.defaultChild as CfnDBCluster).engine;
} else if (this.dbInstance) {
engine = (this.dbInstance.node.defaultChild as CfnDBInstance).engine;
}
let engineFamily;
switch (engine) {
case 'aurora':
case 'aurora-mysql':
case 'mysql':
engineFamily = 'MYSQL';
break;
case 'aurora-postgresql':
case 'postgres':
engineFamily = 'POSTGRESQL';
break;
default:
throw new Error(`Unsupported engine type - ${engine}`);
}
return {
engineFamily,
dbClusters: this.dbCluster ? [ this.dbCluster ] : undefined,
dbInstances: this.dbInstance ? [ this.dbInstance ] : undefined,
};
}
}
/**
* The result of binding a `ProxyTarget` to a `DatabaseProxy`.
*/
export interface ProxyTargetConfig {
/**
* The engine family of the database instance or cluster this proxy connects with.
*/
readonly engineFamily: string;
/**
* The database instances to which this proxy connects.
* Either this or `dbClusters` will be set and the other `undefined`.
* @default - `undefined` if `dbClusters` is set.
*/
readonly dbInstances?: IDatabaseInstance[];
/**
* The database clusters to which this proxy connects.
* Either this or `dbInstances` will be set and the other `undefined`.
* @default - `undefined` if `dbInstances` is set.
*/
readonly dbClusters?: IDatabaseCluster[];
}
/**
* Options for a new DatabaseProxy
*/
export interface DatabaseProxyOptions {
/**
* The identifier for the proxy.
* This name must be unique for all proxies owned by your AWS account in the specified AWS Region.
* An identifier must begin with a letter and must contain only ASCII letters, digits, and hyphens;
* it can't end with a hyphen or contain two consecutive hyphens.
*
* @default - Generated by CloudFormation (recommended)
*/
readonly dbProxyName?: string;
/**
* The duration for a proxy to wait for a connection to become available in the connection pool.
* Only applies when the proxy has opened its maximum number of connections and all connections are busy with client
* sessions.
*
* Value must be between 1 second and 1 hour, or `Duration.seconds(0)` to represent unlimited.
*
* @default cdk.Duration.seconds(120)
*/
readonly borrowTimeout?: cdk.Duration;
/**
* One or more SQL statements for the proxy to run when opening each new database connection.
* Typically used with SET statements to make sure that each connection has identical settings such as time zone
* and character set.
* For multiple statements, use semicolons as the separator.
* You can also include multiple variables in a single SET statement, such as SET x=1, y=2.
*
* not currently supported for PostgreSQL.
*
* @default - no initialization query
*/
readonly initQuery?: string;
/**
* The maximum size of the connection pool for each target in a target group.
* For Aurora MySQL, it is expressed as a percentage of the max_connections setting for the RDS DB instance or Aurora DB
* cluster used by the target group.
*
* 1-100
*
* @default 100
*/
readonly maxConnectionsPercent?: number;
/**
* Controls how actively the proxy closes idle database connections in the connection pool.
* A high value enables the proxy to leave a high percentage of idle connections open.
* A low value causes the proxy to close idle client connections and return the underlying database connections
* to the connection pool.
* For Aurora MySQL, it is expressed as a percentage of the max_connections setting for the RDS DB instance
* or Aurora DB cluster used by the target group.
*
* between 0 and MaxConnectionsPercent
*
* @default 50
*/
readonly maxIdleConnectionsPercent?: number;
/**
* Each item in the list represents a class of SQL operations that normally cause all later statements in a session
* using a proxy to be pinned to the same underlying database connection.
* Including an item in the list exempts that class of SQL operations from the pinning behavior.
*
* @default - no session pinning filters
*/
readonly sessionPinningFilters?: SessionPinningFilter[];
/**
* Whether the proxy includes detailed information about SQL statements in its logs.
* This information helps you to debug issues involving SQL behavior or the performance and scalability of the proxy connections.
* The debug information includes the text of SQL statements that you submit through the proxy.
* Thus, only enable this setting when needed for debugging, and only when you have security measures in place to safeguard any sensitive
* information that appears in the logs.
*
* @default false
*/
readonly debugLogging?: boolean;
/**
* Whether to require or disallow AWS Identity and Access Management (IAM) authentication for connections to the proxy.
*
* @default false
*/
readonly iamAuth?: boolean;
/**
* The number of seconds that a connection to the proxy can be inactive before the proxy disconnects it.
* You can set this value higher or lower than the connection timeout limit for the associated database.
*
* @default cdk.Duration.minutes(30)
*/
readonly idleClientTimeout?: cdk.Duration;
/**
* A Boolean parameter that specifies whether Transport Layer Security (TLS) encryption is required for connections to the proxy.
* By enabling this setting, you can enforce encrypted TLS connections to the proxy.
*
* @default true
*/
readonly requireTLS?: boolean;
/**
* IAM role that the proxy uses to access secrets in AWS Secrets Manager.
*
* @default - A role will automatically be created
*/
readonly role?: iam.IRole;
/**
* The secret that the proxy uses to authenticate to the RDS DB instance or Aurora DB cluster.
* These secrets are stored within Amazon Secrets Manager.
* One or more secrets are required.
*/
readonly secrets: secretsmanager.ISecret[];
/**
* One or more VPC security groups to associate with the new proxy.
*
* @default - No security groups
*/
readonly securityGroups?: ec2.ISecurityGroup[];
/**
* The subnets used by the proxy.
*
* @default - the VPC default strategy if not specified.
*/
readonly vpcSubnets?: ec2.SubnetSelection;
/**
* The VPC to associate with the new proxy.
*/
readonly vpc: ec2.IVpc;
}
/**
* Construction properties for a DatabaseProxy
*/
export interface DatabaseProxyProps extends DatabaseProxyOptions {
/**
* DB proxy target: Instance or Cluster
*/
readonly proxyTarget: ProxyTarget
}
/**
* Properties that describe an existing DB Proxy
*/
export interface DatabaseProxyAttributes {
/**
* DB Proxy Name
*/
readonly dbProxyName: string;
/**
* DB Proxy ARN
*/
readonly dbProxyArn: string;
/**
* Endpoint
*/
readonly endpoint: string;
/**
* The security groups of the instance.
*/
readonly securityGroups: ec2.ISecurityGroup[];
}
/**
* DB Proxy
*/
export interface IDatabaseProxy extends cdk.IResource {
/**
* DB Proxy Name
*
* @attribute
*/
readonly dbProxyName: string;
/**
* DB Proxy ARN
*
* @attribute
*/
readonly dbProxyArn: string;
/**
* Endpoint
*
* @attribute
*/
readonly endpoint: string;
}
/**
* RDS Database Proxy
*
* @resource AWS::RDS::DBProxy
*/
export class DatabaseProxy extends cdk.Resource
implements IDatabaseProxy, ec2.IConnectable, secretsmanager.ISecretAttachmentTarget {
/**
* Import an existing database proxy.
*/
public static fromDatabaseProxyAttributes(
scope: cdk.Construct,
id: string,
attrs: DatabaseProxyAttributes,
): IDatabaseProxy {
class Import extends cdk.Resource implements IDatabaseProxy {
public readonly dbProxyName = attrs.dbProxyName;
public readonly dbProxyArn = attrs.dbProxyArn;
public readonly endpoint = attrs.endpoint;
}
return new Import(scope, id);
}
/**
* DB Proxy Name
*
* @attribute
*/
public readonly dbProxyName: string;
/**
* DB Proxy ARN
*
* @attribute
*/
public readonly dbProxyArn: string;
/**
* Endpoint
*
* @attribute
*/
public readonly endpoint: string;
/**
* Access to network connections.
*/
public readonly connections: ec2.Connections;
private readonly resource: CfnDBProxy;
constructor(scope: cdk.Construct, id: string, props: DatabaseProxyProps) {
super(scope, id, { physicalName: props.dbProxyName || id });
const role = props.role || new iam.Role(this, 'IAMRole', {
assumedBy: new iam.ServicePrincipal('rds.amazonaws.com'),
});
for (const secret of props.secrets) {
secret.grantRead(role);
}
this.connections = new ec2.Connections({ securityGroups: props.securityGroups });
const bindResult = props.proxyTarget.bind(this);
if (props.secrets.length < 1) {
throw new Error('One or more secrets are required.');
}
this.resource = new CfnDBProxy(this, 'Resource', {
auth: props.secrets.map(_ => {
return {
authScheme: 'SECRETS',
iamAuth: props.iamAuth ? 'REQUIRED' : 'DISABLED',
secretArn: _.secretArn,
};
}),
dbProxyName: this.physicalName,
debugLogging: props.debugLogging,
engineFamily: bindResult.engineFamily,
idleClientTimeout: props.idleClientTimeout?.toSeconds(),
requireTls: props.requireTLS ?? true,
roleArn: role.roleArn,
vpcSecurityGroupIds: props.securityGroups?.map(_ => _.securityGroupId),
vpcSubnetIds: props.vpc.selectSubnets(props.vpcSubnets).subnetIds,
});
this.dbProxyName = this.resource.ref;
this.dbProxyArn = this.resource.attrDbProxyArn;
this.endpoint = this.resource.attrEndpoint;
let dbInstanceIdentifiers: string[] | undefined;
if (bindResult.dbInstances) {
// support for only single instance
dbInstanceIdentifiers = [ bindResult.dbInstances[0].instanceIdentifier ];
}
let dbClusterIdentifiers: string[] | undefined;
if (bindResult.dbClusters) {
dbClusterIdentifiers = bindResult.dbClusters.map((c) => c.clusterIdentifier);
}
if (!!dbInstanceIdentifiers && !!dbClusterIdentifiers) {
throw new Error('Cannot specify both dbInstanceIdentifiers and dbClusterIdentifiers');
}
const proxyTargetGroup = new CfnDBProxyTargetGroup(this, 'ProxyTargetGroup', {
dbProxyName: this.dbProxyName,
dbInstanceIdentifiers,
dbClusterIdentifiers,
connectionPoolConfigurationInfo: toConnectionPoolConfigurationInfo(props),
});
// Currently(2020-07-04), this property must be set to default.
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-rds-dbproxytargetgroup.html#TargetGroupName-fn::getatt
proxyTargetGroup.addOverride('Properties.TargetGroupName', 'default');
}
/**
* Renders the secret attachment target specifications.
*/
public asSecretAttachmentTarget(): secretsmanager.SecretAttachmentTargetProps {
return {
targetId: this.dbProxyName,
targetType: secretsmanager.AttachmentTargetType.RDS_DB_PROXY,
};
}
}
/**
* ConnectionPoolConfiguration (L2 => L1)
*/
function toConnectionPoolConfigurationInfo(
props: DatabaseProxyProps,
): CfnDBProxyTargetGroup.ConnectionPoolConfigurationInfoFormatProperty {
return {
connectionBorrowTimeout: props.borrowTimeout?.toSeconds(),
initQuery: props.initQuery,
maxConnectionsPercent: props.maxConnectionsPercent,
maxIdleConnectionsPercent: props.maxIdleConnectionsPercent,
sessionPinningFilters: props.sessionPinningFilters?.map(_ => _.filterName),
};
}