-
Notifications
You must be signed in to change notification settings - Fork 419
/
grant.go
470 lines (410 loc) · 14.6 KB
/
grant.go
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
467
468
469
470
package snowflake
import (
"database/sql"
"errors"
"fmt"
"log"
"strings"
"github.com/jmoiron/sqlx"
)
type grantType string
const (
accountType grantType = "ACCOUNT"
resourceMonitorType grantType = "RESOURCE MONITOR"
integrationType grantType = "INTEGRATION"
databaseType grantType = "DATABASE"
schemaType grantType = "SCHEMA"
stageType grantType = "STAGE"
viewType grantType = "VIEW"
materializedViewType grantType = "MATERIALIZED VIEW"
tableType grantType = "TABLE"
warehouseType grantType = "WAREHOUSE"
externalTableType grantType = "EXTERNAL TABLE"
failoverGroupType grantType = "FAILOVER GROUP"
fileFormatType grantType = "FILE FORMAT"
functionType grantType = "FUNCTION"
procedureType grantType = "PROCEDURE"
sequenceType grantType = "SEQUENCE"
streamType grantType = "STREAM"
maskingPolicyType grantType = "MASKING POLICY"
pipeType grantType = "PIPE"
taskType grantType = "TASK"
rowAccessPolicyType grantType = "ROW ACCESS POLICY"
tagType grantType = "TAG"
userGrantType grantType = "USER"
)
type GrantExecutable interface {
Grant(p string, w bool) string
Revoke(p string) []string
RevokeOwnership(r string) []string
Show() string
}
type GrantBuilder interface {
Name() string
GrantType() string
Role(string) GrantExecutable
Share(string) GrantExecutable
Show() string
}
// CurrentGrantBuilder abstracts the creation of GrantExecutables.
type CurrentGrantBuilder struct {
name string
qualifiedName string
grantType grantType
}
// Name returns the object name for this CurrentGrantBuilder.
func (gb *CurrentGrantBuilder) Name() string {
return gb.name
}
func (gb *CurrentGrantBuilder) GrantType() string {
return string(gb.grantType)
}
// Show returns the SQL that will show all privileges on the grant.
func (gb *CurrentGrantBuilder) Show() string {
return fmt.Sprintf(`SHOW GRANTS ON %v %v`, gb.grantType, gb.qualifiedName)
}
// /////////////////////////////////////////////
// START CurrentMaterializedViewGrantBuilder //
// /////////////////////////////////////////////.
type CurrentMaterializedViewGrantBuilder struct {
name string
qualifiedName string
grantType grantType
}
// Name returns the object name for this CurrentGrantBuilder.
func (gb *CurrentMaterializedViewGrantBuilder) Name() string {
return gb.name
}
func (gb *CurrentMaterializedViewGrantBuilder) GrantType() string {
return string(gb.grantType)
}
// Show returns the SQL that will show all privileges on the grant.
func (gb *CurrentMaterializedViewGrantBuilder) Show() string {
return fmt.Sprintf(`SHOW GRANTS ON %v %v`, gb.grantType, gb.qualifiedName)
}
// Role returns a pointer to a CurrentGrantExecutable for a role.
func (gb *CurrentMaterializedViewGrantBuilder) Role(n string) GrantExecutable {
return &CurrentGrantExecutable{
grantName: gb.qualifiedName,
grantType: viewType,
granteeName: n,
granteeType: roleType,
}
}
// Share returns a pointer to a CurrentGrantExecutable for a share.
func (gb *CurrentMaterializedViewGrantBuilder) Share(n string) GrantExecutable {
return &CurrentGrantExecutable{
grantName: gb.qualifiedName,
grantType: viewType,
granteeName: n,
granteeType: shareType,
}
}
///////////////////////////////////////////////
/// END CurrentMaterializedViewGrantBuilder ///
///////////////////////////////////////////////
// AccountGrant returns a pointer to a CurrentGrantBuilder for an account.
func AccountGrant() GrantBuilder {
return &CurrentGrantBuilder{
grantType: accountType,
}
}
// DatabaseGrant returns a pointer to a CurrentGrantBuilder for a database.
func DatabaseGrant(name string) GrantBuilder {
return &CurrentGrantBuilder{
name: name,
qualifiedName: fmt.Sprintf(`"%v"`, name),
grantType: databaseType,
}
}
// SchemaGrant returns a pointer to a CurrentGrantBuilder for a schema.
func SchemaGrant(db, schema string) GrantBuilder {
return &CurrentGrantBuilder{
name: schema,
qualifiedName: fmt.Sprintf(`"%v"."%v"`, db, schema),
grantType: schemaType,
}
}
// StageGrant returns a pointer to a CurrentGrantBuilder for a stage.
func StageGrant(db, schema, stage string) GrantBuilder {
return &CurrentGrantBuilder{
name: stage,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, stage),
grantType: stageType,
}
}
// ViewGrant returns a pointer to a CurrentGrantBuilder for a view.
func ViewGrant(db, schema, view string) GrantBuilder {
return &CurrentGrantBuilder{
name: view,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, view),
grantType: viewType,
}
}
// MaterializedViewGrant returns a pointer to a CurrentGrantBuilder for a materialized view.
func MaterializedViewGrant(db, schema, view string) GrantBuilder {
return &CurrentMaterializedViewGrantBuilder{
name: view,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, view),
grantType: materializedViewType,
}
}
// TableGrant returns a pointer to a CurrentGrantBuilder for a table.
func TableGrant(db, schema, table string) GrantBuilder {
return &CurrentGrantBuilder{
name: table,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, table),
grantType: tableType,
}
}
// ResourceMonitorGrant returns a pointer to a CurrentGrantBuilder for a resource monitor.
func ResourceMonitorGrant(w string) GrantBuilder {
return &CurrentGrantBuilder{
name: w,
qualifiedName: fmt.Sprintf(`"%v"`, w),
grantType: resourceMonitorType,
}
}
// IntegrationGrant returns a pointer to a CurrentGrantBuilder for an integration.
func IntegrationGrant(w string) GrantBuilder {
return &CurrentGrantBuilder{
name: w,
qualifiedName: fmt.Sprintf(`"%v"`, w),
grantType: integrationType,
}
}
// WarehouseGrant returns a pointer to a CurrentGrantBuilder for a warehouse.
func WarehouseGrant(w string) GrantBuilder {
return &CurrentGrantBuilder{
name: w,
qualifiedName: fmt.Sprintf(`"%v"`, w),
grantType: warehouseType,
}
}
// UserGrant returns a pointer to a CurrentGrantBuilder for a user.
func UserGrant(w string) GrantBuilder {
return &CurrentGrantBuilder{
name: w,
qualifiedName: fmt.Sprintf(`"%v"`, w),
grantType: userGrantType,
}
}
// ExternalTableGrant returns a pointer to a CurrentGrantBuilder for an external table.
func ExternalTableGrant(db, schema, externalTable string) GrantBuilder {
return &CurrentGrantBuilder{
name: externalTable,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, externalTable),
grantType: externalTableType,
}
}
// FailoverGroupGrant returns a pointer to a CurrentGrantBuilder for a failover group.
func FailoverGroupGrant(failoverGroup string) GrantBuilder {
return &CurrentGrantBuilder{
name: failoverGroup,
qualifiedName: fmt.Sprintf(`"%v"`, failoverGroup),
grantType: failoverGroupType,
}
}
// FileFormatGrant returns a pointer to a CurrentGrantBuilder for a file format.
func FileFormatGrant(db, schema, fileFormat string) GrantBuilder {
return &CurrentGrantBuilder{
name: fileFormat,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, fileFormat),
grantType: fileFormatType,
}
}
// FunctionGrant returns a pointer to a CurrentGrantBuilder for a view.
func FunctionGrant(db, schema, function string, argumentTypes []string) GrantBuilder {
return &CurrentGrantBuilder{
name: function,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"(%v)`, db, schema, function, strings.Join(argumentTypes, ", ")),
grantType: functionType,
}
}
// ProcedureGrant returns a pointer to a CurrentGrantBuilder for a procedure.
func ProcedureGrant(db, schema, procedure string, argumentTypes []string) GrantBuilder {
return &CurrentGrantBuilder{
name: procedure,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"(%v)`, db, schema, procedure, strings.Join(argumentTypes, ", ")),
grantType: procedureType,
}
}
// SequenceGrant returns a pointer to a CurrentGrantBuilder for a sequence.
func SequenceGrant(db, schema, sequence string) GrantBuilder {
return &CurrentGrantBuilder{
name: sequence,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, sequence),
grantType: sequenceType,
}
}
// StreamGrant returns a pointer to a CurrentGrantBuilder for a stream.
func StreamGrant(db, schema, stream string) GrantBuilder {
return &CurrentGrantBuilder{
name: stream,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, stream),
grantType: streamType,
}
}
// MaskingPolicyGrant returns a pointer to a CurrentGrantBuilder for a masking policy.
func MaskingPolicyGrant(db, schema, maskingPolicy string) GrantBuilder {
return &CurrentGrantBuilder{
name: maskingPolicy,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, maskingPolicy),
grantType: maskingPolicyType,
}
}
// PipeGrant returns a pointer to a CurrentGrantBuilder for a pipe.
func PipeGrant(db, schema, pipe string) GrantBuilder {
return &CurrentGrantBuilder{
name: pipe,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, pipe),
grantType: pipeType,
}
}
// TaskGrant returns a pointer to a CurrentGrantBuilder for a task.
func TaskGrant(db, schema, task string) GrantBuilder {
return &CurrentGrantBuilder{
name: task,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, task),
grantType: taskType,
}
}
// RowAccessPolicyGrant returns a pointer to a CurrentGrantBuilder for a masking policy.
func RowAccessPolicyGrant(db, schema, rowAccessPolicy string) GrantBuilder {
return &CurrentGrantBuilder{
name: rowAccessPolicy,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, rowAccessPolicy),
grantType: rowAccessPolicyType,
}
}
// TagGrant returns a pointer to a CurrentGrantBuilder for a tag grant.
func TagGrant(db, schema, tag string) GrantBuilder {
return &CurrentGrantBuilder{
name: tag,
qualifiedName: fmt.Sprintf(`"%v"."%v"."%v"`, db, schema, tag),
grantType: tagType,
}
}
type granteeType string
const (
roleType granteeType = "ROLE"
shareType granteeType = "SHARE"
userType granteeType = "USER" // user is only supported for RoleGrants.
)
// CurrentGrantExecutable abstracts the creation of SQL queries to build grants for
// different resources.
type CurrentGrantExecutable struct {
grantName string
grantType grantType
granteeName string
granteeType granteeType
}
// Role returns a pointer to a CurrentGrantExecutable for a role.
func (gb *CurrentGrantBuilder) Role(n string) GrantExecutable {
return &CurrentGrantExecutable{
grantName: gb.qualifiedName,
grantType: gb.grantType,
granteeName: n,
granteeType: roleType,
}
}
// Share returns a pointer to a CurrentGrantExecutable for a share.
func (gb *CurrentGrantBuilder) Share(n string) GrantExecutable {
return &CurrentGrantExecutable{
grantName: gb.qualifiedName,
grantType: gb.grantType,
granteeName: n,
granteeType: shareType,
}
}
// Grant returns the SQL that will grant privileges on the grant to the grantee.
func (ge *CurrentGrantExecutable) Grant(p string, w bool) string {
var template string
if p == `OWNERSHIP` { //nolint:gocritic // todo: please fix this
template = `GRANT %v ON %v %v TO %v "%v" COPY CURRENT GRANTS`
} else if w {
template = `GRANT %v ON %v %v TO %v "%v" WITH GRANT OPTION`
} else {
template = `GRANT %v ON %v %v TO %v "%v"`
}
return fmt.Sprintf(template,
p, ge.grantType, ge.grantName, ge.granteeType, ge.granteeName)
}
// Revoke returns the SQL that will revoke privileges on the grant from the grantee.
func (ge *CurrentGrantExecutable) Revoke(p string) []string {
return []string{
fmt.Sprintf(`REVOKE %v ON %v %v FROM %v "%v"`,
p, ge.grantType, ge.grantName, ge.granteeType, ge.granteeName),
}
}
// Revoke returns the SQL that will revoke ownership privileges on the grant from the grantee.
// Since 10/2020 Snowflake dropped support for REVOKE OWNERSHIP.
// It's only possible to transfer it to another role now, so we grant it to role r.
func (ge *CurrentGrantExecutable) RevokeOwnership(r string) []string {
// early return in the event reversion owner is left blank (default)
// default to Terraform's role in this case
if r == "" {
return []string{
"SET currentRole=CURRENT_ROLE()",
fmt.Sprintf(`GRANT OWNERSHIP ON %v %v TO ROLE IDENTIFIER($currentRole) COPY CURRENT GRANTS`, ge.grantType, ge.grantName),
}
}
return []string{
fmt.Sprintf(`GRANT OWNERSHIP ON %v %v TO ROLE "%v" COPY CURRENT GRANTS`, ge.grantType, ge.grantName, r),
}
}
// Show returns the SQL that will show all grants of the grantee.
func (ge *CurrentGrantExecutable) Show() string {
return fmt.Sprintf(`SHOW GRANTS OF %v "%v"`, ge.granteeType, ge.granteeName)
}
type GrantDetail struct {
CreatedOn sql.NullString `db:"created_on"`
Privilege sql.NullString `db:"privilege"`
GrantedOn sql.NullString `db:"granted_on"`
Name sql.NullString `db:"name"`
GrantedTo sql.NullString `db:"granted_to"`
GranteeName sql.NullString `db:"grantee_name"`
GrantOption sql.NullString `db:"grant_option"`
GrantedBy sql.NullString `db:"granted_by"`
}
func queryGrants(db *sql.DB, stmt string) ([]GrantDetail, error) {
rows, err := Query(db, stmt)
if err != nil {
return nil, err
}
defer rows.Close()
grantDetails := []GrantDetail{}
err = sqlx.StructScan(rows, &grantDetails)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
log.Println("[DEBUG] no grants found")
return nil, fmt.Errorf("unable to scan rows for %s, err = %w", stmt, err)
}
return grantDetails, err
}
return grantDetails, nil
}
func ShowGrantsOn(db *sql.DB, objectType, objectName string) ([]GrantDetail, error) {
stmt := fmt.Sprintf(`SHOW GRANTS ON %v %v`, objectType, objectName)
return queryGrants(db, stmt)
}
func ShowGrantsOnAccount(db *sql.DB) ([]GrantDetail, error) {
stmt := `SHOW GRANTS ON ACCOUNT`
return queryGrants(db, stmt)
}
func ShowGrantsTo(db *sql.DB, objectType, objectName string) ([]GrantDetail, error) {
stmt := fmt.Sprintf(`SHOW GRANTS TO %v "%v"`, objectType, objectName)
return queryGrants(db, stmt)
}
func ShowGrantsOf(db *sql.DB, objectType, objectName string) ([]GrantDetail, error) {
stmt := fmt.Sprintf(`SHOW GRANTS OF %v %v`, objectType, objectName)
return queryGrants(db, stmt)
}
func ShowFutureGrantsIn(db *sql.DB, objectType, objectName string) ([]GrantDetail, error) {
stmt := fmt.Sprintf(`SHOW FUTURE GRANTS IN %v %v`, objectType, objectName)
return queryGrants(db, stmt)
}
func ShowFutureGrantsTo(db *sql.DB, objectType, objectName string) ([]GrantDetail, error) {
stmt := fmt.Sprintf(`SHOW FUTURE GRANTS TO %v %v`, objectType, objectName)
return queryGrants(db, stmt)
}