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"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
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/outpost/proxyv2/templates"
|
|
|
|
"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-09-11 17:30:17 +00:00
|
|
|
rp.Transport = ak.NewTracingTransport(context.TODO(), a.getUpstreamTransport())
|
2021-09-08 18:04:56 +00:00
|
|
|
rp.ErrorHandler = NewProxyErrorHandler(templates.GetTemplates())
|
|
|
|
rp.ModifyResponse = a.proxyModifyResponse
|
|
|
|
a.mux.PathPrefix("/").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
claims, err := a.getClaims(r)
|
|
|
|
if claims == nil && a.IsAllowlisted(r) {
|
|
|
|
a.log.Trace("path can be accessed without authentication")
|
|
|
|
} else if claims == nil && err != nil {
|
|
|
|
a.redirectToStart(rw, r)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
a.addHeaders(r, claims)
|
|
|
|
}
|
|
|
|
before := time.Now()
|
|
|
|
rp.ServeHTTP(rw, r)
|
|
|
|
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,
|
2021-09-08 18:04:56 +00:00
|
|
|
"upstream_host": u.String(),
|
|
|
|
"scheme": r.URL.Scheme,
|
|
|
|
"method": r.Method,
|
|
|
|
"path": r.URL.Path,
|
|
|
|
"host": web.GetHost(r),
|
|
|
|
"user": user,
|
|
|
|
}).Observe(float64(after))
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) proxyModifyRequest(u *url.URL) func(req *http.Request) {
|
|
|
|
return func(req *http.Request) {
|
|
|
|
req.URL.Scheme = u.Scheme
|
|
|
|
req.URL.Host = u.Host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) proxyModifyResponse(res *http.Response) error {
|
|
|
|
return nil
|
|
|
|
}
|