2015-07-14 22:46:15 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-07-14 22:46:15 +00:00
|
|
|
"strings"
|
|
|
|
|
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/helper/strutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-07-14 22:46:15 +00:00
|
|
|
)
|
|
|
|
|
2016-05-14 23:56:49 +00:00
|
|
|
func pathUsersList(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "users/?$",
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ListOperation: b.pathUserList,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathUserHelpSyn,
|
|
|
|
HelpDescription: pathUserHelpDesc,
|
2019-06-21 15:08:08 +00:00
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
Navigation: true,
|
2019-10-17 23:19:14 +00:00
|
|
|
ItemType: "User",
|
2019-06-21 15:08:08 +00:00
|
|
|
},
|
2016-05-14 23:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 22:46:15 +00:00
|
|
|
func pathUsers(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: `users/(?P<name>.+)`,
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2019-06-20 22:36:54 +00:00
|
|
|
"name": {
|
2015-07-14 22:46:15 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the LDAP user.",
|
|
|
|
},
|
|
|
|
|
2019-06-20 22:36:54 +00:00
|
|
|
"groups": {
|
|
|
|
Type: framework.TypeCommaStringSlice,
|
2015-07-17 21:40:06 +00:00
|
|
|
Description: "Comma-separated list of additional groups associated with the user.",
|
2015-07-14 22:46:15 +00:00
|
|
|
},
|
2016-11-29 10:32:59 +00:00
|
|
|
|
2019-06-20 22:36:54 +00:00
|
|
|
"policies": {
|
2017-09-13 15:36:52 +00:00
|
|
|
Type: framework.TypeCommaStringSlice,
|
2017-01-23 15:56:43 +00:00
|
|
|
Description: "Comma-separated list of policies associated with the user.",
|
2016-11-29 10:32:59 +00:00
|
|
|
},
|
2015-07-14 22:46:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.DeleteOperation: b.pathUserDelete,
|
|
|
|
logical.ReadOperation: b.pathUserRead,
|
2016-05-14 23:56:49 +00:00
|
|
|
logical.UpdateOperation: b.pathUserWrite,
|
2015-07-14 22:46:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathUserHelpSyn,
|
|
|
|
HelpDescription: pathUserHelpDesc,
|
2019-07-01 20:35:18 +00:00
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
2019-10-17 23:19:14 +00:00
|
|
|
Action: "Create",
|
|
|
|
ItemType: "User",
|
2019-07-01 20:35:18 +00:00
|
|
|
},
|
2015-07-14 22:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) User(ctx context.Context, s logical.Storage, n string) (*UserEntry, error) {
|
|
|
|
entry, err := s.Get(ctx, "user/"+n)
|
2015-07-14 22:46:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result UserEntry
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathUserDelete(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, "user/"+d.Get("name").(string))
|
2015-07-14 22:46:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathUserRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-04-03 13:52:43 +00:00
|
|
|
username := 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 {
|
|
|
|
username = strings.ToLower(username)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := b.User(ctx, req.Storage, username)
|
2015-07-14 22:46:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if user == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
2017-01-23 15:56:43 +00:00
|
|
|
"groups": strings.Join(user.Groups, ","),
|
2017-09-13 15:36:52 +00:00
|
|
|
"policies": user.Policies,
|
2015-07-14 22:46:15 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathUserWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-04-03 13:52:43 +00:00
|
|
|
lowercaseGroups := false
|
|
|
|
username := 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 {
|
|
|
|
username = strings.ToLower(username)
|
|
|
|
lowercaseGroups = true
|
|
|
|
}
|
|
|
|
|
2019-06-20 22:36:54 +00:00
|
|
|
groups := strutil.RemoveDuplicates(d.Get("groups").([]string), lowercaseGroups)
|
2017-09-13 15:36:52 +00:00
|
|
|
policies := policyutil.ParsePolicies(d.Get("policies"))
|
2015-07-17 21:40:06 +00:00
|
|
|
for i, g := range groups {
|
|
|
|
groups[i] = strings.TrimSpace(g)
|
2015-07-14 22:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store it
|
2018-04-03 13:52:43 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("user/"+username, &UserEntry{
|
2017-01-23 15:56:43 +00:00
|
|
|
Groups: groups,
|
2016-11-29 10:32:59 +00:00
|
|
|
Policies: policies,
|
2015-07-14 22:46:15 +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-07-14 22:46:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathUserList(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, "user/")
|
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], "user/")
|
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-07-14 22:46:15 +00:00
|
|
|
type UserEntry struct {
|
2017-01-23 15:56:43 +00:00
|
|
|
Groups []string
|
2016-11-29 10:32:59 +00:00
|
|
|
Policies []string
|
2015-07-14 22:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const pathUserHelpSyn = `
|
2019-03-13 18:02:45 +00:00
|
|
|
Manage users allowed to authenticate.
|
2015-07-14 22:46:15 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const pathUserHelpDesc = `
|
|
|
|
This endpoint allows you to create, read, update, and delete configuration
|
2015-07-17 21:40:06 +00:00
|
|
|
for LDAP users that are allowed to authenticate, in particular associating
|
|
|
|
additional groups to them.
|
2015-07-14 22:46:15 +00:00
|
|
|
|
2015-07-17 21:40:06 +00:00
|
|
|
Deleting a user will not revoke their auth. To do this, do a revoke on "login/<username>" for
|
2015-07-14 22:46:15 +00:00
|
|
|
the usernames you want revoked.
|
|
|
|
`
|