2018-03-21 22:02:41 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-04-04 02:35:45 +00:00
|
|
|
"io"
|
2018-03-21 22:02:41 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
2018-04-04 00:58:42 +00:00
|
|
|
"github.com/hashicorp/vault/helper/strutil"
|
2018-03-21 22:02:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func kvReadRequest(client *api.Client, path string, params map[string]string) (*api.Secret, error) {
|
|
|
|
r := client.NewRequest("GET", "/v1/"+path)
|
|
|
|
for k, v := range params {
|
|
|
|
r.Params.Set(k, v)
|
|
|
|
}
|
|
|
|
resp, err := client.RawRequest(r)
|
|
|
|
if resp != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
if resp != nil && resp.StatusCode == 404 {
|
2018-04-04 16:26:06 +00:00
|
|
|
secret, parseErr := api.ParseSecret(resp.Body)
|
|
|
|
switch parseErr {
|
2018-04-04 02:35:45 +00:00
|
|
|
case nil:
|
|
|
|
case io.EOF:
|
|
|
|
return nil, nil
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if secret != nil && (len(secret.Warnings) > 0 || len(secret.Data) > 0) {
|
|
|
|
return secret, nil
|
|
|
|
}
|
2018-03-21 22:02:41 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2018-04-23 22:00:02 +00:00
|
|
|
func kvPreflightVersionRequest(client *api.Client, path string) (string, int, error) {
|
2018-04-23 22:16:10 +00:00
|
|
|
r := client.NewRequest("GET", "/v1/sys/internal/ui/mounts/"+path)
|
2018-03-21 22:02:41 +00:00
|
|
|
resp, err := client.RawRequest(r)
|
|
|
|
if resp != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-04-24 22:49:06 +00:00
|
|
|
// If we get a 404 we are using an older version of vault, default to
|
|
|
|
// version 1
|
|
|
|
if resp != nil && resp.StatusCode == 404 {
|
|
|
|
return "", 1, nil
|
|
|
|
}
|
|
|
|
|
2018-04-23 22:00:02 +00:00
|
|
|
return "", 0, err
|
2018-03-21 22:02:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 22:00:02 +00:00
|
|
|
secret, err := api.ParseSecret(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
2018-03-21 22:02:41 +00:00
|
|
|
}
|
2018-04-23 22:00:02 +00:00
|
|
|
var mountPath string
|
|
|
|
if mountPathRaw, ok := secret.Data["path"]; ok {
|
|
|
|
mountPath = mountPathRaw.(string)
|
2018-03-21 22:02:41 +00:00
|
|
|
}
|
2018-04-23 22:00:02 +00:00
|
|
|
options := secret.Data["options"]
|
|
|
|
if options == nil {
|
|
|
|
return mountPath, 1, nil
|
2018-03-21 22:02:41 +00:00
|
|
|
}
|
2018-04-23 22:00:02 +00:00
|
|
|
versionRaw := options.(map[string]interface{})["version"]
|
|
|
|
if versionRaw == nil {
|
|
|
|
return mountPath, 1, nil
|
2018-03-21 22:02:41 +00:00
|
|
|
}
|
2018-04-23 22:00:02 +00:00
|
|
|
version := versionRaw.(string)
|
|
|
|
switch version {
|
|
|
|
case "", "1":
|
|
|
|
return mountPath, 1, nil
|
|
|
|
case "2":
|
|
|
|
return mountPath, 2, nil
|
2018-03-21 22:02:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 22:00:02 +00:00
|
|
|
return mountPath, 1, nil
|
2018-03-21 22:02:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 22:00:02 +00:00
|
|
|
func isKVv2(path string, client *api.Client) (string, bool, error) {
|
|
|
|
mountPath, version, err := kvPreflightVersionRequest(client, path)
|
2018-03-21 22:02:41 +00:00
|
|
|
if err != nil {
|
2018-04-23 22:00:02 +00:00
|
|
|
return "", false, err
|
2018-03-21 22:02:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 22:00:02 +00:00
|
|
|
return mountPath, version == 2, nil
|
2018-03-21 22:02:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 22:00:02 +00:00
|
|
|
func addPrefixToVKVPath(p, mountPath, apiPrefix string) string {
|
|
|
|
p = strings.TrimPrefix(p, mountPath)
|
|
|
|
return path.Join(mountPath, apiPrefix, p)
|
2018-03-21 22:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getHeaderForMap(header string, data map[string]interface{}) string {
|
|
|
|
maxKey := 0
|
|
|
|
for k := range data {
|
|
|
|
if len(k) > maxKey {
|
|
|
|
maxKey = len(k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4 for the column spaces and 5 for the len("value")
|
|
|
|
totalLen := maxKey + 4 + 5
|
|
|
|
|
|
|
|
equalSigns := totalLen - (len(header) + 2)
|
|
|
|
|
|
|
|
// If we have zero or fewer equal signs bump it back up to two on either
|
|
|
|
// side of the header.
|
|
|
|
if equalSigns <= 0 {
|
|
|
|
equalSigns = 4
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the number of equal signs is not divisible by two add a sign.
|
|
|
|
if equalSigns%2 != 0 {
|
|
|
|
equalSigns = equalSigns + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s %s %s", strings.Repeat("=", equalSigns/2), header, strings.Repeat("=", equalSigns/2))
|
|
|
|
}
|
2018-04-04 00:58:42 +00:00
|
|
|
|
|
|
|
func kvParseVersionsFlags(versions []string) []string {
|
|
|
|
versionsOut := make([]string, 0, len(versions))
|
|
|
|
for _, v := range versions {
|
|
|
|
versionsOut = append(versionsOut, strutil.ParseStringSlice(v, ",")...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return versionsOut
|
|
|
|
}
|