diff options
-rw-r--r-- | app.go | 11 | ||||
-rw-r--r-- | app_test.go | 8 | ||||
-rw-r--r-- | internal/source/domains.go | 54 | ||||
-rw-r--r-- | internal/source/domains_test.go | 105 |
4 files changed, 9 insertions, 169 deletions
@@ -35,7 +35,6 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/rejectmethods" "gitlab.com/gitlab-org/gitlab-pages/internal/request" "gitlab.com/gitlab-org/gitlab-pages/internal/serving/disk/zip" - "gitlab.com/gitlab-org/gitlab-pages/internal/source" "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab" "gitlab.com/gitlab-org/gitlab-pages/metrics" ) @@ -50,7 +49,7 @@ var ( type theApp struct { config *cfg.Config - domains *source.Domains + source *gitlab.Gitlab Artifact *artifact.Artifact Auth *auth.Auth Handlers *handlers.Handlers @@ -100,7 +99,7 @@ func (a *theApp) getHostAndDomain(r *http.Request) (string, *domain.Domain, erro } func (a *theApp) domain(ctx context.Context, host string) (*domain.Domain, error) { - return a.domains.GetDomain(ctx, host) + return a.source.GetDomain(ctx, host) } // checkAuthAndServeNotFound performs the auth process if domain can't be found @@ -221,7 +220,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) { - if a.Auth.TryAuthenticate(w, r, a.domains) { + if a.Auth.TryAuthenticate(w, r, a.source) { return } @@ -493,12 +492,12 @@ func (a *theApp) listenMetricsFD(wg *sync.WaitGroup, fd uintptr) { } func runApp(config *cfg.Config) { - domains, err := source.NewDomains(&config.GitLab) + source, err := gitlab.New(&config.GitLab) if err != nil { log.WithError(err).Fatal("could not create domains config source") } - a := theApp{config: config, domains: domains} + a := theApp{config: config, source: source} err = logging.ConfigureLogging(a.config.Log.Format, a.config.Log.Verbose) if err != nil { diff --git a/app_test.go b/app_test.go index 483ffb39..3b476bdb 100644 --- a/app_test.go +++ b/app_test.go @@ -13,7 +13,7 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/config" "gitlab.com/gitlab-org/gitlab-pages/internal/request" - "gitlab.com/gitlab-org/gitlab-pages/internal/source" + "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab" ) func Test_setRequestScheme(t *testing.T) { @@ -92,7 +92,7 @@ func TestHealthCheckMiddleware(t *testing.T) { JWTTokenExpiration: time.Second, } - domains, err := source.NewDomains(&validCfg) + source, err := gitlab.New(&validCfg) require.NoError(t, err) cfg := config.Config{ @@ -102,8 +102,8 @@ func TestHealthCheckMiddleware(t *testing.T) { } app := theApp{ - config: &cfg, - domains: domains, + config: &cfg, + source: source, } handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/internal/source/domains.go b/internal/source/domains.go deleted file mode 100644 index efa613e1..00000000 --- a/internal/source/domains.go +++ /dev/null @@ -1,54 +0,0 @@ -package source - -import ( - "context" - - "gitlab.com/gitlab-org/gitlab-pages/internal/config" - "gitlab.com/gitlab-org/gitlab-pages/internal/domain" - "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab" -) - -// Domains struct wraps the GitLab client to fetch a domain configuration -// TODO: remove/refactor this package https://gitlab.com/gitlab-org/gitlab-pages/-/issues/608 -type Domains struct { - gitlab Source -} - -// NewDomains is a factory method for domains initializing a mutex. It should -// not initialize `dm` as we later check the readiness by comparing it with a -// nil value. -func NewDomains(cfg *config.GitLab) (*Domains, error) { - domains := &Domains{} - if err := domains.setConfigSource(cfg); err != nil { - return nil, err - } - - return domains, nil -} - -// setConfigSource and initialize gitlab source -func (d *Domains) setConfigSource(cfg *config.GitLab) error { - return d.setGitLabClient(cfg) -} - -func (d *Domains) setGitLabClient(cfg *config.GitLab) error { - // We want to notify users about any API issues - // Creating a glClient will start polling connectivity in the background - // and spam errors in log - glClient, err := gitlab.New(cfg) - if err != nil { - return err - } - - d.gitlab = glClient - - return nil -} - -// GetDomain retrieves a domain information from a source. We are using two -// sources here because it allows us to switch behavior and the domain source -// for some subset of domains, to test / PoC the new GitLab Domains Source that -// we plan to use to replace the disk source. -func (d *Domains) GetDomain(ctx context.Context, name string) (*domain.Domain, error) { - return d.gitlab.GetDomain(ctx, name) -} diff --git a/internal/source/domains_test.go b/internal/source/domains_test.go deleted file mode 100644 index c3f0d2cc..00000000 --- a/internal/source/domains_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package source - -import ( - "context" - "testing" - "time" - - "github.com/stretchr/testify/require" - - "gitlab.com/gitlab-org/gitlab-pages/internal/config" - "gitlab.com/gitlab-org/gitlab-pages/internal/domain" -) - -func TestNewDomains(t *testing.T) { - validCfg := config.GitLab{ - InternalServer: "https://gitlab.com", - APISecretKey: []byte("abc"), - ClientHTTPTimeout: time.Second, - JWTTokenExpiration: time.Second, - } - - tests := []struct { - name string - config config.GitLab - expectedErr string - }{ - { - name: "gitlab_source_success", - config: validCfg, - }, - { - name: "gitlab_source_no_url", - config: func() config.GitLab { - cfg := validCfg - cfg.InternalServer = "" - - return cfg - }(), - expectedErr: "GitLab API URL or API secret has not been provided", - }, - { - name: "gitlab_source_no_secret", - config: func() config.GitLab { - cfg := validCfg - cfg.APISecretKey = []byte{} - - return cfg - }(), - expectedErr: "GitLab API URL or API secret has not been provided", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - domains, err := NewDomains(&tt.config) - if tt.expectedErr != "" { - require.EqualError(t, err, tt.expectedErr) - return - } - require.NoError(t, err) - require.NotNil(t, domains.gitlab) - }) - } -} - -func TestGetDomain(t *testing.T) { - t.Run("when requesting an existing domain for gitlab source", func(t *testing.T) { - testDomain := "new-source-test.gitlab.io" - - newSource := NewMockSource() - newSource.On("GetDomain", testDomain). - Return(&domain.Domain{Name: testDomain}, nil). - Once() - defer newSource.AssertExpectations(t) - - domains := newTestDomains(t, newSource) - - domain, err := domains.GetDomain(context.Background(), testDomain) - require.NoError(t, err) - require.NotNil(t, domain) - }) - - t.Run("when requesting a domain that doesn't exist for gitlab source", func(t *testing.T) { - newSource := NewMockSource() - newSource.On("GetDomain", "does-not-exist.test.io"). - Return(nil, nil). - Once() - - defer newSource.AssertExpectations(t) - - domains := newTestDomains(t, newSource) - - domain, err := domains.GetDomain(context.Background(), "does-not-exist.test.io") - require.NoError(t, err) - require.Nil(t, domain) - }) -} - -func newTestDomains(t *testing.T, gitlabSource *MockSource) *Domains { - t.Helper() - - return &Domains{ - gitlab: gitlabSource, - } -} |