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"
|
2019-04-22 16:26:10 +00:00
|
|
|
"strings"
|
2017-05-11 21:38:54 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/mongodb"
|
2019-04-15 18:10:07 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/dbplugin"
|
2020-01-24 08:32:47 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
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 TestMongoDB_Initialize(t *testing.T) {
|
2019-04-22 16:26:10 +00:00
|
|
|
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
|
2017-05-11 21:38:54 +00:00
|
|
|
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) {
|
2019-04-22 16:26:10 +00:00
|
|
|
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
|
2017-05-11 21:38:54 +00:00
|
|
|
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) {
|
2019-04-22 16:26:10 +00:00
|
|
|
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
|
2017-12-05 20:31:01 +00:00
|
|
|
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) {
|
2019-04-22 16:26:10 +00:00
|
|
|
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
|
2017-05-11 21:38:54 +00:00
|
|
|
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)
|
|
|
|
|
2020-01-24 08:32:47 +00:00
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(connURL))
|
2017-05-11 21:38:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-24 08:32:47 +00:00
|
|
|
return client.Ping(ctx, readpref.Primary())
|
2017-05-11 21:38:54 +00:00
|
|
|
}
|
2019-07-05 18:57:01 +00:00
|
|
|
|
|
|
|
func TestMongoDB_SetCredentials(t *testing.T) {
|
|
|
|
cleanup, connURL := mongodb.PrepareTestContainer(t, "latest")
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// The docker test method PrepareTestContainer defaults to a database "test"
|
|
|
|
// if none is provided
|
|
|
|
connURL = connURL + "/test"
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"connection_url": connURL,
|
|
|
|
}
|
|
|
|
|
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the database user in advance, and test the connection
|
|
|
|
dbUser := "testmongouser"
|
|
|
|
startingPassword := "password"
|
2020-01-24 08:32:47 +00:00
|
|
|
testCreateDBUser(t, connURL, "test", dbUser, startingPassword)
|
2019-07-05 18:57:01 +00:00
|
|
|
if err := testCredsExist(t, connURL, dbUser, startingPassword); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newPassword, err := db.GenerateCredentials(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
usernameConfig := dbplugin.StaticUserConfig{
|
|
|
|
Username: dbUser,
|
|
|
|
Password: newPassword,
|
|
|
|
}
|
|
|
|
|
|
|
|
username, password, err := db.SetCredentials(context.Background(), dbplugin.Statements{}, usernameConfig)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
// confirm the original creds used to set still work (should be the same)
|
|
|
|
if err := testCredsExist(t, connURL, dbUser, newPassword); err != nil {
|
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dbUser != username) || (newPassword != password) {
|
|
|
|
t.Fatalf("username/password mismatch: (%s)/(%s) vs (%s)/(%s)", dbUser, username, newPassword, password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:32:47 +00:00
|
|
|
func testCreateDBUser(t testing.TB, connURL, db, username, password string) {
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(connURL))
|
2019-07-05 18:57:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:32:47 +00:00
|
|
|
createUserCmd := &createUserCommand{
|
2019-07-05 18:57:01 +00:00
|
|
|
Username: username,
|
|
|
|
Password: password,
|
2020-01-24 08:32:47 +00:00
|
|
|
Roles: []interface{}{},
|
2019-07-05 18:57:01 +00:00
|
|
|
}
|
2020-01-24 08:32:47 +00:00
|
|
|
result := client.Database(db).RunCommand(ctx, createUserCmd, nil)
|
|
|
|
if result.Err() != nil {
|
|
|
|
t.Fatal(result.Err())
|
2019-07-05 18:57:01 +00:00
|
|
|
}
|
|
|
|
}
|