open-vault/builtin/logical/database/path_role_create.go

94 lines
2.4 KiB
Go
Raw Normal View History

2016-12-19 18:15:58 +00:00
package database
import (
"fmt"
"time"
2016-12-19 18:15:58 +00:00
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"
)
func pathRoleCreate(b *databaseBackend) *framework.Path {
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-11 18:50:34 +00:00
logical.ReadOperation: b.pathRoleCreateRead(),
2016-12-19 18:15:58 +00:00
},
HelpSynopsis: pathRoleCreateReadHelpSyn,
HelpDescription: pathRoleCreateReadHelpDesc,
}
}
2017-04-11 18:50:34 +00:00
func (b *databaseBackend) pathRoleCreateRead() framework.OperationFunc {
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.
if len(dbConfig.AllowedRoles) > 0 && !strutil.StrListContains(dbConfig.AllowedRoles, name) {
return nil, logical.ErrPermissionDenied
}
2017-04-11 18:50:34 +00:00
b.Lock()
defer b.Unlock()
2016-12-19 18:15:58 +00:00
2017-04-11 18:50:34 +00:00
// Get the Database object
db, err := b.getOrCreateDBObj(req.Storage, role.DBName)
if err != nil {
// TODO: return a resp error instead?
return nil, fmt.Errorf("cound not retrieve db with name: %s, got error: %s", role.DBName, err)
}
2016-12-19 18:15:58 +00:00
2017-04-11 18:50:34 +00:00
expiration := time.Now().Add(role.DefaultTTL)
2017-04-11 18:50:34 +00:00
// Create the user
username, password, err := db.CreateUser(role.Statements, req.DisplayName, expiration)
if err != nil {
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
}
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.
`