-
Notifications
You must be signed in to change notification settings - Fork 7
/
secret_printer.go
228 lines (189 loc) · 4.41 KB
/
secret_printer.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package secret
import (
"errors"
"io"
"log"
"os"
"github.com/FalcoSuessgott/vkv/pkg/fs"
"github.com/FalcoSuessgott/vkv/pkg/utils"
"github.com/FalcoSuessgott/vkv/pkg/vault"
"github.com/savioxavier/termlink"
)
// OutputFormat enum of valid output formats.
type OutputFormat int
const (
maskChar = "*"
// MaxValueLength maximum length of passwords.
MaxValueLength = 12
// Base prints the secrets in the default format.
Base OutputFormat = iota
// YAML prints the secrets in yaml format.
YAML
// JSON prints the secrets in json format.
JSON
// Export prints the secrets in export (export "key=value") format.
Export
// Markdown prints the secrets in markdowntable format.
Markdown
// Template renders a given template string or file.
Template
// Policy displays the current token policy capabilities for each path in a matrix.
Policy
)
var (
defaultWriter = os.Stdout
// ErrInvalidFormat invalid output format.
ErrInvalidFormat = errors.New("invalid format (valid options: base, yaml, json, export, markdown)")
)
// Option list of available options for modifying the output.
type Option func(*Printer)
// Printer struct that holds all options used for displaying the secrets.
type Printer struct {
format OutputFormat
writer io.Writer
onlyKeys bool
onlyPaths bool
showVersion bool
showValues bool
showMetadata bool
withHyperLinks bool
valueLength int
template string
vaultClient *vault.Vault
}
// CustomValueLength option for trimming down the output of secrets.
func CustomValueLength(length int) Option {
return func(p *Printer) {
p.valueLength = length
}
}
// OnlyKeys flag for only showing secrets keys.
func OnlyKeys(b bool) Option {
return func(p *Printer) {
if b {
p.onlyKeys = true
}
}
}
// WithHyperLinks for enabling hyperlinks.
func WithHyperLinks(b bool) Option {
return func(p *Printer) {
if b {
p.withHyperLinks = termlink.SupportsHyperlinks()
}
}
}
// OnlyPaths flag for only printing kv paths.
func OnlyPaths(b bool) Option {
return func(p *Printer) {
if b {
p.onlyPaths = true
}
}
}
// ToFormat sets the output format of the printer.
func ToFormat(format OutputFormat) Option {
return func(p *Printer) {
p.format = format
}
}
// WithWriter option for passing a custom io.Writer.
func WithWriter(w io.Writer) Option {
return func(p *Printer) {
p.writer = w
}
}
// ShowValues flag for unmasking secrets in output.
func ShowValues(b bool) Option {
return func(p *Printer) {
if b {
p.showValues = true
}
}
}
// ShowVersion flag for unmasking secrets in output.
func ShowVersion(b bool) Option {
return func(p *Printer) {
if b {
p.showVersion = true
}
}
}
// ShowMetadata flag for unmasking secrets in output.
func ShowMetadata(b bool) Option {
return func(p *Printer) {
if b {
p.showMetadata = true
}
}
}
// WithTemplate sets the template file.
func WithTemplate(str, path string) Option {
return func(p *Printer) {
if str != "" {
p.template = str
return
}
if path != "" {
str, err := fs.ReadFile(path)
if err != nil {
log.Fatalf("error reading %s: %s", path, err.Error())
}
p.template = string(str)
return
}
}
}
// WithVaultClient inject a vault client.
func WithVaultClient(v *vault.Vault) Option {
return func(p *Printer) {
p.vaultClient = v
}
}
// NewPrinter return a new printer struct.
func NewPrinter(opts ...Option) *Printer {
p := &Printer{
writer: defaultWriter,
valueLength: MaxValueLength,
}
for _, opt := range opts {
opt(p)
}
return p
}
func (p *Printer) WithOption(opt Option) {
opt(p)
}
// Out prints out the secrets according all configured options.
// nolint: cyclop
func (p *Printer) Out(enginePath string, secrets map[string]interface{}) error {
for k, v := range secrets {
if !p.showValues {
secrets[k] = p.maskValues(utils.ToMapStringInterface(v))
}
if p.onlyPaths {
secrets[k] = p.printOnlyPaths(utils.ToMapStringInterface(v))
}
if p.onlyKeys {
secrets[k] = p.printOnlykeys(utils.ToMapStringInterface(v))
}
}
switch p.format {
case YAML:
return p.printYAML(secrets)
case JSON:
return p.printJSON(secrets)
case Export:
return p.printExport(secrets)
case Markdown:
return p.printMarkdownTable(enginePath, secrets)
case Template:
return p.printTemplate(secrets)
case Base:
return p.printBase(enginePath, secrets)
case Policy:
return p.printPolicy(secrets)
default:
return ErrInvalidFormat
}
}