2015-03-21 16:19:37 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-03-21 16:19:37 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
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"
|
2015-03-21 16:19:37 +00:00
|
|
|
)
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
const (
|
|
|
|
tokenPolicyType = "token"
|
|
|
|
)
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
func pathToken(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
2017-12-14 18:28:19 +00:00
|
|
|
Pattern: "creds/" + framework.GenericNameRegex("role"),
|
2015-03-21 16:19:37 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2017-12-14 18:28:19 +00:00
|
|
|
"role": &framework.FieldSchema{
|
2015-03-21 16:19:37 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-14 18:28:19 +00:00
|
|
|
role := d.Get("role").(string)
|
2015-03-21 16:19:37 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := req.Storage.Get(ctx, "policy/"+role)
|
2015-03-21 16:19:37 +00:00
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error retrieving role: {{err}}", err)
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|
2015-05-27 23:36:23 +00:00
|
|
|
if entry == nil {
|
2017-12-14 18:28:19 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("role %q not found", role)), 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
|
2018-11-02 14:44:12 +00:00
|
|
|
c, userErr, intErr := b.client(ctx, req.Storage)
|
2016-05-24 18:23:27 +00:00
|
|
|
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
|
2017-12-14 18:28:19 +00:00
|
|
|
tokenName := fmt.Sprintf("Vault %s %s %d", role, req.DisplayName, time.Now().UnixNano())
|
2016-11-04 16:35:16 +00:00
|
|
|
|
2018-06-11 15:21:37 +00:00
|
|
|
writeOpts := &api.WriteOptions{}
|
2018-06-11 15:03:00 +00:00
|
|
|
writeOpts = writeOpts.WithContext(ctx)
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
// Create an ACLEntry for Consul pre 1.4
|
|
|
|
if (result.Policy != "" && result.TokenType == "client") ||
|
|
|
|
(result.Policy == "" && result.TokenType == "management") {
|
|
|
|
token, _, err := c.ACL().Create(&api.ACLEntry{
|
|
|
|
Name: tokenName,
|
|
|
|
Type: result.TokenType,
|
|
|
|
Rules: result.Policy,
|
|
|
|
}, writeOpts)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(err.Error()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the helper to create the secret
|
|
|
|
s := b.Secret(SecretTokenType).Response(map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
}, map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
"role": role,
|
|
|
|
})
|
|
|
|
s.Secret.TTL = result.TTL
|
|
|
|
s.Secret.MaxTTL = result.MaxTTL
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create an ACLToken for Consul 1.4 and above
|
|
|
|
var policyLink = []*api.ACLTokenPolicyLink{}
|
|
|
|
for _, policyName := range result.Policies {
|
|
|
|
policyLink = append(policyLink, &api.ACLTokenPolicyLink{
|
|
|
|
Name: policyName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
token, _, err := c.ACL().TokenCreate(&api.ACLToken{
|
|
|
|
Description: tokenName,
|
|
|
|
Policies: policyLink,
|
|
|
|
Local: result.Local,
|
2018-06-11 15:03:00 +00:00
|
|
|
}, writeOpts)
|
2015-03-21 16:19:37 +00:00
|
|
|
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{}{
|
2018-11-02 14:44:12 +00:00
|
|
|
"token": token.SecretID,
|
|
|
|
"accessor": token.AccessorID,
|
|
|
|
"local": token.Local,
|
2016-05-26 03:24:10 +00:00
|
|
|
}, map[string]interface{}{
|
2018-11-02 14:44:12 +00:00
|
|
|
"token": token.AccessorID,
|
|
|
|
"role": role,
|
|
|
|
"version": tokenPolicyType,
|
2016-05-26 03:24:10 +00:00
|
|
|
})
|
2018-11-02 14:44:12 +00:00
|
|
|
s.Secret.TTL = result.TTL
|
|
|
|
s.Secret.MaxTTL = result.MaxTTL
|
2016-01-29 22:44:09 +00:00
|
|
|
|
|
|
|
return s, nil
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|