@onivoro/server-aws-redshift 22.0.0 → 22.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/lib/classes/server-aws-redshift-config.class.d.ts +2 -3
- package/dist/cjs/lib/classes/server-aws-redshift-config.class.js +0 -1
- package/dist/cjs/lib/services/redshift.service.d.ts +19 -22
- package/dist/cjs/lib/services/redshift.service.js +38 -68
- package/dist/esm/lib/classes/server-aws-redshift-config.class.d.ts +2 -3
- package/dist/esm/lib/classes/server-aws-redshift-config.class.js +0 -1
- package/dist/esm/lib/services/redshift.service.d.ts +19 -22
- package/dist/esm/lib/services/redshift.service.js +38 -68
- package/dist/types/lib/classes/server-aws-redshift-config.class.d.ts +2 -3
- package/dist/types/lib/services/redshift.service.d.ts +19 -22
- package/package.json +1 -1
|
@@ -1,34 +1,31 @@
|
|
|
1
1
|
import { RedshiftDataClient } from "@aws-sdk/client-redshift-data";
|
|
2
|
-
import { ServerAwsRedshiftDataConfig } from '../classes/server-aws-redshift-config.class';
|
|
3
2
|
import { RedshiftServerlessClient } from '@aws-sdk/client-redshift-serverless';
|
|
4
3
|
export declare class RedshiftDataService {
|
|
5
4
|
private redshiftDataClient;
|
|
6
5
|
private redshiftServerlessClient;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
waitForStatement(statementId: string): Promise<void>;
|
|
6
|
+
constructor(redshiftDataClient: RedshiftDataClient, redshiftServerlessClient: RedshiftServerlessClient);
|
|
7
|
+
waitForStatement(statementId: string, maxAttempts?: number, delay?: number): Promise<void>;
|
|
10
8
|
verifyEndpointAccess(workgroupName: string): Promise<import("@aws-sdk/client-redshift-serverless").GetWorkgroupCommandOutput>;
|
|
11
|
-
addIamUserToDatabaseGroup(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
addIamUserToDatabaseGroup(_: {
|
|
10
|
+
database: string;
|
|
11
|
+
workgroupName: string;
|
|
12
|
+
user: string;
|
|
13
|
+
group: string;
|
|
16
14
|
}): Promise<void>;
|
|
17
|
-
createDatabaseUser(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
createDatabaseUser(_: {
|
|
16
|
+
database: string;
|
|
17
|
+
workgroupName: string;
|
|
18
|
+
user: string;
|
|
21
19
|
}): Promise<boolean>;
|
|
22
|
-
grantUsageOnSchema(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
Group: string;
|
|
20
|
+
grantUsageOnSchema(_: {
|
|
21
|
+
database: string;
|
|
22
|
+
workgroupName: string;
|
|
23
|
+
schema: string;
|
|
24
|
+
group: string;
|
|
28
25
|
}): Promise<void>;
|
|
29
|
-
createDbGroupFromIamGroupIfNotExists(
|
|
26
|
+
createDbGroupFromIamGroupIfNotExists(_: {
|
|
30
27
|
iamGroup: string;
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
database: string;
|
|
29
|
+
workgroupName: string;
|
|
33
30
|
}): Promise<any>;
|
|
34
31
|
}
|
|
@@ -12,20 +12,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.RedshiftDataService = void 0;
|
|
13
13
|
const common_1 = require("@nestjs/common");
|
|
14
14
|
const client_redshift_data_1 = require("@aws-sdk/client-redshift-data");
|
|
15
|
-
const server_aws_redshift_config_class_1 = require("../classes/server-aws-redshift-config.class");
|
|
16
15
|
const client_redshift_serverless_1 = require("@aws-sdk/client-redshift-serverless");
|
|
17
16
|
const crypto_1 = require("crypto");
|
|
18
17
|
let RedshiftDataService = class RedshiftDataService {
|
|
19
18
|
redshiftDataClient;
|
|
20
19
|
redshiftServerlessClient;
|
|
21
|
-
|
|
22
|
-
constructor(redshiftDataClient, redshiftServerlessClient, config) {
|
|
20
|
+
constructor(redshiftDataClient, redshiftServerlessClient) {
|
|
23
21
|
this.redshiftDataClient = redshiftDataClient;
|
|
24
22
|
this.redshiftServerlessClient = redshiftServerlessClient;
|
|
25
|
-
this.config = config;
|
|
26
23
|
}
|
|
27
|
-
async waitForStatement(statementId) {
|
|
28
|
-
const maxAttempts = 10;
|
|
24
|
+
async waitForStatement(statementId, maxAttempts = 10, delay = 1000) {
|
|
29
25
|
let attempts = 0;
|
|
30
26
|
while (attempts < maxAttempts) {
|
|
31
27
|
const describeStatementCommand = new client_redshift_data_1.DescribeStatementCommand({
|
|
@@ -39,40 +35,38 @@ let RedshiftDataService = class RedshiftDataService {
|
|
|
39
35
|
throw new Error(`SQL statement failed: ${status.Error}`);
|
|
40
36
|
}
|
|
41
37
|
attempts++;
|
|
42
|
-
await new Promise(resolve => setTimeout(resolve,
|
|
38
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
43
39
|
}
|
|
44
40
|
throw new Error('Timeout waiting for SQL statement to complete');
|
|
45
41
|
}
|
|
46
42
|
async verifyEndpointAccess(workgroupName) {
|
|
47
43
|
const workgroupResponse = await this.redshiftServerlessClient.send(new client_redshift_serverless_1.GetWorkgroupCommand({ workgroupName }));
|
|
48
44
|
if (!workgroupResponse?.workgroup?.endpoint?.address) {
|
|
49
|
-
console.error(`Could not retrieve endpoint for
|
|
45
|
+
console.error(`Could not retrieve endpoint for workgroupName "${workgroupName}"`);
|
|
50
46
|
return;
|
|
51
47
|
}
|
|
52
48
|
return workgroupResponse;
|
|
53
49
|
}
|
|
54
|
-
async addIamUserToDatabaseGroup(
|
|
55
|
-
const { Database, WorkgroupName, User, Group } = params;
|
|
50
|
+
async addIamUserToDatabaseGroup(_) {
|
|
56
51
|
try {
|
|
57
52
|
const groupResult = await this.redshiftDataClient.send(new client_redshift_data_1.ExecuteStatementCommand({
|
|
58
|
-
Database,
|
|
59
|
-
Sql: `ALTER GROUP ${
|
|
60
|
-
WorkgroupName
|
|
53
|
+
Database: _.database,
|
|
54
|
+
Sql: `ALTER GROUP ${_.group} ADD USER "${_.user}";`,
|
|
55
|
+
WorkgroupName: _.workgroupName
|
|
61
56
|
}));
|
|
62
57
|
await this.waitForStatement(groupResult.Id);
|
|
63
|
-
console.log(`Added user "${
|
|
58
|
+
console.log(`Added user "${_.user}" to group "${_.group}`);
|
|
64
59
|
}
|
|
65
60
|
catch (error) {
|
|
66
|
-
console.warn({ detail: `Warning adding ${
|
|
61
|
+
console.warn({ detail: `Warning adding ${_.user} to group:`, error });
|
|
67
62
|
}
|
|
68
63
|
}
|
|
69
|
-
async createDatabaseUser(
|
|
70
|
-
const { Database, WorkgroupName, User } = params;
|
|
64
|
+
async createDatabaseUser(_) {
|
|
71
65
|
try {
|
|
72
66
|
const checkResult = await this.redshiftDataClient.send(new client_redshift_data_1.ExecuteStatementCommand({
|
|
73
|
-
Database,
|
|
67
|
+
Database: _.database,
|
|
74
68
|
Sql: `SELECT u.usename as username FROM pg_user u;`,
|
|
75
|
-
WorkgroupName
|
|
69
|
+
WorkgroupName: _.workgroupName
|
|
76
70
|
}));
|
|
77
71
|
await this.waitForStatement(checkResult.Id);
|
|
78
72
|
const describeCommand = new client_redshift_data_1.DescribeStatementCommand({ Id: checkResult.Id });
|
|
@@ -80,97 +74,74 @@ let RedshiftDataService = class RedshiftDataService {
|
|
|
80
74
|
if (!checkStatus.ResultRows) {
|
|
81
75
|
const placeholderPassword = `IAM_${(0, crypto_1.randomUUID)().replace(/-/g, '_')}`;
|
|
82
76
|
const createUserCommand = new client_redshift_data_1.ExecuteStatementCommand({
|
|
83
|
-
Database,
|
|
84
|
-
Sql: `CREATE USER ${
|
|
85
|
-
WorkgroupName
|
|
77
|
+
Database: _.database,
|
|
78
|
+
Sql: `CREATE USER ${_.user} PASSWORD '${placeholderPassword}';`,
|
|
79
|
+
WorkgroupName: _.workgroupName
|
|
86
80
|
});
|
|
87
81
|
const createResult = await this.redshiftDataClient.send(createUserCommand);
|
|
88
82
|
await this.waitForStatement(createResult.Id);
|
|
89
|
-
console.log(`Created user: ${
|
|
83
|
+
console.log(`Created user: ${_.user}`);
|
|
90
84
|
}
|
|
91
85
|
else {
|
|
92
|
-
console.log(`User "${
|
|
86
|
+
console.log(`User "${_.user} already exists`);
|
|
93
87
|
}
|
|
94
88
|
return true;
|
|
95
89
|
}
|
|
96
90
|
catch (error) {
|
|
97
|
-
console.warn({ detail: `Warning creating user ${
|
|
91
|
+
console.warn({ detail: `Warning creating user ${_.user}`, error });
|
|
98
92
|
return false;
|
|
99
93
|
}
|
|
100
94
|
}
|
|
101
|
-
async grantUsageOnSchema(
|
|
102
|
-
const { GroupName, Database, WorkgroupName, Schema, Group } = params;
|
|
95
|
+
async grantUsageOnSchema(_) {
|
|
103
96
|
try {
|
|
104
|
-
const checkResult = await this.redshiftDataClient.send(new client_redshift_data_1.ExecuteStatementCommand({
|
|
105
|
-
Database,
|
|
106
|
-
Sql: `SELECT 1 FROM pg_group WHERE groname = '${Group}';`,
|
|
107
|
-
WorkgroupName
|
|
108
|
-
}));
|
|
109
|
-
await this.waitForStatement(checkResult.Id);
|
|
110
|
-
const describeCommand = new client_redshift_data_1.DescribeStatementCommand({ Id: checkResult.Id });
|
|
111
|
-
const checkStatus = await this.redshiftDataClient.send(describeCommand);
|
|
112
|
-
if (!checkStatus.ResultRows) {
|
|
113
|
-
const createGroupCommand = new client_redshift_data_1.ExecuteStatementCommand({
|
|
114
|
-
Database,
|
|
115
|
-
Sql: 'CREATE GROUP ${Group};',
|
|
116
|
-
WorkgroupName
|
|
117
|
-
});
|
|
118
|
-
const createResult = await this.redshiftDataClient.send(createGroupCommand);
|
|
119
|
-
await this.waitForStatement(createResult.Id);
|
|
120
|
-
console.log('Created "${Group}" group');
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
console.log('"${Group}" group already exists');
|
|
124
|
-
}
|
|
125
97
|
const setupStatements = [
|
|
126
|
-
`GRANT USAGE ON SCHEMA ${
|
|
127
|
-
`GRANT SELECT ON ALL TABLES IN SCHEMA ${
|
|
128
|
-
`ALTER DEFAULT PRIVILEGES IN SCHEMA ${
|
|
98
|
+
`GRANT USAGE ON SCHEMA ${_.schema} TO GROUP ${_.group};`,
|
|
99
|
+
`GRANT SELECT ON ALL TABLES IN SCHEMA ${_.schema} TO GROUP ${_.group};`,
|
|
100
|
+
`ALTER DEFAULT PRIVILEGES IN SCHEMA ${_.schema} GRANT SELECT ON TABLES TO GROUP ${_.group};`,
|
|
129
101
|
];
|
|
130
102
|
for (const Sql of setupStatements) {
|
|
131
103
|
const executeStatementCommand = new client_redshift_data_1.ExecuteStatementCommand({
|
|
132
|
-
Database,
|
|
104
|
+
Database: _.database,
|
|
133
105
|
Sql,
|
|
134
|
-
WorkgroupName
|
|
106
|
+
WorkgroupName: _.workgroupName
|
|
135
107
|
});
|
|
136
108
|
try {
|
|
137
109
|
const result = await this.redshiftDataClient.send(executeStatementCommand);
|
|
138
110
|
await this.waitForStatement(result.Id);
|
|
139
|
-
console.log(
|
|
111
|
+
console.log(Sql);
|
|
140
112
|
}
|
|
141
113
|
catch (error) {
|
|
142
|
-
console.warn(
|
|
114
|
+
console.warn({ Sql, error });
|
|
143
115
|
}
|
|
144
116
|
}
|
|
145
117
|
}
|
|
146
118
|
catch (error) {
|
|
147
|
-
console.error({
|
|
119
|
+
console.error({ error });
|
|
148
120
|
throw error;
|
|
149
121
|
}
|
|
150
122
|
}
|
|
151
|
-
async createDbGroupFromIamGroupIfNotExists(
|
|
152
|
-
const { iamGroup, Database, WorkgroupName } = params;
|
|
123
|
+
async createDbGroupFromIamGroupIfNotExists(_) {
|
|
153
124
|
try {
|
|
154
125
|
const checkResult = await this.redshiftDataClient.send(new client_redshift_data_1.ExecuteStatementCommand({
|
|
155
|
-
Database,
|
|
156
|
-
Sql: `SELECT 1 FROM pg_group WHERE groname = '${iamGroup}';`,
|
|
157
|
-
WorkgroupName
|
|
126
|
+
Database: _.database,
|
|
127
|
+
Sql: `SELECT 1 FROM pg_group WHERE groname = '${_.iamGroup}';`,
|
|
128
|
+
WorkgroupName: _.workgroupName
|
|
158
129
|
}));
|
|
159
130
|
await this.waitForStatement(checkResult.Id);
|
|
160
131
|
const describeCommand = new client_redshift_data_1.DescribeStatementCommand({ Id: checkResult.Id });
|
|
161
132
|
const checkStatus = await this.redshiftDataClient.send(describeCommand);
|
|
162
133
|
if (!checkStatus.ResultRows) {
|
|
163
134
|
const createGroupCommand = new client_redshift_data_1.ExecuteStatementCommand({
|
|
164
|
-
Database,
|
|
165
|
-
Sql: `CREATE GROUP ${iamGroup};`,
|
|
166
|
-
WorkgroupName
|
|
135
|
+
Database: _.database,
|
|
136
|
+
Sql: `CREATE GROUP ${_.iamGroup};`,
|
|
137
|
+
WorkgroupName: _.workgroupName
|
|
167
138
|
});
|
|
168
139
|
const createResult = await this.redshiftDataClient.send(createGroupCommand);
|
|
169
140
|
await this.waitForStatement(createResult.Id);
|
|
170
|
-
console.log(`Created "${iamGroup}" group`);
|
|
141
|
+
console.log(`Created "${_.iamGroup}" group`);
|
|
171
142
|
}
|
|
172
143
|
else {
|
|
173
|
-
console.log(`"${iamGroup}" group already exists`);
|
|
144
|
+
console.log(`"${_.iamGroup}" group already exists`);
|
|
174
145
|
}
|
|
175
146
|
}
|
|
176
147
|
catch (error) {
|
|
@@ -183,6 +154,5 @@ exports.RedshiftDataService = RedshiftDataService;
|
|
|
183
154
|
exports.RedshiftDataService = RedshiftDataService = __decorate([
|
|
184
155
|
(0, common_1.Injectable)(),
|
|
185
156
|
__metadata("design:paramtypes", [client_redshift_data_1.RedshiftDataClient,
|
|
186
|
-
client_redshift_serverless_1.RedshiftServerlessClient
|
|
187
|
-
server_aws_redshift_config_class_1.ServerAwsRedshiftDataConfig])
|
|
157
|
+
client_redshift_serverless_1.RedshiftServerlessClient])
|
|
188
158
|
], RedshiftDataService);
|
|
@@ -1,34 +1,31 @@
|
|
|
1
1
|
import { RedshiftDataClient } from "@aws-sdk/client-redshift-data";
|
|
2
|
-
import { ServerAwsRedshiftDataConfig } from '../classes/server-aws-redshift-config.class';
|
|
3
2
|
import { RedshiftServerlessClient } from '@aws-sdk/client-redshift-serverless';
|
|
4
3
|
export declare class RedshiftDataService {
|
|
5
4
|
private redshiftDataClient;
|
|
6
5
|
private redshiftServerlessClient;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
waitForStatement(statementId: string): Promise<void>;
|
|
6
|
+
constructor(redshiftDataClient: RedshiftDataClient, redshiftServerlessClient: RedshiftServerlessClient);
|
|
7
|
+
waitForStatement(statementId: string, maxAttempts?: number, delay?: number): Promise<void>;
|
|
10
8
|
verifyEndpointAccess(workgroupName: string): Promise<import("@aws-sdk/client-redshift-serverless").GetWorkgroupCommandOutput>;
|
|
11
|
-
addIamUserToDatabaseGroup(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
addIamUserToDatabaseGroup(_: {
|
|
10
|
+
database: string;
|
|
11
|
+
workgroupName: string;
|
|
12
|
+
user: string;
|
|
13
|
+
group: string;
|
|
16
14
|
}): Promise<void>;
|
|
17
|
-
createDatabaseUser(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
createDatabaseUser(_: {
|
|
16
|
+
database: string;
|
|
17
|
+
workgroupName: string;
|
|
18
|
+
user: string;
|
|
21
19
|
}): Promise<boolean>;
|
|
22
|
-
grantUsageOnSchema(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
Group: string;
|
|
20
|
+
grantUsageOnSchema(_: {
|
|
21
|
+
database: string;
|
|
22
|
+
workgroupName: string;
|
|
23
|
+
schema: string;
|
|
24
|
+
group: string;
|
|
28
25
|
}): Promise<void>;
|
|
29
|
-
createDbGroupFromIamGroupIfNotExists(
|
|
26
|
+
createDbGroupFromIamGroupIfNotExists(_: {
|
|
30
27
|
iamGroup: string;
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
database: string;
|
|
29
|
+
workgroupName: string;
|
|
33
30
|
}): Promise<any>;
|
|
34
31
|
}
|
|
@@ -12,20 +12,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.RedshiftDataService = void 0;
|
|
13
13
|
const common_1 = require("@nestjs/common");
|
|
14
14
|
const client_redshift_data_1 = require("@aws-sdk/client-redshift-data");
|
|
15
|
-
const server_aws_redshift_config_class_1 = require("../classes/server-aws-redshift-config.class");
|
|
16
15
|
const client_redshift_serverless_1 = require("@aws-sdk/client-redshift-serverless");
|
|
17
16
|
const crypto_1 = require("crypto");
|
|
18
17
|
let RedshiftDataService = class RedshiftDataService {
|
|
19
18
|
redshiftDataClient;
|
|
20
19
|
redshiftServerlessClient;
|
|
21
|
-
|
|
22
|
-
constructor(redshiftDataClient, redshiftServerlessClient, config) {
|
|
20
|
+
constructor(redshiftDataClient, redshiftServerlessClient) {
|
|
23
21
|
this.redshiftDataClient = redshiftDataClient;
|
|
24
22
|
this.redshiftServerlessClient = redshiftServerlessClient;
|
|
25
|
-
this.config = config;
|
|
26
23
|
}
|
|
27
|
-
async waitForStatement(statementId) {
|
|
28
|
-
const maxAttempts = 10;
|
|
24
|
+
async waitForStatement(statementId, maxAttempts = 10, delay = 1000) {
|
|
29
25
|
let attempts = 0;
|
|
30
26
|
while (attempts < maxAttempts) {
|
|
31
27
|
const describeStatementCommand = new client_redshift_data_1.DescribeStatementCommand({
|
|
@@ -39,40 +35,38 @@ let RedshiftDataService = class RedshiftDataService {
|
|
|
39
35
|
throw new Error(`SQL statement failed: ${status.Error}`);
|
|
40
36
|
}
|
|
41
37
|
attempts++;
|
|
42
|
-
await new Promise(resolve => setTimeout(resolve,
|
|
38
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
43
39
|
}
|
|
44
40
|
throw new Error('Timeout waiting for SQL statement to complete');
|
|
45
41
|
}
|
|
46
42
|
async verifyEndpointAccess(workgroupName) {
|
|
47
43
|
const workgroupResponse = await this.redshiftServerlessClient.send(new client_redshift_serverless_1.GetWorkgroupCommand({ workgroupName }));
|
|
48
44
|
if (!workgroupResponse?.workgroup?.endpoint?.address) {
|
|
49
|
-
console.error(`Could not retrieve endpoint for
|
|
45
|
+
console.error(`Could not retrieve endpoint for workgroupName "${workgroupName}"`);
|
|
50
46
|
return;
|
|
51
47
|
}
|
|
52
48
|
return workgroupResponse;
|
|
53
49
|
}
|
|
54
|
-
async addIamUserToDatabaseGroup(
|
|
55
|
-
const { Database, WorkgroupName, User, Group } = params;
|
|
50
|
+
async addIamUserToDatabaseGroup(_) {
|
|
56
51
|
try {
|
|
57
52
|
const groupResult = await this.redshiftDataClient.send(new client_redshift_data_1.ExecuteStatementCommand({
|
|
58
|
-
Database,
|
|
59
|
-
Sql: `ALTER GROUP ${
|
|
60
|
-
WorkgroupName
|
|
53
|
+
Database: _.database,
|
|
54
|
+
Sql: `ALTER GROUP ${_.group} ADD USER "${_.user}";`,
|
|
55
|
+
WorkgroupName: _.workgroupName
|
|
61
56
|
}));
|
|
62
57
|
await this.waitForStatement(groupResult.Id);
|
|
63
|
-
console.log(`Added user "${
|
|
58
|
+
console.log(`Added user "${_.user}" to group "${_.group}`);
|
|
64
59
|
}
|
|
65
60
|
catch (error) {
|
|
66
|
-
console.warn({ detail: `Warning adding ${
|
|
61
|
+
console.warn({ detail: `Warning adding ${_.user} to group:`, error });
|
|
67
62
|
}
|
|
68
63
|
}
|
|
69
|
-
async createDatabaseUser(
|
|
70
|
-
const { Database, WorkgroupName, User } = params;
|
|
64
|
+
async createDatabaseUser(_) {
|
|
71
65
|
try {
|
|
72
66
|
const checkResult = await this.redshiftDataClient.send(new client_redshift_data_1.ExecuteStatementCommand({
|
|
73
|
-
Database,
|
|
67
|
+
Database: _.database,
|
|
74
68
|
Sql: `SELECT u.usename as username FROM pg_user u;`,
|
|
75
|
-
WorkgroupName
|
|
69
|
+
WorkgroupName: _.workgroupName
|
|
76
70
|
}));
|
|
77
71
|
await this.waitForStatement(checkResult.Id);
|
|
78
72
|
const describeCommand = new client_redshift_data_1.DescribeStatementCommand({ Id: checkResult.Id });
|
|
@@ -80,97 +74,74 @@ let RedshiftDataService = class RedshiftDataService {
|
|
|
80
74
|
if (!checkStatus.ResultRows) {
|
|
81
75
|
const placeholderPassword = `IAM_${(0, crypto_1.randomUUID)().replace(/-/g, '_')}`;
|
|
82
76
|
const createUserCommand = new client_redshift_data_1.ExecuteStatementCommand({
|
|
83
|
-
Database,
|
|
84
|
-
Sql: `CREATE USER ${
|
|
85
|
-
WorkgroupName
|
|
77
|
+
Database: _.database,
|
|
78
|
+
Sql: `CREATE USER ${_.user} PASSWORD '${placeholderPassword}';`,
|
|
79
|
+
WorkgroupName: _.workgroupName
|
|
86
80
|
});
|
|
87
81
|
const createResult = await this.redshiftDataClient.send(createUserCommand);
|
|
88
82
|
await this.waitForStatement(createResult.Id);
|
|
89
|
-
console.log(`Created user: ${
|
|
83
|
+
console.log(`Created user: ${_.user}`);
|
|
90
84
|
}
|
|
91
85
|
else {
|
|
92
|
-
console.log(`User "${
|
|
86
|
+
console.log(`User "${_.user} already exists`);
|
|
93
87
|
}
|
|
94
88
|
return true;
|
|
95
89
|
}
|
|
96
90
|
catch (error) {
|
|
97
|
-
console.warn({ detail: `Warning creating user ${
|
|
91
|
+
console.warn({ detail: `Warning creating user ${_.user}`, error });
|
|
98
92
|
return false;
|
|
99
93
|
}
|
|
100
94
|
}
|
|
101
|
-
async grantUsageOnSchema(
|
|
102
|
-
const { GroupName, Database, WorkgroupName, Schema, Group } = params;
|
|
95
|
+
async grantUsageOnSchema(_) {
|
|
103
96
|
try {
|
|
104
|
-
const checkResult = await this.redshiftDataClient.send(new client_redshift_data_1.ExecuteStatementCommand({
|
|
105
|
-
Database,
|
|
106
|
-
Sql: `SELECT 1 FROM pg_group WHERE groname = '${Group}';`,
|
|
107
|
-
WorkgroupName
|
|
108
|
-
}));
|
|
109
|
-
await this.waitForStatement(checkResult.Id);
|
|
110
|
-
const describeCommand = new client_redshift_data_1.DescribeStatementCommand({ Id: checkResult.Id });
|
|
111
|
-
const checkStatus = await this.redshiftDataClient.send(describeCommand);
|
|
112
|
-
if (!checkStatus.ResultRows) {
|
|
113
|
-
const createGroupCommand = new client_redshift_data_1.ExecuteStatementCommand({
|
|
114
|
-
Database,
|
|
115
|
-
Sql: 'CREATE GROUP ${Group};',
|
|
116
|
-
WorkgroupName
|
|
117
|
-
});
|
|
118
|
-
const createResult = await this.redshiftDataClient.send(createGroupCommand);
|
|
119
|
-
await this.waitForStatement(createResult.Id);
|
|
120
|
-
console.log('Created "${Group}" group');
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
console.log('"${Group}" group already exists');
|
|
124
|
-
}
|
|
125
97
|
const setupStatements = [
|
|
126
|
-
`GRANT USAGE ON SCHEMA ${
|
|
127
|
-
`GRANT SELECT ON ALL TABLES IN SCHEMA ${
|
|
128
|
-
`ALTER DEFAULT PRIVILEGES IN SCHEMA ${
|
|
98
|
+
`GRANT USAGE ON SCHEMA ${_.schema} TO GROUP ${_.group};`,
|
|
99
|
+
`GRANT SELECT ON ALL TABLES IN SCHEMA ${_.schema} TO GROUP ${_.group};`,
|
|
100
|
+
`ALTER DEFAULT PRIVILEGES IN SCHEMA ${_.schema} GRANT SELECT ON TABLES TO GROUP ${_.group};`,
|
|
129
101
|
];
|
|
130
102
|
for (const Sql of setupStatements) {
|
|
131
103
|
const executeStatementCommand = new client_redshift_data_1.ExecuteStatementCommand({
|
|
132
|
-
Database,
|
|
104
|
+
Database: _.database,
|
|
133
105
|
Sql,
|
|
134
|
-
WorkgroupName
|
|
106
|
+
WorkgroupName: _.workgroupName
|
|
135
107
|
});
|
|
136
108
|
try {
|
|
137
109
|
const result = await this.redshiftDataClient.send(executeStatementCommand);
|
|
138
110
|
await this.waitForStatement(result.Id);
|
|
139
|
-
console.log(
|
|
111
|
+
console.log(Sql);
|
|
140
112
|
}
|
|
141
113
|
catch (error) {
|
|
142
|
-
console.warn(
|
|
114
|
+
console.warn({ Sql, error });
|
|
143
115
|
}
|
|
144
116
|
}
|
|
145
117
|
}
|
|
146
118
|
catch (error) {
|
|
147
|
-
console.error({
|
|
119
|
+
console.error({ error });
|
|
148
120
|
throw error;
|
|
149
121
|
}
|
|
150
122
|
}
|
|
151
|
-
async createDbGroupFromIamGroupIfNotExists(
|
|
152
|
-
const { iamGroup, Database, WorkgroupName } = params;
|
|
123
|
+
async createDbGroupFromIamGroupIfNotExists(_) {
|
|
153
124
|
try {
|
|
154
125
|
const checkResult = await this.redshiftDataClient.send(new client_redshift_data_1.ExecuteStatementCommand({
|
|
155
|
-
Database,
|
|
156
|
-
Sql: `SELECT 1 FROM pg_group WHERE groname = '${iamGroup}';`,
|
|
157
|
-
WorkgroupName
|
|
126
|
+
Database: _.database,
|
|
127
|
+
Sql: `SELECT 1 FROM pg_group WHERE groname = '${_.iamGroup}';`,
|
|
128
|
+
WorkgroupName: _.workgroupName
|
|
158
129
|
}));
|
|
159
130
|
await this.waitForStatement(checkResult.Id);
|
|
160
131
|
const describeCommand = new client_redshift_data_1.DescribeStatementCommand({ Id: checkResult.Id });
|
|
161
132
|
const checkStatus = await this.redshiftDataClient.send(describeCommand);
|
|
162
133
|
if (!checkStatus.ResultRows) {
|
|
163
134
|
const createGroupCommand = new client_redshift_data_1.ExecuteStatementCommand({
|
|
164
|
-
Database,
|
|
165
|
-
Sql: `CREATE GROUP ${iamGroup};`,
|
|
166
|
-
WorkgroupName
|
|
135
|
+
Database: _.database,
|
|
136
|
+
Sql: `CREATE GROUP ${_.iamGroup};`,
|
|
137
|
+
WorkgroupName: _.workgroupName
|
|
167
138
|
});
|
|
168
139
|
const createResult = await this.redshiftDataClient.send(createGroupCommand);
|
|
169
140
|
await this.waitForStatement(createResult.Id);
|
|
170
|
-
console.log(`Created "${iamGroup}" group`);
|
|
141
|
+
console.log(`Created "${_.iamGroup}" group`);
|
|
171
142
|
}
|
|
172
143
|
else {
|
|
173
|
-
console.log(`"${iamGroup}" group already exists`);
|
|
144
|
+
console.log(`"${_.iamGroup}" group already exists`);
|
|
174
145
|
}
|
|
175
146
|
}
|
|
176
147
|
catch (error) {
|
|
@@ -183,6 +154,5 @@ exports.RedshiftDataService = RedshiftDataService;
|
|
|
183
154
|
exports.RedshiftDataService = RedshiftDataService = __decorate([
|
|
184
155
|
(0, common_1.Injectable)(),
|
|
185
156
|
__metadata("design:paramtypes", [client_redshift_data_1.RedshiftDataClient,
|
|
186
|
-
client_redshift_serverless_1.RedshiftServerlessClient
|
|
187
|
-
server_aws_redshift_config_class_1.ServerAwsRedshiftDataConfig])
|
|
157
|
+
client_redshift_serverless_1.RedshiftServerlessClient])
|
|
188
158
|
], RedshiftDataService);
|
|
@@ -1,34 +1,31 @@
|
|
|
1
1
|
import { RedshiftDataClient } from "@aws-sdk/client-redshift-data";
|
|
2
|
-
import { ServerAwsRedshiftDataConfig } from '../classes/server-aws-redshift-config.class';
|
|
3
2
|
import { RedshiftServerlessClient } from '@aws-sdk/client-redshift-serverless';
|
|
4
3
|
export declare class RedshiftDataService {
|
|
5
4
|
private redshiftDataClient;
|
|
6
5
|
private redshiftServerlessClient;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
waitForStatement(statementId: string): Promise<void>;
|
|
6
|
+
constructor(redshiftDataClient: RedshiftDataClient, redshiftServerlessClient: RedshiftServerlessClient);
|
|
7
|
+
waitForStatement(statementId: string, maxAttempts?: number, delay?: number): Promise<void>;
|
|
10
8
|
verifyEndpointAccess(workgroupName: string): Promise<import("@aws-sdk/client-redshift-serverless").GetWorkgroupCommandOutput>;
|
|
11
|
-
addIamUserToDatabaseGroup(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
addIamUserToDatabaseGroup(_: {
|
|
10
|
+
database: string;
|
|
11
|
+
workgroupName: string;
|
|
12
|
+
user: string;
|
|
13
|
+
group: string;
|
|
16
14
|
}): Promise<void>;
|
|
17
|
-
createDatabaseUser(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
createDatabaseUser(_: {
|
|
16
|
+
database: string;
|
|
17
|
+
workgroupName: string;
|
|
18
|
+
user: string;
|
|
21
19
|
}): Promise<boolean>;
|
|
22
|
-
grantUsageOnSchema(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
Group: string;
|
|
20
|
+
grantUsageOnSchema(_: {
|
|
21
|
+
database: string;
|
|
22
|
+
workgroupName: string;
|
|
23
|
+
schema: string;
|
|
24
|
+
group: string;
|
|
28
25
|
}): Promise<void>;
|
|
29
|
-
createDbGroupFromIamGroupIfNotExists(
|
|
26
|
+
createDbGroupFromIamGroupIfNotExists(_: {
|
|
30
27
|
iamGroup: string;
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
database: string;
|
|
29
|
+
workgroupName: string;
|
|
33
30
|
}): Promise<any>;
|
|
34
31
|
}
|