Merge pull request #1653 from hashicorp/userpass-no-500

Don't return 500 for user error in userpass when setting password
This commit is contained in:
Jeff Mitchell 2016-07-25 11:10:12 -04:00 committed by GitHub
commit a642b26f93
2 changed files with 14 additions and 8 deletions

View File

@ -46,26 +46,29 @@ func (b *backend) pathUserPasswordUpdate(
return nil, fmt.Errorf("username does not exist") return nil, fmt.Errorf("username does not exist")
} }
err = b.updateUserPassword(req, d, userEntry) userErr, intErr := b.updateUserPassword(req, d, userEntry)
if err != nil { if intErr != nil {
return nil, err return nil, err
} }
if userErr != nil {
return logical.ErrorResponse(userErr.Error()), nil
}
return nil, b.setUser(req.Storage, username, userEntry) return nil, b.setUser(req.Storage, username, userEntry)
} }
func (b *backend) updateUserPassword(req *logical.Request, d *framework.FieldData, userEntry *UserEntry) error { func (b *backend) updateUserPassword(req *logical.Request, d *framework.FieldData, userEntry *UserEntry) (error, error) {
password := d.Get("password").(string) password := d.Get("password").(string)
if password == "" { if password == "" {
return fmt.Errorf("missing password") return fmt.Errorf("missing password"), nil
} }
// Generate a hash of the password // Generate a hash of the password
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil { if err != nil {
return err return nil, err
} }
userEntry.PasswordHash = hash userEntry.PasswordHash = hash
return nil return nil, nil
} }
const pathUserPasswordHelpSyn = ` const pathUserPasswordHelpSyn = `

View File

@ -156,10 +156,13 @@ func (b *backend) userCreateUpdate(req *logical.Request, d *framework.FieldData)
} }
if _, ok := d.GetOk("password"); ok { if _, ok := d.GetOk("password"); ok {
err = b.updateUserPassword(req, d, userEntry) userErr, intErr := b.updateUserPassword(req, d, userEntry)
if err != nil { if intErr != nil {
return nil, err return nil, err
} }
if userErr != nil {
return logical.ErrorResponse(userErr.Error()), nil
}
} }
if policiesRaw, ok := d.GetOk("policies"); ok { if policiesRaw, ok := d.GetOk("policies"); ok {