diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-09-20 15:18:12 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-09-20 15:18:12 +0300 |
commit | 529988bfe243a610ea5414a48567585c3c3337aa (patch) | |
tree | 6a2a69df82c20f47ec826689ad29ee08da225342 /internal/domain | |
parent | c5ea1f1db89bc5bb53714b071ebbd167c28ab0e7 (diff) |
Make it more explicit how domain config is structured
Diffstat (limited to 'internal/domain')
-rw-r--r-- | internal/domain/config.go (renamed from internal/domain/domain_config.go) | 13 | ||||
-rw-r--r-- | internal/domain/config_test.go (renamed from internal/domain/domain_config_test.go) | 22 | ||||
-rw-r--r-- | internal/domain/domain.go | 2 | ||||
-rw-r--r-- | internal/domain/domain_test.go | 18 | ||||
-rw-r--r-- | internal/domain/group.go | 7 | ||||
-rw-r--r-- | internal/domain/map.go | 10 | ||||
-rw-r--r-- | internal/domain/map_test.go | 4 | ||||
-rw-r--r-- | internal/domain/project.go | 1 |
8 files changed, 41 insertions, 36 deletions
diff --git a/internal/domain/domain_config.go b/internal/domain/config.go index 2ab2ce6c..0836eedd 100644 --- a/internal/domain/domain_config.go +++ b/internal/domain/config.go @@ -7,7 +7,8 @@ import ( "strings" ) -type domainConfig struct { +// Config represents a custom domain config +type Config struct { Domain string Certificate string Key string @@ -16,14 +17,16 @@ type domainConfig struct { AccessControl bool `json:"access_control"` } -type domainsConfig struct { - Domains []domainConfig +// MultiConfig represents a group of custom domain configs +type MultiConfig struct { + Domains []Config HTTPSOnly bool `json:"https_only"` ID uint64 `json:"id"` AccessControl bool `json:"access_control"` } -func (c *domainConfig) Valid(rootDomain string) bool { +// Valid validates a custom domain config for a root domain +func (c *Config) Valid(rootDomain string) bool { if c.Domain == "" { return false } @@ -34,7 +37,7 @@ func (c *domainConfig) Valid(rootDomain string) bool { return !strings.HasSuffix(domain, rootDomain) } -func (c *domainsConfig) Read(group, project string) (err error) { +func (c *MultiConfig) Read(group, project string) (err error) { configFile, err := os.Open(filepath.Join(group, project, "config.json")) if err != nil { return err diff --git a/internal/domain/domain_config_test.go b/internal/domain/config_test.go index 8cdcdeaa..a78c72cf 100644 --- a/internal/domain/domain_config_test.go +++ b/internal/domain/config_test.go @@ -14,25 +14,25 @@ const invalidConfig = `{"Domains":{}}` const validConfig = `{"Domains":[{"Domain":"test"}]}` func TestDomainConfigValidness(t *testing.T) { - d := domainConfig{} + d := Config{} require.False(t, d.Valid("gitlab.io")) - d = domainConfig{Domain: "test"} + d = Config{Domain: "test"} require.True(t, d.Valid("gitlab.io")) - d = domainConfig{Domain: "test"} + d = Config{Domain: "test"} require.True(t, d.Valid("gitlab.io")) - d = domainConfig{Domain: "test.gitlab.io"} + d = Config{Domain: "test.gitlab.io"} require.False(t, d.Valid("gitlab.io")) - d = domainConfig{Domain: "test.test.gitlab.io"} + d = Config{Domain: "test.test.gitlab.io"} require.False(t, d.Valid("gitlab.io")) - d = domainConfig{Domain: "test.testgitlab.io"} + d = Config{Domain: "test.testgitlab.io"} require.True(t, d.Valid("gitlab.io")) - d = domainConfig{Domain: "test.GitLab.Io"} + d = Config{Domain: "test.GitLab.Io"} require.False(t, d.Valid("gitlab.io")) } @@ -40,26 +40,26 @@ func TestDomainConfigRead(t *testing.T) { cleanup := setUpTests(t) defer cleanup() - d := domainsConfig{} + d := MultiConfig{} err := d.Read("test-group", "test-project") require.Error(t, err) os.MkdirAll(filepath.Dir(configFile), 0700) defer os.RemoveAll("test-group") - d = domainsConfig{} + d = MultiConfig{} err = d.Read("test-group", "test-project") require.Error(t, err) err = ioutil.WriteFile(configFile, []byte(invalidConfig), 0600) require.NoError(t, err) - d = domainsConfig{} + d = MultiConfig{} err = d.Read("test-group", "test-project") require.Error(t, err) err = ioutil.WriteFile(configFile, []byte(validConfig), 0600) require.NoError(t, err) - d = domainsConfig{} + d = MultiConfig{} err = d.Read("test-group", "test-project") require.NoError(t, err) } diff --git a/internal/domain/domain.go b/internal/domain/domain.go index efea118a..0c464628 100644 --- a/internal/domain/domain.go +++ b/internal/domain/domain.go @@ -44,7 +44,7 @@ type Domain struct { // custom domains: projectName string - config *domainConfig + config *Config certificate *tls.Certificate certificateError error diff --git a/internal/domain/domain_test.go b/internal/domain/domain_test.go index 1f200afa..41a0c401 100644 --- a/internal/domain/domain_test.go +++ b/internal/domain/domain_test.go @@ -82,7 +82,7 @@ func TestDomainServeHTTP(t *testing.T) { testDomain := &Domain{ group: Group{name: "group"}, projectName: "project2", - config: &domainConfig{ + config: &Config{ Domain: "test.domain.com", }, } @@ -110,7 +110,7 @@ func TestIsHTTPSOnly(t *testing.T) { domain: &Domain{ group: Group{name: "group"}, projectName: "project", - config: &domainConfig{HTTPSOnly: true}, + config: &Config{HTTPSOnly: true}, }, url: "http://custom-domain", expected: true, @@ -120,7 +120,7 @@ func TestIsHTTPSOnly(t *testing.T) { domain: &Domain{ group: Group{name: "group"}, projectName: "project", - config: &domainConfig{HTTPSOnly: false}, + config: &Config{HTTPSOnly: false}, }, url: "http://custom-domain", expected: false, @@ -219,7 +219,7 @@ func TestHasAcmeChallenge(t *testing.T) { domain: &Domain{ group: Group{name: "group.acme"}, projectName: "with.acme.challenge", - config: &domainConfig{HTTPSOnly: true}, + config: &Config{HTTPSOnly: true}, }, token: "existingtoken", expected: true, @@ -229,7 +229,7 @@ func TestHasAcmeChallenge(t *testing.T) { domain: &Domain{ group: Group{name: "group.acme"}, projectName: "with.acme.challenge", - config: &domainConfig{HTTPSOnly: true}, + config: &Config{HTTPSOnly: true}, }, token: "foldertoken", expected: true, @@ -239,7 +239,7 @@ func TestHasAcmeChallenge(t *testing.T) { domain: &Domain{ group: Group{name: "group.acme"}, projectName: "with.acme.challenge", - config: &domainConfig{HTTPSOnly: true}, + config: &Config{HTTPSOnly: true}, }, token: "notexistingtoken", expected: false, @@ -398,7 +398,7 @@ func TestDomain404ServeHTTP(t *testing.T) { testDomain := &Domain{ group: Group{name: "group.404"}, projectName: "domain.404", - config: &domainConfig{ + config: &Config{ Domain: "domain.404.com", }, } @@ -433,7 +433,7 @@ func TestDomainNoCertificate(t *testing.T) { testDomain := &Domain{ group: Group{name: "group"}, projectName: "project2", - config: &domainConfig{ + config: &Config{ Domain: "test.domain.com", }, } @@ -451,7 +451,7 @@ func TestDomainCertificate(t *testing.T) { testDomain := &Domain{ group: Group{name: "group"}, projectName: "project2", - config: &domainConfig{ + config: &Config{ Domain: "test.domain.com", Certificate: fixture.Certificate, Key: fixture.Key, diff --git a/internal/domain/group.go b/internal/domain/group.go index 7dcae83e..e3424a4d 100644 --- a/internal/domain/group.go +++ b/internal/domain/group.go @@ -5,9 +5,7 @@ import ( "strings" ) -type projects map[string]*Project -type subgroups map[string]*Group - +// Group represents a GitLab group with projects and subgroups type Group struct { name string @@ -18,6 +16,9 @@ type Group struct { projects projects } +type projects map[string]*Project +type subgroups map[string]*Group + func (g *Group) digProjectWithSubpath(parentPath string, keys []string) (*Project, string, string) { if len(keys) >= 1 { head := keys[0] diff --git a/internal/domain/map.go b/internal/domain/map.go index e374bea4..f65f52af 100644 --- a/internal/domain/map.go +++ b/internal/domain/map.go @@ -15,7 +15,7 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/metrics" ) -// Map maps domain names to D instances. +// Map maps domain names to Domain instances. type Map map[string]*Domain type domainsUpdater func(Map) @@ -34,7 +34,7 @@ func (dm Map) updateDomainMap(domainName string, domain *Domain) { dm[domainName] = domain } -func (dm Map) addDomain(rootDomain, groupName, projectName string, config *domainConfig) { +func (dm Map) addDomain(rootDomain, groupName, projectName string, config *Config) { newDomain := &Domain{ group: Group{name: groupName}, projectName: projectName, @@ -89,7 +89,7 @@ func (dm Map) updateGroupDomain(rootDomain, groupName, projectPath string, https dm[domainName] = groupDomain } -func (dm Map) readProjectConfig(rootDomain string, group, projectName string, config *domainsConfig) { +func (dm Map) readProjectConfig(rootDomain string, group, projectName string, config *MultiConfig) { if config == nil { // This is necessary to preserve the previous behaviour where a // group domain is created even if no config.json files are @@ -131,7 +131,7 @@ func readProject(group, parent, projectName string, level int, fanIn chan<- jobR // We read the config.json file _before_ fanning in, because it does disk // IO and it does not need access to the domains map. - config := &domainsConfig{} + config := &MultiConfig{} if err := config.Read(group, projectPath); err != nil { config = nil } @@ -163,7 +163,7 @@ func readProjects(group, parent string, level int, buf []byte, fanIn chan<- jobR type jobResult struct { group string project string - config *domainsConfig + config *MultiConfig } // ReadGroups walks the pages directory and populates dm with all the domains it finds. diff --git a/internal/domain/map_test.go b/internal/domain/map_test.go index 2d61d248..f184672c 100644 --- a/internal/domain/map_test.go +++ b/internal/domain/map_test.go @@ -68,10 +68,10 @@ func TestReadProjects(t *testing.T) { } // Check that multiple domains in the same project are recorded faithfully - exp1 := &domainConfig{Domain: "test.domain.com"} + exp1 := &Config{Domain: "test.domain.com"} require.Equal(t, exp1, dm["test.domain.com"].config) - exp2 := &domainConfig{Domain: "other.domain.com", Certificate: "test", Key: "key"} + exp2 := &Config{Domain: "other.domain.com", Certificate: "test", Key: "key"} require.Equal(t, exp2, dm["other.domain.com"].config) // check subgroups diff --git a/internal/domain/project.go b/internal/domain/project.go index 2d38ff74..d0add00d 100644 --- a/internal/domain/project.go +++ b/internal/domain/project.go @@ -1,5 +1,6 @@ package domain +// Project represents GitLab project settings type Project struct { NamespaceProject bool HTTPSOnly bool |