2015-04-25 19:05:26 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-04-25 19:05:26 +00:00
|
|
|
"fmt"
|
2016-09-23 20:05:49 +00:00
|
|
|
"strings"
|
2015-04-25 19:05:26 +00:00
|
|
|
|
2016-09-23 20:05:49 +00:00
|
|
|
"github.com/hashicorp/vault/helper/strutil"
|
2015-04-25 19:05:26 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
const SecretCredsType = "creds"
|
|
|
|
|
2016-10-05 17:52:59 +00:00
|
|
|
// defaultRevocationSQL is a default SQL statement for revoking a user. Revoking
|
2016-10-04 23:30:25 +00:00
|
|
|
// permissions for the user is done before the drop, because MySQL explicitly
|
|
|
|
// documents that open user connections will not be closed. By revoking all
|
|
|
|
// grants, at least we ensure that the open connection is useless. Dropping the
|
|
|
|
// user will only affect the next connection.
|
2016-10-05 17:52:59 +00:00
|
|
|
const defaultRevocationSQL = `
|
2016-10-03 19:59:56 +00:00
|
|
|
REVOKE ALL PRIVILEGES, GRANT OPTION FROM '{{name}}'@'%';
|
|
|
|
DROP USER '{{name}}'@'%'
|
|
|
|
`
|
|
|
|
|
2015-04-25 19:05:26 +00:00
|
|
|
func secretCreds(b *backend) *framework.Secret {
|
|
|
|
return &framework.Secret{
|
|
|
|
Type: SecretCredsType,
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"username": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Username",
|
|
|
|
},
|
|
|
|
|
|
|
|
"password": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Password",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Renew: b.secretCredsRenew,
|
|
|
|
Revoke: b.secretCredsRevoke,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) secretCredsRenew(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-04-25 19:05:26 +00:00
|
|
|
// Get the lease information
|
2018-01-19 06:44:44 +00:00
|
|
|
lease, err := b.Lease(ctx, req.Storage)
|
2015-04-25 19:05:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if lease == nil {
|
2016-01-29 22:44:09 +00:00
|
|
|
lease = &configLease{}
|
2015-04-25 19:05:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 16:20:20 +00:00
|
|
|
resp := &logical.Response{Secret: req.Secret}
|
|
|
|
resp.Secret.TTL = lease.Lease
|
|
|
|
resp.Secret.MaxTTL = lease.LeaseMax
|
|
|
|
return resp, nil
|
2015-04-25 19:05:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) secretCredsRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-10-04 23:30:25 +00:00
|
|
|
var resp *logical.Response
|
2016-09-23 20:05:49 +00:00
|
|
|
|
2015-04-25 19:05:26 +00:00
|
|
|
// Get the username from the internal data
|
|
|
|
usernameRaw, ok := req.Secret.InternalData["username"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing username internal data")
|
|
|
|
}
|
|
|
|
username, ok := usernameRaw.(string)
|
2017-08-02 23:34:58 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("usernameRaw is not a string")
|
|
|
|
}
|
2015-04-25 19:05:26 +00:00
|
|
|
|
|
|
|
// Get our connection
|
2018-01-19 06:44:44 +00:00
|
|
|
db, err := b.DB(ctx, req.Storage)
|
2015-04-25 19:05:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:30:25 +00:00
|
|
|
roleName := ""
|
2016-10-03 19:59:56 +00:00
|
|
|
roleNameRaw, ok := req.Secret.InternalData["role"]
|
2016-10-04 23:30:25 +00:00
|
|
|
if ok {
|
|
|
|
roleName = roleNameRaw.(string)
|
2016-10-04 00:20:00 +00:00
|
|
|
}
|
2016-10-02 22:53:16 +00:00
|
|
|
|
2016-10-04 23:30:25 +00:00
|
|
|
var role *roleEntry
|
2016-10-03 19:59:56 +00:00
|
|
|
if roleName != "" {
|
2018-01-19 06:44:44 +00:00
|
|
|
role, err = b.Role(ctx, req.Storage, roleName)
|
2016-10-03 19:59:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-04 23:30:25 +00:00
|
|
|
}
|
2016-10-04 21:15:29 +00:00
|
|
|
|
2016-10-04 23:30:25 +00:00
|
|
|
// Use a default SQL statement for revocation if one cannot be fetched from the role
|
2016-10-05 17:52:59 +00:00
|
|
|
revocationSQL := defaultRevocationSQL
|
2016-10-04 21:15:29 +00:00
|
|
|
|
2016-10-05 17:52:59 +00:00
|
|
|
if role != nil && role.RevocationSQL != "" {
|
|
|
|
revocationSQL = role.RevocationSQL
|
2016-10-04 23:30:25 +00:00
|
|
|
} else {
|
|
|
|
if resp == nil {
|
|
|
|
resp = &logical.Response{}
|
2016-10-04 00:20:00 +00:00
|
|
|
}
|
2016-10-04 23:30:25 +00:00
|
|
|
resp.AddWarning(fmt.Sprintf("Role %q cannot be found. Using default SQL for revoking user.", roleName))
|
2016-09-23 20:05:49 +00:00
|
|
|
}
|
2015-04-25 19:05:26 +00:00
|
|
|
|
2016-09-23 20:05:49 +00:00
|
|
|
// Start a transaction
|
|
|
|
tx, err := db.Begin()
|
2015-04-25 19:05:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-23 20:05:49 +00:00
|
|
|
defer tx.Rollback()
|
|
|
|
|
2016-10-05 17:52:59 +00:00
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(revocationSQL, ";") {
|
2016-09-23 20:05:49 +00:00
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-10-04 00:20:00 +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/
|
2016-09-23 20:05:49 +00:00
|
|
|
query = strings.Replace(query, "{{name}}", username, -1)
|
|
|
|
_, err = tx.Exec(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-25 19:05:26 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-04 21:15:29 +00:00
|
|
|
|
2016-10-04 23:30:25 +00:00
|
|
|
return resp, nil
|
2015-04-25 19:05:26 +00:00
|
|
|
}
|