Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving ACL #2951

Merged
merged 14 commits into from
Feb 13, 2019
17 changes: 16 additions & 1 deletion dgraph/cmd/alpha/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ func queryHandler(w http.ResponseWriter, r *http.Request) {

d := r.URL.Query().Get("debug")
ctx := context.WithValue(context.Background(), query.DebugKey, d)
if accessJwt := r.Header.Get("X-Dgraph-AccessJWT"); accessJwt != "" {
md := metadata.New(nil)
md.Append("accessJwt", accessJwt)
ctx = metadata.NewIncomingContext(ctx, md)
}

// If ro is set, run this as a readonly query.
if ro := r.URL.Query().Get("ro"); len(ro) > 0 && req.StartTs == 0 {
Expand Down Expand Up @@ -252,6 +257,12 @@ func mutationHandler(w http.ResponseWriter, r *http.Request) {
}
mu.CommitNow = c
}
ctx := context.Background()
if accessJwt := r.Header.Get("X-Dgraph-AccessJWT"); accessJwt != "" {
md := metadata.New(nil)
md.Append("accessJwt", accessJwt)
ctx = metadata.NewIncomingContext(ctx, md)
}

ts, err := extractStartTs(r.URL.Path)
if err != nil {
Expand All @@ -260,7 +271,7 @@ func mutationHandler(w http.ResponseWriter, r *http.Request) {
}
mu.StartTs = ts

resp, err := (&edgraph.Server{}).Mutate(context.Background(), mu)
resp, err := (&edgraph.Server{}).Mutate(ctx, mu)
if err != nil {
x.SetStatusWithData(w, x.ErrorInvalidRequest, err.Error())
return
Expand Down Expand Up @@ -426,6 +437,10 @@ func alterHandler(w http.ResponseWriter, r *http.Request) {
md := metadata.New(nil)
// Pass in an auth token, if present.
md.Append("auth-token", r.Header.Get("X-Dgraph-AuthToken"))
if accessJwt := r.Header.Get("X-Dgraph-AccessJWT"); accessJwt != "" {
md.Append("accessJwt", accessJwt)
}

ctx := metadata.NewIncomingContext(context.Background(), md)
if _, err = (&edgraph.Server{}).Alter(ctx, op); err != nil {
x.SetStatus(w, x.Error, err.Error())
Expand Down
73 changes: 73 additions & 0 deletions dgraph/cmd/alpha/login_ee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// +build !oss

/*
* Copyright 2018 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 alpha

import (
"context"
"encoding/json"
"net/http"

"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgraph/edgraph"
"github.com/dgraph-io/dgraph/x"
)

func loginHandler(w http.ResponseWriter, r *http.Request) {
if commonHandler(w, r) {
return
}

user := r.Header.Get("X-Dgraph-User")
password := r.Header.Get("X-Dgraph-Password")
refreshJwt := r.Header.Get("X-Dgraph-RefreshJWT")
ctx := context.Background()
resp, err := (&edgraph.Server{}).Login(ctx, &api.LoginRequest{
Userid: user,
Password: password,
RefreshToken: refreshJwt,
})

if err != nil {
x.SetStatusWithData(w, x.ErrorInvalidRequest, err.Error())
return
}

jwt := &api.Jwt{}
if err := jwt.Unmarshal(resp.Json); err != nil {
x.SetStatusWithData(w, x.Error, err.Error())
}

response := map[string]interface{}{}
mp := map[string]interface{}{}
mp["accessJWT"] = jwt.AccessJwt
mp["refreshJWT"] = jwt.RefreshJwt
response["data"] = mp

js, err := json.Marshal(response)
if err != nil {
x.SetStatusWithData(w, x.Error, err.Error())
return
}

writeResponse(w, r, js)
gitlw marked this conversation as resolved.
Show resolved Hide resolved
}

func init() {
http.HandleFunc("/login", loginHandler)
}
15 changes: 8 additions & 7 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ they form a Raft group and provide synchronous replication.
// with the flag name so that the values are picked up by Cobra/Viper's various config inputs
// (e.g, config file, env vars, cli flags, etc.)
flag := Alpha.Cmd.Flags()
flag.Bool("enterprise_features", false, "Enable Dgraph enterprise features. "+
"If you set this to true, you agree to the Dgraph Community License.")
flag.StringP("postings", "p", "p", "Directory to store posting lists.")

// Options around how to set up Badger.
Expand Down Expand Up @@ -128,8 +130,9 @@ they form a Raft group and provide synchronous replication.
" The token can be passed as follows: For HTTP requests, in X-Dgraph-AuthToken header."+
" For Grpc, in auth-token key in the context.")

flag.String("hmac_secret_file", "", "The file storing the HMAC secret"+
" that is used for signing the JWT. Enterprise feature.")
flag.String("acl_secret_file", "", "The file that stores the HMAC secret, "+
"which is used for signing the JWT and should have at least 32 ASCII characters. "+
"Enterprise feature.")
flag.Duration("acl_access_ttl", 6*time.Hour, "The TTL for the access jwt. "+
"Enterprise feature.")
flag.Duration("acl_refresh_ttl", 30*24*time.Hour, "The TTL for the refresh jwt. "+
Expand Down Expand Up @@ -440,21 +443,19 @@ func run() {
AllottedMemory: Alpha.Conf.GetFloat64("lru_mb"),
}

secretFile := Alpha.Conf.GetString("hmac_secret_file")
secretFile := Alpha.Conf.GetString("acl_secret_file")
if secretFile != "" {
if !Alpha.Conf.GetBool("enterprise_features") {
glog.Errorf("You must enable Dgraph enterprise features with the " +
glog.Fatalf("You must enable Dgraph enterprise features with the " +
"--enterprise_features option in order to use ACL.")
os.Exit(1)
}

hmacSecret, err := ioutil.ReadFile(secretFile)
if err != nil {
glog.Fatalf("Unable to read HMAC secret from file: %v", secretFile)
}
if len(hmacSecret) < 32 {
glog.Errorf("The HMAC secret file should contain at least 256 bits (32 ascii chars)")
os.Exit(1)
glog.Fatalf("The HMAC secret file should contain at least 256 bits (32 ascii chars)")
}

opts.HmacSecret = hmacSecret
Expand Down
9 changes: 8 additions & 1 deletion dgraph/cmd/alpha/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,14 @@ func alterSchema(s string) error {
if err != nil {
return err
}
_, _, err = runRequest(req)
for {
// keep retrying until we succeed or receive a non-retriable error
_, _, err = runRequest(req)
if err == nil || !strings.Contains(err.Error(), "Please retry operation") {
break
}
}

return err
}

Expand Down
2 changes: 0 additions & 2 deletions dgraph/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ func initCmds() {
"Use 0.0.0.0 instead of localhost to bind to all addresses on local machine.")
RootCmd.PersistentFlags().Bool("expose_trace", false,
"Allow trace endpoint to be accessible from remote")
RootCmd.PersistentFlags().Bool("enterprise_features", false,
"Enable Dgraph enterprise features. If you set this to true, you agree to the Dgraph Community License.")
rootConf.BindPFlags(RootCmd.PersistentFlags())

flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
Expand Down
6 changes: 6 additions & 0 deletions dgraph/cmd/zero/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,12 @@ func (s *Server) ShouldServe(
var proposal pb.ZeroProposal
// Multiple Groups might be assigned to same tablet, so during proposal we will check again.
tablet.Force = false
if x.IsAclPredicate(tablet.Predicate) {
// force all the acl predicates to be allocated to group 1
// this is to make it eaiser to stream ACL updates to all alpha servers
// since they only need to open one pipeline to receive updates for all ACL predicates
tablet.GroupId = 1
}
proposal.Tablet = tablet
if err := s.Node.proposeAndWait(ctx, &proposal); err != nil && err != errTabletAlreadyServed {
span.Annotatef(nil, "While proposing tablet: %v", err)
Expand Down
12 changes: 6 additions & 6 deletions dgraph/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ services:
source: $GOPATH/bin
target: /gobin
read_only: true
command: /gobin/dgraph zero -o 0 --my=zero1:5080 --replicas 3 --idx 1 --logtostderr -v=2 --enterprise_features --bindall --expose_trace --profile_mode block --block_rate 10
command: /gobin/dgraph zero -o 0 --my=zero1:5080 --replicas 3 --idx 1 --logtostderr -v=2 --bindall --expose_trace --profile_mode block --block_rate 10

zero2:
image: dgraph/dgraph:latest
Expand All @@ -38,7 +38,7 @@ services:
source: $GOPATH/bin
target: /gobin
read_only: true
command: /gobin/dgraph zero -o 2 --my=zero2:5082 --replicas 3 --idx 2 --logtostderr -v=2 --enterprise_features --peer=zero1:5080
command: /gobin/dgraph zero -o 2 --my=zero2:5082 --replicas 3 --idx 2 --logtostderr -v=2 --peer=zero1:5080

zero3:
image: dgraph/dgraph:latest
Expand All @@ -57,7 +57,7 @@ services:
source: $GOPATH/bin
target: /gobin
read_only: true
command: /gobin/dgraph zero -o 3 --my=zero3:5083 --replicas 3 --idx 3 --logtostderr -v=2 --enterprise_features --peer=zero1:5080
command: /gobin/dgraph zero -o 3 --my=zero3:5083 --replicas 3 --idx 3 --logtostderr -v=2 --peer=zero1:5080

dg1:
image: dgraph/dgraph:latest
Expand All @@ -74,7 +74,7 @@ services:
labels:
cluster: test
service: alpha
command: /gobin/dgraph alpha --my=dg1:7180 --lru_mb=1024 --zero=zero1:5080 -o 100 --expose_trace --trace 1.0 --profile_mode block --block_rate 10 --logtostderr -v=2 --enterprise_features --whitelist 10.0.0.0:10.255.255.255,172.16.0.0:172.31.255.255,192.168.0.0:192.168.255.255
command: /gobin/dgraph alpha --my=dg1:7180 --lru_mb=1024 --zero=zero1:5080 -o 100 --expose_trace --trace 1.0 --profile_mode block --block_rate 10 --logtostderr -v=2 --whitelist 10.0.0.0:10.255.255.255,172.16.0.0:172.31.255.255,192.168.0.0:192.168.255.255

dg2:
image: dgraph/dgraph:latest
Expand All @@ -93,7 +93,7 @@ services:
labels:
cluster: test
service: alpha
command: /gobin/dgraph alpha --my=dg2:7182 --lru_mb=1024 --zero=zero1:5080 -o 102 --expose_trace --trace 1.0 --profile_mode block --block_rate 10 --logtostderr -v=2 --enterprise_features --whitelist 10.0.0.0:10.255.255.255,172.16.0.0:172.31.255.255,192.168.0.0:192.168.255.255
command: /gobin/dgraph alpha --my=dg2:7182 --lru_mb=1024 --zero=zero1:5080 -o 102 --expose_trace --trace 1.0 --profile_mode block --block_rate 10 --logtostderr -v=2 --whitelist 10.0.0.0:10.255.255.255,172.16.0.0:172.31.255.255,192.168.0.0:192.168.255.255

dg3:
image: dgraph/dgraph:latest
Expand All @@ -112,4 +112,4 @@ services:
labels:
cluster: test
service: alpha
command: /gobin/dgraph alpha --my=dg3:7183 --lru_mb=1024 --zero=zero1:5080 -o 103 --expose_trace --trace 1.0 --profile_mode block --block_rate 10 --logtostderr -v=2 --enterprise_features --whitelist 10.0.0.0:10.255.255.255,172.16.0.0:172.31.255.255,192.168.0.0:192.168.255.255
command: /gobin/dgraph alpha --my=dg3:7183 --lru_mb=1024 --zero=zero1:5080 -o 103 --expose_trace --trace 1.0 --profile_mode block --block_rate 10 --logtostderr -v=2 --whitelist 10.0.0.0:10.255.255.255,172.16.0.0:172.31.255.255,192.168.0.0:192.168.255.255
Loading