Skip to content
Snippets Groups Projects
Commit 436217dc authored by Vladimir Shushlin's avatar Vladimir Shushlin :writing_hand:
Browse files

Merge branch 'ngala/1131-rename-default-domain-redirect' into 'master'

Rename LookupPath defaultDomainRedirect to primaryDomain

See merge request !1105



Merged-by: default avatarVladimir Shushlin <vshushlin@gitlab.com>
Approved-by: Janis Altherr's avatarJanis Altherr <jaltherr@gitlab.com>
Approved-by: default avatarVladimir Shushlin <vshushlin@gitlab.com>
Reviewed-by: default avatarVladimir Shushlin <vshushlin@gitlab.com>
Co-authored-by: default avatarngala <ngala@gitlab.com>
parents b0be8302 b44f2a49
No related branches found
No related tags found
1 merge request!1105Rename LookupPath defaultDomainRedirect to primaryDomain
Pipeline #1610308869 failed
......@@ -25,7 +25,6 @@ import (
"gitlab.com/gitlab-org/gitlab-pages/internal/auth"
cfg "gitlab.com/gitlab-org/gitlab-pages/internal/config"
"gitlab.com/gitlab-org/gitlab-pages/internal/customheaders"
"gitlab.com/gitlab-org/gitlab-pages/internal/defaultdomainredirect"
"gitlab.com/gitlab-org/gitlab-pages/internal/domain"
"gitlab.com/gitlab-org/gitlab-pages/internal/errortracking"
"gitlab.com/gitlab-org/gitlab-pages/internal/handlers"
......@@ -35,6 +34,7 @@ import (
"gitlab.com/gitlab-org/gitlab-pages/internal/logging/slowlogs"
"gitlab.com/gitlab-org/gitlab-pages/internal/namespaceinpath"
"gitlab.com/gitlab-org/gitlab-pages/internal/netutil"
"gitlab.com/gitlab-org/gitlab-pages/internal/primarydomain"
"gitlab.com/gitlab-org/gitlab-pages/internal/redirects"
"gitlab.com/gitlab-org/gitlab-pages/internal/rejectmethods"
"gitlab.com/gitlab-org/gitlab-pages/internal/request"
......@@ -160,7 +160,7 @@ func (a *theApp) buildHandlerPipeline() (http.Handler, error) {
// Handlers should be applied in a reverse order
handler := slowlogs.LogHandlerTiming(a.serveFileOrNotFoundHandler(), "serveFileOrNotFound")
handler = slowlogs.LogHandlerTiming(uniquedomain.NewMiddleware(handler), "UniqueDomain")
handler = slowlogs.LogHandlerTiming(defaultdomainredirect.NewMiddleware(handler), "DefaultDomainRedirect")
handler = slowlogs.LogHandlerTiming(primarydomain.NewMiddleware(handler), "PrimaryDomain")
handler = slowlogs.LogHandlerTiming(a.Auth.AuthorizationMiddleware(handler), "Authorization")
handler = slowlogs.LogHandlerTiming(handlers.ArtifactMiddleware(handler, a.Handlers), "Artifacts")
......
package defaultdomainredirect
package primarydomain
import (
"net"
......@@ -12,8 +12,8 @@ import (
func NewMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
redirectURL := getRedirectURL(r)
if redirectURL == "" {
primaryURL := getPrimaryURL(r)
if primaryURL == "" {
logging.
LogRequest(r).
Debug("default domain: doing nothing")
......@@ -24,18 +24,18 @@ func NewMiddleware(handler http.Handler) http.Handler {
logging.
LogRequest(r).
WithField("redirectURL", redirectURL).
Info("redirecting to default domain")
WithField("primaryURL", primaryURL).
Info("redirecting to primary domain")
http.Redirect(w, r, redirectURL, http.StatusPermanentRedirect)
http.Redirect(w, r, primaryURL, http.StatusPermanentRedirect)
})
}
var getDefaultDomainRedirectAndPrefixFunc = getDefaultDomainRedirectAndPrefix
var getPrimaryDomainAndPrefixFunc = getPrimaryDomainAndPrefix
func getRedirectURL(r *http.Request) string {
defaultDomainRedirect, prefix := getDefaultDomainRedirectAndPrefixFunc(r)
if defaultDomainRedirect == "" {
func getPrimaryURL(r *http.Request) string {
primaryDomain, prefix := getPrimaryDomainAndPrefixFunc(r)
if primaryDomain == "" {
return ""
}
......@@ -44,34 +44,34 @@ func getRedirectURL(r *http.Request) string {
requestHost = r.Host
}
defaultURL, isValid := utils.ParseURL(defaultDomainRedirect)
primaryURL, isValid := utils.ParseURL(primaryDomain)
if !isValid {
return ""
}
// If the requestHost is already the defaultDomainRedirect, no need to redirect
if requestHost == defaultURL.Host {
// If the requestHost is already the primaryDomain, no need to redirect
if requestHost == primaryURL.Host {
return ""
}
// Ensure to redirect to the same path requested
defaultURL.Path = strings.TrimPrefix(
primaryURL.Path = strings.TrimPrefix(
r.URL.Path,
strings.TrimSuffix(prefix, "/"),
)
return defaultURL.String()
return primaryURL.String()
}
func getDefaultDomainRedirectAndPrefix(r *http.Request) (string, string) {
func getPrimaryDomainAndPrefix(r *http.Request) (string, string) {
lookupPath, err := domain.FromRequest(r).GetLookupPath(r)
if err != nil {
logging.
LogRequest(r).
WithError(err).
Error("defaultDomainRedirect: failed to get lookupPath")
Error("primaryDomain: failed to get lookupPath")
return "", ""
}
return lookupPath.DefaultDomainRedirect, lookupPath.Prefix
return lookupPath.PrimaryDomain, lookupPath.Prefix
}
package defaultdomainredirect
package primarydomain
import (
"net/http"
......@@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"
)
func TestMiddlewareRedirectsToDefaultDomain(t *testing.T) {
func TestMiddlewareRedirectsToPrimaryDomain(t *testing.T) {
finalHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
......@@ -19,55 +19,55 @@ func TestMiddlewareRedirectsToDefaultDomain(t *testing.T) {
name string
requestHost string
requestPath string
defaultDomain string
primaryDomain string
prefix string
expectedStatusCode int
expectedLocation string
}{
{
name: "redirect to default domain",
name: "redirect to primary domain",
requestHost: "abc.example.com",
requestPath: "/path/to/resource",
defaultDomain: "https://default.example.com",
primaryDomain: "https://default.example.com",
expectedStatusCode: http.StatusPermanentRedirect,
expectedLocation: "https://default.example.com/path/to/resource",
},
{
name: "redirect to default domain having prefix",
name: "redirect to primary domain having prefix",
requestHost: "abc.example.com",
requestPath: "/project/path/to/resource",
defaultDomain: "https://default.example.com",
primaryDomain: "https://default.example.com",
prefix: "/project/",
expectedStatusCode: http.StatusPermanentRedirect,
expectedLocation: "https://default.example.com/path/to/resource",
},
{
name: "no redirect if already on default domain",
name: "no redirect if already on primary domain",
requestHost: "default.example.com",
requestPath: "/path/to/resource",
defaultDomain: "https://default.example.com",
primaryDomain: "https://default.example.com",
expectedStatusCode: http.StatusOK,
},
{
name: "no redirect if defaultDomainRedirect is empty",
name: "no redirect if primaryDomain is empty",
requestHost: "abc.example.com",
requestPath: "/path/to/resource",
defaultDomain: "",
primaryDomain: "",
expectedStatusCode: http.StatusOK,
},
{
name: "invalid default domain",
name: "invalid primary domain",
requestHost: "abc.example.com",
requestPath: "/path/to/resource",
defaultDomain: "://invalid-url",
primaryDomain: "://invalid-url",
expectedStatusCode: http.StatusOK,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
getDefaultDomainRedirectAndPrefixFunc = func(r *http.Request) (string, string) {
return tc.defaultDomain, tc.prefix
getPrimaryDomainAndPrefixFunc = func(r *http.Request) (string, string) {
return tc.primaryDomain, tc.prefix
}
r := httptest.NewRequest(http.MethodGet, "http://"+tc.requestHost+tc.requestPath, nil)
......
......@@ -2,15 +2,15 @@ package serving
// LookupPath holds a domain project configuration needed to handle a request
type LookupPath struct {
ServingType string // Serving type being used, like `zip`
Prefix string // Project prefix, for example, /my/project in group.gitlab.io/my/project/index.html
Path string // Path is an internal and serving-specific location of a document
SHA256 string
IsNamespaceProject bool // IsNamespaceProject is DEPRECATED, see https://gitlab.com/gitlab-org/gitlab-pages/issues/272
IsHTTPSOnly bool
HasAccessControl bool
ProjectID uint64
UniqueHost string
RootDirectory string
DefaultDomainRedirect string // User can default all traffic to single domain
ServingType string // Serving type being used, like `zip`
Prefix string // Project prefix, for example, /my/project in group.gitlab.io/my/project/index.html
Path string // Path is an internal and serving-specific location of a document
SHA256 string
IsNamespaceProject bool // IsNamespaceProject is DEPRECATED, see https://gitlab.com/gitlab-org/gitlab-pages/issues/272
IsHTTPSOnly bool
HasAccessControl bool
ProjectID uint64
UniqueHost string
RootDirectory string
PrimaryDomain string // User can default all traffic to single domain
}
......@@ -2,14 +2,14 @@ package api
// LookupPath represents a lookup path for a virtual domain
type LookupPath struct {
ProjectID int `json:"project_id,omitempty"`
AccessControl bool `json:"access_control,omitempty"`
HTTPSOnly bool `json:"https_only,omitempty"`
Prefix string `json:"prefix,omitempty"`
Source Source `json:"source,omitempty"`
UniqueHost string `json:"unique_host,omitempty"`
RootDirectory string `json:"root_directory,omitempty"`
DefaultDomainRedirect string `json:"default_domain_redirect,omitempty"`
ProjectID int `json:"project_id,omitempty"`
AccessControl bool `json:"access_control,omitempty"`
HTTPSOnly bool `json:"https_only,omitempty"`
Prefix string `json:"prefix,omitempty"`
Source Source `json:"source,omitempty"`
UniqueHost string `json:"unique_host,omitempty"`
RootDirectory string `json:"root_directory,omitempty"`
PrimaryDomain string `json:"primary_domain,omitempty"`
}
// Source describes GitLab Page serving variant
......
......@@ -22,17 +22,17 @@ var (
// https://gitlab.com/gitlab-org/gitlab-pages/issues/272
func fabricateLookupPath(size int, lookup api.LookupPath) *serving.LookupPath {
return &serving.LookupPath{
ServingType: lookup.Source.Type,
Path: lookup.Source.Path,
SHA256: lookup.Source.SHA256,
Prefix: lookup.Prefix,
IsNamespaceProject: (lookup.Prefix == "/" && size > 1),
IsHTTPSOnly: lookup.HTTPSOnly,
HasAccessControl: lookup.AccessControl,
ProjectID: uint64(lookup.ProjectID),
UniqueHost: lookup.UniqueHost,
RootDirectory: lookup.RootDirectory,
DefaultDomainRedirect: lookup.DefaultDomainRedirect,
ServingType: lookup.Source.Type,
Path: lookup.Source.Path,
SHA256: lookup.Source.SHA256,
Prefix: lookup.Prefix,
IsNamespaceProject: (lookup.Prefix == "/" && size > 1),
IsHTTPSOnly: lookup.HTTPSOnly,
HasAccessControl: lookup.AccessControl,
ProjectID: uint64(lookup.ProjectID),
UniqueHost: lookup.UniqueHost,
RootDirectory: lookup.RootDirectory,
PrimaryDomain: lookup.PrimaryDomain,
}
}
......
......@@ -9,7 +9,7 @@ import (
"gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers"
)
func TestDefaultDomainRedirect(t *testing.T) {
func TestPrimaryDomain(t *testing.T) {
RunPagesProcess(t)
tests := []struct {
......@@ -20,14 +20,14 @@ func TestDefaultDomainRedirect(t *testing.T) {
httpStatus int
}{
{
name: "when project has a default domain redirect",
name: "when project has a primary domain",
requestDomain: "redirect.gitlab-example.com",
requestPath: "/project1",
redirectURL: "https://default.gitlab-example.com",
httpStatus: http.StatusPermanentRedirect,
},
{
name: "when project has a default domain redirect with subpath",
name: "when project has a primary domain with subpath",
requestDomain: "redirect.gitlab-example.com",
requestPath: "/project1/subpath/index.html",
redirectURL: "https://default.gitlab-example.com/subpath/index.html",
......@@ -41,19 +41,19 @@ func TestDefaultDomainRedirect(t *testing.T) {
httpStatus: http.StatusPermanentRedirect,
},
{
name: "when default domain redirect url is malformed",
name: "when primary domain url is malformed",
requestDomain: "redirect.gitlab-example.com",
requestPath: "/with-malformed-default-domain-redirect-url/",
httpStatus: http.StatusOK,
},
{
name: "when already on the default domain",
name: "when already on the primary domain",
requestDomain: "default.gitlab-example.com",
requestPath: "/",
httpStatus: http.StatusOK,
},
{
name: "when project does not have a default domain redirect",
name: "when project does not have a primary domain",
requestDomain: "custom.gitlab-example.com",
requestPath: "/no-redirect/",
httpStatus: http.StatusOK,
......
......@@ -42,13 +42,13 @@ type Responses map[string]Response
// A Response is the struct responsible for creating a project LookupPath.
type Response struct {
projectID int
accessControl bool
httpsOnly bool
pathOnDisk string // base directory is gitlab-pages/shared/pages
uniqueHost string
rootDirectory string
defaultDomainRedirect string
projectID int
accessControl bool
httpsOnly bool
pathOnDisk string // base directory is gitlab-pages/shared/pages
uniqueHost string
rootDirectory string
primaryDomain string
}
func (responses Responses) virtualDomain(host, wd string) api.VirtualDomain {
......@@ -82,13 +82,13 @@ func (response Response) lookupPath(prefix, wd string) api.LookupPath {
sha := hex.EncodeToString(sum[:])
return api.LookupPath{
Prefix: prefix,
ProjectID: response.projectID,
AccessControl: response.accessControl,
HTTPSOnly: response.httpsOnly,
UniqueHost: response.uniqueHost,
RootDirectory: response.rootDirectory,
DefaultDomainRedirect: response.defaultDomainRedirect,
Prefix: prefix,
ProjectID: response.projectID,
AccessControl: response.accessControl,
HTTPSOnly: response.httpsOnly,
UniqueHost: response.uniqueHost,
RootDirectory: response.rootDirectory,
PrimaryDomain: response.primaryDomain,
Source: api.Source{
Type: "zip",
Path: sourcePath,
......@@ -375,22 +375,22 @@ var apiResponses = APIResponses{
},
"redirect.gitlab-example.com": {
"/project1": {
defaultDomainRedirect: "https://default.gitlab-example.com",
pathOnDisk: "group/project",
primaryDomain: "https://default.gitlab-example.com",
pathOnDisk: "group/project",
},
"/subgroup1/subgroup2/project1": {
defaultDomainRedirect: "https://default.gitlab-example.com",
pathOnDisk: "group/project",
primaryDomain: "https://default.gitlab-example.com",
pathOnDisk: "group/project",
},
"/with-malformed-default-domain-redirect-url": {
defaultDomainRedirect: "://invalid-url",
pathOnDisk: "group/project",
primaryDomain: "://invalid-url",
pathOnDisk: "group/project",
},
},
"default.gitlab-example.com": {
"/": {
defaultDomainRedirect: "https://default.gitlab-example.com",
pathOnDisk: "group/project",
primaryDomain: "https://default.gitlab-example.com",
pathOnDisk: "group/project",
},
},
"custom.gitlab-example.com": {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment