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
|
|
|
"fmt"
|
2022-05-10 01:07:35 +00:00
|
|
|
"strings"
|
2015-03-21 16:19:37 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/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"
|
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"),
|
2023-04-13 15:31:37 +00:00
|
|
|
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationPrefix: operationPrefixConsul,
|
|
|
|
OperationVerb: "generate",
|
|
|
|
OperationSuffix: "credentials",
|
|
|
|
},
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"role": {
|
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
|
|
|
},
|
|
|
|
|
|
|
|
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)
|
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 {
|
2021-04-22 15:20:59 +00:00
|
|
|
return nil, fmt.Errorf("error retrieving role: %w", 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
|
|
|
|
2022-02-09 21:44:00 +00:00
|
|
|
var roleConfigData roleConfig
|
|
|
|
if err := entry.DecodeJSON(&roleConfigData); err != nil {
|
2015-05-27 23:36:23 +00:00
|
|
|
return nil, err
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 21:44:00 +00:00
|
|
|
if roleConfigData.TokenType == "" {
|
|
|
|
roleConfigData.TokenType = "client"
|
2015-11-02 21:16:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
2022-02-09 21:44:00 +00:00
|
|
|
if (roleConfigData.Policy != "" && roleConfigData.TokenType == "client") ||
|
|
|
|
(roleConfigData.Policy == "" && roleConfigData.TokenType == "management") {
|
2018-11-02 14:44:12 +00:00
|
|
|
token, _, err := c.ACL().Create(&api.ACLEntry{
|
|
|
|
Name: tokenName,
|
2022-02-09 21:44:00 +00:00
|
|
|
Type: roleConfigData.TokenType,
|
|
|
|
Rules: roleConfigData.Policy,
|
2018-11-02 14:44:12 +00:00
|
|
|
}, 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,
|
|
|
|
})
|
2022-02-09 21:44:00 +00:00
|
|
|
s.Secret.TTL = roleConfigData.TTL
|
|
|
|
s.Secret.MaxTTL = roleConfigData.MaxTTL
|
2018-11-02 14:44:12 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
// Create an ACLToken for Consul 1.4 and above
|
2022-02-09 21:44:00 +00:00
|
|
|
policyLinks := []*api.ACLTokenPolicyLink{}
|
|
|
|
for _, policyName := range roleConfigData.Policies {
|
|
|
|
policyLinks = append(policyLinks, &api.ACLTokenPolicyLink{
|
2018-11-02 14:44:12 +00:00
|
|
|
Name: policyName,
|
|
|
|
})
|
|
|
|
}
|
2022-02-09 21:44:00 +00:00
|
|
|
|
2022-02-17 01:31:08 +00:00
|
|
|
roleLinks := []*api.ACLTokenRoleLink{}
|
|
|
|
for _, roleName := range roleConfigData.ConsulRoles {
|
|
|
|
roleLinks = append(roleLinks, &api.ACLTokenRoleLink{
|
|
|
|
Name: roleName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-10 01:07:35 +00:00
|
|
|
aclServiceIdentities := parseServiceIdentities(roleConfigData.ServiceIdentities)
|
|
|
|
aclNodeIdentities := parseNodeIdentities(roleConfigData.NodeIdentities)
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
token, _, err := c.ACL().TokenCreate(&api.ACLToken{
|
2022-05-10 01:07:35 +00:00
|
|
|
Description: tokenName,
|
|
|
|
Policies: policyLinks,
|
|
|
|
Roles: roleLinks,
|
|
|
|
ServiceIdentities: aclServiceIdentities,
|
|
|
|
NodeIdentities: aclNodeIdentities,
|
|
|
|
Local: roleConfigData.Local,
|
|
|
|
Namespace: roleConfigData.ConsulNamespace,
|
|
|
|
Partition: roleConfigData.Partition,
|
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{}{
|
2022-02-09 21:44:00 +00:00
|
|
|
"token": token.SecretID,
|
|
|
|
"accessor": token.AccessorID,
|
|
|
|
"local": token.Local,
|
|
|
|
"consul_namespace": token.Namespace,
|
|
|
|
"partition": token.Partition,
|
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
|
|
|
})
|
2022-02-09 21:44:00 +00:00
|
|
|
s.Secret.TTL = roleConfigData.TTL
|
|
|
|
s.Secret.MaxTTL = roleConfigData.MaxTTL
|
2016-01-29 22:44:09 +00:00
|
|
|
|
|
|
|
return s, nil
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|
2022-05-10 01:07:35 +00:00
|
|
|
|
|
|
|
func parseServiceIdentities(data []string) []*api.ACLServiceIdentity {
|
|
|
|
aclServiceIdentities := []*api.ACLServiceIdentity{}
|
|
|
|
|
|
|
|
for _, serviceIdentity := range data {
|
|
|
|
entry := &api.ACLServiceIdentity{}
|
|
|
|
components := strings.Split(serviceIdentity, ":")
|
|
|
|
entry.ServiceName = components[0]
|
|
|
|
if len(components) == 2 {
|
|
|
|
entry.Datacenters = strings.Split(components[1], ",")
|
|
|
|
}
|
|
|
|
aclServiceIdentities = append(aclServiceIdentities, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return aclServiceIdentities
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseNodeIdentities(data []string) []*api.ACLNodeIdentity {
|
|
|
|
aclNodeIdentities := []*api.ACLNodeIdentity{}
|
|
|
|
|
|
|
|
for _, nodeIdentity := range data {
|
|
|
|
entry := &api.ACLNodeIdentity{}
|
|
|
|
components := strings.Split(nodeIdentity, ":")
|
|
|
|
entry.NodeName = components[0]
|
|
|
|
if len(components) > 1 {
|
|
|
|
entry.Datacenter = components[1]
|
|
|
|
}
|
|
|
|
aclNodeIdentities = append(aclNodeIdentities, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return aclNodeIdentities
|
|
|
|
}
|