Skip to content

Commit

Permalink
Fix for Engine path (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanveele authored Nov 4, 2021
1 parent ce4bab9 commit 3357102
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
7 changes: 6 additions & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ var deleteCmd = &cobra.Command{
isApproved, _ := cmd.Flags().GetBool("auto-approve")

// Setup Vault client
engine, path := vaultengine.PathSplitPrefix(path)
client := vaultengine.NewClient(vaultAddr, vaultToken, insecure, namespace)
engine, path, err := client.MountpathSplitPrefix(path)
if err != nil {
fmt.Println(err)
return err
}

client.UseEngine(engine)
client.SetEngineType(engineType)

Expand Down
7 changes: 6 additions & 1 deletion cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ var exportCmd = &cobra.Command{
exportFormat, _ := cmd.Flags().GetString("format")
output, _ := cmd.Flags().GetString("output")

engine, path := vaultengine.PathSplitPrefix(path)
client := vaultengine.NewClient(vaultAddr, vaultToken, insecure, namespace)
engine, path, err := client.MountpathSplitPrefix(path)
if err != nil {
fmt.Println(err)
return err
}

client.UseEngine(engine)
client.SetEngineType(engineType)

Expand Down
7 changes: 6 additions & 1 deletion cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ var importCmd = &cobra.Command{
doDecrypt, _ := cmd.Flags().GetBool("decrypt")
privateKey, _ := cmd.Flags().GetString("private-key")

engine, prefix := vaultengine.PathSplitPrefix(path)
client := vaultengine.NewClient(vaultAddr, vaultToken, insecure, namespace)
engine, prefix, err := client.MountpathSplitPrefix(path)
if err != nil {
fmt.Println(err)
return err
}

client.UseEngine(engine)
client.SetEngineType(engineType)

Expand Down
44 changes: 41 additions & 3 deletions pkg/vaultengine/vaultclient.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package vaultengine

import (
"errors"
"strings"

vault "github.com/hashicorp/vault/api"
)

Expand All @@ -18,9 +21,9 @@ type Client struct {
// NewClient creates a instance of the VaultClient struct
func NewClient(addr, token string, insecure bool, namespace string) *Client {
client := &Client{
token: token,
addr: addr,
insecure: insecure,
token: token,
addr: addr,
insecure: insecure,
namespace: namespace}

client.newVaultClient()
Expand All @@ -33,6 +36,41 @@ func (client *Client) UseEngine(engine string) {
client.engine = engine
}

func (client *Client) MountpathSplitPrefix(path string) (string, string, error) {
// Split Engine mountpath from path

r := client.vc.NewRequest("GET", "/v1/sys/internal/ui/mounts/"+path)
resp, err := client.vc.RawRequest(r)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
// any 404 indicates k/v v1
if resp != nil && resp.StatusCode == 404 {
return "", "path", nil
}
return "", "", err
}

secret, err := vault.ParseSecret(resp.Body)
if err != nil {
return "", "", err
}
if secret == nil {
return "", "", errors.New("nil response from pre-flight request")
}
var mountPath string
if mountPathRaw, ok := secret.Data["path"]; ok {
mountPath = mountPathRaw.(string)
}

mountPath = strings.TrimSuffix(mountPath, "/")
suffix := strings.Replace(path, mountPath, "", 1)
suffix = EnsureFolder(strings.TrimPrefix(suffix, "/"))

return mountPath, suffix, nil
}

// SetEngineType defines which vault secret engine type that is being used
func (client *Client) SetEngineType(engineType string) {
client.engineType = engineType
Expand Down

0 comments on commit 3357102

Please sign in to comment.