2016-03-03 14:19:17 +00:00
|
|
|
package mssql
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-03-03 14:19:17 +00:00
|
|
|
"fmt"
|
2016-08-03 18:18:22 +00:00
|
|
|
"strings"
|
2016-03-03 14:19:17 +00:00
|
|
|
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/strutil"
|
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/logical"
|
2016-03-03 14:19:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func pathCredsCreate(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "creds/" + framework.GenericNameRegex("name"),
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"name": {
|
2016-03-03 14:19:17 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the role.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ReadOperation: b.pathCredsCreateRead,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathCredsCreateHelpSyn,
|
|
|
|
HelpDescription: pathCredsCreateHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathCredsCreateRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2016-03-03 14:19:17 +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)
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("unknown role: %s", name)), nil
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:15:18 +00:00
|
|
|
// Determine if we have a lease configuration
|
2018-01-19 06:44:44 +00:00
|
|
|
leaseConfig, err := b.LeaseConfig(ctx, req.Storage)
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-11 02:15:18 +00:00
|
|
|
if leaseConfig == nil {
|
|
|
|
leaseConfig = &configLease{}
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate our username and password
|
|
|
|
displayName := req.DisplayName
|
|
|
|
if len(displayName) > 10 {
|
|
|
|
displayName = displayName[:10]
|
|
|
|
}
|
|
|
|
userUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
username := fmt.Sprintf("%s-%s", displayName, userUUID)
|
|
|
|
password, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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)
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a transaction
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
// Always reset database to default db of connection. Since it is in a
|
|
|
|
// transaction, all statements will be on the same connection in the pool.
|
|
|
|
roleSQL := fmt.Sprintf("USE [%s]; %s", b.defaultDb, role.SQL)
|
|
|
|
|
|
|
|
// Execute each query
|
2016-08-03 18:18:22 +00:00
|
|
|
for _, query := range strutil.ParseArbitraryStringSlice(roleSQL, ";") {
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
if len(query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:31:09 +00:00
|
|
|
m := map[string]string{
|
2016-03-03 14:19:17 +00:00
|
|
|
"name": username,
|
|
|
|
"password": password,
|
|
|
|
}
|
2022-04-26 19:47:06 +00:00
|
|
|
if err := dbtxn.ExecuteTxQueryDirect(ctx, tx, m, query); err != nil {
|
2016-03-03 14:19:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the secret
|
|
|
|
resp := b.Secret(SecretCredsType).Response(map[string]interface{}{
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
}, map[string]interface{}{
|
|
|
|
"username": username,
|
|
|
|
})
|
2018-04-04 10:43:21 +00:00
|
|
|
resp.Secret.TTL = leaseConfig.TTL
|
|
|
|
resp.Secret.MaxTTL = leaseConfig.TTLMax
|
2018-01-18 06:43:38 +00:00
|
|
|
|
2016-03-03 14:19:17 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathCredsCreateHelpSyn = `
|
|
|
|
Request database credentials for a certain role.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathCredsCreateHelpDesc = `
|
|
|
|
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.
|
|
|
|
`
|