Merge pull request #1732 from hashicorp/pre0.6.1-restore-compat

Restore compatibility with pre-0.6.1 servers for CLI/Go API calls
This commit is contained in:
Jeff Mitchell 2016-08-15 08:15:11 -04:00 committed by GitHub
commit 5eaab9f104
6 changed files with 75 additions and 100 deletions

View File

@ -22,21 +22,12 @@ func (c *Sys) AuditHash(path string, input string) (string, error) {
} }
defer resp.Body.Close() 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 { type d struct {
Hash string Hash string `json:"hash"`
} }
var result d var result d
err = mapstructure.Decode(secret.Data, &result) err = resp.DecodeJSON(&result)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -52,26 +43,32 @@ func (c *Sys) ListAudit() (map[string]*Audit, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
secret, err := ParseSecret(resp.Body) var result map[string]interface{}
err = resp.DecodeJSON(&result)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if secret == nil || secret.Data == nil || len(secret.Data) == 0 { mounts := map[string]*Audit{}
return nil, nil for k, v := range result {
} switch v.(type) {
case map[string]interface{}:
result := map[string]*Audit{} default:
for k, v := range secret.Data { continue
}
var res Audit var res Audit
err = mapstructure.Decode(v, &res) err = mapstructure.Decode(v, &res)
if err != nil { if err != nil {
return nil, err 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( 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 // 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. // documentation. Please refer to that documentation for more details.
type Audit struct { type Audit struct {

View File

@ -14,26 +14,32 @@ func (c *Sys) ListAuth() (map[string]*AuthMount, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
secret, err := ParseSecret(resp.Body) var result map[string]interface{}
err = resp.DecodeJSON(&result)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if secret == nil || secret.Data == nil || len(secret.Data) == 0 { mounts := map[string]*AuthMount{}
return nil, nil for k, v := range result {
} switch v.(type) {
case map[string]interface{}:
result := map[string]*AuthMount{} default:
for k, v := range secret.Data { continue
}
var res AuthMount var res AuthMount
err = mapstructure.Decode(v, &res) err = mapstructure.Decode(v, &res)
if err != nil { if err != nil {
return nil, err 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 { func (c *Sys) EnableAuth(path, authType, desc string) error {

View File

@ -28,17 +28,14 @@ func (c *Sys) Capabilities(token, path string) ([]string, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
secret, err := ParseSecret(resp.Body) var result map[string]interface{}
err = resp.DecodeJSON(&result)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if secret == nil || secret.Data == nil || len(secret.Data) == 0 {
return nil, nil
}
var capabilities []string var capabilities []string
capabilitiesRaw := secret.Data["capabilities"].([]interface{}) capabilitiesRaw := result["capabilities"].([]interface{})
for _, capability := range capabilitiesRaw { for _, capability := range capabilitiesRaw {
capabilities = append(capabilities, capability.(string)) capabilities = append(capabilities, capability.(string))
} }

View File

@ -15,26 +15,32 @@ func (c *Sys) ListMounts() (map[string]*MountOutput, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
secret, err := ParseSecret(resp.Body) var result map[string]interface{}
err = resp.DecodeJSON(&result)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if secret == nil || secret.Data == nil || len(secret.Data) == 0 { mounts := map[string]*MountOutput{}
return nil, nil for k, v := range result {
} switch v.(type) {
case map[string]interface{}:
result := map[string]*MountOutput{} default:
for k, v := range secret.Data { continue
}
var res MountOutput var res MountOutput
err = mapstructure.Decode(v, &res) err = mapstructure.Decode(v, &res)
if err != nil { if err != nil {
return nil, err 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 { 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() 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 var result MountConfigOutput
err = mapstructure.Decode(secret.Data, &result) err = resp.DecodeJSON(&result)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,10 +1,6 @@
package api package api
import ( import "fmt"
"fmt"
"github.com/mitchellh/mapstructure"
)
func (c *Sys) ListPolicies() ([]string, error) { func (c *Sys) ListPolicies() ([]string, error) {
r := c.c.NewRequest("GET", "/v1/sys/policy") r := c.c.NewRequest("GET", "/v1/sys/policy")
@ -14,22 +10,25 @@ func (c *Sys) ListPolicies() ([]string, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
secret, err := ParseSecret(resp.Body) var result map[string]interface{}
err = resp.DecodeJSON(&result)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if secret == nil || secret.Data == nil || len(secret.Data) == 0 { var ok bool
return nil, nil if _, ok = result["policies"]; !ok {
return nil, fmt.Errorf("policies not found in response")
} }
var result listPoliciesResp listRaw := result["policies"].([]interface{})
err = mapstructure.Decode(secret.Data, &result) var policies []string
if err != nil {
return nil, err for _, val := range listRaw {
policies = append(policies, val.(string))
} }
return result.Policies, err return policies, err
} }
func (c *Sys) GetPolicy(name string) (string, error) { func (c *Sys) GetPolicy(name string) (string, error) {
@ -45,22 +44,18 @@ func (c *Sys) GetPolicy(name string) (string, error) {
return "", err return "", err
} }
secret, err := ParseSecret(resp.Body) var result map[string]interface{}
err = resp.DecodeJSON(&result)
if err != nil { if err != nil {
return "", err return "", err
} }
if secret == nil || secret.Data == nil || len(secret.Data) == 0 { var ok bool
return "", nil if _, ok = result["rules"]; !ok {
return "", fmt.Errorf("rules not found in response")
} }
var result getPoliciesResp return result["rules"].(string), nil
err = mapstructure.Decode(secret.Data, &result)
if err != nil {
return "", err
}
return result.Rules, err
} }
func (c *Sys) PutPolicy(name, rules string) error { func (c *Sys) PutPolicy(name, rules string) error {

View File

@ -1,10 +1,6 @@
package api package api
import ( import "time"
"time"
"github.com/mitchellh/mapstructure"
)
func (c *Sys) Rotate() error { func (c *Sys) Rotate() error {
r := c.c.NewRequest("POST", "/v1/sys/rotate") r := c.c.NewRequest("POST", "/v1/sys/rotate")
@ -23,25 +19,12 @@ func (c *Sys) KeyStatus() (*KeyStatus, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
secret, err := ParseSecret(resp.Body) result := new(KeyStatus)
if err != nil { err = resp.DecodeJSON(result)
return nil, err return result, 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
} }
type KeyStatus struct { type KeyStatus struct {
Term int Term int `json:"term"`
InstallTime time.Time `json:"install_time"` InstallTime time.Time `json:"install_time"`
} }