internal/hookd/hookd.go

7cc95d1fc1115f22008751a05e9e07156d49d6b0
gitbay/internal/hookd/hookd.go history · blame · raw

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