mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-31 18:48:08 +00:00 
			
		
		
		
	 1fd46cbcb1
			
		
	
	1fd46cbcb1
	
	
	
		
			
			* Fix the mysql legacy username length * Remove boolean parameter * Add a MySQL 5.6 container to test the legacy MySQL plugin against * Add database plugins to the make file * Fix credsutil test
		
			
				
	
	
		
			327 lines
		
	
	
		
			8.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			327 lines
		
	
	
		
			8.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package mysql
 | |
| 
 | |
| import (
 | |
| 	"database/sql"
 | |
| 	"fmt"
 | |
| 	"os"
 | |
| 	"strings"
 | |
| 	"testing"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
 | |
| 	"github.com/hashicorp/vault/plugins/helper/database/connutil"
 | |
| 	"github.com/hashicorp/vault/plugins/helper/database/credsutil"
 | |
| 	dockertest "gopkg.in/ory-am/dockertest.v3"
 | |
| )
 | |
| 
 | |
| func prepareMySQLTestContainer(t *testing.T) (cleanup func(), retURL string) {
 | |
| 	if os.Getenv("MYSQL_URL") != "" {
 | |
| 		return func() {}, os.Getenv("MYSQL_URL")
 | |
| 	}
 | |
| 
 | |
| 	pool, err := dockertest.NewPool("")
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Failed to connect to docker: %s", err)
 | |
| 	}
 | |
| 
 | |
| 	resource, err := pool.Run("mysql", "latest", []string{"MYSQL_ROOT_PASSWORD=secret"})
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Could not start local MySQL 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("root:secret@(localhost:%s)/mysql?parseTime=true", resource.GetPort("3306/tcp"))
 | |
| 
 | |
| 	// exponential backoff-retry
 | |
| 	if err = pool.Retry(func() error {
 | |
| 		var err error
 | |
| 		var db *sql.DB
 | |
| 		db, err = sql.Open("mysql", retURL)
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 		return db.Ping()
 | |
| 	}); err != nil {
 | |
| 		t.Fatalf("Could not connect to MySQL docker container: %s", err)
 | |
| 	}
 | |
| 
 | |
| 	return
 | |
| }
 | |
| 
 | |
| func prepareMySQLLegacyTestContainer(t *testing.T) (cleanup func(), retURL string) {
 | |
| 	if os.Getenv("MYSQL_URL") != "" {
 | |
| 		return func() {}, os.Getenv("MYSQL_URL")
 | |
| 	}
 | |
| 
 | |
| 	pool, err := dockertest.NewPool("")
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Failed to connect to docker: %s", err)
 | |
| 	}
 | |
| 
 | |
| 	// Mysql 5.6 is the last MySQL version to limit usernames to 16 characters.
 | |
| 	resource, err := pool.Run("mysql", "5.6", []string{"MYSQL_ROOT_PASSWORD=secret"})
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Could not start local MySQL 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("root:secret@(localhost:%s)/mysql?parseTime=true", resource.GetPort("3306/tcp"))
 | |
| 
 | |
| 	// exponential backoff-retry
 | |
| 	if err = pool.Retry(func() error {
 | |
| 		var err error
 | |
| 		var db *sql.DB
 | |
| 		db, err = sql.Open("mysql", retURL)
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 		return db.Ping()
 | |
| 	}); err != nil {
 | |
| 		t.Fatalf("Could not connect to MySQL docker container: %s", err)
 | |
| 	}
 | |
| 
 | |
| 	return
 | |
| }
 | |
| 
 | |
| func TestMySQL_Initialize(t *testing.T) {
 | |
| 	cleanup, connURL := prepareMySQLTestContainer(t)
 | |
| 	defer cleanup()
 | |
| 
 | |
| 	connectionDetails := map[string]interface{}{
 | |
| 		"connection_url": connURL,
 | |
| 	}
 | |
| 
 | |
| 	f := New(MetadataLen, MetadataLen, UsernameLen)
 | |
| 	dbRaw, _ := f()
 | |
| 	db := dbRaw.(*MySQL)
 | |
| 	connProducer := db.ConnectionProducer.(*connutil.SQLConnectionProducer)
 | |
| 
 | |
| 	err := db.Initialize(connectionDetails, true)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("err: %s", err)
 | |
| 	}
 | |
| 
 | |
| 	if !connProducer.Initialized {
 | |
| 		t.Fatal("Database should be initalized")
 | |
| 	}
 | |
| 
 | |
| 	err = db.Close()
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("err: %s", err)
 | |
| 	}
 | |
| 
 | |
| 	// Test decoding a string value for max_open_connections
 | |
| 	connectionDetails = map[string]interface{}{
 | |
| 		"connection_url":       connURL,
 | |
| 		"max_open_connections": "5",
 | |
| 	}
 | |
| 
 | |
