diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2021-08-24 11:13:28 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2021-08-24 11:13:28 +0300 |
commit | 5ba6c6935d8867994b695cd3e4a21b4534c880b8 (patch) | |
tree | bb66a8a6b602af60af68c3645d1deca31fd440dd | |
parent | 84a77c9ef1766b81510e06d7c9d77129830b029f (diff) |
refactor: change middleware orderfix-artifact-redirect-for-well-known-path
and handle well-known paths before requesting
a domain's configuration when it's not needed.
Changelog: other
-rw-r--r-- | app.go | 27 | ||||
-rw-r--r-- | internal/auth/auth.go | 4 | ||||
-rw-r--r-- | internal/domain/domain.go | 5 | ||||
-rw-r--r-- | internal/source/gitlab/client/client.go | 1 | ||||
-rw-r--r-- | internal/source/gitlab/gitlab.go | 4 |
5 files changed, 38 insertions, 3 deletions
@@ -8,15 +8,16 @@ import ( "net" "net/http" "os" + "strings" "sync" "time" ghandlers "github.com/gorilla/handlers" "github.com/rs/cors" + "gitlab.com/gitlab-org/labkit/correlation" "gitlab.com/gitlab-org/labkit/log" "gitlab.com/gitlab-org/go-mimedb" - "gitlab.com/gitlab-org/labkit/correlation" "gitlab.com/gitlab-org/labkit/errortracking" labmetrics "gitlab.com/gitlab-org/labkit/metrics" "gitlab.com/gitlab-org/labkit/monitoring" @@ -42,6 +43,9 @@ import ( const ( xForwardedHost = "X-Forwarded-Host" + + pathPrefixArtifacts = "/-/" + pathPrefixAuth = "/auth" ) var ( @@ -56,6 +60,7 @@ type theApp struct { Handlers *handlers.Handlers AcmeMiddleware *acme.Middleware CustomHeaders http.Header + knownPaths []string } func (a *theApp) isReady() bool { @@ -94,6 +99,17 @@ func (a *theApp) redirectToHTTPS(w http.ResponseWriter, r *http.Request, statusC func (a *theApp) getHostAndDomain(r *http.Request) (string, *domain.Domain, error) { host := request.GetHostWithoutPort(r) + // do not fetch domain's config if it's a known path || host + for _, path := range a.knownPaths { + if strings.HasPrefix(r.URL.Path, path) { + return host, nil, nil + } + } + + if host == a.config.General.Domain { + return host, nil, nil + } + domain, err := a.domain(r.Context(), host) return host, domain, err @@ -221,6 +237,7 @@ func (a *theApp) acmeMiddleware(handler http.Handler) http.Handler { // authMiddleware handles authentication requests func (a *theApp) authMiddleware(handler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Printf("authMiddleware do we have a correlation ID yet? %q\n", correlation.ExtractFromContext(r.Context())) if a.Auth.TryAuthenticate(w, r, a.domains) { return } @@ -500,7 +517,13 @@ func runApp(config *cfg.Config) { log.WithError(err).Fatal("could not create domains config source") } - a := theApp{config: config, domains: domains} + knownPaths := []string{ + pathPrefixArtifacts, + pathPrefixAuth, + config.General.StatusPath, + } + + a := theApp{config: config, domains: domains, knownPaths: knownPaths} err = logging.ConfigureLogging(a.config.Log.Format, a.config.Log.Verbose) if err != nil { diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 29aaa582..a1a5ca6f 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -17,6 +17,7 @@ import ( "github.com/gorilla/securecookie" "github.com/gorilla/sessions" "github.com/sirupsen/logrus" + "gitlab.com/gitlab-org/labkit/correlation" "golang.org/x/crypto/hkdf" "gitlab.com/gitlab-org/labkit/errortracking" @@ -433,6 +434,9 @@ func (a *Auth) checkTokenExists(session *sessions.Session, w http.ResponseWriter state := base64.URLEncoding.EncodeToString(securecookie.GenerateRandomKey(16)) session.Values["state"] = state session.Values["uri"] = getRequestAddress(r) + correlationID := correlation.ExtractFromContext(r.Context()) + fmt.Printf("do we have the ID here?\n%q\n", correlationID) + session.Values["correlation_id"] = correlationID // Clear possible proxying delete(session.Values, "proxy_auth_domain") diff --git a/internal/domain/domain.go b/internal/domain/domain.go index 94888e34..4f6da93e 100644 --- a/internal/domain/domain.go +++ b/internal/domain/domain.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "errors" + "fmt" "net/http" "sync" @@ -47,6 +48,7 @@ func (d *Domain) String() string { func (d *Domain) resolve(r *http.Request) (*serving.Request, error) { if d == nil { + fmt.Printf("d is nil in reslove\n") return nil, ErrDomainDoesNotExist } @@ -126,6 +128,7 @@ func (d *Domain) ServeFileHTTP(w http.ResponseWriter, r *http.Request) bool { request, err := d.resolve(r) if err != nil { if errors.Is(err, ErrDomainDoesNotExist) { + fmt.Printf("ServeFileHTTP ErrDomainDoesNotExist\n") // serve generic 404 httperrors.Serve404(w) return true @@ -144,6 +147,7 @@ func (d *Domain) ServeNotFoundHTTP(w http.ResponseWriter, r *http.Request) { request, err := d.resolve(r) if err != nil { if errors.Is(err, ErrDomainDoesNotExist) { + fmt.Printf("ServeNotFoundHTTP ErrDomainDoesNotExist\n") // serve generic 404 httperrors.Serve404(w) return @@ -168,6 +172,7 @@ func (d *Domain) serveNamespaceNotFound(w http.ResponseWriter, r *http.Request) namespaceDomain, err := d.Resolver.Resolve(clonedReq) if err != nil { if errors.Is(err, ErrDomainDoesNotExist) { + fmt.Printf("serveNamespaceNotFound ErrDomainDoesNotExist\n") // serve generic 404 httperrors.Serve404(w) return diff --git a/internal/source/gitlab/client/client.go b/internal/source/gitlab/client/client.go index 30185143..25515612 100644 --- a/internal/source/gitlab/client/client.go +++ b/internal/source/gitlab/client/client.go @@ -110,6 +110,7 @@ func (gc *Client) GetLookup(ctx context.Context, host string) api.Lookup { } if resp == nil { + fmt.Printf("response is nil....\n") return api.Lookup{Name: host, Error: domain.ErrDomainDoesNotExist} } diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index b79b434f..6e8136d0 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -10,6 +10,8 @@ import ( "gitlab.com/gitlab-org/labkit/log" + "gitlab.com/gitlab-org/gitlab-pages/internal/logging" + "gitlab.com/gitlab-org/gitlab-pages/internal/config" "gitlab.com/gitlab-org/gitlab-pages/internal/domain" "gitlab.com/gitlab-org/gitlab-pages/internal/request" @@ -98,7 +100,7 @@ func (g *Gitlab) Resolve(r *http.Request) (*serving.Request, error) { SubPath: subPath}, nil } } - + logging.LogRequest(r).WithError(domain.ErrDomainDoesNotExist).Errorf("no lookup path for: %q", r.URL.Path) return nil, domain.ErrDomainDoesNotExist } |