Skip to content

Commit 358d02c

Browse files
Check context for string namespace metadata
Attach that namespace if found -- will be overwritten if JWL-ACLs namespaces are found
1 parent 2314f84 commit 358d02c

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

edgraph/server.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,21 @@ func (s *Server) Query(ctx context.Context, req *api.Request) (*api.Response, er
11941194

11951195
// Query handles queries or mutations
11961196
func (s *Server) QueryNoGrpc(ctx context.Context, req *api.Request) (*api.Response, error) {
1197-
ctx = x.AttachJWTNamespace(ctx)
1197+
// if the `namespace-str` is present in the metadata, use it to attach the namespace
1198+
// otherwise, use the namespace from the JWT.
1199+
nsStr, err := x.ExtractNamespaceStr(ctx)
1200+
if err == nil {
1201+
ns, err := getNamespaceIDFromName(x.AttachNamespace(ctx, x.RootNamespace), nsStr)
1202+
if err == nil {
1203+
ctx = x.AttachNamespace(ctx, ns)
1204+
} else {
1205+
ctx = x.AttachJWTNamespace(ctx)
1206+
}
1207+
} else {
1208+
ctx = x.AttachJWTNamespace(ctx)
1209+
}
1210+
// if acl is enabled, then the namespace from the JWT will be applied in the test below
1211+
// overriding any namespace from the metadata obtained from the `namespace-str` above.
11981212
if x.WorkerConfig.AclEnabled && req.GetStartTs() != 0 {
11991213
// A fresh StartTs is assigned if it is 0.
12001214
ns, err := x.ExtractNamespace(ctx)

x/x.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ func ExtractNamespace(ctx context.Context) (uint64, error) {
268268
return namespace, nil
269269
}
270270

271+
// ExtractNamespaceStr parses the namespace string value from the incoming gRPC context.
272+
func ExtractNamespaceStr(ctx context.Context) (string, error) {
273+
md, ok := metadata.FromIncomingContext(ctx)
274+
if !ok {
275+
return "", errors.New("No metadata in the context")
276+
}
277+
ns := md.Get("namespace-str")
278+
if len(ns) == 0 {
279+
return "", errors.New("No namespace-str in the metadata of context")
280+
}
281+
return ns[0], nil
282+
}
283+
271284
func IsRootNsOperation(ctx context.Context) bool {
272285
md, ok := metadata.FromIncomingContext(ctx)
273286
if !ok {

0 commit comments

Comments
 (0)