2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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,
|
2022-02-09 21:44:00 +00:00
|
|
|
Description: "Name of the role.",
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
|
2022-05-19 19:43:54 +00:00
|
|
|
// The "policy" and "token_type" parameters were deprecated in Consul back in version 1.4.
|
|
|
|
// They have been removed from Consul as of version 1.11. Consider removing them here in the future.
|
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.`,
|
2022-05-20 20:48:02 +00:00
|
|
|
Deprecated: true,
|
2018-11-02 14:44:12 +00:00
|
|
|
},
|
|
|
|
|
2022-05-19 19:43:54 +00:00
|
|
|
"token_type": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Default: "client",
|
|
|
|
Description: `Which type of token to create: 'client' or 'management'. If
|
|
|
|
a 'management' token, the "policy", "policies", and "consul_roles" parameters are not
|
|
|
|
required. Defaults to 'client'.`,
|
2022-05-20 20:48:02 +00:00
|
|
|
Deprecated: true,
|
2022-05-19 19:43:54 +00:00
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"policies": {
|
2022-05-19 19:43:54 +00:00
|
|
|
Type: framework.TypeCommaStringSlice,
|
|
|
|
Description: `Use "consul_policies" instead.`,
|
|
|
|
Deprecated: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"consul_policies": {
|
2018-11-02 14:44:12 +00:00
|
|
|
Type: framework.TypeCommaStringSlice,
|
2022-05-19 19:43:54 +00:00
|
|
|
Description: `List of policies to attach to the token. Either "consul_policies"
|
|
|
|
or "consul_roles" are required for Consul 1.5 and above, or just "consul_policies" if
|
2022-02-17 01:31:08 +00:00
|
|
|
using Consul 1.4.`,
|
|
|
|
},
|
|
|
|
|
|
|
|
"consul_roles": {
|
|
|
|
Type: framework.TypeCommaStringSlice,
|
|
|
|
Description: `List of Consul roles to attach to the token. Either "policies"
|
|
|
|
or "consul_roles" are required for Consul 1.5 and above.`,
|
2018-11-02 14:44:12 +00:00
|
|
|
},
|
|
|
|
|
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
|
2022-02-17 01:31:08 +00:00
|
|
|
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
|
|
|
"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,
|
2022-02-17 01:31:08 +00:00
|
|
|
Description: `Use "ttl" instead.`,
|
2019-06-27 18:52:52 +00:00
|
|
|
Deprecated: true,
|
2015-05-26 00:33:31 +00:00
|
|
|
},
|
2022-02-09 21:44:00 +00:00
|
|
|
|
|
|
|
"consul_namespace": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Indicates which namespace that the token will be
|
|
|
|
created within. Defaults to 'default'. Available in Consul 1.7 and above.`,
|
|
|
|
},
|
|
|
|
|
|
|
|
"partition": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Indicates which admin partition that the token
|
|
|
|
will be created within. Defaults to 'default'. Available in Consul 1.11 and above.`,
|
|
|
|
},
|
2022-05-10 01:07:35 +00:00
|
|
|
|
|
|
|
"service_identities": {
|
|
|
|
Type: framework.TypeStringSlice,
|
|
|
|
Description: `List of Service Identities to attach to the
|
|
|
|
token, separated by semicolons. Available in Consul 1.5 or above.`,
|
|
|
|
},
|
|
|
|
|
|
|
|
"node_identities": {
|
|
|
|
Type: framework.TypeStringSlice,
|
|
|
|
Description: `List of Node Identities to attach to the
|
|
|
|
token. Available in Consul 1.8.1 or above.`,
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
|
2022-02-17 01:31:08 +00:00
|
|
|
var roleConfigData roleConfig
|
|
|
|
if err := entry.DecodeJSON(&roleConfigData); err != nil {
|
2015-05-27 23:36:23 +00:00
|
|
|
return nil, err
|
2015-05-26 00:33:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 01:31:08 +00:00
|
|
|
if roleConfigData.TokenType == "" {
|
|
|
|
roleConfigData.TokenType = "client"
|
2015-11-02 21:16:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 18:08:37 +00:00
|
|
|
// Generate the response
|
|
|
|
resp := &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
2022-02-17 01:31:08 +00:00
|
|
|
"lease": int64(roleConfigData.TTL.Seconds()),
|
|
|
|
"ttl": int64(roleConfigData.TTL.Seconds()),
|
|
|
|
"max_ttl": int64(roleConfigData.MaxTTL.Seconds()),
|
|
|
|
"token_type": roleConfigData.TokenType,
|
|
|
|
"local": roleConfigData.Local,
|
|
|
|
"consul_namespace": roleConfigData.ConsulNamespace,
|
|
|
|
"partition": roleConfigData.Partition,
|
2015-04-27 18:08:37 +00:00
|
|
|
},
|
|
|
|
}
|
2022-02-17 01:31:08 +00:00
|
|
|
if roleConfigData.Policy != "" {
|
|
|
|
resp.Data["policy"] = base64.StdEncoding.EncodeToString([]byte(roleConfigData.Policy))
|
2015-11-02 21:16:51 +00:00
|
|
|
}
|
2022-02-17 01:31:08 +00:00
|
|
|
if len(roleConfigData.Policies) > 0 {
|
2022-05-19 19:43:54 +00:00
|
|
|
resp.Data["consul_policies"] = roleConfigData.Policies
|
2022-02-17 01:31:08 +00:00
|
|
|
}
|
|
|
|
if len(roleConfigData.ConsulRoles) > 0 {
|
|
|
|
resp.Data["consul_roles"] = roleConfigData.ConsulRoles
|
2018-11-02 14:44:12 +00:00
|
|
|
}
|
2022-05-10 01:07:35 +00:00
|
|
|
if len(roleConfigData.ServiceIdentities) > 0 {
|
|
|
|
resp.Data["service_identities"] = roleConfigData.ServiceIdentities
|
|
|
|
}
|
|
|
|
if len(roleConfigData.NodeIdentities) > 0 {
|
|
|
|
resp.Data["node_identities"] = roleConfigData.NodeIdentities
|
|
|
|
}
|
|
|
|
|
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)
|
2022-05-19 19:43:54 +00:00
|
|
|
consulPolicies := d.Get("consul_policies").([]string)
|
2018-11-02 14:44:12 +00:00
|
|
|
policies := d.Get("policies").([]string)
|
2022-02-17 01:31:08 +00:00
|
|
|
roles := d.Get("consul_roles").([]string)
|
2022-05-10 01:07:35 +00:00
|
|
|
serviceIdentities := d.Get("service_identities").([]string)
|
|
|
|
nodeIdentities := d.Get("node_identities").([]string)
|
2022-02-17 01:31:08 +00:00
|
|
|
|
|
|
|
switch tokenType {
|
|
|
|
case "client":
|
2022-05-19 19:43:54 +00:00
|
|
|
if policy == "" && len(policies) == 0 && len(consulPolicies) == 0 &&
|
|
|
|
len(roles) == 0 && len(serviceIdentities) == 0 && len(nodeIdentities) == 0 {
|
2015-11-02 21:16:51 +00:00
|
|
|
return logical.ErrorResponse(
|
2022-05-10 01:07:35 +00:00
|
|
|
"Use either a policy document, a list of policies or roles, or a set of service or node identities, depending on your Consul version"), nil
|
2015-11-02 21:16:51 +00:00
|
|
|
}
|
2022-02-17 01:31:08 +00:00
|
|
|
case "management":
|
|
|
|
default:
|
2022-05-10 01:07:35 +00:00
|
|
|
return logical.ErrorResponse("token_type must be \"client\" or \"management\""), nil
|
2018-11-02 14:44:12 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:43:54 +00:00
|
|
|
if len(consulPolicies) == 0 {
|
|
|
|
consulPolicies = policies
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-02-09 21:44:00 +00:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
local := d.Get("local").(bool)
|
|
|
|
namespace := d.Get("consul_namespace").(string)
|
|
|
|
partition := d.Get("partition").(string)
|
2015-05-27 23:36:23 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("policy/"+name, roleConfig{
|
2022-05-10 01:07:35 +00:00
|
|
|
Policy: string(policyRaw),
|
2022-05-19 19:43:54 +00:00
|
|
|
Policies: consulPolicies,
|
2022-05-10 01:07:35 +00:00
|
|
|
ConsulRoles: roles,
|
|
|
|
ServiceIdentities: serviceIdentities,
|
|
|
|
NodeIdentities: nodeIdentities,
|
|
|
|
TokenType: tokenType,
|
|
|
|
TTL: ttl,
|
|
|
|
MaxTTL: maxTTL,
|
|
|
|
Local: local,
|
|
|
|
ConsulNamespace: namespace,
|
|
|
|
Partition: partition,
|
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 {
|
2022-05-10 01:07:35 +00:00
|
|
|
Policy string `json:"policy"`
|
|
|
|
Policies []string `json:"policies"`
|
|
|
|
ConsulRoles []string `json:"consul_roles"`
|
|
|
|
ServiceIdentities []string `json:"service_identities"`
|
|
|
|
NodeIdentities []string `json:"node_identities"`
|
|
|
|
TTL time.Duration `json:"lease"`
|
|
|
|
MaxTTL time.Duration `json:"max_ttl"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
Local bool `json:"local"`
|
|
|
|
ConsulNamespace string `json:"consul_namespace"`
|
|
|
|
Partition string `json:"partition"`
|
2015-05-27 23:36:23 +00:00
|
|
|
}
|