2021-09-08 18:04:56 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
2021-09-11 17:30:17 +00:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-09-08 18:04:56 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2021-12-12 12:46:31 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-09-08 18:04:56 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-12-20 18:42:45 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-09-08 18:04:56 +00:00
|
|
|
"goauthentik.io/internal/outpost/proxyv2/metrics"
|
|
|
|
"goauthentik.io/internal/utils/web"
|
|
|
|
)
|
|
|
|
|
2021-09-11 17:30:17 +00:00
|
|
|
func (a *Application) getUpstreamTransport() http.RoundTripper {
|
|
|
|
return &http.Transport{
|
2021-10-02 19:17:15 +00:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: !*a.proxyConfig.InternalHostSslValidation},
|
2021-09-11 17:30:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
func (a *Application) configureProxy() error {
|
|
|
|
// Reverse proxy to the application server
|
|
|
|
u, err := url.Parse(*a.proxyConfig.InternalHost)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rp := &httputil.ReverseProxy{Director: a.proxyModifyRequest(u)}
|
2021-12-12 12:46:31 +00:00
|
|
|
rsp := sentry.StartSpan(context.TODO(), "authentik.outposts.proxy.application_transport")
|
2022-06-20 09:54:10 +00:00
|
|
|
rp.Transport = web.NewTracingTransport(rsp.Context(), a.getUpstreamTransport())
|
2021-12-20 21:20:23 +00:00
|
|
|
rp.ErrorHandler = a.newProxyErrorHandler()
|
2021-09-08 18:04:56 +00:00
|
|
|
rp.ModifyResponse = a.proxyModifyResponse
|
|
|
|
a.mux.PathPrefix("/").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
claims, err := a.getClaims(r)
|
2022-01-24 19:50:13 +00:00
|
|
|
if claims == nil && a.IsAllowlisted(r.URL) {
|
2021-09-08 18:04:56 +00:00
|
|
|
a.log.Trace("path can be accessed without authentication")
|
|
|
|
} else if claims == nil && err != nil {
|
|
|
|
a.redirectToStart(rw, r)
|
|
|
|
return
|
|
|
|
} else {
|
2021-12-01 19:05:56 +00:00
|
|
|
a.addHeaders(r.Header, claims)
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
before := time.Now()
|
|
|
|
rp.ServeHTTP(rw, r)
|
2021-12-20 18:42:45 +00:00
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil || err == http.ErrAbortHandler {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.WithError(err.(error)).Error("recover in reverse proxy")
|
|
|
|
}()
|
2021-09-08 18:04:56 +00:00
|
|
|
after := time.Since(before)
|
|
|
|
|
|
|
|
metrics.UpstreamTiming.With(prometheus.Labels{
|
2021-09-16 08:03:31 +00:00
|
|
|
"outpost_name": a.outpostName,
|
2022-01-30 20:35:08 +00:00
|
|
|
"upstream_host": r.URL.Host,
|
2021-09-08 18:04:56 +00:00
|
|
|
"method": r.Method,
|
2022-06-02 22:06:09 +00:00
|
|
|
"scheme": r.URL.Scheme,
|
2021-09-08 18:04:56 +00:00
|
|
|
"host": web.GetHost(r),
|
|
|
|
}).Observe(float64(after))
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-30 21:29:34 +00:00
|
|
|
func (a *Application) proxyModifyRequest(ou *url.URL) func(req *http.Request) {
|
2022-01-30 20:35:08 +00:00
|
|
|
return func(r *http.Request) {
|
2022-02-10 22:09:55 +00:00
|
|
|
r.Header.Set("X-Forwarded-Host", r.Host)
|
2022-01-30 20:35:08 +00:00
|
|
|
claims, _ := a.getClaims(r)
|
2022-02-07 18:59:06 +00:00
|
|
|
r.URL.Scheme = ou.Scheme
|
|
|
|
r.URL.Host = ou.Host
|
2022-02-08 15:30:26 +00:00
|
|
|
if claims != nil && claims.Proxy != nil && claims.Proxy.BackendOverride != "" {
|
2022-01-30 21:29:34 +00:00
|
|
|
u, err := url.Parse(claims.Proxy.BackendOverride)
|
2022-01-30 20:35:08 +00:00
|
|
|
if err != nil {
|
|
|
|
a.log.WithField("backend_override", claims.Proxy.BackendOverride).WithError(err).Warning("failed parse user backend override")
|
2022-02-08 15:30:26 +00:00
|
|
|
} else {
|
|
|
|
r.URL.Scheme = u.Scheme
|
|
|
|
r.URL.Host = u.Host
|
2022-01-30 20:35:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-08 15:30:26 +00:00
|
|
|
a.log.WithField("upstream_url", r.URL.String()).Trace("final upstream url")
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) proxyModifyResponse(res *http.Response) error {
|
2022-01-25 09:57:53 +00:00
|
|
|
res.Header.Set("X-Powered-By", "authentik_proxy2")
|
2021-09-08 18:04:56 +00:00
|
|
|
return nil
|
|
|
|
}
|