2017-10-11 17:21:20 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2017-10-11 17:21:20 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2017-10-11 17:21:20 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
|
|
|
"github.com/hashicorp/vault/helper/identity"
|
2018-06-24 11:45:53 +00:00
|
|
|
"github.com/hashicorp/vault/helper/locksutil"
|
|
|
|
"github.com/hashicorp/vault/helper/storagepacker"
|
2017-10-11 17:21:20 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
// aliasPaths returns the API endpoints to operate on aliases.
|
|
|
|
// Following are the paths supported:
|
2017-11-14 01:59:42 +00:00
|
|
|
// entity-alias - To register/modify an alias
|
|
|
|
// entity-alias/id - To read, modify, delete and list aliases based on their ID
|
2017-10-11 17:21:20 +00:00
|
|
|
func aliasPaths(i *IdentityStore) []*framework.Path {
|
|
|
|
return []*framework.Path{
|
2017-11-02 20:05:48 +00:00
|
|
|
{
|
|
|
|
Pattern: "entity-alias$",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"id": {
|
|
|
|
Type: framework.TypeString,
|
2017-11-14 01:59:42 +00:00
|
|
|
Description: "ID of the entity alias. If set, updates the corresponding entity alias.",
|
2017-11-02 20:05:48 +00:00
|
|
|
},
|
2017-11-14 01:59:42 +00:00
|
|
|
// entity_id is deprecated in favor of canonical_id
|
2017-11-02 20:05:48 +00:00
|
|
|
"entity_id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Entity ID to which this alias belongs to",
|
|
|
|
},
|
|
|
|
"canonical_id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Entity ID to which this alias belongs to",
|
|
|
|
},
|
|
|
|
"mount_accessor": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Mount accessor to which this alias belongs to",
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the alias",
|
|
|
|
},
|
|
|
|
"metadata": {
|
2017-11-14 01:59:42 +00:00
|
|
|
Type: framework.TypeKVPairs,
|
|
|
|
Description: `Metadata to be associated with the alias.
|
|
|
|
In CLI, this parameter can be repeated multiple times, and it all gets merged together.
|
|
|
|
For example:
|
|
|
|
vault <command> <path> metadata=key1=value1 metadata=key2=value2
|
|
|
|
`,
|
2017-11-02 20:05:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2018-01-08 18:31:38 +00:00
|
|
|
logical.UpdateOperation: i.pathAliasRegister(),
|
2017-11-02 20:05:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: strings.TrimSpace(aliasHelp["alias"][0]),
|
|
|
|
HelpDescription: strings.TrimSpace(aliasHelp["alias"][1]),
|
|
|
|
},
|
|
|
|
// BC path for identity/entity-alias
|
2017-10-11 17:21:20 +00:00
|
|
|
{
|
|
|
|
Pattern: "alias$",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "ID of the alias",
|
|
|
|
},
|
2017-11-02 20:05:48 +00:00
|
|
|
// entity_id is deprecated
|
2017-10-11 17:21:20 +00:00
|
|
|
"entity_id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Entity ID to which this alias belongs to",
|
|
|
|
},
|
2017-11-02 20:05:48 +00:00
|
|
|
"canonical_id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Entity ID to which this alias belongs to",
|
|
|
|
},
|
2017-10-11 17:21:20 +00:00
|
|
|
"mount_accessor": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Mount accessor to which this alias belongs to",
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the alias",
|
|
|
|
},
|
|
|
|
"metadata": {
|
2017-11-14 01:59:42 +00:00
|
|
|
Type: framework.TypeKVPairs,
|
|
|
|
Description: `Metadata to be associated with the alias.
|
|
|
|
In CLI, this parameter can be repeated multiple times, and it all gets merged together.
|
|
|
|
For example:
|
|
|
|
vault <command> <path> metadata=key1=value1 metadata=key2=value2
|
|
|
|
`,
|
2017-10-11 17:21:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2018-01-08 18:31:38 +00:00
|
|
|
logical.UpdateOperation: i.pathAliasRegister(),
|
2017-10-11 17:21:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: strings.TrimSpace(aliasHelp["alias"][0]),
|
|
|
|
HelpDescription: strings.TrimSpace(aliasHelp["alias"][1]),
|
|
|
|
},
|
2017-11-02 20:05:48 +00:00
|
|
|
{
|
|
|
|
Pattern: "entity-alias/id/" + framework.GenericNameRegex("id"),
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "ID of the alias",
|
|
|
|
},
|
|
|
|
// entity_id is deprecated
|
|
|
|
"entity_id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Entity ID to which this alias belongs to",
|
|
|
|
},
|
|
|
|
"canonical_id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Entity ID to which this alias should be tied to",
|
|
|
|
},
|
|
|
|
"mount_accessor": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Mount accessor to which this alias belongs to",
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the alias",
|
|
|
|
},
|
|
|
|
"metadata": {
|
2017-11-14 01:59:42 +00:00
|
|
|
Type: framework.TypeKVPairs,
|
|
|
|
Description: `Metadata to be associated with the alias.
|
|
|
|
In CLI, this parameter can be repeated multiple times, and it all gets merged together.
|
|
|
|
For example:
|
|
|
|
vault <command> <path> metadata=key1=value1 metadata=key2=value2
|
|
|
|
`,
|
2017-10-11 17:21:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2018-01-08 18:31:38 +00:00
|
|
|
logical.UpdateOperation: i.pathAliasIDUpdate(),
|
|
|
|
logical.ReadOperation: i.pathAliasIDRead(),
|
|
|
|
logical.DeleteOperation: i.pathAliasIDDelete(),
|
2017-10-11 17:21:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: strings.TrimSpace(aliasHelp["alias-id"][0]),
|
|
|
|
HelpDescription: strings.TrimSpace(aliasHelp["alias-id"][1]),
|
|
|
|
},
|
2017-11-02 20:05:48 +00:00
|
|
|
{
|
|
|
|
Pattern: "entity-alias/id/?$",
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2018-01-08 18:31:38 +00:00
|
|
|
logical.ListOperation: i.pathAliasIDList(),
|
2017-11-02 20:05:48 +00:00
|
|
|
},
|
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
HelpSynopsis: strings.TrimSpace(aliasHelp["alias-id-list"][0]),
|
|
|
|
HelpDescription: strings.TrimSpace(aliasHelp["alias-id-list"][1]),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pathAliasRegister is used to register new alias
|
2018-01-08 18:31:38 +00:00
|
|
|
func (i *IdentityStore) pathAliasRegister() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
_, ok := d.GetOk("id")
|
|
|
|
if ok {
|
|
|
|
return i.pathAliasIDUpdate()(ctx, req, d)
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
return i.handleAliasUpdateCommon(req, d, nil)
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
// pathAliasIDUpdate is used to update an alias based on the given
|
2017-10-11 17:21:20 +00:00
|
|
|
// alias ID
|
2018-01-08 18:31:38 +00:00
|
|
|
func (i *IdentityStore) pathAliasIDUpdate() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
// Get alias id
|
|
|
|
aliasID := d.Get("id").(string)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
if aliasID == "" {
|
|
|
|
return logical.ErrorResponse("empty alias ID"), nil
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
alias, err := i.MemDBAliasByID(aliasID, true, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if alias == nil {
|
|
|
|
return logical.ErrorResponse("invalid alias id"), nil
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
return i.handleAliasUpdateCommon(req, d, alias)
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
// handleAliasUpdateCommon is used to update an alias
|
2017-10-11 17:21:20 +00:00
|
|
|
func (i *IdentityStore) handleAliasUpdateCommon(req *logical.Request, d *framework.FieldData, alias *identity.Alias) (*logical.Response, error) {
|
|
|
|
var err error
|
|
|
|
var newAlias bool
|
|
|
|
var entity *identity.Entity
|
|
|
|
var previousEntity *identity.Entity
|
|
|
|
|
|
|
|
// Alias will be nil when a new alias is being registered; create a
|
|
|
|
// new struct in that case.
|
|
|
|
if alias == nil {
|
|
|
|
alias = &identity.Alias{}
|
|
|
|
newAlias = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get entity id
|
2017-11-02 20:05:48 +00:00
|
|
|
canonicalID := d.Get("entity_id").(string)
|
|
|
|
if canonicalID == "" {
|
|
|
|
canonicalID = d.Get("canonical_id").(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if canonicalID != "" {
|
|
|
|
entity, err = i.MemDBEntityByID(canonicalID, true)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entity == nil {
|
|
|
|
return logical.ErrorResponse("invalid entity ID"), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get alias name
|
|
|
|
aliasName := d.Get("name").(string)
|
|
|
|
if aliasName == "" {
|
|
|
|
return logical.ErrorResponse("missing alias name"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mountAccessor := d.Get("mount_accessor").(string)
|
|
|
|
if mountAccessor == "" {
|
|
|
|
return logical.ErrorResponse("missing mount_accessor"), nil
|
|
|
|
}
|
|
|
|
|
2018-04-03 02:17:33 +00:00
|
|
|
mountValidationResp := i.core.router.validateMountByAccessor(mountAccessor)
|
2017-10-11 17:21:20 +00:00
|
|
|
if mountValidationResp == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("invalid mount accessor %q", mountAccessor)), nil
|
|
|
|
}
|
|
|
|
|
2018-04-23 17:46:14 +00:00
|
|
|
if mountValidationResp.MountLocal {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("mount_accessor %q is of a local mount", mountAccessor)), nil
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
// Get alias metadata
|
2017-11-08 19:51:40 +00:00
|
|
|
metadata, ok, err := d.GetOkErr("metadata")
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("failed to parse metadata: %v", err)), nil
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
var aliasMetadata map[string]string
|
|
|
|
if ok {
|
2017-11-08 19:51:40 +00:00
|
|
|
aliasMetadata = metadata.(map[string]string)
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
aliasByFactors, err := i.MemDBAliasByFactors(mountValidationResp.MountAccessor, aliasName, false, false)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &logical.Response{}
|
|
|
|
|
|
|
|
if newAlias {
|
|
|
|
if aliasByFactors != nil {
|
|
|
|
return logical.ErrorResponse("combination of mount and alias name is already in use"), nil
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
// If this is an alias being tied to a non-existent entity, create
|
2017-10-11 17:21:20 +00:00
|
|
|
// a new entity for it.
|
|
|
|
if entity == nil {
|
|
|
|
entity = &identity.Entity{
|
|
|
|
Aliases: []*identity.Alias{
|
|
|
|
alias,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entity.Aliases = append(entity.Aliases, alias)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Verify that the combination of alias name and mount is not
|
|
|
|
// already tied to a different alias
|
|
|
|
if aliasByFactors != nil && aliasByFactors.ID != alias.ID {
|
|
|
|
return logical.ErrorResponse("combination of mount and alias name is already in use"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the entity to which the alias is tied to
|
2017-11-02 20:05:48 +00:00
|
|
|
existingEntity, err := i.MemDBEntityByAliasID(alias.ID, true)
|
2017-10-11 17:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingEntity == nil {
|
|
|
|
return nil, fmt.Errorf("alias is not associated with an entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
if entity != nil && entity.ID != existingEntity.ID {
|
|
|
|
// Alias should be transferred from 'existingEntity' to 'entity'
|
2018-06-24 11:45:53 +00:00
|
|
|
for aliasIndex, item := range existingEntity.Aliases {
|
|
|
|
if item.ID == alias.ID {
|
|
|
|
entity.Aliases = append(existingEntity.Aliases[:aliasIndex], existingEntity.Aliases[aliasIndex+1:]...)
|
|
|
|
break
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
2018-06-24 11:45:53 +00:00
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
previousEntity = existingEntity
|
|
|
|
entity.Aliases = append(entity.Aliases, alias)
|
|
|
|
resp.AddWarning(fmt.Sprintf("alias is being transferred from entity %q to %q", existingEntity.ID, entity.ID))
|
|
|
|
} else {
|
|
|
|
// Update entity with modified alias
|
2018-06-24 11:45:53 +00:00
|
|
|
aliasFound := false
|
|
|
|
for aliasIndex, item := range existingEntity.Aliases {
|
|
|
|
if item.ID == alias.ID {
|
|
|
|
aliasFound = true
|
|
|
|
existingEntity.Aliases[aliasIndex] = alias
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !aliasFound {
|
|
|
|
return nil, fmt.Errorf("alias does not exist in entity")
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
2018-06-24 11:45:53 +00:00
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
entity = existingEntity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID creation and other validations; This is more useful for new entities
|
|
|
|
// and may not perform anything for the existing entities. Placing the
|
|
|
|
// check here to make the flow common for both new and existing entities.
|
|
|
|
err = i.sanitizeEntity(entity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the fields
|
|
|
|
alias.Name = aliasName
|
|
|
|
alias.Metadata = aliasMetadata
|
|
|
|
alias.MountAccessor = mountValidationResp.MountAccessor
|
2018-05-25 18:34:24 +00:00
|
|
|
|
|
|
|
// Explicitly set to empty as in the past we incorrectly saved it
|
|
|
|
alias.MountPath = ""
|
|
|
|
alias.MountType = ""
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
// Set the canonical ID in the alias index. This should be done after
|
2017-10-11 17:21:20 +00:00
|
|
|
// sanitizing entity.
|
2017-11-02 20:05:48 +00:00
|
|
|
alias.CanonicalID = entity.ID
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
// ID creation and other validations
|
|
|
|
err = i.sanitizeAlias(alias)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index entity and its aliases in MemDB and persist entity along with
|
|
|
|
// aliases in storage. If the alias is being transferred over from
|
|
|
|
// one entity to another, previous entity needs to get refreshed in MemDB
|
|
|
|
// and persisted in storage as well.
|
|
|
|
err = i.upsertEntity(entity, previousEntity, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return ID of both alias and entity
|
|
|
|
resp.Data = map[string]interface{}{
|
2017-11-02 20:05:48 +00:00
|
|
|
"id": alias.ID,
|
|
|
|
"canonical_id": entity.ID,
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
// pathAliasIDRead returns the properties of an alias for a given
|
2017-10-11 17:21:20 +00:00
|
|
|
// alias ID
|
2018-01-08 18:31:38 +00:00
|
|
|
func (i *IdentityStore) pathAliasIDRead() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
aliasID := d.Get("id").(string)
|
|
|
|
if aliasID == "" {
|
|
|
|
return logical.ErrorResponse("missing alias id"), nil
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
alias, err := i.MemDBAliasByID(aliasID, false, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
return i.handleAliasReadCommon(alias)
|
|
|
|
}
|
2017-11-02 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IdentityStore) handleAliasReadCommon(alias *identity.Alias) (*logical.Response, error) {
|
2017-10-11 17:21:20 +00:00
|
|
|
if alias == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
respData := map[string]interface{}{}
|
|
|
|
respData["id"] = alias.ID
|
2017-11-02 20:05:48 +00:00
|
|
|
respData["canonical_id"] = alias.CanonicalID
|
2017-10-11 17:21:20 +00:00
|
|
|
respData["mount_accessor"] = alias.MountAccessor
|
|
|
|
respData["metadata"] = alias.Metadata
|
|
|
|
respData["name"] = alias.Name
|
2017-11-02 20:05:48 +00:00
|
|
|
respData["merged_from_canonical_ids"] = alias.MergedFromCanonicalIDs
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2018-05-25 18:34:24 +00:00
|
|
|
if mountValidationResp := i.core.router.validateMountByAccessor(alias.MountAccessor); mountValidationResp != nil {
|
|
|
|
respData["mount_path"] = mountValidationResp.MountPath
|
|
|
|
respData["mount_type"] = mountValidationResp.MountType
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
// Convert protobuf timestamp into RFC3339 format
|
|
|
|
respData["creation_time"] = ptypes.TimestampString(alias.CreationTime)
|
|
|
|
respData["last_update_time"] = ptypes.TimestampString(alias.LastUpdateTime)
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: respData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:05:48 +00:00
|
|
|
// pathAliasIDDelete deletes the alias for a given alias ID
|
2018-01-08 18:31:38 +00:00
|
|
|
func (i *IdentityStore) pathAliasIDDelete() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
aliasID := d.Get("id").(string)
|
|
|
|
if aliasID == "" {
|
|
|
|
return logical.ErrorResponse("missing alias ID"), nil
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2018-06-24 11:45:53 +00:00
|
|
|
// Fetch the alias using its ID
|
|
|
|
alias, err := i.MemDBAliasByID(aliasID, false, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no alias for the ID, do nothing
|
|
|
|
if alias == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the entity to which the alias is tied to
|
|
|
|
lockEntity, err := i.MemDBEntityByAliasID(alias.ID, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no entity tied to a valid alias, something is wrong
|
|
|
|
if lockEntity == nil {
|
|
|
|
return nil, fmt.Errorf("alias not associated to an entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Acquire the lock to modify the entity storage entry
|
|
|
|
lock := locksutil.LockForKey(i.entityLocks, lockEntity.ID)
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
// Create a MemDB transaction to delete entity
|
|
|
|
txn := i.db.Txn(true)
|
|
|
|
defer txn.Abort()
|
|
|
|
|
|
|
|
// Fetch the alias again after acquiring the lock using the transaction
|
|
|
|
// created above
|
|
|
|
alias, err = i.MemDBAliasByIDInTxn(txn, aliasID, false, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no alias for the ID, do nothing
|
|
|
|
if alias == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the entity again after acquiring the lock using the transaction
|
|
|
|
// created above
|
|
|
|
entity, err := i.MemDBEntityByAliasIDInTxn(txn, alias.ID, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no entity tied to a valid alias, something is wrong
|
|
|
|
if entity == nil {
|
|
|
|
return nil, fmt.Errorf("alias not associated to an entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock switching should not end up in this code pointing to different
|
|
|
|
// entities
|
|
|
|
if lockEntity.ID != entity.ID {
|
|
|
|
return nil, fmt.Errorf("operating on an entity to which the lock doesn't belong to")
|
|
|
|
}
|
|
|
|
|
|
|
|
aliases := []*identity.Alias{
|
|
|
|
alias,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete alias from the entity object
|
|
|
|
err = i.deleteAliasesInEntityInTxn(txn, entity, aliases)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the entity index in the entities table
|
|
|
|
err = i.MemDBUpsertEntityInTxn(txn, entity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Persist the entity object
|
|
|
|
entityAsAny, err := ptypes.MarshalAny(entity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
item := &storagepacker.Item{
|
|
|
|
ID: entity.ID,
|
|
|
|
Message: entityAsAny,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = i.entityPacker.PutItem(item)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Committing the transaction *after* successfully updating entity in
|
|
|
|
// storage
|
|
|
|
txn.Commit()
|
|
|
|
|
|
|
|
return nil, nil
|
2018-01-08 18:31:38 +00:00
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pathAliasIDList lists the IDs of all the valid aliases in the identity
|
|
|
|
// store
|
2018-01-08 18:31:38 +00:00
|
|
|
func (i *IdentityStore) pathAliasIDList() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
iter, err := i.MemDBAliases(ws, false)
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("failed to fetch iterator for aliases in memdb: {{err}}", err)
|
2018-01-08 18:31:38 +00:00
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
var aliasIDs []string
|
2018-05-25 18:34:24 +00:00
|
|
|
aliasInfo := map[string]interface{}{}
|
|
|
|
|
|
|
|
type mountInfo struct {
|
|
|
|
MountType string
|
|
|
|
MountPath string
|
|
|
|
}
|
|
|
|
mountAccessorMap := map[string]mountInfo{}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
for {
|
|
|
|
raw := iter.Next()
|
|
|
|
if raw == nil {
|
|
|
|
break
|
|
|
|
}
|
2018-05-25 18:34:24 +00:00
|
|
|
alias := raw.(*identity.Alias)
|
|
|
|
aliasIDs = append(aliasIDs, alias.ID)
|
|
|
|
aliasInfoEntry := map[string]interface{}{
|
|
|
|
"name": alias.Name,
|
|
|
|
"canonical_id": alias.CanonicalID,
|
|
|
|
"mount_accessor": alias.MountAccessor,
|
|
|
|
}
|
|
|
|
|
|
|
|
mi, ok := mountAccessorMap[alias.MountAccessor]
|
|
|
|
if ok {
|
|
|
|
aliasInfoEntry["mount_type"] = mi.MountType
|
|
|
|
aliasInfoEntry["mount_path"] = mi.MountPath
|
|
|
|
} else {
|
|
|
|
mi = mountInfo{}
|
|
|
|
if mountValidationResp := i.core.router.validateMountByAccessor(alias.MountAccessor); mountValidationResp != nil {
|
|
|
|
mi.MountType = mountValidationResp.MountType
|
|
|
|
mi.MountPath = mountValidationResp.MountPath
|
|
|
|
aliasInfoEntry["mount_type"] = mi.MountType
|
|
|
|
aliasInfoEntry["mount_path"] = mi.MountPath
|
|
|
|
}
|
|
|
|
mountAccessorMap[alias.MountAccessor] = mi
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasInfo[alias.ID] = aliasInfoEntry
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 18:34:24 +00:00
|
|
|
return logical.ListResponseWithInfo(aliasIDs, aliasInfo), nil
|
2018-01-08 18:31:38 +00:00
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var aliasHelp = map[string][2]string{
|
|
|
|
"alias": {
|
2017-11-02 20:05:48 +00:00
|
|
|
"Create a new alias.",
|
2017-10-11 17:21:20 +00:00
|
|
|
"",
|
|
|
|
},
|
|
|
|
"alias-id": {
|
2017-11-02 20:05:48 +00:00
|
|
|
"Update, read or delete an alias ID.",
|
2017-10-11 17:21:20 +00:00
|
|
|
"",
|
|
|
|
},
|
|
|
|
"alias-id-list": {
|
2018-04-17 17:54:04 +00:00
|
|
|
"List all the alias IDs.",
|
2017-10-11 17:21:20 +00:00
|
|
|
"",
|
|
|
|
},
|
|
|
|
}
|