-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add dgraph upgrade tool to upgrade the ACL data format. Co-authored-by: Animesh Chandra Pathak <[email protected]> Co-authored-by: Aman Mangal <[email protected]>
- Loading branch information
1 parent
065d860
commit e604b91
Showing
5 changed files
with
258 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* Copyright 2020 Dgraph Labs, Inc. and Contributors | ||
* | ||
* 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 upgrade | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/dgraph-io/dgo/v2" | ||
"github.com/dgraph-io/dgo/v2/protos/api" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
oldACLQuery = ` | ||
{ | ||
rules(func: type(Group)) { | ||
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 { | ||
alpha := Upgrade.Conf.GetString("alpha") | ||
userName := Upgrade.Conf.GetString("user") | ||
password := Upgrade.Conf.GetString("password") | ||
deleteOld := Upgrade.Conf.GetBool("deleteOld") | ||
|
||
// TODO(Aman): add TLS configuration. | ||
conn, err := grpc.Dial(alpha, grpc.WithInsecure()) | ||
if err != nil { | ||
return fmt.Errorf("unable to connect to Dgraph cluster: %w", err) | ||
} | ||
defer conn.Close() | ||
|
||
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn)) | ||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
defer cancel() | ||
|
||
// login to cluster | ||
if err := dg.Login(ctx, userName, password); err != nil { | ||
return fmt.Errorf("unable to login to Dgraph cluster: %w", err) | ||
} | ||
|
||
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
resp, err := dg.NewReadOnlyTxn().Query(ctx, oldACLQuery) | ||
if err != nil { | ||
return fmt.Errorf("unable to query old ACL rules: %w", err) | ||
} | ||
|
||
data := make(map[string][]group) | ||
if err := json.Unmarshal(resp.Json, &data); err != nil { | ||
return fmt.Errorf("unable to unmarshal old ACLs: %w", err) | ||
} | ||
|
||
groups, ok := data["rules"] | ||
if !ok { | ||
return fmt.Errorf("Unable to parse ACLs: %v", string(resp.Json)) | ||
} | ||
|
||
counter := 1 | ||
var nquads []*api.NQuad | ||
for _, group := range groups { | ||
if len(group.ACL) == 0 { | ||
continue | ||
} | ||
|
||
var rs rules | ||
if err := json.Unmarshal([]byte(group.ACL), &rs); err != nil { | ||
return fmt.Errorf("Unable to unmarshal ACL: %v :: %w", string(group.ACL), err) | ||
} | ||
|
||
for _, r := range rs { | ||
newRuleStr := fmt.Sprintf("_:newrule%d", counter) | ||
nquads = append(nquads, &api.NQuad{ | ||
Subject: newRuleStr, | ||
Predicate: "dgraph.rule.predicate", | ||
ObjectValue: &api.Value{ | ||
Val: &api.Value_StrVal{StrVal: r.Predicate}, | ||
}, | ||
}) | ||
|
||
nquads = append(nquads, &api.NQuad{ | ||
Subject: newRuleStr, | ||
Predicate: "dgraph.rule.permission", | ||
ObjectValue: &api.Value{ | ||
Val: &api.Value_IntVal{IntVal: int64(r.Permission)}, | ||
}, | ||
}) | ||
|
||
nquads = append(nquads, &api.NQuad{ | ||
Subject: group.UID, | ||
Predicate: "dgraph.acl.rule", | ||
ObjectId: newRuleStr, | ||
}) | ||
|
||
counter++ | ||
} | ||
} | ||
|
||
// Nothing to do. | ||
if len(nquads) == 0 { | ||
return fmt.Errorf("no old rules found in the cluster") | ||
} | ||
|
||
if err := mutateACL(dg, nquads); err != nil { | ||
return fmt.Errorf("error upgrading ACL rules: %w", err) | ||
} | ||
fmt.Println("Successfully upgraded ACL rules.") | ||
|
||
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
if deleteOld { | ||
err = dg.Alter(ctx, &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 | ||
} | ||
|
||
func mutateACL(dg *dgo.Dgraph, nquads []*api.NQuad) error { | ||
var err error | ||
for i := 0; i < 3; i++ { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
_, err = dg.NewTxn().Mutate(ctx, &api.Mutation{ | ||
Set: nquads, | ||
CommitNow: true, | ||
}) | ||
if err != nil { | ||
fmt.Printf("error in running mutation, retrying: %v\n", err) | ||
continue | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2020 Dgraph Labs, Inc. and Contributors | ||
* | ||
* 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 upgrade | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/dgraph-io/dgraph/x" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
// Upgrade is the sub-command used to upgrade dgraph cluster. | ||
Upgrade x.SubCommand | ||
) | ||
|
||
func init() { | ||
Upgrade.Cmd = &cobra.Command{ | ||
Use: "upgrade", | ||
Short: "Run the Dgraph upgrade tool", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
run() | ||
}, | ||
} | ||
|
||
flag := Upgrade.Cmd.Flags() | ||
flag.Bool("acl", false, "upgrade ACL from v1.2.2 to >v20.03.0") | ||
flag.StringP("alpha", "a", "127.0.0.1:9080", "Dgraph Alpha gRPC server address") | ||
flag.StringP("user", "u", "", "Username if login is required.") | ||
flag.StringP("password", "p", "", "Password of the user.") | ||
flag.BoolP("deleteOld", "d", true, "Delete the older ACL predicates") | ||
} | ||
|
||
func run() { | ||
if !Upgrade.Conf.GetBool("acl") { | ||
fmt.Fprint(os.Stderr, "Error! we only support acl upgrade as of now.\n") | ||
return | ||
} | ||
|
||
if err := upgradeACLRules(); err != nil { | ||
fmt.Fprintln(os.Stderr, "Error in upgrading ACL!", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters