forked from petoju/terraform-provider-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_grant.go
1119 lines (948 loc) · 31.4 KB
/
resource_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
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mysql
import (
"context"
"database/sql"
"fmt"
"log"
"reflect"
"regexp"
"sort"
"strings"
"unicode"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type ObjectT string
var (
kProcedure ObjectT = "PROCEDURE"
kFunction ObjectT = "FUNCTION"
kTable ObjectT = "TABLE"
)
var grantCreateMutex = NewKeyedMutex()
type MySQLGrant interface {
GetId() string
SQLGrantStatement() string
SQLRevokeStatement() string
GetUserOrRole() UserOrRole
GrantOption() bool
}
type MySQLGrantWithDatabase interface {
MySQLGrant
GetDatabase() string
}
type MySQLGrantWithTable interface {
MySQLGrantWithDatabase
GetTable() string
}
type MySQLGrantWithPrivileges interface {
MySQLGrant
GetPrivileges() []string
AppendPrivileges([]string)
}
type MySQLGrantWithRoles interface {
MySQLGrant
GetRoles() []string
AppendRoles([]string)
}
func grantsConflict(grantA MySQLGrant, grantB MySQLGrant) bool {
if reflect.TypeOf(grantA) != reflect.TypeOf(grantB) {
return false
}
grantAWithDatabase, aOk := grantA.(MySQLGrantWithDatabase)
grantBWithDatabase, bOk := grantB.(MySQLGrantWithDatabase)
if aOk != bOk {
return false
}
if aOk && bOk {
if grantAWithDatabase.GetDatabase() != grantBWithDatabase.GetDatabase() {
return false
}
}
grantAWithTable, aOk := grantA.(MySQLGrantWithTable)
grantBWithTable, bOk := grantB.(MySQLGrantWithTable)
if aOk != bOk {
return false
}
if aOk && bOk {
if grantAWithTable.GetTable() != grantBWithTable.GetTable() {
return false
}
}
return true
}
type PrivilegesPartiallyRevocable interface {
SQLPartialRevokePrivilegesStatement(privilegesToRevoke []string) string
}
type UserOrRole struct {
Name string
Host string
}
func (u UserOrRole) IDString() string {
if u.Host == "" {
return u.Name
}
return fmt.Sprintf("%s@%s", u.Name, u.Host)
}
func (u UserOrRole) SQLString() string {
if u.Host == "" {
return fmt.Sprintf("'%s'", u.Name)
}
return fmt.Sprintf("'%s'@'%s'", u.Name, u.Host)
}
func (u UserOrRole) Equals(other UserOrRole) bool {
if u.Name != other.Name {
return false
}
if (u.Host == "" || u.Host == "%") && (other.Host == "" || other.Host == "%") {
return true
}
return u.Host == other.Host
}
type TablePrivilegeGrant struct {
Database string
Table string
Privileges []string
Grant bool
UserOrRole UserOrRole
TLSOption string
}
func (t *TablePrivilegeGrant) GetId() string {
return fmt.Sprintf("%s:%s:%s", t.UserOrRole.IDString(), t.GetDatabase(), t.GetTable())
}
func (t *TablePrivilegeGrant) GetUserOrRole() UserOrRole {
return t.UserOrRole
}
func (t *TablePrivilegeGrant) GrantOption() bool {
return t.Grant
}
func (t *TablePrivilegeGrant) GetDatabase() string {
if t.Database == "*" {
return "*"
} else {
return fmt.Sprintf("`%s`", t.Database)
}
}
func (t *TablePrivilegeGrant) GetTable() string {
if t.Table == "*" || t.Table == "" {
return "*"
} else {
return fmt.Sprintf("`%s`", t.Table)
}
}
func (t *TablePrivilegeGrant) GetPrivileges() []string {
return t.Privileges
}
func (t *TablePrivilegeGrant) AppendPrivileges(privs []string) {
t.Privileges = append(t.Privileges, privs...)
}
func (t *TablePrivilegeGrant) SQLGrantStatement() string {
stmtSql := fmt.Sprintf("GRANT %s ON %s.%s TO %s", strings.Join(t.Privileges, ", "), t.GetDatabase(), t.GetTable(), t.UserOrRole.SQLString())
if t.TLSOption != "" && strings.ToLower(t.TLSOption) != "none" {
stmtSql += fmt.Sprintf(" REQUIRE %s", t.TLSOption)
}
if t.Grant {
stmtSql += " WITH GRANT OPTION"
}
return stmtSql
}
// containsAllPrivilege returns true if the privileges list contains an ALL PRIVILEGES grant
// this is used because there is special case behavior for ALL PRIVILEGES grants. In particular,
// if a user has ALL PRIVILEGES, we _cannot_ revoke ALL PRIVILEGES, GRANT OPTION because this is
// invalid syntax.
// See: https://github.com/petoju/terraform-provider-mysql/issues/120
func containsAllPrivilege(privileges []string) bool {
for _, p := range privileges {
if kReAllPrivileges.MatchString(p) {
return true
}
}
return false
}
func (t *TablePrivilegeGrant) SQLRevokeStatement() string {
privs := t.Privileges
if t.Grant && !containsAllPrivilege(privs) {
privs = append(privs, "GRANT OPTION")
}
return fmt.Sprintf("REVOKE %s ON %s.%s FROM %s", strings.Join(privs, ", "), t.GetDatabase(), t.GetTable(), t.UserOrRole.SQLString())
}
func (t *TablePrivilegeGrant) SQLPartialRevokePrivilegesStatement(privilegesToRevoke []string) string {
if t.Grant && !containsAllPrivilege(privilegesToRevoke) {
privilegesToRevoke = append(privilegesToRevoke, "GRANT OPTION")
}
return fmt.Sprintf("REVOKE %s ON %s.%s FROM %s", strings.Join(privilegesToRevoke, ", "), t.GetDatabase(), t.GetTable(), t.UserOrRole.SQLString())
}
type ProcedurePrivilegeGrant struct {
Database string
ObjectT ObjectT
CallableName string
Privileges []string
Grant bool
UserOrRole UserOrRole
TLSOption string
}
func (t *ProcedurePrivilegeGrant) GetId() string {
return fmt.Sprintf("%s:%s:%s", t.UserOrRole.IDString(), t.GetDatabase(), t.GetCallableName())
}
func (t *ProcedurePrivilegeGrant) GetUserOrRole() UserOrRole {
return t.UserOrRole
}
func (t *ProcedurePrivilegeGrant) GrantOption() bool {
return t.Grant
}
func (t *ProcedurePrivilegeGrant) GetDatabase() string {
if strings.Compare(t.Database, "*") != 0 && !strings.HasSuffix(t.Database, "`") {
return fmt.Sprintf("`%s`", t.Database)
}
return t.Database
}
func (t *ProcedurePrivilegeGrant) GetCallableName() string {
return fmt.Sprintf("`%s`", t.CallableName)
}
func (t *ProcedurePrivilegeGrant) GetPrivileges() []string {
return t.Privileges
}
func (t *ProcedurePrivilegeGrant) AppendPrivileges(privs []string) {
t.Privileges = append(t.Privileges, privs...)
}
func (t *ProcedurePrivilegeGrant) SQLGrantStatement() string {
stmtSql := fmt.Sprintf("GRANT %s ON %s %s.%s TO %s", strings.Join(t.Privileges, ", "), t.ObjectT, t.GetDatabase(), t.GetCallableName(), t.UserOrRole.SQLString())
if t.TLSOption != "" && strings.ToLower(t.TLSOption) != "none" {
stmtSql += fmt.Sprintf(" REQUIRE %s", t.TLSOption)
}
if t.Grant {
stmtSql += " WITH GRANT OPTION"
}
return stmtSql
}
func (t *ProcedurePrivilegeGrant) SQLRevokeStatement() string {
privs := t.Privileges
if t.Grant && !containsAllPrivilege(privs) {
privs = append(privs, "GRANT OPTION")
}
stmt := fmt.Sprintf("REVOKE %s ON %s %s.%s FROM %s", strings.Join(privs, ", "), t.ObjectT, t.GetDatabase(), t.GetCallableName(), t.UserOrRole.SQLString())
return stmt
}
func (t *ProcedurePrivilegeGrant) SQLPartialRevokePrivilegesStatement(privilegesToRevoke []string) string {
privs := privilegesToRevoke
if t.Grant && !containsAllPrivilege(privilegesToRevoke) {
privs = append(privs, "GRANT OPTION")
}
return fmt.Sprintf("REVOKE %s ON %s %s.%s FROM %s", strings.Join(privs, ", "), t.ObjectT, t.GetDatabase(), t.GetCallableName(), t.UserOrRole.SQLString())
}
type RoleGrant struct {
Roles []string
Grant bool
UserOrRole UserOrRole
TLSOption string
}
func (t *RoleGrant) GetId() string {
return fmt.Sprintf("%s", t.UserOrRole.IDString())
}
func (t *RoleGrant) GetUserOrRole() UserOrRole {
return t.UserOrRole
}
func (t *RoleGrant) GrantOption() bool {
return t.Grant
}
func (t *RoleGrant) SQLGrantStatement() string {
stmtSql := fmt.Sprintf("GRANT '%s' TO %s", strings.Join(t.Roles, "', '"), t.UserOrRole.SQLString())
if t.TLSOption != "" && strings.ToLower(t.TLSOption) != "none" {
stmtSql += fmt.Sprintf(" REQUIRE %s", t.TLSOption)
}
if t.Grant {
stmtSql += " WITH ADMIN OPTION"
}
return stmtSql
}
func (t *RoleGrant) SQLRevokeStatement() string {
return fmt.Sprintf("REVOKE '%s' FROM %s", strings.Join(t.Roles, "', '"), t.UserOrRole.SQLString())
}
func (t *RoleGrant) GetRoles() []string {
return t.Roles
}
func (t *RoleGrant) AppendRoles(roles []string) {
t.Roles = append(t.Roles, roles...)
}
func resourceGrant() *schema.Resource {
return &schema.Resource{
CreateContext: CreateGrant,
UpdateContext: UpdateGrant,
ReadContext: ReadGrant,
DeleteContext: DeleteGrant,
Importer: &schema.ResourceImporter{
StateContext: ImportGrant,
},
Schema: map[string]*schema.Schema{
"user": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"role"},
},
"role": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"user", "host"},
},
"host": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "localhost",
ConflictsWith: []string{"role"},
},
"database": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"table": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "*",
},
"privileges": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"roles": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"privileges"},
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"grant": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},
"tls_option": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Deprecated: "Please use tls_option in mysql_user.",
Default: "NONE",
},
},
}
}
func supportsRoles(ctx context.Context, meta interface{}) (bool, error) {
currentVersion := getVersionFromMeta(ctx, meta)
requiredVersion, _ := version.NewVersion("8.0.0")
hasRoles := currentVersion.GreaterThan(requiredVersion)
return hasRoles, nil
}
var kReProcedureWithoutDatabase = regexp.MustCompile(`(?i)^(function|procedure) ([^.]*)$`)
var kReProcedureWithDatabase = regexp.MustCompile(`(?i)^(function|procedure) ([^.]*)\.([^.]*)$`)
func parseResourceFromData(d *schema.ResourceData) (MySQLGrant, diag.Diagnostics) {
// Step 1: Parse the user/role
var userOrRole UserOrRole
userAttr, userOk := d.GetOk("user")
hostAttr, hostOk := d.GetOk("host")
roleAttr, roleOk := d.GetOk("role")
if (userOk && userAttr.(string) == "") && (roleOk && roleAttr == "") {
return nil, diag.Errorf("User or role name must be specified")
}
if userOk && hostOk && userAttr.(string) != "" && hostAttr.(string) != "" {
userOrRole = UserOrRole{
Name: userAttr.(string),
Host: hostAttr.(string),
}
} else if roleOk && roleAttr.(string) != "" {
userOrRole = UserOrRole{
Name: roleAttr.(string),
}
} else {
return nil, diag.Errorf("One of user/host or role is required")
}
// Step 2: Get generic attributes
database := d.Get("database").(string)
tlsOption := d.Get("tls_option").(string)
grantOption := d.Get("grant").(bool)
// Step 3a: If `roles` is specified, we have a role grant
if attr, ok := d.GetOk("roles"); ok {
roles := setToArray(attr)
return &RoleGrant{
Roles: roles,
Grant: grantOption,
UserOrRole: userOrRole,
TLSOption: tlsOption,
}, nil
}
// Step 3b. If the database is a procedure or function, we have a procedure grant
if kReProcedureWithDatabase.MatchString(database) || kReProcedureWithoutDatabase.MatchString(database) {
var callableType ObjectT
var callableName string
if kReProcedureWithDatabase.MatchString(database) {
matches := kReProcedureWithDatabase.FindStringSubmatch(database)
callableType = ObjectT(matches[1])
database = matches[2]
callableName = matches[3]
} else {
matches := kReProcedureWithoutDatabase.FindStringSubmatch(database)
callableType = ObjectT(matches[1])
database = matches[2]
callableName = d.Get("table").(string)
}
privsList := setToArray(d.Get("privileges"))
privileges := normalizePerms(privsList)
return &ProcedurePrivilegeGrant{
Database: database,
ObjectT: callableType,
CallableName: callableName,
Privileges: privileges,
Grant: grantOption,
UserOrRole: userOrRole,
TLSOption: tlsOption,
}, nil
}
// Step 3c. Otherwise, we have a table grant
privsList := setToArray(d.Get("privileges"))
privileges := normalizePerms(privsList)
return &TablePrivilegeGrant{
Database: database,
Table: d.Get("table").(string),
Privileges: privileges,
Grant: grantOption,
UserOrRole: userOrRole,
TLSOption: tlsOption,
}, nil
}
func CreateGrant(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
db, err := getDatabaseFromMeta(ctx, meta)
if err != nil {
return diag.FromErr(err)
}
// Parse the ResourceData
grant, diagErr := parseResourceFromData(d)
if err != nil {
return diagErr
}
// Determine whether the database has support for roles
hasRolesSupport, err := supportsRoles(ctx, meta)
if err != nil {
return diag.Errorf("failed getting role support: %v", err)
}
if _, ok := grant.(*RoleGrant); ok && !hasRolesSupport {
return diag.Errorf("role grants are not supported by this version of MySQL")
}
// Acquire a lock for the user
// This is necessary so that the conflicting grant check is correct with respect to other grants being created
grantCreateMutex.Lock(grant.GetUserOrRole().IDString())
defer grantCreateMutex.Unlock(grant.GetUserOrRole().IDString())
// Check to see if there are existing roles that might be clobbered by this grant
conflictingGrant, err := getMatchingGrant(ctx, db, grant)
if err != nil {
return diag.Errorf("failed showing grants: %v", err)
}
if conflictingGrant != nil {
return diag.Errorf("user/role %#v already has grant %v - ", grant.GetUserOrRole(), conflictingGrant)
}
stmtSQL := grant.SQLGrantStatement()
log.Println("[DEBUG] Executing statement:", stmtSQL)
_, err = db.ExecContext(ctx, stmtSQL)
if err != nil {
return diag.Errorf("Error running SQL (%v): %v", stmtSQL, err)
}
d.SetId(grant.GetId())
return ReadGrant(ctx, d, meta)
}
func ReadGrant(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
db, err := getDatabaseFromMeta(ctx, meta)
if err != nil {
return diag.Errorf("failed getting database from Meta: %v", err)
}
grantFromTf, diagErr := parseResourceFromData(d)
if diagErr != nil {
return diagErr
}
grantFromDb, err := getMatchingGrant(ctx, db, grantFromTf)
if err != nil {
return diag.Errorf("ReadGrant - getting all grants failed: %v", err)
}
if grantFromDb == nil {
log.Printf("[WARN] GRANT not found for %#v - removing from state", grantFromTf.GetUserOrRole())
d.SetId("")
return nil
}
setDataFromGrant(grantFromDb, d)
return nil
}
func UpdateGrant(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
db, err := getDatabaseFromMeta(ctx, meta)
if err != nil {
return diag.FromErr(err)
}
if err != nil {
return diag.Errorf("failed getting user or role: %v", err)
}
if d.HasChange("privileges") {
grant, diagErr := parseResourceFromData(d)
if diagErr != nil {
return diagErr
}
err = updatePrivileges(ctx, db, d, grant)
if err != nil {
return diag.Errorf("failed updating privileges: %v", err)
}
}
return nil
}
func updatePrivileges(ctx context.Context, db *sql.DB, d *schema.ResourceData, grant MySQLGrant) error {
oldPrivsIf, newPrivsIf := d.GetChange("privileges")
oldPrivs := oldPrivsIf.(*schema.Set)
newPrivs := newPrivsIf.(*schema.Set)
grantIfs := newPrivs.Difference(oldPrivs).List()
revokeIfs := oldPrivs.Difference(newPrivs).List()
// Normalize the privileges to revoke
privsToRevoke := []string{}
for _, revokeIf := range revokeIfs {
privsToRevoke = append(privsToRevoke, revokeIf.(string))
}
privsToRevoke = normalizePerms(privsToRevoke)
// Do a partial revoke of anything that has been removed
if len(privsToRevoke) > 0 {
partialRevoker, ok := grant.(PrivilegesPartiallyRevocable)
if !ok {
return fmt.Errorf("grant does not support partial privilege revokes")
}
sqlCommand := partialRevoker.SQLPartialRevokePrivilegesStatement(privsToRevoke)
log.Printf("[DEBUG] SQL for partial revoke: %s", sqlCommand)
if _, err := db.ExecContext(ctx, sqlCommand); err != nil {
return err
}
}
// Do a full grant if anything has been added
if len(grantIfs) > 0 {
sqlCommand := grant.SQLGrantStatement()
log.Printf("[DEBUG] SQL to re-grant privileges: %s", sqlCommand)
if _, err := db.ExecContext(ctx, sqlCommand); err != nil {
return err
}
}
return nil
}
func DeleteGrant(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
db, err := getDatabaseFromMeta(ctx, meta)
if err != nil {
return diag.FromErr(err)
}
// Parse the grant from ResourceData
grant, diagErr := parseResourceFromData(d)
if err != nil {
return diagErr
}
// Acquire a lock for the user
grantCreateMutex.Lock(grant.GetUserOrRole().IDString())
defer grantCreateMutex.Unlock(grant.GetUserOrRole().IDString())
sqlStatement := grant.SQLRevokeStatement()
log.Printf("[DEBUG] SQL to delete grant: %s", sqlStatement)
_, err = db.ExecContext(ctx, sqlStatement)
if err != nil {
if !isNonExistingGrant(err) {
return diag.Errorf("error revoking %s: %s", sqlStatement, err)
}
}
return nil
}
func isNonExistingGrant(err error) bool {
errorNumber := mysqlErrorNumber(err)
// 1141 = ER_NONEXISTING_GRANT
// 1147 = ER_NONEXISTING_TABLE_GRANT
// 1403 = ER_NONEXISTING_PROC_GRANT
return errorNumber == 1141 || errorNumber == 1147 || errorNumber == 1403
}
func ImportGrant(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
userHostDatabaseTable := strings.Split(d.Id(), "@")
if len(userHostDatabaseTable) != 4 && len(userHostDatabaseTable) != 5 {
return nil, fmt.Errorf("wrong ID format %s - expected user@host@database@table (and optionally ending @ to signify grant option) where some parts can be empty)", d.Id())
}
user := userHostDatabaseTable[0]
host := userHostDatabaseTable[1]
database := userHostDatabaseTable[2]
table := userHostDatabaseTable[3]
grantOption := len(userHostDatabaseTable) == 5
userOrRole := UserOrRole{
Name: user,
Host: host,
}
desiredGrant := &TablePrivilegeGrant{
Database: database,
Table: table,
Grant: grantOption,
UserOrRole: userOrRole,
}
db, err := getDatabaseFromMeta(ctx, meta)
if err != nil {
return nil, fmt.Errorf("got error while getting database from meta: %w", err)
}
grants, err := showUserGrants(ctx, db, userOrRole)
if err != nil {
return nil, fmt.Errorf("failed to showUserGrants in import: %w", err)
}
for _, foundGrant := range grants {
if grantsConflict(desiredGrant, foundGrant) {
res := resourceGrant().Data(nil)
setDataFromGrant(foundGrant, res)
return []*schema.ResourceData{res}, nil
}
}
return nil, fmt.Errorf("failed to find the grant to import: %v -- found %#v", userHostDatabaseTable, grants)
}
// setDataFromGrant copies the values from MySQLGrant to the schema.ResourceData
// This function is used when importing a new Grant, or when syncing remote state to Terraform state
// It is responsible for pulling any non-identifying properties (e.g. grant, tls_option) into the Terraform state
// Identifying properties (database, table) are already set either as part of the import id or required properties
// of the Terraform resource.
func setDataFromGrant(grant MySQLGrant, d *schema.ResourceData) *schema.ResourceData {
if tableGrant, ok := grant.(*TablePrivilegeGrant); ok {
d.Set("grant", grant.GrantOption())
d.Set("tls_option", tableGrant.TLSOption)
} else if procedureGrant, ok := grant.(*ProcedurePrivilegeGrant); ok {
d.Set("grant", grant.GrantOption())
d.Set("tls_option", procedureGrant.TLSOption)
} else if roleGrant, ok := grant.(*RoleGrant); ok {
d.Set("grant", grant.GrantOption())
d.Set("roles", roleGrant.Roles)
d.Set("tls_option", roleGrant.TLSOption)
} else {
panic("Unknown grant type")
}
// Only set privileges if there is a delta in the normalized privileges
if grantWithPriv, hasPriv := grant.(MySQLGrantWithPrivileges); hasPriv {
currentPriv, ok := d.GetOk("privileges")
if !ok {
d.Set("privileges", grantWithPriv.GetPrivileges())
} else {
currentPrivs := setToArray(currentPriv.(*schema.Set))
currentPrivs = normalizePerms(currentPrivs)
if !reflect.DeepEqual(currentPrivs, grantWithPriv.GetPrivileges()) {
d.Set("privileges", grantWithPriv.GetPrivileges())
}
}
}
// We need to use the raw pointer to access Table / Database without wrapping them with backticks.
if tablePrivGrant, isTablePriv := grant.(*TablePrivilegeGrant); isTablePriv {
d.Set("table", tablePrivGrant.Table)
d.Set("database", tablePrivGrant.Database)
}
// This is a bit of a hack, since we don't have a way to distingush between users and roles
// from the grant itself. We can only infer it from the schema.
userOrRole := grant.GetUserOrRole()
if d.Get("role") != "" {
d.Set("role", userOrRole.Name)
} else {
d.Set("user", userOrRole.Name)
d.Set("host", userOrRole.Host)
}
// This needs to happen for import to work.
d.SetId(grant.GetId())
return d
}
func combineGrants(grantA MySQLGrant, grantB MySQLGrant) (MySQLGrant, error) {
// Check if the grants cover the same user, table, database
// If not, throw an error because they are unmergeable
if !grantsConflict(grantA, grantB) {
return nil, fmt.Errorf("unable to combine MySQLGrant %s with %s because they don't cover the same table/database/user", grantA, grantB)
}
// We can combine grants with privileges
grantAWithPrivileges, aOk := grantA.(MySQLGrantWithPrivileges)
grantBWithPrivileges, bOk := grantB.(MySQLGrantWithPrivileges)
if aOk && bOk {
grantAWithPrivileges.AppendPrivileges(grantBWithPrivileges.GetPrivileges())
return grantA, nil
}
// We can combine grants with roles
grantAWithRoles, aOk := grantA.(MySQLGrantWithRoles)
grantBWithRoles, bOk := grantB.(MySQLGrantWithRoles)
if aOk && bOk {
grantAWithRoles.AppendRoles(grantBWithRoles.GetRoles())
return grantA, nil
}
return nil, fmt.Errorf("unable to combine MySQLGrant %s of type %T with %s of type %T", grantA, grantA, grantB, grantB)
}
func getMatchingGrant(ctx context.Context, db *sql.DB, desiredGrant MySQLGrant) (MySQLGrant, error) {
allGrants, err := showUserGrants(ctx, db, desiredGrant.GetUserOrRole())
var result MySQLGrant
if err != nil {
return nil, fmt.Errorf("showGrant - getting all grants failed: %w", err)
}
for _, dbGrant := range allGrants {
// Check if the grants cover the same user, table, database
// If not, continue
if !grantsConflict(desiredGrant, dbGrant) {
log.Printf("[DEBUG] Skipping grant %#v as it doesn't match %#v", dbGrant, desiredGrant)
continue
}
// For some reason, MySQL separates privileges into multiple lines
// So to normalize them, we need to combine them into a single MySQLGrant
if result != nil {
result, err = combineGrants(result, dbGrant)
if err != nil {
return nil, fmt.Errorf("failed to combine grants in getMatchingGrant: %w", err)
}
} else {
result = dbGrant
}
}
return result, nil
}
var (
kUserOrRoleRegex = regexp.MustCompile("['`]?([^'`]+)['`]?(?:@['`]?([^'`]+)['`]?)?")
)
func parseUserOrRoleFromRow(userOrRoleStr string) (*UserOrRole, error) {
userHostMatches := kUserOrRoleRegex.FindStringSubmatch(userOrRoleStr)
if len(userHostMatches) == 3 {
return &UserOrRole{
Name: userHostMatches[1],
Host: userHostMatches[2],
}, nil
} else if len(userHostMatches) == 2 {
return &UserOrRole{
Name: userHostMatches[1],
Host: "%",
}, nil
} else {
return nil, fmt.Errorf("failed to parse user or role portion of grant statement: %s", userOrRoleStr)
}
}
var (
kDatabaseAndObjectRegex = regexp.MustCompile("['`]?([^'`]+)['`]?\\.['`]?([^'`]+)['`]?")
)
func parseDatabaseQualifiedObject(objectRef string) (string, string, error) {
if matches := kDatabaseAndObjectRegex.FindStringSubmatch(objectRef); len(matches) == 3 {
return matches[1], matches[2], nil
}
return "", "", fmt.Errorf("failed to parse database and table portion of grant statement: %s", objectRef)
}
var (
kRequireRegex = regexp.MustCompile(`.*REQUIRE\s+(.*)`)
kGrantRegex = regexp.MustCompile(`\bGRANT OPTION\b|\bADMIN OPTION\b`)
procedureGrantRegex = regexp.MustCompile(`GRANT\s+(.+)\s+ON\s+(FUNCTION|PROCEDURE)\s+(.+)\s+TO\s+(.+)`)
tableGrantRegex = regexp.MustCompile(`GRANT\s+(.+)\s+ON\s+(.+)\s+TO\s+(.+)`)
roleGrantRegex = regexp.MustCompile(`GRANT\s+(.+)\s+TO\s+(.+)`)
)
func parseGrantFromRow(grantStr string) (MySQLGrant, error) {
// Ignore REVOKE.*
if strings.HasPrefix(grantStr, "REVOKE") {
log.Printf("[WARN] Partial revokes are not fully supported and lead to unexpected behavior. Consult documentation https://dev.mysql.com/doc/refman/8.0/en/partial-revokes.html on how to disable them for safe and reliable terraform. Relevant partial revoke: %s\n", grantStr)
return nil, nil
}
// Parse Require Statement
tlsOption := "NONE"
if requireMatches := kRequireRegex.FindStringSubmatch(grantStr); len(requireMatches) == 2 {
tlsOption = requireMatches[1]
}
if procedureMatches := procedureGrantRegex.FindStringSubmatch(grantStr); len(procedureMatches) == 5 {
privsStr := procedureMatches[1]
privileges := extractPermTypes(privsStr)
privileges = normalizePerms(privileges)
// After normalizePerms, we may have empty privileges. If so, skip this grant.
if len(privileges) == 0 {
return nil, nil
}
userOrRole, err := parseUserOrRoleFromRow(procedureMatches[4])
if err != nil {
return nil, fmt.Errorf("failed to parseUserOrRole for procedure grant: %w", err)
}
database, callable, err := parseDatabaseQualifiedObject(procedureMatches[3])
if err != nil {
return nil, fmt.Errorf("failed to parseDatabaseQualifiedObject for procedure grant: %w", err)
}
grant := &ProcedurePrivilegeGrant{
Database: database,
ObjectT: ObjectT(procedureMatches[2]),
CallableName: callable,
Privileges: privileges,
Grant: kGrantRegex.MatchString(grantStr),
UserOrRole: *userOrRole,
TLSOption: tlsOption,
}
log.Printf("[DEBUG] Got procedure parsed grant: %s, parsed grant is %s: %v", grantStr, reflect.TypeOf(grant), grant)
return grant, nil
} else if tableMatches := tableGrantRegex.FindStringSubmatch(grantStr); len(tableMatches) == 4 {
privsStr := tableMatches[1]
privileges := extractPermTypes(privsStr)
privileges = normalizePerms(privileges)
// After normalizePerms, we may have empty privileges. If so, skip this grant.
if len(privileges) == 0 {
return nil, nil
}
userOrRole, err := parseUserOrRoleFromRow(tableMatches[3])
if err != nil {
return nil, fmt.Errorf("failed to parseUserOrRole for table grant: %w", err)
}
database, table, err := parseDatabaseQualifiedObject(tableMatches[2])
if err != nil {
return nil, fmt.Errorf("failed to parseDatabaseQualifiedObject for table grant: %w", err)
}
grant := &TablePrivilegeGrant{
Database: database,
Table: table,
Privileges: privileges,
Grant: kGrantRegex.MatchString(grantStr),
UserOrRole: *userOrRole,
TLSOption: tlsOption,
}
log.Printf("[DEBUG] Got table parsed grant: %s, parsed grant is %s: %v", grantStr, reflect.TypeOf(grant), grant)
return grant, nil
} else if roleMatches := roleGrantRegex.FindStringSubmatch(grantStr); len(roleMatches) == 3 {
rolesStart := strings.Split(roleMatches[1], ",")
roles := make([]string, len(rolesStart))
for i, role := range rolesStart {
roles[i] = strings.Trim(role, "`@%\" ")
}
userOrRole, err := parseUserOrRoleFromRow(roleMatches[2])
if err != nil {
return nil, fmt.Errorf("failed to parseUserOrRole for role grant: %w", err)
}
grant := &RoleGrant{
Roles: roles,
Grant: kGrantRegex.MatchString(grantStr),
UserOrRole: *userOrRole,
TLSOption: tlsOption,
}
log.Printf("[DEBUG] Got: %s, parsed grant is %s: %v", grantStr, reflect.TypeOf(grant), grant)
return grant, nil
} else {
return nil, fmt.Errorf("failed to parse object portion of grant statement: %s", grantStr)
}
}
func showUserGrants(ctx context.Context, db *sql.DB, userOrRole UserOrRole) ([]MySQLGrant, error) {
grants := []MySQLGrant{}
sqlStatement := fmt.Sprintf("SHOW GRANTS FOR %s", userOrRole.SQLString())
log.Printf("[DEBUG] SQL to show grants: %s", sqlStatement)
rows, err := db.QueryContext(ctx, sqlStatement)
if isNonExistingGrant(err) {
return []MySQLGrant{}, nil
}
if err != nil {
return nil, fmt.Errorf("showUserGrants - getting grants failed: %w", err)
}
defer rows.Close()
for rows.Next() {
var rawGrant string
err := rows.Scan(&rawGrant)
if err != nil {
return nil, fmt.Errorf("showUserGrants - reading row failed: %w", err)
}
parsedGrant, err := parseGrantFromRow(rawGrant)
if err != nil {
return nil, fmt.Errorf("failed to parseGrantFromRow: %w", err)
}
if parsedGrant == nil {
continue
}
// Filter out any grants that don't match the provided user