Skip to content

Commit

Permalink
feat(generic): support legacy kv engine of type generic
Browse files Browse the repository at this point in the history
  • Loading branch information
FalcoSuessgott committed Apr 12, 2024
1 parent d2e6f15 commit 1082212
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 43 deletions.
6 changes: 4 additions & 2 deletions cmd/export/export.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package export

import (
"errors"
"fmt"
"io"
"log"
Expand All @@ -16,7 +17,7 @@ import (

const envVarExportPrefix = "VKV_EXPORT_"

var errInvalidFlagCombination = fmt.Errorf("invalid flag combination specified")
var errInvalidFlagCombination = errors.New("invalid flag combination specified")

// exportOptions holds all available commandline options.
type exportOptions struct {
Expand All @@ -41,6 +42,7 @@ type exportOptions struct {
}

// NewExportCmd export subcommand.
//
//nolint:lll
func NewExportCmd(writer io.Writer, vaultClient *vault.Vault) *cobra.Command {
var err error
Expand Down Expand Up @@ -134,7 +136,7 @@ func (o *exportOptions) validateFlags() error {
case (o.OnlyKeys && o.ShowValues), (o.OnlyPaths && o.ShowValues), (o.OnlyKeys && o.OnlyPaths):
return errInvalidFlagCombination
case o.EnginePath == "" && o.Path == "":
return fmt.Errorf("no KV-paths given. Either --engine-path / -e or --path / -p needs to be specified")
return errors.New("no KV-paths given. Either --engine-path / -e or --path / -p needs to be specified")
case true:
switch strings.ToLower(o.FormatString) {
case "yaml", "yml":
Expand Down
11 changes: 6 additions & 5 deletions cmd/imp/import.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package imp

import (
"errors"
"fmt"
"io"
"log"
Expand All @@ -16,7 +17,7 @@ import (

const envVarImportPrefix = "VKV_IMPORT_"

var errInvalidFlagCombination = fmt.Errorf("invalid flag combination specified")
var errInvalidFlagCombination = errors.New("invalid flag combination specified")

type importOptions struct {
Force bool `env:"FORCE"`
Expand All @@ -31,7 +32,7 @@ type importOptions struct {
}

// NewImportCmd import subcommand.
//nolint: cyclop, gocognit
// nolint: cyclop, gocognit
func NewImportCmd(writer io.Writer, vaultClient *vault.Vault) *cobra.Command {
var err error

Expand Down Expand Up @@ -127,11 +128,11 @@ func (o *importOptions) parseEnvs() error {
return nil
}

//nolint: cyclop
// nolint: cyclop
func (o *importOptions) validateFlags(args []string) error {
switch {
case len(args) == 0 && o.Path == "":
return fmt.Errorf("no KV-path given, -path / -p needs to be specified")
return errors.New("no KV-path given, -path / -p needs to be specified")
case o.Force && o.DryRun:
return fmt.Errorf("%w: %s", errInvalidFlagCombination, "cannot specify both --force and --dry-run")
case o.Silent && o.DryRun:
Expand Down Expand Up @@ -165,7 +166,7 @@ func (o *importOptions) getInput(cmd *cobra.Command) ([]byte, error) {
fmt.Fprintln(o.writer, "reading secrets from STDIN")

if len(out) == 0 {
return nil, fmt.Errorf("no input found, perhaps the piped command failed or specified file is empty")
return nil, errors.New("no input found, perhaps the piped command failed or specified file is empty")
}

return out, nil
Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"io"
"os"
Expand All @@ -17,6 +18,7 @@ import (
)

// NewRootCmd vkv root command.
//
//nolint:cyclop
func NewRootCmd(v string, writer io.Writer) *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -56,7 +58,7 @@ func NewRootCmd(v string, writer io.Writer) *cobra.Command {
}
}
default:
return fmt.Errorf("invalid value for VKV_MODE")
return errors.New("invalid value for VKV_MODE")
}

return cmd.Help()
Expand Down
4 changes: 2 additions & 2 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"bytes"
"fmt"
"io"
"log"
"path"
Expand Down Expand Up @@ -40,6 +39,7 @@ func defaultServerOptions() *serverOptions {
}

// NewServerCmd export subcommand.
//
//nolint:lll
func NewServerCmd(writer io.Writer, vaultClient *vault.Vault) *cobra.Command {
var err error
Expand Down Expand Up @@ -156,7 +156,7 @@ func (o *serverOptions) serve() error {
c.Data(200, "text/plain", o.readSecrets())
})

return r.Run(fmt.Sprintf(":%s", o.Port))
return r.Run(o.Port)
}

func (o *serverOptions) readSecrets() []byte {
Expand Down
3 changes: 2 additions & 1 deletion cmd/snapshot/snapshot_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (o *snapshotRestoreOptions) parseEnvs() error {
return nil
}

//nolint: cyclop
// nolint: cyclop
func (o *snapshotRestoreOptions) restoreSecrets(v *vault.Vault, source string) error {
return filepath.Walk(source, func(p string, info os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -119,6 +119,7 @@ func (o *snapshotRestoreOptions) restoreSecrets(v *vault.Vault, source string) e

// create engine
v.Client.SetNamespace(ns)

if err := v.EnableKV2EngineErrorIfNotForced(true, engine); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/snapshot/snapshot_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func newSnapshotSaveCmd(writer io.Writer, vaultClient *vault.Vault) *cobra.Comma
return cmd
}

//nolint: cyclop
// nolint: cyclop
func (o *snapshotSaveOptions) backupKVEngines(v *vault.Vault, engines map[string][]string) error {
for _, ns := range utils.SortMapKeys(utils.ToMapStringInterface(engines)) {
nsDir := path.Join(o.Destination, ns)
Expand Down
2 changes: 1 addition & 1 deletion pkg/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Run runs the given command and returns the output.
//nolint: gosec
// nolint: gosec
func Run(cmd []string) ([]byte, error) {
var stdout, stderr bytes.Buffer

Expand Down
7 changes: 4 additions & 3 deletions pkg/printer/engine/engine_printer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package engine

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -29,7 +30,7 @@ var (
defaultWriter = os.Stdout

// ErrInvalidFormat invalid output format.
ErrInvalidFormat = fmt.Errorf("invalid format (valid options: base, yaml, json, export, markdown)")
ErrInvalidFormat = errors.New("invalid format (valid options: base, yaml, json, export, markdown)")
)

// Option list of available options for modifying the output.
Expand Down Expand Up @@ -85,12 +86,12 @@ func NewPrinter(opts ...Option) *Printer {
}

// Out prints out engines.
//nolint: cyclop
// nolint: cyclop
func (p *Printer) Out(engines map[string][]string) error {
engineList := p.buildEngineList(engines)

if len(engineList) == 0 {
return fmt.Errorf("no engines found")
return errors.New("no engines found")
}

if p.Regex != "" {
Expand Down
7 changes: 4 additions & 3 deletions pkg/printer/namespace/namespace_printer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package namespace

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -29,7 +30,7 @@ var (
defaultWriter = os.Stdout

// ErrInvalidFormat invalid output format.
ErrInvalidFormat = fmt.Errorf("invalid format (valid options: base, yaml, json, export, markdown)")
ErrInvalidFormat = errors.New("invalid format (valid options: base, yaml, json, export, markdown)")
)

// Option list of available options for modifying the output.
Expand Down Expand Up @@ -77,12 +78,12 @@ func NewPrinter(opts ...Option) *Printer {
}

// Out prits out namespaces in various formats.
//nolint: cyclop
// nolint: cyclop
func (p *Printer) Out(ns map[string][]string) error {
nsList := p.buildNamespaceList(ns)

if len(ns) == 0 {
return fmt.Errorf("no namespaces found")
return errors.New("no namespaces found")
}

if p.Regex != "" {
Expand Down
3 changes: 2 additions & 1 deletion pkg/printer/secret/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (p *Printer) printMarkdownTable(enginePath string, secrets map[string]inter
return nil
}

//nolint: gocognit, nestif, cyclop
// nolint: gocognit, nestif, cyclop
func (p *Printer) buildMarkdownTable(enginePath string, secrets map[string]interface{}) ([]string, [][]string) {
data := [][]string{}
headers := []string{}
Expand Down Expand Up @@ -83,6 +83,7 @@ func (p *Printer) buildMarkdownTable(enginePath string, secrets map[string]inter
if p.showVersion {
d = append(d, "")
}

if p.showMetadata {
d = append(d, "")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/printer/secret/secret_printer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package secret

import (
"fmt"
"errors"
"io"
"log"
"os"
Expand Down Expand Up @@ -46,7 +46,7 @@ var (
defaultWriter = os.Stdout

// ErrInvalidFormat invalid output format.
ErrInvalidFormat = fmt.Errorf("invalid format (valid options: base, yaml, json, export, markdown)")
ErrInvalidFormat = errors.New("invalid format (valid options: base, yaml, json, export, markdown)")
)

// Option list of available options for modifying the output.
Expand Down Expand Up @@ -180,7 +180,7 @@ func (p *Printer) WithOption(opt Option) {
}

// Out prints out the secrets according all configured options.
//nolint: cyclop
// nolint: cyclop
func (p *Printer) Out(enginePath string, secrets map[string]interface{}) error {
for k, v := range secrets {
if !p.showValues {
Expand Down
7 changes: 3 additions & 4 deletions pkg/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package testutils

import (
"context"
"fmt"
"os"
"time"

Expand All @@ -13,7 +12,7 @@ import (

var (
vaultVersion = "latest"
image = fmt.Sprintf("hashicorp/vault:%s", vaultVersion)
image = "hashicorp/vault:" + vaultVersion
envs = map[string]string{}
token = "root"
)
Expand All @@ -37,7 +36,7 @@ func StartTestContainer() (*TestContainer, error) {
// use OSS image per default, if license is available use enterprise
if license, ok := os.LookupEnv("VAULT_LICENSE"); ok {
envs["VAULT_LICENSE"] = license
image = fmt.Sprintf("hashicorp/vault-enterprise:%s", vaultVersion)
image = "hashicorp/vault-enterprise" + vaultVersion
}

req := testcontainers.ContainerRequest{
Expand Down Expand Up @@ -70,7 +69,7 @@ func StartTestContainer() (*TestContainer, error) {

return &TestContainer{
Container: c, ctx: ctx,
URI: fmt.Sprintf("http://127.0.0.1:%s", mappedPort.Port()),
URI: "http://127.0.0.1:" + mappedPort.Port(),
Token: token,
}, nil
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/vault/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vault

import (
"errors"
"fmt"
"os"
"strings"
Expand All @@ -19,7 +20,7 @@ func NewDefaultClient() (*Vault, error) {
// error if no VAULT_ADDR exported
_, ok := os.LookupEnv("VAULT_ADDR")
if !ok {
return nil, fmt.Errorf("VAULT_ADDR required but not set")
return nil, errors.New("VAULT_ADDR required but not set")
}

// get vault token
Expand All @@ -40,7 +41,7 @@ func NewDefaultClient() (*Vault, error) {

// if toke is still empty, error
if vaultToken == "" {
return nil, fmt.Errorf("VKV_LOGIN_COMMAND or VAULT_TOKEN required but not set")
return nil, errors.New("VKV_LOGIN_COMMAND or VAULT_TOKEN required but not set")
}

// read all other vault env vars
Expand Down
2 changes: 1 addition & 1 deletion pkg/vault/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (v *Vault) ListKVSecretEngines(ns string) ([]string, error) {
return nil, fmt.Errorf("cannot get type of engine: %s", k)
}

if fmt.Sprintf("%v", t) == "kv" {
if fmt.Sprintf("%v", t) == "kv" || fmt.Sprintf("%v", t) == "generic" {
engineList = append(engineList, k)
}
}
Expand Down
Loading

0 comments on commit 1082212

Please sign in to comment.