-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
40 lines (34 loc) · 1.08 KB
/
admin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package mongo
import (
"fmt"
"os"
"gopkg.in/mgo.v2"
)
// AdminUser is the name of the user that is initially created in mongo.
const AdminUser = "admin"
var (
processSignal = (*os.Process).Signal
)
// SetAdminMongoPassword sets the administrative password
// to access a mongo database. If the password is non-empty,
// all subsequent attempts to access the database must
// be authorized; otherwise no authorization is required.
func SetAdminMongoPassword(session *mgo.Session, user, password string) error {
admin := session.DB("admin")
if password != "" {
if err := admin.UpsertUser(&mgo.User{
Username: user,
Password: password,
Roles: []mgo.Role{mgo.RoleDBAdminAny, mgo.RoleUserAdminAny, mgo.RoleClusterAdmin, mgo.RoleReadWriteAny},
}); err != nil {
return fmt.Errorf("cannot set admin password: %v", err)
}
} else {
if err := admin.RemoveUser(user); err != nil && err != mgo.ErrNotFound {
return fmt.Errorf("cannot disable admin password: %v", err)
}
}
return nil
}