This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
path_config_connection.go
309 lines (270 loc) · 9.91 KB
/
path_config_connection.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package splunk
import (
"context"
"fmt"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/certutil"
"github.com/hashicorp/vault/sdk/logical"
)
// pathConfigConnection returns a configured framework.Path setup to
// operate on plugins.
func (b *backend) pathConfigConnection() *framework.Path {
return &framework.Path{
Pattern: fmt.Sprintf("config/%s", framework.GenericNameRegex("name")),
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeString,
Description: "Name of the Splunk connection.",
},
"username": {
Type: framework.TypeString,
Description: "Admin user with permission to create new accounts.",
},
"password": {
Type: framework.TypeString,
Description: "Admin password.",
},
"url": {
Type: framework.TypeString,
Description: "Splunk server URL.",
},
"is_standalone": {
Type: framework.TypeBool,
Description: `Whether this is a standalone or multi-node deployment. Default: false`,
Default: false,
},
"allowed_roles": {
Type: framework.TypeCommaStringSlice,
Description: trimIndent(`
Comma separated string or array of the role names allowed to get creds
from this Splunk connection. If empty, no roles are allowed. If "*", all
roles are allowed.`),
},
"verify": {
Type: framework.TypeBool,
Default: true,
Description: trimIndent(`
If true, the connection details are verified by actually connecting to
Splunk. Default: true`),
},
"insecure_tls": {
Type: framework.TypeBool,
Default: false,
Description: trimIndent(`
Whether to use TLS but skip verification; has no effect if a CA
certificate is provided. Default: false`),
},
"tls_min_version": {
Type: framework.TypeString,
Default: "tls12",
Description: trimIndent(`
Minimum TLS version to use. Accepted values are "tls10", "tls11" or
"tls12". Default: "tls12".`),
},
"pem_bundle": {
Type: framework.TypeString,
Description: trimIndent(`
PEM-format, concatenated unencrypted secret key and certificate, with
optional CA certificate.`),
},
"pem_json": {
Type: framework.TypeString,
Description: trimIndent(`
JSON containing a PEM-format, unencrypted secret key and certificate, with
optional CA certificate. The JSON output of a certificate issued with the
PKI backend can be directly passed into this parameter. If both this and
"pem_bundle" are specified, this will take precedence.`),
},
"root_ca": {
Type: framework.TypeString,
Description: `PEM-format, concatenated CA certificates.`,
},
"connect_timeout": {
Type: framework.TypeDurationSecond,
Default: "30s",
Description: `The connection timeout to use. Default: 30s.`,
},
},
ExistenceCheck: b.connectionExistenceCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.CreateOperation: b.connectionWriteHandler,
logical.UpdateOperation: b.connectionWriteHandler,
logical.ReadOperation: b.connectionReadHandler,
logical.DeleteOperation: b.connectionDeleteHandler,
},
HelpSynopsis: pathConfigConnectionHelpSyn,
HelpDescription: pathConfigConnectionHelpDesc,
}
}
func (b *backend) connectionExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
name := data.Get("name").(string)
return connectionConfigExists(ctx, req.Storage, name)
}
func (b *backend) connectionReadHandler(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
config, err := connectionConfigLoad(ctx, req.Storage, name)
if err != nil {
return nil, err
}
resp := &logical.Response{
Data: config.toResponseData(),
}
return resp, nil
}
func (b *backend) connectionDeleteHandler(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
config, err := connectionConfigLoad(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if err := req.Storage.Delete(ctx, fmt.Sprintf("config/%s", name)); err != nil {
return nil, fmt.Errorf("error reading connection configuration: %w", err)
}
// XXXX WAL
if err := b.clearConnection(config.ID); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) connectionWriteHandler(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
if name == "" {
return logical.ErrorResponse(respErrEmptyName), nil
}
config := &splunkConfig{}
if req.Operation != logical.CreateOperation {
var err error
config, err = connectionConfigLoad(ctx, req.Storage, name)
if err != nil {
return nil, fmt.Errorf("error reading connection configuration: %w", err)
}
}
if usernameRaw, ok := getValue(data, req.Operation, "username"); ok {
config.Username = usernameRaw.(string)
}
if config.Username == "" {
return logical.ErrorResponse("empty username"), nil
}
if passwordRaw, ok := getValue(data, req.Operation, "password"); ok {
config.Password = passwordRaw.(string)
}
if urlRaw, ok := getValue(data, req.Operation, "url"); ok {
config.URL = urlRaw.(string)
}
if config.URL == "" {
return logical.ErrorResponse("empty URL"), nil
}
if isStandalone, ok := getValue(data, req.Operation, "is_standalone"); ok {
config.IsStandalone = isStandalone.(bool)
}
if verifyRaw, ok := getValue(data, req.Operation, "verify"); ok {
config.Verify = verifyRaw.(bool)
}
if allowedRolesRaw, ok := getValue(data, req.Operation, "allowed_roles"); ok {
config.AllowedRoles = allowedRolesRaw.([]string)
}
if len(config.AllowedRoles) == 0 {
return logical.ErrorResponse("allowed_roles cannot be empty"), nil
}
// XXX go through all established leases if allowed_roles change?
if insecureTLSRaw, ok := getValue(data, req.Operation, "insecure_tls"); ok {
config.InsecureTLS = insecureTLSRaw.(bool)
}
pemBundle := data.Get("pem_bundle").(string)
pemJSON := data.Get("pem_json").(string)
rootCA := data.Get("root_ca").(string)
var certBundle *certutil.CertBundle
var parsedCertBundle *certutil.ParsedCertBundle
var err error
switch {
case len(pemJSON) != 0:
parsedCertBundle, err = certutil.ParsePKIJSON([]byte(pemJSON))
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("Could not parse given JSON; it must be in the format of the output of the PKI backend certificate issuing command: %s", err)), nil
}
certBundle, err = parsedCertBundle.ToCertBundle()
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("Error marshaling PEM information: %s", err)), nil
}
config.Certificate = certBundle.Certificate
config.PrivateKey = certBundle.PrivateKey
config.CAChain = certBundle.CAChain
case len(pemBundle) != 0:
parsedCertBundle, err = certutil.ParsePEMBundle(pemBundle)
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("Error parsing the given PEM information: %s", err)), nil
}
certBundle, err = parsedCertBundle.ToCertBundle()
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("Error marshaling PEM information: %s", err)), nil
}
config.Certificate = certBundle.Certificate
config.PrivateKey = certBundle.PrivateKey
config.CAChain = certBundle.CAChain
}
if config.CAChain == nil {
config.CAChain = []string{}
}
if len(rootCA) > 0 {
config.RootCA = []string{rootCA} // XXXX parse PEM
}
if config.RootCA == nil {
config.RootCA = []string{}
}
if connectTimeoutRaw, ok := getValue(data, req.Operation, "connect_timeout"); ok {
config.ConnectTimeout = time.Duration(connectTimeoutRaw.(int)) * time.Second
}
if err := config.store(ctx, req.Storage, name); err != nil {
return nil, fmt.Errorf("error writing connection configuration: %w", err)
}
// if config.Verify {
// config.verifyConnection(ctx, req.Storage, name)
// }
return nil, nil
}
func (b *backend) pathConnectionsList() *framework.Path {
return &framework.Path{
Pattern: "config/?$",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.connectionListHandler,
},
HelpSynopsis: pathConfigConnectionHelpSyn,
HelpDescription: pathConfigConnectionHelpDesc,
}
}
func (b *backend) connectionListHandler(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, "config/")
if err != nil {
return nil, err
}
return logical.ListResponse(entries), nil
}
const pathConfigConnectionHelpSyn = `
Configure connection details to a Splunk instance.
`
const pathConfigConnectionHelpDesc = `
See the documentation for config/name for a full list of accepted
connection details.
"username", "password" and "url" are self-explanatory, although the
given user must have admin access within Splunk. Note that since
this backend issues username/password credentials, Splunk must be
configured to allow local users for authentication.
TLS works as follows:
* If "insecure_tls" is set to true, the connection will not perform
verification of the server certificate
* If only "issuing_ca" is set in "pem_json", or the only certificate
in "pem_bundle" is a CA certificate, the given CA certificate will
be used for server certificate verification; otherwise the system CA
certificates will be used.
* If "certificate" and "private_key" are set in "pem_bundle" or
"pem_json", client auth will be turned on for the connection.
* If "root_ca" is set, the PEM-concatenated set of CA certificates
will be added, and used instead of the system CA certificates.
"pem_bundle" should be a PEM-concatenated bundle of a private key +
client certificate, an issuing CA certificate, or both. "pem_json"
should contain the same information; for convenience, the JSON format
is the same as that output by the issue command from the PKI backend.
When configuring the connection information, the backend will verify
its validity.
`