/
allowed_stmt_linter.go
615 lines (578 loc) · 18.8 KB
/
allowed_stmt_linter.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package linters
import (
"fmt"
"github.com/pingcap/parser/ast"
"github.com/erda-project/erda/pkg/database/sqllint/linterror"
"github.com/erda-project/erda/pkg/database/sqllint/rules"
"github.com/erda-project/erda/pkg/database/sqllint/script"
)
const (
CreateDatabaseStmt uint64 = 1 << iota
AlterDatabaseStmt
DropDatabaseStmt
CreateTableStmt
DropTableStmt
DropSequenceStmt
RenameTableStmt
CreateViewStmt
CreateSequenceStmt
CreateIndexStmt
DropIndexStmt
LockTablesStmt
UnlockTablesStmt
CleanupTableLockStmt
RepairTableStmt
TruncateTableStmt
RecoverTableStmt
FlashBackTableStmt
AlterTableOption
AlterTableAddColumns
AlterTableAddConstraint
AlterTableDropColumn
AlterTableDropPrimaryKey
AlterTableDropIndex
AlterTableDropForeignKey
AlterTableModifyColumn
AlterTableChangeColumn
AlterTableRenameColumn
AlterTableRenameTable
AlterTableAlterColumn
AlterTableLock
AlterTableAlgorithm
AlterTableRenameIndex
AlterTableForce
AlterTableAddPartitions
AlterTableCoalescePartitions
AlterTableDropPartition
AlterTableTruncatePartition
AlterTablePartition
AlterTableEnableKeys
AlterTableDisableKeys
AlterTableRemovePartitioning
AlterTableWithValidation
AlterTableWithoutValidation
AlterTableSecondaryLoad
AlterTableSecondaryUnload
AlterTableRebuildPartition
AlterTableReorganizePartition
AlterTableCheckPartitions
AlterTableExchangePartition
AlterTableOptimizePartition
AlterTableRepairPartition
AlterTableImportPartitionTablespace
AlterTableDiscardPartitionTablespace
AlterTableAlterCheck
AlterTableDropCheck
AlterTableImportTablespace
AlterTableDiscardTablespace
AlterTableIndexInvisible
AlterTableOrderByColumns
AlterTableSetTiFlashReplica
)
const (
SelectStmt uint64 = 1 << iota
UnionStmt
LoadDataStmt
InsertStmt
DeleteStmt
UpdateStmt
ShowStmt
SplitRegionStmt
)
const (
AlterTableStmt = AlterTableOption | AlterTableAddColumns | AlterTableAddConstraint |
AlterTableDropColumn | AlterTableDropPrimaryKey | AlterTableDropIndex |
AlterTableDropForeignKey | AlterTableModifyColumn |
AlterTableChangeColumn | AlterTableRenameColumn | AlterTableRenameTable | AlterTableAlterColumn |
AlterTableLock | AlterTableAlgorithm | AlterTableRenameIndex |
AlterTableForce | AlterTableAddPartitions | AlterTableCoalescePartitions |
AlterTableDropPartition | AlterTableTruncatePartition | AlterTablePartition |
AlterTableEnableKeys | AlterTableDisableKeys | AlterTableRemovePartitioning |
AlterTableWithValidation | AlterTableWithoutValidation | AlterTableSecondaryLoad |
AlterTableSecondaryUnload | AlterTableRebuildPartition | AlterTableReorganizePartition |
AlterTableCheckPartitions | AlterTableExchangePartition | AlterTableOptimizePartition |
AlterTableRepairPartition | AlterTableImportPartitionTablespace | AlterTableDiscardPartitionTablespace |
AlterTableAlterCheck | AlterTableDropCheck | AlterTableImportTablespace |
AlterTableDiscardTablespace | AlterTableIndexInvisible | AlterTableOrderByColumns |
AlterTableSetTiFlashReplica
DDLStmt = CreateDatabaseStmt | AlterDatabaseStmt | DropDatabaseStmt |
CreateTableStmt | DropTableStmt | DropSequenceStmt |
RenameTableStmt | CreateViewStmt | CreateSequenceStmt |
CreateIndexStmt | DropIndexStmt | LockTablesStmt |
UnlockTablesStmt | CleanupTableLockStmt | RepairTableStmt |
TruncateTableStmt | RecoverTableStmt | FlashBackTableStmt |
AlterTableStmt
DMLStmt = SelectStmt | UnionStmt | LoadDataStmt |
InsertStmt | DeleteStmt | UpdateStmt |
ShowStmt | SplitRegionStmt
)
type AllowedStmt struct {
baseLinter
allowedDDL uint64
allowedDML uint64
}
func NewAllowedStmtLinter(allowedDDLs, allowedDMLs uint64) rules.Ruler {
return func(script script.Script) rules.Rule {
return &AllowedStmt{
baseLinter: baseLinter{
s: script,
err: nil,
text: "",
},
allowedDDL: allowedDDLs,
allowedDML: allowedDMLs,
}
}
}
func (l *AllowedStmt) Enter(in ast.Node) (ast.Node, bool) {
if l.text == "" || in.Text() != "" {
l.text = in.Text()
}
switch stmt := in.(type) {
case *ast.CreateDatabaseStmt:
if l.allowedDDL&CreateDatabaseStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed CreateDatabaseStmt", func([]byte) bool {
return true
})
}
case *ast.AlterDatabaseStmt:
if l.allowedDDL&AlterDatabaseStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterDatabaseStmt", func([]byte) bool {
return true
})
}
case *ast.DropDatabaseStmt:
if l.allowedDDL&DropDatabaseStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed DropDatabaseStmt", func([]byte) bool {
return true
})
}
case *ast.CreateTableStmt:
if l.allowedDDL&CreateTableStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed CreateTableStmt", func([]byte) bool {
return true
})
}
case *ast.DropTableStmt:
if l.allowedDDL&DropTableStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed DropTableStmt", func([]byte) bool {
return true
})
}
case *ast.DropSequenceStmt:
if l.allowedDDL&DropSequenceStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed DropSequenceStmt", func([]byte) bool {
return true
})
}
case *ast.RenameTableStmt:
if l.allowedDDL&RenameTableStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed RenameTableStmt", func([]byte) bool {
return true
})
}
case *ast.CreateViewStmt:
if l.allowedDDL&CreateViewStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed CreateViewStmt", func([]byte) bool {
return true
})
}
case *ast.CreateSequenceStmt:
if l.allowedDDL&CreateSequenceStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed CreateSequenceStmt", func([]byte) bool {
return true
})
return in, true
}
case *ast.CreateIndexStmt:
if l.allowedDDL&CreateIndexStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed CreateIndexStmt", func([]byte) bool {
return true
})
return in, true
}
case *ast.DropIndexStmt:
if l.allowedDDL&DropIndexStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed DropIndexStmt", func([]byte) bool {
return true
})
}
case *ast.LockTablesStmt:
if l.allowedDDL&LockTablesStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed LockTablesStmt", func([]byte) bool {
return true
})
}
case *ast.UnlockTablesStmt:
if l.allowedDDL&LockTablesStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed LockTablesStmt", func([]byte) bool {
return true
})
}
case *ast.CleanupTableLockStmt:
if l.allowedDDL&CleanupTableLockStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed CleanupTableLockStmt", func([]byte) bool {
return true
})
}
case *ast.RepairTableStmt:
if l.allowedDDL&RepairTableStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed RepairTableStmt", func([]byte) bool {
return true
})
}
case *ast.TruncateTableStmt:
if l.allowedDDL&TruncateTableStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed TruncateTableStmt", func([]byte) bool {
return true
})
}
case *ast.RecoverTableStmt:
if l.allowedDDL&RecoverTableStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed RecoverTableStmt", func([]byte) bool {
return true
})
}
case *ast.FlashBackTableStmt:
if l.allowedDDL&FlashBackTableStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed FlashBackTableStmt", func([]byte) bool {
return true
})
}
case *ast.AlterTableStmt:
for _, spec := range stmt.Specs {
switch spec.Tp {
case ast.AlterTableOption:
if l.allowedDDL&AlterTableOption == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableOption", func([]byte) bool {
return true
})
}
case ast.AlterTableAddColumns:
if l.allowedDDL&AlterTableAddColumns == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableAddColumns", func([]byte) bool {
return true
})
}
case ast.AlterTableAddConstraint:
if l.allowedDDL&AlterTableAddConstraint == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableAddConstraint", func([]byte) bool {
return true
})
}
case ast.AlterTableDropColumn:
if l.allowedDDL&AlterTableDropColumn == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableDropColumn", func([]byte) bool {
return true
})
}
case ast.AlterTableDropPrimaryKey:
if l.allowedDDL&AlterTableDropPrimaryKey == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableDropPrimaryKey", func([]byte) bool {
return true
})
}
case ast.AlterTableDropIndex:
if l.allowedDDL&AlterTableDropIndex == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableDropIndex", func([]byte) bool {
return true
})
}
case ast.AlterTableDropForeignKey:
if l.allowedDDL&AlterTableDropForeignKey == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableDropForeignKey", func([]byte) bool {
return true
})
}
case ast.AlterTableModifyColumn:
if l.allowedDDL&AlterTableModifyColumn == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableModifyColumn", func([]byte) bool {
return true
})
}
case ast.AlterTableChangeColumn:
if l.allowedDDL&AlterTableChangeColumn == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableChangeColumn", func([]byte) bool {
return true
})
}
case ast.AlterTableRenameColumn:
if l.allowedDDL&AlterTableRenameColumn == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableRenameColumn", func([]byte) bool {
return true
})
}
case ast.AlterTableRenameTable:
if l.allowedDDL&AlterTableRenameTable == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableRenameTable", func([]byte) bool {
return true
})
}
case ast.AlterTableAlterColumn:
if l.allowedDDL&AlterTableAlterColumn == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableAlterColumn", func([]byte) bool {
return true
})
}
case ast.AlterTableLock:
if l.allowedDDL&AlterTableLock == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableLock", func([]byte) bool {
return true
})
}
case ast.AlterTableAlgorithm:
if l.allowedDDL&AlterTableAlgorithm == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableAlgorithm", func([]byte) bool {
return true
})
}
case ast.AlterTableRenameIndex:
if l.allowedDDL&AlterTableRenameIndex == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableRenameIndex", func([]byte) bool {
return true
})
}
case ast.AlterTableForce:
if l.allowedDDL&AlterTableForce == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableForce", func([]byte) bool {
return true
})
}
case ast.AlterTableAddPartitions:
if l.allowedDDL&AlterTableAddPartitions == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableAddPartitions", func([]byte) bool {
return true
})
}
case ast.AlterTableCoalescePartitions:
if l.allowedDDL&AlterTableCoalescePartitions == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableCoalescePartitions", func([]byte) bool {
return true
})
}
case ast.AlterTableDropPartition:
if l.allowedDDL&AlterTableDropPartition == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableDropPartition", func([]byte) bool {
return true
})
}
case ast.AlterTableTruncatePartition:
if l.allowedDDL&AlterTableTruncatePartition == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableTruncatePartition", func([]byte) bool {
return true
})
}
case ast.AlterTablePartition:
if l.allowedDDL&AlterTablePartition == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTablePartition", func([]byte) bool {
return true
})
}
case ast.AlterTableEnableKeys:
if l.allowedDDL&AlterTableEnableKeys == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableEnableKeys", func([]byte) bool {
return true
})
}
case ast.AlterTableDisableKeys:
if l.allowedDDL&AlterTableDisableKeys == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableDisableKeys", func([]byte) bool {
return true
})
}
case ast.AlterTableRemovePartitioning:
if l.allowedDDL&AlterTableRemovePartitioning == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableRemovePartitioning", func([]byte) bool {
return true
})
}
case ast.AlterTableWithValidation:
if l.allowedDDL&AlterTableWithValidation == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableWithValidation", func([]byte) bool {
return true
})
}
case ast.AlterTableWithoutValidation:
if l.allowedDDL&AlterTableWithoutValidation == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableWithoutValidation", func([]byte) bool {
return true
})
}
case ast.AlterTableSecondaryLoad:
if l.allowedDDL&AlterTableSecondaryLoad == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableSecondaryLoad", func([]byte) bool {
return true
})
}
case ast.AlterTableSecondaryUnload:
if l.allowedDDL&AlterTableSecondaryUnload == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableSecondaryUnload", func([]byte) bool {
return true
})
}
case ast.AlterTableRebuildPartition:
if l.allowedDDL&AlterTableRebuildPartition == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableRebuildPartition", func([]byte) bool {
return true
})
}
case ast.AlterTableReorganizePartition:
if l.allowedDDL&AlterTableReorganizePartition == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableReorganizePartition", func([]byte) bool {
return true
})
}
case ast.AlterTableCheckPartitions:
if l.allowedDDL&AlterTableCheckPartitions == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableCheckPartitions", func([]byte) bool {
return true
})
}
case ast.AlterTableExchangePartition:
if l.allowedDDL&AlterTableExchangePartition == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableExchangePartition", func([]byte) bool {
return true
})
}
case ast.AlterTableOptimizePartition:
if l.allowedDDL&AlterTableOptimizePartition == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableOptimizePartition", func([]byte) bool {
return true
})
}
case ast.AlterTableRepairPartition:
if l.allowedDDL&AlterTableRepairPartition == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableRepairPartition", func([]byte) bool {
return true
})
}
case ast.AlterTableImportPartitionTablespace:
if l.allowedDDL&AlterTableImportPartitionTablespace == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableImportPartitionTablespace", func([]byte) bool {
return true
})
}
case ast.AlterTableDiscardPartitionTablespace:
if l.allowedDDL&AlterTableDiscardPartitionTablespace == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableDiscardPartitionTablespace", func([]byte) bool {
return true
})
}
case ast.AlterTableAlterCheck:
if l.allowedDDL&AlterTableAlterCheck == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableAlterCheck", func([]byte) bool {
return true
})
}
case ast.AlterTableDropCheck:
if l.allowedDDL&AlterTableDropCheck == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableDropCheck", func([]byte) bool {
return true
})
}
case ast.AlterTableImportTablespace:
if l.allowedDDL&AlterTableImportTablespace == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableImportTablespace", func([]byte) bool {
return true
})
}
case ast.AlterTableDiscardTablespace:
if l.allowedDDL&AlterTableDiscardTablespace == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableDiscardTablespace", func([]byte) bool {
return true
})
}
case ast.AlterTableIndexInvisible:
if l.allowedDDL&AlterTableIndexInvisible == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableIndexInvisible", func([]byte) bool {
return true
})
}
case ast.AlterTableOrderByColumns:
if l.allowedDDL&AlterTableOrderByColumns == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableOrderByColumns", func([]byte) bool {
return true
})
}
case ast.AlterTableSetTiFlashReplica:
if l.allowedDDL&AlterTableSetTiFlashReplica == 0 {
l.err = linterror.New(l.s, l.text, "not allowed AlterTableSetTiFlashReplica", func([]byte) bool {
return true
})
}
}
}
case *ast.SelectStmt:
if l.allowedDML&SelectStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed SelectStmt", func([]byte) bool {
return true
})
}
case *ast.UnionStmt:
if l.allowedDML&UnionStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed UnionStmt", func([]byte) bool {
return true
})
}
case *ast.LoadDataStmt:
if l.allowedDML&LoadDataStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed LoadDataStmt", func([]byte) bool {
return true
})
}
case *ast.InsertStmt:
if l.allowedDML&InsertStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed InsertStmt", func([]byte) bool {
return true
})
}
case *ast.DeleteStmt:
if l.allowedDML&DeleteStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed DeleteStmt", func([]byte) bool {
return true
})
}
case *ast.UpdateStmt:
if l.allowedDML&UpdateStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed UpdateStmt", func([]byte) bool {
return true
})
}
case *ast.ShowStmt:
if l.allowedDML&ShowStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed ShowStmt", func([]byte) bool {
return true
})
}
case *ast.SplitRegionStmt:
if l.allowedDML&SplitRegionStmt == 0 {
l.err = linterror.New(l.s, l.text, "not allowed SplitRegionStmt", func([]byte) bool {
return true
})
}
default:
l.err = linterror.New(l.s, l.text, fmt.Sprintf("not allowed stmt %T", in), func([]byte) bool {
return true
})
}
return in, true
}
func (l *AllowedStmt) Leave(in ast.Node) (ast.Node, bool) {
return in, true
}
func (l *AllowedStmt) Error() error {
return l.err
}