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
|
|
|
"crypto/tls"
|
2017-12-05 20:31:01 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2017-05-11 21:38:54 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2017-05-11 21:38:54 +00:00
|
|
|
"github.com/hashicorp/vault/plugins/helper/database/connutil"
|
2018-03-21 19:05:56 +00:00
|
|
|
"github.com/hashicorp/vault/plugins/helper/database/dbutil"
|
2017-05-11 21:38:54 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
|
|
|
"gopkg.in/mgo.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// mongoDBConnectionProducer implements ConnectionProducer and provides an
|
|
|
|
// interface for databases to make connections.
|
|
|
|
type mongoDBConnectionProducer struct {
|
|
|
|
ConnectionURL string `json:"connection_url" structs:"connection_url" mapstructure:"connection_url"`
|
2017-12-05 20:31:01 +00:00
|
|
|
WriteConcern string `json:"write_concern" structs:"write_concern" mapstructure:"write_concern"`
|
2018-03-21 19:05:56 +00:00
|
|
|
Username string `json:"username" structs:"username" mapstructure:"username"`
|
|
|
|
Password string `json:"password" structs:"password" mapstructure:"password"`
|
2017-05-11 21:38:54 +00:00
|
|
|
|
|
|
|
Initialized bool
|
2018-03-21 19:05:56 +00:00
|
|
|
RawConfig map[string]interface{}
|
2017-05-11 21:38:54 +00:00
|
|
|
Type string
|
|
|
|
session *mgo.Session
|
2017-12-05 20:31:01 +00:00
|
|
|
safe *mgo.Safe
|
2017-05-11 21:38:54 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
func (c *mongoDBConnectionProducer) Initialize(ctx context.Context, conf map[string]interface{}, verifyConnection bool) error {
|
2018-03-21 19:05:56 +00:00
|
|
|
_, err := c.Init(ctx, conf, verifyConnection)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize parses connection configuration.
|
|
|
|
func (c *mongoDBConnectionProducer) Init(ctx context.Context, conf map[string]interface{}, verifyConnection bool) (map[string]interface{}, error) {
|
2017-05-11 21:38:54 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
c.RawConfig = conf
|
|
|
|
|
2017-06-15 01:59:27 +00:00
|
|
|
err := mapstructure.WeakDecode(conf, c)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
2018-03-21 19:05:56 +00:00
|
|
|
return nil, err
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.ConnectionURL) == 0 {
|
2018-03-21 19:05:56 +00:00
|
|
|
return nil, fmt.Errorf("connection_url cannot be empty")
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
c.ConnectionURL = dbutil.QueryHelper(c.ConnectionURL, map[string]string{
|
|
|
|
"username": c.Username,
|
|
|
|
"password": c.Password,
|
|
|
|
})
|
|
|
|
|
2017-12-05 20:31:01 +00:00
|
|
|
if c.WriteConcern != "" {
|
|
|
|
input := c.WriteConcern
|
|
|
|
|
|
|
|
// Try to base64 decode the input. If successful, consider the decoded
|
|
|
|
// value as input.
|
|
|
|
inputBytes, err := base64.StdEncoding.DecodeString(input)
|
|
|
|
if err == nil {
|
|
|
|
input = string(inputBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
concern := &mgo.Safe{}
|
|
|
|
err = json.Unmarshal([]byte(input), concern)
|
|
|
|
if err != nil {
|
2018-03-21 19:05:56 +00:00
|
|
|
return nil, errwrap.Wrapf("error mashalling write_concern: {{err}}", err)
|
2017-12-05 20:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Guard against empty, non-nil mgo.Safe object; we don't want to pass that
|
|
|
|
// into mgo.SetSafe in Connection().
|
|
|
|
if (mgo.Safe{} == *concern) {
|
2018-03-21 19:05:56 +00:00
|
|
|
return nil, fmt.Errorf("provided write_concern values did not map to any mgo.Safe fields")
|
2017-12-05 20:31:01 +00:00
|
|
|
}
|
|
|
|
c.safe = concern
|
|
|
|
}
|
|
|
|
|
2017-05-11 21:38:54 +00:00
|
|
|
// Set initialized to true at this point since all fields are set,
|
|
|
|
// and the connection can be established at a later time.
|
|
|
|
c.Initialized = true
|
|
|
|
|
|
|
|
if verifyConnection {
|
2017-12-14 22:03:11 +00:00
|
|
|
if _, err := c.Connection(ctx); err != nil {
|
2018-03-21 19:05:56 +00:00
|
|
|
return nil, errwrap.Wrapf("error verifying connection: {{err}}", err)
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.session.Ping(); err != nil {
|
2018-03-21 19:05:56 +00:00
|
|
|
return nil, errwrap.Wrapf("error verifying connection: {{err}}", err)
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
return conf, nil
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Connection creates or returns an existing a database connection. If the session fails
|
2018-01-08 17:42:05 +00:00
|
|
|
// on a ping check, the session will be closed and then re-created.
|
2017-12-14 22:03:11 +00:00
|
|
|
func (c *mongoDBConnectionProducer) Connection(_ context.Context) (interface{}, error) {
|
2017-05-11 21:38:54 +00:00
|
|
|
if !c.Initialized {
|
|
|
|
return nil, connutil.ErrNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.session != nil {
|
2017-12-19 15:11:04 +00:00
|
|
|
if err := c.session.Ping(); err == nil {
|
|
|
|
return c.session, nil
|
|
|
|
}
|
|
|
|
c.session.Close()
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dialInfo, err := parseMongoURL(c.ConnectionURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.session, err = mgo.DialWithInfo(dialInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-05 20:31:01 +00:00
|
|
|
|
|
|
|
if c.safe != nil {
|
|
|
|
c.session.SetSafe(c.safe)
|
|
|
|
}
|
|
|
|
|
2017-05-11 21:38:54 +00:00
|
|
|
c.session.SetSyncTimeout(1 * time.Minute)
|
|
|
|
c.session.SetSocketTimeout(1 * time.Minute)
|
|
|
|
|
2018-01-08 17:42:05 +00:00
|
|
|
return c.session, nil
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close terminates the database connection.
|
|
|
|
func (c *mongoDBConnectionProducer) Close() error {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if c.session != nil {
|
|
|
|
c.session.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.session = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseMongoURL(rawURL string) (*mgo.DialInfo, error) {
|
|
|
|
url, err := url.Parse(rawURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info := mgo.DialInfo{
|
|
|
|
Addrs: strings.Split(url.Host, ","),
|
|
|
|
Database: strings.TrimPrefix(url.Path, "/"),
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
if url.User != nil {
|
|
|
|
info.Username = url.User.Username()
|
|
|
|
info.Password, _ = url.User.Password()
|
|
|
|
}
|
|
|
|
|
|
|
|
query := url.Query()
|
|
|
|
for key, values := range query {
|
|
|
|
var value string
|
|
|
|
if len(values) > 0 {
|
|
|
|
value = values[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "authSource":
|
|
|
|
info.Source = value
|
|
|
|
case "authMechanism":
|
|
|
|
info.Mechanism = value
|
|
|
|
case "gssapiServiceName":
|
|
|
|
info.Service = value
|
|
|
|
case "replicaSet":
|
|
|
|
info.ReplicaSetName = value
|
|
|
|
case "maxPoolSize":
|
|
|
|
poolLimit, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("bad value for maxPoolSize: " + value)
|
|
|
|
}
|
|
|
|
info.PoolLimit = poolLimit
|
|
|
|
case "ssl":
|
|
|
|
// Unfortunately, mgo doesn't support the ssl parameter in its MongoDB URI parsing logic, so we have to handle that
|
|
|
|
// ourselves. See https://github.com/go-mgo/mgo/issues/84
|
|
|
|
ssl, err := strconv.ParseBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("bad value for ssl: " + value)
|
|
|
|
}
|
|
|
|
if ssl {
|
|
|
|
info.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
|
|
|
|
return tls.Dial("tcp", addr.String(), &tls.Config{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "connect":
|
|
|
|
if value == "direct" {
|
|
|
|
info.Direct = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if value == "replicaSet" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unsupported connection URL option: " + key + "=" + value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &info, nil
|
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
|
|
|
func (c *mongoDBConnectionProducer) secretValues() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
c.Password: "[password]",
|
|
|
|
}
|
|
|
|
}
|