2017-05-11 21:38:54 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
2017-12-14 22:03:11 +00:00
|
|
|
"context"
|
2017-05-11 21:38:54 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-04-28 00:20:25 +00:00
|
|
|
"io"
|
|
|
|
"strings"
|
2017-05-11 21:38:54 +00:00
|
|
|
|
2019-04-15 18:10:07 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/helper/dbutil"
|
2021-02-11 21:07:58 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/template"
|
|
|
|
|
|
|
|
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
|
2020-10-02 21:35:11 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2020-01-24 08:32:47 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2020-10-02 21:35:11 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
2020-01-24 08:32:47 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo/writeconcern"
|
|
|
|
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
|
2017-05-11 21:38:54 +00:00
|
|
|
)
|
|
|
|
|
2021-02-11 21:07:58 +00:00
|
|
|
const (
|
|
|
|
mongoDBTypeName = "mongodb"
|
|
|
|
|
|
|
|
defaultUserNameTemplate = `{{ printf "v-%s-%s-%s-%s" (.DisplayName | truncate 15) (.RoleName | truncate 15) (random 20) (unix_time) | truncate 100 }}`
|
|
|
|
)
|
2017-05-11 21:38:54 +00:00
|
|
|
|
|
|
|
// MongoDB is an implementation of Database interface
|
|
|
|
type MongoDB struct {
|
2018-03-21 19:05:56 +00:00
|
|
|
*mongoDBConnectionProducer
|
2021-02-11 21:07:58 +00:00
|
|
|
|
|
|
|
usernameProducer template.StringTemplate
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
var _ dbplugin.Database = &MongoDB{}
|
2017-12-14 22:03:11 +00:00
|
|
|
|
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()
|
2020-10-15 19:20:12 +00:00
|
|
|
dbType := dbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.secretValues)
|
2018-03-21 19:05:56 +00:00
|
|
|
return dbType, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func new() *MongoDB {
|
2020-10-02 21:35:11 +00:00
|
|
|
connProducer := &mongoDBConnectionProducer{
|
|
|
|
Type: mongoDBTypeName,
|
2017-06-06 13:49:49 +00:00
|
|
|
}
|
2017-05-11 21:38:54 +00:00
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
return &MongoDB{
|
|
|
|
mongoDBConnectionProducer: connProducer,
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the TypeName for this backend
|
|
|
|
func (m *MongoDB) Type() (string, error) {
|
|
|
|
return mongoDBTypeName, nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:32:47 +00:00
|
|
|
func (m *MongoDB) getConnection(ctx context.Context) (*mongo.Client, error) {
|
|
|
|
client, err := m.Connection(ctx)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:32:47 +00:00
|
|
|
return client.(*mongo.Client), nil
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m *MongoDB) Initialize(ctx context.Context, req dbplugin.InitializeRequest) (dbplugin.InitializeResponse, error) {
|
2017-05-11 21:38:54 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-10-02 21:35:11 +00:00
|
|
|
m.RawConfig = req.Config
|
2018-03-21 19:05:56 +00:00
|
|
|
|
2021-02-11 21:07:58 +00:00
|
|
|
usernameTemplate, err := strutil.GetString(req.Config, "username_template")
|
|
|
|
if err != nil {
|
|
|
|
return dbplugin.InitializeResponse{}, fmt.Errorf("failed to retrieve username_template: %w", err)
|
|
|
|
}
|
|
|
|
if usernameTemplate == "" {
|
|
|
|
usernameTemplate = defaultUserNameTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
up, err := template.NewTemplate(template.Template(usernameTemplate))
|
|
|
|
if err != nil {
|
|
|
|
return dbplugin.InitializeResponse{}, fmt.Errorf("unable to initialize username template: %w", err)
|
|
|
|
}
|
|
|
|
m.usernameProducer = up
|
|
|
|
|
|
|
|
_, err = m.usernameProducer.Generate(dbplugin.UsernameMetadata{})
|
|
|
|
if err != nil {
|
|
|
|
return dbplugin.InitializeResponse{}, fmt.Errorf("invalid username template: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mapstructure.WeakDecode(req.Config, m.mongoDBConnectionProducer)
|
2020-10-02 21:35:11 +00:00
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.InitializeResponse{}, err
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 21:35:11 +00:00
|
|
|
if len(m.ConnectionURL) == 0 {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.InitializeResponse{}, fmt.Errorf("connection_url cannot be empty-mongo fail")
|
2020-10-02 21:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeOpts, err := m.getWriteConcern()
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.InitializeResponse{}, err
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 21:35:11 +00:00
|
|
|
authOpts, err := m.getTLSAuth()
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.InitializeResponse{}, err
|
2020-10-02 21:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.clientOptions = options.MergeClientOptions(writeOpts, authOpts)
|
|
|
|
|
|
|
|
// Set initialized to true at this point since all fields are set,
|
|
|
|
// and the connection can be established at a later time.
|
|
|
|
m.Initialized = true
|
|
|
|
|
|
|
|
if req.VerifyConnection {
|
|
|
|
_, err := m.Connection(ctx)
|
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.InitializeResponse{}, fmt.Errorf("failed to verify connection: %w", err)
|
2020-10-02 21:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = m.client.Ping(ctx, readpref.Primary())
|
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.InitializeResponse{}, fmt.Errorf("failed to verify connection: %w", err)
|
2020-10-02 21:35:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
resp := dbplugin.InitializeResponse{
|
2020-10-02 21:35:11 +00:00
|
|
|
Config: req.Config,
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m *MongoDB) NewUser(ctx context.Context, req dbplugin.NewUserRequest) (dbplugin.NewUserResponse, error) {
|
2020-10-02 21:35:11 +00:00
|
|
|
// Grab the lock
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
if len(req.Statements.Commands) == 0 {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.NewUserResponse{}, dbutil.ErrEmptyCreationStatement
|
2020-10-02 21:35:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:07:58 +00:00
|
|
|
username, err := m.usernameProducer.Generate(req.UsernameConfig)
|
2020-10-02 21:35:11 +00:00
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.NewUserResponse{}, err
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal statements.CreationStatements into mongodbRoles
|
|
|
|
var mongoCS mongoDBStatement
|
2020-10-02 21:35:11 +00:00
|
|
|
err = json.Unmarshal([]byte(req.Statements.Commands[0]), &mongoCS)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.NewUserResponse{}, err
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default to "admin" if no db provided
|
|
|
|
if mongoCS.DB == "" {
|
|
|
|
mongoCS.DB = "admin"
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mongoCS.Roles) == 0 {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.NewUserResponse{}, fmt.Errorf("roles array is required in creation statement")
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createUserCmd := createUserCommand{
|
|
|
|
Username: username,
|
2020-10-02 21:35:11 +00:00
|
|
|
Password: req.Password,
|
2017-05-11 21:38:54 +00:00
|
|
|
Roles: mongoCS.Roles.toStandardRolesArray(),
|
|
|
|
}
|
|
|
|
|
2020-04-28 00:20:25 +00:00
|
|
|
if err := m.runCommandWithRetry(ctx, mongoCS.DB, createUserCmd); err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.NewUserResponse{}, err
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
resp := dbplugin.NewUserResponse{
|
2020-10-02 21:35:11 +00:00
|
|
|
Username: username,
|
|
|
|
}
|
|
|
|
return resp, nil
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m *MongoDB) UpdateUser(ctx context.Context, req dbplugin.UpdateUserRequest) (dbplugin.UpdateUserResponse, error) {
|
2020-10-02 21:35:11 +00:00
|
|
|
if req.Password != nil {
|
|
|
|
err := m.changeUserPassword(ctx, req.Username, req.Password.NewPassword)
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.UpdateUserResponse{}, err
|
2020-10-02 21:35:11 +00:00
|
|
|
}
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.UpdateUserResponse{}, nil
|
2020-10-02 21:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MongoDB) changeUserPassword(ctx context.Context, username, password string) error {
|
2019-07-05 18:57:01 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-10-02 21:35:11 +00:00
|
|
|
connURL := m.getConnectionURL()
|
|
|
|
cs, err := connstring.Parse(connURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-05 18:57:01 +00:00
|
|
|
|
2020-10-02 21:35:11 +00:00
|
|
|
// Currently doesn't support custom statements for changing the user's password
|
2020-01-24 08:32:47 +00:00
|
|
|
changeUserCmd := &updateUserCommand{
|
2019-07-05 18:57:01 +00:00
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
|
2020-10-02 21:35:11 +00:00
|
|
|
database := cs.Database
|
|
|
|
if username == m.Username || database == "" {
|
|
|
|
database = "admin"
|
2019-07-05 18:57:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 21:35:11 +00:00
|
|
|
err = m.runCommandWithRetry(ctx, database, changeUserCmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-05 18:57:01 +00:00
|
|
|
|
2017-05-11 21:38:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m *MongoDB) DeleteUser(ctx context.Context, req dbplugin.DeleteUserRequest) (dbplugin.DeleteUserResponse, error) {
|
2018-10-04 13:51:08 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2017-05-11 21:38:54 +00:00
|
|
|
// If no revocation statements provided, pass in empty JSON
|
2018-03-21 19:05:56 +00:00
|
|
|
var revocationStatement string
|
2020-10-02 21:35:11 +00:00
|
|
|
switch len(req.Statements.Commands) {
|
2018-03-21 19:05:56 +00:00
|
|
|
case 0:
|
2017-05-11 21:38:54 +00:00
|
|
|
revocationStatement = `{}`
|
2018-03-21 19:05:56 +00:00
|
|
|
case 1:
|
2020-10-02 21:35:11 +00:00
|
|
|
revocationStatement = req.Statements.Commands[0]
|
2018-03-21 19:05:56 +00:00
|
|
|
default:
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, fmt.Errorf("expected 0 or 1 revocation statements, got %d", len(req.Statements.Commands))
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal revocation statements into mongodbRoles
|
|
|
|
var mongoCS mongoDBStatement
|
2020-04-28 00:20:25 +00:00
|
|
|
err := json.Unmarshal([]byte(revocationStatement), &mongoCS)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db := mongoCS.DB
|
|
|
|
// If db is not specified, use the default authenticationDatabase "admin"
|
|
|
|
if db == "" {
|
|
|
|
db = "admin"
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:32:47 +00:00
|
|
|
dropUserCmd := &dropUserCommand{
|
2020-10-02 21:35:11 +00:00
|
|
|
Username: req.Username,
|
2020-01-24 08:32:47 +00:00
|
|
|
WriteConcern: writeconcern.New(writeconcern.WMajority()),
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 21:35:11 +00:00
|
|
|
err = m.runCommandWithRetry(ctx, db, dropUserCmd)
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
2020-01-24 08:32:47 +00:00
|
|
|
|
2020-04-28 00:20:25 +00:00
|
|
|
// runCommandWithRetry runs a command and retries once more if there's a failure
|
|
|
|
// on the first attempt. This should be called with the lock held
|
|
|
|
func (m *MongoDB) runCommandWithRetry(ctx context.Context, db string, cmd interface{}) error {
|
|
|
|
// Get the client
|
|
|
|
client, err := m.getConnection(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run command
|
|
|
|
result := client.Database(db).RunCommand(ctx, cmd, nil)
|
2020-01-24 08:32:47 +00:00
|
|
|
|
2020-04-28 00:20:25 +00:00
|
|
|
// Error check on the first attempt
|
|
|
|
err = result.Err()
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return nil
|
|
|
|
case err == io.EOF, strings.Contains(err.Error(), "EOF"):
|
|
|
|
// Call getConnection to reset and retry query if we get an EOF error on first attempt.
|
|
|
|
client, err = m.getConnection(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result = client.Database(db).RunCommand(ctx, cmd, nil)
|
|
|
|
if err := result.Err(); err != nil {
|
|
|
|
return err
|
2020-01-24 08:32:47 +00:00
|
|
|
}
|
2020-04-28 00:20:25 +00:00
|
|
|
default:
|
|
|
|
return err
|
2020-01-24 08:32:47 +00:00
|
|
|
}
|
2020-04-28 00:20:25 +00:00
|
|
|
|
2020-01-24 08:32:47 +00:00
|
|
|
return nil
|
|
|
|
}
|