showcase
- v0.2.4
- 37 commits
- 1 branch
- 6 tags
- updated
- CI 2/2
config.go
4.3 KB · 133 lines
1// Package config loads and validates config.json.2package config34import (5 "bytes"6 "encoding/json"7 "fmt"8 "net/netip"9 "net/url"10 "os"11 "regexp"12 "slices"13 "strings"14)1516type Config struct {17 GiteaURL string `json:"gitea_url"`18 User string `json:"user"`19 SiteURL string `json:"site_url"`20 // BasePath ("/projects") puts the pages under one path segment, so a21 // proxy can serve them at danieldaum.net/projects. Clone, go get, docker22 // pull and /healthz stay at the root of site_url. Empty means the root.23 BasePath string `json:"base_path"`24 Repos []string `json:"repos"`25 Images []string `json:"images"`26 // Featured repos for the profile, in order (up to 6). Empty means the27 // most recently updated ones.28 Featured []string `json:"featured"`29 MaxCommits int `json:"max_commits"`30 Theme string `json:"theme"` // saturn palette, default moss31 // Proxies (IPs or CIDRs) whose X-Forwarded-For is believed, for logs only.32 TrustedProxies []string `json:"trusted_proxies"`33 // GiteaTokenFile holds a Gitea token for the packages API only (see34 // gitea.Client.SetToken). A file, so the token stays out of config.json.35 GiteaTokenFile string `json:"gitea_token_file"`36 GiteaToken string `json:"-"` // read from GiteaTokenFile; never written out37}3839var (40 nameRe = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]*$`)41 baseRe = regexp.MustCompile(`^(/[a-z0-9][a-z0-9-]*)?$`)42 imageRe = regexp.MustCompile(`^[a-z0-9]+([._-][a-z0-9]+)*(/[a-z0-9]+([._-][a-z0-9]+)*)*$`)43 // Top-level names the site or router uses for itself.44 reserved = []string{"static", "v2", "healthz", "api", "user", "repos", "index.html", "404.html"}45 themes = []string{"moss", "ink", "wine", "plum", "teal", "flexoki"}46)4748func Load(path string) (*Config, error) {49 b, err := os.ReadFile(path)50 if err != nil {51 return nil, err52 }53 var c Config54 dec := json.NewDecoder(bytes.NewReader(b))55 dec.DisallowUnknownFields()56 if err := dec.Decode(&c); err != nil {57 return nil, fmt.Errorf("config: %w", err)58 }59 if err := c.validate(); err != nil {60 return nil, fmt.Errorf("config: %w", err)61 }62 return &c, nil63}6465func (c *Config) validate() error {66 c.GiteaURL = strings.TrimRight(c.GiteaURL, "/")67 c.SiteURL = strings.TrimRight(c.SiteURL, "/")68 for _, u := range []string{c.GiteaURL, c.SiteURL} {69 p, err := url.Parse(u)70 if err != nil || (p.Scheme != "http" && p.Scheme != "https") || p.Host == "" {71 return fmt.Errorf("bad url %q", u)72 }73 }74 if !nameRe.MatchString(c.User) {75 return fmt.Errorf("bad user %q", c.User)76 }77 // Gitea matches user names in any case, but image refs must be lowercase78 c.User = strings.ToLower(c.User)79 seen := map[string]bool{}80 for _, r := range c.Repos {81 l := strings.ToLower(r)82 if !nameRe.MatchString(r) || strings.HasSuffix(l, ".git") || slices.Contains(reserved, l) || seen[l] {83 return fmt.Errorf("bad or duplicate repo name %q", r)84 }85 seen[l] = true86 }87 for _, f := range c.Featured {88 if !c.HasRepo(f) {89 return fmt.Errorf("featured repo %q is not in repos", f)90 }91 }92 for _, im := range c.Images {93 if !imageRe.MatchString(im) {94 return fmt.Errorf("bad image name %q", im)95 }96 }97 for _, p := range c.TrustedProxies {98 if _, err := netip.ParsePrefix(p); err != nil {99 if _, err := netip.ParseAddr(p); err != nil {100 return fmt.Errorf("bad trusted proxy %q", p)101 }102 }103 }104 if b := strings.TrimPrefix(c.BasePath, "/"); !baseRe.MatchString(c.BasePath) || slices.Contains(reserved, b) || seen[b] {105 return fmt.Errorf("base_path %q must be empty or one lowercase segment like /projects, not a reserved or repo name", c.BasePath)106 }107 if c.Theme == "" {108 c.Theme = "moss"109 }110 if !slices.Contains(themes, c.Theme) {111 return fmt.Errorf("unknown theme %q", c.Theme)112 }113 if c.MaxCommits <= 0 {114 c.MaxCommits = 50115 }116 if c.GiteaTokenFile != "" {117 b, err := os.ReadFile(c.GiteaTokenFile)118 if err != nil {119 return fmt.Errorf("gitea_token_file: %w", err)120 }121 c.GiteaToken = strings.TrimSpace(string(b))122 if c.GiteaToken == "" || strings.ContainsAny(c.GiteaToken, " \t\r\n") {123 return fmt.Errorf("gitea_token_file %s must hold one token", c.GiteaTokenFile)124 }125 }126 return nil127}128129// HasRepo reports whether name is allowlisted. Names are compared exactly.130func (c *Config) HasRepo(name string) bool { return slices.Contains(c.Repos, name) }131132// HasImage reports whether image is allowlisted.133func (c *Config) HasImage(name string) bool { return slices.Contains(c.Images, name) }