diff --git a/api/sys_audit.go b/api/sys_audit.go index b6fed6af94..1ffdef880f 100644 --- a/api/sys_audit.go +++ b/api/sys_audit.go @@ -22,21 +22,12 @@ func (c *Sys) AuditHash(path string, input string) (string, error) { } defer resp.Body.Close() - secret, err := ParseSecret(resp.Body) - if err != nil { - return "", err - } - - if secret == nil || secret.Data == nil || len(secret.Data) == 0 { - return "", nil - } - type d struct { - Hash string + Hash string `json:"hash"` } var result d - err = mapstructure.Decode(secret.Data, &result) + err = resp.DecodeJSON(&result) if err != nil { return "", err } @@ -52,26 +43,32 @@ func (c *Sys) ListAudit() (map[string]*Audit, error) { } defer resp.Body.Close() - secret, err := ParseSecret(resp.Body) + var result map[string]interface{} + err = resp.DecodeJSON(&result) if err != nil { return nil, err } - if secret == nil || secret.Data == nil || len(secret.Data) == 0 { - return nil, nil - } - - result := map[string]*Audit{} - for k, v := range secret.Data { + mounts := map[string]*Audit{} + for k, v := range result { + switch v.(type) { + case map[string]interface{}: + default: + continue + } var res Audit err = mapstructure.Decode(v, &res) if err != nil { return nil, err } - result[k] = &res + // Not a mount, some other api.Secret data + if res.Type == "" { + continue + } + mounts[k] = &res } - return result, err + return mounts, nil } func (c *Sys) EnableAudit( @@ -106,7 +103,7 @@ func (c *Sys) DisableAudit(path string) error { } // Structures for the requests/resposne are all down here. They aren't -// individually documentd because the map almost directly to the raw HTTP API +// individually documented because the map almost directly to the raw HTTP API // documentation. Please refer to that documentation for more details. type Audit struct { diff --git a/api/sys_auth.go b/api/sys_auth.go index 743b8e6d8a..1940e84172 100644 --- a/api/sys_auth.go +++ b/api/sys_auth.go @@ -14,26 +14,32 @@ func (c *Sys) ListAuth() (map[string]*AuthMount, error) { } defer resp.Body.Close() - secret, err := ParseSecret(resp.Body) + var result map[string]interface{} + err = resp.DecodeJSON(&result) if err != nil { return nil, err } - if secret == nil || secret.Data == nil || len(secret.Data) == 0 { - return nil, nil - } - - result := map[string]*AuthMount{} - for k, v := range secret.Data { + mounts := map[string]*AuthMount{} + for k, v := range result { + switch v.(type) { + case map[string]interface{}: + default: + continue + } var res AuthMount err = mapstructure.Decode(v, &res) if err != nil { return nil, err } - result[k] = &res + // Not a mount, some other api.Secret data + if res.Type == "" { + continue + } + mounts[k] = &res } - return result, err + return mounts, nil } func (c *Sys) EnableAuth(path, authType, desc string) error { diff --git a/api/sys_capabilities.go b/api/sys_capabilities.go index 6d501a495e..80f6218849 100644 --- a/api/sys_capabilities.go +++ b/api/sys_capabilities.go @@ -28,17 +28,14 @@ func (c *Sys) Capabilities(token, path string) ([]string, error) { } defer resp.Body.Close() - secret, err := ParseSecret(resp.Body) + var result map[string]interface{} + err = resp.DecodeJSON(&result) if err != nil { return nil, err } - if secret == nil || secret.Data == nil || len(secret.Data) == 0 { - return nil, nil - } - var capabilities []string - capabilitiesRaw := secret.Data["capabilities"].([]interface{}) + capabilitiesRaw := result["capabilities"].([]interface{}) for _, capability := range capabilitiesRaw { capabilities = append(capabilities, capability.(string)) } diff --git a/api/sys_mounts.go b/api/sys_mounts.go index 504e5711be..ca5e42707a 100644 --- a/api/sys_mounts.go +++ b/api/sys_mounts.go @@ -15,26 +15,32 @@ func (c *Sys) ListMounts() (map[string]*MountOutput, error) { } defer resp.Body.Close() - secret, err := ParseSecret(resp.Body) + var result map[string]interface{} + err = resp.DecodeJSON(&result) if err != nil { return nil, err } - if secret == nil || secret.Data == nil || len(secret.Data) == 0 { - return nil, nil - } - - result := map[string]*MountOutput{} - for k, v := range secret.Data { + mounts := map[string]*MountOutput{} + for k, v := range result { + switch v.(type) { + case map[string]interface{}: + default: + continue + } var res MountOutput err = mapstructure.Decode(v, &res) if err != nil { return nil, err } - result[k] = &res + // Not a mount, some other api.Secret data + if res.Type == "" { + continue + } + mounts[k] = &res } - return result, nil + return mounts, nil } func (c *Sys) Mount(path string, mountInfo *MountInput) error { @@ -104,17 +110,8 @@ func (c *Sys) MountConfig(path string) (*MountConfigOutput, error) { } defer resp.Body.Close() - secret, err := ParseSecret(resp.Body) - if err != nil { - return nil, err - } - - if secret == nil || secret.Data == nil || len(secret.Data) == 0 { - return nil, nil - } - var result MountConfigOutput - err = mapstructure.Decode(secret.Data, &result) + err = resp.DecodeJSON(&result) if err != nil { return nil, err } diff --git a/api/sys_policy.go b/api/sys_policy.go index 35e18b3885..ba0e17fab5 100644 --- a/api/sys_policy.go +++ b/api/sys_policy.go @@ -1,10 +1,6 @@ package api -import ( - "fmt" - - "github.com/mitchellh/mapstructure" -) +import "fmt" func (c *Sys) ListPolicies() ([]string, error) { r := c.c.NewRequest("GET", "/v1/sys/policy") @@ -14,22 +10,25 @@ func (c *Sys) ListPolicies() ([]string, error) { } defer resp.Body.Close() - secret, err := ParseSecret(resp.Body) + var result map[string]interface{} + err = resp.DecodeJSON(&result) if err != nil { return nil, err } - if secret == nil || secret.Data == nil || len(secret.Data) == 0 { - return nil, nil + var ok bool + if _, ok = result["policies"]; !ok { + return nil, fmt.Errorf("policies not found in response") } - var result listPoliciesResp - err = mapstructure.Decode(secret.Data, &result) - if err != nil { - return nil, err + listRaw := result["policies"].([]interface{}) + var policies []string + + for _, val := range listRaw { + policies = append(policies, val.(string)) } - return result.Policies, err + return policies, err } func (c *Sys) GetPolicy(name string) (string, error) { @@ -45,22 +44,18 @@ func (c *Sys) GetPolicy(name string) (string, error) { return "", err } - secret, err := ParseSecret(resp.Body) + var result map[string]interface{} + err = resp.DecodeJSON(&result) if err != nil { return "", err } - if secret == nil || secret.Data == nil || len(secret.Data) == 0 { - return "", nil + var ok bool + if _, ok = result["rules"]; !ok { + return "", fmt.Errorf("rules not found in response") } - var result getPoliciesResp - err = mapstructure.Decode(secret.Data, &result) - if err != nil { - return "", err - } - - return result.Rules, err + return result["rules"].(string), nil } func (c *Sys) PutPolicy(name, rules string) error { diff --git a/api/sys_rotate.go b/api/sys_rotate.go index 2a78b4691c..8108dced82 100644 --- a/api/sys_rotate.go +++ b/api/sys_rotate.go @@ -1,10 +1,6 @@ package api -import ( - "time" - - "github.com/mitchellh/mapstructure" -) +import "time" func (c *Sys) Rotate() error { r := c.c.NewRequest("POST", "/v1/sys/rotate") @@ -23,25 +19,12 @@ func (c *Sys) KeyStatus() (*KeyStatus, error) { } defer resp.Body.Close() - secret, err := ParseSecret(resp.Body) - if err != nil { - return nil, err - } - - if secret == nil || secret.Data == nil || len(secret.Data) == 0 { - return nil, nil - } - - var result KeyStatus - err = mapstructure.Decode(secret.Data, &result) - if err != nil { - return nil, err - } - - return &result, err + result := new(KeyStatus) + err = resp.DecodeJSON(result) + return result, err } type KeyStatus struct { - Term int + Term int `json:"term"` InstallTime time.Time `json:"install_time"` }