2017-04-13 20:48:32 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2017-12-14 22:03:11 +00:00
|
|
|
"context"
|
2017-04-13 20:48:32 +00:00
|
|
|
"database/sql"
|
2018-03-21 19:05:56 +00:00
|
|
|
"errors"
|
2017-04-13 20:48:32 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-11-28 18:19:49 +00:00
|
|
|
stdmysql "github.com/go-sql-driver/mysql"
|
2017-05-02 21:40:11 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2019-04-15 18:10:07 +00:00
|
|
|
"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"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
2017-04-13 20:48:32 +00:00
|
|
|
)
|
|
|
|
|
2017-05-03 20:33:56 +00:00
|
|
|
const (
|
|
|
|
defaultMysqlRevocationStmts = `
|
|
|
|
REVOKE ALL PRIVILEGES, GRANT OPTION FROM '{{name}}'@'%';
|
|
|
|
DROP USER '{{name}}'@'%'
|
|
|
|
`
|
2018-03-21 19:05:56 +00:00
|
|
|
|
|
|
|
defaultMySQLRotateRootCredentialsSQL = `
|
|
|
|
ALTER USER '{{username}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
`
|
|
|
|
|
2017-05-03 20:33:56 +00:00
|
|
|
mySQLTypeName = "mysql"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-06-06 13:49:49 +00:00
|
|
|
MetadataLen int = 10
|
|
|
|
LegacyMetadataLen int = 4
|
|
|
|
UsernameLen int = 32
|
|
|
|
LegacyUsernameLen int = 16
|
2017-05-03 20:33:56 +00:00
|
|
|
)
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
var _ dbplugin.Database = &MySQL{}
|
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
type MySQL struct {
|
2018-03-21 19:05:56 +00:00
|
|
|
*connutil.SQLConnectionProducer
|
2017-04-13 20:48:32 +00:00
|
|
|
credsutil.CredentialsProducer
|
|
|
|
}
|
|
|
|
|
2017-04-21 16:10:26 +00:00
|
|
|
// New implements builtinplugins.BuiltinFactory
|
2017-08-11 01:28:18 +00:00
|
|
|
func New(displayNameLen, roleNameLen, usernameLen int) func() (interface{}, error) {
|
2017-05-03 20:33:56 +00:00
|
|
|
return func() (interface{}, error) {
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new(displayNameLen, roleNameLen, usernameLen)
|
|
|
|
// Wrap the plugin with middleware to sanitize errors
|
|
|
|
dbType := dbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.SecretValues)
|
2017-05-03 20:33:56 +00:00
|
|
|
|
|
|
|
return dbType, nil
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
func new(displayNameLen, roleNameLen, usernameLen int) *MySQL {
|
|
|
|
connProducer := &connutil.SQLConnectionProducer{}
|
|
|
|
connProducer.Type = mySQLTypeName
|
|
|
|
|
|
|
|
credsProducer := &credsutil.SQLCredentialsProducer{
|
|
|
|
DisplayNameLen: displayNameLen,
|
|
|
|
RoleNameLen: roleNameLen,
|
|
|
|
UsernameLen: usernameLen,
|
|
|
|
Separator: "-",
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MySQL{
|
|
|
|
SQLConnectionProducer: connProducer,
|
|
|
|
CredentialsProducer: credsProducer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
// Run instantiates a MySQL object, and runs the RPC server for the plugin
|
2017-05-02 21:40:11 +00:00
|
|
|
func Run(apiTLSConfig *api.TLSConfig) error {
|
2017-08-11 01:28:18 +00:00
|
|
|
return runCommon(false, apiTLSConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run instantiates a MySQL object, and runs the RPC server for the plugin
|
|
|
|
func RunLegacy(apiTLSConfig *api.TLSConfig) error {
|
|
|
|
return runCommon(true, apiTLSConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCommon(legacy bool, apiTLSConfig *api.TLSConfig) error {
|
|
|
|
var f func() (interface{}, error)
|
|
|
|
if legacy {
|
|
|
|
f = New(credsutil.NoneLength, LegacyMetadataLen, LegacyUsernameLen)
|
|
|
|
} else {
|
|
|
|
f = New(MetadataLen, MetadataLen, UsernameLen)
|
|
|
|
}
|
2017-05-03 20:33:56 +00:00
|
|
|
dbType, err := f()
|
2017-04-21 01:46:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2019-04-15 18:10:07 +00:00
|
|
|
dbplugin.Serve(dbType.(dbplugin.Database), api.VaultPluginTLSProvider(apiTLSConfig))
|
2017-04-13 20:48:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MySQL) Type() (string, error) {
|
|
|
|
return mySQLTypeName, nil
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
func (m *MySQL) getConnection(ctx context.Context) (*sql.DB, error) {
|
|
|
|
db, err := m.Connection(ctx)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.(*sql.DB), nil
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
func (m *MySQL) CreateUser(ctx context.Context, statements dbplugin.Statements, usernameConfig dbplugin.UsernameConfig, expiration time.Time) (username string, password string, err error) {
|
2017-04-13 20:48:32 +00:00
|
|
|
// Grab the lock
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
statements = dbutil.StatementCompatibilityHelper(statements)
|
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
// Get the connection
|
2017-12-14 22:03:11 +00:00
|
|
|
db, err := m.getConnection(ctx)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
if len(statements.Creation) == 0 {
|
2017-04-13 20:48:32 +00:00
|
|
|
return "", "", dbutil.ErrEmptyCreationStatement
|
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
username, err = m.GenerateUsername(usernameConfig)
|
2017-04-13 20:48:32 +00:00
|
|
|
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
|
2017-12-14 22:03:11 +00:00
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// Execute each query
|
2018-03-21 19:05:56 +00:00
|
|
|
for _, stmt := range statements.Creation {
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
2017-11-28 18:19:49 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
query = dbutil.QueryHelper(query, map[string]string{
|
|
|
|
"name": username,
|
|
|
|
"password": password,
|
|
|
|
"expiration": expirationStr,
|
|
|
|
})
|
|
|
|
|
|
|
|
stmt, err := tx.PrepareContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
// If the error code we get back is Error 1295: This command is not
|
|
|
|
// supported in the prepared statement protocol yet, we will execute
|
|
|
|
// the statement without preparing it. This allows the caller to
|
|
|
|
// manually prepare statements, as well as run other not yet
|
|
|
|
// prepare supported commands. If there is no error when running we
|
|
|
|
// will continue to the next statement.
|
|
|
|
if e, ok := err.(*stdmysql.MySQLError); ok && e.Number == 1295 {
|
|
|
|
_, err = tx.ExecContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2017-11-28 18:19:49 +00:00
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
if _, err := stmt.ExecContext(ctx); err != nil {
|
2018-04-17 23:31:09 +00:00
|
|
|
stmt.Close()
|
2018-03-21 19:05:56 +00:00
|
|
|
return "", "", err
|
|
|
|
}
|
2018-04-17 23:31:09 +00:00
|
|
|
stmt.Close()
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return username, password, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOOP
|
2017-12-14 22:03:11 +00:00
|
|
|
func (m *MySQL) RenewUser(ctx context.Context, statements dbplugin.Statements, username string, expiration time.Time) error {
|
2017-04-13 20:48:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
func (m *MySQL) RevokeUser(ctx context.Context, statements dbplugin.Statements, username string) error {
|
2017-04-13 20:48:32 +00:00
|
|
|
// Grab the read lock
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
statements = dbutil.StatementCompatibilityHelper(statements)
|
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
// Get the connection
|
2017-12-14 22:03:11 +00:00
|
|
|
db, err := m.getConnection(ctx)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
revocationStmts := statements.Revocation
|
2017-04-13 20:48:32 +00:00
|
|
|
// Use a default SQL statement for revocation if one cannot be fetched from the role
|
2018-03-21 19:05:56 +00:00
|
|
|
if len(revocationStmts) == 0 {
|
|
|
|
revocationStmts = []string{defaultMysqlRevocationStmts}
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start a transaction
|
2017-12-14 22:03:11 +00:00
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
for _, stmt := range revocationStmts {
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
// This is not a prepared statement because not all commands are supported
|
|
|
|
// 1295: This command is not supported in the prepared statement protocol yet
|
|
|
|
// Reference https://mariadb.com/kb/en/mariadb/prepare-statement/
|
|
|
|
query = strings.Replace(query, "{{name}}", username, -1)
|
|
|
|
_, err = tx.ExecContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
|
|
|
func (m *MySQL) 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{defaultMySQLRotateRootCredentialsSQL}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-04-17 23:31:09 +00:00
|
|
|
|
2019-02-05 21:02:48 +00:00
|
|
|
// This is not a prepared statement because not all commands are supported
|
|
|
|
// 1295: This command is not supported in the prepared statement protocol yet
|
|
|
|
// Reference https://mariadb.com/kb/en/mariadb/prepare-statement/
|
|
|
|
query = strings.Replace(query, "{{username}}", m.Username, -1)
|
|
|
|
query = strings.Replace(query, "{{password}}", password, -1)
|
|
|
|
|
|
|
|
if _, err := tx.ExecContext(ctx, query); err != nil {
|
2018-03-21 19:05:56 +00:00
|
|
|
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
|
|
|
|
}
|
Combined Database Backend: Static Accounts (#6834)
* 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
2019-06-19 19:45:39 +00:00
|
|
|
|
|
|
|
// GenerateCredentials returns a generated password
|
|
|
|
func (m *MySQL) GenerateCredentials(ctx context.Context) (string, error) {
|
|
|
|
password, err := m.GeneratePassword()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return password, nil
|
|
|
|
}
|