2017-09-20 20:59:35 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2017-09-20 20:59:35 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2017-12-15 22:06:56 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2017-09-20 20:59:35 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-09-20 20:59:35 +00:00
|
|
|
)
|
|
|
|
|
2018-08-16 19:48:23 +00:00
|
|
|
// maxTokenNameLength is the maximum length for the name of a Nomad access
|
|
|
|
// token
|
|
|
|
const maxTokenNameLength = 256
|
|
|
|
|
2017-11-07 14:58:19 +00:00
|
|
|
func pathCredsCreate(b *backend) *framework.Path {
|
2017-09-20 20:59:35 +00:00
|
|
|
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{
|
|
|
|
logical.ReadOperation: b.pathTokenRead,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathTokenRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2017-09-20 20:59:35 +00:00
|
|
|
name := d.Get("name").(string)
|
2018-08-16 19:48:23 +00:00
|
|
|
conf, _ := b.readConfigAccess(ctx, req.Storage)
|
|
|
|
// establish a default
|
|
|
|
tokenNameLength := maxTokenNameLength
|
|
|
|
if conf != nil && conf.MaxTokenNameLength > 0 {
|
|
|
|
tokenNameLength = conf.MaxTokenNameLength
|
|
|
|
}
|
2017-09-20 20:59:35 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
role, err := b.Role(ctx, req.Storage, name)
|
2017-09-20 20:59:35 +00:00
|
|
|
if err != nil {
|
2017-12-15 22:06:56 +00:00
|
|
|
return nil, errwrap.Wrapf("error retrieving role: {{err}}", err)
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|
2017-11-07 14:58:19 +00:00
|
|
|
if role == nil {
|
2017-12-15 22:06:56 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("role %q not found", name)), nil
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 14:58:19 +00:00
|
|
|
// Determine if we have a lease configuration
|
2018-01-19 06:44:44 +00:00
|
|
|
leaseConfig, err := b.LeaseConfig(ctx, req.Storage)
|
2017-11-07 14:58:19 +00:00
|
|
|
if err != nil {
|
2017-09-20 20:59:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-07 14:58:19 +00:00
|
|
|
if leaseConfig == nil {
|
|
|
|
leaseConfig = &configLease{}
|
|
|
|
}
|
2017-09-20 20:59:35 +00:00
|
|
|
|
|
|
|
// Get the nomad client
|
2018-01-19 06:44:44 +00:00
|
|
|
c, err := b.client(ctx, req.Storage)
|
2017-11-06 21:34:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a name for the token
|
2017-12-15 22:06:56 +00:00
|
|
|
tokenName := fmt.Sprintf("vault-%s-%s-%d", name, req.DisplayName, time.Now().UnixNano())
|
2017-09-20 20:59:35 +00:00
|
|
|
|
2018-08-16 19:48:23 +00:00
|
|
|
// Note: if the given role name is sufficiently long, the UnixNano() portion
|
|
|
|
// of the pseudo randomized token name is the part that gets trimmed off,
|
|
|
|
// weakening it's randomness.
|
|
|
|
if len(tokenName) > tokenNameLength {
|
|
|
|
tokenName = tokenName[:tokenNameLength]
|
2018-02-20 15:16:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 20:59:35 +00:00
|
|
|
// Create it
|
|
|
|
token, _, err := c.ACLTokens().Create(&api.ACLToken{
|
|
|
|
Name: tokenName,
|
2017-11-07 14:58:19 +00:00
|
|
|
Type: role.TokenType,
|
2017-11-29 16:31:17 +00:00
|
|
|
Policies: role.Policies,
|
2017-11-07 14:58:19 +00:00
|
|
|
Global: role.Global,
|
2017-09-20 20:59:35 +00:00
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
2017-11-29 10:58:02 +00:00
|
|
|
return nil, err
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the helper to create the secret
|
2017-11-07 14:58:19 +00:00
|
|
|
resp := b.Secret(SecretTokenType).Response(map[string]interface{}{
|
2017-09-20 22:14:35 +00:00
|
|
|
"secret_id": token.SecretID,
|
|
|
|
"accessor_id": token.AccessorID,
|
2017-09-20 20:59:35 +00:00
|
|
|
}, map[string]interface{}{
|
2017-09-20 22:14:35 +00:00
|
|
|
"accessor_id": token.AccessorID,
|
2017-09-20 20:59:35 +00:00
|
|
|
})
|
2017-11-07 14:58:19 +00:00
|
|
|
resp.Secret.TTL = leaseConfig.TTL
|
2018-04-03 16:20:20 +00:00
|
|
|
resp.Secret.MaxTTL = leaseConfig.MaxTTL
|
2017-09-20 20:59:35 +00:00
|
|
|
|
2017-11-07 14:58:19 +00:00
|
|
|
return resp, nil
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|