Skip to content

Commit

Permalink
fix ns login after drop data
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaji-kharse committed Sep 11, 2024
1 parent 1cd18d4 commit 25ae577
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 2 deletions.
6 changes: 4 additions & 2 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, er
defer span.End()

ctx = x.AttachJWTNamespace(ctx)

span.Annotatef(nil, "Alter operation: %+v", op)

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

// just reinsert the GraphQL schema, no need to alter dgraph schema as this was drop_data
_, err = UpdateGQLSchema(ctx, graphQLSchema, "")
// recreate the admin account after a drop data operation
InitializeAcl(nil)

// Since all data has been dropped, we need to recreate the admin account in the respective namespace.
upsertGuardianAndGroot(nil, namespace)
return empty, err
}

Expand Down
123 changes: 123 additions & 0 deletions systest/multi-tenancy/login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//go:build integration
// +build integration

/*
* Copyright 2024 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 main

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/dgraph-io/dgo/v240/protos/api"
"github.com/dgraph-io/dgraph/v24/dgraphapi"
"github.com/dgraph-io/dgraph/v24/x"
)

func addData(t *testing.T, gc *dgraphapi.GrpcClient) {
require.NoError(t, gc.SetupSchema(`name: string .`))

rdfs := `
_:a <name> "alice" .
_:b <name> "bob" .
_:c <name> "sagar" .
_:d <name> "ajay" .`
_, err := gc.Mutate(&api.Mutation{SetNquads: []byte(rdfs), CommitNow: true})
require.NoError(t, err)
}

func (msuite *MultitenancyTestSuite) TestLoggingIntoTheNSAfterDropDataFromTheNS() {
t := msuite.T()
gc, cleanup, err := msuite.dc.Client()
require.NoError(t, err)
defer cleanup()

hc, err := msuite.dc.HTTPClient()
require.NoError(t, err)

require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))
require.NoError(t, gc.DropAll())

require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))
for i := 1; i < 5; i++ {
ns, err := hc.AddNamespace()
require.NoError(t, err)
require.NoError(t, gc.LoginIntoNamespace(context.Background(), dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))

addData(t, gc)

// Drop data from the namespace
require.NoError(t, gc.Alter(context.Background(), &api.Operation{DropOp: api.Operation_DATA}))

// Login into the namespace
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
}
}

func (msuite *MultitenancyTestSuite) TestLoggingIntoAllNamespacesAfterDropDataOperationFromDefaultNs() {
t := msuite.T()
gc, cleanup, err := msuite.dc.Client()
require.NoError(t, err)
defer cleanup()

hc, err := msuite.dc.HTTPClient()
require.NoError(t, err)

require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))

require.NoError(t, gc.DropAll())
require.NoError(t, hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))

nss := []uint64{}
for i := 1; i <= 2; i++ {
ns, err := hc.AddNamespace()
require.NoError(t, err)
nss = append(nss, ns)
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))

addData(t, gc)
}

// Drop data from default namespace
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))

require.NoError(t, gc.Alter(context.Background(), &api.Operation{DropOp: api.Operation_DATA}))

// verify here that login into the namespace should not fail
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))

for _, ns := range nss {
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, ns))
query := `{
q(func: has(name)) {
count(uid)
}
}`
resp, err := gc.Query(query)
require.NoError(t, err)
require.Contains(t, string(resp.Json), `"count":4`)
}
}

0 comments on commit 25ae577

Please sign in to comment.