mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-31 02:28:09 +00:00 
			
		
		
		
	 35667f93a7
			
		
	
	35667f93a7
	
	
	
		
			
			* Add priority queue to sdk * fix issue of storing pointers and now copy * update to use copy structure * Remove file, put Item struct def. into other file * add link * clean up docs * refactor internal data structure to hide heap method implementations. Other cleanup after feedback * rename PushItem and PopItem to just Push/Pop, after encapsulating the heap methods * updates after feedback * refactoring/renaming * guard against pushing a nil item * minor updates after feedback * Add SetCredentials, GenerateCredentials gRPC methods to combined database backend gPRC * Initial Combined database backend implementation of static accounts and automatic rotation * vendor updates * initial implementation of static accounts with Combined database backend, starting with PostgreSQL implementation * add lock and setup of rotation queue * vendor the queue * rebase on new method signature of queue * remove mongo tests for now * update default role sql * gofmt after rebase * cleanup after rebasing to remove checks for ErrNotFound error * rebase cdcr-priority-queue * vendor dependencies with 'go mod vendor' * website database docs for Static Role support * document the rotate-role API endpoint * postgres specific static role docs * use constants for paths * updates from review * remove dead code * combine and clarify error message for older plugins * Update builtin/logical/database/backend.go Co-Authored-By: Jim Kalafut <jim@kalafut.net> * cleanups from feedback * code and comment cleanups * move db.RLock higher to protect db.GenerateCredentials call * Return output with WALID if we failed to delete the WAL * Update builtin/logical/database/path_creds_create.go Co-Authored-By: Jim Kalafut <jim@kalafut.net> * updates after running 'make fmt' * update after running 'make proto' * Update builtin/logical/database/path_roles.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/path_roles.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * update comment and remove and rearrange some dead code * Update website/source/api/secret/databases/index.html.md Co-Authored-By: Jim Kalafut <jim@kalafut.net> * cleanups after review * Update sdk/database/dbplugin/grpc_transport.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * code cleanup after feedback * remove PasswordLastSet; it's not used * document GenerateCredentials and SetCredentials * Update builtin/logical/database/path_rotate_credentials.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * wrap pop and popbykey in backend methods to protect against nil cred rotation queue * use strings.HasPrefix instead of direct equality check for path * Forgot to commit this * updates after feedback * re-purpose an outdated test to now check that static and dynamic roles cannot share a name * check for unique name across dynamic and static roles * refactor loadStaticWALs to return a map of name/setCredentialsWAL struct to consolidate where we're calling set credentials * remove commented out code * refactor to have loadstaticwals filter out wals for roles that no longer exist * return error if nil input given * add nil check for input into setStaticAccount * Update builtin/logical/database/path_roles.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * add constant for queue tick time in seconds, used for comparrison in updates * Update builtin/logical/database/path_roles.go Co-Authored-By: Jim Kalafut <jim@kalafut.net> * code cleanup after review * remove misplaced code comment * remove commented out code * create a queue in the Factory method, even if it's never used * update path_roles to use a common set of fields, with specific overrides for dynamic/static roles by type * document new method * move rotation things into a specific file * rename test file and consolidate some static account tests * Update builtin/logical/database/path_roles.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/rotation.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/rotation.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/rotation.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/rotation.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * Update builtin/logical/database/rotation.go Co-Authored-By: Brian Kassouf <briankassouf@users.noreply.github.com> * update code comments, method names, and move more methods into rotation.go * update comments to be capitalized * remove the item from the queue before we try to destroy it * findStaticWAL returns an error * use lowercase keys when encoding WAL entries * small cleanups * remove vestigial static account check * remove redundant DeleteWAL call in populate queue * if we error on loading role, push back to queue with 10 second backoff * poll in initqueue to make sure the backend is setup and can write/delete data * add revoke_user_on_delete flag to allow users to opt-in to revoking the static database user on delete of the Vault role. Default false * add code comments on read-only loop * code comment updates * re-push if error returned from find static wal * add locksutil and acquire locks when pop'ing from the queue * grab exclusive locks for updating static roles * Add SetCredentials and GenerateCredentials stubs to mockPlugin * add a switch in initQueue to listen for cancelation * remove guard on zero time, it should have no affect * create a new context in Factory to pass on and use for closing the backend queue * restore master copy of vendor dir
		
			
				
	
	
		
			393 lines
		
	
	
		
			9.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			393 lines
		
	
	
		
			9.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package mssql
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"database/sql"
 | |
| 	"errors"
 | |
