2015-03-21 16:19:37 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathToken(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
2015-08-21 07:56:13 +00:00
|
|
|
Pattern: "creds/" + framework.GenericNameRegex("name"),
|
2015-03-21 16:19:37 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
2015-04-27 20:59:56 +00:00
|
|
|
Description: "Name of the role",
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ReadOperation: b.pathTokenRead,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathTokenRead(
|
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-05-26 00:33:31 +00:00
|
|
|
name := d.Get("name").(string)
|
2015-03-21 16:19:37 +00:00
|
|
|
|
2015-05-27 23:36:23 +00:00
|
|
|
entry, err := req.Storage.Get("policy/" + name)
|
2015-03-21 16:19:37 +00:00
|
|
|
if err != nil {
|
2015-04-27 20:59:56 +00:00
|
|
|
return nil, fmt.Errorf("error retrieving role: %s", err)
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|
2015-05-27 23:36:23 +00:00
|
|
|
if entry == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Role '%s' not found", name)), nil
|
2015-05-26 00:33:31 +00:00
|
|
|
}
|
2015-05-27 23:36:23 +00:00
|
|
|
|
|
|
|
var result roleConfig
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 21:16:51 +00:00
|
|
|
if result.TokenType == "" {
|
|
|
|
result.TokenType = "client"
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
// Get the consul client
|
2016-05-24 18:23:27 +00:00
|
|
|
c, userErr, intErr := client(req.Storage)
|
|
|
|
if intErr != nil {
|
|
|
|
return nil, intErr
|
|
|
|
}
|
|
|
|
if userErr != nil {
|
2016-12-01 18:22:32 +00:00
|
|
|
return logical.ErrorResponse(userErr.Error()), nil
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 16:35:16 +00:00
|
|
|
// Generate a name for the token
|
|
|
|
tokenName := fmt.Sprintf("Vault %s %s %d", name, req.DisplayName, time.Now().UnixNano())
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
// Create it
|
|
|
|
token, _, err := c.ACL().Create(&api.ACLEntry{
|
2015-05-26 00:33:31 +00:00
|
|
|
Name: tokenName,
|
2015-11-02 21:16:51 +00:00
|
|
|
Type: result.TokenType,
|
2015-05-27 23:36:23 +00:00
|
|
|
Rules: result.Policy,
|
2015-03-21 16:19:37 +00:00
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(err.Error()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the helper to create the secret
|
2016-01-29 22:44:09 +00:00
|
|
|
s := b.Secret(SecretTokenType).Response(map[string]interface{}{
|
2015-03-21 16:19:37 +00:00
|
|
|
"token": token,
|
2016-05-26 03:24:10 +00:00
|
|
|
}, map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
})
|
2016-01-29 22:44:09 +00:00
|
|
|
s.Secret.TTL = result.Lease
|
|
|
|
|
|
|
|
return s, nil
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|