New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Userpass: Update the password and policies associated to user #1216
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1980d0f
Userpass: Support updating policies and password
vishalnayak aa89269
Tests for updating password and policies in userpass backend
vishalnayak e51661c
Added paths to update policies and password
vishalnayak cad1ee3
Reuse the variable instead of fetching 'name' again
vishalnayak 0e1769d
Change path structure of password and policies endpoints in userpass
vishalnayak c29a121
Fetch and store UserEntry to properly handle both create and update
vishalnayak 5249c0d
Refactor updating and creating userEntry into a helper function
vishalnayak 91f4aab
Input validations and field renaming
vishalnayak 79ff367
Added API documentation for userpass backend
vishalnayak b8e007c
Use helper for existence check. Avoid panic by fetching default value…
vishalnayak 5905429
Reduce the visibility of setUser
vishalnayak cfbab2c
Refactor updating user values
vishalnayak 6f2b428
Addessing review comments
vishalnayak daab5d6
Fix SanitizeTTL check
vishalnayak 4ae83b7
Add comments to existence functions
vishalnayak File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package userpass | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"golang.org/x/crypto/bcrypt" | ||
|
||
"github.com/hashicorp/vault/logical" | ||
"github.com/hashicorp/vault/logical/framework" | ||
) | ||
|
||
func pathUserPassword(b *backend) *framework.Path { | ||
return &framework.Path{ | ||
Pattern: "users/password/" + framework.GenericNameRegex("name"), | ||
Fields: map[string]*framework.FieldSchema{ | ||
"name": &framework.FieldSchema{ | ||
Type: framework.TypeString, | ||
Description: "Username for this user.", | ||
}, | ||
|
||
"password": &framework.FieldSchema{ | ||
Type: framework.TypeString, | ||
Description: "Password for this user.", | ||
}, | ||
}, | ||
|
||
Callbacks: map[logical.Operation]framework.OperationFunc{ | ||
logical.UpdateOperation: b.pathUserPasswordUpdate, | ||
}, | ||
|
||
HelpSynopsis: pathUserPasswordHelpSyn, | ||
HelpDescription: pathUserPasswordHelpDesc, | ||
} | ||
} | ||
|
||
func (b *backend) pathUserPasswordUpdate( | ||
req *logical.Request, d *framework.FieldData) (*logical.Response, error) { | ||
username := strings.ToLower(d.Get("name").(string)) | ||
if username == "" { | ||
return nil, fmt.Errorf("missing username") | ||
} | ||
|
||
password := d.Get("password").(string) | ||
if password == "" { | ||
return nil, fmt.Errorf("missing password") | ||
} | ||
|
||
userEntry, err := b.User(req.Storage, username) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if userEntry == nil { | ||
return nil, nil | ||
} | ||
|
||
// Generate a hash of the password | ||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Set the new password hash | ||
userEntry.PasswordHash = hash | ||
|
||
// Store the UserEntry | ||
err = b.SetUser(req.Storage, username, userEntry) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return nil, nil | ||
} | ||
|
||
const pathUserPasswordHelpSyn = ` | ||
Reset user's password. | ||
` | ||
|
||
const pathUserPasswordHelpDesc = ` | ||
This endpoint allows resetting the user's password. | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package userpass | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/vault/logical" | ||
"github.com/hashicorp/vault/logical/framework" | ||
) | ||
|
||
func pathUserPolicies(b *backend) *framework.Path { | ||
return &framework.Path{ | ||
Pattern: "users/policies/" + framework.GenericNameRegex("name"), | ||
Fields: map[string]*framework.FieldSchema{ | ||
"name": &framework.FieldSchema{ | ||
Type: framework.TypeString, | ||
Description: "Username for this user.", | ||
}, | ||
"policies": &framework.FieldSchema{ | ||
Type: framework.TypeString, | ||
Description: "Comma-separated list of policies", | ||
}, | ||
}, | ||
|
||
Callbacks: map[logical.Operation]framework.OperationFunc{ | ||
logical.UpdateOperation: b.pathUserPoliciesUpdate, | ||
}, | ||
|
||
HelpSynopsis: pathUserPoliciesHelpSyn, | ||
HelpDescription: pathUserPoliciesHelpDesc, | ||
} | ||
} | ||
func (b *backend) pathUserPoliciesUpdate( | ||
req *logical.Request, d *framework.FieldData) (*logical.Response, error) { | ||
username := strings.ToLower(d.Get("name").(string)) | ||
if username == "" { | ||
return nil, fmt.Errorf("missing username") | ||
} | ||
|
||
policies := strings.Split(d.Get("policies").(string), ",") | ||
for i, p := range policies { | ||
policies[i] = strings.TrimSpace(p) | ||
} | ||
|
||
userEntry, err := b.User(req.Storage, username) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if userEntry == nil { | ||
return nil, nil | ||
} | ||
|
||
userEntry.Policies = policies | ||
|
||
// Store the UserEntry | ||
err = b.SetUser(req.Storage, username, userEntry) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return nil, nil | ||
} | ||
|
||
const pathUserPoliciesHelpSyn = ` | ||
Update the policies associated with the username. | ||
` | ||
|
||
const pathUserPoliciesHelpDesc = ` | ||
This endpoint allows updating the policies associated with the username. | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,14 +43,31 @@ func pathUsers(b *backend) *framework.Path { | |
Callbacks: map[logical.Operation]framework.OperationFunc{ | ||
logical.DeleteOperation: b.pathUserDelete, | ||
logical.ReadOperation: b.pathUserRead, | ||
logical.UpdateOperation: b.pathUserWrite, | ||
logical.UpdateOperation: b.pathUserWrite, | ||
logical.CreateOperation: b.pathUserWrite, | ||
}, | ||
|
||
ExistenceCheck: b.userExistenceCheck, | ||
|
||
HelpSynopsis: pathUserHelpSyn, | ||
HelpDescription: pathUserHelpDesc, | ||
} | ||
} | ||
|
||
func (b *backend) userExistenceCheck(req *logical.Request, data *framework.FieldData) (bool, error) { | ||
username := data.Get("name").(string) | ||
if username == "" { | ||
return false, fmt.Errorf("name cannot be empty") | ||
} | ||
|
||
entry, err := req.Storage.Get("user/" + strings.ToLower(username)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use the same logic here that you're using in |
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return entry != nil, nil | ||
} | ||
|
||
func (b *backend) User(s logical.Storage, n string) (*UserEntry, error) { | ||
entry, err := s.Get("user/" + strings.ToLower(n)) | ||
if err != nil { | ||
|
@@ -68,6 +85,15 @@ func (b *backend) User(s logical.Storage, n string) (*UserEntry, error) { | |
return &result, nil | ||
} | ||
|
||
func (b *backend) SetUser(s logical.Storage, username string, userEntry *UserEntry) error { | ||
entry, err := logical.StorageEntryJSON("user/"+username, userEntry) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return s.Put(entry) | ||
} | ||
|
||
func (b *backend) pathUserDelete( | ||
req *logical.Request, d *framework.FieldData) (*logical.Response, error) { | ||
err := req.Storage.Delete("user/" + strings.ToLower(d.Get("name").(string))) | ||
|
@@ -98,7 +124,15 @@ func (b *backend) pathUserRead( | |
func (b *backend) pathUserWrite( | ||
req *logical.Request, d *framework.FieldData) (*logical.Response, error) { | ||
name := strings.ToLower(d.Get("name").(string)) | ||
if name == "" { | ||
return nil, fmt.Errorf("missing username") | ||
} | ||
|
||
password := d.Get("password").(string) | ||
if password == "" { | ||
return nil, fmt.Errorf("missing password") | ||
} | ||
|
||
policies := strings.Split(d.Get("policies").(string), ",") | ||
for i, p := range policies { | ||
policies[i] = strings.TrimSpace(p) | ||
|
@@ -117,8 +151,7 @@ func (b *backend) pathUserWrite( | |
return logical.ErrorResponse(fmt.Sprintf("err: %s", err)), nil | ||
} | ||
|
||
// Store it | ||
entry, err := logical.StorageEntryJSON("user/"+name, &UserEntry{ | ||
err = b.SetUser(req.Storage, name, &UserEntry{ | ||
PasswordHash: hash, | ||
Policies: policies, | ||
TTL: ttl, | ||
|
@@ -127,9 +160,6 @@ func (b *backend) pathUserWrite( | |
if err != nil { | ||
return nil, err | ||
} | ||
if err := req.Storage.Put(entry); err != nil { | ||
return nil, err | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TestBacken
->TestBackend