Skip to content

Commit 0a99f61

Browse files
authored
make v25 APIs work with no ACL (#9408)
1 parent 08fbcfc commit 0a99f61

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

edgraph/alter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func executeDropAllInNs(ctx context.Context, startTs uint64, req *apiv25.AlterRe
108108
}
109109

110110
err = x.RetryUntilSuccess(10, 100*time.Millisecond, func() error {
111-
return createGuardianAndGroot(x.AttachNamespace(ctx, nsID), nsID, "password")
111+
return createGuardianAndGroot(x.AttachNamespace(ctx, nsID), "password")
112112
})
113113
if err != nil {
114114
return errors.Wrapf(err, "Failed to create guardian and groot: ")

edgraph/multi_tenancy.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,23 @@ func (s *Server) CreateNamespaceInternal(ctx context.Context, passwd string) (ui
105105
}
106106

107107
err = x.RetryUntilSuccess(10, 100*time.Millisecond, func() error {
108-
return createGuardianAndGroot(ctx, ids.StartId, passwd)
108+
return createGuardianAndGroot(ctx, passwd)
109109
})
110110
if err != nil {
111111
return 0, errors.Wrapf(err, "Failed to create guardian and groot: ")
112112
}
113+
113114
glog.V(2).Infof("Created namespace: %d", ns)
114115
return ns, nil
115116
}
116117

117118
// This function is used while creating new namespace. New namespace creation is only allowed
118119
// by the guardians of the galaxy group.
119-
func createGuardianAndGroot(ctx context.Context, namespace uint64, passwd string) error {
120+
func createGuardianAndGroot(ctx context.Context, passwd string) error {
121+
if !x.WorkerConfig.AclEnabled {
122+
return nil
123+
}
124+
120125
if err := upsertGuardian(ctx); err != nil {
121126
return errors.Wrap(err, "While creating Guardian")
122127
}

edgraph/ns_query_no_acl_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//go:build integration2
2+
3+
/*
4+
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
package edgraph
9+
10+
import (
11+
"context"
12+
"testing"
13+
14+
"github.com/dgraph-io/dgo/v250"
15+
"github.com/stretchr/testify/require"
16+
17+
"github.com/hypermodeinc/dgraph/v25/dgraphtest"
18+
)
19+
20+
func TestNamespaces(t *testing.T) {
21+
conf := dgraphtest.NewClusterConfig().WithNumAlphas(1).WithNumZeros(1).WithReplicas(1)
22+
c, err := dgraphtest.NewLocalCluster(conf)
23+
require.NoError(t, err)
24+
defer func() { c.Cleanup(t.Failed()) }()
25+
require.NoError(t, c.Start())
26+
27+
// ensure that Open works with no ACL
28+
alphaGrpcPort, err := c.GetAlphaGrpcPublicPort(0)
29+
require.NoError(t, err)
30+
_, err = dgo.Open("dgraph://localhost:" + alphaGrpcPort)
31+
require.NoError(t, err)
32+
33+
client, cleanup, err := c.Client()
34+
require.NoError(t, err)
35+
defer cleanup()
36+
37+
// Drop all data
38+
require.NoError(t, client.DropAll())
39+
40+
// Create two namespaces
41+
ctx := context.Background()
42+
require.NoError(t, client.CreateNamespace(ctx, "ns1"))
43+
require.NoError(t, client.CreateNamespace(ctx, "ns2"))
44+
45+
// namespace 1
46+
require.NoError(t, client.SetSchema(ctx, "ns1", `name: string @index(exact) .`))
47+
resp, err := client.RunDQL(ctx, "ns1", `{ set {_:a <name> "Alice" .}}`)
48+
require.NoError(t, err)
49+
require.Equal(t, 1, len(resp.BlankUids))
50+
resp, err = client.RunDQL(ctx, "ns1", `{ q(func: has(name)) { name } }`)
51+
require.NoError(t, err)
52+
require.JSONEq(t, `{"q":[{"name":"Alice"}]}`, string(resp.GetQueryResult()))
53+
54+
// namespace 2
55+
require.NoError(t, client.SetSchema(ctx, "ns2", `name: string @index(exact) .`))
56+
_, err = client.RunDQL(ctx, "ns2", `{ set {_:a <name> "Bob" .}}`)
57+
require.NoError(t, err)
58+
resp, err = client.RunDQL(ctx, "ns2", `{ q(func: has(name)) { name } }`)
59+
require.NoError(t, err)
60+
require.JSONEq(t, `{"q":[{"name":"Bob"}]}`, string(resp.GetQueryResult()))
61+
62+
// rename ns2 namespace
63+
require.NoError(t, client.RenameNamespace(ctx, "ns2", "ns2-new"))
64+
65+
// check if the data is still there
66+
resp, err = client.RunDQL(ctx, "ns2-new", `{ q(func: has(name)) { name } }`)
67+
require.NoError(t, err)
68+
require.JSONEq(t, `{"q":[{"name":"Bob"}]}`, string(resp.GetQueryResult()))
69+
70+
// List Namespaces
71+
nsMaps, err := client.ListNamespaces(ctx)
72+
require.NoError(t, err)
73+
require.Len(t, nsMaps, 3)
74+
75+
// drop ns2-new namespace
76+
require.NoError(t, client.DropNamespace(ctx, "ns2-new"))
77+
_, err = client.RunDQL(ctx, "ns2-new", `{ q(func: has(name)) { name } }`)
78+
require.ErrorContains(t, err, "namespace \"ns2-new\" not found")
79+
nsMaps, err = client.ListNamespaces(ctx)
80+
require.NoError(t, err)
81+
require.Len(t, nsMaps, 2)
82+
83+
// drop ns1 namespace
84+
require.NoError(t, client.DropNamespace(ctx, "ns1"))
85+
_, err = client.RunDQL(ctx, "ns1", `{ q(func: has(name)) { name } }`)
86+
require.ErrorContains(t, err, "namespace \"ns1\" not found")
87+
}

x/acl_enc_keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func checkAclKeyLength(alg jwt.SigningMethod, key Sensitive) error {
139139

140140
// SHA length has to be smaller or equal to the key length
141141
if sl > len(key)*8 {
142-
return errors.Errorf("ACL key length [%v <= %v] bits for JWT algorithm [%v]", sl, len(key)*8, alg.Alg())
142+
return errors.Errorf("ACL key length [%v <= %v] bits for JWT algorithm [%v]", len(key)*8, sl, alg.Alg())
143143
}
144144
return nil
145145
}

0 commit comments

Comments
 (0)