s.CheckObjectType(c, "/arvados/v1/"+url, arvadostest.AdminToken, skippedFields)
}
}
+
+func (s *HandlerSuite) TestRedactRailsAPIHostFromErrors(c *check.C) {
+ req := httptest.NewRequest("GET", "https://0.0.0.0:1/arvados/v1/collections/zzzzz-4zz18-abcdefghijklmno", nil)
+ req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
+ resp := httptest.NewRecorder()
+ s.handler.ServeHTTP(resp, req)
+ c.Check(resp.Code, check.Equals, http.StatusNotFound)
+ var jresp struct {
+ Errors []string
+ }
+ c.Log(resp.Body.String())
+ c.Assert(json.NewDecoder(resp.Body).Decode(&jresp), check.IsNil)
+ c.Assert(jresp.Errors, check.HasLen, 1)
+ c.Check(jresp.Errors[0], check.Matches, `.*//railsapi\.internal/arvados/v1/collections/.*: 404 Not Found.*`)
+ c.Check(jresp.Errors[0], check.Not(check.Matches), `(?ms).*127.0.0.1.*`)
+}
}
type Conn struct {
- SendHeader http.Header
+ SendHeader http.Header
+ RedactHostInErrors bool
+
clusterID string
httpClient http.Client
baseURL url.URL
path = strings.Replace(path, "/{uuid}", "/"+uuid, 1)
delete(params, "uuid")
}
- return aClient.RequestAndDecodeContext(ctx, dst, ep.Method, path, body, params)
+ err = aClient.RequestAndDecodeContext(ctx, dst, ep.Method, path, body, params)
+ if err != nil && conn.RedactHostInErrors {
+ redacted := strings.Replace(err.Error(), conn.baseURL.String(), "//railsapi.internal", -1)
+ if strings.HasPrefix(redacted, "request failed: ") {
+ redacted = strings.Replace(redacted, "request failed: ", "", -1)
+ }
+ if redacted != err.Error() {
+ if err, ok := err.(httpStatusError); ok {
+ return wrapHTTPStatusError(err, redacted)
+ } else {
+ return errors.New(redacted)
+ }
+ }
+ }
+ return err
}
func (conn *Conn) BaseURL() url.URL {
err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
return resp, err
}
+
+// httpStatusError is an error with an HTTP status code that can be
+// propagated by lib/controller/router, etc.
+type httpStatusError interface {
+ error
+ HTTPStatus() int
+}
+
+// wrappedHTTPStatusError is used to augment/replace an error message
+// while preserving the HTTP status code indicated by the original
+// error.
+type wrappedHTTPStatusError struct {
+ httpStatusError
+ message string
+}
+
+func wrapHTTPStatusError(err httpStatusError, message string) httpStatusError {
+ return wrappedHTTPStatusError{err, message}
+}
+
+func (err wrappedHTTPStatusError) Error() string {
+ return err.message
+}