2015-07-14 22:46:15 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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{
|
|
|
|
Type: framework.TypeString,
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) User(s logical.Storage, n string) (*UserEntry, error) {
|
|
|
|
entry, err := s.Get("user/" + n)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathUserDelete(
|
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
err := req.Storage.Delete("user/" + d.Get("name").(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathUserRead(
|
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
user, err := b.User(req.Storage, d.Get("name").(string))
|
|
|
|
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, ","),
|
2016-11-29 10:32:59 +00:00
|
|
|
"policies": strings.Join(user.Policies, ","),
|
2015-07-14 22:46:15 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathUserWrite(
|
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
name := d.Get("name").(string)
|
2017-04-19 14:36:45 +00:00
|
|
|
groups := strutil.RemoveDuplicates(strutil.ParseStringSlice(d.Get("groups").(string), ","), false)
|
2017-01-23 15:56:43 +00:00
|
|
|
policies := policyutil.ParsePolicies(d.Get("policies").(string))
|
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
|
|
|
|
entry, err := logical.StorageEntryJSON("user/"+name, &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
|
|
|
|
}
|
|
|
|
if err := req.Storage.Put(entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-05-14 23:56:49 +00:00
|
|
|
func (b *backend) pathUserList(
|
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
users, err := req.Storage.List("user/")
|
|
|
|
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.
|
|
|
|
`
|