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

feat(multi-tenancy): Add mutation for guardian of galaxy to reset password of users of any namespaces #7418

Merged
merged 5 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,61 @@ type existingGQLSchemaQryResp struct {
ExistingGQLSchema []graphQLSchemaNode `json:"ExistingGQLSchema"`
}

func (s *Server) ResetPassword(ctx context.Context, ns uint64, id, password string) error {
if err := AuthGuardianOfTheGalaxy(ctx); err != nil {
return errors.Wrapf(err, "Reset password got error:")
}

query := fmt.Sprintf(`{
x as updateUser(func: type(dgraph.type.User)) @filter(eq(dgraph.xid, "%s")) {
uid
}
}`, id)

userNQuads := []*api.NQuad{
{
Subject: "uid(x)",
Predicate: "dgraph.password",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: password}},
},
}
req := &Request{
req: &api.Request{
CommitNow: true,
Query: query,
Mutations: []*api.Mutation{
{
Set: userNQuads,
Cond: "@if(gt(len(x), 0))",
},
},
},
doAuth: NoAuthorize,
}
ctx = x.AttachNamespace(ctx, ns)
resp, err := (&Server{}).doQuery(ctx, req)
if err != nil {
return errors.Wrapf(err, "while upserting user with id %s", x.GrootId)
}

type userNode struct {
Uid string `json:"uid"`
}

type userQryResp struct {
User []userNode `json:"updateUser"`
}
var userResp userQryResp
if err := json.Unmarshal(resp.GetJson(), &userResp); err != nil {
return errors.Wrap(err, "Reset password failed with error")
}

if len(userResp.User) == 0 {
return errors.New("Failed to reset password, user doesn't exist")
}
return nil
}

func (s *Server) CreateNamespace(ctx context.Context) (uint64, error) {
ctx = x.AttachJWTNamespace(ctx)
glog.V(2).Info("Got create namespace request from namespace: ", x.ExtractNamespace(ctx))
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ github.com/dgraph-io/badger v1.6.0 h1:DshxFxZWXUcO0xX476VJC07Xsr6ZCBVRHKZ93Oh7Ev
github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
github.com/dgraph-io/badger/v3 v3.0.0-20210208122220-162b5787192b h1:nD2CjktZ78EClTWWwFhMvVuBxYunKEFLClNkFHGr+aU=
github.com/dgraph-io/badger/v3 v3.0.0-20210208122220-162b5787192b/go.mod h1:ag1DYFcc5xVWtMbbwnl9ESgk1dE2ukWO4hdvzENQnAw=
github.com/dgraph-io/dgo/v200 v200.0.0-20210208072308-4dd991b9b20e h1:3K0Wg/IMUTp4vyCRyD5SlmCYbmVxAz5o6eJvSiS72Gs=
github.com/dgraph-io/dgo/v200 v200.0.0-20210208072308-4dd991b9b20e/go.mod h1:ky1IOcEAlOxmk89KxXGECgRAEkdJrNHVymvCmixaVuM=
github.com/dgraph-io/dgo/v200 v200.0.0-20210208110130-c589adec3d8f h1:XZRsTllGQWUu0SpAb8mGW9motF0KmD7f6UEKa7y2grE=
github.com/dgraph-io/dgo/v200 v200.0.0-20210208110130-c589adec3d8f/go.mod h1:ky1IOcEAlOxmk89KxXGECgRAEkdJrNHVymvCmixaVuM=
github.com/dgraph-io/gqlgen v0.13.2 h1:TNhndk+eHKj5qE7BenKKSYdSIdOGhLqxR1rCiMso9KM=
Expand Down
1 change: 1 addition & 0 deletions graphql/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ func newAdminResolverFactory() resolve.ResolverFactory {
"draining": resolveDraining,
"export": resolveExport,
"login": resolveLogin,
"resetPassword": resolveResetPassword,
"restore": resolveRestore,
"shutdown": resolveShutdown,
}
Expand Down
12 changes: 12 additions & 0 deletions graphql/admin/endpoints_ee.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,12 @@ const adminTypes = `
namespaceId: Int
message: String
}

type ResetPasswordPayload {
userId: String
message: String
namespace: Int
}
`

const adminMutations = `
Expand Down Expand Up @@ -478,6 +484,12 @@ const adminMutations = `
Delete a namespace.
"""
deleteNamespace(input: NamespaceInput!): NamespacePayload

"""
Reset password can only be used by the Guardians of the galaxy to reset password of
any user in any namespace.
"""
resetPassword(userId: String, password: String, namespace: Int): ResetPasswordPayload
`

const adminQueries = `
Expand Down
74 changes: 74 additions & 0 deletions graphql/admin/reset_password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2020 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 admin

import (
"context"
"encoding/json"
"strconv"

"github.com/dgraph-io/dgraph/edgraph"
"github.com/dgraph-io/dgraph/graphql/resolve"
"github.com/dgraph-io/dgraph/graphql/schema"
"github.com/golang/glog"
)

type passwordInput struct {
UserID string
Password string
Namespace uint64
}

func resolveResetPassword(ctx context.Context, m schema.Mutation) (*resolve.Resolved, bool) {
glog.Info("Got reset password request")

in := getPasswordInput(m)
err := (&edgraph.Server{}).ResetPassword(ctx, in.Namespace, in.UserID, in.Password)
if err != nil {
return resolve.EmptyResult(m, err), false
}

return resolve.DataResult(
m,
map[string]interface{}{
m.Name(): map[string]interface{}{
"userId": in.UserID,
"message": "Reset password is successful",
"namespace": json.Number(strconv.Itoa(int(in.Namespace))),
},
},
nil,
), true

}

func getPasswordInput(m schema.Mutation) *passwordInput {
var input passwordInput

input.UserID, _ = m.ArgValue("userId").(string)
input.Password, _ = m.ArgValue("password").(string)

b, err := json.Marshal(m.ArgValue("namespace"))
if err != nil {
return nil
}

if err = json.Unmarshal(b, &input.Namespace); err != nil {
return nil
}
return &input
}