2017-09-18 15:48:07 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/containous/traefik/log"
|
2018-01-10 16:48:04 +00:00
|
|
|
"github.com/containous/traefik/middlewares/tracing"
|
2017-09-18 15:48:07 +00:00
|
|
|
"github.com/containous/traefik/types"
|
|
|
|
"github.com/vulcand/oxy/forward"
|
|
|
|
"github.com/vulcand/oxy/utils"
|
|
|
|
)
|
|
|
|
|
2017-12-09 23:58:21 +00:00
|
|
|
const (
|
2018-06-13 13:14:03 +00:00
|
|
|
xForwardedURI = "X-Forwarded-Uri"
|
|
|
|
xForwardedMethod = "X-Forwarded-Method"
|
2017-12-09 23:58:21 +00:00
|
|
|
)
|
|
|
|
|
2017-09-18 15:48:07 +00:00
|
|
|
// Forward the authentication to a external server
|
|
|
|
func Forward(config *types.Forward, w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
2017-11-02 10:06:03 +00:00
|
|
|
// Ensure our request client does not follow redirects
|
|
|
|
httpClient := http.Client{
|
|
|
|
CheckRedirect: func(r *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
}
|
2018-04-23 13:28:04 +00:00
|
|
|
|
2017-09-18 15:48:07 +00:00
|
|
|
if config.TLS != nil {
|
|
|
|
tlsConfig, err := config.TLS.CreateTLSConfig()
|
|
|
|
if err != nil {
|
2018-01-10 16:48:04 +00:00
|
|
|
tracing.SetErrorAndDebugLog(r, "Unable to configure TLS to call %s. Cause %s", config.Address, err)
|
2017-09-18 15:48:07 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2018-04-23 13:28:04 +00:00
|
|
|
|
2017-09-18 15:48:07 +00:00
|
|
|
httpClient.Transport = &http.Transport{
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 13:28:04 +00:00
|
|
|
|
2017-09-18 15:48:07 +00:00
|
|
|
forwardReq, err := http.NewRequest(http.MethodGet, config.Address, nil)
|
2018-01-10 16:48:04 +00:00
|
|
|
tracing.LogRequest(tracing.GetSpan(r), forwardReq)
|
2017-09-18 15:48:07 +00:00
|
|
|
if err != nil {
|
2018-01-10 16:48:04 +00:00
|
|
|
tracing.SetErrorAndDebugLog(r, "Error calling %s. Cause %s", config.Address, err)
|
2017-09-18 15:48:07 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeHeader(r, forwardReq, config.TrustForwardHeader)
|
|
|
|
|
2018-01-10 16:48:04 +00:00
|
|
|
tracing.InjectRequestHeaders(forwardReq)
|
|
|
|
|
2017-09-18 15:48:07 +00:00
|
|
|
forwardResponse, forwardErr := httpClient.Do(forwardReq)
|
|
|
|
if forwardErr != nil {
|
2018-01-10 16:48:04 +00:00
|
|
|
tracing.SetErrorAndDebugLog(r, "Error calling %s. Cause: %s", config.Address, forwardErr)
|
2017-09-18 15:48:07 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, readError := ioutil.ReadAll(forwardResponse.Body)
|
|
|
|
if readError != nil {
|
2018-01-10 16:48:04 +00:00
|
|
|
tracing.SetErrorAndDebugLog(r, "Error reading body %s. Cause: %s", config.Address, readError)
|
2017-09-18 15:48:07 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer forwardResponse.Body.Close()
|
|
|
|
|
2017-11-02 10:06:03 +00:00
|
|
|
// Pass the forward response's body and selected headers if it
|
|
|
|
// didn't return a response within the range of [200, 300).
|
2017-09-18 15:48:07 +00:00
|
|
|
if forwardResponse.StatusCode < http.StatusOK || forwardResponse.StatusCode >= http.StatusMultipleChoices {
|
|
|
|
log.Debugf("Remote error %s. StatusCode: %d", config.Address, forwardResponse.StatusCode)
|
2017-11-02 10:06:03 +00:00
|
|
|
|
2018-04-23 13:28:04 +00:00
|
|
|
utils.CopyHeaders(w.Header(), forwardResponse.Header)
|
2018-09-18 12:22:03 +00:00
|
|
|
utils.RemoveHeaders(w.Header(), forward.HopHeaders...)
|
2018-04-23 13:28:04 +00:00
|
|
|
|
2017-11-02 10:06:03 +00:00
|
|
|
// Grab the location header, if any.
|
|
|
|
redirectURL, err := forwardResponse.Location()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != http.ErrNoLocation {
|
2018-01-10 16:48:04 +00:00
|
|
|
tracing.SetErrorAndDebugLog(r, "Error reading response location header %s. Cause: %s", config.Address, err)
|
2017-11-02 10:06:03 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if redirectURL.String() != "" {
|
|
|
|
// Set the location in our response if one was sent back.
|
2018-04-23 13:28:04 +00:00
|
|
|
w.Header().Set("Location", redirectURL.String())
|
2017-11-02 10:06:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 16:48:04 +00:00
|
|
|
tracing.LogResponseCode(tracing.GetSpan(r), forwardResponse.StatusCode)
|
2017-09-18 15:48:07 +00:00
|
|
|
w.WriteHeader(forwardResponse.StatusCode)
|
|
|
|
w.Write(body)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-30 05:54:03 +00:00
|
|
|
for _, headerName := range config.AuthResponseHeaders {
|
|
|
|
r.Header.Set(headerName, forwardResponse.Header.Get(headerName))
|
|
|
|
}
|
|
|
|
|
2017-09-18 15:48:07 +00:00
|
|
|
r.RequestURI = r.URL.RequestURI()
|
|
|
|
next(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeHeader(req *http.Request, forwardReq *http.Request, trustForwardHeader bool) {
|
|
|
|
utils.CopyHeaders(forwardReq.Header, req.Header)
|
2018-09-24 08:42:03 +00:00
|
|
|
utils.RemoveHeaders(forwardReq.Header, forward.HopHeaders...)
|
2017-09-18 15:48:07 +00:00
|
|
|
|
|
|
|
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
|
|
|
if trustForwardHeader {
|
|
|
|
if prior, ok := req.Header[forward.XForwardedFor]; ok {
|
|
|
|
clientIP = strings.Join(prior, ", ") + ", " + clientIP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
forwardReq.Header.Set(forward.XForwardedFor, clientIP)
|
|
|
|
}
|
|
|
|
|
2018-06-13 13:14:03 +00:00
|
|
|
if xMethod := req.Header.Get(xForwardedMethod); xMethod != "" && trustForwardHeader {
|
|
|
|
forwardReq.Header.Set(xForwardedMethod, xMethod)
|
|
|
|
} else if req.Method != "" {
|
|
|
|
forwardReq.Header.Set(xForwardedMethod, req.Method)
|
|
|
|
} else {
|
|
|
|
forwardReq.Header.Del(xForwardedMethod)
|
|
|
|
}
|
|
|
|
|
2017-09-18 15:48:07 +00:00
|
|
|
if xfp := req.Header.Get(forward.XForwardedProto); xfp != "" && trustForwardHeader {
|
|
|
|
forwardReq.Header.Set(forward.XForwardedProto, xfp)
|
|
|
|
} else if req.TLS != nil {
|
|
|
|
forwardReq.Header.Set(forward.XForwardedProto, "https")
|
|
|
|
} else {
|
|
|
|
forwardReq.Header.Set(forward.XForwardedProto, "http")
|
|
|
|
}
|
|
|
|
|
|
|
|
if xfp := req.Header.Get(forward.XForwardedPort); xfp != "" && trustForwardHeader {
|
|
|
|
forwardReq.Header.Set(forward.XForwardedPort, xfp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if xfh := req.Header.Get(forward.XForwardedHost); xfh != "" && trustForwardHeader {
|
|
|
|
forwardReq.Header.Set(forward.XForwardedHost, xfh)
|
|
|
|
} else if req.Host != "" {
|
|
|
|
forwardReq.Header.Set(forward.XForwardedHost, req.Host)
|
|
|
|
} else {
|
|
|
|
forwardReq.Header.Del(forward.XForwardedHost)
|
|
|
|
}
|
2017-12-09 23:58:21 +00:00
|
|
|
|
|
|
|
if xfURI := req.Header.Get(xForwardedURI); xfURI != "" && trustForwardHeader {
|
|
|
|
forwardReq.Header.Set(xForwardedURI, xfURI)
|
|
|
|
} else if req.URL.RequestURI() != "" {
|
|
|
|
forwardReq.Header.Set(xForwardedURI, req.URL.RequestURI())
|
|
|
|
} else {
|
|
|
|
forwardReq.Header.Del(xForwardedURI)
|
|
|
|
}
|
2017-09-18 15:48:07 +00:00
|
|
|
}
|