2016-12-19 18:15:58 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2017-12-14 22:03:11 +00:00
|
|
|
"context"
|
2016-12-19 18:15:58 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
const SecretCredsType = "creds"
|
|
|
|
|
|
|
|
func secretCreds(b *databaseBackend) *framework.Secret {
|
|
|
|
return &framework.Secret{
|
2017-03-08 22:46:53 +00:00
|
|
|
Type: SecretCredsType,
|
|
|
|
Fields: map[string]*framework.FieldSchema{},
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
Renew: b.secretCredsRenew(),
|
|
|
|
Revoke: b.secretCredsRevoke(),
|
2016-12-19 18:15:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
func (b *databaseBackend) secretCredsRenew() framework.OperationFunc {
|
2018-01-08 18:31:38 +00:00
|
|
|
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2017-04-11 18:50:34 +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)
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
roleNameRaw, ok := req.Secret.InternalData["role"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("could not find role with name: %s", req.Secret.InternalData["role"])
|
|
|
|
}
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
role, err := b.Role(ctx, req.Storage, roleNameRaw.(string))
|
2017-04-11 18:50:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
2017-04-26 17:29:16 +00:00
|
|
|
return nil, fmt.Errorf("error during renew: could not find role with name %s", req.Secret.InternalData["role"])
|
2017-04-11 18:50:34 +00:00
|
|
|
}
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
f := framework.LeaseExtend(role.DefaultTTL, role.MaxTTL, b.System())
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err := f(ctx, req, data)
|
2017-04-11 18:50:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-20 19:46:20 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
// Grab the read lock
|
2017-04-26 22:23:14 +00:00
|
|
|
b.RLock()
|
2018-01-18 00:21:59 +00:00
|
|
|
unlockFunc := b.RUnlock
|
2016-12-20 19:46:20 +00:00
|
|
|
|
2017-04-26 22:23:14 +00:00
|
|
|
// Get the Database object
|
|
|
|
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
|
2018-01-08 18:31:38 +00:00
|
|
|
db, err = b.createDBObj(ctx, req.Storage, role.DBName)
|
2017-04-26 22:23:14 +00:00
|
|
|
if err != nil {
|
2017-04-26 22:55:34 +00:00
|
|
|
unlockFunc()
|
2018-03-20 18:54:10 +00:00
|
|
|
return nil, fmt.Errorf("could not retrieve db with name: %s, got error: %s", role.DBName, err)
|
2017-04-26 22:23:14 +00:00
|
|
|
}
|
2016-12-19 18:15:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
// Make sure we increase the VALID UNTIL endpoint for this user.
|
|
|
|
if expireTime := resp.Secret.ExpirationTime(); !expireTime.IsZero() {
|
2018-01-08 18:31:38 +00:00
|
|
|
err := db.RenewUser(ctx, role.Statements, username, expireTime)
|
2017-04-11 18:50:34 +00:00
|
|
|
if err != nil {
|
2018-01-18 00:21:59 +00:00
|
|
|
unlockFunc()
|
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
|
|
|
|
2018-01-18 00:21:59 +00:00
|
|
|
unlockFunc()
|
2017-04-11 18:50:34 +00:00
|
|
|
return resp, nil
|
2016-12-19 18:15:58 +00:00
|
|
|
}
|
2017-04-11 18:50:34 +00:00
|
|
|
}
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
func (b *databaseBackend) secretCredsRevoke() framework.OperationFunc {
|
2018-01-08 18:31:38 +00:00
|
|
|
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2017-04-11 18:50:34 +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)
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
var resp *logical.Response
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
roleNameRaw, ok := req.Secret.InternalData["role"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no role name was provided")
|
|
|
|
}
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
role, err := b.Role(ctx, req.Storage, roleNameRaw.(string))
|
2016-12-19 18:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
2017-04-26 17:29:16 +00:00
|
|
|
return nil, fmt.Errorf("error during revoke: could not find role with name %s", req.Secret.InternalData["role"])
|
2016-12-19 18:15:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
// Grab the read lock
|
2017-04-26 22:23:14 +00:00
|
|
|
b.RLock()
|
2018-01-18 00:21:59 +00:00
|
|
|
unlockFunc := b.RUnlock
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2017-04-11 18:50:34 +00:00
|
|
|
// Get our connection
|
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
|
2018-01-08 18:31:38 +00:00
|
|
|
db, err = b.createDBObj(ctx, req.Storage, role.DBName)
|
2017-04-26 22:23:14 +00:00
|
|
|
if err != nil {
|
2017-04-26 22:55:34 +00:00
|
|
|
unlockFunc()
|
2018-03-20 18:54:10 +00:00
|
|
|
return nil, fmt.Errorf("could not retrieve db with name: %s, got error: %s", role.DBName, err)
|
2017-04-26 22:23:14 +00:00
|
|
|
}
|
2017-04-11 18:50:34 +00:00
|
|
|
}
|
2016-12-19 18:15:58 +00:00
|
|
|
|
2018-01-18 00:21:59 +00:00
|
|
|
if err := db.RevokeUser(ctx, role.Statements, username); err != nil {
|
|
|
|
unlockFunc()
|
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
|
|
|
|
2018-01-18 00:21:59 +00:00
|
|
|
unlockFunc()
|
2017-04-11 18:50:34 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
2016-12-19 18:15:58 +00:00
|
|
|
}
|