2015-04-19 01:37:27 +00:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-04-19 01:37:27 +00:00
|
|
|
"fmt"
|
2016-08-03 18:18:22 +00:00
|
|
|
"strings"
|
2015-04-19 01:37:27 +00:00
|
|
|
"time"
|
|
|
|
|
2019-01-09 00:48:57 +00:00
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-15 18:10:07 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/dbtxn"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-04-19 01:37:27 +00:00
|
|
|
_ "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-19 01:37:27 +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-19 01:37:27 +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-19 01:37:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-19 04:59:59 +00:00
|
|
|
if role == nil {
|
2015-04-19 01:37:27 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("unknown role: %s", name)), nil
|
|
|
|
}
|
|
|
|
|
2015-04-19 04:45:05 +00:00
|
|
|
// Determine if we have a lease
|
2018-01-19 06:44:44 +00:00
|
|
|
lease, err := b.Lease(ctx, req.Storage)
|
2015-04-19 04:45:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-29 22:44:09 +00:00
|
|
|
// Unlike some other backends we need a lease here (can't leave as 0 and
|
|
|
|
// let core fill it in) because Postgres also expires users as a safety
|
|
|
|
// measure, so cannot be zero
|
2015-04-19 04:45:05 +00:00
|
|
|
if lease == nil {
|
2016-01-29 22:44:09 +00:00
|
|
|
lease = &configLease{
|
|
|
|
Lease: b.System().DefaultLeaseTTL(),
|
|
|
|
}
|
2015-04-19 04:45:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 20:31:56 +00:00
|
|
|
// Generate the username, password and expiration. PG limits user to 63 characters
|
|
|
|
displayName := req.DisplayName
|
|
|
|
if len(displayName) > 26 {
|
|
|
|
displayName = displayName[:26]
|
|
|
|
}
|
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)
|
2015-06-17 20:31:56 +00:00
|
|
|
if len(username) > 63 {
|
|
|
|
username = username[:63]
|
|
|
|
}
|
2016-01-13 18:40:08 +00:00
|
|
|
password, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-04 10:43:21 +00:00
|
|
|
|
|
|
|
ttl, _, err := framework.CalculateTTL(b.System(), 0, lease.Lease, 0, lease.LeaseMax, 0, time.Time{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-07 21:44:14 +00:00
|
|
|
expiration := time.Now().
|
2018-01-18 06:43:38 +00:00
|
|
|
Add(ttl).
|
2015-09-10 15:11:49 +00:00
|
|
|
Format("2006-01-02 15:04:05-0700")
|
2015-04-19 01:37:27 +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-27 18:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a transaction
|
|
|
|
tx, err := db.Begin()
|
2015-04-19 01:37:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-28 15:03:56 +00:00
|
|
|
defer func() {
|
|
|
|
tx.Rollback()
|
|
|
|
}()
|
2015-04-27 18:31:27 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:31:09 +00:00
|
|
|
m := map[string]string{
|
2015-04-27 18:31:27 +00:00
|
|
|
"name": username,
|
|
|
|
"password": password,
|
|
|
|
"expiration": expiration,
|
|
|
|
}
|
2018-04-17 23:31:09 +00:00
|
|
|
|
|
|
|
if err := dbtxn.ExecuteTxQuery(ctx, tx, m, query); err != nil {
|
2015-04-27 18:31:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
2016-06-28 15:03:56 +00:00
|
|
|
|
2015-04-27 18:31:27 +00:00
|
|
|
if err := tx.Commit(); err != nil {
|
2015-04-19 01:37:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the secret
|
2016-06-28 15:03:56 +00:00
|
|
|
|
2015-04-19 04:45:05 +00:00
|
|
|
resp := b.Secret(SecretCredsType).Response(map[string]interface{}{
|
2015-04-19 01:37:27 +00:00
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}, map[string]interface{}{
|
|
|
|
"username": username,
|
2016-10-05 17:52:59 +00:00
|
|
|
"role": name,
|
2015-04-19 04:45:05 +00:00
|
|
|
})
|
2018-04-04 10:43:21 +00:00
|
|
|
resp.Secret.TTL = lease.Lease
|
|
|
|
resp.Secret.MaxTTL = lease.LeaseMax
|
2015-04-19 04:45:05 +00:00
|
|
|
return resp, nil
|
2015-04-19 01:37:27 +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.
|
|
|
|
`
|