open-vault/builtin/credential/userpass/path_user_policies.go

79 lines
2 KiB
Go
Raw Normal View History

package userpass
import (
"context"
2016-03-16 18:21:14 +00:00
"fmt"
2016-03-16 17:39:20 +00:00
"github.com/hashicorp/vault/sdk/framework"
2019-04-12 22:08:46 +00:00
"github.com/hashicorp/vault/sdk/helper/policyutil"
"github.com/hashicorp/vault/sdk/helper/tokenutil"
"github.com/hashicorp/vault/sdk/logical"
)
func pathUserPolicies(b *backend) *framework.Path {
return &framework.Path{
2016-03-15 21:47:13 +00:00
Pattern: "users/" + framework.GenericNameRegex("username") + "/policies$",
Fields: map[string]*framework.FieldSchema{
"username": {
Type: framework.TypeString,
Description: "Username for this user.",
},
"policies": {
Type: framework.TypeCommaStringSlice,
Description: tokenutil.DeprecationText("token_policies"),
Deprecated: true,
},
"token_policies": {
Type: framework.TypeCommaStringSlice,
Description: "Comma-separated list of policies",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathUserPoliciesUpdate,
},
HelpSynopsis: pathUserPoliciesHelpSyn,
HelpDescription: pathUserPoliciesHelpDesc,
}
}
func (b *backend) pathUserPoliciesUpdate(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
2016-03-16 17:39:20 +00:00
username := d.Get("username").(string)
userEntry, err := b.user(ctx, req.Storage, username)
2016-03-16 17:39:20 +00:00
if err != nil {
return nil, err
}
2016-03-16 18:21:14 +00:00
if userEntry == nil {
return nil, fmt.Errorf("username does not exist")
}
2016-03-16 17:39:20 +00:00
policiesRaw, ok := d.GetOk("token_policies")
if !ok {
policiesRaw, ok = d.GetOk("policies")
if ok {
userEntry.Policies = policyutil.ParsePolicies(policiesRaw)
userEntry.TokenPolicies = userEntry.Policies
}
} else {
userEntry.TokenPolicies = policyutil.ParsePolicies(policiesRaw)
_, ok = d.GetOk("policies")
if ok {
userEntry.Policies = userEntry.TokenPolicies
} else {
userEntry.Policies = nil
}
}
2016-03-16 17:39:20 +00:00
return nil, b.setUser(ctx, req.Storage, username, userEntry)
2016-03-16 17:39:20 +00:00
}
const pathUserPoliciesHelpSyn = `
Update the policies associated with the username.
`
const pathUserPoliciesHelpDesc = `
This endpoint allows updating the policies associated with the username.
`