2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-05-06 23:48:59 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2018-04-03 13:52:43 +00:00
|
|
|
"strings"
|
2018-01-08 18:31:38 +00:00
|
|
|
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-12 22:08:46 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/policyutil"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-05-06 23:48:59 +00:00
|
|
|
)
|
|
|
|
|
2016-05-14 23:56:49 +00:00
|
|
|
func pathGroupsList(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "groups/?$",
|
|
|
|
|
2023-04-07 17:16:17 +00:00
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationPrefix: operationPrefixLDAP,
|
|
|
|
OperationSuffix: "groups",
|
|
|
|
Navigation: true,
|
|
|
|
ItemType: "Group",
|
|
|
|
},
|
|
|
|
|
2016-05-14 23:56:49 +00:00
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ListOperation: b.pathGroupList,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathGroupHelpSyn,
|
|
|
|
HelpDescription: pathGroupHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 23:48:59 +00:00
|
|
|
func pathGroups(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
2015-06-29 21:50:55 +00:00
|
|
|
Pattern: `groups/(?P<name>.+)`,
|
2023-04-07 17:16:17 +00:00
|
|
|
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationPrefix: operationPrefixLDAP,
|
|
|
|
OperationSuffix: "group",
|
|
|
|
Action: "Create",
|
|
|
|
ItemType: "Group",
|
|
|
|
},
|
|
|
|
|
2015-05-06 23:48:59 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2019-05-01 21:56:18 +00:00
|
|
|
"name": {
|
2015-05-06 23:48:59 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the LDAP group.",
|
|
|
|
},
|
|
|
|
|
2019-05-01 21:56:18 +00:00
|
|
|
"policies": {
|
2017-09-13 15:36:52 +00:00
|
|
|
Type: framework.TypeCommaStringSlice,
|
2015-05-06 23:48:59 +00:00
|
|
|
Description: "Comma-separated list of policies associated to the group.",
|
2023-04-19 16:16:30 +00:00
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
Description: "A list of policies associated to the group.",
|
|
|
|
},
|
2015-05-06 23:48:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.DeleteOperation: b.pathGroupDelete,
|
|
|
|
logical.ReadOperation: b.pathGroupRead,
|
2016-04-06 00:30:38 +00:00
|
|
|
logical.UpdateOperation: b.pathGroupWrite,
|
2015-05-06 23:48:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathGroupHelpSyn,
|
|
|
|
HelpDescription: pathGroupHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) Group(ctx context.Context, s logical.Storage, n string) (*GroupEntry, error) {
|
|
|
|
entry, err := s.Get(ctx, "group/"+n)
|
2015-05-06 23:48:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result GroupEntry
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathGroupDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
err := req.Storage.Delete(ctx, "group/"+d.Get("name").(string))
|
2015-05-06 23:48:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathGroupRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-04-03 13:52:43 +00:00
|
|
|
groupname := d.Get("name").(string)
|
|
|
|
|
|
|
|
cfg, err := b.Config(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if cfg == nil {
|
|
|
|
return logical.ErrorResponse("ldap backend not configured"), nil
|
|
|
|
}
|
|
|
|
if !*cfg.CaseSensitiveNames {
|
|
|
|
groupname = strings.ToLower(groupname)
|
|
|
|
}
|
|
|
|
|
|
|
|
group, err := b.Group(ctx, req.Storage, groupname)
|
2015-05-06 23:48:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if group == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
2017-09-13 15:36:52 +00:00
|
|
|
"policies": group.Policies,
|
2015-05-06 23:48:59 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathGroupWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-04-03 13:52:43 +00:00
|
|
|
groupname := d.Get("name").(string)
|
|
|
|
|
|
|
|
cfg, err := b.Config(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if cfg == nil {
|
|
|
|
return logical.ErrorResponse("ldap backend not configured"), nil
|
|
|
|
}
|
|
|
|
if !*cfg.CaseSensitiveNames {
|
|
|
|
groupname = strings.ToLower(groupname)
|
|
|
|
}
|
|
|
|
|
2015-05-06 23:48:59 +00:00
|
|
|
// Store it
|
2018-04-03 13:52:43 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("group/"+groupname, &GroupEntry{
|
2017-09-13 15:36:52 +00:00
|
|
|
Policies: policyutil.ParsePolicies(d.Get("policies")),
|
2015-05-06 23:48:59 +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-06 23:48:59 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathGroupList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2019-05-01 21:56:18 +00:00
|
|
|
keys, err := logical.CollectKeysWithPrefix(ctx, req.Storage, "group/")
|
2016-05-14 23:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-01 21:56:18 +00:00
|
|
|
for i := range keys {
|
|
|
|
keys[i] = strings.TrimPrefix(keys[i], "group/")
|
2018-10-17 21:56:51 +00:00
|
|
|
}
|
2019-05-01 21:56:18 +00:00
|
|
|
return logical.ListResponse(keys), nil
|
2016-05-14 23:56:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 23:48:59 +00:00
|
|
|
type GroupEntry struct {
|
|
|
|
Policies []string
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathGroupHelpSyn = `
|
2019-03-13 18:02:45 +00:00
|
|
|
Manage additional groups for users allowed to authenticate.
|
2015-05-06 23:48:59 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const pathGroupHelpDesc = `
|
|
|
|
This endpoint allows you to create, read, update, and delete configuration
|
|
|
|
for LDAP groups that are allowed to authenticate, and associate policies to
|
|
|
|
them.
|
|
|
|
|
|
|
|
Deleting a group will not revoke auth for prior authenticated users in that
|
|
|
|
group. To do this, do a revoke on "login/<username>" for
|
|
|
|
the usernames you want revoked.
|
|
|
|
`
|