-
Notifications
You must be signed in to change notification settings - Fork 282
/
vault_funcs.go
174 lines (163 loc) · 5.52 KB
/
vault_funcs.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
package credentiallibrariescmd
import (
"github.com/hashicorp/boundary/api/credentiallibraries"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/go-secure-stdlib/parseutil"
)
func init() {
extraVaultFlagsFunc = extraVaultFlagsFuncImpl
extraVaultActionsFlagsMapFunc = extraVaultActionsFlagsMapFuncImpl
extraVaultFlagsHandlingFunc = extraVaultFlagHandlingFuncImpl
}
const (
pathFlagName = "vault-path"
httpMethodFlagName = "vault-http-method"
httpRequestBodyFlagName = "vault-http-request-body"
credentialTypeFlagName = "credential-type"
credentialMappingFlagName = "credential-mapping-override"
)
type extraVaultCmdVars struct {
flagPath string
flagHttpMethod string
flagHttpRequestBody string
flagCredentialType string
flagCredentialMapping []base.CombinedSliceFlagValue
}
func extraVaultActionsFlagsMapFuncImpl() map[string][]string {
flags := map[string][]string{
"create": {
pathFlagName,
httpMethodFlagName,
httpRequestBodyFlagName,
credentialTypeFlagName,
credentialMappingFlagName,
},
"update": {
pathFlagName,
httpMethodFlagName,
httpRequestBodyFlagName,
credentialMappingFlagName,
},
}
return flags
}
func extraVaultFlagsFuncImpl(c *VaultCommand, set *base.FlagSets, _ *base.FlagSet) {
f := set.NewFlagSet("Vault Credential Library Options")
for _, name := range flagsVaultMap[c.Func] {
switch name {
case pathFlagName:
f.StringVar(&base.StringVar{
Name: pathFlagName,
Target: &c.flagPath,
Usage: "The path in vault to request credentials from.",
})
case httpMethodFlagName:
f.StringVar(&base.StringVar{
Name: httpMethodFlagName,
Target: &c.flagHttpMethod,
Usage: "The http method the library should use when communicating with vault.",
})
case httpRequestBodyFlagName:
f.StringVar(&base.StringVar{
Name: httpRequestBodyFlagName,
Target: &c.flagHttpRequestBody,
Usage: "The http request body the library uses to communicate with vault. This can be the value itself, refer to a file on disk (file://) from which the value will be read, or an env var (env://) from which the value will be read.",
})
case credentialTypeFlagName:
f.StringVar(&base.StringVar{
Name: credentialTypeFlagName,
Target: &c.flagCredentialType,
Usage: "The type of credential this library will issue, defaults to Unspecified.",
})
case credentialMappingFlagName:
f.CombinationSliceVar(&base.CombinationSliceVar{
Name: credentialMappingFlagName,
Target: &c.flagCredentialMapping,
KvSplit: true,
Usage: "The credential mapping override.",
})
}
}
}
func extraVaultFlagHandlingFuncImpl(c *VaultCommand, _ *base.FlagSets, opts *[]credentiallibraries.Option) bool {
switch c.flagPath {
case "":
default:
*opts = append(*opts, credentiallibraries.WithVaultCredentialLibraryPath(c.flagPath))
}
switch c.flagHttpMethod {
case "":
case "null":
*opts = append(*opts, credentiallibraries.DefaultVaultCredentialLibraryHttpMethod())
default:
*opts = append(*opts, credentiallibraries.WithVaultCredentialLibraryHttpMethod(c.flagHttpMethod))
}
switch c.flagHttpRequestBody {
case "":
case "null":
*opts = append(*opts, credentiallibraries.DefaultVaultCredentialLibraryHttpRequestBody())
default:
rb, _ := parseutil.ParsePath(c.flagHttpRequestBody)
*opts = append(*opts, credentiallibraries.WithVaultCredentialLibraryHttpRequestBody(rb))
}
switch c.flagCredentialType {
case "":
case "null":
*opts = append(*opts, credentiallibraries.DefaultCredentialType())
default:
*opts = append(*opts, credentiallibraries.WithCredentialType(c.flagCredentialType))
}
switch len(c.flagCredentialMapping) {
case 0:
case 1:
if len(c.flagCredentialMapping[0].Keys) == 0 && c.flagCredentialMapping[0].Value == "null" {
*opts = append(*opts, credentiallibraries.DefaultCredentialMappingOverrides())
break
}
fallthrough
default:
mappings := make(map[string]interface{}, len(c.flagCredentialMapping))
for _, mapping := range c.flagCredentialMapping {
switch {
case len(mapping.Keys) != 1 || mapping.Keys[0] == "" || mapping.Value == "":
// mapping override does not support key segments (e.g. 'x.y=z')
c.UI.Error("Credential mapping override must be in the format 'key=value', 'key=null' to clear field or 'null' to clear all.")
return false
case mapping.Value == "null":
// user provided 'key=null' indicating the field specific override should
// be cleared, set map value to nil
mappings[mapping.Keys[0]] = nil
default:
mappings[mapping.Keys[0]] = mapping.Value
}
}
*opts = append(*opts, credentiallibraries.WithCredentialMappingOverrides(mappings))
}
return true
}
func (c *VaultCommand) extraVaultHelpFunc(_ map[string]func() string) string {
var helpStr string
switch c.Func {
case "create":
helpStr = base.WrapForHelpText([]string{
"Usage: boundary credential-libraries create vault -credential-store-id [options] [args]",
"",
" Create a vault-type credential library. Example:",
"",
` $ boundary credential-libraries create vault -credential-store-id csvlt_1234567890 -vault-path "/some/path"`,
"",
"",
})
case "update":
helpStr = base.WrapForHelpText([]string{
"Usage: boundary credential-libraries update vault [options] [args]",
"",
" Update a vault-type credential library given its ID. Example:",
"",
` $ boundary credential-libraries update vault -id clvlt_1234567890 -name devops -description "For DevOps usage"`,
"",
"",
})
}
return helpStr + c.Flags().Help()
}