2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2017-11-02 20:05:48 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2017-11-02 20:05:48 +00:00
|
|
|
"github.com/hashicorp/vault/helper/identity"
|
2018-09-18 03:03:00 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
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"
|
2017-11-02 20:05:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func groupAliasPaths(i *IdentityStore) []*framework.Path {
|
|
|
|
return []*framework.Path{
|
|
|
|
{
|
|
|
|
Pattern: "group-alias$",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "ID of the group alias.",
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Alias of the group.",
|
|
|
|
},
|
|
|
|
"mount_accessor": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Mount accessor to which this alias belongs to.",
|
|
|
|
},
|
|
|
|
"canonical_id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "ID of the group to which this is an alias.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2018-01-08 18:31:38 +00:00
|
|
|
logical.UpdateOperation: i.pathGroupAliasRegister(),
|
2017-11-02 20:05:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: strings.TrimSpace(groupAliasHelp["group-alias"][0]),
|
|
|
|
HelpDescription: strings.TrimSpace(groupAliasHelp["group-alias"][1]),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: "group-alias/id/" + framework.GenericNameRegex("id"),
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "ID of the group alias.",
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Alias of the group.",
|
|
|
|
},
|
|
|
|
"mount_accessor": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Mount accessor to which this alias belongs to.",
|
|
|
|
},
|
|
|
|
"canonical_id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "ID of the group to which this is an alias.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2018-04-02 14:42:01 +00:00
|
|
|
logical.UpdateOperation: i.pathGroupAliasIDUpdate(),
|
2018-01-08 18:31:38 +00:00
|
|
|
logical.ReadOperation: i.pathGroupAliasIDRead(),
|
|
|
|
logical.DeleteOperation: i.pathGroupAliasIDDelete(),
|
2017-11-02 20:05:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: strings.TrimSpace(groupAliasHelp["group-alias-by-id"][0]),
|
2018-04-17 17:54:04 +00:00
|
|
|
HelpDescription: strings.TrimSpace(groupAliasHelp["group-alias-by-id"][1]),
|
2017-11-02 20:05:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: "group-alias/id/?$",
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2018-01-08 18:31:38 +00:00
|
|
|
logical.ListOperation: i.pathGroupAliasIDList(),
|
2017-11-02 20:05:48 +00:00
|
|
|
},
|
|
|
|
|
2018-04-17 17:54:04 +00:00
|
|
|
HelpSynopsis: strings.TrimSpace(groupAliasHelp["group-alias-id-list"][0]),
|
|
|
|
HelpDescription: strings.TrimSpace(groupAliasHelp["group-alias-id-list"][1]),
|
2017-11-02 20:05:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (i *IdentityStore) pathGroupAliasRegister() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
_, ok := d.GetOk("id")
|
|
|
|
if ok {
|
|
|
|
return i.pathGroupAliasIDUpdate()(ctx, req, d)
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
i.groupLock.Lock()
|
|
|
|
defer i.groupLock.Unlock()
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
return i.handleGroupAliasUpdateCommon(ctx, req, d, nil)
|
2018-01-08 18:31:38 +00:00
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (i *IdentityStore) pathGroupAliasIDUpdate() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
groupAliasID := d.Get("id").(string)
|
|
|
|
if groupAliasID == "" {
|
|
|
|
return logical.ErrorResponse("empty group alias ID"), nil
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
i.groupLock.Lock()
|
|
|
|
defer i.groupLock.Unlock()
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
groupAlias, err := i.MemDBAliasByID(groupAliasID, true, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if groupAlias == nil {
|
|
|
|
return logical.ErrorResponse("invalid group alias ID"), nil
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
return i.handleGroupAliasUpdateCommon(ctx, req, d, groupAlias)
|
2018-01-08 18:31:38 +00:00
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
// NOTE: Currently we don't allow by-factors modification of group aliases the
|
|
|
|
// way we do with entities. As a result if a groupAlias is defined here we know
|
|
|
|
// that this is an update, where they provided an ID parameter.
|
2018-09-18 03:03:00 +00:00
|
|
|
func (i *IdentityStore) handleGroupAliasUpdateCommon(ctx context.Context, req *logical.Request, d *framework.FieldData, groupAlias *identity.Alias) (*logical.Response, error) {
|
2019-06-18 20:42:54 +00:00
|
|
|
var newGroup, previousGroup *identity.Group
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
ns, err := namespace.FromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
if groupAlias == nil {
|
|
|
|
groupAlias = &identity.Alias{
|
|
|
|
CreationTime: ptypes.TimestampNow(),
|
|
|
|
NamespaceID: ns.ID,
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2019-06-18 20:42:54 +00:00
|
|
|
groupAlias.LastUpdateTime = groupAlias.CreationTime
|
|
|
|
} else {
|
|
|
|
if ns.ID != groupAlias.NamespaceID {
|
|
|
|
return logical.ErrorResponse("existing alias not in the same namespace as request"), logical.ErrPermissionDenied
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2019-06-18 20:42:54 +00:00
|
|
|
groupAlias.LastUpdateTime = ptypes.TimestampNow()
|
|
|
|
if groupAlias.CreationTime == nil {
|
|
|
|
groupAlias.CreationTime = groupAlias.LastUpdateTime
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get group alias name
|
2019-06-18 20:42:54 +00:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
if name == "" {
|
2017-11-02 20:05:48 +00:00
|
|
|
return logical.ErrorResponse("missing alias name"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mountAccessor := d.Get("mount_accessor").(string)
|
|
|
|
if mountAccessor == "" {
|
|
|
|
return logical.ErrorResponse("missing mount_accessor"), nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
canonicalID := d.Get("canonical_id").(string)
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
if groupAlias.Name == name && groupAlias.MountAccessor == mountAccessor && (canonicalID == "" || groupAlias.CanonicalID == canonicalID) {
|
|
|
|
// Nothing to do, be idempotent
|
|
|
|
return nil, nil
|
2018-04-23 17:46:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
// Explicitly correct for previous versions that persisted this
|
|
|
|
groupAlias.MountType = ""
|
|
|
|
|
|
|
|
// Canonical ID handling
|
|
|
|
{
|
|
|
|
if canonicalID != "" {
|
|
|
|
newGroup, err = i.MemDBGroupByID(canonicalID, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if newGroup == nil {
|
|
|
|
return logical.ErrorResponse("invalid group ID given in 'canonical_id'"), nil
|
|
|
|
}
|
|
|
|
if newGroup.Type != groupTypeExternal {
|
|
|
|
return logical.ErrorResponse("alias can't be set on an internal group"), nil
|
|
|
|
}
|
|
|
|
if newGroup.NamespaceID != groupAlias.NamespaceID {
|
|
|
|
return logical.ErrorResponse("group referenced with 'canonical_id' not in the same namespace as alias"), logical.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
groupAlias.CanonicalID = canonicalID
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
// Validate name/accessor whether new or update
|
|
|
|
{
|
2021-08-30 19:31:11 +00:00
|
|
|
mountEntry := i.router.MatchingMountByAccessor(mountAccessor)
|
2019-06-18 20:42:54 +00:00
|
|
|
if mountEntry == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("invalid mount accessor %q", mountAccessor)), nil
|
|
|
|
}
|
|
|
|
if mountEntry.Local {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("mount accessor %q is a local mount", mountAccessor)), nil
|
|
|
|
}
|
|
|
|
if mountEntry.NamespaceID != groupAlias.NamespaceID {
|
|
|
|
return logical.ErrorResponse("mount referenced via 'mount_accessor' not in the same namespace as alias"), logical.ErrPermissionDenied
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
groupAliasByFactors, err := i.MemDBAliasByFactors(mountEntry.Accessor, name, false, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
2019-06-18 20:42:54 +00:00
|
|
|
// This check will still work for the new case too since it won't have
|
|
|
|
// an ID yet
|
2017-11-02 20:05:48 +00:00
|
|
|
if groupAliasByFactors != nil && groupAliasByFactors.ID != groupAlias.ID {
|
|
|
|
return logical.ErrorResponse("combination of mount and group alias name is already in use"), nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
groupAlias.Name = name
|
|
|
|
groupAlias.MountAccessor = mountAccessor
|
|
|
|
}
|
|
|
|
|
|
|
|
switch groupAlias.ID {
|
|
|
|
case "":
|
|
|
|
// It's a new alias
|
|
|
|
if newGroup == nil {
|
|
|
|
// If this is a new alias being tied to a non-existent group,
|
|
|
|
// create a new group for it
|
|
|
|
newGroup = &identity.Group{
|
|
|
|
Type: groupTypeExternal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Fetch the group, if any, to which the alias is tied to
|
|
|
|
previousGroup, err = i.MemDBGroupByAliasID(groupAlias.ID, true)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-18 20:42:54 +00:00
|
|
|
if previousGroup == nil {
|
2017-11-02 20:05:48 +00:00
|
|
|
return nil, fmt.Errorf("group alias is not associated with a group")
|
|
|
|
}
|
2019-06-18 20:42:54 +00:00
|
|
|
if previousGroup.NamespaceID != groupAlias.NamespaceID {
|
|
|
|
return logical.ErrorResponse("previous group found for alias not in the same namespace as alias"), logical.ErrPermissionDenied
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
if newGroup == nil || newGroup.ID == previousGroup.ID {
|
|
|
|
// If newGroup is nil they didn't specify a canonical ID, so they
|
|
|
|
// aren't trying to update it; set the existing group as the "new"
|
|
|
|
// one. If it's the same ID they specified the same canonical ID,
|
|
|
|
// so follow the same behavior.
|
|
|
|
newGroup = previousGroup
|
|
|
|
previousGroup = nil
|
|
|
|
} else {
|
|
|
|
// The alias is moving, so nil out the previous group alias
|
|
|
|
previousGroup.Alias = nil
|
|
|
|
}
|
2019-06-14 16:53:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
newGroup.Alias = groupAlias
|
|
|
|
err = i.sanitizeAndUpsertGroup(ctx, newGroup, previousGroup, nil)
|
2017-11-02 20:05:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-18 20:42:54 +00:00
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"id": groupAlias.ID,
|
|
|
|
"canonical_id": newGroup.ID,
|
|
|
|
},
|
|
|
|
}, nil
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pathGroupAliasIDRead returns the properties of an alias for a given
|
|
|
|
// alias ID
|
2018-01-08 18:31:38 +00:00
|
|
|
func (i *IdentityStore) pathGroupAliasIDRead() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
groupAliasID := d.Get("id").(string)
|
|
|
|
if groupAliasID == "" {
|
|
|
|
return logical.ErrorResponse("empty group alias id"), nil
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
groupAlias, err := i.MemDBAliasByID(groupAliasID, false, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
return i.handleAliasReadCommon(ctx, groupAlias)
|
2018-01-08 18:31:38 +00:00
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pathGroupAliasIDDelete deletes the group's alias for a given group alias ID
|
2018-01-08 18:31:38 +00:00
|
|
|
func (i *IdentityStore) pathGroupAliasIDDelete() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
groupAliasID := d.Get("id").(string)
|
|
|
|
if groupAliasID == "" {
|
|
|
|
return logical.ErrorResponse("missing group alias ID"), nil
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
|
2018-06-24 11:45:53 +00:00
|
|
|
i.groupLock.Lock()
|
|
|
|
defer i.groupLock.Unlock()
|
|
|
|
|
|
|
|
txn := i.db.Txn(true)
|
|
|
|
defer txn.Abort()
|
|
|
|
|
|
|
|
alias, err := i.MemDBAliasByIDInTxn(txn, groupAliasID, false, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if alias == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
ns, err := namespace.FromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ns.ID != alias.NamespaceID {
|
2019-06-18 20:42:54 +00:00
|
|
|
return logical.ErrorResponse("request namespace is not the same as the group alias namespace"), logical.ErrPermissionDenied
|
2018-09-18 03:03:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 11:45:53 +00:00
|
|
|
group, err := i.MemDBGroupByAliasIDInTxn(txn, alias.ID, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no group tied to a valid alias, something is wrong
|
|
|
|
if group == nil {
|
|
|
|
return nil, fmt.Errorf("alias not associated to a group")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete group alias in memdb
|
|
|
|
err = i.MemDBDeleteAliasByIDInTxn(txn, group.Alias.ID, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the alias
|
|
|
|
group.Alias = nil
|
|
|
|
|
2019-05-01 17:47:41 +00:00
|
|
|
err = i.UpsertGroupInTxn(ctx, txn, group, true)
|
2018-06-24 11:45:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
txn.Commit()
|
|
|
|
|
|
|
|
return nil, nil
|
2018-01-08 18:31:38 +00:00
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pathGroupAliasIDList lists the IDs of all the valid group aliases in the
|
|
|
|
// identity store
|
2018-01-08 18:31:38 +00:00
|
|
|
func (i *IdentityStore) pathGroupAliasIDList() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-09-18 03:03:00 +00:00
|
|
|
return i.handleAliasListCommon(ctx, true)
|
2018-01-08 18:31:38 +00:00
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var groupAliasHelp = map[string][2]string{
|
|
|
|
"group-alias": {
|
|
|
|
"Creates a new group alias, or updates an existing one.",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
"group-alias-id": {
|
|
|
|
"Update, read or delete a group alias using ID.",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
"group-alias-id-list": {
|
2018-04-17 17:54:04 +00:00
|
|
|
"List all the group alias IDs.",
|
2017-11-02 20:05:48 +00:00
|
|
|
"",
|
|
|
|
},
|
|
|
|
}
|