2016-12-19 18:15:58 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-04-10 19:24:16 +00:00
|
|
|
"time"
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
|
2017-04-13 17:33:34 +00:00
|
|
|
"github.com/hashicorp/vault/helper/strutil"
|
2016-12-19 18:15:58 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2017-04-25 17:39:17 +00:00
|
|
|
func pathCredsCreate(b *databaseBackend) *framework.Path {
|
2016-12-19 18:15:58 +00:00
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "creds/" + framework.GenericNameRegex("name"),
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the role.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2017-04-25 17:39:17 +00:00
|
|
|
logical.ReadOperation: b.pathCredsCreateRead(),
|
2016-12-19 18:15:58 +00:00
|
|
|
},
|
|
|
|
|
2017-04-25 17:39:17 +00:00
|
|
|
HelpSynopsis: pathCredsCreateReadHelpSyn,
|
|
|
|
HelpDescription: pathCredsCreateReadHelpDesc,
|
2016-12-19 18:15:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 17:39:17 +00:00
|
|
|
func (b *databaseBackend) pathCredsCreateRead() framework.OperationFunc {
|
2017-04-11 18:50:34 +00:00
|
|
|
return func(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
name := data.Get("name").(string)
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
// Get the role
|
|
|
|
role, err := b.Role(req.Storage, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
2017-04-24 21:03:48 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("unknown role: %s", name)), nil
|
2017-04-11 18:50:34 +00:00
|
|
|
}
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-13 17:33:34 +00:00
|
|
|
dbConfig, err := b.DatabaseConfig(req.Storage, role.DBName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If role name isn't in the database's allowed roles, send back a
|
|
|
|
// permission denied.
|
2017-10-30 20:24:25 +00:00
|
|
|
if !strutil.StrListContains(dbConfig.AllowedRoles, "*") && !strutil.StrListContainsGlob(dbConfig.AllowedRoles, name) {
|
2017-04-13 17:33:34 +00:00
|
|
|
return nil, logical.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2017-04-26 22:55:34 +00:00
|
|
|
// Grab the read lock
|
2017-04-26 22:23:14 +00:00
|
|
|
b.RLock()
|
2017-04-26 22:55:34 +00:00
|
|
|
var unlockFunc func() = b.RUnlock
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
// Get the Database object
|
2017-04-26 22:23:14 +00:00
|
|
|
db, ok := b.getDBObj(role.DBName)
|
|
|
|
if !ok {
|
|
|
|
// Upgrade lock
|
|
|
|
b.RUnlock()
|
|
|
|
b.Lock()
|
2017-04-26 22:55:34 +00:00
|
|
|
unlockFunc = b.Unlock
|
2017-04-26 22:23:14 +00:00
|
|
|
|
|
|
|
// Create a new DB object
|
|
|
|
db, err = b.createDBObj(req.Storage, role.DBName)
|
|
|
|
if err != nil {
|
2017-04-26 22:55:34 +00:00
|
|
|
unlockFunc()
|
2017-04-26 22:23:14 +00:00
|
|
|
return nil, fmt.Errorf("cound not retrieve db with name: %s, got error: %s", role.DBName, err)
|
|
|
|
}
|
2017-04-11 18:50:34 +00:00
|
|
|
}
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
expiration := time.Now().Add(role.DefaultTTL)
|
2016-12-20 19:46:20 +00:00
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: req.DisplayName,
|
|
|
|
RoleName: name,
|
|
|
|
}
|
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
// Create the user
|
2017-06-06 13:49:49 +00:00
|
|
|
username, password, err := db.CreateUser(role.Statements, usernameConfig, expiration)
|
2017-04-26 22:55:34 +00:00
|
|
|
// Unlock
|
|
|
|
unlockFunc()
|
2017-04-11 18:50:34 +00:00
|
|
|
if err != nil {
|
2017-04-26 22:55:34 +00:00
|
|
|
b.closeIfShutdown(role.DBName, err)
|
2017-04-11 18:50:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
resp := b.Secret(SecretCredsType).Response(map[string]interface{}{
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}, map[string]interface{}{
|
|
|
|
"username": username,
|
|
|
|
"role": name,
|
|
|
|
})
|
|
|
|
resp.Secret.TTL = role.DefaultTTL
|
|
|
|
return resp, nil
|
|
|
|
}
|
2016-12-19 18:15:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 17:39:17 +00:00
|
|
|
const pathCredsCreateReadHelpSyn = `
|
2016-12-19 18:15:58 +00:00
|
|
|
Request database credentials for a certain role.
|
|
|
|
`
|
|
|
|
|
2017-04-25 17:39:17 +00:00
|
|
|
const pathCredsCreateReadHelpDesc = `
|
2016-12-19 18:15:58 +00:00
|
|
|
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.
|
|
|
|
`
|