-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathPostgresMetaRoles.ts
293 lines (285 loc) · 8.64 KB
/
PostgresMetaRoles.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
import { ident, literal } from 'pg-format'
import rolesSql from './sql/roles.sql'
import {
PostgresMetaResult,
PostgresRole,
PostgresRoleCreate,
PostgresRoleUpdate,
} from './types.js'
export function changeRoleConfig2Object(config: string[]) {
if (!config) {
return null
}
return config.reduce((acc: any, cur) => {
const [key, value] = cur.split('=')
acc[key] = value
return acc
}, {})
}
export default class PostgresMetaRoles {
query: (sql: string) => Promise<PostgresMetaResult<any>>
constructor(query: (sql: string) => Promise<PostgresMetaResult<any>>) {
this.query = query
}
async list({
includeDefaultRoles = false,
limit,
offset,
}: {
includeDefaultRoles?: boolean
limit?: number
offset?: number
} = {}): Promise<PostgresMetaResult<PostgresRole[]>> {
let sql = `
WITH
roles AS (${rolesSql})
SELECT
*
FROM
roles
WHERE
true`
if (!includeDefaultRoles) {
// All default/predefined roles start with pg_: https://www.postgresql.org/docs/15/predefined-roles.html
// The pg_ prefix is also reserved:
//
// ```
// postgres=# create role pg_mytmp;
// ERROR: role name "pg_mytmp" is reserved
// DETAIL: Role names starting with "pg_" are reserved.
// ```
sql += ` AND NOT pg_catalog.starts_with(name, 'pg_')`
}
if (limit) {
sql += ` LIMIT ${limit}`
}
if (offset) {
sql += ` OFFSET ${offset}`
}
const result = await this.query(sql)
if (result.data) {
result.data = result.data.map((role: any) => {
role.config = changeRoleConfig2Object(role.config)
return role
})
}
return result
}
async retrieve({ id }: { id: number }): Promise<PostgresMetaResult<PostgresRole>>
async retrieve({ name }: { name: string }): Promise<PostgresMetaResult<PostgresRole>>
async retrieve({
id,
name,
}: {
id?: number
name?: string
}): Promise<PostgresMetaResult<PostgresRole>> {
if (id) {
const sql = `${rolesSql} WHERE oid = ${literal(id)};`
const { data, error } = await this.query(sql)
if (error) {
return { data, error }
} else if (data.length === 0) {
return { data: null, error: { message: `Cannot find a role with ID ${id}` } }
} else {
data[0].config = changeRoleConfig2Object(data[0].config)
return { data: data[0], error }
}
} else if (name) {
const sql = `${rolesSql} WHERE rolname = ${literal(name)};`
const { data, error } = await this.query(sql)
if (error) {
return { data, error }
} else if (data.length === 0) {
return { data: null, error: { message: `Cannot find a role named ${name}` } }
} else {
data[0].config = changeRoleConfig2Object(data[0].config)
return { data: data[0], error }
}
} else {
return { data: null, error: { message: 'Invalid parameters on role retrieve' } }
}
}
async create({
name,
is_superuser = false,
can_create_db = false,
can_create_role = false,
inherit_role = true,
can_login = false,
is_replication_role = false,
can_bypass_rls = false,
connection_limit = -1,
password,
valid_until,
member_of,
members,
admins,
config,
}: PostgresRoleCreate): Promise<PostgresMetaResult<PostgresRole>> {
const isSuperuserClause = is_superuser ? 'SUPERUSER' : 'NOSUPERUSER'
const canCreateDbClause = can_create_db ? 'CREATEDB' : 'NOCREATEDB'
const canCreateRoleClause = can_create_role ? 'CREATEROLE' : 'NOCREATEROLE'
const inheritRoleClause = inherit_role ? 'INHERIT' : 'NOINHERIT'
const canLoginClause = can_login ? 'LOGIN' : 'NOLOGIN'
const isReplicationRoleClause = is_replication_role ? 'REPLICATION' : 'NOREPLICATION'
const canBypassRlsClause = can_bypass_rls ? 'BYPASSRLS' : 'NOBYPASSRLS'
const connectionLimitClause = `CONNECTION LIMIT ${connection_limit}`
const passwordClause = password === undefined ? '' : `PASSWORD ${literal(password)}`
const validUntilClause = valid_until === undefined ? '' : `VALID UNTIL ${literal(valid_until)}`
const memberOfClause = member_of === undefined ? '' : `IN ROLE ${member_of.join(',')}`
const membersClause = members === undefined ? '' : `ROLE ${members.join(',')}`
const adminsClause = admins === undefined ? '' : `ADMIN ${admins.join(',')}`
let configClause = ''
if (config !== undefined) {
configClause = Object.keys(config)
.map((k) => {
const v = config[k]
if (!k || !v) {
return ''
}
return `ALTER ROLE ${name} SET ${k} = ${v};`
})
.join('\n')
}
const sql = `
BEGIN;
CREATE ROLE ${ident(name)}
WITH
${isSuperuserClause}
${canCreateDbClause}
${canCreateRoleClause}
${inheritRoleClause}
${canLoginClause}
${isReplicationRoleClause}
${canBypassRlsClause}
${connectionLimitClause}
${passwordClause}
${validUntilClause}
${memberOfClause}
${membersClause}
${adminsClause};
${configClause ? configClause : ''}
COMMIT;`
const { error } = await this.query(sql)
if (error) {
return { data: null, error }
}
return await this.retrieve({ name })
}
async update(
id: number,
{
name,
is_superuser,
can_create_db,
can_create_role,
inherit_role,
can_login,
is_replication_role,
can_bypass_rls,
connection_limit,
password,
valid_until,
config,
}: PostgresRoleUpdate
): Promise<PostgresMetaResult<PostgresRole>> {
const { data: old, error } = await this.retrieve({ id })
if (error) {
return { data: null, error }
}
const nameSql =
name === undefined ? '' : `ALTER ROLE ${ident(old!.name)} RENAME TO ${ident(name)};`
let isSuperuserClause = ''
if (is_superuser !== undefined) {
isSuperuserClause = is_superuser ? 'SUPERUSER' : 'NOSUPERUSER'
}
let canCreateDbClause = ''
if (can_create_db !== undefined) {
canCreateDbClause = can_create_db ? 'CREATEDB' : 'NOCREATEDB'
}
let canCreateRoleClause = ''
if (can_create_role !== undefined) {
canCreateRoleClause = can_create_role ? 'CREATEROLE' : 'NOCREATEROLE'
}
let inheritRoleClause = ''
if (inherit_role !== undefined) {
inheritRoleClause = inherit_role ? 'INHERIT' : 'NOINHERIT'
}
let canLoginClause = ''
if (can_login !== undefined) {
canLoginClause = can_login ? 'LOGIN' : 'NOLOGIN'
}
let isReplicationRoleClause = ''
if (is_replication_role !== undefined) {
isReplicationRoleClause = is_replication_role ? 'REPLICATION' : 'NOREPLICATION'
}
let canBypassRlsClause = ''
if (can_bypass_rls !== undefined) {
canBypassRlsClause = can_bypass_rls ? 'BYPASSRLS' : 'NOBYPASSRLS'
}
const connectionLimitClause =
connection_limit === undefined ? '' : `CONNECTION LIMIT ${connection_limit}`
const passwordClause = password === undefined ? '' : `PASSWORD ${literal(password)}`
const validUntilClause = valid_until === undefined ? '' : `VALID UNTIL ${literal(valid_until)}`
let configClause = ''
if (config !== undefined) {
const configSql = config.map((c) => {
const { op, path, value } = c
const k = path
const v = value || null
if (!k) {
throw new Error(`Invalid config value ${value}`)
}
switch (op) {
case 'add':
case 'replace':
return `ALTER ROLE ${ident(old!.name)} SET ${ident(k)} = ${literal(v)};`
case 'remove':
return `ALTER ROLE ${ident(old!.name)} RESET ${ident(k)};`
default:
throw new Error(`Invalid config op ${op}`)
}
})
configClause = configSql.filter(Boolean).join('')
}
// nameSql must be last
const sql = `
BEGIN;
ALTER ROLE ${ident(old!.name)}
${isSuperuserClause}
${canCreateDbClause}
${canCreateRoleClause}
${inheritRoleClause}
${canLoginClause}
${isReplicationRoleClause}
${canBypassRlsClause}
${connectionLimitClause}
${passwordClause}
${validUntilClause};
${configClause ? configClause : ''}
${nameSql}
COMMIT;`
{
const { error } = await this.query(sql)
if (error) {
return { data: null, error }
}
}
return await this.retrieve({ id })
}
async remove(id: number): Promise<PostgresMetaResult<PostgresRole>> {
const { data: role, error } = await this.retrieve({ id })
if (error) {
return { data: null, error }
}
const sql = `DROP ROLE ${ident(role!.name)};`
{
const { error } = await this.query(sql)
if (error) {
return { data: null, error }
}
}
return { data: role!, error: null }
}
}