Skip to content

Commit 60f752f

Browse files
committed
v2.0.1
1 parent 1d693bf commit 60f752f

File tree

6 files changed

+51
-48
lines changed

6 files changed

+51
-48
lines changed

cdk-postgresql/lib/database.handler.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import format from "pg-format";
2-
import { createClient, validateConnection, hashCode } from "./util";
2+
import { getConnectedClient, validateConnection, hashCode } from "./util";
33
import * as postgres from "./postgres";
44

55
import {
@@ -99,8 +99,7 @@ export const createDatabase = async (props: {
9999
}) => {
100100
const { connection, name, owner } = props;
101101
console.log("Creating database", name);
102-
const client = await createClient(connection);
103-
await client.connect();
102+
const client = await getConnectedClient(connection);
104103

105104
await postgres.createDatabase({ client, name, owner });
106105
await client.end();
@@ -113,8 +112,7 @@ export const deleteDatabase = async (
113112
owner: string
114113
) => {
115114
console.log("Deleting database", name);
116-
const client = await createClient(connection);
117-
await client.connect();
115+
const client = await getConnectedClient(connection);
118116

119117
// First, drop all remaining DB connections
120118
// Sometimes, DB connections are still alive even though the ECS service has been deleted
@@ -136,8 +134,7 @@ export const updateDbOwner = async (
136134
owner: string
137135
) => {
138136
console.log(`Updating DB ${name} owner to ${owner}`);
139-
const client = await createClient(connection);
140-
await client.connect();
137+
const client = await getConnectedClient(connection);
141138

142139
await client.query(format("ALTER DATABASE %I OWNER TO %I", name, owner));
143140
await client.end();

cdk-postgresql/lib/provider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class Provider extends Construct implements iam.IGrantable {
104104
nodeModules: ["pg", "pg-format"],
105105
},
106106
logRetention: logs.RetentionDays.ONE_MONTH,
107-
timeout: cdk.Duration.seconds(30),
107+
timeout: cdk.Duration.minutes(15),
108108
vpc,
109109
securityGroups: handlerSecurityGroups,
110110
});

cdk-postgresql/lib/role.handler.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import {
1111
validateConnection,
1212
hashCode,
13-
createClient,
13+
getConnectedClient,
1414
secretsmanager,
1515
} from "./util";
1616
import { Connection } from "./lambda.types";
@@ -109,8 +109,7 @@ const generatePhysicalId = (props: Props): string => {
109109

110110
export const deleteRole = async (connection: Connection, name: string) => {
111111
console.log("Deleting user", name);
112-
const client = await createClient(connection);
113-
await client.connect();
112+
const client = await getConnectedClient(connection);
114113

115114
await client.query(format("DROP USER %I", name));
116115
await client.end();
@@ -122,8 +121,7 @@ export const updateRoleName = async (
122121
newName: string
123122
) => {
124123
console.log(`Updating role name from ${oldName} to ${newName}`);
125-
const client = await createClient(connection);
126-
await client.connect();
124+
const client = await getConnectedClient(connection);
127125

128126
await client.query(format("ALTER ROLE %I RENAME TO %I", oldName, newName));
129127
await client.end();
@@ -137,8 +135,7 @@ export const updateRolePassword = async (props: {
137135
const { connection, name, passwordArn } = props;
138136
console.log("Updating user password", name);
139137

140-
const client = await createClient(connection);
141-
await client.connect();
138+
const client = await getConnectedClient(connection);
142139

143140
const { SecretString: password } = await secretsmanager.getSecretValue({
144141
SecretId: passwordArn,
@@ -155,8 +152,7 @@ export const createRole = async (props: {
155152
}) => {
156153
const { connection, name, passwordArn } = props;
157154
console.log("Creating user", name);
158-
const client = await createClient(connection);
159-
await client.connect();
155+
const client = await getConnectedClient(connection);
160156

161157
const { SecretString: password } = await secretsmanager.getSecretValue({
162158
SecretId: passwordArn,

cdk-postgresql/lib/util.ts

+40-18
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,49 @@ export const isObject = (obj: any): obj is { [key: string]: any } => {
88
return typeof obj === "object" && !Array.isArray(obj) && obj !== null;
99
};
1010

11-
export const createClient = async (connection: Connection) => {
11+
export const getConnectedClient = async (connection: Connection) => {
1212
console.debug(
1313
`creating PG client with connection: ${JSON.stringify(connection)}`
1414
);
1515

16+
const password = await getPassword(connection);
17+
18+
const clientProps: ClientConfig = {
19+
host: connection.Host,
20+
port: connection.Port,
21+
user: connection.Username,
22+
password,
23+
database: connection.Database,
24+
};
25+
26+
console.debug(`clientProps: ${JSON.stringify(clientProps)}`);
27+
28+
if (connection.SSLMode === "require") {
29+
clientProps.ssl = {
30+
rejectUnauthorized: false,
31+
};
32+
}
33+
34+
let client;
35+
let tries = 0;
36+
let connected = false;
37+
do {
38+
tries++;
39+
client = new Client(clientProps);
40+
try {
41+
await client.connect();
42+
} catch (err) {
43+
console.debug({ err, tries });
44+
await new Promise((resolve) => setTimeout(resolve, 5000));
45+
continue;
46+
}
47+
connected = true;
48+
} while (!connected);
49+
console.debug("connected");
50+
return client;
51+
};
52+
53+
const getPassword = async (connection: Connection) => {
1654
const { SecretString } = await secretsmanager.getSecretValue({
1755
SecretId: connection.PasswordArn,
1856
});
@@ -45,23 +83,7 @@ export const createClient = async (connection: Connection) => {
4583
password = parsedSecret;
4684
}
4785

48-
const clientProps: ClientConfig = {
49-
host: connection.Host,
50-
port: connection.Port,
51-
user: connection.Username,
52-
password,
53-
database: connection.Database,
54-
};
55-
56-
console.debug(`clientProps: ${JSON.stringify(clientProps)}`);
57-
58-
if (connection.SSLMode === "require") {
59-
clientProps.ssl = {
60-
rejectUnauthorized: false,
61-
};
62-
}
63-
64-
return new Client(clientProps);
86+
return password;
6587
};
6688

6789
export const validateConnection = (connection: Connection) => {

cdk-postgresql/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@botpress/cdk-postgresql",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Postgresql constructs for AWS CDK",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

yarn.lock

-12
Original file line numberDiff line numberDiff line change
@@ -954,18 +954,6 @@
954954
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
955955
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==
956956

957-
"@botpress/[email protected]":
958-
version "2.0.0-beta.9"
959-
resolved "https://registry.yarnpkg.com/@botpress/cdk-postgresql/-/cdk-postgresql-2.0.0-beta.9.tgz#baf0a2c096c322c30fac487db868ab642cfd8177"
960-
integrity sha512-qGhwws5zJXeNWx+vCJmq7EkUiceWJJ/7KwqWJqNBzqytPPz5kKBOG55sq1rRKgn3Dk+sN7wbHndFPzcg61ZX5w==
961-
dependencies:
962-
"@aws-sdk/client-secrets-manager" "3.47.2"
963-
"@types/node" "17.0.8"
964-
esbuild "^0.14.12"
965-
pg "8.7.1"
966-
pg-format "1.0.4"
967-
verror "^1.10.1"
968-
969957
"@istanbuljs/load-nyc-config@^1.0.0":
970958
version "1.1.0"
971959
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"

0 commit comments

Comments
 (0)