2015-04-25 19:05:26 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2015-12-16 17:56:20 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathRoleCreateRead(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
name := data.Get("name").(string)
|
|
|
|
|
|
|
|
// Get the role
|
|
|
|
role, err := b.Role(req.Storage, name)
|
|
|
|
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
|
|
|
|
lease, err := b.Lease(req.Storage)
|
|
|
|
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 13:36:51 +00:00
|
|
|
// Generate our username and password. The username will be the name of
|
|
|
|
// the role, truncated to role.displaynameLength, appended to a uuid,
|
|
|
|
// with the entire string truncated to role.usernameLength.
|
2016-07-10 22:57:47 +00:00
|
|
|
displayName := name
|
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
|
|
|
|
}
|
|
|
|
username := fmt.Sprintf("%s-%s", 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
|
2015-04-25 19:05:26 +00:00
|
|
|
db, err := b.DB(req.Storage)
|
|
|
|
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
|
|
|
|
for _, query := range SplitSQL(role.SQL) {
|
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,
|
|
|
|
})
|
2015-08-21 00:47:17 +00:00
|
|
|
resp.Secret.TTL = lease.Lease
|
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.
|
|
|
|
`
|