github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/mongodb/users.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package mongodb 21 22 import ( 23 "context" 24 25 "github.com/pkg/errors" 26 "go.mongodb.org/mongo-driver/bson" 27 "go.mongodb.org/mongo-driver/mongo" 28 ) 29 30 func CreateUser(ctx context.Context, client *mongo.Client, user, pwd string, roles ...map[string]interface{}) error { 31 resp := OKResponse{} 32 33 res := client.Database("admin").RunCommand(ctx, bson.D{ 34 {Key: "createUser", Value: user}, 35 {Key: "pwd", Value: pwd}, 36 {Key: "roles", Value: roles}, 37 }) 38 if res.Err() != nil { 39 return errors.Wrap(res.Err(), "failed to create user") 40 } 41 42 err := res.Decode(&resp) 43 if err != nil { 44 return errors.Wrap(err, "failed to decode response") 45 } 46 47 if resp.OK != 1 { 48 return errors.Errorf("mongo says: %s", resp.Errmsg) 49 } 50 51 return nil 52 } 53 54 func GetUser(ctx context.Context, client *mongo.Client, userName string) (*User, error) { 55 resp := UsersInfo{} 56 res := client.Database("admin").RunCommand(ctx, bson.D{{Key: "usersInfo", Value: userName}}) 57 if res.Err() != nil { 58 return nil, errors.Wrap(res.Err(), "run command") 59 } 60 61 err := res.Decode(&resp) 62 if err != nil { 63 return nil, errors.Wrap(err, "failed to decode response") 64 } 65 if resp.OK != 1 { 66 return nil, errors.Errorf("mongo says: %s", resp.Errmsg) 67 } 68 if len(resp.Users) == 0 { 69 return nil, nil 70 } 71 return &resp.Users[0], nil 72 } 73 74 func UpdateUserRoles(ctx context.Context, client *mongo.Client, userName string, roles []map[string]interface{}) error { 75 return client.Database("admin").RunCommand(ctx, bson.D{{Key: "updateUser", Value: userName}, {Key: "roles", Value: roles}}).Err() 76 } 77 78 // UpdateUserPass updates user's password 79 func UpdateUserPass(ctx context.Context, client *mongo.Client, name, pass string) error { 80 return client.Database("admin").RunCommand(ctx, bson.D{{Key: "updateUser", Value: name}, {Key: "pwd", Value: pass}}).Err() 81 } 82 83 // DropUser delete user 84 func DropUser(ctx context.Context, client *mongo.Client, userName string) error { 85 user, err := GetUser(ctx, client, userName) 86 if err != nil { 87 return errors.Wrap(err, "get user") 88 } 89 90 if user == nil { 91 return errors.New(userName + " user not exists") 92 } 93 94 err = client.Database("admin").RunCommand(ctx, bson.D{{Key: "dropUser", Value: userName}}).Err() 95 return errors.Wrap(err, "drop user") 96 }