Skip to content

Commit

Permalink
feat: allow to specify kv version to avoid autodetection
Browse files Browse the repository at this point in the history
Avoiding the call the get the mounts, and with the caching of the vault
proxy, is it possible to have an offline copy of the vault content.

See hashicorp/vault#19879
  • Loading branch information
adrianlzt authored and xbglowx committed Jan 4, 2024
1 parent 17881c8 commit 88a7584
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
18 changes: 11 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.

import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -56,7 +57,7 @@ Regex are supported`,
return checkRequiredFlags(cmd)
},
Run: func(cmd *cobra.Command, args []string) {
VaultKvSearch(args, searchObjects, showSecrets, useRegex, crawlingDelay, jsonOutput)
VaultKvSearch(args, searchObjects, showSecrets, useRegex, crawlingDelay, kvVersion, jsonOutput)
},
Args: cobra.ExactArgs(2),
Example: "vault-kv-search kv/ foo",
Expand All @@ -68,19 +69,22 @@ func Execute() {
cobra.CheckErr(RootCmd.Execute())
}

var crawlingDelay int
var jsonOutput bool
var showSecrets bool
var useRegex bool
var searchObjects []string
var (
crawlingDelay int
kvVersion int
jsonOutput bool
showSecrets bool
useRegex bool
searchObjects []string
)

func init() {
RootCmd.Flags().IntVarP(&crawlingDelay, "delay", "d", 15, "Crawling delay in millisconds")
RootCmd.Flags().IntVarP(&kvVersion, "kv-version", "k", 0, "KV version (1,2). Autodetect if not defined")
RootCmd.Flags().BoolVarP(&jsonOutput, "json", "j", false, "Output as JSON")
RootCmd.Flags().BoolVarP(&showSecrets, "showsecrets", "s", false, "Show secrets values")
RootCmd.Flags().BoolVarP(&useRegex, "regex", "r", false, "Enable searching regex substring")
RootCmd.Flags().StringSliceVar(&searchObjects, "search", []string{"value"}, "Which Vault objects to "+
"search against. Choices are any and all of the following 'key,value,path'. Can be specified multiple times or "+
"once using format CSV. Defaults to 'value'")

}
19 changes: 11 additions & 8 deletions cmd/vault-kv-search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"encoding/json"
"errors"
"fmt"
vault "github.com/hashicorp/vault/api"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"

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

type vaultClient struct {
Expand Down Expand Up @@ -54,7 +55,7 @@ func (vc *vaultClient) getKvVersion(path string) (int, error) {
}

// VaultKvSearch is the main function
func VaultKvSearch(args []string, searchObjects []string, showSecrets bool, useRegex bool, crawlingDelay int, jsonOutput bool) {
func VaultKvSearch(args []string, searchObjects []string, showSecrets bool, useRegex bool, crawlingDelay int, version int, jsonOutput bool) {
config := vault.DefaultConfig()
config.Timeout = time.Second * 5

Expand All @@ -70,18 +71,21 @@ func VaultKvSearch(args []string, searchObjects []string, showSecrets bool, useR
sys: client.Sys(),
crawlingDelay: crawlingDelay,
jsonOutput: jsonOutput,
showSecrets: showSecrets, //pragma: allowlist secret
showSecrets: showSecrets, // pragma: allowlist secret
useRegex: useRegex,
searchObjects: searchObjects,
searchString: args[1],
wg: sync.WaitGroup{},
}

startPath := args[0]
version, err := vc.getKvVersion(startPath)
if err != nil {
fmt.Println(err)
os.Exit(1)

if version == 0 {
version, err = vc.getKvVersion(startPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

if !vc.jsonOutput {
Expand Down Expand Up @@ -171,7 +175,6 @@ func (vc *vaultClient) digDeeper(version int, data map[string]interface{}, dirEn

func (vc *vaultClient) readLeafs(path string, searchObjects []string, version int) {
pathList, err := vc.logical.List(path)

if err != nil {
fmt.Fprintf(os.Stderr, "Failed to list: %s\n%s", vc.searchString, err)
os.Exit(1)
Expand Down

0 comments on commit 88a7584

Please sign in to comment.