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"
|
|
|
|
|
2017-01-23 15:56:43 +00:00
|
|
|
"github.com/hashicorp/vault/helper/policyutil"
|
|
|
|
"github.com/hashicorp/vault/helper/strutil"
|
2015-07-14 22:46:15 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the LDAP user.",
|
|
|
|
},
|
|
|
|
|
2015-07-17 21:40:06 +00:00
|
|
|
"groups": &framework.FieldSchema{
|
2015-07-14 22:46:15 +00:00
|
|
|
Type: framework.TypeString,
|
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
|
|
|
|
|
|
|
"policies": &framework.FieldSchema{
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := strutil.RemoveDuplicates(strutil.ParseStringSlice(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) {
|
2018-01-19 06:44:44 +00:00
|
|
|
users, err := req.Storage.List(ctx, "user/")
|
2016-05-14 23:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return logical.ListResponse(users), nil
|
|
|
|
}
|
|
|
|
|
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 = `
|
2015-07-17 21:40:06 +00:00
|
|
|
Manage additional groups for 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.
|
|
|
|
`
|