Skip to content

Commit 4d1bab7

Browse files
fix ns login after drop data
1 parent b5288f2 commit 4d1bab7

File tree

2 files changed

+131
-2
lines changed

2 files changed

+131
-2
lines changed

edgraph/server.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, er
377377
defer span.End()
378378

379379
ctx = x.AttachJWTNamespace(ctx)
380+
380381
span.Annotatef(nil, "Alter operation: %+v", op)
381382

382383
// Always print out Alter operations because they are important and rare.
@@ -458,8 +459,9 @@ func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, er
458459

459460
// just reinsert the GraphQL schema, no need to alter dgraph schema as this was drop_data
460461
_, err = UpdateGQLSchema(ctx, graphQLSchema, "")
461-
// recreate the admin account after a drop data operation
462-
InitializeAcl(nil)
462+
463+
// Since all data has been dropped, we need to recreate the admin account in the respective namespace.
464+
upsertGuardianAndGroot(nil, namespace)
463465
return empty, err
464466
}
465467

systest/multi-tenancy/login_test.go

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//go:build integration
2+
// +build integration
3+
4+
/*
5+
* Copyright 2024 Dgraph Labs, Inc. and Contributors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package main
21+
22+
import (
23+
"context"
24+
"testing"
25+
26+
"github.com/stretchr/testify/require"
27+
28+
"github.com/dgraph-io/dgo/v240/protos/api"
29+
"github.com/dgraph-io/dgraph/v24/dgraphapi"
30+
"github.com/dgraph-io/dgraph/v24/x"
31+
)
32+
33+
func addData(t *testing.T, gc *dgraphapi.GrpcClient) {
34+
require.NoError(t, gc.SetupSchema(`name: string .`))
35+
36+
rdfs := `
37+
_:a <name> "alice" .
38+
_:b <name> "bob" .
39+
_:c <name> "sagar" .
40+
_:d <name> "ajay" .`
41+
_, err := gc.Mutate(&api.Mutation{SetNquads: []byte(rdfs), CommitNow: true})
42+
require.NoError(t, err)
43+
}
44+
45+
func (msuite *MultitenancyTestSuite) TestLoggingIntoTheNSAfterDropDataFromTheNS() {
46+
t := msuite.T()
47+
gc, cleanup, err := msuite.dc.Client()
48+
require.NoError(t, err)
49+
defer cleanup()
50+
51+
hc, err := msuite.dc.HTTPClient()
52+
require.NoError(t, err)
53+
54+
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
55+
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))
56+
require.NoError(t, gc.DropAll())
57+
for i := 1; i < 5; i++ {
58+
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser,
59+
dgraphapi.DefaultPassword, x.GalaxyNamespace))
60+
ns, err := hc.AddNamespace()
61+
require.NoError(t, err)
62+
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
63+
require.NoError(t, gc.LoginIntoNamespace(context.Background(), dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
64+
65+
addData(t, gc)
66+
67+
// Drop data from the namespace
68+
require.NoError(t, gc.Alter(context.Background(), &api.Operation{DropOp: api.Operation_DATA}))
69+
70+
// Login into the namespace
71+
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
72+
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
73+
74+
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
75+
}
76+
}
77+
78+
func (msuite *MultitenancyTestSuite) TestLoggingIntoAllNamespacesAfterDropDataOperationFromDefaultNs() {
79+
t := msuite.T()
80+
gc, cleanup, err := msuite.dc.Client()
81+
require.NoError(t, err)
82+
defer cleanup()
83+
84+
hc, err := msuite.dc.HTTPClient()
85+
require.NoError(t, err)
86+
87+
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
88+
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))
89+
90+
require.NoError(t, gc.DropAll())
91+
nss := []uint64{}
92+
for i := 1; i < 2; i++ {
93+
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))
94+
ns, err := hc.AddNamespace()
95+
nss = append(nss, ns)
96+
require.NoError(t, err)
97+
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
98+
require.NoError(t, gc.LoginIntoNamespace(context.Background(), dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
99+
100+
addData(t, gc)
101+
}
102+
103+
// Drop data from default namespace
104+
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
105+
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))
106+
107+
require.NoError(t, gc.Alter(context.Background(), &api.Operation{DropOp: api.Operation_DATA}))
108+
109+
// verify here that login into the namespace should not fail
110+
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
111+
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))
112+
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
113+
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))
114+
115+
for _, ns := range nss {
116+
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
117+
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
118+
query := `{
119+
q(func: has(name)) {
120+
count(uid)
121+
}
122+
}`
123+
resp, err := gc.Query(query)
124+
require.NoError(t, err)
125+
require.Contains(t, string(resp.Json), `"count":4`)
126+
}
127+
}

0 commit comments

Comments
 (0)