2015-03-11 18:33:20 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-03-11 18:43:49 +00:00
|
|
|
"bytes"
|
2023-02-06 14:41:56 +00:00
|
|
|
"encoding/json"
|
2015-03-11 18:43:49 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-04-04 02:35:45 +00:00
|
|
|
"io/ioutil"
|
2015-03-11 18:33:20 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Response is a raw response that wraps an HTTP response.
|
|
|
|
type Response struct {
|
|
|
|
*http.Response
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeJSON will decode the response body to a JSON structure. This
|
|
|
|
// will consume the response body, but will not close it. Close must
|
|
|
|
// still be called.
|
|
|
|
func (r *Response) DecodeJSON(out interface{}) error {
|
2023-02-06 14:41:56 +00:00
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
dec.UseNumber()
|
|
|
|
return dec.Decode(out)
|
2015-03-11 18:33:20 +00:00
|
|
|
}
|
2015-03-11 18:43:49 +00:00
|
|
|
|
|
|
|
// Error returns an error response if there is one. If there is an error,
|
|
|
|
// this will fully consume the response body, but will not close it. The
|
|
|
|
// body must still be closed manually.
|
|
|
|
func (r *Response) Error() error {
|
2017-06-12 22:31:36 +00:00
|
|
|
// 200 to 399 are okay status codes. 429 is the code for health status of
|
2020-06-26 21:13:16 +00:00
|
|
|
// standby nodes, otherwise, 429 is treated as quota limit reached.
|
|
|
|
if (r.StatusCode >= 200 && r.StatusCode < 400) || (r.StatusCode == 429 && r.Request.URL.Path == "/v1/sys/health") {
|
2015-03-11 18:43:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have an error. Let's copy the body into our own buffer first,
|
|
|
|
// so that if we can't decode JSON, we can at least copy it raw.
|
2018-04-04 02:35:45 +00:00
|
|
|
bodyBuf := &bytes.Buffer{}
|
|
|
|
if _, err := io.Copy(bodyBuf, r.Body); err != nil {
|
2015-03-11 18:43:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-04 02:35:45 +00:00
|
|
|
r.Body.Close()
|
|
|
|
r.Body = ioutil.NopCloser(bodyBuf)
|
2023-02-06 14:41:56 +00:00
|
|
|
ns := r.Header.Get(NamespaceHeaderName)
|
2018-04-04 02:35:45 +00:00
|
|
|
|
2019-06-26 18:35:08 +00:00
|
|
|
// Build up the error object
|
|
|
|
respErr := &ResponseError{
|
2021-07-30 16:32:05 +00:00
|
|
|
HTTPMethod: r.Request.Method,
|
|
|
|
URL: r.Request.URL.String(),
|
|
|
|
StatusCode: r.StatusCode,
|
|
|
|
NamespacePath: ns,
|
2019-06-26 18:35:08 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 18:43:49 +00:00
|
|
|
// Decode the error response if we can. Note that we wrap the bodyBuf
|
|
|
|
// in a bytes.Reader here so that the JSON decoder doesn't move the
|
|
|
|
// read pointer for the original buffer.
|
|
|
|
var resp ErrorResponse
|
2023-02-06 14:41:56 +00:00
|
|
|
dec := json.NewDecoder(bytes.NewReader(bodyBuf.Bytes()))
|
|
|
|
dec.UseNumber()
|
|
|
|
if err := dec.Decode(&resp); err != nil {
|
2019-06-26 18:35:08 +00:00
|
|
|
// Store the fact that we couldn't decode the errors
|
|
|
|
respErr.RawError = true
|
|
|
|
respErr.Errors = []string{bodyBuf.String()}
|
|
|
|
} else {
|
|
|
|
// Store the decoded errors
|
|
|
|
respErr.Errors = resp.Errors
|
2015-03-11 18:43:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 18:35:08 +00:00
|
|
|
return respErr
|
2015-03-11 18:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorResponse is the raw structure of errors when they're returned by the
|
|
|
|
// HTTP API.
|
|
|
|
type ErrorResponse struct {
|
|
|
|
Errors []string
|
|
|
|
}
|
2019-06-26 18:35:08 +00:00
|
|
|
|
|
|
|
// ResponseError is the error returned when Vault responds with an error or
|
|
|
|
// non-success HTTP status code. If a request to Vault fails because of a
|
|
|
|
// network error a different error message will be returned. ResponseError gives
|
|
|
|
// access to the underlying errors and status code.
|
|
|
|
type ResponseError struct {
|
|
|
|
// HTTPMethod is the HTTP method for the request (PUT, GET, etc).
|
|
|
|
HTTPMethod string
|
|
|
|
|
|
|
|
// URL is the URL of the request.
|
|
|
|
URL string
|
|
|
|
|
|
|
|
// StatusCode is the HTTP status code.
|
|
|
|
StatusCode int
|
|
|
|
|
|
|
|
// RawError marks that the underlying error messages returned by Vault were
|
|
|
|
// not parsable. The Errors slice will contain the raw response body as the
|
|
|
|
// first and only error string if this value is set to true.
|
|
|
|
RawError bool
|
|
|
|
|
|
|
|
// Errors are the underlying errors returned by Vault.
|
|
|
|
Errors []string
|
2021-07-30 16:32:05 +00:00
|
|
|
|
|
|
|
// Namespace path to be reported to the client if it is set to anything other
|
|
|
|
// than root
|
|
|
|
NamespacePath string
|
2019-06-26 18:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns a human-readable error string for the response error.
|
|
|
|
func (r *ResponseError) Error() string {
|
|
|
|
errString := "Errors"
|
|
|
|
if r.RawError {
|
|
|
|
errString = "Raw Message"
|
|
|
|
}
|
|
|
|
|
2021-07-30 16:32:05 +00:00
|
|
|
var ns string
|
|
|
|
if r.NamespacePath != "" && r.NamespacePath != "root/" {
|
|
|
|
ns = "Namespace: " + r.NamespacePath + "\n"
|
|
|
|
}
|
|
|
|
|
2019-06-26 18:35:08 +00:00
|
|
|
var errBody bytes.Buffer
|
|
|
|
errBody.WriteString(fmt.Sprintf(
|
|
|
|
"Error making API request.\n\n"+
|
2021-07-30 16:32:05 +00:00
|
|
|
ns+
|
2019-06-26 18:35:08 +00:00
|
|
|
"URL: %s %s\n"+
|
|
|
|
"Code: %d. %s:\n\n",
|
|
|
|
r.HTTPMethod, r.URL, r.StatusCode, errString))
|
|
|
|
|
|
|
|
if r.RawError && len(r.Errors) == 1 {
|
|
|
|
errBody.WriteString(r.Errors[0])
|
|
|
|
} else {
|
|
|
|
for _, err := range r.Errors {
|
|
|
|
errBody.WriteString(fmt.Sprintf("* %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errBody.String()
|
|
|
|
}
|