BlueGit

timfa bluegit

Files

bluegit / internal / server / setup.go

setup.go

feat: webhooks weren privénetten en de beheerder kiest de datadisk
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
1ab44366 History
setup.go 4.8 KiB Download Raw
1package server
2
3import (
4 "context"
5 "net/http"
6 "os"
7 "strings"
8
9 "gitlab.com/Timfa/bluegit/internal/gitrepo"
10 "gitlab.com/Timfa/bluegit/internal/state"
11)
12
13const noStorageMsg = "Opslag is nog niet ingesteld. De beheerder kiest een pad in de webinterface.\n"
14
15func allowsWithoutStorage(r *http.Request) bool {
16 switch r.URL.Path {
17 case "/healthz", "/login", "/auth/callback", "/logout", "/setup":
18 return true
19 }
20 return strings.HasPrefix(r.URL.Path, "/assets/")
21}
22
23func isGitPath(path string) bool {
24 return strings.HasSuffix(path, "/info/refs") ||
25 strings.HasSuffix(path, "/git-upload-pack") ||
26 strings.HasSuffix(path, "/git-receive-pack")
27}
28
29func (s *Server) gate(next http.Handler) http.Handler {
30 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
31 if allowsWithoutStorage(r) || s.StoragePath() != "" {
32 next.ServeHTTP(w, r)
33 return
34 }
35 if isGitPath(r.URL.Path) || strings.HasPrefix(r.URL.Path, "/api/") {
36 http.Error(w, noStorageMsg, http.StatusServiceUnavailable)
37 return
38 }
39 if p := s.who(r); p.OK && p.User.InstanceAdmin {
40 http.Redirect(w, r, "/setup", http.StatusFound)
41 return
42 }
43 next.ServeHTTP(w, r)
44 })
45}
46
47// StoragePath is de datadisk. Leeg zolang de beheerder er geen heeft gekozen.
48func (s *Server) StoragePath() string {
49 s.storageMu.RLock()
50 set, path := s.storageSet, s.storage
51 s.storageMu.RUnlock()
52 if set {
53 return path
54 }
55 s.storageMu.Lock()
56 defer s.storageMu.Unlock()
57 if !s.storageSet {
58 s.storage = s.Cfg.StoragePath
59 s.storageSet = true
60 }
61 return s.storage
62}
63
64// AdoptStorage zet het pad dat bij het starten al geldig was.
65func (s *Server) AdoptStorage(path string) {
66 s.storageMu.Lock()
67 s.storage = path
68 s.storageSet = true
69 s.storageMu.Unlock()
70 if path != "" && s.AfterStorage != nil {
71 s.AfterStorage(path)
72 }
73}
74
75func (s *Server) setupPage(w http.ResponseWriter, r *http.Request) {
76 if _, ok := s.adminOnly(w, r); !ok {
77 return
78 }
79 s.renderSetup(w, r, "", "")
80}
81
82func (s *Server) setupSave(w http.ResponseWriter, r *http.Request) {
83 if _, ok := s.adminOnly(w, r); !ok || !s.csrfOK(w, r) {
84 return
85 }
86 if s.StateDir == "" {
87 s.renderSetup(w, r, "De state-map ontbreekt, dus het pad kan niet bewaard worden.", r.FormValue("path"))
88 return
89 }
90 info, err := state.Inspect(r.FormValue("path"))
91 if err != nil {
92 s.renderSetup(w, r, err.Error(), strings.TrimSpace(r.FormValue("path")))
93 return
94 }
95 if same, err := samePath(s.StateDir, info.Path); err != nil {
96 s.renderSetup(w, r, err.Error(), info.Path)
97 return
98 } else if same {
99 s.renderSetup(w, r, "De state-map is niet de datadisk. Kies de schijf waar de repositories komen.", info.Path)
100 return
101 }
102 previous := s.StoragePath()
103 if err := state.Write(s.StateDir, info.Path); err != nil {
104 s.renderSetup(w, r, err.Error(), info.Path)
105 return
106 }
107 s.AdoptStorage(info.Path)
108 s.RefreshHooks()
109 notice := "Opslag ingesteld op " + info.Path + ". " + state.Format(info.Free) + " vrij."
110 if previous != "" && previous != info.Path {
111 notice = "Opslagpad gewijzigd naar " + info.Path + ". Bestaande data is niet verplaatst. " + state.Format(info.Free) + " vrij."
112 }
113 s.redirect(w, r, "/", notice, "")
114}
115
116func samePath(stateDir, path string) (bool, error) {
117 if stateDir == "" {
118 return false, nil
119 }
120 return strings.TrimRight(stateDir, "/") == strings.TrimRight(path, "/"), nil
121}
122
123func (s *Server) adminOnly(w http.ResponseWriter, r *http.Request) (bool, bool) {
124 u, ok := s.loginRequired(w, r)
125 if !ok {
126 return false, false
127 }
128 if !u.InstanceAdmin {
129 http.Error(w, "alleen de installatiebeheerder stelt de opslag in", http.StatusForbidden)
130 return false, false
131 }
132 return true, true
133}
134
135func (s *Server) renderSetup(w http.ResponseWriter, r *http.Request, problem, prefill string) {
136 v := s.view(w, r, "Opslag")
137 if problem != "" {
138 v.Err = problem
139 }
140 v.MinFree = state.Format(state.MinFreeBytes)
141 v.StoragePath = strings.TrimSpace(prefill)
142 if v.StoragePath == "" {
143 v.StoragePath = s.StoragePath()
144 }
145 if v.StoragePath == "" && s.StateDir != "" {
146 if saved, err := state.Read(s.StateDir); err == nil {
147 v.StoragePath = saved
148 }
149 }
150 if v.StoragePath != "" {
151 info, err := state.Inspect(v.StoragePath)
152 if info.Total > 0 {
153 v.StorageFree = state.Format(info.Free)
154 v.StorageTotal = state.Format(info.Total)
155 }
156 if err != nil && v.Err == "" {
157 v.Err = err.Error()
158 }
159 }
160 s.render(w, "setup", v)
161}
162
163// RefreshHooks zet de pre-receive-hook opnieuw in elke repository die op de datadisk staat.
164func (s *Server) RefreshHooks() {
165 root := s.StoragePath()
166 if root == "" || s.HookBin == "" || s.Store == nil {
167 return
168 }
169 repos, err := s.Store.AllRepos(context.Background())
170 if err != nil {
171 return
172 }
173 for _, repo := range repos {
174 dir := gitrepo.Dir(root, repo.OwnerPath, repo.Name)
175 if _, err := os.Stat(dir); err != nil {
176 continue
177 }
178 if err := gitrepo.InstallHook(dir, s.HookBin); err != nil && s.Log != nil {
179 s.Log.Error("hook", "repo", repo.FullPath(), "err", err)
180 }
181 }
182}