| 	err = db.Initialize(connectionDetails, true)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("err: %s", err)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestMySQL_CreateUser(t *testing.T) {
 | |
| 	cleanup, connURL := prepareMySQLTestContainer(t)
 | |
| 	defer cleanup()
 | |
| 
 | |
| 	connectionDetails := map[string]interface{}{
 | |
| 		"connection_url": connURL,
 | |
| 	}
 | |
| 
 | |
| 	f := New(MetadataLen, MetadataLen, UsernameLen)
 | |
| 	dbRaw, _ := f()
 | |
| 	db := dbRaw.(*MySQL)
 | |
| 
 | |
| 	err := db.Initialize(connectionDetails, true)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("err: %s", err)
 | |
| 	}
 | |
| 
 | |
| 	usernameConfig := dbplugin.UsernameConfig{
 | |
| 		DisplayName: "test-long-displayname",
 | |
| 		RoleName:    "test-long-rolename",
 | |
| 	}
 | |
| 
 | |
| 	// Test with no configured Creation Statememt
 | |
| 	_, _, err = db.CreateUser(dbplugin.Statements{}, usernameConfig, time.Now().Add(time.Minute))
 | |
| 	if err == nil {
 | |
| 		t.Fatal("Expected error when no creation statement is provided")
 | |
| 	}
 | |
| 
 | |
| 	statements := dbplugin.Statements{
 | |
| 		CreationStatements: testMySQLRoleWildCard,
 | |
| 	}
 | |
| 
 | |
| 	username, password, err := db.CreateUser(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 a second time to make sure usernames don't collide
 | |
| 	username, password, err = db.CreateUser(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 TestMySQL_CreateUser_Legacy(t *testing.T) {
 | |
| 	cleanup, connURL := prepareMySQLLegacyTestContainer(t)
 | |
| 	defer cleanup()
 | |
| 
 | |
| 	connectionDetails := map[string]interface{}{
 | |
| 		"connection_url": connURL,
 | |
| 	}
 | |
| 
 | |
| 	f := New(credsutil.NoneLength, LegacyMetadataLen, LegacyUsernameLen)
 | |
| 	dbRaw, _ := f()
 | |
| 	db := dbRaw.(*MySQL)
 | |
| 
 | |
| 	err := db.Initialize(connectionDetails, true)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("err: %s", err)
 | |
| 	}
 | |
| 
 | |
| 	usernameConfig := dbplugin.UsernameConfig{
 | |
| 		DisplayName: "test-long-displayname",
 | |
| 		RoleName:    "test-long-rolename",
 | |
| 	}
 | |
| 
 | |
| 	// Test with no configured Creation Statememt
 | |
| 	_, _, err = db.CreateUser(dbplugin.Statements{}, usernameConfig, time.Now().Add(time.Minute))
 | |
| 	if err == nil {
 | |
| 		t.Fatal("Expected error when no creation statement is provided")
 | |
| 	}
 | |
| 
 | |
| 	statements := dbplugin.Statements{
 | |
| 		CreationStatements: testMySQLRoleWildCard,
 | |
| 	}
 | |
| 
 | |
| 	username, password, err := db.CreateUser(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 a second time to make sure usernames don't collide
 | |
| 	username, password, err = db.CreateUser(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 TestMySQL_RevokeUser(t *testing.T) {
 | |
| 	cleanup, connURL := prepareMySQLTestContainer(t)
 | |
| 	defer cleanup()
 | |
| 
 | |
| 	connectionDetails := map[string]interface{}{
 | |
| 		"connection_url": connURL,
 | |
| 	}
 | |
| 
 | |
| 	f := New(MetadataLen, MetadataLen, UsernameLen)
 | |
| 	dbRaw, _ := f()
 | |
| 	db := dbRaw.(*MySQL)
 | |
| 
 | |
| 	err := db.Initialize(connectionDetails, true)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("err: %s", err)
 | |
| 	}
 | |
| 
 | |
| 	statements := dbplugin.Statements{
 | |
| 		CreationStatements: testMySQLRoleWildCard,
 | |
| 	}
 | |
| 
 | |
| 	usernameConfig := dbplugin.UsernameConfig{
 | |
| 		DisplayName: "test",
 | |
| 		RoleName:    "test",
 | |
| 	}
 | |
| 
 | |
| 	username, password, err := db.CreateUser(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 revoke statememts
 | |
| 	err = db.RevokeUser(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")
 | |
| 	}
 | |
| 
 | |
| 	statements.CreationStatements = testMySQLRoleWildCard
 | |
| 	username, password, err = db.CreateUser(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 custom revoke statements
 | |
| 	statements.RevocationStatements = testMySQLRevocationSQL
 | |
| 	err = db.RevokeUser(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 {
 | |
| 	// Log in with the new creds
 | |
| 	connURL = strings.Replace(connURL, "root:secret", fmt.Sprintf("%s:%s", username, password), 1)
 | |
| 	db, err := sql.Open("mysql", connURL)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer db.Close()
 | |
| 	return db.Ping()
 | |
| }
 | |
| 
 | |
| const testMySQLRoleWildCard = `
 | |
| CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';
 | |
| GRANT SELECT ON *.* TO '{{name}}'@'%';
 | |
| `
 | |
| const testMySQLRevocationSQL = `
 | |
| REVOKE ALL PRIVILEGES, GRANT OPTION FROM '{{name}}'@'%'; 
 | |
| DROP USER '{{name}}'@'%';
 | |
| `
 |