feat(vault-tui): implement KV client and service for managing secrets
- Added internal/vault/client.go for creating a Vault client with configuration settings. - Introduced internal/vault/errors.go to classify Vault API errors for better UI handling. - Created internal/vault/kv.go to manage KV secrets, including listing, reading, writing, and deleting operations. - Implemented internal/vault/mounts.go to list and describe secret engine mounts. - Developed internal/vault/service.go to provide a unified entry point for Vault operations. - Added internal/vault/kv_test.go for comprehensive testing of KV operations. - Introduced internal/ui/toast.go for transient notifications in the UI. - Added renovate.json for dependency management and updates.
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func writeCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "write",
|
||||
Usage: "create or update a secret (e.g. `vault-tui write secret/team/prod/db user=admin pass=hunter2`)",
|
||||
ArgsUsage: "<path> key=value [key=value ...]",
|
||||
Flags: []cli.Flag{
|
||||
&cli.IntFlag{Name: "cas", Usage: "KV v2 check-and-set version (default: current version, if require_cas is on)"},
|
||||
&cli.BoolFlag{Name: "force", Usage: "skip check-and-set even if require_cas is on"},
|
||||
},
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
a, err := appFrom(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args := cmd.Args().Slice()
|
||||
if len(args) < 2 {
|
||||
return fmt.Errorf("usage: vault-tui write <path> key=value [key=value ...]")
|
||||
}
|
||||
if _, err := a.EnsureLoggedIn(ctx, cmd.String("token")); err != nil {
|
||||
return err
|
||||
}
|
||||
svc := a.Service()
|
||||
if svc.ReadOnly {
|
||||
return fmt.Errorf("refusing to write: vault-tui is in read-only mode (pass --write to override)")
|
||||
}
|
||||
mount, rel, err := splitMountPath(ctx, svc, args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := parseKVArgs(args[1:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
iface := make(map[string]interface{}, len(data))
|
||||
for k, v := range data {
|
||||
iface[k] = v
|
||||
}
|
||||
|
||||
useCAS := a.Settings.RequireCAS && !cmd.Bool("force")
|
||||
cas := int(cmd.Int("cas"))
|
||||
if useCAS && !cmd.IsSet("cas") {
|
||||
current, err := svc.KV.Read(ctx, mount, rel, 0)
|
||||
if err == nil && current != nil {
|
||||
cas = current.Version
|
||||
}
|
||||
}
|
||||
|
||||
ack, err := svc.KV.Write(ctx, mount, rel, iface, useCAS, cas)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ack.Version > 0 {
|
||||
fmt.Printf("wrote %s (version %d)\n", args[0], ack.Version)
|
||||
} else {
|
||||
fmt.Printf("wrote %s\n", args[0])
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user