From 2a23f2fb9bca74302dcdc40def50c748da4a5e06 Mon Sep 17 00:00:00 2001 From: Jaime Martinez Date: Tue, 9 Jun 2020 16:20:31 +1000 Subject: Move serving 404 logic to domain package Simplify responsibilities of auth package and reduce complexity of app.go deciding which content to serve. --- internal/domain/domain.go | 23 +++++++++++++++++++---- internal/domain/domain_test.go | 12 +++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) (limited to 'internal/domain') diff --git a/internal/domain/domain.go b/internal/domain/domain.go index 2a301121..9fa843a3 100644 --- a/internal/domain/domain.go +++ b/internal/domain/domain.go @@ -169,14 +169,14 @@ func (d *Domain) ServeNotFoundHTTP(w http.ResponseWriter, r *http.Request) { request.ServeNotFoundHTTP(w, r) } -// ServeNamespaceNotFound will try to find a parent namespace domain for a request +// serveNamespaceNotFound will try to find a parent namespace domain for a request // that failed authentication so that we serve the custom namespace error page for // public namespace domains -func (d *Domain) ServeNamespaceNotFound(w http.ResponseWriter, r *http.Request) { - // override the path nd try to resolve the domain name +func (d *Domain) serveNamespaceNotFound(w http.ResponseWriter, r *http.Request) { + // override the path and try to resolve the domain name r.URL.Path = "/" namespaceDomain, err := d.Resolver.Resolve(r) - if err != nil { + if err != nil || namespaceDomain.LookupPath == nil { httperrors.Serve404(w) return } @@ -189,3 +189,18 @@ func (d *Domain) ServeNamespaceNotFound(w http.ResponseWriter, r *http.Request) httperrors.Serve404(w) } + +// ServeNotFoundAuthFailed handler to be called when auth failed so the correct custom +// 404 page is served. +func (d *Domain) ServeNotFoundAuthFailed(w http.ResponseWriter, r *http.Request) { + if d.isUnconfigured() || !d.HasLookupPath(r) { + httperrors.Serve404(w) + return + } + if d.IsNamespaceProject(r) { + d.ServeNotFoundHTTP(w, r) + return + } + + d.serveNamespaceNotFound(w, r) +} diff --git a/internal/domain/domain_test.go b/internal/domain/domain_test.go index e152b031..9e89f0e5 100644 --- a/internal/domain/domain_test.go +++ b/internal/domain/domain_test.go @@ -209,6 +209,16 @@ func TestDomain_ServeNamespaceNotFound(t *testing.T) { }, expectedResponse: "The page you're looking for could not be found.", }, + { + name: "no_parent_namespace_domain", + domain: "group.404.gitlab-example.com", + path: "/unknown", + resolver: &stubbedResolver{ + project: nil, + subpath: "/", + }, + expectedResponse: "The page you're looking for could not be found.", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -218,7 +228,7 @@ func TestDomain_ServeNamespaceNotFound(t *testing.T) { } w := httptest.NewRecorder() r := httptest.NewRequest("GET", fmt.Sprintf("http://%s%s", tt.domain, tt.path), nil) - d.ServeNamespaceNotFound(w, r) + d.serveNamespaceNotFound(w, r) resp := w.Result() require.Equal(t, http.StatusNotFound, resp.StatusCode) -- cgit v1.2.3