2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2024-01-02 21:36:20 +03:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 19:00:52 +03:00
|
|
|
|
2017-10-11 20:21:20 +03:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-08 21:31:38 +03:00
|
|
|
"context"
|
2017-10-11 20:21:20 +03:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2021-07-16 03:17:31 +03:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/strutil"
|
2017-10-11 20:21:20 +03:00
|
|
|
"github.com/hashicorp/vault/helper/identity"
|
2018-09-18 06:03:00 +03:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2019-04-13 10:44:06 +03:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-13 00:54:35 +03:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-10-11 20:21:20 +03:00
|
|
|
)
|
|
|
|
|
2017-11-02 23:05:48 +03:00
|
|
|
const (
|
|
|
|
groupTypeInternal = "internal"
|
|
|
|
groupTypeExternal = "external"
|
|
|
|
)
|
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
func groupPathFields() map[string]*framework.FieldSchema {
|
|
|
|
return map[string]*framework.FieldSchema{
|
|
|
|
"id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "ID of the group. If set, updates the corresponding existing group.",
|
|
|
|
},
|
|
|
|
"type": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Type of the group, 'internal' or 'external'. Defaults to 'internal'",
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the group.",
|
|
|
|
},
|
|
|
|
"metadata": {
|
|
|
|
Type: framework.TypeKVPairs,
|
|
|
|
Description: `Metadata to be associated with the group.
|
2017-11-14 04:59:42 +03:00
|
|
|
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
|
|
|
|
`,
|
2018-09-25 22:28:28 +03:00
|
|
|
},
|
|
|
|
"policies": {
|
|
|
|
Type: framework.TypeCommaStringSlice,
|
|
|
|
Description: "Policies to be tied to the group.",
|
|
|
|
},
|
|
|
|
"member_group_ids": {
|
|
|
|
Type: framework.TypeCommaStringSlice,
|
|
|
|
Description: "Group IDs to be assigned as group members.",
|
|
|
|
},
|
|
|
|
"member_entity_ids": {
|
|
|
|
Type: framework.TypeCommaStringSlice,
|
|
|
|
Description: "Entity IDs to be assigned as group members.",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func groupPaths(i *IdentityStore) []*framework.Path {
|
|
|
|
return []*framework.Path{
|
|
|
|
{
|
|
|
|
Pattern: "group$",
|
2023-04-12 22:45:12 +03:00
|
|
|
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationPrefix: "group",
|
|
|
|
OperationVerb: "create",
|
|
|
|
},
|
|
|
|
|
|
|
|
Fields: groupPathFields(),
|
2023-06-08 21:59:55 +03:00
|
|
|
Operations: map[logical.Operation]framework.OperationHandler{
|
|
|
|
logical.UpdateOperation: &framework.PathOperation{
|
|
|
|
Callback: i.pathGroupRegister(),
|
|
|
|
ForwardPerformanceStandby: true,
|
|
|
|
},
|
2017-10-11 20:21:20 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: strings.TrimSpace(groupHelp["register"][0]),
|
|
|
|
HelpDescription: strings.TrimSpace(groupHelp["register"][1]),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: "group/id/" + framework.GenericNameRegex("id"),
|
2023-04-12 22:45:12 +03:00
|
|
|
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationPrefix: "group",
|
|
|
|
OperationSuffix: "by-id",
|
|
|
|
},
|
|
|
|
|
|
|
|
Fields: groupPathFields(),
|
|
|
|
|
|
|
|
Operations: map[logical.Operation]framework.OperationHandler{
|
|
|
|
logical.UpdateOperation: &framework.PathOperation{
|
|
|
|
Callback: i.pathGroupIDUpdate(),
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationVerb: "update",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
logical.ReadOperation: &framework.PathOperation{
|
|
|
|
Callback: i.pathGroupIDRead(),
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationVerb: "read",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
logical.DeleteOperation: &framework.PathOperation{
|
|
|
|
Callback: i.pathGroupIDDelete(),
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationVerb: "delete",
|
|
|
|
},
|
|
|
|
},
|
2017-10-11 20:21:20 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: strings.TrimSpace(groupHelp["group-by-id"][0]),
|
|
|
|
HelpDescription: strings.TrimSpace(groupHelp["group-by-id"][1]),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: "group/id/?$",
|
2023-04-12 22:45:12 +03:00
|
|
|
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationPrefix: "group",
|
|
|
|
OperationSuffix: "by-id",
|
|
|
|
},
|
|
|
|
|
2017-10-11 20:21:20 +03:00
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2018-01-08 21:31:38 +03:00
|
|
|
logical.ListOperation: i.pathGroupIDList(),
|
2017-10-11 20:21:20 +03:00
|
|
|
},
|
|
|
|
|
2018-04-17 20:54:04 +03:00
|
|
|
HelpSynopsis: strings.TrimSpace(groupHelp["group-id-list"][0]),
|
|
|
|
HelpDescription: strings.TrimSpace(groupHelp["group-id-list"][1]),
|
2017-10-11 20:21:20 +03:00
|
|
|
},
|
2018-09-25 22:28:28 +03:00
|
|
|
{
|
2019-07-03 15:56:01 +03:00
|
|
|
Pattern: "group/name/(?P<name>.+)",
|
2023-04-12 22:45:12 +03:00
|
|
|
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationPrefix: "group",
|
|
|
|
OperationSuffix: "by-name",
|
|
|
|
},
|
|
|
|
|
|
|
|
Fields: groupPathFields(),
|
|
|
|
|
|
|
|
Operations: map[logical.Operation]framework.OperationHandler{
|
|
|
|
logical.UpdateOperation: &framework.PathOperation{
|
|
|
|
Callback: i.pathGroupNameUpdate(),
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationVerb: "update",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
logical.ReadOperation: &framework.PathOperation{
|
|
|
|
Callback: i.pathGroupNameRead(),
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationVerb: "read",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
logical.DeleteOperation: &framework.PathOperation{
|
|
|
|
Callback: i.pathGroupNameDelete(),
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationVerb: "delete",
|
|
|
|
},
|
|
|
|
},
|
2018-09-25 22:28:28 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: strings.TrimSpace(groupHelp["group-by-name"][0]),
|
|
|
|
HelpDescription: strings.TrimSpace(groupHelp["group-by-name"][1]),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: "group/name/?$",
|
2023-04-12 22:45:12 +03:00
|
|
|
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationPrefix: "group",
|
|
|
|
OperationSuffix: "by-name",
|
|
|
|
},
|
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ListOperation: i.pathGroupNameList(),
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: strings.TrimSpace(groupHelp["group-name-list"][0]),
|
|
|
|
HelpDescription: strings.TrimSpace(groupHelp["group-name-list"][1]),
|
|
|
|
},
|
2017-10-11 20:21:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 21:31:38 +03:00
|
|
|
func (i *IdentityStore) pathGroupRegister() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
_, ok := d.GetOk("id")
|
|
|
|
if ok {
|
|
|
|
return i.pathGroupIDUpdate()(ctx, req, d)
|
|
|
|
}
|
2017-10-11 20:21:20 +03:00
|
|
|
|
2021-01-30 01:50:08 +03:00
|
|
|
_, ok = d.GetOk("name")
|
|
|
|
if ok {
|
|
|
|
return i.pathGroupNameUpdate()(ctx, req, d)
|
|
|
|
}
|
|
|
|
|
2018-01-08 21:31:38 +03:00
|
|
|
i.groupLock.Lock()
|
|
|
|
defer i.groupLock.Unlock()
|
2017-10-11 20:21:20 +03:00
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
return i.handleGroupUpdateCommon(ctx, req, d, nil)
|
2018-01-08 21:31:38 +03:00
|
|
|
}
|
2017-10-11 20:21:20 +03:00
|
|
|
}
|
|
|
|
|
2018-01-08 21:31:38 +03:00
|
|
|
func (i *IdentityStore) pathGroupIDUpdate() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
groupID := d.Get("id").(string)
|
|
|
|
if groupID == "" {
|
|
|
|
return logical.ErrorResponse("empty group ID"), nil
|
|
|
|
}
|
2017-10-11 20:21:20 +03:00
|
|
|
|
2018-01-08 21:31:38 +03:00
|
|
|
i.groupLock.Lock()
|
|
|
|
defer i.groupLock.Unlock()
|
2017-10-11 20:21:20 +03:00
|
|
|
|
2018-01-08 21:31:38 +03:00
|
|
|
group, err := i.MemDBGroupByID(groupID, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if group == nil {
|
|
|
|
return logical.ErrorResponse("invalid group ID"), nil
|
|
|
|
}
|
2017-10-11 20:21:20 +03:00
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
return i.handleGroupUpdateCommon(ctx, req, d, group)
|
2018-01-08 21:31:38 +03:00
|
|
|
}
|
2017-10-11 20:21:20 +03:00
|
|
|
}
|
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
func (i *IdentityStore) pathGroupNameUpdate() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
groupName := d.Get("name").(string)
|
|
|
|
if groupName == "" {
|
|
|
|
return logical.ErrorResponse("empty group name"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
i.groupLock.Lock()
|
|
|
|
defer i.groupLock.Unlock()
|
|
|
|
|
|
|
|
group, err := i.MemDBGroupByName(ctx, groupName, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return i.handleGroupUpdateCommon(ctx, req, d, group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
func (i *IdentityStore) handleGroupUpdateCommon(ctx context.Context, req *logical.Request, d *framework.FieldData, group *identity.Group) (*logical.Response, error) {
|
2017-10-11 20:21:20 +03:00
|
|
|
var newGroup bool
|
|
|
|
if group == nil {
|
2018-09-18 06:03:00 +03:00
|
|
|
group = new(identity.Group)
|
2017-10-11 20:21:20 +03:00
|
|
|
newGroup = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the policies if supplied
|
|
|
|
policiesRaw, ok := d.GetOk("policies")
|
|
|
|
if ok {
|
2022-05-26 15:52:19 +03:00
|
|
|
group.Policies = strutil.RemoveDuplicatesStable(policiesRaw.([]string), true)
|
2017-10-11 20:21:20 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 23:12:23 +03:00
|
|
|
if strutil.StrListContains(group.Policies, "root") {
|
|
|
|
return logical.ErrorResponse("policies cannot contain root"), nil
|
|
|
|
}
|
|
|
|
|
2017-11-02 23:05:48 +03:00
|
|
|
groupTypeRaw, ok := d.GetOk("type")
|
|
|
|
if ok {
|
|
|
|
groupType := groupTypeRaw.(string)
|
|
|
|
if group.Type != "" && groupType != group.Type {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("group type cannot be changed")), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
group.Type = groupType
|
|
|
|
}
|
|
|
|
|
|
|
|
// If group type is not set, default to internal type
|
|
|
|
if group.Type == "" {
|
|
|
|
group.Type = groupTypeInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
if group.Type != groupTypeInternal && group.Type != groupTypeExternal {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("invalid group type %q", group.Type)), nil
|
|
|
|
}
|
|
|
|
|
2017-10-11 20:21:20 +03:00
|
|
|
// Get the name
|
|
|
|
groupName := d.Get("name").(string)
|
|
|
|
if groupName != "" {
|
|
|
|
// Check if there is a group already existing for the given name
|
2018-09-18 06:03:00 +03:00
|
|
|
groupByName, err := i.MemDBGroupByName(ctx, groupName, false)
|
2017-10-11 20:21:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-30 01:50:08 +03:00
|
|
|
// If no existing group has this name, go ahead with the creation or rename.
|
|
|
|
// If there is a group, it must match the group passed in; groupByName
|
|
|
|
// should not be modified as it's in memdb.
|
2017-10-11 20:21:20 +03:00
|
|
|
switch {
|
2018-09-18 06:03:00 +03:00
|
|
|
case groupByName == nil:
|
|
|
|
// Allowed
|
2021-01-30 01:50:08 +03:00
|
|
|
case groupByName.ID != group.ID:
|
2017-10-11 20:21:20 +03:00
|
|
|
return logical.ErrorResponse("group name is already in use"), nil
|
|
|
|
}
|
|
|
|
group.Name = groupName
|
|
|
|
}
|
|
|
|
|
2017-11-08 22:51:40 +03: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 20:21:20 +03:00
|
|
|
if ok {
|
2017-11-08 22:51:40 +03:00
|
|
|
group.Metadata = metadata.(map[string]string)
|
2017-10-11 20:21:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
memberEntityIDsRaw, ok := d.GetOk("member_entity_ids")
|
|
|
|
if ok {
|
2017-11-02 23:05:48 +03:00
|
|
|
if group.Type == groupTypeExternal {
|
|
|
|
return logical.ErrorResponse("member entities can't be set manually for external groups"), nil
|
|
|
|
}
|
2017-10-11 20:21:20 +03:00
|
|
|
group.MemberEntityIDs = memberEntityIDsRaw.([]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
memberGroupIDsRaw, ok := d.GetOk("member_group_ids")
|
|
|
|
var memberGroupIDs []string
|
|
|
|
if ok {
|
2017-11-02 23:05:48 +03:00
|
|
|
if group.Type == groupTypeExternal {
|
|
|
|
return logical.ErrorResponse("member groups can't be set for external groups"), nil
|
|
|
|
}
|
2017-10-11 20:21:20 +03:00
|
|
|
memberGroupIDs = memberGroupIDsRaw.([]string)
|
|
|
|
}
|
|
|
|
|
2019-06-18 23:42:54 +03:00
|
|
|
err = i.sanitizeAndUpsertGroup(ctx, group, nil, memberGroupIDs)
|
2017-10-11 20:21:20 +03:00
|
|
|
if err != nil {
|
2022-06-10 17:15:31 +03:00
|
|
|
if errStr := err.Error(); strings.HasPrefix(errStr, errCycleDetectedPrefix) {
|
|
|
|
return logical.ErrorResponse(errStr), nil
|
|
|
|
}
|
|
|
|
|
2017-10-11 20:21:20 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
if !newGroup {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-10-11 20:21:20 +03:00
|
|
|
respData := map[string]interface{}{
|
|
|
|
"id": group.ID,
|
|
|
|
"name": group.Name,
|
|
|
|
}
|
|
|
|
return &logical.Response{
|
|
|
|
Data: respData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 21:31:38 +03:00
|
|
|
func (i *IdentityStore) pathGroupIDRead() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
groupID := d.Get("id").(string)
|
|
|
|
if groupID == "" {
|
|
|
|
return logical.ErrorResponse("empty group id"), nil
|
|
|
|
}
|
2017-10-11 20:21:20 +03:00
|
|
|
|
2018-01-08 21:31:38 +03:00
|
|
|
group, err := i.MemDBGroupByID(groupID, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-18 06:03:00 +03:00
|
|
|
if group == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-10-11 20:21:20 +03:00
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
return i.handleGroupReadCommon(ctx, group)
|
2018-01-08 21:31:38 +03:00
|
|
|
}
|
2017-10-11 20:21:20 +03:00
|
|
|
}
|
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
func (i *IdentityStore) pathGroupNameRead() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
groupName := d.Get("name").(string)
|
|
|
|
if groupName == "" {
|
|
|
|
return logical.ErrorResponse("empty group name"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
group, err := i.MemDBGroupByName(ctx, groupName, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if group == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.handleGroupReadCommon(ctx, group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
func (i *IdentityStore) handleGroupReadCommon(ctx context.Context, group *identity.Group) (*logical.Response, error) {
|
2017-10-11 20:21:20 +03:00
|
|
|
if group == nil {
|
2017-11-02 23:05:48 +03:00
|
|
|
return nil, nil
|
2017-10-11 20:21:20 +03:00
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
ns, err := namespace.FromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ns.ID != group.NamespaceID {
|
2019-06-18 23:42:54 +03:00
|
|
|
return logical.ErrorResponse("request namespace is not the same as the group namespace"), logical.ErrPermissionDenied
|
2018-09-18 06:03:00 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 20:21:20 +03:00
|
|
|
respData := map[string]interface{}{}
|
|
|
|
respData["id"] = group.ID
|
|
|
|
respData["name"] = group.Name
|
|
|
|
respData["policies"] = group.Policies
|
|
|
|
respData["member_entity_ids"] = group.MemberEntityIDs
|
2018-05-29 21:58:23 +03:00
|
|
|
respData["parent_group_ids"] = group.ParentGroupIDs
|
2017-10-11 20:21:20 +03:00
|
|
|
respData["metadata"] = group.Metadata
|
|
|
|
respData["creation_time"] = ptypes.TimestampString(group.CreationTime)
|
|
|
|
respData["last_update_time"] = ptypes.TimestampString(group.LastUpdateTime)
|
|
|
|
respData["modify_index"] = group.ModifyIndex
|
2017-11-02 23:05:48 +03:00
|
|
|
respData["type"] = group.Type
|
2019-06-14 19:53:00 +03:00
|
|
|
respData["namespace_id"] = group.NamespaceID
|
2017-11-02 23:05:48 +03:00
|
|
|
|
|
|
|
aliasMap := map[string]interface{}{}
|
|
|
|
if group.Alias != nil {
|
|
|
|
aliasMap["id"] = group.Alias.ID
|
|
|
|
aliasMap["canonical_id"] = group.Alias.CanonicalID
|
|
|
|
aliasMap["mount_accessor"] = group.Alias.MountAccessor
|
|
|
|
aliasMap["metadata"] = group.Alias.Metadata
|
|
|
|
aliasMap["name"] = group.Alias.Name
|
|
|
|
aliasMap["merged_from_canonical_ids"] = group.Alias.MergedFromCanonicalIDs
|
|
|
|
aliasMap["creation_time"] = ptypes.TimestampString(group.Alias.CreationTime)
|
|
|
|
aliasMap["last_update_time"] = ptypes.TimestampString(group.Alias.LastUpdateTime)
|
2018-08-09 21:07:24 +03:00
|
|
|
|
2021-08-30 22:31:11 +03:00
|
|
|
if mountValidationResp := i.router.ValidateMountByAccessor(group.Alias.MountAccessor); mountValidationResp != nil {
|
2018-08-09 21:07:24 +03:00
|
|
|
aliasMap["mount_path"] = mountValidationResp.MountPath
|
|
|
|
aliasMap["mount_type"] = mountValidationResp.MountType
|
|
|
|
}
|
2017-11-02 23:05:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
respData["alias"] = aliasMap
|
2017-10-11 20:21:20 +03:00
|
|
|
|
2018-06-24 14:45:53 +03:00
|
|
|
var memberGroupIDs []string
|
|
|
|
memberGroups, err := i.MemDBGroupsByParentGroupID(group.ID, false)
|
2017-10-11 20:21:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-24 14:45:53 +03:00
|
|
|
for _, memberGroup := range memberGroups {
|
|
|
|
memberGroupIDs = append(memberGroupIDs, memberGroup.ID)
|
|
|
|
}
|
|
|
|
|
2017-10-11 20:21:20 +03:00
|
|
|
respData["member_group_ids"] = memberGroupIDs
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: respData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 21:31:38 +03:00
|
|
|
func (i *IdentityStore) pathGroupIDDelete() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
groupID := d.Get("id").(string)
|
|
|
|
if groupID == "" {
|
|
|
|
return logical.ErrorResponse("empty group ID"), nil
|
|
|
|
}
|
2018-06-24 14:45:53 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
return i.handleGroupDeleteCommon(ctx, groupID, true)
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 14:45:53 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
func (i *IdentityStore) pathGroupNameDelete() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
groupName := d.Get("name").(string)
|
|
|
|
if groupName == "" {
|
|
|
|
return logical.ErrorResponse("empty group name"), nil
|
|
|
|
}
|
2018-06-24 14:45:53 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
return i.handleGroupDeleteCommon(ctx, groupName, false)
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 14:45:53 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
func (i *IdentityStore) handleGroupDeleteCommon(ctx context.Context, key string, byID bool) (*logical.Response, error) {
|
|
|
|
// Acquire the lock to modify the group storage entry
|
|
|
|
i.groupLock.Lock()
|
|
|
|
defer i.groupLock.Unlock()
|
2018-06-24 14:45:53 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
// Create a MemDB transaction to delete group
|
|
|
|
txn := i.db.Txn(true)
|
|
|
|
defer txn.Abort()
|
2018-06-24 14:45:53 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
var group *identity.Group
|
|
|
|
var err error
|
|
|
|
switch byID {
|
|
|
|
case true:
|
|
|
|
group, err = i.MemDBGroupByIDInTxn(txn, key, false)
|
2018-09-18 06:03:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-25 22:28:28 +03:00
|
|
|
default:
|
|
|
|
group, err = i.MemDBGroupByNameInTxn(ctx, txn, key, false)
|
2018-06-24 14:45:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-25 22:28:28 +03:00
|
|
|
}
|
|
|
|
if group == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ns, err := namespace.FromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if group.NamespaceID != ns.ID {
|
2019-06-18 23:42:54 +03:00
|
|
|
return logical.ErrorResponse("request namespace is not the same as the group namespace"), logical.ErrPermissionDenied
|
2018-09-25 22:28:28 +03:00
|
|
|
}
|
2018-06-24 14:45:53 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
// Delete group alias from memdb
|
|
|
|
if group.Type == groupTypeExternal && group.Alias != nil {
|
|
|
|
err = i.MemDBDeleteAliasByIDInTxn(txn, group.Alias.ID, true)
|
2018-06-24 14:45:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-25 22:28:28 +03:00
|
|
|
}
|
2018-06-24 14:45:53 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
// Delete the group using the same transaction
|
|
|
|
err = i.MemDBDeleteGroupByIDInTxn(txn, group.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-24 14:45:53 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
// Delete the group from storage
|
2019-05-01 20:47:41 +03:00
|
|
|
err = i.groupPacker.DeleteItem(ctx, group.ID)
|
2018-09-25 22:28:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-10-11 20:21:20 +03:00
|
|
|
}
|
2018-09-25 22:28:28 +03:00
|
|
|
|
|
|
|
// Committing the transaction *after* successfully deleting group
|
|
|
|
txn.Commit()
|
|
|
|
|
|
|
|
return nil, nil
|
2017-10-11 20:21:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// pathGroupIDList lists the IDs of all the groups in the identity store
|
2018-01-08 21:31:38 +03:00
|
|
|
func (i *IdentityStore) pathGroupIDList() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-09-25 22:28:28 +03:00
|
|
|
return i.handleGroupListCommon(ctx, true)
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 14:45:53 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
// pathGroupNameList lists the names of all the groups in the identity store
|
|
|
|
func (i *IdentityStore) pathGroupNameList() framework.OperationFunc {
|
|
|
|
return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
return i.handleGroupListCommon(ctx, false)
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 14:45:53 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
func (i *IdentityStore) handleGroupListCommon(ctx context.Context, byID bool) (*logical.Response, error) {
|
|
|
|
ns, err := namespace.FromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
txn := i.db.Txn(false)
|
|
|
|
|
|
|
|
iter, err := txn.Get(groupsTable, "namespace_id", ns.ID)
|
|
|
|
if err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("failed to lookup groups using namespace ID: %w", err)
|
2018-09-25 22:28:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var keys []string
|
|
|
|
groupInfo := map[string]interface{}{}
|
|
|
|
|
|
|
|
type mountInfo struct {
|
|
|
|
MountType string
|
|
|
|
MountPath string
|
|
|
|
}
|
|
|
|
mountAccessorMap := map[string]mountInfo{}
|
|
|
|
|
|
|
|
for entry := iter.Next(); entry != nil; entry = iter.Next() {
|
|
|
|
group := entry.(*identity.Group)
|
2017-10-11 20:21:20 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
if byID {
|
|
|
|
keys = append(keys, group.ID)
|
|
|
|
} else {
|
|
|
|
keys = append(keys, group.Name)
|
|
|
|
}
|
2018-05-25 21:34:24 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
groupInfoEntry := map[string]interface{}{
|
|
|
|
"name": group.Name,
|
|
|
|
"num_member_entities": len(group.MemberEntityIDs),
|
|
|
|
"num_parent_groups": len(group.ParentGroupIDs),
|
2018-05-25 21:34:24 +03:00
|
|
|
}
|
2018-09-25 22:28:28 +03:00
|
|
|
if group.Alias != nil {
|
|
|
|
entry := map[string]interface{}{
|
|
|
|
"id": group.Alias.ID,
|
|
|
|
"name": group.Alias.Name,
|
|
|
|
"mount_accessor": group.Alias.MountAccessor,
|
2018-05-25 21:34:24 +03:00
|
|
|
}
|
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
mi, ok := mountAccessorMap[group.Alias.MountAccessor]
|
|
|
|
if ok {
|
|
|
|
entry["mount_type"] = mi.MountType
|
|
|
|
entry["mount_path"] = mi.MountPath
|
|
|
|
} else {
|
|
|
|
mi = mountInfo{}
|
2021-08-30 22:31:11 +03:00
|
|
|
if mountValidationResp := i.router.ValidateMountByAccessor(group.Alias.MountAccessor); mountValidationResp != nil {
|
2018-09-25 22:28:28 +03:00
|
|
|
mi.MountType = mountValidationResp.MountType
|
|
|
|
mi.MountPath = mountValidationResp.MountPath
|
2018-05-25 21:34:24 +03:00
|
|
|
entry["mount_type"] = mi.MountType
|
|
|
|
entry["mount_path"] = mi.MountPath
|
|
|
|
}
|
2018-09-25 22:28:28 +03:00
|
|
|
mountAccessorMap[group.Alias.MountAccessor] = mi
|
2018-05-25 21:34:24 +03:00
|
|
|
}
|
2017-10-11 20:21:20 +03:00
|
|
|
|
2018-09-25 22:28:28 +03:00
|
|
|
groupInfoEntry["alias"] = entry
|
|
|
|
}
|
|
|
|
groupInfo[group.ID] = groupInfoEntry
|
2018-01-08 21:31:38 +03:00
|
|
|
}
|
2018-09-25 22:28:28 +03:00
|
|
|
|
|
|
|
return logical.ListResponseWithInfo(keys, groupInfo), nil
|
2017-10-11 20:21:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var groupHelp = map[string][2]string{
|
|
|
|
"register": {
|
|
|
|
"Create a new group.",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
"group-by-id": {
|
|
|
|
"Update or delete an existing group using its ID.",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
"group-id-list": {
|
|
|
|
"List all the group IDs.",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
}
|