Skip to content

Commit

Permalink
Do not initialize result map in parseRole, refactor ListResponseWithInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
calvn committed Nov 3, 2017
1 parent 13a8fd7 commit 6e9e88c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 6 additions & 4 deletions builtin/logical/ssh/path_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,10 @@ func (b *backend) getRole(s logical.Storage, n string) (*sshRole, error) {
}

// parseRole converts a sshRole object into its map[string]interface representation,
// with appropriate values for each keyType. If the keyType is invalid, it will retun
// with appropriate values for each KeyType. If the KeyType is invalid, it will retun
// an error.
func (b *backend) parseRole(role *sshRole) (map[string]interface{}, error) {
result := map[string]interface{}{}
var result map[string]interface{}

switch role.KeyType {
case KeyTypeOTP:
Expand Down Expand Up @@ -589,8 +589,10 @@ func (b *backend) pathRoleList(req *logical.Request, d *framework.FieldData) (*l
continue
}

keyInfo[entry] = map[string]interface{}{
"key_type": roleInfo["key_type"],
if keyType, ok := roleInfo["key_type"]; ok {
keyInfo[entry] = map[string]interface{}{
"key_type": keyType,
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions logical/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,17 @@ func ListResponse(keys []string) *Response {
func ListResponseWithInfo(keys []string, keyInfo map[string]interface{}) *Response {
resp := ListResponse(keys)

resp.Data["key_info"] = map[string]interface{}{}
keyInfoData := make(map[string]interface{})
for _, key := range keys {
val, ok := keyInfo[key]
if ok {
resp.Data["key_info"].(map[string]interface{})[key] = val
keyInfoData[key] = val
}
}

if len(keyInfoData) > 0 {
resp.Data["key_info"] = keyInfoData
}

return resp
}

0 comments on commit 6e9e88c

Please sign in to comment.