From 6e9e88cc41bbb01de9942c104c861d36c5fff648 Mon Sep 17 00:00:00 2001 From: Calvin Leung Huang Date: Fri, 3 Nov 2017 14:22:48 -0400 Subject: [PATCH] Do not initialize result map in parseRole, refactor ListResponseWithInfo --- builtin/logical/ssh/path_roles.go | 10 ++++++---- logical/response.go | 9 +++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/builtin/logical/ssh/path_roles.go b/builtin/logical/ssh/path_roles.go index 0387a123316f..ec2d534496b5 100644 --- a/builtin/logical/ssh/path_roles.go +++ b/builtin/logical/ssh/path_roles.go @@ -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: @@ -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, + } } } diff --git a/logical/response.go b/logical/response.go index 33be1a43cb67..ab92fd541b07 100644 --- a/logical/response.go +++ b/logical/response.go @@ -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 }