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
|
|
|
|
2016-04-06 00:30:38 +00:00
|
|
|
"github.com/hashicorp/vault/helper/policyutil"
|
2015-05-06 23:48:59 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2016-05-14 23:56:49 +00:00
|
|
|
func pathGroupsList(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "groups/?$",
|
|
|
|
|
|
|
|
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>.+)`,
|
2015-05-06 23:48:59 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the LDAP group.",
|
|
|
|
},
|
|
|
|
|
|
|
|
"policies": &framework.FieldSchema{
|
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.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
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) {
|
2018-01-19 06:44:44 +00:00
|
|
|
groups, err := req.Storage.List(ctx, "group/")
|
2016-05-14 23:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return logical.ListResponse(groups), nil
|
|
|
|
}
|
|
|
|
|
2015-05-06 23:48:59 +00:00
|
|
|
type GroupEntry struct {
|
|
|
|
Policies []string
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathGroupHelpSyn = `
|
|
|
|
Manage users allowed to authenticate.
|
|
|
|
`
|
|
|
|
|
|
|
|
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.
|
|
|
|
`
|