2015-06-19 17:10:19 +00:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-06-19 17:10:19 +00:00
|
|
|
"fmt"
|
2015-08-20 08:07:25 +00:00
|
|
|
"strings"
|
2015-06-30 15:15:46 +00:00
|
|
|
"time"
|
2015-06-19 17:10:19 +00:00
|
|
|
|
2016-09-27 17:25:18 +00:00
|
|
|
"github.com/gocql/gocql"
|
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-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-06-19 17:10:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func pathCredsCreate(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
2015-08-21 07:56:13 +00:00
|
|
|
Pattern: "creds/" + framework.GenericNameRegex("name"),
|
2015-06-19 17:10:19 +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.pathCredsCreateRead,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathCredsCreateReadHelpSyn,
|
|
|
|
HelpDescription: pathCredsCreateReadHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathCredsCreateRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2015-06-19 17:10:19 +00:00
|
|
|
name := data.Get("name").(string)
|
|
|
|
|
|
|
|
// Get the role
|
2018-01-19 06:44:44 +00:00
|
|
|
role, err := getRole(ctx, req.Storage, name)
|
2015-06-19 17:10:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Unknown role: %s", name)), nil
|
|
|
|
}
|
|
|
|
|
2015-06-30 13:27:23 +00:00
|
|
|
displayName := req.DisplayName
|
2016-01-13 18:40:08 +00:00
|
|
|
userUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-26 20:26:23 +00:00
|
|
|
username := fmt.Sprintf("vault_%s_%s_%s_%d", name, displayName, userUUID, time.Now().Unix())
|
|
|
|
username = strings.Replace(username, "-", "_", -1)
|
2016-01-13 18:40:08 +00:00
|
|
|
password, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-19 17:10:19 +00:00
|
|
|
|
|
|
|
// Get our connection
|
2018-01-19 06:44:44 +00:00
|
|
|
session, err := b.DB(ctx, req.Storage)
|
2015-06-19 17:10:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-19 12:34:17 +00:00
|
|
|
|
2016-09-27 17:25:18 +00:00
|
|
|
// Set consistency
|
|
|
|
if role.Consistency != "" {
|
|
|
|
consistencyValue, err := gocql.ParseConsistencyWrapper(role.Consistency)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-19 12:34:17 +00:00
|
|
|
|
2016-09-27 17:25:18 +00:00
|
|
|
session.SetConsistency(consistencyValue)
|
|
|
|
}
|
2015-06-19 17:10:19 +00:00
|
|
|
|
|
|
|
// Execute each query
|
2016-08-03 18:18:22 +00:00
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(role.CreationCQL, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-19 17:10:19 +00:00
|
|
|
err = session.Query(substQuery(query, map[string]string{
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
})).Exec()
|
|
|
|
if err != nil {
|
2016-08-03 18:18:22 +00:00
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(role.RollbackCQL, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-19 17:10:19 +00:00
|
|
|
session.Query(substQuery(query, map[string]string{
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
})).Exec()
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the secret
|
|
|
|
resp := b.Secret(SecretCredsType).Response(map[string]interface{}{
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}, map[string]interface{}{
|
|
|
|
"username": username,
|
|
|
|
"role": name,
|
|
|
|
})
|
2015-08-21 00:47:17 +00:00
|
|
|
resp.Secret.TTL = role.Lease
|
2015-06-19 17:10:19 +00:00
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathCredsCreateReadHelpSyn = `
|
|
|
|
Request database credentials for a certain role.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathCredsCreateReadHelpDesc = `
|
|
|
|
This path creates database credentials for a certain role. The
|
|
|
|
database credentials will be generated on demand and will be automatically
|
|
|
|
revoked when the lease is up.
|
|
|
|
`
|