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
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
mgo "gopkg.in/mgo.v2"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
|
2019-04-15 18:10:07 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/dbplugin"
|
2018-07-11 21:49:13 +00:00
|
|
|
"github.com/ory/dockertest"
|
2017-05-11 21:38:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const testMongoDBRole = `{ "db": "admin", "roles": [ { "role": "readWrite" } ] }`
|
|
|
|
|
2017-12-05 20:31:01 +00:00
|
|
|
const testMongoDBWriteConcern = `{ "wmode": "majority", "wtimeout": 5000 }`
|
|
|
|
|
2017-05-11 21:38:54 +00:00
|
|
|
func prepareMongoDBTestContainer(t *testing.T) (cleanup func(), retURL string) {
|
|
|
|
if os.Getenv("MONGODB_URL") != "" {
|
|
|
|
return func() {}, os.Getenv("MONGODB_URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resource, err := pool.Run("mongo", "latest", []string{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start local mongo docker container: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = func() {
|
|
|
|
err := pool.Purge(resource)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to cleanup local container: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retURL = fmt.Sprintf("mongodb://localhost:%s", resource.GetPort("27017/tcp"))
|
|
|
|
|
|
|
|
// exponential backoff-retry
|
|
|
|
if err = pool.Retry(func() error {
|
|
|
|
var err error
|
|
|
|
dialInfo, err := parseMongoURL(retURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := mgo.DialWithInfo(dialInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-26 15:28:58 +00:00
|
|
|
defer session.Close()
|
2017-05-11 21:38:54 +00:00
|
|
|
session.SetSyncTimeout(1 * time.Minute)
|
|
|
|
session.SetSocketTimeout(1 * time.Minute)
|
|
|
|
return session.Ping()
|
|
|
|
}); err != nil {
|
2018-08-07 16:56:33 +00:00
|
|
|
cleanup()
|
2017-05-11 21:38:54 +00:00
|
|
|
t.Fatalf("Could not connect to mongo docker container: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMongoDB_Initialize(t *testing.T) {
|
|
|
|
cleanup, connURL := prepareMongoDBTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
if !db.Initialized {
|
2017-05-11 21:38:54 +00:00
|
|
|
t.Fatal("Database should be initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMongoDB_CreateUser(t *testing.T) {
|
|
|
|
cleanup, connURL := prepareMongoDBTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-12-05 20:31:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{testMongoDBRole},
|
2017-12-05 20:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
|
2017-12-05 20:31:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMongoDB_CreateUser_writeConcern(t *testing.T) {
|
|
|
|
cleanup, connURL := prepareMongoDBTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
"write_concern": testMongoDBWriteConcern,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{testMongoDBRole},
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMongoDB_RevokeUser(t *testing.T) {
|
|
|
|
cleanup, connURL := prepareMongoDBTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{testMongoDBRole},
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := testCredsExist(t, connURL, username, password); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Test default revocation statement
|
2017-12-14 22:03:11 +00:00
|
|
|
err = db.RevokeUser(context.Background(), statements, username)
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = testCredsExist(t, connURL, username, password); err == nil {
|
|
|
|
t.Fatal("Credentials were not revoked")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCredsExist(t testing.TB, connURL, username, password string) error {
|
|
|
|
connURL = strings.Replace(connURL, "mongodb://", fmt.Sprintf("mongodb://%s:%s@", username, password), 1)
|
|
|
|
dialInfo, err := parseMongoURL(connURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := mgo.DialWithInfo(dialInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
session.SetSyncTimeout(1 * time.Minute)
|
|
|
|
session.SetSocketTimeout(1 * time.Minute)
|
|
|
|
return session.Ping()
|
|
|
|
}
|