diff options
| -rw-r--r-- | Makefile | 35 | ||||
| -rw-r--r-- | authz.conf | 46 | ||||
| -rw-r--r-- | build/relpath.awk | 79 |
3 files changed, 136 insertions, 24 deletions
@@ -36,6 +36,18 @@ $(PROG): $(OBJS) src/%.o: src/%.c src/authz.h $(CC) $(STD) $(WARN) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< +# AddressSanitizer/UBSan build of everything, so that the behavioural suite - +# which is what actually exercises the editing code paths - runs against them: +# +# make clean && make sanitizer +# +# Leaks are checked, which is why every command frees what it read: a program +# that exits in a millisecond does not need the memory back, but it does need +# the leak report to stay empty, or the one real leak hides among forty. +sanitizer: CFLAGS := -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer +sanitizer: LDFLAGS := -fsanitize=address,undefined +sanitizer: test + # The unit test links the parts it tests, not the command line, so that a # broken argument parser cannot hide a working matcher (or the other way round). $(UNIT): tests/unit.c src/util.o src/glob.o src/policy.o @@ -47,14 +59,27 @@ test: $(PROG) $(UNIT) $(UNIT) AUTHZ=./$(PROG) sh tests/run.sh +# The hook is a symlink to the program, named 'pre-receive', because that name +# is what core.hooksPath looks for and what selects authorize mode inside the +# binary. The link is relative so that a staged install, an unpacked prefix and +# the image built from either all resolve to the binary that came with them +# rather than to whatever lives at the same absolute path on the reading side. +# +# The policy is configuration: an existing one is an operator's file, so it is +# kept and the new one arrives beside it as authz.conf.new. install: $(PROG) install -d $(DESTDIR)$(BINDIR) $(DESTDIR)$(HOOKDIR) $(DESTDIR)$(SYSCONFDIR) install -m 0755 $(PROG) $(DESTDIR)$(BINDIR)/$(PROG) - install -m 0644 authz.conf $(DESTDIR)$(SYSCONFDIR)/authz.conf - rm -f $(DESTDIR)$(HOOKDIR)/pre-receive - cd $(DESTDIR)$(HOOKDIR) && \ - ln -s "`awk -v from='$(SHAREDIR)/hooks' -v to='$(BINDIR)/$(PROG)' \ - -f build/relpath.awk`" pre-receive + @if [ -e $(DESTDIR)$(SYSCONFDIR)/authz.conf ]; then \ + install -m 0644 authz.conf $(DESTDIR)$(SYSCONFDIR)/authz.conf.new; \ + echo " kept $(DESTDIR)$(SYSCONFDIR)/authz.conf; new example is authz.conf.new"; \ + else \ + install -m 0644 authz.conf $(DESTDIR)$(SYSCONFDIR)/authz.conf; \ + fi + link=`awk -v from='$(HOOKDIR)' -v to='$(BINDIR)/$(PROG)' -f build/relpath.awk`; \ + rm -f $(DESTDIR)$(HOOKDIR)/pre-receive; \ + ln -s "$$link" $(DESTDIR)$(HOOKDIR)/pre-receive; \ + echo " $(HOOKDIR)/pre-receive -> $$link" uninstall: rm -f $(DESTDIR)$(BINDIR)/$(PROG) $(DESTDIR)$(HOOKDIR)/pre-receive diff --git a/authz.conf b/authz.conf new file mode 100644 index 0000000..afea7d2 --- /dev/null +++ b/authz.conf @@ -0,0 +1,46 @@ +# git-host access policy. +# +# One file, read by the receive hook on every push and by nothing that serves +# reads. The deployed copy is mounted read-only - a ConfigMap - so changes are +# made to the copy in git and rolled out; `git-authz grant`, `revoke` and +# `protect` edit a source copy and refuse, with an explanation, when pointed at +# the read-only one. +# +# <user> <repo> <write|force|admin> [ <ref pattern> ... ] +# +# Levels stack: admin implies force implies write, and every rule for a user +# counts, so the strongest match wins. There is no 'deny': access is taken +# away by deleting the rule that gave it. +# +# A ref pattern is a glob in which '*' crosses '/'. "main", "heads/main" and +# "refs/heads/main" name the same branch; a bare name is always a branch, so a +# tag has to be written "refs/tags/v1" or "tags/v1". With no patterns at all +# the rule covers the whole repository, refs included. +# +# '@all' means everyone this policy already names somewhere. It is not a +# wildcard for anonymous users: nobody reaches it who has no rule of their own. + +# What costs an extra level to move. A write on these needs force, a force +# needs admin, and refs outside refs/heads and refs/tags need admin regardless. +set protected-refs refs/heads/main refs/tags/* + +# The account for break-glass and for the refs nobody else should touch: it is +# unscoped, so it covers the whole repository including refs/notes and friends. +alice * admin + +# Default for everyone the policy knows: their own branches, anywhere. Reads +# are open, so this is about writing, and there is no rule here to be the +# fall-through for someone the policy has never mentioned. +@all * write refs/heads/* + +# Contributor on team: feature branches only. +bob team write refs/heads/feature/* + +# Senior on team: may rewrite his feature branches and delete them. +carol team force refs/heads/feature/* + +# Maintainer of team: everything, including main, tags, and deletions. +dave team admin + +# Team A's repository, with an unrelated ACL. +erin secrets admin diff --git a/build/relpath.awk b/build/relpath.awk index 6c9a031..ea5ca99 100644 --- a/build/relpath.awk +++ b/build/relpath.awk @@ -1,22 +1,63 @@ -# The path from one directory to a file, expressed relative to that directory. +# relpath.awk - the path from one directory to one file, relative to the +# directory. Used by `make install' for the hook symlink: # -# The pre-receive hook is this same binary reached through a symlink, because -# that is how git's core.hooksPath finds it. An absolute symlink would be a -# hook that works at exactly one prefix; a relative one survives a staged -# install (DESTDIR), a prefix built elsewhere, and a container image that moves -# /usr/local around - and `ls -l` still says what the hook is. +# awk -v from=/usr/local/share/git-host/hooks -v to=/usr/local/bin/git-authz \ +# -f build/relpath.awk -> ../../../bin/git-authz +# +# A relative link survives being moved: a staged install under DESTDIR, an +# unpacked prefix, a container image built somewhere else. An absolute one +# points at whatever happens to live at that path on the machine reading it, +# which during a staged install is the wrong answer and after a failed one is a +# broken link nothing explains. +# +# POSIX awk, so busybox awk and mawk and gawk all agree. + +function split_path(p, out, n, parts, i, depth) { + n = split(p, parts, "/") + depth = 0 + for (i = 1; i <= n; i++) { + if (parts[i] == "" || parts[i] == ".") + continue # empty runs, and "." changes nothing + if (parts[i] == "..") { + if (depth > 0) + depth-- # already inside a component we know + else + out[++depth] = ".." # above the root we cannot see into + continue + } + out[++depth] = parts[i] + } + return depth +} + BEGIN { - n = split(from, f, "/") - m = split(to, t, "/") - i = 1 - while (i <= n && i <= m && f[i] == t[i]) - i++ - rel = "" - for (k = i; k <= n; k++) - rel = rel (rel == "" ? "" : "/") ".." - for (k = i; k <= m; k++) - rel = rel (rel == "" ? "" : "/") t[k] - if (rel == "") - rel = "." - print rel + if (from == "" || to == "") { + print "relpath: needs -v from=DIR -v to=FILE" > "/dev/stderr" + exit 64 + } + + nf = split_path(from, f) + nt = split_path(to, t) + + common = 0 + while (common < nf && common < nt && f[common + 1] == t[common + 1]) + common++ + + # Anything above a ".." we could not resolve has to be answered with an + # absolute path: there is no relative route into a directory we cannot name. + if ((nf > common && f[common + 1] == "..") || + (nt > common && t[common + 1] == "..")) { + print to + exit 0 + } + + res = "" + for (i = common + 1; i <= nf; i++) + res = res ".." "/" + for (i = common + 1; i <= nt; i++) + res = res t[i] "/" + sub(/\/$/, "", res) + if (res == "") + res = "." # the file is the directory's own name + print res } |
