Fallback to version 1 if the vault server is too old to have the kv preflight endpoint (#4445)

This commit is contained in:
Brian Kassouf 2018-04-24 15:49:06 -07:00 committed by GitHub
parent f61ea271d7
commit 817f816eb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 0 deletions

View File

@ -116,6 +116,9 @@ func (c *KVGetCommand) Run(args []string) int {
secret, err := kvReadRequest(client, path, versionParam)
if err != nil {
c.UI.Error(fmt.Sprintf("Error reading %s: %s", path, err))
if secret != nil {
OutputSecret(c.UI, secret)
}
return 2
}
if secret == nil {
@ -142,6 +145,11 @@ func (c *KVGetCommand) Run(args []string) int {
return OutputSecret(c.UI, secret)
}
if len(secret.Warnings) > 0 {
tf := TableFormatter{}
tf.printWarnings(c.UI, secret)
}
if metadata, ok := secret.Data["metadata"]; ok && metadata != nil {
c.UI.Info(getHeaderForMap("Metadata", metadata.(map[string]interface{})))
OutputData(c.UI, metadata)

View File

@ -47,6 +47,12 @@ func kvPreflightVersionRequest(client *api.Client, path string) (string, int, er
defer resp.Body.Close()
}
if err != nil {
// 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
}
return "", 0, err
}

View File

@ -142,6 +142,9 @@ func (c *KVPutCommand) Run(args []string) int {
secret, err := client.Logical().Write(path, data)
if err != nil {
c.UI.Error(fmt.Sprintf("Error writing data to %s: %s", path, err))
if secret != nil {
OutputSecret(c.UI, secret)
}
return 2
}
if secret == nil {