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
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2015-05-26 00:33:31 +00:00
|
|
|
"time"
|
2015-03-21 16:19:37 +00:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2016-11-04 16:35:16 +00:00
|
|
|
func pathListRoles(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "roles/?$",
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ListOperation: b.pathRoleList,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
func pathRoles(b *backend) *framework.Path {
|
2015-03-21 16:19:37 +00:00
|
|
|
return &framework.Path{
|
2015-08-21 07:56:13 +00:00
|
|
|
Pattern: "roles/" + framework.GenericNameRegex("name"),
|
2015-03-21 16:19:37 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"name": {
|
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
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"policy": {
|
2015-11-02 21:16:51 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Policy document, base64 encoded. Required
|
2018-11-02 14:44:12 +00:00
|
|
|
for 'client' tokens. Required for Consul pre-1.4.`,
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"policies": {
|
2018-11-02 14:44:12 +00:00
|
|
|
Type: framework.TypeCommaStringSlice,
|
|
|
|
Description: `List of policies to attach to the token. Required
|
|
|
|
for Consul 1.4 or above.`,
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"local": {
|
2018-11-02 14:44:12 +00:00
|
|
|
Type: framework.TypeBool,
|
|
|
|
Description: `Indicates that the token should not be replicated globally
|
|
|
|
and instead be local to the current datacenter. Available in Consul 1.4 and above.`,
|
2015-11-02 21:16:51 +00:00
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"token_type": {
|
2015-11-02 21:16:51 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Default: "client",
|
|
|
|
Description: `Which type of token to create: 'client'
|
|
|
|
or 'management'. If a 'management' token,
|
|
|
|
the "policy" parameter is not required.
|
|
|
|
Defaults to 'client'.`,
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
2015-05-26 00:33:31 +00:00
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"ttl": {
|
2018-11-02 14:44:12 +00:00
|
|
|
Type: framework.TypeDurationSecond,
|
|
|
|
Description: "TTL for the Consul token created from the role.",
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"max_ttl": {
|
2018-11-02 14:44:12 +00:00
|
|
|
Type: framework.TypeDurationSecond,
|
|
|
|
Description: "Max TTL for the Consul token created from the role.",
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"lease": {
|
2017-12-14 18:28:19 +00:00
|
|
|
Type: framework.TypeDurationSecond,
|
2019-06-27 18:52:52 +00:00
|
|
|
Description: "Use ttl instead.",
|
|
|
|
Deprecated: true,
|
2015-05-26 00:33:31 +00:00
|
|
|
},
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2018-11-02 14:44:12 +00:00
|
|
|
logical.ReadOperation: b.pathRolesRead,
|
|
|
|
logical.UpdateOperation: b.pathRolesWrite,
|
|
|
|
logical.DeleteOperation: b.pathRolesDelete,
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathRoleList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
entries, err := req.Storage.List(ctx, "policy/")
|
2016-11-04 16:35:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return logical.ListResponse(entries), nil
|
|
|
|
}
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
func (b *backend) pathRolesRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-04-27 18:08:37 +00:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := req.Storage.Get(ctx, "policy/"+name)
|
2015-04-27 18:08:37 +00:00
|
|
|
if err != nil {
|
2015-05-27 23:36:23 +00:00
|
|
|
return nil, err
|
2015-04-27 18:08:37 +00:00
|
|
|
}
|
2015-05-27 23:36:23 +00:00
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
2015-04-27 18:08:37 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 23:36:23 +00:00
|
|
|
var result roleConfig
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
2015-05-26 00:33:31 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 21:16:51 +00:00
|
|
|
if result.TokenType == "" {
|
|
|
|
result.TokenType = "client"
|
|
|
|
}
|
|
|
|
|
2015-04-27 18:08:37 +00:00
|
|
|
// Generate the response
|
|
|
|
resp := &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
2018-11-02 14:44:12 +00:00
|
|
|
"lease": int64(result.TTL.Seconds()),
|
|
|
|
"ttl": int64(result.TTL.Seconds()),
|
|
|
|
"max_ttl": int64(result.MaxTTL.Seconds()),
|
2015-11-02 21:16:51 +00:00
|
|
|
"token_type": result.TokenType,
|
2018-11-02 14:44:12 +00:00
|
|
|
"local": result.Local,
|
2015-04-27 18:08:37 +00:00
|
|
|
},
|
|
|
|
}
|
2015-11-02 21:16:51 +00:00
|
|
|
if result.Policy != "" {
|
|
|
|
resp.Data["policy"] = base64.StdEncoding.EncodeToString([]byte(result.Policy))
|
|
|
|
}
|
2018-11-02 14:44:12 +00:00
|
|
|
if len(result.Policies) > 0 {
|
|
|
|
resp.Data["policies"] = result.Policies
|
|
|
|
}
|
2015-04-27 18:08:37 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
func (b *backend) pathRolesWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-11-02 21:16:51 +00:00
|
|
|
tokenType := d.Get("token_type").(string)
|
|
|
|
policy := d.Get("policy").(string)
|
2018-11-02 14:44:12 +00:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
policies := d.Get("policies").([]string)
|
|
|
|
local := d.Get("local").(bool)
|
|
|
|
|
|
|
|
if len(policies) == 0 {
|
|
|
|
switch tokenType {
|
|
|
|
case "client":
|
|
|
|
if policy == "" {
|
|
|
|
return logical.ErrorResponse(
|
|
|
|
"Use either a policy document, or a list of policies, depending on your Consul version"), nil
|
|
|
|
}
|
|
|
|
case "management":
|
|
|
|
default:
|
2015-11-02 21:16:51 +00:00
|
|
|
return logical.ErrorResponse(
|
2018-11-02 14:44:12 +00:00
|
|
|
"token_type must be \"client\" or \"management\""), nil
|
2015-11-02 21:16:51 +00:00
|
|
|
}
|
2018-11-02 14:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
policyRaw, err := base64.StdEncoding.DecodeString(policy)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
|
|
|
"Error decoding policy base64: %s", err)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ttl time.Duration
|
|
|
|
ttlRaw, ok := d.GetOk("ttl")
|
|
|
|
if ok {
|
|
|
|
ttl = time.Second * time.Duration(ttlRaw.(int))
|
|
|
|
} else {
|
|
|
|
leaseParamRaw, ok := d.GetOk("lease")
|
|
|
|
if ok {
|
|
|
|
ttl = time.Second * time.Duration(leaseParamRaw.(int))
|
2015-11-02 21:16:51 +00:00
|
|
|
}
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|
2016-01-29 22:44:09 +00:00
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
var maxTTL time.Duration
|
|
|
|
maxTTLRaw, ok := d.GetOk("max_ttl")
|
2017-12-14 18:28:19 +00:00
|
|
|
if ok {
|
2018-11-02 14:44:12 +00:00
|
|
|
maxTTL = time.Second * time.Duration(maxTTLRaw.(int))
|
2015-05-26 00:33:31 +00:00
|
|
|
}
|
2015-05-27 23:36:23 +00:00
|
|
|
|
|
|
|
entry, err := logical.StorageEntryJSON("policy/"+name, roleConfig{
|
2015-11-02 21:16:51 +00:00
|
|
|
Policy: string(policyRaw),
|
2018-11-02 14:44:12 +00:00
|
|
|
Policies: policies,
|
2015-11-02 21:16:51 +00:00
|
|
|
TokenType: tokenType,
|
2018-11-02 14:44:12 +00:00
|
|
|
TTL: ttl,
|
|
|
|
MaxTTL: maxTTL,
|
|
|
|
Local: local,
|
2015-05-26 00:33:31 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Put(ctx, entry); err != nil {
|
2015-05-27 23:36:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2015-04-27 18:08:37 +00:00
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
func (b *backend) pathRolesDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-04-27 18:08:37 +00:00
|
|
|
name := d.Get("name").(string)
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Delete(ctx, "policy/"+name); err != nil {
|
2015-04-27 18:08:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-05-27 23:36:23 +00:00
|
|
|
|
|
|
|
type roleConfig struct {
|
2015-11-02 21:16:51 +00:00
|
|
|
Policy string `json:"policy"`
|
2018-11-02 14:44:12 +00:00
|
|
|
Policies []string `json:"policies"`
|
|
|
|
TTL time.Duration `json:"lease"`
|
|
|
|
MaxTTL time.Duration `json:"max_ttl"`
|
2015-11-02 21:16:51 +00:00
|
|
|
TokenType string `json:"token_type"`
|
2018-11-02 14:44:12 +00:00
|
|
|
Local bool `json:"local"`
|
2015-05-27 23:36:23 +00:00
|
|
|
}
|