Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VAULT-1303 when a request to vault fails, show namespace if set #12196

Merged
merged 5 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ func (r *Response) Error() error {

r.Body.Close()
r.Body = ioutil.NopCloser(bodyBuf)
ns := r.Header.Get("X-Vault-Namespace")
hghaf099 marked this conversation as resolved.
Show resolved Hide resolved

// Build up the error object
respErr := &ResponseError{
HTTPMethod: r.Request.Method,
URL: r.Request.URL.String(),
StatusCode: r.StatusCode,
HTTPMethod: r.Request.Method,
URL: r.Request.URL.String(),
StatusCode: r.StatusCode,
NamespacePath: ns,
}

// Decode the error response if we can. Note that we wrap the bodyBuf
Expand Down Expand Up @@ -92,6 +94,10 @@ type ResponseError struct {

// Errors are the underlying errors returned by Vault.
Errors []string

// Namespace path to be reported to the client if it is set to anything other
// than root
NamespacePath string
}

// Error returns a human-readable error string for the response error.
Expand All @@ -101,9 +107,15 @@ func (r *ResponseError) Error() string {
errString = "Raw Message"
}

var ns string
if r.NamespacePath != "" && r.NamespacePath != "root/" {
ns = "Namespace: " + r.NamespacePath + "\n"
}

var errBody bytes.Buffer
errBody.WriteString(fmt.Sprintf(
"Error making API request.\n\n"+
ns+
"URL: %s %s\n"+
"Code: %d. %s:\n\n",
r.HTTPMethod, r.URL, r.StatusCode, errString))
Expand Down
3 changes: 3 additions & 0 deletions changelog/12196.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
core (enterprise): namespace header included in responses, Go client uses it when displaying error messages
```
6 changes: 6 additions & 0 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ func wrapGenericHandler(core *vault.Core, h http.Handler, props *vault.HandlerPr
return
}

// Setting the namespace in the header to be included in the error message
ns := r.Header.Get("X-Vault-Namespace")
if ns != "" {
w.Header().Set("X-Vault-Namespace", ns)
}

h.ServeHTTP(w, r)

cancelFunc()
Expand Down