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-08-03 18:18:22 +00:00
|
|
|
"strings"
|
2015-04-25 19:05:26 +00:00
|
|
|
|
2015-12-16 17:56:20 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2016-08-03 18:18:22 +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"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathRoleCreate(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
2015-08-21 07:56:13 +00:00
|
|
|
Pattern: "creds/" + framework.GenericNameRegex("name"),
|
2015-04-25 19:05:26 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the role.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ReadOperation: b.pathRoleCreateRead,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathRoleCreateReadHelpSyn,
|
|
|
|
HelpDescription: pathRoleCreateReadHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathRoleCreateRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2015-04-25 19:05:26 +00:00
|
|
|
name := data.Get("name").(string)
|
|
|
|
|
|
|
|
// Get the role
|
2018-01-19 06:44:44 +00:00
|
|
|
role, err := b.Role(ctx, req.Storage, name)
|
2015-04-25 19:05:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("unknown role: %s", name)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if we have a lease
|
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
|
|
|
}
|
|
|
|
|
2016-07-20 17:17:00 +00:00
|
|
|
// Generate our username and password. The username will be a
|
|
|
|
// concatenation of:
|
|
|
|
//
|
|
|
|
// - the role name, truncated to role.rolenameLength (default 4)
|
|
|
|
// - the token display name, truncated to role.displaynameLength (default 4)
|
|
|
|
// - a UUID
|
|
|
|
//
|
2018-03-20 18:54:10 +00:00
|
|
|
// the entire concatenated string is then truncated to role.usernameLength,
|
|
|
|
// which by default is 16 due to limitations in older but still-prevalent
|
2016-07-20 17:17:00 +00:00
|
|
|
// versions of MySQL.
|
|
|
|
roleName := name
|
|
|
|
if len(roleName) > role.RolenameLength {
|
|
|
|
roleName = roleName[:role.RolenameLength]
|
|
|
|
}
|
|
|
|
displayName := req.DisplayName
|
2016-07-20 13:36:51 +00:00
|
|
|
if len(displayName) > role.DisplaynameLength {
|
|
|
|
displayName = displayName[:role.DisplaynameLength]
|
2015-04-25 19:56:11 +00:00
|
|
|
}
|
2016-01-13 18:40:08 +00:00
|
|
|
userUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-20 17:17:00 +00:00
|
|
|
username := fmt.Sprintf("%s-%s-%s", roleName, displayName, userUUID)
|
2016-07-20 13:36:51 +00:00
|
|
|
if len(username) > role.UsernameLength {
|
|
|
|
username = username[:role.UsernameLength]
|
2015-04-25 19:56:11 +00:00
|
|
|
}
|
2016-01-13 18:40:08 +00:00
|
|
|
password, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-25 19:05:26 +00:00
|
|
|
|
2016-06-29 21:20:41 +00:00
|
|
|
// Get our handle
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a transaction
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// Execute each query
|
2016-08-03 18:18:22 +00:00
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(role.SQL, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-06-29 21:20:41 +00:00
|
|
|
stmt, err := tx.Prepare(Query(query, map[string]string{
|
2015-04-25 19:05:26 +00:00
|
|
|
"name": username,
|
|
|
|
"password": password,
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-29 21:39:47 +00:00
|
|
|
defer stmt.Close()
|
2015-04-25 19:05:26 +00:00
|
|
|
if _, err := stmt.Exec(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the secret
|
|
|
|
resp := b.Secret(SecretCredsType).Response(map[string]interface{}{
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}, map[string]interface{}{
|
|
|
|
"username": username,
|
2016-10-03 19:57:47 +00:00
|
|
|
"role": name,
|
2015-04-25 19:05:26 +00:00
|
|
|
})
|
2018-01-18 06:43:38 +00:00
|
|
|
|
2018-04-03 16:20:20 +00:00
|
|
|
resp.Secret.TTL = lease.Lease
|
|
|
|
resp.Secret.MaxTTL = lease.LeaseMax
|
2018-01-18 06:43:38 +00:00
|
|
|
|
2015-04-25 19:05:26 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathRoleCreateReadHelpSyn = `
|
|
|
|
Request database credentials for a certain role.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathRoleCreateReadHelpDesc = `
|
|
|
|
This path reads database credentials for a certain role. The
|
|
|
|
database credentials will be generated on demand and will be automatically
|
|
|
|
revoked when the lease is up.
|
|
|
|
`
|