-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (151 loc) · 3.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"fmt"
"os"
"path"
"strings"
"github.com/adrg/xdg"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/spinner"
"github.com/charmbracelet/log"
"github.com/charmbracelet/x/term"
"github.com/jon4hz/awoolt/config"
"github.com/jon4hz/awoolt/version"
"github.com/openbao/openbao/api/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var rootCmd = &cobra.Command{
Use: "awoolt",
Short: "interactively browse vault/openbao in the terminal.",
Version: version.Version,
Run: root,
}
var rootFlags struct {
engine string
path string
}
func must(err error) {
if err != nil {
log.Fatal("Error", "err", err)
}
}
func init() {
rootCmd.Flags().StringVarP(&rootFlags.engine, "engine", "e", "", "secret engine to use")
rootCmd.Flags().StringVarP(&rootFlags.path, "path", "p", "", "secret path")
must(viper.BindPFlags(rootCmd.Flags()))
rootCmd.AddCommand(versionCmd)
}
func root(cmd *cobra.Command, _ []string) {
config, err := config.Load("")
if err != nil {
log.Fatal("Failed to load config", "err", err)
}
if config.Engine == "" {
log.Fatal("No engine specified :(")
}
apiConfig := api.DefaultConfig()
client, err := api.NewClient(apiConfig)
if err != nil {
log.Fatal("Failed to create client", "err", err)
}
token, err := os.ReadFile(path.Join(xdg.Home, ".vault-token"))
if err != nil {
log.Fatal("Failed to read token. Login to vault first!", "err", err)
}
client.SetToken(string(token))
path := vaultPath{config.Engine}
if rootFlags.path != "" {
path.Add(strings.Split(rootFlags.path, "/")...)
}
for {
availableKeys, done, err := listSecretsSpinner(client, path)
if err != nil {
log.Fatal("Failed to list secrets", "err", err)
}
if done {
break
}
nextPathElement, err := selectNextPathElement(path.String(), availableKeys)
if err != nil {
return
}
path.Add(nextPathElement)
}
secret, err := client.KVv2(path.Engine()).Get(cmd.Context(), path.Path())
if err != nil {
log.Fatal("Failed to get secret", "err", err)
}
if secret == nil {
log.Fatal("Secret not found")
}
fmt.Printf("path: %s\n", path.Path())
printSecret(secret)
}
func listSecrets(client *api.Client, path vaultPath) ([]string, bool, error) {
secret, err := client.Logical().List(path.MetadataPath())
if err != nil {
return nil, false, err
}
if secret == nil {
return nil, true, nil
}
keys, ok := secret.Data["keys"].([]any)
if !ok {
log.Fatal("Failed to convert keys", "keys", secret.Data["keys"])
}
availableKeys := make([]string, len(keys))
for i, key := range keys {
availableKeys[i] = key.(string)
}
return availableKeys, false, nil
}
func listSecretsSpinner(client *api.Client, path vaultPath) (availableKeys []string, done bool, err error) {
serr := spinner.New().
Title("Fetching secrets...").
Action(func() {
availableKeys, done, err = listSecrets(client, path)
}).
Run()
if serr != nil {
err = serr
}
return
}
func selectNextPathElement(path string, options []string) (string, error) {
_, height, err := term.GetSize(os.Stdin.Fd())
if err != nil {
return "", err
}
var nextPath string
err = huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title(path).
Height(min(len(options)+2, height-2)).
Options(huh.NewOptions(options...)...).
Value(&nextPath),
),
).Run()
return nextPath, err
}
func printSecret(s *api.KVSecret) {
for k, v := range s.Data {
fmt.Printf("%s: %s\n", k, v)
}
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version info",
Run: func(_ *cobra.Command, _ []string) {
fmt.Printf("Version: %s\n", version.Version)
fmt.Printf("Commit: %s\n", version.Commit)
fmt.Printf("Date: %s\n", version.Date)
fmt.Printf("BuiltBy: %s\n", version.BuiltBy)
},
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}