-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
permissions.go
256 lines (218 loc) · 6.54 KB
/
permissions.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package proto
import (
"fmt"
"hash/crc64"
"sort"
"github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/concurrency"
)
var (
hashTable = crc64.MakeTable(crc64.ISO)
)
type Permission interface {
PrimaryKey() string
String() string
}
type PermissionList interface {
Get(int) Permission
Len() int
}
func printPrivileges(priv map[string]string) string {
si := make([]string, 0, len(priv))
for k := range priv {
si = append(si, k)
}
sort.Strings(si)
result := ""
for _, k := range si {
result += " " + k + "(" + priv[k] + ")"
}
return result
}
// UserPermission describes a single row in the mysql.user table
// Primary key is Host+User
// PasswordChecksum is the crc64 of the password, for security reasons
type UserPermission struct {
Host string
User string
PasswordChecksum uint64
Privileges map[string]string
}
func NewUserPermission(fields []proto.Field, values []sqltypes.Value) *UserPermission {
up := &UserPermission{Privileges: make(map[string]string)}
for i, field := range fields {
switch field.Name {
case "Host":
up.Host = values[i].String()
case "User":
up.User = values[i].String()
case "Password":
up.PasswordChecksum = crc64.Checksum(([]byte)(values[i].String()), hashTable)
default:
up.Privileges[field.Name] = values[i].String()
}
}
return up
}
func (up *UserPermission) PrimaryKey() string {
return up.Host + ":" + up.User
}
func (up *UserPermission) String() string {
var passwd string
if up.PasswordChecksum == 0 {
passwd = "NoPassword"
} else {
passwd = fmt.Sprintf("PasswordChecksum(%v)", up.PasswordChecksum)
}
return "UserPermission " + passwd + printPrivileges(up.Privileges)
}
type UserPermissionList []*UserPermission
func (upl UserPermissionList) Get(i int) Permission {
return upl[i]
}
func (upl UserPermissionList) Len() int {
return len(upl)
}
// DbPermission describes a single row in the mysql.db table
// Primary key is Host+Db+User
type DbPermission struct {
Host string
Db string
User string
Privileges map[string]string
}
func NewDbPermission(fields []proto.Field, values []sqltypes.Value) *DbPermission {
up := &DbPermission{Privileges: make(map[string]string)}
for i, field := range fields {
switch field.Name {
case "Host":
up.Host = values[i].String()
case "Db":
up.Db = values[i].String()
case "User":
up.User = values[i].String()
default:
up.Privileges[field.Name] = values[i].String()
}
}
return up
}
func (dp *DbPermission) PrimaryKey() string {
return dp.Host + ":" + dp.Db + ":" + dp.User
}
func (dp *DbPermission) String() string {
return "DbPermission" + printPrivileges(dp.Privileges)
}
type DbPermissionList []*DbPermission
func (upl DbPermissionList) Get(i int) Permission {
return upl[i]
}
func (upl DbPermissionList) Len() int {
return len(upl)
}
// HostPermission describes a single row in the mysql.host table
// Primary key is Host+Db
type HostPermission struct {
Host string
Db string
Privileges map[string]string
}
func NewHostPermission(fields []proto.Field, values []sqltypes.Value) *HostPermission {
hp := &HostPermission{Privileges: make(map[string]string)}
for i, field := range fields {
switch field.Name {
case "Host":
hp.Host = values[i].String()
case "Db":
hp.Db = values[i].String()
default:
hp.Privileges[field.Name] = values[i].String()
}
}
return hp
}
func (hp *HostPermission) PrimaryKey() string {
return hp.Host + ":" + hp.Db
}
func (hp *HostPermission) String() string {
return "HostPermission" + printPrivileges(hp.Privileges)
}
type HostPermissionList []*HostPermission
func (upl HostPermissionList) Get(i int) Permission {
return upl[i]
}
func (upl HostPermissionList) Len() int {
return len(upl)
}
// Permissions have all the rows in mysql.{user,db,host} tables,
// (all rows are sorted by primary key)
type Permissions struct {
UserPermissions UserPermissionList
DbPermissions DbPermissionList
HostPermissions HostPermissionList
}
func printPermissions(name string, permissions PermissionList) string {
result := name + " Permissions:\n"
for i := 0; i < permissions.Len(); i++ {
perm := permissions.Get(i)
result += " " + perm.PrimaryKey() + ": " + perm.String() + "\n"
}
return result
}
func (permissions *Permissions) String() string {
return printPermissions("User", permissions.UserPermissions) +
printPermissions("Db", permissions.DbPermissions) +
printPermissions("Host", permissions.HostPermissions)
}
func diffPermissions(name, leftName string, left PermissionList, rightName string, right PermissionList, er concurrency.ErrorRecorder) {
leftIndex := 0
rightIndex := 0
for leftIndex < left.Len() && rightIndex < right.Len() {
l := left.Get(leftIndex)
r := right.Get(rightIndex)
// extra value on the left side
if l.PrimaryKey() < r.PrimaryKey() {
er.RecordError(fmt.Errorf("%v has an extra %v %v", leftName, name, l.PrimaryKey()))
leftIndex++
continue
}
// extra value on the right side
if l.PrimaryKey() > r.PrimaryKey() {
er.RecordError(fmt.Errorf("%v has an extra %v %v", rightName, name, r.PrimaryKey()))
rightIndex++
continue
}
// same name, let's see content
if l.String() != r.String() {
er.RecordError(fmt.Errorf("%v and %v disagree on %v %v:\n%v\n differs from:\n%v", leftName, rightName, name, l.PrimaryKey(), l.String(), r.String()))
}
leftIndex++
rightIndex++
}
for leftIndex < left.Len() {
er.RecordError(fmt.Errorf("%v has an extra %v %v", leftName, name, left.Get(leftIndex).PrimaryKey()))
leftIndex++
}
for rightIndex < right.Len() {
er.RecordError(fmt.Errorf("%v has an extra %v %v", rightName, name, right.Get(rightIndex).PrimaryKey()))
rightIndex++
}
}
func DiffPermissions(leftName string, left *Permissions, rightName string, right *Permissions, er concurrency.ErrorRecorder) {
diffPermissions("user", leftName, left.UserPermissions, rightName, right.UserPermissions, er)
diffPermissions("db", leftName, left.DbPermissions, rightName, right.DbPermissions, er)
diffPermissions("host", leftName, left.HostPermissions, rightName, right.HostPermissions, er)
}
func DiffPermissionsToArray(leftName string, left *Permissions, rightName string, right *Permissions) (result []string) {
er := concurrency.AllErrorRecorder{}
DiffPermissions(leftName, left, rightName, right, &er)
if er.HasErrors() {
return er.ErrorStrings()
} else {
return nil
}
}