| 	"fmt"
 | |
| 	"strings"
 | |
| 	"time"
 | |
| 
 | |
| 	_ "github.com/denisenkom/go-mssqldb"
 | |
| 	"github.com/hashicorp/errwrap"
 | |
| 	multierror "github.com/hashicorp/go-multierror"
 | |
| 	"github.com/hashicorp/vault/api"
 | |
| 	"github.com/hashicorp/vault/sdk/database/dbplugin"
 | |
| 	"github.com/hashicorp/vault/sdk/database/helper/connutil"
 | |
| 	"github.com/hashicorp/vault/sdk/database/helper/credsutil"
 | |
| 	"github.com/hashicorp/vault/sdk/database/helper/dbutil"
 | |
| 	"github.com/hashicorp/vault/sdk/helper/dbtxn"
 | |
| 	"github.com/hashicorp/vault/sdk/helper/strutil"
 | |
| )
 | |
| 
 | |
| const msSQLTypeName = "mssql"
 | |
| 
 | |
| var _ dbplugin.Database = &MSSQL{}
 | |
| 
 | |
| // MSSQL is an implementation of Database interface
 | |
| type MSSQL struct {
 | |
| 	*connutil.SQLConnectionProducer
 | |
| 	credsutil.CredentialsProducer
 | |
| }
 | |
| 
 | |
| func New() (interface{}, error) {
 | |
| 	db := new()
 | |
| 	// Wrap the plugin with middleware to sanitize errors
 | |
| 	dbType := dbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.SecretValues)
 | |
| 
 | |
| 	return dbType, nil
 | |
| }
 | |
| 
 | |
