forked from hashicorp/vault-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_requests.go
438 lines (370 loc) · 12 KB
/
client_requests.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package vault
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/hashicorp/go-retryablehttp"
)
// Read attempts to read the value stored at the given Vault path.
func (c *Client) Read(ctx context.Context, path string, options ...RequestOption) (*Response[map[string]interface{}], error) {
requestModifiers, err := requestOptionsToRequestModifiers(options)
if err != nil {
return nil, err
}
return sendRequestParseResponse[map[string]interface{}](
ctx,
c,
http.MethodGet,
v1Path(path),
nil, // request body
requestModifiers.additionalQueryParameters,
requestModifiers,
)
}
// ReadRaw attempts to read the value stored at the given Vault path and returns
// a raw *http.Response. Compared with `Read`, this function:
// - does not parse the response
// - does not check the response for errors
// - does not apply the client-level request timeout
func (c *Client) ReadRaw(ctx context.Context, path string, options ...RequestOption) (*http.Response, error) {
requestModifiers, err := requestOptionsToRequestModifiers(options)
if err != nil {
return nil, err
}
return sendRequestReturnRawResponse(
ctx,
c,
http.MethodGet,
v1Path(path),
nil, // request body
requestModifiers.additionalQueryParameters,
requestModifiers,
)
}
// Write attempts to write the given map to the given Vault path.
func (c *Client) Write(ctx context.Context, path string, body map[string]interface{}, options ...RequestOption) (*Response[map[string]interface{}], error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(body); err != nil {
return nil, fmt.Errorf("could not encode request body: %w", err)
}
return c.WriteFromReader(ctx, path, &buf, options...)
}
// WriteFromBytes attempts to write the given byte slice to the given Vault path.
func (c *Client) WriteFromBytes(ctx context.Context, path string, body []byte, options ...RequestOption) (*Response[map[string]interface{}], error) {
return c.WriteFromReader(ctx, path, bytes.NewReader(body), options...)
}
// WriteFromReader attempts to write the given io.Reader data to the given Vault path.
func (c *Client) WriteFromReader(ctx context.Context, path string, body io.Reader, options ...RequestOption) (*Response[map[string]interface{}], error) {
requestModifiers, err := requestOptionsToRequestModifiers(options)
if err != nil {
return nil, err
}
return sendRequestParseResponse[map[string]interface{}](
ctx,
c,
http.MethodPost,
v1Path(path),
body,
requestModifiers.additionalQueryParameters,
requestModifiers,
)
}
// List attempts to list the keys stored at the given Vault path.
func (c *Client) List(ctx context.Context, path string, options ...RequestOption) (*Response[map[string]interface{}], error) {
requestModifiers, err := requestOptionsToRequestModifiers(options)
if err != nil {
return nil, err
}
requestQueryParameters := requestModifiers.additionalQueryParametersOrDefault()
requestQueryParameters.Add("list", "true")
return sendRequestParseResponse[map[string]interface{}](
ctx,
c,
http.MethodGet,
v1Path(path),
nil, // request body
requestQueryParameters,
requestModifiers,
)
}
// Delete attempts to delete the value stored at the given Vault path.
func (c *Client) Delete(ctx context.Context, path string, options ...RequestOption) (*Response[map[string]interface{}], error) {
requestModifiers, err := requestOptionsToRequestModifiers(options)
if err != nil {
return nil, err
}
return sendRequestParseResponse[map[string]interface{}](
ctx,
c,
http.MethodDelete,
v1Path(path),
nil, // request body
requestModifiers.additionalQueryParameters,
requestModifiers,
)
}
// sendStructuredRequestParseResponse constructs a structured request, sends it, and parses the response
func sendStructuredRequestParseResponse[ResponseT any](
ctx context.Context,
client *Client,
method string,
path string,
body any,
parameters url.Values,
requestModifiers requestModifiers,
) (*Response[ResponseT], error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(body); err != nil {
return nil, fmt.Errorf("could not encode request body: %w", err)
}
return sendRequestParseResponse[ResponseT](
ctx,
client,
method,
path,
&buf,
parameters,
requestModifiers,
)
}
// sendRequestParseResponse constructs a request, sends it, and parses the response
func sendRequestParseResponse[ResponseT any](
ctx context.Context,
client *Client,
method string,
path string,
body io.Reader,
parameters url.Values,
requestModifiers requestModifiers,
) (*Response[ResponseT], error) {
// apply the client-level request timeout, if set
if client.configuration.RequestTimeout > 0 {
var cancelContextFunc context.CancelFunc
ctx, cancelContextFunc = context.WithTimeout(ctx, client.configuration.RequestTimeout)
defer cancelContextFunc()
}
// clone the client-level request modifiers to prevent race conditions
modifiers := client.cloneClientRequestModifiers()
// merge in the request-level request modifiers
mergeRequestModifiers(&modifiers, &requestModifiers)
req, err := client.newRequest(ctx, method, path, body, parameters, modifiers.headers)
if err != nil {
return nil, err
}
resp, err := client.send(ctx, req, true, modifiers.requestCallbacks, modifiers.responseCallbacks)
if err != nil || resp == nil {
return nil, err
}
defer resp.Body.Close()
if err := isResponseError(req, resp); err != nil {
return nil, err
}
return parseResponse[ResponseT](resp.Body)
}
// sendRequestReturnRawResponse constructs a request, sends it and returns a raw
// *http.Response. Compared with sendRequestParseResponse, this function:
// - does not parse the response
// - does not check the response for errors
// - does not apply the client-level request timeout
func sendRequestReturnRawResponse(
ctx context.Context,
client *Client,
method string,
path string,
body io.Reader,
parameters url.Values,
requestModifiers requestModifiers,
) (*http.Response, error) {
// clone the client-level request modifiers to prevent race conditions
modifiers := client.cloneClientRequestModifiers()
// merge in the request-level request modifiers
mergeRequestModifiers(&modifiers, &requestModifiers)
req, err := client.newRequest(ctx, method, path, body, parameters, modifiers.headers)
if err != nil {
return nil, err
}
return client.send(ctx, req, true, modifiers.requestCallbacks, modifiers.responseCallbacks)
}
// newRequest constructs a new request with Vault-specific headers
func (c *Client) newRequest(
ctx context.Context,
method string,
path string,
body io.Reader,
parameters url.Values,
headers requestHeaders,
) (*http.Request, error) {
// concatenate the base address with the given path
url, err := c.parsedBaseAddress.Parse(path)
if err != nil {
return nil, fmt.Errorf("could not join %q with the base address: %w", path, err)
}
// add query parameters (if any)
if len(parameters) != 0 {
url.RawQuery = parameters.Encode()
}
req, err := http.NewRequestWithContext(ctx, method, url.String(), body)
if err != nil {
return nil, fmt.Errorf("could not create '%s %s' request: %w", method, url.String(), err)
}
// populate request headers
if headers.customHeaders != nil {
req.Header = headers.customHeaders
}
if headers.userAgent != "" {
req.Header.Add("User-Agent", headers.userAgent)
}
if headers.token != "" {
req.Header.Set("X-Vault-Token", headers.token)
}
if headers.namespace != "" {
req.Header.Set("X-Vault-Namespace", headers.namespace)
}
for _, credentials := range headers.mfaCredentials {
req.Header.Add("X-Vault-MFA", credentials)
}
if headers.responseWrappingTTL != 0 {
req.Header.Set("X-Vault-Wrap-TTL", headers.responseWrappingTTL.String())
}
switch headers.replicationForwardingMode {
// unconditional forwarding (see 'ReplicationForwardAlways' docs)
case ReplicationForwardAlways:
req.Header.Set("X-Vault-Forward", "active-node")
// conditional formwarding (see 'ReplicationForwardInconsistent' docs)
case ReplicationForwardInconsistent:
req.Header.Set("X-Vault-Inconsistent", "forward-active-node")
}
return req, nil
}
// send sends the given request to Vault, handling retries, redirects, and rate limiting
func (c *Client) send(
ctx context.Context,
req *http.Request,
retry bool,
requestCallbacks []RequestCallback,
responseCallbacks []ResponseCallback,
) (*http.Response, error) {
// block on the rate limiter, if set
if c.configuration.RateLimiter != nil {
c.configuration.RateLimiter.Wait(ctx)
}
if c.configuration.EnforceReadYourWritesConsistency {
c.replicationStates.requireReplicationStates(req)
}
// invoke request callbacks
for _, callback := range requestCallbacks {
callback(req)
}
var (
resp *http.Response
err error
)
// allow at most one redirect to prevent redirect loops
redirectCount := 1
for {
resp, err = c.do(req, retry)
if err != nil {
return resp, err
}
redirect, err := c.handleRedirect(req, resp, &redirectCount)
if err != nil {
return nil, err
}
if !redirect {
break
}
}
// invoke response callbacks
for _, callback := range responseCallbacks {
callback(req, resp)
}
if c.configuration.EnforceReadYourWritesConsistency && resp != nil {
c.replicationStates.recordReplicationState(resp)
}
return resp, nil
}
// do is a helper function that wraps http.client.Do / retryablehttp.client.Do
func (c *Client) do(req *http.Request, retry bool) (*http.Response, error) {
// In the vast majority of cases, the retryablehttp client will be used.
// However, the retryablehttp client reads the entire request's body into
// an internal byte array, which could cause particularly large requests
// (e.g. raft snapshot requests) to run out of memory. For such requests,
// a regular http client is used instead.
if !retry {
return c.client.Do(req)
}
retryableReq, err := retryablehttp.FromRequest(req)
if err != nil {
return nil, fmt.Errorf("could not form a retryable request: %w", err)
}
return c.clientWithRetries.Do(retryableReq)
}
// handleRedirect checks the given response for a redirect status & modifies
// the request accordingly if the redirect is needed
func (c *Client) handleRedirect(req *http.Request, resp *http.Response, redirectCount *int) (bool, *RedirectError) {
switch resp.StatusCode {
case
http.StatusMovedPermanently, // 301
http.StatusFound, // 302
http.StatusTemporaryRedirect: // 307
default:
return false, nil
}
// a helper function to form a redirect error
redirectError := func(redirectTo *url.URL, message string, args ...any) *RedirectError {
var redirectLocation string
if redirectTo != nil {
redirectLocation = redirectTo.String()
}
return &RedirectError{
StatusCode: resp.StatusCode,
Message: fmt.Sprintf(message, args...),
RedirectLocation: redirectLocation,
OriginalRequest: req,
}
}
redirectTo, err := resp.Location()
if err != nil {
return false, redirectError(nil, "could not read the redirect location: %s", err.Error())
}
if c.configuration.DisableRedirects {
return false, redirectError(redirectTo, "the redirects are disabled")
}
if *redirectCount <= 0 {
return false, redirectError(redirectTo, "at most one redirect is allowed")
}
*redirectCount--
if req.URL.Scheme == "https" && redirectTo.Scheme != "https" {
return false, redirectError(redirectTo, "the redirect would cause a protocol downgrade")
}
// restore the original request body (if any) since it had been consumed by client.Do
if req.GetBody != nil {
b, err := req.GetBody()
if err != nil {
return false, redirectError(redirectTo, "could not restore the request body: %s", err.Error())
}
req.Body = b
}
req.URL = redirectTo
return true, nil
}
// v1Path returns the path string prefixed with /v1/ if needed
func v1Path(path string) string {
switch {
case strings.HasPrefix(path, "/v1/"):
return path
case strings.HasPrefix(path, "v1/"):
return "/" + path
case strings.HasPrefix(path, "/"):
return "/v1" + path
default:
return "/v1/" + path
}
}