diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2021-04-29 09:16:58 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2021-05-11 10:28:33 +0300 |
commit | 8ea4cb76586c0841456c5973168cd8451f6c6c0a (patch) | |
tree | f7d276e04078630150d37621f3b9db98e9be58b2 /internal/source | |
parent | fa0a436cb1b514af6e0eeeb0af3f95575025375d (diff) |
Add flag enable-disk
* Adds `enable-disk` flag -> allows to explicitly enable disk, on by default but will turn into off by default from %"14.0"
* When `enable-disk=true` -> serve from either source.Type = file or zip, independently from API response (effectively no change from current behaviour)
* When `enable-disk=false` -> serve 500 if source.Type = file or source.Path.HasPrefix("file://")
* This change is completely independent from -domain-config-source
Changelog: added
Diffstat (limited to 'internal/source')
-rw-r--r-- | internal/source/gitlab/factory.go | 23 | ||||
-rw-r--r-- | internal/source/gitlab/factory_test.go | 31 | ||||
-rw-r--r-- | internal/source/gitlab/gitlab.go | 18 | ||||
-rw-r--r-- | internal/source/gitlab/gitlab_test.go | 2 |
4 files changed, 58 insertions, 16 deletions
diff --git a/internal/source/gitlab/factory.go b/internal/source/gitlab/factory.go index b033a592..77ad63ed 100644 --- a/internal/source/gitlab/factory.go +++ b/internal/source/gitlab/factory.go @@ -1,6 +1,9 @@ package gitlab import ( + "errors" + "fmt" + log "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitlab-pages/internal/serving" @@ -9,6 +12,10 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api" ) +var ( + errDiskDisabled = errors.New("gitlab: disk access is disabled via enable-disk=false") +) + // fabricateLookupPath fabricates a serving LookupPath based on the API LookupPath // `size` argument is DEPRECATED, see // https://gitlab.com/gitlab-org/gitlab-pages/issues/272 @@ -25,14 +32,18 @@ func fabricateLookupPath(size int, lookup api.LookupPath) *serving.LookupPath { } // fabricateServing fabricates serving based on the GitLab API response -func fabricateServing(lookup api.LookupPath) serving.Serving { +func (g *Gitlab) fabricateServing(lookup api.LookupPath) (serving.Serving, error) { source := lookup.Source switch source.Type { case "file": - return local.Instance() + if !g.enableDisk { + return nil, errDiskDisabled + } + + return local.Instance(), nil case "zip": - return zip.Instance() + return zip.Instance(), nil case "serverless": log.Errorf("attempted to fabricate serverless serving for project %d", lookup.ProjectID) @@ -49,9 +60,5 @@ func fabricateServing(lookup api.LookupPath) serving.Serving { // return serving } - return defaultServing() -} - -func defaultServing() serving.Serving { - return local.Instance() + return nil, fmt.Errorf("gitlab: unkown serving source type: %q", source.Type) } diff --git a/internal/source/gitlab/factory_test.go b/internal/source/gitlab/factory_test.go index 46740d35..43157a5a 100644 --- a/internal/source/gitlab/factory_test.go +++ b/internal/source/gitlab/factory_test.go @@ -1,6 +1,7 @@ package gitlab import ( + "fmt" "testing" "github.com/stretchr/testify/require" @@ -32,15 +33,38 @@ func TestFabricateLookupPath(t *testing.T) { func TestFabricateServing(t *testing.T) { t.Run("when lookup path requires disk serving", func(t *testing.T) { + g := Gitlab{ + enableDisk: true, + } + lookup := api.LookupPath{ Prefix: "/", Source: api.Source{Type: "file"}, } + srv, err := g.fabricateServing(lookup) + require.NoError(t, err) + require.IsType(t, &disk.Disk{}, srv) + }) + + t.Run("when lookup path requires disk serving but disk is disabled", func(t *testing.T) { + g := Gitlab{ + enableDisk: false, + } - require.IsType(t, &disk.Disk{}, fabricateServing(lookup)) + lookup := api.LookupPath{ + Prefix: "/", + Source: api.Source{Type: "file"}, + } + srv, err := g.fabricateServing(lookup) + require.EqualError(t, err, errDiskDisabled.Error()) + require.Nil(t, srv) }) t.Run("when lookup path requires serverless serving", func(t *testing.T) { + g := Gitlab{ + enableDisk: true, + } + lookup := api.LookupPath{ Prefix: "/", Source: api.Source{ @@ -58,8 +82,11 @@ func TestFabricateServing(t *testing.T) { }, } + srv, err := g.fabricateServing(lookup) + require.EqualError(t, err, fmt.Sprintf("gitlab: unkown serving source type: %q", lookup.Source.Type)) + // Serverless serving has been deprecated. // require.IsType(t, &serverless.Serverless{}, fabricateServing(lookup)) - require.IsType(t, &disk.Disk{}, fabricateServing(lookup)) + require.Nil(t, srv) }) } diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index fdcbd88f..14788e1d 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -23,9 +23,11 @@ import ( // Gitlab source represent a new domains configuration source. We fetch all the // information about domains from GitLab instance. type Gitlab struct { - client api.Resolver - mu sync.RWMutex - isReady bool + client api.Resolver + mu sync.RWMutex + isReady bool + useLegacyStorage bool + enableDisk bool } // New returns a new instance of gitlab domain source. @@ -36,7 +38,8 @@ func New(cfg *config.GitLab) (*Gitlab, error) { } g := &Gitlab{ - client: cache.NewCache(glClient, &cfg.Cache), + client: cache.NewCache(glClient, &cfg.Cache), + enableDisk: cfg.EnableDisk, } go g.poll(backoff.DefaultInitialInterval, maxPollingTime) @@ -92,8 +95,13 @@ func (g *Gitlab) Resolve(r *http.Request) (*serving.Request, error) { subPath = strings.TrimPrefix(urlPath, lookup.Prefix) } + srv, err := g.fabricateServing(lookup) + if err != nil { + return nil, err + } + return &serving.Request{ - Serving: fabricateServing(lookup), + Serving: srv, LookupPath: fabricateLookupPath(size, lookup), SubPath: subPath}, nil } diff --git a/internal/source/gitlab/gitlab_test.go b/internal/source/gitlab/gitlab_test.go index bd6b2171..4a5f1a7b 100644 --- a/internal/source/gitlab/gitlab_test.go +++ b/internal/source/gitlab/gitlab_test.go @@ -43,7 +43,7 @@ func TestGetDomain(t *testing.T) { func TestResolve(t *testing.T) { client := client.StubClient{File: "client/testdata/test.gitlab.io.json"} - source := Gitlab{client: client} + source := Gitlab{client: client, enableDisk: true} t.Run("when requesting nested group project with root path", func(t *testing.T) { target := "https://test.gitlab.io:443/my/pages/project/" |