| func new() *MSSQL {
 | |
| 	connProducer := &connutil.SQLConnectionProducer{}
 | |
| 	connProducer.Type = msSQLTypeName
 | |
| 
 | |
| 	credsProducer := &credsutil.SQLCredentialsProducer{
 | |
| 		DisplayNameLen: 20,
 | |
| 		RoleNameLen:    20,
 | |
| 		UsernameLen:    128,
 | |
| 		Separator:      "-",
 | |
| 	}
 | |
| 
 | |
| 	return &MSSQL{
 | |
| 		SQLConnectionProducer: connProducer,
 | |
| 		CredentialsProducer:   credsProducer,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // Run instantiates a MSSQL object, and runs the RPC server for the plugin
 | |
| func Run(apiTLSConfig *api.TLSConfig) error {
 | |
| 	dbType, err := New()
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	dbplugin.Serve(dbType.(dbplugin.Database), api.VaultPluginTLSProvider(apiTLSConfig))
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // Type returns the TypeName for this backend
 | |
| func (m *MSSQL) Type() (string, error) {
 | |
| 	return msSQLTypeName, nil
 | |
| }
 | |
| 
 | |
| func (m *MSSQL) getConnection(ctx context.Context) (*sql.DB, error) {
 | |
| 	db, err := m.Connection(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return db.(*sql.DB), nil
 | |
| }
 | |
| 
 | |
| // CreateUser generates the username/password on the underlying MSSQL secret backend as instructed by
 | |
| // the CreationStatement provided.
 | |
| func (m *MSSQL) CreateUser(ctx context.Context, statements dbplugin.Statements, usernameConfig dbplugin.UsernameConfig, expiration time.Time) (username string, password string, err error) {
 | |
| 	// Grab the lock
 | |
| 	m.Lock()
 | |
| 	defer m.Unlock()
 | |
| 
 | |
| 	statements = dbutil.StatementCompatibilityHelper(statements)
 | |
| 
 | |
| 	// Get the connection
 | |
| 	db, err := m.getConnection(ctx)
 | |
| 	if err != nil {
 | |
| 		return "", "", err
 | |
| 	}
 | |
| 
 | |
| 	if len(statements.Creation) == 0 {
 | |
| 		return "", "", dbutil.ErrEmptyCreationStatement
 | |
| 	}
 | |
| 
 | |
| 	username, err = m.GenerateUsername(usernameConfig)
 | |
| 	if err != nil {
 | |
| 		return "", "", err
 | |
| 	}
 | |
| 
 | |
| 	password, err = m.GeneratePassword()
 | |
| 	if err != nil {
 | |
| 		return "", "", err
 | |
| 	}
 | |
| 
 | |
| 	expirationStr, err := m.GenerateExpiration(expiration)
 | |
| 	if err != nil {
 | |
| 		return "", "", err
 | |
| 	}
 | |
| 
 | |
| 	// Start a transaction
 | |
| 	tx, err := db.BeginTx(ctx, nil)
 | |
| 	if err != nil {
 | |
| 		return "", "", err
 | |
| 	}
 | |
| 	defer tx.Rollback()
 | |
| 
 | |
| 	// Execute each query
 | |
| 	for _, stmt := range statements.Creation {
 | |
| 		for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
 | |
| 			query = strings.TrimSpace(query)
 | |
| 			if len(query) == 0 {
 | |
| 				continue
 | |
| 			}
 | |
| 
 | |
| 			m := map[string]string{
 | |
| 				"name":       username,
 | |
| 				"password":   password,
 | |
| 				"expiration": expirationStr,
 | |
| 			}
 | |
| 
 | |
| 			if err := dbtxn.ExecuteTxQuery(ctx, tx, m, query); err != nil {
 | |
| 				return "", "", err
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	// Commit the transaction
 | |
| 	if err := tx.Commit(); err != nil {
 | |
| 		return "", "", err
 | |
| 	}
 | |
| 
 | |
| 	return username, password, nil
 | |
| }
 | |
| 
 | |
| // RenewUser is not supported on MSSQL, so this is a no-op.
 | |
| func (m *MSSQL) RenewUser(ctx context.Context, statements dbplugin.Statements, username string, expiration time.Time) error {
 | |
| 	// NOOP
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // RevokeUser attempts to drop the specified user. It will first attempt to disable login,
 | |
| // then kill pending connections from that user, and finally drop the user and login from the
 | |
| // database instance.
 | |
| func (m *MSSQL) RevokeUser(ctx context.Context, statements dbplugin.Statements, username string) error {
 | |
| 	statements = dbutil.StatementCompatibilityHelper(statements)
 | |
| 
 | |
| 	if len(statements.Revocation) == 0 {
 | |
| 		return m.revokeUserDefault(ctx, username)
 | |
| 	}
 | |
| 
 | |
| 	// Get connection
 | |
| 	db, err := m.getConnection(ctx)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	var result error
 | |
| 
 | |
| 	// Execute each query
 | |
| 	for _, stmt := range statements.Revocation {
 | |
| 		for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
 | |
| 			query = strings.TrimSpace(query)
 | |
| 			if len(query) == 0 {
 | |
| 				continue
 | |
| 			}
 | |
| 
 | |
| 			m := map[string]string{
 | |
| 				"name": username,
 | |
| 			}
 | |
| 			if err := dbtxn.ExecuteDBQuery(ctx, db, m, query); err != nil {
 | |
| 				result = multierror.Append(result, err)
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return result
 | |
| }
 | |
| 
 | |
| func (m *MSSQL) revokeUserDefault(ctx context.Context, username string) error {
 | |
| 	// Get connection
 | |
| 	db, err := m.getConnection(ctx)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	// First disable server login
 | |
| 	disableStmt, err := db.PrepareContext(ctx, fmt.Sprintf("ALTER LOGIN [%s] DISABLE;", username))
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer disableStmt.Close()
 | |
| 	if _, err := disableStmt.ExecContext(ctx); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	// Query for sessions for the login so that we can kill any outstanding
 | |
| 	// sessions.  There cannot be any active sessions before we drop the logins
 | |
| 	// This isn't done in a transaction because even if we fail along the way,
 | |
| 	// we want to remove as much access as possible
 | |
| 	sessionStmt, err := db.PrepareContext(ctx, fmt.Sprintf(
 | |
| 		"SELECT session_id FROM sys.dm_exec_sessions WHERE login_name = '%s';", username))
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer sessionStmt.Close()
 | |
| 
 | |
| 	sessionRows, err := sessionStmt.QueryContext(ctx)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer sessionRows.Close()
 | |
| 
 | |
| 	var revokeStmts []string
 | |
| 	for sessionRows.Next() {
 | |
| 		var sessionID int
 | |
| 		err = sessionRows.Scan(&sessionID)
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 		revokeStmts = append(revokeStmts, fmt.Sprintf("KILL %d;", sessionID))
 | |
| 	}
 | |
| 
 | |
| 	// Query for database users using undocumented stored procedure for now since
 | |
| 	// it is the easiest way to get this information;
 | |
| 	// we need to drop the database users before we can drop the login and the role
 | |
| 	// This isn't done in a transaction because even if we fail along the way,
 | |
| 	// we want to remove as much access as possible
 | |
| 	stmt, err := db.PrepareContext(ctx, fmt.Sprintf("EXEC master.dbo.sp_msloginmappings '%s';", username))
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer stmt.Close()
 | |
| 
 | |
| 	rows, err := stmt.QueryContext(ctx)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer rows.Close()
 | |
| 
 | |
| 	for rows.Next() {
 | |
| 		var loginName, dbName, qUsername, aliasName sql.NullString
 | |
| 		err = rows.Scan(&loginName, &dbName, &qUsername, &aliasName)
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 		if !dbName.Valid {
 | |
| 			continue
 | |
| 		}
 | |
| 		revokeStmts = append(revokeStmts, fmt.Sprintf(dropUserSQL, dbName.String, username, username))
 | |
| 	}
 | |
| 
 | |
| 	// we do not stop on error, as we want to remove as
 | |
| 	// many permissions as possible right now
 | |
| 	var lastStmtError error
 | |
| 	for _, query := range revokeStmts {
 | |
| 		if err := dbtxn.ExecuteDBQuery(ctx, db, nil, query); err != nil {
 | |
| 			lastStmtError = err
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	// can't drop if not all database users are dropped
 | |
| 	if rows.Err() != nil {
 | |
| 		return errwrap.Wrapf("could not generate sql statements for all rows: {{err}}", rows.Err())
 | |
| 	}
 | |
| 	if lastStmtError != nil {
 | |
| 		return errwrap.Wrapf("could not perform all sql statements: {{err}}", lastStmtError)
 | |
| 	}
 | |
| 
 | |
| 	// Drop this login
 | |
| 	stmt, err = db.PrepareContext(ctx, fmt.Sprintf(dropLoginSQL, username, username))
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer stmt.Close()
 | |
| 	if _, err := stmt.ExecContext(ctx); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (m *MSSQL) RotateRootCredentials(ctx context.Context, statements []string) (map[string]interface{}, error) {
 | |
| 	m.Lock()
 | |
| 	defer m.Unlock()
 | |
| 
 | |
| 	if len(m.Username) == 0 || len(m.Password) == 0 {
 | |
| 		return nil, errors.New("username and password are required to rotate")
 | |
| 	}
 | |
| 
 | |
| 	rotateStatents := statements
 | |
| 	if len(rotateStatents) == 0 {
 | |
| 		rotateStatents = []string{rotateRootCredentialsSQL}
 | |
| 	}
 | |
| 
 | |
| 	db, err := m.getConnection(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	tx, err := db.BeginTx(ctx, nil)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	defer func() {
 | |
| 		tx.Rollback()
 | |
| 	}()
 | |
| 
 | |
| 	password, err := m.GeneratePassword()
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	for _, stmt := range rotateStatents {
 | |
| 		for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
 | |
| 			query = strings.TrimSpace(query)
 | |
| 			if len(query) == 0 {
 | |
| 				continue
 | |
| 			}
 | |
| 
 | |
| 			m := map[string]string{
 | |
| 				"username": m.Username,
 | |
| 				"password": password,
 | |
| 			}
 | |
| 			if err := dbtxn.ExecuteTxQuery(ctx, tx, m, query); err != nil {
 | |
| 				return nil, err
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	if err := tx.Commit(); err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	if err := db.Close(); err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	m.RawConfig["password"] = password
 | |
| 	return m.RawConfig, nil
 | |
| }
 | |
| 
 | |
| const dropUserSQL = `
 | |
| USE [%s]
 | |
| IF EXISTS
 | |
|   (SELECT name
 | |
|    FROM sys.database_principals
 | |
|    WHERE name = N'%s')
 | |
| BEGIN
 | |
|   DROP USER [%s]
 | |
| END
 | |
| `
 | |
| 
 | |
| const dropLoginSQL = `
 | |
| IF EXISTS
 | |
|   (SELECT name
 | |
|    FROM master.sys.server_principals
 | |
|    WHERE name = N'%s')
 | |
| BEGIN
 | |
|   DROP LOGIN [%s]
 | |
| END
 | |
| `
 | |
| 
 | |
| const rotateRootCredentialsSQL = `
 | |
| ALTER LOGIN [{{username}}] WITH PASSWORD = '{{password}}' 
 | |
| `
 | |
| 
 | |
| // GenerateCredentials returns a generated password
 | |
| func (m *MSSQL) GenerateCredentials(ctx context.Context) (string, error) {
 | |
| 	password, err := m.GeneratePassword()
 | |
| 	if err != nil {
 | |
| 		return "", err
 | |
| 	}
 | |
| 	return password, nil
 | |
| }
 |