-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathchange_v20.03.0.go
119 lines (101 loc) · 2.55 KB
/
change_v20.03.0.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
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package upgrade
import (
"encoding/json"
"fmt"
"github.com/dgraph-io/dgo/v240/protos/api"
"github.com/hypermodeinc/dgraph/v24/x"
)
const (
queryACLGroupsBefore_v20_03_0 = `
{
rules(func: type(Group)) @filter(has(dgraph.group.acl)) {
uid
dgraph.group.acl
}
}
`
)
type group struct {
UID string `json:"uid"`
ACL string `json:"dgraph.group.acl,omitempty"`
}
type rule struct {
Predicate string `json:"predicate,omitempty"`
Permission int `json:"perm,omitempty"`
}
type rules []rule
func upgradeACLRules() error {
dg, cb := x.GetDgraphClient(Upgrade.Conf, true)
defer cb()
data := make(map[string][]group)
if err := getQueryResult(dg, queryACLGroupsBefore_v20_03_0, &data); err != nil {
return fmt.Errorf("error querying old ACL rules: %w", err)
}
groups, ok := data["rules"]
if !ok {
return fmt.Errorf("unable to parse ACLs: %v", data)
}
counter := 1
var nquads []*api.NQuad
for _, group := range groups {
if group.ACL == "" {
continue
}
var rs rules
if err := json.Unmarshal([]byte(group.ACL), &rs); err != nil {
return fmt.Errorf("unable to unmarshal ACL: %v :: %w", group.ACL, err)
}
for _, r := range rs {
newRuleStr := fmt.Sprintf("_:newrule%d", counter)
nquads = append(nquads, []*api.NQuad{
// the name of the type was Rule in v20.03.0
getTypeNquad(newRuleStr, "Rule"),
{
Subject: newRuleStr,
Predicate: "dgraph.rule.predicate",
ObjectValue: &api.Value{
Val: &api.Value_StrVal{StrVal: r.Predicate},
},
},
{
Subject: newRuleStr,
Predicate: "dgraph.rule.permission",
ObjectValue: &api.Value{
Val: &api.Value_IntVal{IntVal: int64(r.Permission)},
},
},
{
Subject: group.UID,
Predicate: "dgraph.acl.rule",
ObjectId: newRuleStr,
},
}...)
counter++
}
}
// Nothing to do.
if len(nquads) == 0 {
fmt.Println("nothing to do: no old rules found in the cluster")
return nil
}
if err := mutateWithClient(dg, &api.Mutation{Set: nquads}); err != nil {
return fmt.Errorf("error upgrading ACL rules: %w", err)
}
fmt.Println("Successfully upgraded ACL rules.")
deleteOld := Upgrade.Conf.GetBool("deleteOld")
if deleteOld {
err := alterWithClient(dg, &api.Operation{
DropOp: api.Operation_ATTR,
DropValue: "dgraph.group.acl",
})
if err != nil {
return fmt.Errorf("error deleting old acl predicates: %w", err)
}
fmt.Println("Successfully deleted old rules.")
}
return nil
}