2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-03-09 21:38:50 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2018-05-10 00:44:53 +03:00
|
|
|
"io/ioutil"
|
2016-07-12 00:37:46 +03:00
|
|
|
"net/http"
|
2015-03-09 21:38:50 +03:00
|
|
|
"net/url"
|
2018-05-10 00:44:53 +03:00
|
|
|
|
|
|
|
retryablehttp "github.com/hashicorp/go-retryablehttp"
|
2015-03-09 21:38:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Request is a raw request configuration structure used to initiate
|
|
|
|
// API requests to the Vault server.
|
|
|
|
type Request struct {
|
2017-10-23 23:52:56 +03:00
|
|
|
Method string
|
|
|
|
URL *url.URL
|
2020-03-11 16:22:58 +03:00
|
|
|
Host string
|
2017-10-23 23:52:56 +03:00
|
|
|
Params url.Values
|
|
|
|
Headers http.Header
|
|
|
|
ClientToken string
|
|
|
|
MFAHeaderVals []string
|
|
|
|
WrapTTL string
|
|
|
|
Obj interface{}
|
2018-06-01 16:12:43 +03:00
|
|
|
|
|
|
|
// When possible, use BodyBytes as it is more efficient due to how the
|
|
|
|
// retry logic works
|
|
|
|
BodyBytes []byte
|
|
|
|
|
|
|
|
// Fallback
|
|
|
|
Body io.Reader
|
|
|
|
BodySize int64
|
2017-10-23 23:52:56 +03:00
|
|
|
|
|
|
|
// Whether to request overriding soft-mandatory Sentinel policies (RGPs and
|
|
|
|
// EGPs). If set, the override flag will take effect for all policies
|
|
|
|
// evaluated during the request.
|
|
|
|
PolicyOverride bool
|
2015-03-09 21:38:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetJSONBody is used to set a request body that is a JSON-encoded value.
|
|
|
|
func (r *Request) SetJSONBody(val interface{}) error {
|
2018-06-01 16:12:43 +03:00
|
|
|
buf, err := json.Marshal(val)
|
|
|
|
if err != nil {
|
2015-03-09 21:38:50 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-20 20:44:51 +03:00
|
|
|
r.Obj = val
|
2018-06-01 16:12:43 +03:00
|
|
|
r.BodyBytes = buf
|
2015-03-09 21:38:50 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-20 20:44:51 +03:00
|
|
|
// ResetJSONBody is used to reset the body for a redirect
|
|
|
|
func (r *Request) ResetJSONBody() error {
|
2018-06-01 16:12:43 +03:00
|
|
|
if r.BodyBytes == nil {
|
2015-04-20 20:44:51 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return r.SetJSONBody(r.Obj)
|
|
|
|
}
|
|
|
|
|
2018-06-01 16:12:43 +03:00
|
|
|
// DEPRECATED: ToHTTP turns this request into a valid *http.Request for use
|
|
|
|
// with the net/http package.
|
2016-07-12 00:37:46 +03:00
|
|
|
func (r *Request) ToHTTP() (*http.Request, error) {
|
2018-06-01 16:12:43 +03:00
|
|
|
req, err := r.toRetryableHTTP()
|
2018-05-10 00:44:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-01 16:12:43 +03:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case r.BodyBytes == nil && r.Body == nil:
|
|
|
|
// No body
|
|
|
|
|
|
|
|
case r.BodyBytes != nil:
|
|
|
|
req.Request.Body = ioutil.NopCloser(bytes.NewReader(r.BodyBytes))
|
|
|
|
|
|
|
|
default:
|
|
|
|
if c, ok := r.Body.(io.ReadCloser); ok {
|
|
|
|
req.Request.Body = c
|
|
|
|
} else {
|
|
|
|
req.Request.Body = ioutil.NopCloser(r.Body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 00:44:53 +03:00
|
|
|
return req.Request, nil
|
|
|
|
}
|
|
|
|
|
2018-06-01 16:12:43 +03:00
|
|
|
func (r *Request) toRetryableHTTP() (*retryablehttp.Request, error) {
|
2015-03-09 21:38:50 +03:00
|
|
|
// Encode the query parameters
|
|
|
|
r.URL.RawQuery = r.Params.Encode()
|
|
|
|
|
2018-05-10 00:44:53 +03:00
|
|
|
// Create the HTTP request, defaulting to retryable
|
|
|
|
var req *retryablehttp.Request
|
|
|
|
|
2018-06-01 16:12:43 +03:00
|
|
|
var err error
|
|
|
|
var body interface{}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case r.BodyBytes == nil && r.Body == nil:
|
|
|
|
// No body
|
|
|
|
|
|
|
|
case r.BodyBytes != nil:
|
|
|
|
// Use bytes, it's more efficient
|
|
|
|
body = r.BodyBytes
|
|
|
|
|
|
|
|
default:
|
|
|
|
body = r.Body
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err = retryablehttp.NewRequest(r.Method, r.URL.RequestURI(), body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-03-09 21:38:50 +03:00
|
|
|
}
|
|
|
|
|
2017-03-10 15:19:23 +03:00
|
|
|
req.URL.User = r.URL.User
|
2015-03-09 21:38:50 +03:00
|
|
|
req.URL.Scheme = r.URL.Scheme
|
|
|
|
req.URL.Host = r.URL.Host
|
2020-03-11 16:22:58 +03:00
|
|
|
req.Host = r.Host
|
2015-03-09 21:38:50 +03:00
|
|
|
|
2017-06-20 01:20:44 +03:00
|
|
|
if r.Headers != nil {
|
|
|
|
for header, vals := range r.Headers {
|
|
|
|
for _, val := range vals {
|
|
|
|
req.Header.Add(header, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-22 03:36:19 +03:00
|
|
|
if len(r.ClientToken) != 0 {
|
2023-02-06 17:41:56 +03:00
|
|
|
req.Header.Set(AuthHeaderName, r.ClientToken)
|
2015-08-22 03:36:19 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 08:58:58 +03:00
|
|
|
if len(r.WrapTTL) != 0 {
|
|
|
|
req.Header.Set("X-Vault-Wrap-TTL", r.WrapTTL)
|
|
|
|
}
|
|
|
|
|
2017-10-23 23:52:56 +03:00
|
|
|
if len(r.MFAHeaderVals) != 0 {
|
|
|
|
for _, mfaHeaderVal := range r.MFAHeaderVals {
|
|
|
|
req.Header.Add("X-Vault-MFA", mfaHeaderVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.PolicyOverride {
|
|
|
|
req.Header.Set("X-Vault-Policy-Override", "true")
|
|
|
|
}
|
|
|
|
|
2015-03-09 21:38:50 +03:00
|
|
|
return req, nil
|
|
|
|
}
|