2017-05-11 21:38:54 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
2017-12-14 22:03:11 +00:00
|
|
|
"context"
|
2018-03-21 19:05:56 +00:00
|
|
|
"errors"
|
2017-08-31 20:50:26 +00:00
|
|
|
"io"
|
|
|
|
"strings"
|
2017-05-11 21:38:54 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
|
2017-08-31 20:50:26 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2017-05-11 21:38:54 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
|
|
|
|
"github.com/hashicorp/vault/plugins"
|
|
|
|
"github.com/hashicorp/vault/plugins/helper/database/credsutil"
|
|
|
|
"github.com/hashicorp/vault/plugins/helper/database/dbutil"
|
|
|
|
"gopkg.in/mgo.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const mongoDBTypeName = "mongodb"
|
|
|
|
|
|
|
|
// MongoDB is an implementation of Database interface
|
|
|
|
type MongoDB struct {
|
2018-03-21 19:05:56 +00:00
|
|
|
*mongoDBConnectionProducer
|
2017-05-11 21:38:54 +00:00
|
|
|
credsutil.CredentialsProducer
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
var _ dbplugin.Database = &MongoDB{}
|
|
|
|
|
2017-05-11 21:38:54 +00:00
|
|
|
// New returns a new MongoDB instance
|
|
|
|
func New() (interface{}, error) {
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
dbType := dbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.secretValues)
|
|
|
|
return dbType, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func new() *MongoDB {
|
2017-05-11 21:38:54 +00:00
|
|
|
connProducer := &mongoDBConnectionProducer{}
|
|
|
|
connProducer.Type = mongoDBTypeName
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
credsProducer := &credsutil.SQLCredentialsProducer{
|
|
|
|
DisplayNameLen: 15,
|
|
|
|
RoleNameLen: 15,
|
|
|
|
UsernameLen: 100,
|
|
|
|
Separator: "-",
|
|
|
|
}
|
2017-05-11 21:38:54 +00:00
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
return &MongoDB{
|
|
|
|
mongoDBConnectionProducer: connProducer,
|
|
|
|
CredentialsProducer: credsProducer,
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run instantiates a MongoDB object, and runs the RPC server for the plugin
|
|
|
|
func Run(apiTLSConfig *api.TLSConfig) error {
|
|
|
|
dbType, err := New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins.Serve(dbType.(*MongoDB), apiTLSConfig)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the TypeName for this backend
|
|
|
|
func (m *MongoDB) Type() (string, error) {
|
|
|
|
return mongoDBTypeName, nil
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
func (m *MongoDB) getConnection(ctx context.Context) (*mgo.Session, error) {
|
|
|
|
session, err := m.Connection(ctx)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.(*mgo.Session), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUser generates the username/password on the underlying secret backend as instructed by
|
|
|
|
// the CreationStatement provided. The creation statement is a JSON blob that has a db value,
|
|
|
|
// and an array of roles that accepts a role, and an optional db value pair. This array will
|
|
|
|
// be normalized the format specified in the mongoDB docs:
|
|
|
|
// https://docs.mongodb.com/manual/reference/command/createUser/#dbcmd.createUser
|
|
|
|
//
|
|
|
|
// JSON Example:
|
|
|
|
// { "db": "admin", "roles": [{ "role": "readWrite" }, {"role": "read", "db": "foo"}] }
|
2017-12-14 22:03:11 +00:00
|
|
|
func (m *MongoDB) CreateUser(ctx context.Context, statements dbplugin.Statements, usernameConfig dbplugin.UsernameConfig, expiration time.Time) (username string, password string, err error) {
|
2017-05-11 21:38:54 +00:00
|
|
|
// Grab the lock
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
statements = dbutil.StatementCompatibilityHelper(statements)
|
|
|
|
|
|
|
|
if len(statements.Creation) == 0 {
|
2017-05-11 21:38:54 +00:00
|
|
|
return "", "", dbutil.ErrEmptyCreationStatement
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
session, err := m.getConnection(ctx)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
username, err = m.GenerateUsername(usernameConfig)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
password, err = m.GeneratePassword()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal statements.CreationStatements into mongodbRoles
|
|
|
|
var mongoCS mongoDBStatement
|
2018-03-21 19:05:56 +00:00
|
|
|
err = json.Unmarshal([]byte(statements.Creation[0]), &mongoCS)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to "admin" if no db provided
|
|
|
|
if mongoCS.DB == "" {
|
|
|
|
mongoCS.DB = "admin"
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mongoCS.Roles) == 0 {
|
|
|
|
return "", "", fmt.Errorf("roles array is required in creation statement")
|
|
|
|
}
|
|
|
|
|
|
|
|
createUserCmd := createUserCommand{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
Roles: mongoCS.Roles.toStandardRolesArray(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = session.DB(mongoCS.DB).Run(createUserCmd, nil)
|
2017-08-31 20:50:26 +00:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
case err == io.EOF, strings.Contains(err.Error(), "EOF"):
|
2018-01-08 17:42:05 +00:00
|
|
|
// Call getConnection to reset and retry query if we get an EOF error on first attempt.
|
2017-12-14 22:03:11 +00:00
|
|
|
session, err := m.getConnection(ctx)
|
2017-08-31 20:50:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
err = session.DB(mongoCS.DB).Run(createUserCmd, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
default:
|
2017-05-11 21:38:54 +00:00
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return username, password, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenewUser is not supported on MongoDB, so this is a no-op.
|
2017-12-14 22:03:11 +00:00
|
|
|
func (m *MongoDB) RenewUser(ctx context.Context, statements dbplugin.Statements, username string, expiration time.Time) error {
|
2017-05-11 21:38:54 +00:00
|
|
|
// NOOP
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// RevokeUser drops the specified user from the authentication database. If none is provided
|
2017-05-11 21:38:54 +00:00
|
|
|
// in the revocation statement, the default "admin" authentication database will be assumed.
|
2017-12-14 22:03:11 +00:00
|
|
|
func (m *MongoDB) RevokeUser(ctx context.Context, statements dbplugin.Statements, username string) error {
|
2018-03-21 19:05:56 +00:00
|
|
|
statements = dbutil.StatementCompatibilityHelper(statements)
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
session, err := m.getConnection(ctx)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no revocation statements provided, pass in empty JSON
|
2018-03-21 19:05:56 +00:00
|
|
|
var revocationStatement string
|
|
|
|
switch len(statements.Revocation) {
|
|
|
|
case 0:
|
2017-05-11 21:38:54 +00:00
|
|
|
revocationStatement = `{}`
|
2018-03-21 19:05:56 +00:00
|
|
|
case 1:
|
|
|
|
revocationStatement = statements.Revocation[0]
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("expected 0 or 1 revocation statements, got %d", len(statements.Revocation))
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal revocation statements into mongodbRoles
|
|
|
|
var mongoCS mongoDBStatement
|
|
|
|
err = json.Unmarshal([]byte(revocationStatement), &mongoCS)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
db := mongoCS.DB
|
|
|
|
// If db is not specified, use the default authenticationDatabase "admin"
|
|
|
|
if db == "" {
|
|
|
|
db = "admin"
|
|
|
|
}
|
|
|
|
|
|
|
|
err = session.DB(db).RemoveUser(username)
|
2017-08-31 20:50:26 +00:00
|
|
|
switch {
|
|
|
|
case err == nil, err == mgo.ErrNotFound:
|
|
|
|
case err == io.EOF, strings.Contains(err.Error(), "EOF"):
|
2018-03-21 19:05:56 +00:00
|
|
|
if err := m.Close(); err != nil {
|
2017-08-31 20:50:26 +00:00
|
|
|
return errwrap.Wrapf("error closing EOF'd mongo connection: {{err}}", err)
|
|
|
|
}
|
2017-12-14 22:03:11 +00:00
|
|
|
session, err := m.getConnection(ctx)
|
2017-08-31 20:50:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = session.DB(db).RemoveUser(username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
2017-05-11 21:38:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
|
|
|
// RotateRootCredentials is not currently supported on MongoDB
|
|
|
|
func (m *MongoDB) RotateRootCredentials(ctx context.Context, statements []string) (map[string]interface{}, error) {
|
|
|
|
return nil, errors.New("root credentaion rotation is not currently implemented in this database secrets engine")
|
|
|
|
}
|