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-11 17:30:17 +00:00
|
|
|
"goauthentik.io/internal/outpost/ak"
|
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")
|
|
|
|
rp.Transport = ak.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)
|
|
|
|
|
|
|
|
user := ""
|
|
|
|
if claims != nil {
|
|
|
|
user = claims.Email
|
|
|
|
}
|
|
|
|
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
|
|
|
"scheme": r.URL.Scheme,
|
|
|
|
"method": r.Method,
|
|
|
|
"path": r.URL.Path,
|
|
|
|
"host": web.GetHost(r),
|
|
|
|
"user": user,
|
|
|
|
}).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) {
|
|
|
|
claims, _ := a.getClaims(r)
|
2022-02-03 16:58:38 +00:00
|
|
|
if 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-01-30 21:29:34 +00:00
|
|
|
r.URL.Scheme = u.Scheme
|
|
|
|
r.URL.Host = u.Host
|
|
|
|
} else {
|
|
|
|
r.URL.Scheme = ou.Scheme
|
|
|
|
r.URL.Host = ou.Host
|
2022-01-30 20:35:08 +00:00
|
|
|
}
|
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
|
|
|
|
}
|