mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-11-03 20:17:59 +00:00 
			
		
		
		
	* redoing connection handling * a little more cleanup * empty implementation of rotation * updating rotate signature * signature update * updating interfaces again :( * changing back to interface * adding templated url support and rotation for postgres * adding correct username * return updates * updating statements to be a list * adding error sanitizing middleware * fixing log sanitizier * adding postgres rotate test * removing conf from rotate * adding rotate command * adding mysql rotate * finishing up the endpoint in the db backend for rotate * no more structs, just store raw config * fixing tests * adding db instance lock * adding support for statement list in cassandra * wip redoing interface to support BC * adding falllback for Initialize implementation * adding backwards compat for statements * fix tests * fix more tests * fixing up tests, switching to new fields in statements * fixing more tests * adding mssql and mysql * wrapping all the things in middleware, implementing templating for mongodb * wrapping all db servers with error santizer * fixing test * store the name with the db instance * adding rotate to cassandra * adding compatibility translation to both server and plugin * reordering a few things * store the name with the db instance * reordering * adding a few more tests * switch secret values from slice to map * addressing some feedback * reinstate execute plugin after resetting connection * set database connection to closed * switching secret values func to map[string]interface for potential future uses * addressing feedback
		
			
				
	
	
		
			217 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			217 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package mongodb
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"fmt"
 | 
						|
	"os"
 | 
						|
	"testing"
 | 
						|
	"time"
 | 
						|
 | 
						|
	mgo "gopkg.in/mgo.v2"
 | 
						|
 | 
						|
	"strings"
 | 
						|
 | 
						|
	"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
 | 
						|
	dockertest "gopkg.in/ory-am/dockertest.v3"
 | 
						|
)
 | 
						|
 | 
						|
const testMongoDBRole = `{ "db": "admin", "roles": [ { "role": "readWrite" } ] }`
 | 
						|
 | 
						|
const testMongoDBWriteConcern = `{ "wmode": "majority", "wtimeout": 5000 }`
 | 
						|
 | 
						|
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
 | 
						|
		}
 | 
						|
		session.SetSyncTimeout(1 * time.Minute)
 | 
						|
		session.SetSocketTimeout(1 * time.Minute)
 | 
						|
		return session.Ping()
 | 
						|
	}); err != nil {
 | 
						|
		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,
 | 
						|
	}
 | 
						|
 | 
						|
	db := new()
 | 
						|
	_, err := db.Init(context.Background(), connectionDetails, true)
 | 
						|
	if err != nil {
 | 
						|
		t.Fatalf("err: %s", err)
 | 
						|
	}
 | 
						|
 | 
						|
	if !db.Initialized {
 | 
						|
		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,
 | 
						|
	}
 | 
						|
 | 
						|
	db := new()
 | 
						|
	_, err := db.Init(context.Background(), connectionDetails, true)
 | 
						|
	if err != nil {
 | 
						|
		t.Fatalf("err: %s", err)
 | 
						|
	}
 | 
						|
 | 
						|
	statements := dbplugin.Statements{
 | 
						|
		Creation: []string{testMongoDBRole},
 | 
						|
	}
 | 
						|
 | 
						|
	usernameConfig := dbplugin.UsernameConfig{
 | 
						|
		DisplayName: "test",
 | 
						|
		RoleName:    "test",
 | 
						|
	}
 | 
						|
 | 
						|
	username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
 | 
						|
	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,
 | 
						|
	}
 | 
						|
 | 
						|
	db := new()
 | 
						|
	_, err := db.Init(context.Background(), connectionDetails, true)
 | 
						|
	if err != nil {
 | 
						|
		t.Fatalf("err: %s", err)
 | 
						|
	}
 | 
						|
 | 
						|
	statements := dbplugin.Statements{
 | 
						|
		Creation: []string{testMongoDBRole},
 | 
						|
	}
 | 
						|
 | 
						|
	usernameConfig := dbplugin.UsernameConfig{
 | 
						|
		DisplayName: "test",
 | 
						|
		RoleName:    "test",
 | 
						|
	}
 | 
						|
 | 
						|
	username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
 | 
						|
	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,
 | 
						|
	}
 | 
						|
 | 
						|
	db := new()
 | 
						|
	_, err := db.Init(context.Background(), connectionDetails, true)
 | 
						|
	if err != nil {
 | 
						|
		t.Fatalf("err: %s", err)
 | 
						|
	}
 | 
						|
 | 
						|
	statements := dbplugin.Statements{
 | 
						|
		Creation: []string{testMongoDBRole},
 | 
						|
	}
 | 
						|
 | 
						|
	usernameConfig := dbplugin.UsernameConfig{
 | 
						|
		DisplayName: "test",
 | 
						|
		RoleName:    "test",
 | 
						|
	}
 | 
						|
 | 
						|
	username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
 | 
						|
	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)
 | 
						|
	}
 | 
						|
 | 
						|
	// Test default revocation statement
 | 
						|
	err = db.RevokeUser(context.Background(), statements, username)
 | 
						|
	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()
 | 
						|
}
 |