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