Skip to content

Commit

Permalink
Introduce proxyConnectError code, log if present only
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Jan 12, 2021
1 parent 4f7a48c commit a687dd0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
2 changes: 2 additions & 0 deletions errors/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
APIError Code = 4000 + iota
APIRouteNotFound
APIConnect
APIProxyConnect
APIReqBodySizeExceeded
)

Expand Down Expand Up @@ -54,6 +55,7 @@ var codes = map[Code]string{
APIError: "API failed",
APIRouteNotFound: "API route not found",
APIConnect: "API upstream connection error",
APIProxyConnect: "upstream connection error via configured proxy",
APIReqBodySizeExceeded: "Request body size exceeded",
// 5xxx
AuthorizationRequired: "Authorization required",
Expand Down
2 changes: 1 addition & 1 deletion errors/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func httpStatus(code Code) int {
switch code {
case APIRouteNotFound, FilesRouteNotFound, RouteNotFound, SPARouteNotFound:
return http.StatusNotFound
case APIConnect, UpstreamResponseValidationFailed:
case APIConnect, APIProxyConnect, UpstreamResponseValidationFailed:
return http.StatusBadGateway
case APIReqBodySizeExceeded:
return http.StatusRequestEntityTooLarge
Expand Down
6 changes: 5 additions & 1 deletion handler/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ func (p *Proxy) roundtrip(rw http.ResponseWriter, req *http.Request) {
roundtripInfo.BeReq, roundtripInfo.BeResp = outreq, res
if err != nil {
roundtripInfo.Err = err
p.srvOptions.APIErrTpl.ServeError(couperErr.APIConnect).ServeHTTP(rw, req)
errCode := couperErr.APIConnect
if strings.HasPrefix(err.Error(), "proxyconnect") {
errCode = couperErr.APIProxyConnect
}
p.srvOptions.APIErrTpl.ServeError(errCode).ServeHTTP(rw, req)
return
}

Expand Down
16 changes: 7 additions & 9 deletions logging/access_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,12 @@ func (log *AccessLog) ServeHTTP(rw http.ResponseWriter, req *http.Request, nextH
nextHandler.ServeHTTP(rw, req)
serveDone := time.Now()

proxy := ""
reqCtx := req
if isUpstreamRequest && roundtripInfo != nil && roundtripInfo.BeReq != nil {
reqCtx = roundtripInfo.BeReq
if roundtripInfo.BeResp != nil {
reqCtx.TLS = roundtripInfo.BeResp.TLS
}

if !log.conf.NoProxyFromEnv {
u, err := http.ProxyFromEnvironment(roundtripInfo.BeReq)
if err == nil && u != nil {
proxy = u.Host
}
}
}

uniqueID := reqCtx.Context().Value(request.UID)
Expand All @@ -136,7 +128,6 @@ func (log *AccessLog) ServeHTTP(rw http.ResponseWriter, req *http.Request, nextH
fields := Fields{
"method": reqCtx.Method,
"proto": reqCtx.Proto,
"proxy": proxy,
"request": requestFields,
"server": serverName,
"uid": uniqueID,
Expand All @@ -153,6 +144,13 @@ func (log *AccessLog) ServeHTTP(rw http.ResponseWriter, req *http.Request, nextH
backendName = serverName + ":" + endpointName
}
fields["backend"] = backendName

if !log.conf.NoProxyFromEnv && roundtripInfo != nil && roundtripInfo.BeReq != nil {
u, err := http.ProxyFromEnvironment(roundtripInfo.BeReq)
if err == nil && u != nil {
fields["proxy"] = u.Host
}
}
}

if log.conf.TypeFieldKey != "" {
Expand Down
4 changes: 2 additions & 2 deletions server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {
expectation{http.StatusNotFound, []byte(`{"code": 4001}`), http.Header{"Content-Type": {"application/json"}}, ""},
},
{
testRequest{http.MethodGet, "http://anyserver:8080/v1/connect-error/"},
expectation{http.StatusBadGateway, []byte(`{"code": 4002}`), http.Header{"Content-Type": {"application/json"}}, "api"},
testRequest{http.MethodGet, "http://anyserver:8080/v1/connect-error/"}, // in this case proxyconnect fails
expectation{http.StatusBadGateway, []byte(`{"code": 4003}`), http.Header{"Content-Type": {"application/json"}}, "api"},
},
{
testRequest{http.MethodGet, "http://anyserver:8080/v1x"},
Expand Down

0 comments on commit a687dd0

Please sign in to comment.