2017-04-13 20:48:32 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-07-07 17:38:12 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2017-05-02 21:40:11 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2017-04-13 20:48:32 +00:00
|
|
|
"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
|
|
|
|
"github.com/hashicorp/vault/helper/strutil"
|
2017-05-02 21:40:11 +00:00
|
|
|
"github.com/hashicorp/vault/plugins"
|
2017-04-13 20:48:32 +00:00
|
|
|
"github.com/hashicorp/vault/plugins/helper/database/connutil"
|
|
|
|
"github.com/hashicorp/vault/plugins/helper/database/credsutil"
|
|
|
|
"github.com/hashicorp/vault/plugins/helper/database/dbutil"
|
|
|
|
)
|
|
|
|
|
2017-05-03 20:33:56 +00:00
|
|
|
const (
|
|
|
|
defaultMysqlRevocationStmts = `
|
|
|
|
REVOKE ALL PRIVILEGES, GRANT OPTION FROM '{{name}}'@'%';
|
|
|
|
DROP USER '{{name}}'@'%'
|
|
|
|
`
|
|
|
|
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
|
|
|
|
|
|
|
type MySQL struct {
|
|
|
|
connutil.ConnectionProducer
|
|
|
|
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) {
|
|
|
|
connProducer := &connutil.SQLConnectionProducer{}
|
|
|
|
connProducer.Type = mySQLTypeName
|
|
|
|
|
|
|
|
credsProducer := &credsutil.SQLCredentialsProducer{
|
2017-08-11 01:28:18 +00:00
|
|
|
DisplayNameLen: displayNameLen,
|
|
|
|
RoleNameLen: roleNameLen,
|
2017-05-03 20:33:56 +00:00
|
|
|
UsernameLen: usernameLen,
|
2017-06-06 13:49:49 +00:00
|
|
|
Separator: "-",
|
2017-05-03 20:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dbType := &MySQL{
|
|
|
|
ConnectionProducer: connProducer,
|
|
|
|
CredentialsProducer: credsProducer,
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbType, nil
|
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
|
|
|
|
2017-05-02 21:40:11 +00:00
|
|
|
plugins.Serve(dbType.(*MySQL), apiTLSConfig)
|
2017-04-13 20:48:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MySQL) Type() (string, error) {
|
|
|
|
return mySQLTypeName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MySQL) getConnection() (*sql.DB, error) {
|
|
|
|
db, err := m.Connection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.(*sql.DB), nil
|
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
func (m *MySQL) CreateUser(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()
|
|
|
|
|
|
|
|
// Get the connection
|
|
|
|
db, err := m.getConnection()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if statements.CreationStatements == "" {
|
|
|
|
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
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// Execute each query
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(statements.CreationStatements, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(dbutil.QueryHelper(query, map[string]string{
|
|
|
|
"name": username,
|
|
|
|
"password": password,
|
|
|
|
"expiration": expirationStr,
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
if _, err := stmt.Exec(); err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return username, password, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOOP
|
|
|
|
func (m *MySQL) RenewUser(statements dbplugin.Statements, username string, expiration time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MySQL) RevokeUser(statements dbplugin.Statements, username string) error {
|
|
|
|
// Grab the read lock
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
// Get the connection
|
|
|
|
db, err := m.getConnection()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
revocationStmts := statements.RevocationStatements
|
|
|
|
// Use a default SQL statement for revocation if one cannot be fetched from the role
|
|
|
|
if revocationStmts == "" {
|
|
|
|
revocationStmts = defaultMysqlRevocationStmts
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a transaction
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(revocationStmts, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.Exec(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|