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"
|
2020-10-09 17:32:38 +00:00
|
|
|
"fmt"
|
2017-04-13 20:48:32 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
stdmysql "github.com/go-sql-driver/mysql"
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/strutil"
|
2020-10-15 19:20:12 +00:00
|
|
|
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
|
2019-04-15 18:10:07 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/helper/dbutil"
|
2021-02-11 21:08:32 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/template"
|
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
|
|
|
|
2019-07-05 18:52:56 +00:00
|
|
|
defaultMySQLRotateCredentialsSQL = `
|
2018-03-21 19:05:56 +00:00
|
|
|
ALTER USER '{{username}}'@'%' IDENTIFIED BY '{{password}}';
|
|
|
|
`
|
|
|
|
|
2017-05-03 20:33:56 +00:00
|
|
|
mySQLTypeName = "mysql"
|
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
DefaultUserNameTemplate = `{{ printf "v-%s-%s-%s-%s" (.DisplayName | truncate 10) (.RoleName | truncate 10) (random 20) (unix_time) | truncate 32 }}`
|
|
|
|
DefaultLegacyUserNameTemplate = `{{ printf "v-%s-%s-%s" (.RoleName | truncate 4) (random 20) | truncate 16 }}`
|
2017-05-03 20:33:56 +00:00
|
|
|
)
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
var _ dbplugin.Database = (*MySQL)(nil)
|
2017-12-14 22:03:11 +00:00
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
type MySQL struct {
|
2020-06-17 18:46:01 +00:00
|
|
|
*mySQLConnectionProducer
|
2020-12-01 22:24:51 +00:00
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
usernameProducer template.StringTemplate
|
|
|
|
defaultUsernameTemplate string
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 16:10:26 +00:00
|
|
|
// New implements builtinplugins.BuiltinFactory
|
2021-02-11 21:08:32 +00:00
|
|
|
func New(defaultUsernameTemplate string) func() (interface{}, error) {
|
2017-05-03 20:33:56 +00:00
|
|
|
return func() (interface{}, error) {
|
2021-02-11 21:08:32 +00:00
|
|
|
if defaultUsernameTemplate == "" {
|
|
|
|
return nil, fmt.Errorf("missing default username template")
|
|
|
|
}
|
|
|
|
db := newMySQL(defaultUsernameTemplate)
|
2018-03-21 19:05:56 +00:00
|
|
|
// Wrap the plugin with middleware to sanitize errors
|
2020-10-15 19:20:12 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
func newMySQL(defaultUsernameTemplate string) *MySQL {
|
2020-06-17 18:46:01 +00:00
|
|
|
connProducer := &mySQLConnectionProducer{}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
|
|
|
return &MySQL{
|
2020-06-17 18:46:01 +00:00
|
|
|
mySQLConnectionProducer: connProducer,
|
2021-02-11 21:08:32 +00:00
|
|
|
defaultUsernameTemplate: defaultUsernameTemplate,
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:48:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m *MySQL) Initialize(ctx context.Context, req dbplugin.InitializeRequest) (dbplugin.InitializeResponse, error) {
|
2021-02-11 21:08:32 +00:00
|
|
|
usernameTemplate, err := strutil.GetString(req.Config, "username_template")
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.InitializeResponse{}, err
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
2021-02-11 21:08:32 +00:00
|
|
|
|
|
|
|
if usernameTemplate == "" {
|
|
|
|
usernameTemplate = m.defaultUsernameTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
up, err := template.NewTemplate(template.Template(usernameTemplate))
|
|
|
|
if err != nil {
|
|
|
|
return dbplugin.InitializeResponse{}, fmt.Errorf("unable to initialize username template: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.usernameProducer = up
|
|
|
|
|
|
|
|
_, err = m.usernameProducer.Generate(dbplugin.UsernameMetadata{})
|
|
|
|
if err != nil {
|
|
|
|
return dbplugin.InitializeResponse{}, fmt.Errorf("invalid username template: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.mySQLConnectionProducer.Initialize(ctx, req.Config, req.VerifyConnection)
|
|
|
|
if err != nil {
|
|
|
|
return dbplugin.InitializeResponse{}, err
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
resp := dbplugin.InitializeResponse{
|
2020-10-09 17:32:38 +00:00
|
|
|
Config: req.Config,
|
|
|
|
}
|
2021-02-11 21:08:32 +00:00
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m *MySQL) NewUser(ctx context.Context, req dbplugin.NewUserRequest) (dbplugin.NewUserResponse, error) {
|
2020-10-09 17:32:38 +00:00
|
|
|
if len(req.Statements.Commands) == 0 {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.NewUserResponse{}, dbutil.ErrEmptyCreationStatement
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:08:32 +00:00
|
|
|
username, err := m.usernameProducer.Generate(req.UsernameConfig)
|
2017-04-13 20:48:32 +00:00
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.NewUserResponse{}, err
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
password := req.Password
|
|
|
|
|
|
|
|
expirationStr := req.Expiration.Format("2006-01-02 15:04:05-0700")
|
|
|
|
|
2019-07-05 18:52:56 +00:00
|
|
|
queryMap := map[string]string{
|
|
|
|
"name": username,
|
2020-02-03 20:57:28 +00:00
|
|
|
"username": username,
|
2019-07-05 18:52:56 +00:00
|
|
|
"password": password,
|
|
|
|
"expiration": expirationStr,
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
if err := m.executePreparedStatementsWithMap(ctx, req.Statements.Commands, queryMap); err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.NewUserResponse{}, err
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
2020-10-09 17:32:38 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
resp := dbplugin.NewUserResponse{
|
2020-10-09 17:32:38 +00:00
|
|
|
Username: username,
|
|
|
|
}
|
|
|
|
return resp, nil
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m *MySQL) DeleteUser(ctx context.Context, req dbplugin.DeleteUserRequest) (dbplugin.DeleteUserResponse, error) {
|
2017-04-13 20:48:32 +00:00
|
|
|
// Grab the read lock
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
// 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 {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
revocationStmts := req.Statements.Commands
|
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 {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
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/
|
2020-10-09 17:32:38 +00:00
|
|
|
query = strings.Replace(query, "{{name}}", req.Username, -1)
|
|
|
|
query = strings.Replace(query, "{{username}}", req.Username, -1)
|
2018-03-21 19:05:56 +00:00
|
|
|
_, err = tx.ExecContext(ctx, query)
|
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
2020-10-09 17:32:38 +00:00
|
|
|
err = tx.Commit()
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.DeleteUserResponse{}, err
|
2017-04-13 20:48:32 +00:00
|
|
|
}
|
2018-03-21 19:05:56 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
func (m *MySQL) UpdateUser(ctx context.Context, req dbplugin.UpdateUserRequest) (dbplugin.UpdateUserResponse, error) {
|
2020-10-09 17:32:38 +00:00
|
|
|
if req.Password == nil && req.Expiration == nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.UpdateUserResponse{}, fmt.Errorf("no change requested")
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
if req.Password != nil {
|
|
|
|
err := m.changeUserPassword(ctx, req.Username, req.Password.NewPassword, req.Password.Statements.Commands)
|
|
|
|
if err != nil {
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.UpdateUserResponse{}, fmt.Errorf("failed to change password: %w", err)
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
// Expiration change/update is currently a no-op
|
2018-03-21 19:05:56 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
return dbplugin.UpdateUserResponse{}, nil
|
2018-03-21 19:05:56 +00:00
|
|
|
}
|
2019-07-05 18:52:56 +00:00
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
func (m *MySQL) changeUserPassword(ctx context.Context, username, password string, rotateStatements []string) error {
|
|
|
|
if username == "" || password == "" {
|
|
|
|
return errors.New("must provide both username and password")
|
2019-07-05 18:52:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
if len(rotateStatements) == 0 {
|
|
|
|
rotateStatements = []string{defaultMySQLRotateCredentialsSQL}
|
2019-07-05 18:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
queryMap := map[string]string{
|
|
|
|
"name": username,
|
2020-02-03 20:57:28 +00:00
|
|
|
"username": username,
|
2019-07-05 18:52:56 +00:00
|
|
|
"password": password,
|
|
|
|
}
|
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
if err := m.executePreparedStatementsWithMap(ctx, rotateStatements, queryMap); err != nil {
|
|
|
|
return err
|
2019-07-05 18:52:56 +00:00
|
|
|
}
|
2020-10-09 17:32:38 +00:00
|
|
|
return nil
|
2019-07-05 18:52:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:32:38 +00:00
|
|
|
// executePreparedStatementsWithMap loops through the given templated SQL statements and
|
2020-02-13 22:11:33 +00:00
|
|
|
// applies the map to them, interpolating values into the templates, returning
|
|
|
|
// the resulting username and password
|
2020-10-09 17:32:38 +00:00
|
|
|
func (m *MySQL) executePreparedStatementsWithMap(ctx context.Context, statements []string, queryMap map[string]string) error {
|
2019-07-05 18:52:56 +00:00
|
|
|
// Grab the lock
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
// Get the connection
|
|
|
|
db, err := m.getConnection(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Start a transaction
|
|
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Execute each query
|
|
|
|
for _, stmt := range statements {
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(stmt, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
query = dbutil.QueryHelper(query, queryMap)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
stmt.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := stmt.ExecContext(ctx); err != nil {
|
|
|
|
stmt.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stmt.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|