Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to specify kv version to avoid autodetection #32

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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