-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathcheck_upgrade.go
More file actions
579 lines (506 loc) · 14.9 KB
/
Copy pathcheck_upgrade.go
File metadata and controls
579 lines (506 loc) · 14.9 KB
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
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/
package checkupgrade
import (
"crypto"
"crypto/ed25519"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/spf13/cobra"
"github.com/hypermodeinc/dgraph/v25/dgraphapi"
"github.com/hypermodeinc/dgraph/v25/x"
)
var (
CheckUpgrade x.SubCommand
)
const (
alphaHttp = "http_port"
dgUser = "dgUser"
password = "password"
namespace = "namespace"
aclSecretKeyFilePath = "aclSecretKeyFilePath"
jwtAlg = "jwt-alg"
deleteDup = "delete-duplicates"
guardianGroup = "guardians"
)
type commandInput struct {
alphaHttp string
dgUser string
password string
namespace uint64
aclSecretKeyFilePath string
jwtAlg string
dupDelete bool
}
type aclNode struct {
UID string `json:"uid"`
DgraphXID string `json:"dgraph.xid"`
DgraphType []string `json:"dgraph.type"`
DgraphUserGroup []UserGroup `json:"dgraph.user.group"`
}
type UserGroup struct {
UID string `json:"uid"`
DgraphXid string `json:"dgraph.xid"`
}
type Nodes struct {
Nodes []aclNode `json:"nodes"`
}
type Response struct {
Data Nodes `json:"data"`
}
func parseJWTKey(alg jwt.SigningMethod, key x.Sensitive) (interface{}, interface{}, error) {
switch {
case strings.HasPrefix(alg.Alg(), "HS"):
return key, key, nil
case strings.HasPrefix(alg.Alg(), "ES"):
pk, err := jwt.ParseECPrivateKeyFromPEM(key)
if err != nil {
return nil, nil, fmt.Errorf("error parsing ACL key as ECDSA private key: %w", err)
}
return pk, &pk.PublicKey, nil
case strings.HasPrefix(alg.Alg(), "RS") || strings.HasPrefix(alg.Alg(), "PS"):
pk, err := jwt.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
return nil, nil, fmt.Errorf("error parsing ACL key as RSA private key: %w", err)
}
return pk, &pk.PublicKey, nil
case alg.Alg() == "EdDSA":
pk, err := jwt.ParseEdPrivateKeyFromPEM(key)
if err != nil {
return nil, nil, fmt.Errorf("error parsing ACL key as EdDSA private key: %w", err)
}
return pk.(crypto.Signer), pk.(ed25519.PrivateKey).Public(), nil
default:
return nil, nil, fmt.Errorf("unsupported signing algorithm: %v", alg.Alg())
}
}
func getAccessJwt(userId string, group string, namespace uint64, aclSecretFile string,
algStr string) (string, error) {
aclKey, err := os.ReadFile(aclSecretFile)
if err != nil {
return "", fmt.Errorf("error reading ACL secret key from file: %s: %s", aclSecretFile, err)
}
var aclAlg jwt.SigningMethod
var privKey interface{}
if aclKey != nil {
aclAlg = jwt.GetSigningMethod(algStr)
if aclAlg == nil {
return "", fmt.Errorf("unsupported jwt signing algorithm for acl: %v", algStr)
}
privKey, _, err = parseJWTKey(aclAlg, aclKey)
if err != nil {
return "", err
}
}
token := jwt.NewWithClaims(aclAlg, jwt.MapClaims{
"userid": userId,
"groups": []string{group},
"namespace": namespace,
"exp": time.Now().Add(time.Hour).Unix(),
})
jwtString, err := token.SignedString(x.MaybeKeyToBytes(privKey))
if err != nil {
return "", fmt.Errorf("unable to encode jwt to string: %w", err)
}
return jwtString, nil
}
func setupClient(alphaHttp string) (*dgraphapi.HTTPClient, error) {
httpClient, err := dgraphapi.GetHttpClient(alphaHttp, "")
if err != nil {
return nil, fmt.Errorf("while getting HTTP client: %w", err)
}
return httpClient, nil
}
func contains(slice []string, value string) bool {
for _, v := range slice {
if v == value {
return true
}
}
return false
}
func findDuplicateNodes(aclNodes []aclNode) [3]map[string][]string {
du := make(map[string][]string)
dg := make(map[string][]string)
dug := make(map[string][]string)
for i, node1 := range aclNodes {
for j := i + 1; j < len(aclNodes); j++ {
node2 := aclNodes[j]
if node1.DgraphXID == node2.DgraphXID {
if node1.DgraphType[0] == "dgraph.type.User" && node1.DgraphType[0] == node2.DgraphType[0] {
filterAndRecordDuplicates(du, node1, node2)
} else if node1.DgraphType[0] == "dgraph.type.Group" && node1.DgraphType[0] == node2.DgraphType[0] {
filterAndRecordDuplicates(dg, node1, node2)
} else {
filterAndRecordDuplicates(dug, node1, node2)
}
}
}
}
return [3]map[string][]string{
du, dg, dug,
}
}
func filterAndRecordDuplicates(du map[string][]string, node1 aclNode, node2 aclNode) {
if _, exists := du[node1.DgraphXID]; !exists {
du[node1.DgraphXID] = []string{}
}
if !contains(du[node1.DgraphXID], node1.UID) {
du[node1.DgraphXID] = append(du[node1.DgraphXID], node1.UID)
}
if !contains(du[node1.DgraphXID], node2.UID) {
du[node1.DgraphXID] = append(du[node1.DgraphXID], node2.UID)
}
}
func queryDuplicateNodes(hc *dgraphapi.HTTPClient) ([3]map[string][]string, error) {
query := `{
nodes(func: has(dgraph.xid)) {
uid
dgraph.xid
dgraph.type
}
}`
resp, err := hc.PostDqlQuery(query)
if err != nil {
return [3]map[string][]string{}, fmt.Errorf("while querying dgraph for duplicate nodes: %w", err)
}
var result Response
if err := json.Unmarshal(resp, &result); err != nil {
return [3]map[string][]string{}, fmt.Errorf("while unmarshalling response: %v: %w", string(resp), err)
}
return findDuplicateNodes(result.Data.Nodes), nil
}
func printAndDeleteDuplicates(hc *dgraphapi.HTTPClient, entityType string, ns uint64, nodesmap map[string][]string,
dupDelete bool) error {
if len(nodesmap) == 0 {
return nil
}
fmt.Printf("Found duplicate %ss in namespace: #%v\n", entityType, ns)
for key, node := range nodesmap {
fmt.Printf("dgraph.xid %v , Uids: %v\n", key, node)
}
if dupDelete {
switch entityType {
case "user":
return deleteDuplicatesUser(hc, nodesmap)
case "group":
return deleteDuplicatesGroup(hc, nodesmap)
default:
return deleteDuplicatesUserGroup(hc, nodesmap)
}
}
return nil
}
func deleteUids(hc *dgraphapi.HTTPClient, uids []string, skipUid int, node string) error {
query := `{
delete {
<%v> * * .
}
}`
for i, uid := range uids {
if i == skipUid {
continue
}
fmt.Printf("deleting following uid [%v] of duplicate node:%v\n", uid, node)
_, err := hc.Mutate(fmt.Sprintf(query, uid), true)
if err != nil {
return err
}
}
return nil
}
// uniqueStringsExcluding extracts unique strings from nodeCollection excluding those in the exclude slice
func uniqueStringsExcluding(nodeCollection [][]string, exclude []string) []string {
excludeMap := make(map[string]struct{})
for _, e := range exclude {
excludeMap[e] = struct{}{}
}
uniqueMap := make(map[string]struct{})
for _, nodes := range nodeCollection {
for _, node := range nodes {
if _, inExclude := excludeMap[node]; !inExclude {
uniqueMap[node] = struct{}{}
}
}
}
uniqueSlice := make([]string, 0, len(uniqueMap))
for node := range uniqueMap {
uniqueSlice = append(uniqueSlice, node)
}
return uniqueSlice
}
func queryUserGroup(hc *dgraphapi.HTTPClient, uid string) (aclNode, error) {
query := fmt.Sprintf(`{
nodes(func: eq("dgraph.xid", "%v")) {
uid
dgraph.xid
}
}`, uid)
resp, err := hc.PostDqlQuery(query)
if err != nil {
return aclNode{}, err
}
var result Response
if err := json.Unmarshal(resp, &result); err != nil {
return aclNode{}, fmt.Errorf("while unmarshalling response: %v: %w", string(resp), err)
}
if len(result.Data.Nodes) > 1 {
return aclNode{}, nil
}
return result.Data.Nodes[0], nil
}
func addUsersToGroup(hc *dgraphapi.HTTPClient, users []string, groupUid string) error {
rdf := ``
for _, user := range users {
fmt.Printf("adding user %v to group %v\n", user, groupUid)
node, err := queryUserGroup(hc, user)
if err != nil {
return err
}
if node.UID != "" {
rdf += fmt.Sprintf("<%v> <dgraph.user.group> <%v> .\n", groupUid, node.UID)
}
}
_, err := hc.Mutate(rdf, true)
if err != nil {
return err
}
return nil
}
func deleteDuplicatesGroup(hc *dgraphapi.HTTPClient, duplicates map[string][]string) error {
query := `{
nodes(func: uid(%v)) {
uid
dgraph.xid
dgraph.type
~dgraph.user.group{
dgraph.xid
}
}
}`
for group, uids := range duplicates {
var nodeCollection [][]string
for _, uid := range uids {
resp, err := hc.PostDqlQuery(fmt.Sprintf(query, uid))
if err != nil {
return err
}
var result Response
if err := json.Unmarshal(resp, &result); err != nil {
log.Fatalf("while unmarshalling response: %v", err)
}
var strs []string
for i := range result.Data.Nodes[0].DgraphUserGroup {
strs = append(strs, result.Data.Nodes[0].DgraphUserGroup[i].DgraphXid)
}
nodeCollection = append(nodeCollection, strs)
}
var saveIndex int
prevLen := 0
fmt.Printf("keeping group%v with uid: %v", group, uids[saveIndex])
if group == guardianGroup {
for k, nodes := range nodeCollection {
if contains(nodes, "groot") && len(nodes) > prevLen {
saveIndex = k
prevLen = len(nodes)
}
}
uniqueUsers := uniqueStringsExcluding(nodeCollection, uids)
if err := addUsersToGroup(hc, uniqueUsers, uids[saveIndex]); err != nil {
return err
}
if err := deleteUids(hc, uids, saveIndex, group); err != nil {
return err
}
} else {
if err := deleteUids(hc, uids, 0, group); err != nil {
return err
}
}
}
return nil
}
func deleteDuplicatesUser(hc *dgraphapi.HTTPClient, duplicates map[string][]string) error {
query := `{
nodes(func: uid(%v)) {
uid
dgraph.xid
dgraph.type
dgraph.user.group{
dgraph.xid
}
}
}`
for user, uids := range duplicates {
var groupsCollection [][]string
for _, uid := range uids {
resp, err := hc.PostDqlQuery(fmt.Sprintf(query, uid))
if err != nil {
return err
}
var result Response
if err := json.Unmarshal(resp, &result); err != nil {
log.Fatalf("while unmarshalling response: %v", err)
}
var strs []string
for i := range result.Data.Nodes[0].DgraphUserGroup {
strs = append(strs, result.Data.Nodes[0].DgraphUserGroup[i].DgraphXid)
}
groupsCollection = append(groupsCollection, strs)
}
var saveIndex int
prevLen := 0
for k, groups := range groupsCollection {
if contains(groups, "guardians") && len(groups) > prevLen {
saveIndex = k
prevLen = len(groups)
}
}
fmt.Printf("keeping user%v with uid: %v", user, uids[saveIndex])
if err := deleteUids(hc, uids, saveIndex, user); err != nil {
return err
}
}
return nil
}
func deleteDuplicatesUserGroup(hc *dgraphapi.HTTPClient, duplicates map[string][]string) error {
// we will delete only user in this case
query := `{
nodes(func: uid(%v)) {
uid
dgraph.xid
dgraph.type
}
}`
for userGroup, uids := range duplicates {
var saveIndex int
for i, uid := range uids {
resp, err := hc.PostDqlQuery(fmt.Sprintf(query, uid))
if err != nil {
return err
}
var result Response
if err := json.Unmarshal(resp, &result); err != nil {
log.Fatalf("while unmarshalling response: %v", err)
}
if result.Data.Nodes[0].DgraphType[0] == "dgraph.type.group" {
saveIndex = i
break
}
}
fmt.Printf("keeping group%v with uid: %v", userGroup, uids[saveIndex])
fmt.Print("\n")
if err := deleteUids(hc, uids, saveIndex, userGroup); err != nil {
return err
}
}
return nil
}
func init() {
CheckUpgrade.Cmd = &cobra.Command{
Use: "checkupgrade",
Short: "Run the checkupgrade tool",
Long: "The checkupgrade tool is used to check for duplicate dgraph.xid's in the Dgraph database before upgrade.",
Run: func(cmd *cobra.Command, args []string) {
run()
},
Annotations: map[string]string{"group": "tool"},
}
CheckUpgrade.Cmd.SetHelpTemplate(x.NonRootTemplate)
flag := CheckUpgrade.Cmd.Flags()
flag.String(alphaHttp, "127.0.0.1:8080", "Dgraph Alpha Http server address")
flag.String(namespace, "", "Namespace to check for duplicate nodes")
flag.String(dgUser, "groot", "Username of the namespace's user")
flag.String(password, "password", "Password of the namespace's user")
flag.String(aclSecretKeyFilePath, "", "path of file that stores secret key or private key,"+
" which is used to sign the ACL JWT")
flag.String(jwtAlg, "HS256", "JWT signing algorithm")
flag.String(deleteDup, "false", "set this flag to true to delete duplicates nodes")
}
func run() {
if err := checkUpgrade(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
func checkUpgrade() error {
fmt.Println("Running check-upgrade tool")
cmdInput := parseInput()
var accessJwt string
var err error
if cmdInput.aclSecretKeyFilePath != "" {
accessJwt, err = getAccessJwt(dgraphapi.DefaultUser, guardianGroup, 0, cmdInput.aclSecretKeyFilePath,
cmdInput.jwtAlg)
if err != nil {
return fmt.Errorf("while getting access jwt token: %w", err)
}
}
hc, err := setupClient(cmdInput.alphaHttp)
if err != nil {
return fmt.Errorf("while setting up clients: %w", err)
}
hc.AccessJwt = accessJwt
var namespaces []uint64
if cmdInput.namespace == 0 {
namespaces, err = hc.ListNamespaces()
if err != nil {
return fmt.Errorf("while lisiting namespaces: %w", err)
}
} else {
namespaces = append(namespaces, cmdInput.namespace)
}
for _, ns := range namespaces {
if cmdInput.aclSecretKeyFilePath != "" {
hc.AccessJwt, err = getAccessJwt(dgraphapi.DefaultUser, guardianGroup, ns, cmdInput.aclSecretKeyFilePath,
cmdInput.jwtAlg)
if err != nil {
return fmt.Errorf("while getting access jwt token for namespace %v: %w", ns, err)
}
} else {
if err := hc.LoginIntoNamespace(cmdInput.dgUser, cmdInput.password, ns); err != nil {
return fmt.Errorf("while logging into namespace %v: %w", ns, err)
}
}
duplicates, err := queryDuplicateNodes(hc)
if err != nil {
return err
}
if err := printAndDeleteDuplicates(hc, "user", ns, duplicates[0], cmdInput.dupDelete); err != nil {
return err
}
// example output:
// Found duplicate users in namespace: #0
// dgraph.xid user1 , Uids: [0x4 0x3]
if err := printAndDeleteDuplicates(hc, "group", ns, duplicates[1], cmdInput.dupDelete); err != nil {
return err
}
// Found duplicate groups in namespace: #1
// dgraph.xid group1 , Uids: [0x2714 0x2711]
if err := printAndDeleteDuplicates(hc, "groups and user", ns, duplicates[2], cmdInput.dupDelete); err != nil {
return err
}
// Found duplicate groups and users in namespace: #0
// dgraph.xid userGroup1 , Uids: [0x7532 0x7531]
}
fmt.Println("To delete duplicate nodes use following mutation: ")
deleteMut := `
{
delete {
<UID> * * .
}
}`
fmt.Fprint(os.Stderr, deleteMut)
return nil
}
func parseInput() *commandInput {
return &commandInput{alphaHttp: CheckUpgrade.Conf.GetString(alphaHttp), dgUser: CheckUpgrade.Conf.GetString(dgUser),
password: CheckUpgrade.Conf.GetString(password), namespace: CheckUpgrade.Conf.GetUint64(namespace),
aclSecretKeyFilePath: CheckUpgrade.Conf.GetString(aclSecretKeyFilePath),
jwtAlg: CheckUpgrade.Conf.GetString(jwtAlg), dupDelete: CheckUpgrade.Conf.GetBool(deleteDup)}
}