2015-04-19 21:59:30 +00:00
|
|
|
package userpass
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-09-17 00:12:41 +00:00
|
|
|
"fmt"
|
2015-04-19 21:59:30 +00:00
|
|
|
"strings"
|
2015-09-17 00:12:41 +00:00
|
|
|
"time"
|
2015-04-19 21:59:30 +00:00
|
|
|
|
2019-01-09 00:48:57 +00:00
|
|
|
sockaddr "github.com/hashicorp/go-sockaddr"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-06-28 22:20:53 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/tokenutil"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-04-19 21:59:30 +00:00
|
|
|
)
|
|
|
|
|
2016-04-09 22:28:30 +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-10-17 23:19:14 +00:00
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
Navigation: true,
|
|
|
|
ItemType: "User",
|
|
|
|
},
|
2016-04-09 22:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:59:30 +00:00
|
|
|
func pathUsers(b *backend) *framework.Path {
|
2019-06-28 22:20:53 +00:00
|
|
|
p := &framework.Path{
|
2016-03-15 21:47:13 +00:00
|
|
|
Pattern: "users/" + framework.GenericNameRegex("username"),
|
2015-04-19 21:59:30 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"username": {
|
2015-04-19 21:59:30 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Username for this user.",
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"password": {
|
2015-04-19 21:59:30 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Password for this user.",
|
2019-10-17 23:19:14 +00:00
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
Sensitive: true,
|
|
|
|
},
|
2015-04-19 21:59:30 +00:00
|
|
|
},
|
2019-02-01 16:23:40 +00:00
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"policies": {
|
2019-02-01 16:23:40 +00:00
|
|
|
Type: framework.TypeCommaStringSlice,
|
2019-06-28 22:20:53 +00:00
|
|
|
Description: tokenutil.DeprecationText("token_policies"),
|
|
|
|
Deprecated: true,
|
2019-02-01 16:23:40 +00:00
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"ttl": {
|
2019-02-01 16:23:40 +00:00
|
|
|
Type: framework.TypeDurationSecond,
|
2019-06-28 22:20:53 +00:00
|
|
|
Description: tokenutil.DeprecationText("token_ttl"),
|
|
|
|
Deprecated: true,
|
2019-02-01 16:23:40 +00:00
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"max_ttl": {
|
2019-02-01 16:23:40 +00:00
|
|
|
Type: framework.TypeDurationSecond,
|
2019-06-28 22:20:53 +00:00
|
|
|
Description: tokenutil.DeprecationText("token_max_ttl"),
|
|
|
|
Deprecated: true,
|
2019-02-01 16:23:40 +00:00
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"bound_cidrs": {
|
2019-06-28 22:20:53 +00:00
|
|
|
Type: framework.TypeCommaStringSlice,
|
|
|
|
Description: tokenutil.DeprecationText("token_bound_cidrs"),
|
|
|
|
Deprecated: true,
|
2019-02-01 16:23:40 +00:00
|
|
|
},
|
2015-04-19 21:59:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.DeleteOperation: b.pathUserDelete,
|
|
|
|
logical.ReadOperation: b.pathUserRead,
|
2016-03-15 19:17:50 +00:00
|
|
|
logical.UpdateOperation: b.pathUserWrite,
|
|
|
|
logical.CreateOperation: b.pathUserWrite,
|
2015-04-19 21:59:30 +00:00
|
|
|
},
|
|
|
|
|
2016-03-15 19:17:50 +00:00
|
|
|
ExistenceCheck: b.userExistenceCheck,
|
|
|
|
|
2015-04-19 21:59:30 +00:00
|
|
|
HelpSynopsis: pathUserHelpSyn,
|
|
|
|
HelpDescription: pathUserHelpDesc,
|
2019-10-17 23:19:14 +00:00
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
Action: "Create",
|
|
|
|
ItemType: "User",
|
|
|
|
},
|
2015-04-19 21:59:30 +00:00
|
|
|
}
|
2019-06-28 22:20:53 +00:00
|
|
|
|
|
|
|
tokenutil.AddTokenFields(p.Fields)
|
|
|
|
return p
|
2015-04-19 21:59:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 22:20:53 +00:00
|
|
|
func (b *backend) userExistenceCheck(ctx context.Context, req *logical.Request, d *framework.FieldData) (bool, error) {
|
|
|
|
userEntry, err := b.user(ctx, req.Storage, d.Get("username").(string))
|
2016-03-15 19:17:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2016-03-16 15:26:33 +00:00
|
|
|
return userEntry != nil, nil
|
2016-03-15 19:17:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) user(ctx context.Context, s logical.Storage, username string) (*UserEntry, error) {
|
2016-03-16 18:21:14 +00:00
|
|
|
if username == "" {
|
|
|
|
return nil, fmt.Errorf("missing username")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := s.Get(ctx, "user/"+strings.ToLower(username))
|
2015-04-19 21:59:30 +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
|
|
|
|
}
|
|
|
|
|
2019-06-28 22:20:53 +00:00
|
|
|
if result.TokenTTL == 0 && result.TTL > 0 {
|
|
|
|
result.TokenTTL = result.TTL
|
|
|
|
}
|
|
|
|
if result.TokenMaxTTL == 0 && result.MaxTTL > 0 {
|
|
|
|
result.TokenMaxTTL = result.MaxTTL
|
|
|
|
}
|
|
|
|
if len(result.TokenPolicies) == 0 && len(result.Policies) > 0 {
|
|
|
|
result.TokenPolicies = result.Policies
|
|
|
|
}
|
|
|
|
if len(result.TokenBoundCIDRs) == 0 && len(result.BoundCIDRs) > 0 {
|
|
|
|
result.TokenBoundCIDRs = result.BoundCIDRs
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:59:30 +00:00
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) setUser(ctx context.Context, s logical.Storage, username string, userEntry *UserEntry) error {
|
2016-03-15 19:17:50 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("user/"+username, userEntry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
return s.Put(ctx, entry)
|
2016-03-15 19:17:50 +00:00
|
|
|
}
|
|
|
|
|
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-04-09 22:28:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return logical.ListResponse(users), 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/"+strings.ToLower(d.Get("username").(string)))
|
2015-04-19 21:59:30 +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-01-19 06:44:44 +00:00
|
|
|
user, err := b.user(ctx, req.Storage, strings.ToLower(d.Get("username").(string)))
|
2015-04-19 21:59:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if user == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-06-28 22:20:53 +00:00
|
|
|
data := map[string]interface{}{}
|
|
|
|
user.PopulateTokenData(data)
|
|
|
|
|
|
|
|
// Add backwards compat data
|
|
|
|
if user.TTL > 0 {
|
|
|
|
data["ttl"] = int64(user.TTL.Seconds())
|
|
|
|
}
|
|
|
|
if user.MaxTTL > 0 {
|
|
|
|
data["max_ttl"] = int64(user.MaxTTL.Seconds())
|
|
|
|
}
|
|
|
|
if len(user.Policies) > 0 {
|
2019-06-29 20:36:21 +00:00
|
|
|
data["policies"] = data["token_policies"]
|
2019-06-28 22:20:53 +00:00
|
|
|
}
|
|
|
|
if len(user.BoundCIDRs) > 0 {
|
|
|
|
data["bound_cidrs"] = user.BoundCIDRs
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:59:30 +00:00
|
|
|
return &logical.Response{
|
2019-06-28 22:20:53 +00:00
|
|
|
Data: data,
|
2015-04-19 21:59:30 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) userCreateUpdate(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-03-15 21:47:13 +00:00
|
|
|
username := strings.ToLower(d.Get("username").(string))
|
2018-01-19 06:44:44 +00:00
|
|
|
userEntry, err := b.user(ctx, req.Storage, username)
|
2016-03-15 21:05:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Due to existence check, user will only be nil if it's a create operation
|
2016-03-15 21:32:39 +00:00
|
|
|
if userEntry == nil {
|
|
|
|
userEntry = &UserEntry{}
|
2016-03-15 19:17:50 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 22:20:53 +00:00
|
|
|
if err := userEntry.ParseTokenFields(req, d); err != nil {
|
|
|
|
return logical.ErrorResponse(err.Error()), logical.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
|
2016-03-16 18:21:14 +00:00
|
|
|
if _, ok := d.GetOk("password"); ok {
|
2016-07-25 15:09:46 +00:00
|
|
|
userErr, intErr := b.updateUserPassword(req, d, userEntry)
|
|
|
|
if intErr != nil {
|
2019-07-24 16:58:26 +00:00
|
|
|
return nil, intErr
|
2016-03-16 18:21:14 +00:00
|
|
|
}
|
2016-07-25 15:09:46 +00:00
|
|
|
if userErr != nil {
|
2016-07-25 15:14:15 +00:00
|
|
|
return logical.ErrorResponse(userErr.Error()), logical.ErrInvalidRequest
|
2016-07-25 15:09:46 +00:00
|
|
|
}
|
2016-03-16 17:39:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 22:20:53 +00:00
|
|
|
// handle upgrade cases
|
|
|
|
{
|
2019-07-02 13:52:05 +00:00
|
|
|
if err := tokenutil.UpgradeValue(d, "policies", "token_policies", &userEntry.Policies, &userEntry.TokenPolicies); err != nil {
|
|
|
|
return logical.ErrorResponse(err.Error()), nil
|
2019-06-28 22:20:53 +00:00
|
|
|
}
|
2019-02-01 16:23:40 +00:00
|
|
|
|
2019-07-02 13:52:05 +00:00
|
|
|
if err := tokenutil.UpgradeValue(d, "ttl", "token_ttl", &userEntry.TTL, &userEntry.TokenTTL); err != nil {
|
|
|
|
return logical.ErrorResponse(err.Error()), nil
|
2019-06-28 22:20:53 +00:00
|
|
|
}
|
2019-02-01 16:23:40 +00:00
|
|
|
|
2019-07-02 13:52:05 +00:00
|
|
|
if err := tokenutil.UpgradeValue(d, "max_ttl", "token_max_ttl", &userEntry.MaxTTL, &userEntry.TokenMaxTTL); err != nil {
|
|
|
|
return logical.ErrorResponse(err.Error()), nil
|
2019-06-28 22:20:53 +00:00
|
|
|
}
|
2019-02-01 16:23:40 +00:00
|
|
|
|
2020-04-23 21:00:21 +00:00
|
|
|
if err := tokenutil.UpgradeValue(d, "bound_cidrs", "token_bound_cidrs", &userEntry.BoundCIDRs, &userEntry.TokenBoundCIDRs); err != nil {
|
2019-07-02 13:52:05 +00:00
|
|
|
return logical.ErrorResponse(err.Error()), nil
|
2019-06-28 22:20:53 +00:00
|
|
|
}
|
2018-05-21 18:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
return nil, b.setUser(ctx, req.Storage, username, userEntry)
|
2016-03-15 21:32:39 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathUserWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-03-15 21:32:39 +00:00
|
|
|
password := d.Get("password").(string)
|
2016-03-16 18:21:14 +00:00
|
|
|
if req.Operation == logical.CreateOperation && password == "" {
|
2016-03-16 19:14:06 +00:00
|
|
|
return logical.ErrorResponse("missing password"), logical.ErrInvalidRequest
|
2016-03-15 21:32:39 +00:00
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
return b.userCreateUpdate(ctx, req, d)
|
2015-04-19 21:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UserEntry struct {
|
2019-06-28 22:20:53 +00:00
|
|
|
tokenutil.TokenParams
|
|
|
|
|
2015-07-13 05:09:24 +00:00
|
|
|
// Password is deprecated in Vault 0.2 in favor of
|
2016-05-15 16:58:36 +00:00
|
|
|
// PasswordHash, but is retained for backwards compatibility.
|
2015-04-19 21:59:30 +00:00
|
|
|
Password string
|
2015-07-13 05:09:24 +00:00
|
|
|
|
|
|
|
// PasswordHash is a bcrypt hash of the password. This is
|
|
|
|
// used instead of the actual password in Vault 0.2+.
|
|
|
|
PasswordHash []byte
|
|
|
|
|
2019-02-01 16:23:40 +00:00
|
|
|
Policies []string
|
2015-09-17 00:12:41 +00:00
|
|
|
|
|
|
|
// Duration after which the user will be revoked unless renewed
|
2019-02-01 16:23:40 +00:00
|
|
|
TTL time.Duration
|
2015-09-17 00:12:41 +00:00
|
|
|
|
|
|
|
// Maximum duration for which user can be valid
|
2019-02-01 16:23:40 +00:00
|
|
|
MaxTTL time.Duration
|
2018-05-21 18:47:28 +00:00
|
|
|
|
2019-02-01 16:23:40 +00:00
|
|
|
BoundCIDRs []*sockaddr.SockAddrMarshaler
|
2015-04-19 21:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const pathUserHelpSyn = `
|
|
|
|
Manage users allowed to authenticate.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathUserHelpDesc = `
|
|
|
|
This endpoint allows you to create, read, update, and delete users
|
|
|
|
that are allowed to authenticate.
|
2015-04-19 22:06:29 +00:00
|
|
|
|
|
|
|
Deleting a user will not revoke auth for prior authenticated users
|
|
|
|
with that name. To do this, do a revoke on "login/<username>" for
|
|
|
|
the username you want revoked. If you don't need to revoke login immediately,
|
|
|
|
then the next renew will cause the lease to expire.
|
2015-04-19 21:59:30 +00:00
|
|
|
`
|