internal/hookd/hookd.go

ab515bab173c501ad2228f0af8068a8730f8b5ea
gitbay/internal/hookd/hookd.go history · blame · raw

344 lines · 10692 bytes

  1// Package hookd is the unix-socket bridge between git hooks and the daemon.
  2// The hook process (gitbayd in hook mode) computes git facts — it inherits
  3// git's quarantine environment, which the daemon does not see — and sends
  4// them here; the daemon answers with a policy decision.
  5//
  6// pre-receive is two-phase when the repo requires signed commits: the first
  7// response sets NeedCommits, and the hook answers with the raw commit
  8// objects (only the hook can read them out of quarantine) for verification.
  9package hookd
 10
 11import (
 12	"crypto/sha256"
 13	"encoding/json"
 14	"fmt"
 15	"log/slog"
 16	"net"
 17	"os"
 18	"path"
 19	"path/filepath"
 20	"strings"
 21	"time"
 22
 23	"gitbay.org/gitbay/internal/ci"
 24	"gitbay.org/gitbay/internal/config"
 25	"gitbay.org/gitbay/internal/control"
 26	"gitbay.org/gitbay/internal/gitutil"
 27	"gitbay.org/gitbay/internal/policy"
 28	"gitbay.org/gitbay/internal/sig"
 29	"gitbay.org/gitbay/internal/store"
 30)
 31
 32// Env variable names passed to git transport subprocesses and inherited by
 33// hooks.
 34const (
 35	EnvSocket = "GITBAY_HOOK_SOCKET"
 36	EnvRepoID = "GITBAY_REPO_ID"
 37	EnvUserID = "GITBAY_USER_ID"
 38)
 39
 40type Request struct {
 41	Hook    string             `json:"hook"` // pre-receive | post-receive
 42	RepoID  int64              `json:"repo_id"`
 43	UserID  int64              `json:"user_id"`
 44	Updates []policy.RefUpdate `json:"updates"`
 45}
 46
 47type RawCommit struct {
 48	SHA string `json:"sha"`
 49	Raw []byte `json:"raw"`
 50}
 51
 52// CommitsPayload is the hook's second message when NeedCommits was set.
 53type CommitsPayload struct {
 54	Commits []RawCommit `json:"commits"`
 55}
 56
 57type Response struct {
 58	Allow       bool   `json:"allow"`
 59	Message     string `json:"message,omitempty"`
 60	NeedCommits bool   `json:"need_commits,omitempty"`
 61}
 62
 63// SocketPath returns the hook socket location. It prefers the server root,
 64// but unix socket paths are capped (~104 bytes on macOS, 108 on Linux), so
 65// deep roots fall back to a hashed name under the system temp directory.
 66// Hooks receive the chosen path via GITBAY_HOOK_SOCKET, so both sides always
 67// agree.
 68func SocketPath(root string) string {
 69	p := filepath.Join(root, "hook.sock")
 70	if len(p) <= 100 {
 71		return p
 72	}
 73	sum := sha256.Sum256([]byte(root))
 74	return filepath.Join(os.TempDir(), fmt.Sprintf("gitbay-%x.sock", sum[:8]))
 75}
 76
 77type Server struct {
 78	cfg config.Config
 79	st  *store.Store
 80}
 81
 82// Serve listens on the unix socket until the listener is closed.
 83func Serve(cfg config.Config, st *store.Store) (func() error, error) {
 84	path := SocketPath(cfg.Server.Root)
 85	os.Remove(path)
 86	ln, err := net.Listen("unix", path)
 87	if err != nil {
 88		return nil, err
 89	}
 90	s := &Server{cfg: cfg, st: st}
 91	go func() {
 92		for {
 93			conn, err := ln.Accept()
 94			if err != nil {
 95				return
 96			}
 97			go s.handle(conn)
 98		}
 99	}()
100	return ln.Close, nil
101}
102
103func (s *Server) handle(conn net.Conn) {
104	defer conn.Close()
105	dec := json.NewDecoder(conn)
106	enc := json.NewEncoder(conn)
107	var req Request
108	if err := dec.Decode(&req); err != nil {
109		enc.Encode(Response{Allow: false, Message: "bad hook request"})
110		return
111	}
112	switch req.Hook {
113	case "pre-receive":
114		s.preReceive(req, dec, enc)
115	case "post-receive":
116		s.postReceive(req)
117		enc.Encode(Response{Allow: true})
118	default:
119		enc.Encode(Response{Allow: false, Message: fmt.Sprintf("unknown hook %q", req.Hook)})
120	}
121}
122
123func (s *Server) preReceive(req Request, dec *json.Decoder, enc *json.Encoder) {
124	repo, err := s.st.RepoByID(req.RepoID)
125	if err != nil {
126		enc.Encode(Response{Allow: false, Message: "unknown repository"})
127		return
128	}
129	if msg := policy.CheckPush(repo, req.Updates); msg != "" {
130		enc.Encode(Response{Allow: false, Message: msg})
131		return
132	}
133	if !repo.Settings.RequireSignedCommits {
134		enc.Encode(Response{Allow: true})
135		return
136	}
137
138	// Phase two: ask the hook for the incoming commit objects.
139	if err := enc.Encode(Response{Allow: true, NeedCommits: true}); err != nil {
140		return
141	}
142	var payload CommitsPayload
143	if err := dec.Decode(&payload); err != nil {
144		enc.Encode(Response{Allow: false, Message: "bad commits payload"})
145		return
146	}
147	db := store.SigDB{Store: s.st}
148	for _, rc := range payload.Commits {
149		parsed, err := sig.ParseCommit(rc.Raw)
150		if err != nil {
151			enc.Encode(Response{Allow: false, Message: fmt.Sprintf("unparseable commit %s", rc.SHA)})
152			return
153		}
154		res, err := sig.VerifyCommit(db, parsed)
155		if err != nil || res.State != sig.Verified {
156			state := "error"
157			if err == nil {
158				state = string(res.State)
159			}
160			enc.Encode(Response{Allow: false, Message: fmt.Sprintf(
161				"this repository requires signed commits: %.10s is %s", rc.SHA, state)})
162			return
163		}
164	}
165	enc.Encode(Response{Allow: true})
166}
167
168// postReceive applies the cross-repo MR effect: a push to a source branch
169// refreshes refs/merge-requests/N/head in every target repo, by fetching —
170// the target owns the objects, so the MR outlives the fork. This is the only
171// place a hook writes outside its own repository.
172func (s *Server) postReceive(req Request) {
173	pushedRepo, pushedRepoErr := s.st.RepoByID(req.RepoID)
174	for _, u := range req.Updates {
175		// Every ref update is an event webhooks can subscribe to.
176		s.st.RecordEvent(req.RepoID, req.UserID, "push", fmt.Sprintf(
177			`{"ref":%q,"old":%q,"new":%q,"forced":%v,"deleted":%v}`,
178			u.Ref, u.Old, u.New, u.IsForce, u.IsDelete))
179
180		// Any ref update — branch or tag — schedules the push mirrors.
181		s.st.MarkMirrorsDirty(req.RepoID, "push")
182
183		// Tag pushes run the tag-triggered CI jobs.
184		if tag, ok := strings.CutPrefix(u.Ref, "refs/tags/"); ok && !u.IsDelete && pushedRepoErr == nil {
185			s.queueTagBuilds(pushedRepo, req.UserID, tag, u.New)
186		}
187
188		branch, ok := cutHeads(u.Ref)
189		if !ok {
190			continue
191		}
192		// Commits landing on the default branch act on issue references
193		// in their messages (closes #N, plain #N).
194		if pushedRepoErr == nil && branch == pushedRepo.DefaultBranch && !u.IsDelete {
195			dir := control.RepoDir(s.cfg.Server.Root, pushedRepo.OwnerName, pushedRepo.Name)
196			control.ProcessCommitMessages(s.st, dir, pushedRepo, req.UserID, u.Old, u.New)
197			control.RecordLandedCommits(s.st, dir, pushedRepo, u.Old, u.New)
198		}
199		// A branch push with a .gitbay/ci.yml queues one build per job.
200		if pushedRepoErr == nil && !u.IsDelete {
201			s.queueBuilds(pushedRepo, req.UserID, branch, u.New)
202		}
203		if u.IsForce {
204			s.st.Audit(req.UserID, "push.forced", map[string]any{
205				"repo": req.RepoID, "ref": u.Ref, "old": u.Old, "new": u.New})
206		}
207		mrs, err := s.st.OpenMRsBySource(req.RepoID, branch)
208		if err != nil {
209			slog.Error("post-receive: listing MRs", "err", err)
210			continue
211		}
212		srcRepo, err := s.st.RepoByID(req.RepoID)
213		if err != nil {
214			continue
215		}
216		srcDir := control.RepoDir(s.cfg.Server.Root, srcRepo.OwnerName, srcRepo.Name)
217		for _, mr := range mrs {
218			target, err := s.st.RepoByID(mr.RepoID)
219			if err != nil {
220				continue
221			}
222			if u.IsDelete {
223				if mr.State == "open" {
224					s.st.SetMRState(mr.ID, "source_gone")
225				}
226				continue // head ref retained: the diff stays viewable
227			}
228			dstDir := control.RepoDir(s.cfg.Server.Root, target.OwnerName, target.Name)
229			headRef := fmt.Sprintf("refs/merge-requests/%d/head", mr.Number)
230			if err := gitutil.FetchInto(dstDir, srcDir, u.New, headRef); err != nil {
231				slog.Error("post-receive: refreshing MR head", "mr", mr.Number, "err", err)
232				continue
233			}
234			if err := s.st.UpdateMRHead(mr.ID, u.New); err != nil {
235				slog.Error("post-receive: recording MR head", "mr", mr.Number, "err", err)
236			}
237			if srcRepo.ID != target.ID {
238				control.QueueMRBuilds(s.st, s.cfg.Server.Root, s.cfg.Server.SiteURL,
239					target, req.UserID, mr.Number, u.New)
240			}
241			if mr.State == "source_gone" {
242				s.st.SetMRState(mr.ID, "open") // branch came back
243			}
244		}
245	}
246}
247
248// queueBuilds queues the push jobs for a branch update. The work is
249// shared with the merge path, which moves a ref without reaching a hook.
250func (s *Server) queueBuilds(repo store.Repo, userID int64, branch, sha string) {
251	control.QueueBranchBuilds(
252		s.st, s.cfg.Server.Root, s.cfg.Server.SiteURL,
253		repo, userID, branch, sha, time.Now())
254}
255
256// queueTagBuilds runs the jobs whose tag pattern matches a pushed tag.
257// The build records the tag as its ref and the peeled commit as its sha,
258// so statuses land on the commit, not an annotated tag object.
259func (s *Server) queueTagBuilds(repo store.Repo, userID int64, tag, pushed string) {
260	dir := control.RepoDir(s.cfg.Server.Root, repo.OwnerName, repo.Name)
261	sha, err := gitutil.PeelToCommit(dir, pushed)
262	if err != nil {
263		return
264	}
265	raw, err := gitutil.ReadBlob(dir, sha, ci.ConfigPath, 1<<16)
266	if err != nil {
267		return
268	}
269	jobs, err := ci.Parse(raw)
270	if err != nil {
271		return // the branch push already reported ci/config
272	}
273	for _, j := range jobs {
274		if j.Tags == "" {
275			continue
276		}
277		if ok, _ := path.Match(j.Tags, tag); !ok {
278			continue
279		}
280		steps, _ := json.Marshal(j.Steps)
281		n, err := s.st.CreateBuild(repo.ID, j.Name, sha, tag, string(steps), true)
282		if err != nil {
283			slog.Error("queueing tag build", "repo", repo.Path(), "job", j.Name, "err", err)
284			continue
285		}
286		url := fmt.Sprintf("%s/%s/builds/%d", s.cfg.Server.SiteURL, repo.Path(), n)
287		s.st.SetCommitStatus(repo.ID, sha, "ci/"+j.Name, "pending", "tag "+tag, url, userID)
288	}
289}
290
291func cutHeads(ref string) (string, bool) {
292	const p = "refs/heads/"
293	if len(ref) > len(p) && ref[:len(p)] == p {
294		return ref[len(p):], true
295	}
296	return "", false
297}
298
299// Ask sends one request from the hook process to the daemon. commits is
300// called if the daemon asks for the incoming commit objects.
301func Ask(socketPath string, req Request, commits func() (CommitsPayload, error)) (Response, error) {
302	conn, err := net.Dial("unix", socketPath)
303	if err != nil {
304		return Response{}, err
305	}
306	defer conn.Close()
307	enc := json.NewEncoder(conn)
308	dec := json.NewDecoder(conn)
309	if err := enc.Encode(req); err != nil {
310		return Response{}, err
311	}
312	var resp Response
313	if err := dec.Decode(&resp); err != nil {
314		return Response{}, err
315	}
316	if !resp.NeedCommits {
317		return resp, nil
318	}
319	payload, err := commits()
320	if err != nil {
321		return Response{}, err
322	}
323	if err := enc.Encode(payload); err != nil {
324		return Response{}, err
325	}
326	err = dec.Decode(&resp)
327	return resp, err
328}
329
330// WriteHookScripts (re)generates the shared hooks directory. Called at
331// daemon startup so a moved binary self-heals; every repo points here via
332// core.hooksPath.
333func WriteHookScripts(hooksDir, gitbaydPath string) error {
334	if err := os.MkdirAll(hooksDir, 0o755); err != nil {
335		return err
336	}
337	for _, hook := range []string{"pre-receive", "post-receive"} {
338		script := fmt.Sprintf("#!/bin/sh\nexec %q hook %s\n", gitbaydPath, hook)
339		if err := os.WriteFile(filepath.Join(hooksDir, hook), []byte(script), 0o755); err != nil {
340			return err
341		}
342	}
343	return nil
344}