From bc7f4da406fbd802b08985ba583e52f040bdba85 Mon Sep 17 00:00:00 2001
From: dev
Date: Sat, 14 Nov 2026 00:06:35 +0000
Subject: Layout: cmd/, src/, test/, everything built into obj/
The tree had grown sideways: main.c living among the sources it dispatches to,
one test binary covering everything, object files left in the source directory
because the Makefile put them there and, worse, tracked them.
Now the command line is cmd/main.c, the engine is src/ and nothing in it knows
about the command line, and each area of the engine has its own program under
test/ - glob, norm, parse, need, check - each linking the library and reporting
its own count. A failure says which area broke instead of taking the whole run
down with a single "unit: 171" line, which is the only thing a per-file split
buys and it is worth the small duplication the shared test.h absorbs.
Objects go to obj/, test runners to obj/test_runner/, so a build leaves the
tree as it found it. The stale src/*.o and tests/unit that were committed
earlier are gone; they were build output in a source tree and the .gitignore
that should have stopped them arrived after they did.
The Makefile follows the same shape as the other C trees here: directories in
variables, wildcards for the sources, one pattern rule that makes its own
directory, test runners built from the same objects as the binary so they
cannot test a different build than ships, and `test` running every runner with
a foreach so the first failure stops the line. `strict` is `all` with -Werror,
which is what CI now calls instead of passing CFLAGS in from outside; the
sanitizer and install targets are unchanged in what they do.
Behaviour is unchanged: 171 direct checks and 154 behavioural ones, same as
before the move, and make install still stages a working hook symlink.
---
.dockerignore | 4 +-
.gitignore | 7 +-
Makefile | 132 +++++----
README | 29 +-
ci/build.yaml | 2 +-
cmd/main.c | 293 +++++++++++++++++++
container/Dockerfile | 3 +-
src/main.c | 293 -------------------
test/check.c | 171 +++++++++++
test/glob.c | 50 ++++
test/need.c | 47 +++
test/norm.c | 87 ++++++
test/parse.c | 133 +++++++++
test/run.sh | 801 +++++++++++++++++++++++++++++++++++++++++++++++++++
test/test.h | 141 +++++++++
tests/run.sh | 801 ---------------------------------------------------
tests/unit.c | 545 -----------------------------------
17 files changed, 1830 insertions(+), 1709 deletions(-)
create mode 100644 cmd/main.c
delete mode 100644 src/main.c
create mode 100644 test/check.c
create mode 100644 test/glob.c
create mode 100644 test/need.c
create mode 100644 test/norm.c
create mode 100644 test/parse.c
create mode 100755 test/run.sh
create mode 100644 test/test.h
delete mode 100644 tests/run.sh
delete mode 100644 tests/unit.c
diff --git a/.dockerignore b/.dockerignore
index 6a3c2a0..d269209 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,5 +1,5 @@
# Build artifacts. An object file or a test binary built on the machine doing the building is the wrong libc for the image, and the failure it produces is "no such file" for a file that is plainly there.
*.o
+obj/
/git-authz
-/tests/unit
-/tests/tmp/
+/test/tmp/
diff --git a/.gitignore b/.gitignore
index 86d6bb6..9a1136b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
-*.o
+compile_commands.json
+obj/
/git-authz
-/tests/unit
-/tests/tmp/
+/.cache
+/test/tmp
diff --git a/Makefile b/Makefile
index 1f93122..758b968 100644
--- a/Makefile
+++ b/Makefile
@@ -1,72 +1,83 @@
-# git-authz - who may move which refs. C99 and libc, nothing else.
-#
-# make build ./git-authz
-# make test build, then run the unit suite and the behavioural suite
-# make install install into $(DESTDIR)$(PREFIX)
-# make clean
-#
-# No configure script, no cmake, no generated headers: the only portability
-# knob this program needs is CFLAGS. Warnings are variables so that a strict
-# build is a command line and not a build system:
-#
-# make WARN="-Wall -Wextra -Wpedantic -Werror"
+SRC_DIR = src
+CMD_DIR = cmd
+TST_DIR = test
-CC ?= cc
-PREFIX ?= /usr/local
-BINDIR ?= $(PREFIX)/bin
-SHAREDIR ?= $(PREFIX)/share/git-host
-SYSCONFDIR ?= /etc/git-host
-HOOKDIR ?= $(SHAREDIR)/hooks
+OBJ_DIR = obj
-STD ?= -std=c99
-WARN ?= -Wall -Wextra -Wpedantic
-CFLAGS ?= -O2 -g
-CPPFLAGS += -D_POSIX_C_SOURCE=200809L
+PROG = git-authz
-PROG = git-authz
-UNIT = tests/unit
-OBJS = src/util.o src/glob.o src/policy.o src/hook.o src/validate.o src/edit.o \
- src/main.o
+CMD_SRC = $(wildcard $(CMD_DIR)/*.c)
+CMD_OBJ = $(CMD_SRC:%=$(OBJ_DIR)/%.o)
-all: $(PROG)
+TST_SRC = $(wildcard $(TST_DIR)/*.c)
+TST_RUN = $(TST_SRC:$(TST_DIR)/%.c=$(OBJ_DIR)/test_runner/%)
-$(PROG): $(OBJS)
- $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS)
+CC_SRC = $(wildcard $(SRC_DIR)/*.c)
+CC_OBJ = $(CC_SRC:%=$(OBJ_DIR)/%.o)
-src/%.o: src/%.c src/authz.h
- $(CC) $(STD) $(WARN) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
+CFLAGS = -std=c99 -Wall -Wextra -Wpedantic -I$(SRC_DIR)
+CFLAGS += -D_POSIX_C_SOURCE=200809L
+CFLAGS += -g
+LDFLAGS =
-# 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
+$(OBJ_DIR)/%.c.o: %.c
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) -o $@ -c $<
+
+$(PROG): $(CC_OBJ) $(CMD_OBJ)
+ @mkdir -p $(@D)
+ $(CC) $(LDFLAGS) $(CC_OBJ) $(CMD_OBJ) -o $@
+
+$(OBJ_DIR)/test_runner/%: $(OBJ_DIR)/$(TST_DIR)/%.c.o $(CC_OBJ)
+ @mkdir -p $(@D)
+ $(CC) $(LDFLAGS) $(CC_OBJ) $< -o $@
+
+# The test/*.c programs are the pieces tested directly: what a glob matches,
+# what a ref normalises to, what a policy decides and what the refusal says.
+# test/run.sh is the other half - the same binary driven the way a receive hook
+# drives it, with an identity in the environment and "old new ref" on stdin,
+# and no git anywhere in it.
+.PHONY: test
+test: $(PROG) $(TST_RUN)
+ @mkdir -p $(TST_DIR)/tmp
+ $(foreach var,$(TST_RUN),./$(var) &&) true
+ AUTHZ=./$(PROG) sh $(TST_DIR)/run.sh
+
+# Both suites again with AddressSanitizer and UBSan, which is the only reason
+# every command frees what it read: a process that exits in a millisecond does
+# not need the memory back, but it does need that report to stay empty, or the
+# one real leak hides among forty.
+.PHONY: sanitizer
+sanitizer: CFLAGS := -std=c99 -Wall -Wextra -Wpedantic -I$(SRC_DIR) \
+ -D_POSIX_C_SOURCE=200809L -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
- $(CC) $(STD) $(WARN) $(CPPFLAGS) $(CFLAGS) -o $@ tests/unit.c \
- src/util.o src/glob.o src/policy.o
-
-test: $(PROG) $(UNIT)
- mkdir -p tests/tmp
- $(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 same build with the warnings turned into errors, so that CI proves the
+# tree is clean rather than merely quiet.
+.PHONY: strict
+strict: CFLAGS += -Werror
+strict: all
+
+.PHONY: all
+all: $(PROG) $(TST_RUN)
+
+# The hook is a symlink named 'pre-receive', because that name is what
+# core.hooksPath looks for and what selects authorize mode inside the binary.
+# It is relative so that a staged install, an unpacked prefix or an image
+# assembled elsewhere points at the binary that came with it rather than at
+# whatever occupies that absolute path on the machine reading it.
#
# 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.
+PREFIX ?= /usr/local
+BINDIR ?= $(PREFIX)/bin
+SHAREDIR ?= $(PREFIX)/share/git-host
+SYSCONFDIR ?= /etc/git-host
+HOOKDIR ?= $(SHAREDIR)/hooks
+
+.PHONY: install
install: $(PROG)
install -d $(DESTDIR)$(BINDIR) $(DESTDIR)$(HOOKDIR) $(DESTDIR)$(SYSCONFDIR)
install -m 0755 $(PROG) $(DESTDIR)$(BINDIR)/$(PROG)
@@ -81,11 +92,12 @@ install: $(PROG)
ln -s "$$link" $(DESTDIR)$(HOOKDIR)/pre-receive; \
echo " $(HOOKDIR)/pre-receive -> $$link"
+.PHONY: uninstall
uninstall:
rm -f $(DESTDIR)$(BINDIR)/$(PROG) $(DESTDIR)$(HOOKDIR)/pre-receive
+.PHONY: clean
clean:
- rm -f $(OBJS) $(PROG) $(UNIT)
- rm -rf tests/tmp
-
-.PHONY: all test install uninstall clean
+ @$(RM) -rf obj/
+ @$(RM) -rf $(TST_DIR)/tmp
+ @$(RM) $(PROG)
diff --git a/README b/README
index 8b5d9e3..f795411 100644
--- a/README
+++ b/README
@@ -220,14 +220,37 @@ Reading this as an operator
hit it instead of a ticket to whoever set this up.
+Layout
+------
+
+ cmd/main.c the command line: dispatch, usage, exit status
+ src/ the engine, as a library the command line adds nothing to
+ test/ one program per area, plus the behavioural suite
+ build/ helpers used while building and installing
+ container/ the image: Dockerfile, entrypoint, server configuration
+ ci/ the pipeline
+
+ src/glob.c the pattern matching, such as it is
+ src/util.c allocation, string buffers, writing a file atomically
+ src/policy.c reading the file, and answering from it
+ src/hook.c the mode git calls, and the only one a push depends on
+ src/validate.c does this policy, and the host around it, add up
+ src/edit.c grant and revoke, without losing the file
+
+Everything compiles into `obj/`, test runners included, so a build leaves the
+tree as it found it apart from the binary. `git-authz` is one program rather
+than several because every one of them has to answer "what does this rule mean"
+identically: a `check` that disagrees with the hook is not two programs, it is
+one that lies.
+
+
Tests
-----
make test
-`tests/unit.c` tests the policy engine directly - matching, normalisation, the
-ladder, `@all`, scope, the sentences - and is the place where the semantics
-are pinned. `tests/run.sh` drives the binary the way git does: an identity in
+The programs in `test/` test the engine directly - matching, normalisation, the
+ladder, `@all`, scope, the sentences - and are where the semantics are pinned. `test/run.sh` drives the binary the way git does: an identity in
the environment, `old new ref` lines on stdin, the repository as the working
directory, the hook reached through its own name. It uses no git at all, so it
runs anywhere a compiler does, and it tests the real binary rather than a model
diff --git a/ci/build.yaml b/ci/build.yaml
index e8db98c..d881096 100644
--- a/ci/build.yaml
+++ b/ci/build.yaml
@@ -53,7 +53,7 @@ jobs:
apk add --no-cache build-base git >/dev/null
cd repo
make clean
- make WARN="-Wall -Wextra -Wpedantic -Werror" 2>&1 | tail -3
+ make strict 2>&1 | tail -3
make test | tail -1
make clean
make sanitizer | tail -1
diff --git a/cmd/main.c b/cmd/main.c
new file mode 100644
index 0000000..8a42b72
--- /dev/null
+++ b/cmd/main.c
@@ -0,0 +1,293 @@
+/* main.c - one program, four jobs.
+ *
+ * check an operator asking, or a script
+ * authorize git asking, once per push (also reached as 'pre-receive')
+ * validate the entrypoint and CI asking before anything is served
+ * edit changing the file (grant, revoke)
+ *
+ * They are one program because they must agree, exactly, about what a rule
+ * means: a check that answers differently from the hook is worse than two
+ * programs, it is a program that lies. The exit status is the interface, and
+ * it never changes: 0 allowed, 1 denied, 3 no usable policy, 64 usage.
+ */
+#include "authz.h"
+
+#include
+#include
+#include
+
+#define POLICY_ENV "GIT_HOST_AUTHZ"
+#define DEFAULT_POLICY "/etc/git-host/authz.conf"
+#define DEFAULT_ROOT "/var/lib/git"
+#define DEFAULT_HOOKS "/usr/local/share/git-host/hooks"
+#define DEFAULT_GITCONFIG "/etc/gitconfig"
+#define DEFAULT_SSHD "/etc/ssh/sshd_config"
+#define DEFAULT_PASSWD "/etc/passwd"
+#define DEFAULT_KEYS "/run/git-host/secrets/authorized_keys"
+#define DEFAULT_HTPASSWD "/etc/git-host/secrets/htpasswd"
+
+static void usage(FILE *f)
+{
+ fprintf(f,
+"usage: git-authz [--policy PATH] [arguments]\n"
+"\n"
+" check [ref]\n"
+" one question: allow or deny on stdout, the reason with it.\n"
+" Without a ref it asks about the repository as a whole.\n"
+"\n"
+" authorize read a push exactly as git's receive hook does - identity from\n"
+" GIT_HOST_USER (ssh) or REMOTE_USER (http), one \"old new ref\" line per\n"
+" ref on stdin - and deny the push on the first ref it may not allow.\n"
+" This also runs as 'pre-receive', which is how core.hooksPath finds it.\n"
+" --user U --repo R override the identity and the repository\n"
+" --ff auto|yes|no whether to ask git if old is an ancestor of new\n"
+" --head auto|none|REF the default branch, or how to treat deletions\n"
+" --no-git ask git nothing (an unknown HEAD is not guessed)\n"
+" --explain report the allows too\n"
+"\n"
+" validate is the policy parseable, and is the host wired to run it?\n"
+" --no-host only the policy, none of the wiring below\n"
+" --root DIR repositories (default %s)\n"
+" --hook-dir DIR hooks (default %s)\n"
+" --gitconfig FILE (default %s)\n"
+" --sshd-config FILE (default %s)\n"
+" --passwd FILE (default %s)\n"
+" --keys FILE (default %s)\n"
+" --htpasswd FILE (default %s)\n"
+" --strict warnings are failures too\n"
+"\n"
+" grant [ref ...] add one rule, or replace that pair's\n"
+" revoke take it away again\n"
+" --out FILE | --stdout write elsewhere instead of in place\n"
+" The deployed policy is a read-only mount on purpose; these are for the\n"
+" source copy, and they say so when they are pointed at a read-only file.\n"
+"\n"
+"Levels are monotone: admin implies force implies write. '@all' means everyone\n"
+"the policy already names. \"main\", \"heads/main\" and \"refs/heads/main\" are the\n"
+"same branch; a bare name is always a branch and never a tag. A rule says what\n"
+"it says: nothing else in the file can raise the price of a ref.\n"
+"\n"
+"Exit status: 0 allowed, 1 denied, 3 the policy could not be read or does not\n"
+"parse - nothing is allowed on a policy that cannot be read - and 64 for usage.\n",
+DEFAULT_ROOT, DEFAULT_HOOKS, DEFAULT_GITCONFIG, DEFAULT_SSHD, DEFAULT_PASSWD,
+DEFAULT_KEYS, DEFAULT_HTPASSWD);
+}
+
+static int one_option(int argc, char **argv, int *i, const char *name, const char **slot)
+{
+ size_t n = strlen(name);
+
+ if (strcmp(argv[*i], name) == 0) {
+ if (*i + 1 >= argc) {
+ fprintf(stderr, "%s: %s wants a value\n", progname, name);
+ return -1;
+ }
+ *slot = argv[++*i];
+ return 1;
+ }
+ if (strncmp(argv[*i], name, n) == 0 && argv[*i][n] == '=') {
+ *slot = argv[*i] + n + 1;
+ return 1;
+ }
+ return 0;
+}
+
+/* The policy is not usable, so the only safe answer is no. Three, not one:
+ * the push was refused because this host is broken, not because that person is
+ * not allowed, and whoever is debugging wants the two to sound different. */
+static int deny_policy(const char *path, int rc)
+{
+ fprintf(stderr, "%s: deny: the policy at %s %s\n", progname, path,
+ rc == -2 ? "cannot be read" : "does not parse");
+ return 3;
+}
+
+/* Everything that answers out of a loaded policy. Split out of main so the
+ * policy is freed on the way out whichever way that is: a process this short
+ * lived could leave the heap to the kernel, but then a sanitizer run reports
+ * forty leaks and stops showing the one that matters. */
+static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv,
+ const char *path, hook_opts ho, validate_opts vo)
+{
+ if (strcmp(cmd, "validate") == 0) {
+ int j;
+
+ for (j = 0; j < argc; j++) {
+ int got;
+
+ got = one_option(argc, argv, &j, "--root", &vo.root);
+ if (got == 0)
+ got = one_option(argc, argv, &j, "--hook-dir", &vo.hook_dir);
+ if (got == 0)
+ got = one_option(argc, argv, &j, "--gitconfig", &vo.gitconfig);
+ if (got == 0)
+ got = one_option(argc, argv, &j, "--sshd-config", &vo.sshd_config);
+ if (got == 0)
+ got = one_option(argc, argv, &j, "--passwd", &vo.passwd);
+ if (got == 0)
+ got = one_option(argc, argv, &j, "--keys", &vo.keys);
+ if (got == 0)
+ got = one_option(argc, argv, &j, "--htpasswd", &vo.htpasswd);
+ if (got < 0)
+ return 64;
+ if (got == 0 && strcmp(argv[j], "--strict") == 0) {
+ vo.strict = 1;
+ continue;
+ }
+ /* The policy is the part that decides who may push. Everything
+ * else is how this host is wired, and a host without a git account
+ * yet - a container being built, a config directory being checked
+ * in - still wants its policy read. */
+ if (got == 0 && strcmp(argv[j], "--no-host") == 0) {
+ vo.root = NULL;
+ vo.hook_dir = NULL;
+ vo.gitconfig = NULL;
+ vo.sshd_config = NULL;
+ vo.passwd = NULL;
+ vo.keys = NULL;
+ vo.htpasswd = NULL;
+ continue;
+ }
+ if (got == 0) {
+ fprintf(stderr, "%s: validate: unknown argument %s\n", progname,
+ argv[j]);
+ return 64;
+ }
+ }
+ /* A policy that does not parse is itself the finding, so it is passed
+ * down and reported rather than turning this into a bare exit 3. */
+ return cmd_validate(p, vo);
+ }
+
+ if (strcmp(cmd, "authorize") == 0) {
+ if (rc != 0)
+ return deny_policy(path, rc);
+ return cmd_authorize(p, ho, argc, argv);
+ }
+
+ if (strcmp(cmd, "check") == 0) {
+ decision_t d;
+ int level;
+
+ if (argc < 3 || argc > 4) {
+ fprintf(stderr, "%s: check wants [ref]\n", progname);
+ return 64;
+ }
+ level = authz_level_of(argv[2]);
+ if (level < 0) {
+ fprintf(stderr, "%s: unknown access level \"%s\" (write force admin)\n",
+ progname, argv[2]);
+ return 64;
+ }
+ if (rc != 0)
+ return deny_policy(path, rc);
+ authz_check(p, argv[0], argv[1], (level_t)level, argc > 3 ? argv[3] : NULL, &d);
+ printf("%s: %s\n", d.allowed ? "allow" : "deny", d.why);
+ return d.allowed ? 0 : 1;
+ }
+
+ fprintf(stderr, "%s: unknown command \"%s\"\n", progname, cmd);
+ usage(stderr);
+ return 64;
+}
+
+int main(int argc, char **argv)
+{
+ const char *path;
+ const char *base;
+ const char *cmd;
+ policy_t p;
+ hook_opts ho;
+ validate_opts vo;
+ edit_opts eo;
+ int i;
+ int rc;
+ int status;
+
+ base = strrchr(argv[0], '/');
+ progname = base != NULL ? base + 1 : argv[0];
+
+ path = getenv(POLICY_ENV);
+ if (path == NULL || path[0] == '\0')
+ path = DEFAULT_POLICY;
+
+ memset(&ho, 0, sizeof(ho));
+ ho.use_git = 1;
+ memset(&vo, 0, sizeof(vo));
+ vo.root = DEFAULT_ROOT;
+ vo.hook_dir = DEFAULT_HOOKS;
+ vo.gitconfig = DEFAULT_GITCONFIG;
+ vo.sshd_config = DEFAULT_SSHD;
+ vo.passwd = DEFAULT_PASSWD;
+ vo.keys = DEFAULT_KEYS;
+ vo.htpasswd = DEFAULT_HTPASSWD;
+ memset(&eo, 0, sizeof(eo));
+
+ /* Reached through the hook symlink. Git's own arguments to a hook are not
+ * this program's, so they are dropped, and the policy comes from the
+ * environment - a hook is not in a position to be told where the policy is
+ * by a person who is not there. */
+ if (strcmp(progname, "pre-receive") == 0) {
+ policy_init(&p, path);
+ rc = policy_load(&p);
+ if (rc != 0) {
+ policy_free(&p);
+ return deny_policy(path, rc);
+ }
+ status = cmd_authorize(&p, ho, 0, NULL);
+ policy_free(&p);
+ return status;
+ }
+
+ i = 1;
+ while (i < argc && argv[i][0] == '-' && argv[i][1] != '\0') {
+ int got = one_option(argc, argv, &i, "--policy", &path);
+
+ if (got < 0)
+ return 64;
+ if (got == 0 && (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)) {
+ usage(stdout);
+ return 0;
+ }
+ if (got == 0) {
+ fprintf(stderr, "%s: unknown option before the command: %s\n", progname,
+ argv[i]);
+ usage(stderr);
+ return 64;
+ }
+ i++;
+ }
+ if (i >= argc) {
+ usage(stderr);
+ return 64;
+ }
+ cmd = argv[i++];
+ argc -= i;
+ argv += i;
+
+ if (strcmp(cmd, "help") == 0 || strcmp(cmd, "-h") == 0 ||
+ strcmp(cmd, "--help") == 0) {
+ usage(stdout);
+ return 0;
+ }
+
+ /* The three commands that change the file. They read it themselves, and
+ * they must not disagree with the checker about what a rule means, which is
+ * why they are in the same binary. */
+ if (strcmp(cmd, "grant") == 0 || strcmp(cmd, "revoke") == 0) {
+ int consumed = 0;
+
+ if (authz_parse_out(argc, argv, &eo, &consumed) != 0)
+ return 64;
+ argc -= consumed; /* what is left was compacted to the front */
+ if (strcmp(cmd, "grant") == 0)
+ return cmd_grant(path, eo, argc, argv);
+ return cmd_revoke(path, eo, argc, argv);
+ }
+
+ policy_init(&p, path);
+ rc = policy_load(&p);
+ status = dispatch(&p, rc, cmd, argc, argv, path, ho, vo);
+ policy_free(&p);
+ return status;
+}
diff --git a/container/Dockerfile b/container/Dockerfile
index e73ed34..1828748 100644
--- a/container/Dockerfile
+++ b/container/Dockerfile
@@ -23,8 +23,9 @@ RUN apk add --no-cache build-base
WORKDIR /src
COPY Makefile README authz.conf ./
COPY src ./src
+COPY cmd ./cmd
+COPY test ./test
COPY build ./build
-COPY tests ./tests
# The suite runs in the image it ships in, against the musl build: that is the
# only place the two are guaranteed to meet, and an image built from a tree
# whose tests fail should not exist.
diff --git a/src/main.c b/src/main.c
deleted file mode 100644
index 8a42b72..0000000
--- a/src/main.c
+++ /dev/null
@@ -1,293 +0,0 @@
-/* main.c - one program, four jobs.
- *
- * check an operator asking, or a script
- * authorize git asking, once per push (also reached as 'pre-receive')
- * validate the entrypoint and CI asking before anything is served
- * edit changing the file (grant, revoke)
- *
- * They are one program because they must agree, exactly, about what a rule
- * means: a check that answers differently from the hook is worse than two
- * programs, it is a program that lies. The exit status is the interface, and
- * it never changes: 0 allowed, 1 denied, 3 no usable policy, 64 usage.
- */
-#include "authz.h"
-
-#include
-#include
-#include
-
-#define POLICY_ENV "GIT_HOST_AUTHZ"
-#define DEFAULT_POLICY "/etc/git-host/authz.conf"
-#define DEFAULT_ROOT "/var/lib/git"
-#define DEFAULT_HOOKS "/usr/local/share/git-host/hooks"
-#define DEFAULT_GITCONFIG "/etc/gitconfig"
-#define DEFAULT_SSHD "/etc/ssh/sshd_config"
-#define DEFAULT_PASSWD "/etc/passwd"
-#define DEFAULT_KEYS "/run/git-host/secrets/authorized_keys"
-#define DEFAULT_HTPASSWD "/etc/git-host/secrets/htpasswd"
-
-static void usage(FILE *f)
-{
- fprintf(f,
-"usage: git-authz [--policy PATH] [arguments]\n"
-"\n"
-" check [ref]\n"
-" one question: allow or deny on stdout, the reason with it.\n"
-" Without a ref it asks about the repository as a whole.\n"
-"\n"
-" authorize read a push exactly as git's receive hook does - identity from\n"
-" GIT_HOST_USER (ssh) or REMOTE_USER (http), one \"old new ref\" line per\n"
-" ref on stdin - and deny the push on the first ref it may not allow.\n"
-" This also runs as 'pre-receive', which is how core.hooksPath finds it.\n"
-" --user U --repo R override the identity and the repository\n"
-" --ff auto|yes|no whether to ask git if old is an ancestor of new\n"
-" --head auto|none|REF the default branch, or how to treat deletions\n"
-" --no-git ask git nothing (an unknown HEAD is not guessed)\n"
-" --explain report the allows too\n"
-"\n"
-" validate is the policy parseable, and is the host wired to run it?\n"
-" --no-host only the policy, none of the wiring below\n"
-" --root DIR repositories (default %s)\n"
-" --hook-dir DIR hooks (default %s)\n"
-" --gitconfig FILE (default %s)\n"
-" --sshd-config FILE (default %s)\n"
-" --passwd FILE (default %s)\n"
-" --keys FILE (default %s)\n"
-" --htpasswd FILE (default %s)\n"
-" --strict warnings are failures too\n"
-"\n"
-" grant [ref ...] add one rule, or replace that pair's\n"
-" revoke take it away again\n"
-" --out FILE | --stdout write elsewhere instead of in place\n"
-" The deployed policy is a read-only mount on purpose; these are for the\n"
-" source copy, and they say so when they are pointed at a read-only file.\n"
-"\n"
-"Levels are monotone: admin implies force implies write. '@all' means everyone\n"
-"the policy already names. \"main\", \"heads/main\" and \"refs/heads/main\" are the\n"
-"same branch; a bare name is always a branch and never a tag. A rule says what\n"
-"it says: nothing else in the file can raise the price of a ref.\n"
-"\n"
-"Exit status: 0 allowed, 1 denied, 3 the policy could not be read or does not\n"
-"parse - nothing is allowed on a policy that cannot be read - and 64 for usage.\n",
-DEFAULT_ROOT, DEFAULT_HOOKS, DEFAULT_GITCONFIG, DEFAULT_SSHD, DEFAULT_PASSWD,
-DEFAULT_KEYS, DEFAULT_HTPASSWD);
-}
-
-static int one_option(int argc, char **argv, int *i, const char *name, const char **slot)
-{
- size_t n = strlen(name);
-
- if (strcmp(argv[*i], name) == 0) {
- if (*i + 1 >= argc) {
- fprintf(stderr, "%s: %s wants a value\n", progname, name);
- return -1;
- }
- *slot = argv[++*i];
- return 1;
- }
- if (strncmp(argv[*i], name, n) == 0 && argv[*i][n] == '=') {
- *slot = argv[*i] + n + 1;
- return 1;
- }
- return 0;
-}
-
-/* The policy is not usable, so the only safe answer is no. Three, not one:
- * the push was refused because this host is broken, not because that person is
- * not allowed, and whoever is debugging wants the two to sound different. */
-static int deny_policy(const char *path, int rc)
-{
- fprintf(stderr, "%s: deny: the policy at %s %s\n", progname, path,
- rc == -2 ? "cannot be read" : "does not parse");
- return 3;
-}
-
-/* Everything that answers out of a loaded policy. Split out of main so the
- * policy is freed on the way out whichever way that is: a process this short
- * lived could leave the heap to the kernel, but then a sanitizer run reports
- * forty leaks and stops showing the one that matters. */
-static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv,
- const char *path, hook_opts ho, validate_opts vo)
-{
- if (strcmp(cmd, "validate") == 0) {
- int j;
-
- for (j = 0; j < argc; j++) {
- int got;
-
- got = one_option(argc, argv, &j, "--root", &vo.root);
- if (got == 0)
- got = one_option(argc, argv, &j, "--hook-dir", &vo.hook_dir);
- if (got == 0)
- got = one_option(argc, argv, &j, "--gitconfig", &vo.gitconfig);
- if (got == 0)
- got = one_option(argc, argv, &j, "--sshd-config", &vo.sshd_config);
- if (got == 0)
- got = one_option(argc, argv, &j, "--passwd", &vo.passwd);
- if (got == 0)
- got = one_option(argc, argv, &j, "--keys", &vo.keys);
- if (got == 0)
- got = one_option(argc, argv, &j, "--htpasswd", &vo.htpasswd);
- if (got < 0)
- return 64;
- if (got == 0 && strcmp(argv[j], "--strict") == 0) {
- vo.strict = 1;
- continue;
- }
- /* The policy is the part that decides who may push. Everything
- * else is how this host is wired, and a host without a git account
- * yet - a container being built, a config directory being checked
- * in - still wants its policy read. */
- if (got == 0 && strcmp(argv[j], "--no-host") == 0) {
- vo.root = NULL;
- vo.hook_dir = NULL;
- vo.gitconfig = NULL;
- vo.sshd_config = NULL;
- vo.passwd = NULL;
- vo.keys = NULL;
- vo.htpasswd = NULL;
- continue;
- }
- if (got == 0) {
- fprintf(stderr, "%s: validate: unknown argument %s\n", progname,
- argv[j]);
- return 64;
- }
- }
- /* A policy that does not parse is itself the finding, so it is passed
- * down and reported rather than turning this into a bare exit 3. */
- return cmd_validate(p, vo);
- }
-
- if (strcmp(cmd, "authorize") == 0) {
- if (rc != 0)
- return deny_policy(path, rc);
- return cmd_authorize(p, ho, argc, argv);
- }
-
- if (strcmp(cmd, "check") == 0) {
- decision_t d;
- int level;
-
- if (argc < 3 || argc > 4) {
- fprintf(stderr, "%s: check wants [ref]\n", progname);
- return 64;
- }
- level = authz_level_of(argv[2]);
- if (level < 0) {
- fprintf(stderr, "%s: unknown access level \"%s\" (write force admin)\n",
- progname, argv[2]);
- return 64;
- }
- if (rc != 0)
- return deny_policy(path, rc);
- authz_check(p, argv[0], argv[1], (level_t)level, argc > 3 ? argv[3] : NULL, &d);
- printf("%s: %s\n", d.allowed ? "allow" : "deny", d.why);
- return d.allowed ? 0 : 1;
- }
-
- fprintf(stderr, "%s: unknown command \"%s\"\n", progname, cmd);
- usage(stderr);
- return 64;
-}
-
-int main(int argc, char **argv)
-{
- const char *path;
- const char *base;
- const char *cmd;
- policy_t p;
- hook_opts ho;
- validate_opts vo;
- edit_opts eo;
- int i;
- int rc;
- int status;
-
- base = strrchr(argv[0], '/');
- progname = base != NULL ? base + 1 : argv[0];
-
- path = getenv(POLICY_ENV);
- if (path == NULL || path[0] == '\0')
- path = DEFAULT_POLICY;
-
- memset(&ho, 0, sizeof(ho));
- ho.use_git = 1;
- memset(&vo, 0, sizeof(vo));
- vo.root = DEFAULT_ROOT;
- vo.hook_dir = DEFAULT_HOOKS;
- vo.gitconfig = DEFAULT_GITCONFIG;
- vo.sshd_config = DEFAULT_SSHD;
- vo.passwd = DEFAULT_PASSWD;
- vo.keys = DEFAULT_KEYS;
- vo.htpasswd = DEFAULT_HTPASSWD;
- memset(&eo, 0, sizeof(eo));
-
- /* Reached through the hook symlink. Git's own arguments to a hook are not
- * this program's, so they are dropped, and the policy comes from the
- * environment - a hook is not in a position to be told where the policy is
- * by a person who is not there. */
- if (strcmp(progname, "pre-receive") == 0) {
- policy_init(&p, path);
- rc = policy_load(&p);
- if (rc != 0) {
- policy_free(&p);
- return deny_policy(path, rc);
- }
- status = cmd_authorize(&p, ho, 0, NULL);
- policy_free(&p);
- return status;
- }
-
- i = 1;
- while (i < argc && argv[i][0] == '-' && argv[i][1] != '\0') {
- int got = one_option(argc, argv, &i, "--policy", &path);
-
- if (got < 0)
- return 64;
- if (got == 0 && (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)) {
- usage(stdout);
- return 0;
- }
- if (got == 0) {
- fprintf(stderr, "%s: unknown option before the command: %s\n", progname,
- argv[i]);
- usage(stderr);
- return 64;
- }
- i++;
- }
- if (i >= argc) {
- usage(stderr);
- return 64;
- }
- cmd = argv[i++];
- argc -= i;
- argv += i;
-
- if (strcmp(cmd, "help") == 0 || strcmp(cmd, "-h") == 0 ||
- strcmp(cmd, "--help") == 0) {
- usage(stdout);
- return 0;
- }
-
- /* The three commands that change the file. They read it themselves, and
- * they must not disagree with the checker about what a rule means, which is
- * why they are in the same binary. */
- if (strcmp(cmd, "grant") == 0 || strcmp(cmd, "revoke") == 0) {
- int consumed = 0;
-
- if (authz_parse_out(argc, argv, &eo, &consumed) != 0)
- return 64;
- argc -= consumed; /* what is left was compacted to the front */
- if (strcmp(cmd, "grant") == 0)
- return cmd_grant(path, eo, argc, argv);
- return cmd_revoke(path, eo, argc, argv);
- }
-
- policy_init(&p, path);
- rc = policy_load(&p);
- status = dispatch(&p, rc, cmd, argc, argv, path, ho, vo);
- policy_free(&p);
- return status;
-}
diff --git a/test/check.c b/test/check.c
new file mode 100644
index 0000000..3df2ade
--- /dev/null
+++ b/test/check.c
@@ -0,0 +1,171 @@
+/* check.c - The decision, and the sentence that goes with it. A refusal has to name
+ * the level held, the ref and the level required, because a person who is
+ * refused should be able to fix it without opening the policy first.
+ *
+ * Part of the direct suite: this links the library, not the command
+ * line. test/run.sh is the other half, and drives the program the way git does.
+ */
+#include "test.h"
+
+static void test_check(void)
+{
+ policy_t p;
+ int rc;
+
+ rc = load_text(&p,
+ "alice * admin\n"
+ "@all * write refs/heads/feature/*\n"
+ "frank team write refs/heads/main refs/tags/*\n"
+ "bob team write refs/heads/feature/*\n"
+ "carol team force refs/heads/feature/*\n"
+ "dave team admin\n"
+ "erin secrets admin\n");
+ CHECK_INT(rc, 0);
+
+ /* the ladder */
+ expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/feature/x");
+ expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/feature/x", "needs force");
+ expect_deny(&p, "bob", "team", LV_ADMIN, "refs/heads/feature/x", "needs admin");
+ expect_allow(&p, "carol", "team", LV_FORCE, "refs/heads/feature/x");
+ expect_allow(&p, "dave", "team", LV_ADMIN, "refs/heads/main");
+
+ /* strongest match wins, additively */
+ expect_allow(&p, "dave", "team", LV_WRITE, "refs/heads/feature/x");
+ expect_allow(&p, "alice", "anything", LV_ADMIN, "refs/heads/main");
+
+ /* '@all' reaches the named, and nobody else. Note that the '@all *' rule
+ * below reaches every repository, so a named user is not confined to the
+ * repository their own rule names - that is what the rule says. */
+ expect_allow(&p, "carol", "web", LV_WRITE, "refs/heads/feature/any");
+ expect_allow(&p, "erin", "web", LV_WRITE, "refs/heads/feature/any");
+ expect_deny(&p, "nobody", "web", LV_WRITE, "refs/heads/any", "no rule");
+ expect_deny(&p, "", "web", LV_WRITE, "refs/heads/any", "no identity");
+ expect_deny(&p, NULL, "web", LV_WRITE, "refs/heads/any", "no identity");
+
+ /* main is not special: it is refused here because no rule of bob's or
+ * carol's names it, and allowed for dave because his names everything. */
+ expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/main",
+ "but not on refs/heads/main");
+ expect_deny(&p, "carol", "team", LV_FORCE, "refs/heads/main",
+ "but not on refs/heads/main");
+ expect_allow(&p, "dave", "team", LV_WRITE, "refs/heads/main");
+ /* and the person whose rule does name main, and nothing else, gets main */
+ expect_allow(&p, "frank", "team", LV_WRITE, "refs/heads/main");
+ /* frank reaches the '@all' rule like anyone else the policy names, so what
+ * he lacks is a branch nobody's rule names, and a rewrite of his own */
+ expect_allow(&p, "frank", "team", LV_WRITE, "refs/heads/feature/x");
+ expect_deny(&p, "frank", "team", LV_WRITE, "refs/heads/dev",
+ "but not on refs/heads/dev");
+ expect_deny(&p, "frank", "team", LV_FORCE, "refs/heads/main", "needs force");
+
+ /* refs that are not branches or tags: only an admin, at any request level */
+ expect_allow(&p, "dave", "team", LV_WRITE, "refs/pull/1/head");
+ expect_allow(&p, "dave", "team", LV_ADMIN, "refs/pull/1/head");
+ expect_deny(&p, "bob", "team", LV_ADMIN, "refs/notes/x", "needs admin");
+
+ /* the same branch, three spellings */
+ expect_deny(&p, "bob", "team", LV_WRITE, "main", "but not on refs/heads/main");
+ expect_deny(&p, "bob", "team", LV_WRITE, "heads/main",
+ "but not on refs/heads/main");
+
+ /* repository matching: .git is the same repository */
+ expect_allow(&p, "bob", "team.git", LV_WRITE, "refs/heads/feature/x");
+ expect_allow(&p, "erin", "secrets.git", LV_ADMIN, "refs/heads/main");
+
+ /* the question without a ref */
+ expect_allow(&p, "dave", "team", LV_FORCE, NULL);
+ expect_allow(&p, "alice", "team", LV_ADMIN, NULL);
+ expect_allow(&p, "bob", "team", LV_WRITE, NULL);
+ expect_deny(&p, "bob", "team", LV_FORCE, NULL, "no force");
+ expect_allow(&p, "erin", "team", LV_WRITE, NULL); /* via '@all *' */
+ expect_deny(&p, "nobody", "team", LV_WRITE, NULL, "no write");
+
+ /* what a denial names: the level held, and the level needed */
+ {
+ decision_t d;
+
+ authz_check(&p, "bob", "team", LV_ADMIN, "refs/heads/feature/x", &d);
+ CHECK(!d.allowed);
+ CHECK_INT(d.need, LV_ADMIN);
+ CHECK(strstr(d.why, "bob has write") != NULL);
+ CHECK(strstr(d.why, "needs admin") != NULL);
+
+ authz_check(&p, "nobody", "team", LV_WRITE, "refs/heads/dev", &d);
+ CHECK(!d.allowed);
+ CHECK(strstr(d.why, "nobody has no rule for team") != NULL);
+ }
+ policy_free(&p);
+}
+
+static void test_scope_edges(void)
+{
+ policy_t p;
+ int rc;
+
+ /* a rule with no refs is the whole repository: every branch, main included,
+ * which is exactly why naming the refs is the honest way to write one */
+ rc = load_text(&p, "bob team write\n");
+ CHECK_INT(rc, 0);
+ expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/anything");
+ expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/main");
+ /* write on everything still is not force */
+ expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/main", "needs force");
+ policy_free(&p);
+
+ /* a bare name is a branch, and only a branch */
+ rc = load_text(&p, "bob web write refs/heads/*\n");
+ CHECK_INT(rc, 0);
+ expect_allow(&p, "bob", "web", LV_WRITE, "v1"); /* refs/heads/v1 */
+ expect_deny(&p, "bob", "web", LV_WRITE, "refs/tags/v1", "needs write");
+ expect_deny(&p, "bob", "web", LV_WRITE, "tags/v1", "needs write");
+ policy_free(&p);
+
+ /* a user may hold several rules for one repository; all of them count */
+ rc = load_text(&p,
+ "bob team write refs/heads/dev\n"
+ "bob team force refs/heads/feature/*\n");
+ CHECK_INT(rc, 0);
+ expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/dev");
+ expect_allow(&p, "bob", "team", LV_FORCE, "refs/heads/feature/x");
+ /* A refusal names the level held on the ref in question, not the strongest
+ * level held somewhere in the repository: "you have force here" next to a
+ * refusal is a sentence nobody can act on. */
+ expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/dev",
+ "bob has write on team refs/heads/dev; it needs force");
+ expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/other",
+ "but not on refs/heads/other");
+ policy_free(&p);
+
+ /* naming refs in a rule is the whole of the mechanism: what is not named is
+ * not granted, and the refusal says so rather than blaming a list */
+ rc = load_text(&p, "bob web write refs/heads/feature/* refs/heads/dev\n");
+ CHECK_INT(rc, 0);
+ expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/dev");
+ expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/feature/x");
+ expect_deny(&p, "bob", "web", LV_WRITE, "refs/heads/main", "but not on");
+ expect_deny(&p, "bob", "api", LV_WRITE, "refs/heads/dev", "no rule");
+ policy_free(&p);
+
+ /* reachable-by-nobody detection, which validate reports */
+ rc = load_text(&p, "bob team write\n");
+ CHECK_INT(rc, 0);
+ CHECK(authz_repo_reachable(&p, "team"));
+ CHECK(authz_repo_reachable(&p, "team.git"));
+ CHECK(!authz_repo_reachable(&p, "other"));
+ policy_free(&p);
+
+ rc = load_text(&p, "bob * write\n");
+ CHECK_INT(rc, 0);
+ CHECK(authz_repo_reachable(&p, "anything"));
+ policy_free(&p);
+}
+
+int main(void)
+{
+ test_init("");
+
+ test_check();
+ test_scope_edges();
+
+ return test_report("check");
+}
diff --git a/test/glob.c b/test/glob.c
new file mode 100644
index 0000000..ee1bece
--- /dev/null
+++ b/test/glob.c
@@ -0,0 +1,50 @@
+/* glob.c - What a glob matches. Written out rather than reached for with fnmatch(3)
+ * or regex(3), so this is the file that says what "*" and "[!a-z]" mean here.
+ *
+ * Part of the direct suite: this links the library, not the command
+ * line. test/run.sh is the other half, and drives the program the way git does.
+ */
+#include "test.h"
+
+static void test_glob(void)
+{
+ CHECK(glob_match("*", ""));
+ CHECK(glob_match("*", "anything/at/all"));
+ CHECK(!glob_match("", "x"));
+ CHECK(glob_match("", ""));
+ CHECK(glob_match("refs/heads/*", "refs/heads/main"));
+ CHECK(glob_match("refs/heads/*", "refs/heads/a/b/c")); /* '*' crosses '/' */
+ CHECK(!glob_match("refs/heads/*", "refs/tags/v1"));
+ CHECK(glob_match("refs/heads/feature/*", "refs/heads/feature/login"));
+ CHECK(!glob_match("refs/heads/feature/*", "refs/heads/featurey"));
+ CHECK(glob_match("refs/tags/v?", "refs/tags/v1"));
+ CHECK(!glob_match("refs/tags/v?", "refs/tags/v12"));
+ CHECK(glob_match("refs/heads/[ab]*", "refs/heads/beta"));
+ CHECK(glob_match("refs/heads/[ab]*", "refs/heads/alpha"));
+ CHECK(!glob_match("refs/heads/[ab]*", "refs/heads/delta"));
+ CHECK(glob_match("refs/heads/[!ab]*", "refs/heads/delta"));
+ CHECK(!glob_match("refs/heads/[!ab]*", "refs/heads/alpha"));
+ CHECK(glob_match("refs/heads/[a-c]1", "refs/heads/b1"));
+ CHECK(!glob_match("refs/heads/[a-c]1", "refs/heads/d1"));
+ CHECK(glob_match("refs/heads/]", "refs/heads/]")); /* ']' first is a member */
+ CHECK(glob_match("refs/heads/a[-]z", "refs/heads/a-z")); /* trailing '-' literal */
+ CHECK(glob_match("refs/heads/[", "refs/heads/[")); /* unclosed is literal */
+ CHECK(glob_match("a*b*c", "aXXbYYc"));
+ CHECK(!glob_match("a*b*c", "aXXbYY"));
+ CHECK(glob_match("*.git", "team.git"));
+ CHECK(glob_match("team*", "team"));
+ CHECK(glob_match("refs/heads/ma?n", "refs/heads/main"));
+ CHECK(!glob_match("refs/heads/main", "refs/heads/MAIN")); /* case matters */
+ CHECK(glob_match("refs/*/x", "refs/a/b/x"));
+ CHECK(glob_match("**", "x")); /* redundant stars */
+ CHECK(!glob_match("refs/heads/*", ""));
+}
+
+int main(void)
+{
+ test_init("");
+
+ test_glob();
+
+ return test_report("glob");
+}
diff --git a/test/need.c b/test/need.c
new file mode 100644
index 0000000..f831111
--- /dev/null
+++ b/test/need.c
@@ -0,0 +1,47 @@
+/* need.c - What a ref costs beyond what the rule said - which is only ever the case
+ * for refs outside refs/heads and refs/tags. There is no second list in the
+ * policy that can raise the price of a ref behind the operator's back.
+ *
+ * Part of the direct suite: this links the library, not the command
+ * line. test/run.sh is the other half, and drives the program the way git does.
+ */
+#include "test.h"
+
+static void test_need_for(void)
+{
+ char raised[64];
+
+ /* A branch or a tag costs exactly what the rule said: there is no list
+ * elsewhere in the file that can raise the price of one. */
+ CHECK_INT(authz_need_for("refs/heads/dev", LV_WRITE, raised, sizeof(raised)),
+ LV_WRITE);
+ CHECK_STR(raised, "");
+ CHECK_INT(authz_need_for("refs/heads/main", LV_WRITE, raised, sizeof(raised)),
+ LV_WRITE);
+ CHECK_INT(authz_need_for("refs/tags/v1", LV_WRITE, raised, sizeof(raised)),
+ LV_WRITE);
+ CHECK_INT(authz_need_for("refs/heads/feature/x", LV_FORCE, raised, sizeof(raised)),
+ LV_FORCE);
+
+ /* Anything outside refs/heads and refs/tags is for maintainers, whatever a
+ * rule appears to grant about it. */
+ CHECK_INT(authz_need_for("refs/pull/1/head", LV_WRITE, raised, sizeof(raised)),
+ LV_ADMIN);
+ CHECK_STR(raised, "neither a branch nor a tag");
+ CHECK_INT(authz_need_for("refs/pull/1/head", LV_FORCE, raised, sizeof(raised)),
+ LV_ADMIN);
+ CHECK_INT(authz_need_for("refs/notes/x", LV_WRITE, raised, sizeof(raised)),
+ LV_ADMIN);
+ CHECK_INT(authz_need_for("refs/notes/x", LV_ADMIN, raised, sizeof(raised)),
+ LV_ADMIN);
+ CHECK_STR(raised, ""); /* admin is already the top */
+}
+
+int main(void)
+{
+ test_init("");
+
+ test_need_for();
+
+ return test_report("need");
+}
diff --git a/test/norm.c b/test/norm.c
new file mode 100644
index 0000000..acfb6ec
--- /dev/null
+++ b/test/norm.c
@@ -0,0 +1,87 @@
+/* norm.c - Refs and repositories have one spelling each internally. This is the file
+ * that pins "main", "heads/main" and "refs/heads/main" being one branch, and
+ * a bare name never meaning a tag.
+ *
+ * Part of the direct suite: this links the library, not the command
+ * line. test/run.sh is the other half, and drives the program the way git does.
+ */
+#include "test.h"
+
+static void test_normalisation(void)
+{
+ char *s;
+
+ s = authz_norm_ref("main");
+ CHECK_STR(s, "refs/heads/main");
+ free(s);
+ s = authz_norm_ref("heads/main");
+ CHECK_STR(s, "refs/heads/main");
+ free(s);
+ s = authz_norm_ref("refs/heads/main");
+ CHECK_STR(s, "refs/heads/main");
+ free(s);
+ s = authz_norm_ref("v1.0");
+ CHECK_STR(s, "refs/heads/v1.0"); /* a bare name is never a tag */
+ free(s);
+ s = authz_norm_ref("tags/v1.0");
+ CHECK_STR(s, "refs/tags/v1.0");
+ free(s);
+ s = authz_norm_ref("notes/commits");
+ CHECK_STR(s, "refs/notes/commits");
+ free(s);
+ s = authz_norm_ref("*");
+ CHECK_STR(s, "refs/*");
+ free(s);
+ s = authz_norm_ref("all");
+ CHECK_STR(s, "refs/*");
+ free(s);
+ s = authz_norm_ref("refs");
+ CHECK_STR(s, "refs/*");
+ free(s);
+
+ s = authz_norm_repo("web.git");
+ CHECK_STR(s, "web");
+ free(s);
+ s = authz_norm_repo("web");
+ CHECK_STR(s, "web");
+ free(s);
+ s = authz_norm_repo("*");
+ CHECK_STR(s, "*");
+ free(s);
+ s = authz_norm_repo(".git");
+ CHECK_STR(s, ".git"); /* a repository called ".git" is not a suffix */
+ free(s);
+
+ CHECK_INT(authz_level_of("write"), LV_WRITE);
+ CHECK_INT(authz_level_of("force"), LV_FORCE);
+ CHECK_INT(authz_level_of("admin"), LV_ADMIN);
+ CHECK_INT(authz_level_of("deny"), -1);
+ CHECK_INT(authz_level_of("read"), -1);
+ CHECK_STR(authz_level_name(LV_NONE), "none");
+ CHECK(authz_level_of("admin") > authz_level_of("force"));
+ CHECK(authz_level_of("force") > authz_level_of("write"));
+
+ CHECK(authz_user_pattern_ok("alice"));
+ CHECK(authz_user_pattern_ok("a.b-c_1"));
+ CHECK(authz_user_pattern_ok("@all"));
+ CHECK(authz_user_pattern_ok("a[bc]d"));
+ CHECK(!authz_user_pattern_ok(""));
+ CHECK(!authz_user_pattern_ok("a/b")); /* a path is not a user name */
+ CHECK(!authz_user_pattern_ok("a$b"));
+ CHECK(!authz_user_pattern_ok("-x"));
+ CHECK(authz_repo_pattern_ok("team"));
+ CHECK(authz_repo_pattern_ok("*"));
+ CHECK(authz_repo_pattern_ok("team-*"));
+ CHECK(!authz_repo_pattern_ok("group/team")); /* flat names only */
+ CHECK(!authz_repo_pattern_ok(""));
+ CHECK(!authz_repo_pattern_ok("a$b"));
+}
+
+int main(void)
+{
+ test_init("");
+
+ test_normalisation();
+
+ return test_report("norm");
+}
diff --git a/test/parse.c b/test/parse.c
new file mode 100644
index 0000000..fb137c5
--- /dev/null
+++ b/test/parse.c
@@ -0,0 +1,133 @@
+/* parse.c - Reading the file: what parses, what is refused, and who "@all" turns out to
+ * be. One bad line poisons the whole file, so the error cases matter as much
+ * as the good ones.
+ *
+ * Part of the direct suite: this links the library, not the command
+ * line. test/run.sh is the other half, and drives the program the way git does.
+ */
+#include "test.h"
+
+static void test_parse(void)
+{
+ policy_t p;
+ int rc;
+
+ rc = load_text(&p,
+ "# a comment\n"
+ "\n"
+ " \n"
+ "alice web admin # trailing comment\n"
+ "bob web write refs/heads/feature/* refs/heads/dev\n"
+ "@all team write main\n");
+ CHECK_INT(rc, 0);
+ CHECK_INT(p.errors, 0);
+ CHECK_INT(p.nrules, 3);
+ CHECK_STR(p.rules[0].user, "alice");
+ CHECK_STR(p.rules[0].repo, "web");
+ CHECK_INT(p.rules[0].level, LV_ADMIN);
+ CHECK_INT(p.rules[0].nrefs, 0);
+ CHECK_INT(p.rules[0].line, 4);
+ CHECK_STR(p.rules[1].refs[0], "refs/heads/feature/*");
+ CHECK_INT(p.rules[1].nrefs, 2);
+ CHECK_STR(p.rules[2].refs[0], "refs/heads/main"); /* normalised at parse time */
+ policy_free(&p);
+
+ /* Errors: each of these must stop the file being used at all. The complaints
+ * are asserted as counts, and the parser writes them to stderr as it goes, so
+ * stderr is set aside here - a wall of expected errors would hide the failures.
+ * The descriptor is redirected rather than freopen'd, because freopen would
+ * quietly make stderr block-buffered and hand the whole wall back at exit. */
+ {
+ int keep = dup(2);
+ int devnull = open("/dev/null", O_WRONLY);
+
+ if (devnull >= 0) {
+ dup2(devnull, 2);
+ close(devnull);
+ }
+
+ rc = load_text(&p, "alice web everything\n");
+ CHECK_INT(rc, -1);
+ CHECK_INT(p.errors, 1);
+ policy_free(&p);
+
+ rc = load_text(&p, "alice web/extra write\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ rc = load_text(&p, "alice web write !refs/heads/main\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ rc = load_text(&p, "alice web\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ rc = load_text(&p, "set other-thing refs/heads/main\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ /* Removed settings are refused by name, not ignored: a policy that
+ * still carries one would otherwise mean "everyone with write can fast
+ * -forward main" while looking like it said the opposite. */
+ rc = load_text(&p, "set protected-refs refs/heads/main\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ rc = load_text(&p, "set protected-refs.web refs/heads/main\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ rc = load_text(&p, "alice web write refs/heads/main\nbob web nope refs/heads/dev\n");
+ CHECK_INT(rc, -1); /* one bad line poisons the file: nothing is used */
+ policy_free(&p);
+
+ fflush(stderr);
+ dup2(keep, 2);
+ close(keep);
+ }
+
+ /* There is no settings list any more: a rule says what it says. */
+ rc = load_text(&p, "alice * admin\n");
+ CHECK_INT(rc, 0);
+ CHECK_INT(p.nrules, 1);
+ policy_free(&p);
+}
+
+static void test_known_users(void)
+{
+ policy_t p;
+ int rc;
+
+ rc = load_text(&p,
+ "alice web admin\n"
+ "bob web write\n"
+ "@all web write refs/heads/*\n");
+ CHECK_INT(rc, 0);
+ CHECK(authz_user_known(&p, "alice"));
+ CHECK(authz_user_known(&p, "bob"));
+ CHECK(!authz_user_known(&p, "carol")); /* '@all' never invents a name */
+ CHECK(!authz_user_known(&p, ""));
+ policy_free(&p);
+
+ rc = load_text(&p, "@all web write\n");
+ CHECK_INT(rc, 0);
+ CHECK(!authz_user_known(&p, "alice")); /* nobody is known yet */
+ policy_free(&p);
+
+ rc = load_text(&p, "ali* web write\n");
+ CHECK_INT(rc, 0);
+ CHECK(authz_user_known(&p, "alice")); /* a pattern is still a name */
+ CHECK(!authz_user_known(&p, "bob"));
+ policy_free(&p);
+}
+
+int main(void)
+{
+ test_init("");
+
+ test_parse();
+ test_known_users();
+
+ return test_report("parse");
+}
diff --git a/test/run.sh b/test/run.sh
new file mode 100755
index 0000000..ab1de2d
--- /dev/null
+++ b/test/run.sh
@@ -0,0 +1,801 @@
+#!/bin/sh
+# run.sh - drive git-authz the way git does, without git.
+#
+# Everything a receive hook knows, this feeds it: an identity in the
+# environment, "old new ref" lines on stdin, the repository as the working
+# directory. What only the object store knows - whether old is an ancestor of
+# new, and which branch HEAD points at - is supplied explicitly (--ff, --head),
+# so the suite is deterministic and needs no repository, no commits, and no
+# git. hook.c asks git those questions in production; the answers it gets are
+# what the tests below pin the consequences of.
+#
+# POSIX sh: this runs under dash, busybox ash and bash alike, because the
+# container it has to survive is Alpine.
+
+AUTHZ=${AUTHZ:-./git-authz}
+if [ ! -x "$AUTHZ" ]; then
+ printf 'run.sh: no executable at %s (run it from the package root, or set AUTHZ)\n' "$AUTHZ" >&2
+ exit 1
+fi
+# A hook is a symlink to the program, so the link has to be written with a path
+# that means the same thing from inside the temporary tree.
+AUTHZ_ABS=$(CDPATH= cd -- "$(dirname -- "$AUTHZ")" && pwd)/$(basename -- "$AUTHZ")
+
+TMP=${TMPDIR:-/tmp}/git-authz-run.$$
+P=$TMP/authz.conf
+passed=0
+failed=0
+skipped=0
+
+cleanup() {
+ chmod -R u+rwX "$TMP" 2>/dev/null
+ rm -rf "$TMP"
+}
+trap 'cleanup; exit 130' INT TERM
+trap cleanup EXIT
+
+mkdir -p "$TMP" || exit 1
+
+ZERO=0000000000000000000000000000000000000000
+A=1111111111111111111111111111111111111111
+B=2222222222222222222222222222222222222222
+C=3333333333333333333333333333333333333333
+
+# ------------------------------------------------------------------ harness
+
+section() { printf '\n# %s\n' "$1"; }
+
+ok() { passed=$((passed + 1)); }
+bad() { failed=$((failed + 1)); printf 'not ok: %s\n' "$1"; }
+skip_it() {
+ skipped=$((skipped + 1))
+ printf 'ok (skipped): %s - %s\n' "$1" "$2"
+}
+
+# expect
+expect() {
+ if [ "$4" != "$2" ]; then
+ bad "$1: exit $4, wanted $2 (said: $5)"
+ elif [ "$3" != "-" ] && ! contains "$5" "$3"; then
+ bad "$1: said \"$5\", wanted it to contain \"$3\""
+ else
+ ok
+ fi
+}
+
+# expect_not
+expect_not() {
+ if [ "$4" != "$2" ]; then
+ bad "$1: exit $4, wanted $2 (said: $5)"
+ elif [ "$3" != "-" ] && contains "$5" "$3"; then
+ bad "$1: said \"$5\", which contains the forbidden \"$3\""
+ else
+ ok
+ fi
+}
+
+contains() {
+ case $1 in
+ *"$2"*) return 0 ;;
+ *) return 1 ;;
+ esac
+}
+
+# The policy from a here-document.
+policy() {
+ cat >"$P"
+}
+
+# check [ref]
+do_check() {
+ "$AUTHZ" --policy "$POLICY" check "$@" 2>&1
+}
+
+# Which policy the two drivers below read. Overridden per test with
+# "out=$(POLICY=something push ...)", which is a subshell and therefore does not
+# leak into the next test.
+POLICY=$P
+
+# push [extra authorize args] (ref lines on stdin)
+push() {
+ user=$1
+ repo=$2
+ shift 2
+ if [ "$user" = none ]; then
+ env -u GIT_HOST_USER -u REMOTE_USER GIT_HOST_REPO="$repo" \
+ "$AUTHZ" --policy "$POLICY" authorize --no-git "$@" 2>&1
+ else
+ GIT_HOST_USER="$user" GIT_HOST_REPO="$repo" \
+ "$AUTHZ" --policy "$POLICY" authorize --no-git "$@" 2>&1
+ fi
+}
+
+# hook (invoked through the hook name, as git does, with
+# the repository as the working directory and git's own argument in hand)
+# Reached the way core.hooksPath reaches it: by name, with the repository as the
+# working directory, git's own argument in hand (which is not ours, and must not
+# be read as one), and no --policy anywhere - the hook gets its policy from
+# GIT_HOST_AUTHZ or the default path, exactly as a deployed one does.
+hook() {
+ user=$1
+ dir=$2
+ if [ "$user" = none ]; then
+ ( cd "$dir" && env -u GIT_HOST_USER -u REMOTE_USER GIT_HOST_AUTHZ="$POLICY" \
+ "$TMP/bin/pre-receive" "$dir" 2>&1 )
+ else
+ ( cd "$dir" && env GIT_HOST_USER="$user" GIT_HOST_AUTHZ="$POLICY" \
+ "$TMP/bin/pre-receive" "$dir" 2>&1 )
+ fi
+}
+
+# ------------------------------------------------------------------ fixture
+
+section "fixture"
+
+cat >"$TMP/authz.conf" <<'EOF'
+# who may move which refs
+alice * admin # the one account that can do anything
+@all * write refs/heads/feature/* # anyone the policy names: working branches
+frank team write refs/heads/main refs/tags/* # releases, and nothing else
+bob team write refs/heads/feature/*
+carol team force refs/heads/feature/*
+dave team admin
+erin secrets admin
+EOF
+
+# A repository directory, and the hook name this binary is reached under.
+mkdir -p "$TMP/root/team.git" "$TMP/bin"
+ln -sf "$AUTHZ_ABS" "$TMP/bin/pre-receive"
+if [ ! -x "$TMP/bin/pre-receive" ]; then
+ cp "$AUTHZ" "$TMP/bin/pre-receive"
+fi
+chmod 755 "$TMP/bin/pre-receive"
+if [ ! -L "$TMP/bin/pre-receive" ]; then
+ skip_it "hook reached by its own name" "cannot make a symlink here"
+fi
+
+# A policy with no '@all' line, for the tests that need a repository and a
+# stranger rather than a policy that reaches everywhere.
+cat >"$TMP/plain.conf" <<'EOF'
+alice * admin
+bob team write refs/heads/feature/*
+EOF
+
+out=$("$AUTHZ" --policy "$P" validate --no-host 2>&1)
+rc=$?
+expect "the fixture policy is sound" 0 "is in force" "$rc" "$out"
+
+# ---------------------------------------------------------------- usage
+
+section "usage and exit codes"
+
+out=$("$AUTHZ" --policy "$P" 2>&1); rc=$?
+expect "no command is a usage error" 64 "usage:" "$rc" "$out"
+out=$("$AUTHZ" --policy "$P" frobnicate 2>&1); rc=$?
+expect "unknown command" 64 "unknown command" "$rc" "$out"
+out=$("$AUTHZ" --policy "$P" check alice web 2>&1); rc=$?
+expect "check wants three arguments" 64 "wants " "$rc" "$out"
+out=$("$AUTHZ" --policy "$P" check alice web read 2>&1); rc=$?
+expect "'read' is not a level" 64 "unknown access level" "$rc" "$out"
+out=$("$AUTHZ" --help 2>&1); rc=$?
+expect "--help" 0 "Exit status" "$rc" "$out"
+out=$("$AUTHZ" --policy "$P" authorize --wat 2>&1 &1); rc=$?
+expect "a granted admin can write anything" 0 "allow" "$rc" "$out"
+
+# ------------------------------------------------------------------ check
+
+section "check: the ladder"
+
+out=$(do_check bob team write refs/heads/feature/login); rc=$?
+expect "write on a listed pattern" 0 "allow" "$rc" "$out"
+out=$(do_check bob team force refs/heads/feature/login); rc=$?
+expect "write is not force" 1 "needs force" "$rc" "$out"
+out=$(do_check carol team force refs/heads/feature/login); rc=$?
+expect "force on a listed pattern" 0 "allow" "$rc" "$out"
+out=$(do_check carol team admin refs/heads/feature/login); rc=$?
+expect "force is not admin" 1 "needs admin" "$rc" "$out"
+out=$(do_check dave team admin refs/heads/anything); rc=$?
+expect "admin on the whole repository" 0 "allow" "$rc" "$out"
+out=$(do_check dave team write refs/heads/anything); rc=$?
+expect "admin answers a write too" 0 "allow" "$rc" "$out"
+out=$(POLICY="$TMP/plain.conf" do_check erin other write refs/heads/dev); rc=$?
+expect "a stranger to the repository" 1 "no rule for other" "$rc" "$out"
+out=$(do_check erin other write refs/heads/feature/x); rc=$?
+expect "unless '@all' reaches that repository too" 0 "allow" "$rc" "$out"
+
+section "check: names mean what they say"
+
+out=$(do_check bob team.git write refs/heads/feature/x); rc=$?
+expect "team.git is team" 0 "allow" "$rc" "$out"
+out=$(do_check bob team write main); rc=$?
+expect "a bare name is a branch" 1 "refs/heads/main" "$rc" "$out"
+out=$(do_check bob team write heads/main); rc=$?
+expect "heads/main is the same branch" 1 "refs/heads/main" "$rc" "$out"
+out=$(do_check bob team write refs/heads/main); rc=$?
+expect "and so is refs/heads/main" 1 "refs/heads/main" "$rc" "$out"
+out=$(do_check bob team write v1); rc=$?
+expect "a bare v1 is a branch, never a tag" 1 "refs/heads/v1" "$rc" "$out"
+out=$(do_check bob team write tags/v1); rc=$?
+expect "and it takes tags/v1 to mean a tag" 1 "refs/tags/v1" "$rc" "$out"
+out=$(do_check dave team write refs/tags/v1); rc=$?
+expect "an admin can tag" 0 "allow" "$rc" "$out"
+out=$(do_check carol team write refs/heads/feature/deep/nested); rc=$?
+expect "* crosses a slash" 0 "allow" "$rc" "$out"
+
+section "check: a rule covers what it names, and nothing else"
+
+out=$(do_check bob team write refs/heads/main); rc=$?
+expect "bob's rules never name main" 1 "but not on refs/heads/main" "$rc" "$out"
+out=$(do_check carol team force refs/heads/main); rc=$?
+expect "and neither do carol's, at any level" 1 "but not on refs/heads/main" "$rc" "$out"
+out=$(do_check frank team write refs/heads/main); rc=$?
+expect "frank's rule does name it, so he may move it" 0 "allow" "$rc" "$out"
+out=$(do_check frank team write refs/heads/feature/x); rc=$?
+expect "and he reaches the '@all' rule like anyone else named" 0 "allow" "$rc" "$out"
+out=$(do_check frank team force refs/heads/main); rc=$?
+expect "naming a ref is not the same as holding every level on it" 1 "needs force" "$rc" "$out"
+out=$(do_check dave team force refs/heads/main); rc=$?
+expect "an unscoped admin covers main too" 0 "allow" "$rc" "$out"
+out=$(do_check bob team write refs/tags/v1); rc=$?
+expect "tags have to be named as well" 1 "refs/tags/v1" "$rc" "$out"
+out=$(do_check frank team write refs/tags/v1); rc=$?
+expect "and frank named his" 0 "allow" "$rc" "$out"
+
+section "check: refs that are neither branch nor tag"
+
+out=$(do_check bob team write refs/pull/1/head); rc=$?
+expect "a fetch ref needs admin" 1 "needs admin" "$rc" "$out"
+out=$(do_check dave team write refs/pull/1/head); rc=$?
+expect "an admin moves anything" 0 "allow" "$rc" "$out"
+out=$(do_check alice secrets write refs/notes/x); rc=$?
+expect "notes are for admins" 0 "allow" "$rc" "$out"
+
+section "check: '@all' is everyone the policy names"
+
+out=$(do_check carol web write refs/heads/feature/any); rc=$?
+expect "a named user reaches the @all rule" 0 "allow" "$rc" "$out"
+out=$(do_check nobody team write refs/heads/dev); rc=$?
+expect "a name the policy does not have gets nothing" 1 "no rule" "$rc" "$out"
+out=$(do_check '' team write refs/heads/dev); rc=$?
+expect "an empty identity is no identity" 1 "no identity" "$rc" "$out"
+
+section "check: the question with no ref"
+
+out=$(do_check dave team force); rc=$?
+expect "an unscoped admin has force somewhere" 0 "allow" "$rc" "$out"
+out=$(do_check bob team write); rc=$?
+expect "a scoped rule is 'somewhere'" 0 "some refs" "$rc" "$out"
+out=$(do_check bob team force); rc=$?
+expect "but not at a level he lacks" 1 "no force" "$rc" "$out"
+out=$(do_check alice web admin); rc=$?
+expect "an unscoped rule is 'all of'" 0 "all of web" "$rc" "$out"
+
+# -------------------------------------------------------- authorize (hook)
+
+section "authorize: what git hands the hook"
+
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
+ push bob team --explain); rc=$?
+expect "creating a branch is write" 0 "allow" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/notes/x |
+ push bob team); rc=$?
+expect "a ref that is neither a branch nor a tag is for admins" 1 "needs admin" "$rc" "$out"
+out=$(POLICY="$TMP/plain.conf" printf '%s %s %s\n' "$ZERO" "$A" refs/heads/release |
+ POLICY="$TMP/plain.conf" push bob team); rc=$?
+expect "a branch outside the pattern is denied" 1 "deny" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
+ push bob team --ff=yes --explain); rc=$?
+expect "a fast-forward is write" 0 "allow" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
+ push bob team --ff=no); rc=$?
+expect "a rewrite is force" 1 "needs force" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
+ push carol other --ff=no); rc=$?
+expect "a rewrite in a repository she only reaches through @all" 1 "needs force" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
+ push bob team); rc=$?
+expect "deleting is force" 1 "needs force" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
+ push carol team); rc=$?
+expect "carol may delete her own kind of branch" 0 "-" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
+ push carol team --head=refs/heads/dev); rc=$?
+expect "deleting the default branch is admin" 1 "needs admin" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
+ push carol team --head=refs/heads/other); rc=$?
+expect "deleting a branch she holds force on is allowed" 0 "-" "$rc" "$out"
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
+ push carol team --head=refs/heads/other); rc=$?
+expect "and only force on that ref will do, not force somewhere else" 1 \
+ "carol holds force on team, but not on refs/heads/dev" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
+ push dave team --head=refs/heads/dev); rc=$?
+expect "an admin deletes the default branch" 0 "-" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
+ env PATH=/nonexistent GIT_HOST_USER=carol GIT_HOST_REPO=team \
+ "$AUTHZ" --policy "$P" authorize 2>&1); rc=$?
+expect "a HEAD that cannot be read is charged as the default branch" 1 "needs admin" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
+ push bob team --ff=auto); rc=$?
+expect "with no git to ask, an update is a rewrite" 1 "needs force" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$B" refs/pull/7/head |
+ push dave team); rc=$?
+expect "an admin writes a fetch ref" 0 "-" "$rc" "$out"
+
+section "authorize: identity"
+
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | push none team); rc=$?
+expect "anonymous is denied" 1 "no identity" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
+ env -u GIT_HOST_USER REMOTE_USER=bob GIT_HOST_REPO=team \
+ "$AUTHZ" --policy "$P" authorize --no-git 2>&1); rc=$?
+expect "REMOTE_USER is an identity too (http)" 0 "-" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
+ env GIT_HOST_USER=carol REMOTE_USER=bob GIT_HOST_REPO=team \
+ "$AUTHZ" --policy "$P" authorize --no-git --explain 2>&1); rc=$?
+expect "GIT_HOST_USER wins over REMOTE_USER" 0 "carol has write" "$rc" "$out"
+
+out=$(POLICY="$TMP/plain.conf" printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
+ POLICY="$TMP/plain.conf" push bob team --explain); rc=$?
+expect "the named repository is allowed" 0 "on team" "$rc" "$out"
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
+ POLICY="$TMP/plain.conf" push bob other --explain); rc=$?
+expect "another repository is not" 1 "no rule for other" "$rc" "$out"
+
+section "authorize: several refs, and the first denial"
+
+out=$(printf '%s %s %s\n%s %s %s\n%s %s %s\n' \
+ "$ZERO" "$A" refs/heads/feature/one \
+ "$ZERO" "$B" refs/heads/feature/two \
+ "$ZERO" "$C" refs/heads/feature/three |
+ push bob team --explain); rc=$?
+expect "a whole push of allowed refs" 0 "feature/three" "$rc" "$out"
+
+out=$(printf '%s %s %s\n%s %s %s\n' \
+ "$ZERO" "$A" refs/heads/feature/one \
+ "$ZERO" "$B" refs/heads/main |
+ push bob team); rc=$?
+expect "one ref nobody may stop the whole push" 1 "refs/heads/main" "$rc" "$out"
+expect_not "and the refs before it are not reported either" 1 "feature/one" "$rc" "$out"
+
+out=$(printf '%s %s %s\n%s %s %s\n' \
+ "$ZERO" "$A" refs/heads/main \
+ "$ZERO" "$B" refs/heads/feature/one |
+ push bob team); rc=$?
+expect "the refusal says who, what and what it would take" 1 \
+ "bob holds write on team, but not on refs/heads/main" "$rc" "$out"
+expect_not "and stops at the first refusal" 1 "feature/one" "$rc" "$out"
+
+section "authorize: input that is not a push"
+
+out=$(printf 'nonsense\n' | push bob team); rc=$?
+expect "a line with too few fields" 1 "expected" "$rc" "$out"
+out=$(printf '%s %s %s\n' "$A" "$B" heads/feature/x | push bob team); rc=$?
+expect "a refname that is not a refname" 1 "not an update git would send" "$rc" "$out"
+out=$(printf '%s %s %s\n' notanoid "$B" refs/heads/x | push bob team); rc=$?
+expect "an object id that is not one" 1 "not an update git would send" "$rc" "$out"
+out=$(printf '' | push bob team); rc=$?
+expect "no refs at all is nothing to deny" 0 "-" "$rc" "$out"
+out=$(printf '\n\n' | push bob team); rc=$?
+expect "blank lines are skipped" 0 "-" "$rc" "$out"
+
+# --------------------------------------------------------- the hook by name
+
+section "authorize: reached as pre-receive"
+
+out=$(POLICY="$TMP/plain.conf" printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
+ POLICY="$TMP/plain.conf" hook bob "$TMP/root/team.git"); rc=$?
+expect "the hook name selects authorize, and git's argument is ignored" 0 "-" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/release |
+ POLICY="$TMP/plain.conf" hook bob "$TMP/root/team.git"); rc=$?
+expect "and denies what authorize denies, on the repository it found" 1 \
+ "bob holds write on team, but not on refs/heads/release" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
+ POLICY="$TMP/plain.conf" hook none "$TMP/root/team.git"); rc=$?
+expect "an anonymous key is denied at the hook" 1 "no identity" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
+ POLICY="$TMP/broken.conf" hook bob "$TMP/root/team.git"); rc=$?
+expect "a hook on a policy that does not parse stops the push" 3 "deny" "$rc" "$out"
+
+# the repository, taken from the working directory under GIT_REPO_ROOT
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
+ ( cd "$TMP/root/team.git" && env -u GIT_HOST_REPO GIT_HOST_USER=bob \
+ GIT_REPO_ROOT="$TMP/root" "$AUTHZ_ABS" --policy "$P" authorize --no-git --explain 2>&1 )); rc=$?
+expect "GIT_REPO_ROOT plus the cwd names the repository" 0 "on team" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
+ ( cd "$TMP/root/team.git" && env -u GIT_HOST_REPO GIT_HOST_USER=bob \
+ GIT_REPO_ROOT=/var/lib/git "$AUTHZ_ABS" --policy "$P" authorize --no-git --explain 2>&1 )); rc=$?
+expect "and the last path component works when it is not the root" 0 "team" "$rc" "$out"
+
+# -------------------------------------------------------------- fail closed
+
+section "fail closed"
+
+printf 'alice web admin\nbroken line\n' >"$TMP/broken.conf"
+out=$("$AUTHZ" --policy "$TMP/broken.conf" check alice web admin refs/heads/x 2>&1); rc=$?
+expect "a policy that does not parse denies" 3 "does not parse" "$rc" "$out"
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/x |
+ env GIT_HOST_USER=alice GIT_HOST_REPO=web "$AUTHZ" --policy "$TMP/broken.conf" \
+ authorize --no-git 2>&1); rc=$?
+expect "and the hook denies" 3 "deny" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/no-such.conf" check alice web admin 2>&1); rc=$?
+expect "a policy that is not there denies" 3 "cannot be read" "$rc" "$out"
+out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/x |
+ env GIT_HOST_USER=alice GIT_HOST_REPO=web "$AUTHZ" --policy "$TMP/no-such.conf" \
+ authorize 2>&1); rc=$?
+expect "and the hook denies" 3 "deny" "$rc" "$out"
+
+printf 'alice web admin\n' >"$TMP/secret.conf"
+chmod 000 "$TMP/secret.conf"
+if [ "$(id -u)" = 0 ]; then
+ skip_it "an unreadable policy denies" "running as root, who can read anything"
+else
+ out=$("$AUTHZ" --policy "$TMP/secret.conf" check alice web admin 2>&1); rc=$?
+ expect "an unreadable policy denies" 3 "cannot be read" "$rc" "$out"
+fi
+chmod 644 "$TMP/secret.conf"
+
+# ---------------------------------------------------------------- validate
+
+section "validate: the policy alone"
+
+POLICY=$P
+out=$("$AUTHZ" --policy "$P" validate --no-host 2>&1); rc=$?
+expect "the fixture passes" 0 "is in force" "$rc" "$out"
+expect_not "with nothing to complain about" 0 "warn " "$rc" "$out"
+
+out=$("$AUTHZ" --policy "$TMP/broken.conf" validate --no-host 2>&1); rc=$?
+expect "a broken policy is reported with its line number" 3 ":2: error" "$rc" "$out"
+
+cat >"$TMP/noise.conf" <<'EOF'
+@all web write
+bob missing write refs/heads/*
+EOF
+out=$("$AUTHZ" --policy "$TMP/noise.conf" validate --no-host 2>&1); rc=$?
+expect "warnings do not fail a validate" 0 "warning(s)" "$rc" "$out"
+contains "$out" "@all the whole of web" || bad "an unscoped @all should be warned about"
+out=$("$AUTHZ" --policy "$TMP/noise.conf" validate --no-host --strict 2>&1); rc=$?
+expect "--strict makes warnings fatal" 1 "warning" "$rc" "$out"
+
+out=$("$AUTHZ" --policy "$TMP/nope.conf" validate --no-host 2>&1); rc=$?
+expect "a missing policy is not a valid policy" 3 "cannot be read" "$rc" "$out"
+
+section "validate: the host around it"
+
+mkhost() {
+ H=$TMP/host
+ rm -rf "$H"
+ mkdir -p "$H/hooks" "$H/repos/team.git" "$H/repos/secrets.git" "$H/secrets"
+ ln -sf "$AUTHZ_ABS" "$H/hooks/pre-receive" 2>/dev/null || cp "$AUTHZ" "$H/hooks/pre-receive"
+ printf '[core]\n\thooksPath = %s/hooks\n' "$H" >"$H/gitconfig"
+ printf 'PermitUserEnvironment yes\nForceCommand /usr/bin/git-shell\nAuthorizedKeysFile %s/secrets/authorized_keys\n' "$H" >"$H/sshd_config"
+ printf 'git:x:1000:1000::/var/lib/git:/usr/bin/git-shell\n' >"$H/passwd"
+ printf 'ssh-ed25519 AAAA bob\n' >"$H/secrets/authorized_keys"
+ printf 'environment="GIT_HOST_USER=bob" ssh-ed25519 BBBB\n' >>"$H/secrets/authorized_keys"
+ printf 'bob:$2y$05$hash\n' >"$H/secrets/htpasswd"
+ cat >"$H/authz.conf" <<'POL'
+alice * admin
+bob team write refs/heads/feature/*
+POL
+}
+
+host_validate() {
+ "$AUTHZ" --policy "$H/authz.conf" validate \
+ --root "$H/repos" --hook-dir "$H/hooks" --gitconfig "$H/gitconfig" \
+ --sshd-config "$H/sshd_config" --passwd "$H/passwd" \
+ --keys "$H/secrets/authorized_keys" --htpasswd "$H/secrets/htpasswd" "$@" 2>&1
+}
+
+mkhost
+out=$(host_validate); rc=$?
+expect "a wired-up host passes" 0 "is in force" "$rc" "$out"
+expect "and says what it checked" 0 "ForceCommand" "$rc" "$out"
+
+sed "s|hooksPath = .*|hooksPath = /somewhere/else|" "$H/gitconfig" >"$H/gitconfig2"
+mv "$H/gitconfig2" "$H/gitconfig"
+out=$(host_validate); rc=$?
+expect "the wrong core.hooksPath is an error" 1 "checked by something else" "$rc" "$out"
+
+sed "s|hooksPath|hooks-path|" "$H/gitconfig" >"$H/gitconfig2"
+mv "$H/gitconfig2" "$H/gitconfig"
+out=$(host_validate); rc=$?
+expect "no core.hooksPath at all is an error" 1 "does not set core.hooksPath" "$rc" "$out"
+
+mkhost
+mv "$H/hooks/pre-receive" "$H/hooks/away"
+out=$(host_validate); rc=$?
+expect "a missing hook is an error" 1 "every push would be accepted unchecked" "$rc" "$out"
+mv "$H/hooks/away" "$H/hooks/pre-receive"
+
+mkhost
+sed "s/PermitUserEnvironment yes/PermitUserEnvironment no/" "$H/sshd_config" >"$H/s2"
+mv "$H/s2" "$H/sshd_config"
+out=$(host_validate); rc=$?
+expect "PermitUserEnvironment no means anonymous pushes" 1 "anonymous" "$rc" "$out"
+
+mkhost
+sed "s|^ForceCommand.*|# no force command|" "$H/sshd_config" >"$H/s2"
+mv "$H/s2" "$H/sshd_config"
+out=$(host_validate); rc=$?
+expect "no ForceCommand is an error" 1 "interactive shell" "$rc" "$out"
+
+mkhost
+sed "s|git-shell|/bin/sh|" "$H/passwd" >"$H/p2"
+mv "$H/p2" "$H/passwd"
+out=$(host_validate); rc=$?
+expect "a git account with a real shell is an error" 1 "not git-shell" "$rc" "$out"
+
+mkhost
+printf 'environment="GIT_HOST_USER=ghost" ssh-ed25519 CCCC\n' >"$H/secrets/authorized_keys"
+out=$(host_validate); rc=$?
+expect "a key identity with no rule is a warning" 0 "ghost" "$rc" "$out"
+out=$(host_validate --strict); rc=$?
+expect "and fatal under --strict" 1 "ghost" "$rc" "$out"
+
+mkhost
+printf 'ssh-ed25519 AAAAanonymouskey\n' >>"$H/secrets/authorized_keys"
+out=$(host_validate); rc=$?
+expect "an identity-less key is a warning" 0 "no GIT_HOST_USER" "$rc" "$out"
+
+mkhost
+printf 'mallory:$2y$05$hash\n' >>"$H/secrets/htpasswd"
+out=$(host_validate); rc=$?
+expect "an htpasswd user with no rule is a warning" 0 "change nothing" "$rc" "$out"
+
+mkhost
+printf 'bob team write\n' >"$H/authz.conf"
+mkdir -p "$H/repos/orphan.git"
+out=$(host_validate); rc=$?
+expect "a repository nobody can push to is a warning" 0 "orphan" "$rc" "$out"
+
+mkhost
+printf 'bob nowhere write\n' >>"$H/authz.conf"
+out=$(host_validate); rc=$?
+expect "a rule on a repository that is not there is a warning" 0 "nowhere" "$rc" "$out"
+
+mkhost
+printf 'alice web admin\nbob nowhere write refs/heads/*\n' >"$H/authz.conf"
+printf 'mallory:$2y$05$x\n' >"$H/secrets/htpasswd"
+mkdir -p "$H/repos/orphan.git"
+out=$(host_validate); rc=$?
+if contains "$out" "orphan" && contains "$out" "mallory" &&
+ contains "$out" "nowhere" && contains "$out" "web"; then
+ ok
+else
+ bad "several warnings at once: said $out"
+fi
+expect "warnings alone still let the host serve" 0 "is in force" "$rc" "$out"
+
+# -------------------------------------------------------------- edit (grant)
+
+section "grant and revoke"
+
+cat >"$TMP/edit.conf" <<'EOF'
+# policy, kept in git and rolled out as a ConfigMap
+alice * admin # break glass
+bob team write refs/heads/feature/*
+EOF
+
+out=$("$AUTHZ" --policy "$TMP/edit.conf" grant carol team force refs/heads/feature/* 2>&1); rc=$?
+expect "grant appends" 0 "appended" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" check carol team force refs/heads/feature/x 2>&1); rc=$?
+expect "and the grant is in force immediately" 0 "allow" "$rc" "$out"
+if grep -q '^# policy, kept in git' "$TMP/edit.conf" &&
+ grep -q 'break glass' "$TMP/edit.conf"; then
+ ok
+else
+ bad "the comments survived neither"
+fi
+
+out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team admin 2>&1); rc=$?
+expect "grant replaces the pair's rule" 0 "replaced line 3" "$rc" "$out"
+n=$(grep -c '^bob' "$TMP/edit.conf")
+[ "$n" = 1 ] && ok || bad "bob's rule is not collapsed into one line ($n lines)"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" check bob team admin refs/heads/main 2>&1); rc=$?
+expect "and the new level applies" 0 "allow" "$rc" "$out"
+
+out=$("$AUTHZ" --policy "$TMP/edit.conf" revoke bob team 2>&1); rc=$?
+expect "revoke removes the rule" 0 "revoked bob" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" check bob team admin refs/heads/dev 2>&1); rc=$?
+expect "and the access is gone" 1 "no rule" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" revoke bob team 2>&1); rc=$?
+expect "revoking nothing reports nothing" 1 "no rule for bob" "$rc" "$out"
+
+out=$("$AUTHZ" --policy "$TMP/edit.conf" grant frank team write refs/heads/main refs/tags/release/* 2>&1); rc=$?
+expect "grant can name several refs" 0 "granted frank" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" check frank team write refs/heads/main 2>&1); rc=$?
+expect "and the list is in force" 0 "allow" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" check frank team write refs/heads/dev 2>&1); rc=$?
+expect "and covers nothing else of his own" 1 "but not on refs/heads/dev" "$rc" "$out"
+if grep -q '^frank team write refs/heads/main refs/tags/release/\*$' "$TMP/edit.conf"; then
+ ok
+else
+ bad "the refs did not land on one line as written"
+fi
+
+out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team nope 2>&1); rc=$?
+expect "grant rejects a level that is not one" 64 "unknown access level" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob group/team write 2>&1); rc=$?
+expect "grant rejects a repository name with a slash in it" 64 "invalid repo pattern" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write '!refs/heads/main' 2>&1); rc=$?
+expect "grant has no deny" 64 "no deny" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" protect refs/heads/main 2>&1); rc=$?
+expect "protect is gone, and says it is unknown" 64 "unknown command" "$rc" "$out"
+printf 'set protected-refs refs/heads/main\n' >>"$TMP/edit.conf"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" check alice team admin refs/heads/main 2>&1); rc=$?
+expect "a policy that still carries protected-refs is refused, not ignored" 3 "no longer exists" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" validate --no-host 2>&1); rc=$?
+expect "and validate says every push is denied" 3 "not used" "$rc" "$out"
+grep -v 'protected-refs' "$TMP/edit.conf" > "$TMP/edit.conf.clean" &&
+ cp "$TMP/edit.conf.clean" "$TMP/edit.conf"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" validate --no-host 2>&1); rc=$?
+expect "cleaned, it validates again" 0 "is in force" "$rc" "$out"
+
+cp "$TMP/edit.conf" "$TMP/edit.conf.orig"
+out=$("$AUTHZ" --policy "$TMP/broken.conf" grant bob team write 2>&1); rc=$?
+expect "editing a policy that does not parse is refused" 3 "does not parse" "$rc" "$out"
+cmp -s "$TMP/edit.conf" "$TMP/edit.conf.orig" || bad "a refused edit still changed the file"
+
+section "edit: where the result goes"
+
+out=$("$AUTHZ" --policy "$TMP/edit.conf" grant erin secrets admin --out "$TMP/other.conf" 2>&1); rc=$?
+expect "--out writes elsewhere" 0 "$TMP/other.conf" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/other.conf" check erin secrets admin refs/heads/main 2>&1); rc=$?
+expect "and the new file works" 0 "allow" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" check erin secrets admin refs/heads/main 2>&1); rc=$?
+expect "and the original does not have it" 1 "no rule" "$rc" "$out"
+
+out=$("$AUTHZ" --policy "$TMP/edit.conf" grant erin secrets admin --stdout 2>&1 >/dev/null); rc=$?
+expect "--stdout reports on stderr" 0 "untouched" "$rc" "$out"
+cp "$TMP/edit.conf" "$TMP/edit.conf.bak"
+out=$("$AUTHZ" --policy "$TMP/edit.conf" revoke carol team --stdout 2>/dev/null); rc=$?
+expect "--stdout prints the new file" 0 "alice * admin" "$rc" "$out"
+cmp -s "$TMP/edit.conf" "$TMP/edit.conf.bak" && ok || bad "--stdout modified the policy"
+if contains "$out" "alice * admin" && contains "$out" "break glass"; then
+ ok
+else
+ bad "--stdout lost the comments or the settings"
+fi
+
+# A file whose *directory* is read-only cannot be replaced, which is what a
+# read-only ConfigMap mount means in practice. A 0444 file in a writable
+# directory is a different thing and worth pinning too: the grant succeeds and
+# the mode survives, because it is the directory that decides.
+cp "$TMP/edit.conf" "$TMP/ro.conf"
+chmod 444 "$TMP/ro.conf"
+out=$("$AUTHZ" --policy "$TMP/ro.conf" grant bob team admin 2>&1); rc=$?
+if [ "$(id -u)" = 0 ]; then
+ skip_it "an unwritable directory is refused" "running as root, who defeats chmod"
+ expect "the mode of a 0444 policy survives a write" 0 "granted" "$rc" "$out"
+ mode=$(ls -l "$TMP/ro.conf" | cut -c2-10)
+ [ "$mode" = "r--r--r--" ] && ok || bad "a 0444 policy came back as $mode"
+else
+ mkdir -p "$TMP/rodir"
+ cp "$TMP/edit.conf" "$TMP/rodir/policy.conf"
+ chmod 555 "$TMP/rodir"
+ out=$("$AUTHZ" --policy "$TMP/rodir/policy.conf" grant bob team admin 2>&1); rc=$?
+ expect "a policy in a read-only directory cannot be replaced" 1 "cannot write beside" "$rc" "$out"
+ chmod 755 "$TMP/rodir"
+fi
+out=$("$AUTHZ" --policy "$TMP/ro.conf" grant bob team admin --out - 2>/dev/null); rc=$?
+expect "--out works whatever the input's mode is" 0 "bob team admin" "$rc" "$out"
+
+section "edit: leaving the file alone"
+
+cat >"$TMP/mode.conf" <<'EOF'
+alice * admin
+EOF
+chmod 640 "$TMP/mode.conf"
+out=$("$AUTHZ" --policy "$TMP/mode.conf" grant bob team write 2>&1); rc=$?
+expect "a grant succeeds" 0 "granted" "$rc" "$out"
+mode=$(ls -l "$TMP/mode.conf" | cut -c2-10)
+[ "$mode" = "rw-r-----" ] && ok || bad "the mode changed to $mode"
+left=$(ls -a "$TMP" | grep -c '^\.authz')
+[ "$left" = 0 ] && ok || bad "$left temporary file(s) left behind"
+
+printf 'alice * admin' >"$TMP/nonl.conf" # no final newline
+out=$("$AUTHZ" --policy "$TMP/nonl.conf" grant bob team write 2>&1); rc=$?
+expect "a file without a trailing newline is editable" 0 "granted" "$rc" "$out"
+[ "$(tail -c 1 "$TMP/nonl.conf" | wc -l)" = 1 ] && ok || bad "the last line still has no newline"
+
+out=$("$AUTHZ" --policy "$TMP/fresh.conf" grant alice '*' admin 2>&1); rc=$?
+expect "grant can create the first rule of a new policy" 0 "granted" "$rc" "$out"
+out=$("$AUTHZ" --policy "$TMP/fresh.conf" check alice anything admin refs/heads/main 2>&1); rc=$?
+expect "and the new policy works" 0 "allow" "$rc" "$out"
+
+out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write --out "$TMP/nodir/x.conf" 2>&1); rc=$?
+expect "writing where the directory is not there fails" 1 "cannot write beside" "$rc" "$out"
+
+# ------------------------------------------------------------------ install
+
+section "install"
+
+if ! command -v install >/dev/null 2>&1 || ! command -v make >/dev/null 2>&1; then
+ skip_it "make install" "no make or install(1) here"
+ skip_it "the hook the installer leaves" "no make or install(1) here"
+ skip_it "an existing policy is not overwritten" "no make or install(1) here"
+else
+ S=$TMP/stage
+ out=$(make install DESTDIR="$S" 2>&1); rc=$?
+ expect "make install into a DESTDIR" 0 "pre-receive" "$rc" "$out"
+
+ # The hook has to be a relative link: staged, unpacked, or shipped in an
+ # image, it must point at the binary that came with it rather than at
+ # whatever occupies that absolute path on the machine reading it.
+ link=$(ls -l "$S/usr/local/share/git-host/hooks/pre-receive")
+ case $link in
+ *'= /'*) bad "the installed hook is an absolute symlink: $link" ;;
+ *'pre-receive -> ../../'*) ok ;;
+ *) bad "the installed hook is not a relative symlink: $link" ;;
+ esac
+
+ # End to end through the installed name, with the installed policy, in a
+ # repository under an installed root: nothing but the paths from the
+ # installer here.
+ mkdir -p "$S/var/lib/git/team.git"
+ out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
+ ( cd "$S/var/lib/git/team.git" && env GIT_HOST_USER=bob \
+ GIT_REPO_ROOT="$S/var/lib/git" GIT_HOST_AUTHZ="$S/etc/git-host/authz.conf" \
+ "$S/usr/local/share/git-host/hooks/pre-receive" "." 2>&1 )); rc=$?
+ expect "the installed hook allows what the policy allows" 0 "-" "$rc" "$out"
+
+ out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/main |
+ ( cd "$S/var/lib/git/team.git" && env GIT_HOST_USER=bob \
+ GIT_REPO_ROOT="$S/var/lib/git" GIT_HOST_AUTHZ="$S/etc/git-host/authz.conf" \
+ "$S/usr/local/share/git-host/hooks/pre-receive" "." 2>&1 )); rc=$?
+ expect "and denies what it denies" 1 "deny" "$rc" "$out"
+ contains "$out" "pre-receive:" || bad "the hook should say so in its own name: $out"
+
+ printf 'someone * admin\n' >"$S/etc/git-host/authz.conf"
+ out=$(make install DESTDIR="$S" 2>&1); rc=$?
+ expect "installing again does not fail" 0 "" "$rc" "$out"
+ if grep -q 'someone' "$S/etc/git-host/authz.conf" &&
+ [ -f "$S/etc/git-host/authz.conf.new" ]; then
+ ok
+ else
+ bad "an existing policy was overwritten, or the new example is nowhere"
+ fi
+fi
+
+# ------------------------------------------------------------ the shipped policy
+
+if [ -f authz.conf ]; then
+ section "the example policy that ships with it"
+
+ out=$("$AUTHZ" --policy authz.conf validate --no-host 2>&1); rc=$?
+ expect "it validates" 0 "is in force" "$rc" "$out"
+ out=$("$AUTHZ" --policy authz.conf check alice anything admin refs/heads/main 2>&1); rc=$?
+ expect "alice can do anything" 0 "allow" "$rc" "$out"
+ out=$("$AUTHZ" --policy authz.conf check bob team write refs/heads/feature/x 2>&1); rc=$?
+ expect "bob works on features" 0 "allow" "$rc" "$out"
+ out=$("$AUTHZ" --policy authz.conf check bob team write refs/heads/main 2>&1); rc=$?
+ expect "bob cannot touch main" 1 "but not on refs/heads/main" "$rc" "$out"
+ out=$("$AUTHZ" --policy authz.conf check nobody team write refs/heads/x 2>&1); rc=$?
+ expect "a stranger cannot" 1 "no rule" "$rc" "$out"
+fi
+
+# ------------------------------------------------------------------ summary
+
+printf '\n%s\n' "passed $passed, failed $failed, skipped $skipped"
+[ "$failed" = 0 ] || exit 1
+exit 0
diff --git a/test/test.h b/test/test.h
new file mode 100644
index 0000000..c4f63d0
--- /dev/null
+++ b/test/test.h
@@ -0,0 +1,141 @@
+/* test.h - the little harness the test programs share.
+ *
+ * No test framework, because a test suite with a dependency is a test suite
+ * that does not run at 3am. Each program in test/ is its own: it links the
+ * library, runs its own cases and reports its own count, so one failing file
+ * says which area broke rather than taking the whole run down with it.
+ */
+#ifndef TEST_H
+#define TEST_H
+
+#include "authz.h"
+
+#include
+#include
+#include
+#include
+#include
+#include
+
+static int checks;
+static int failures;
+static char tmproot[128];
+
+#define CHECK(cond) \
+ do { \
+ checks++; \
+ if (!(cond)) { \
+ failures++; \
+ printf("not ok %d: %s [%s:%d]\n", failures, #cond, __FILE__, __LINE__);\
+ } \
+ } while (0)
+
+#define CHECK_STR(got, want) \
+ do { \
+ checks++; \
+ if (strcmp((got), (want)) != 0) { \
+ failures++; \
+ printf("not ok %d: got \"%s\", want \"%s\" [%s:%d]\n", failures, \
+ (got), (want), __FILE__, __LINE__); \
+ } \
+ } while (0)
+
+#define CHECK_INT(got, want) \
+ do { \
+ checks++; \
+ if ((got) != (want)) { \
+ failures++; \
+ printf("not ok %d: got %ld, want %ld [%s:%d]\n", failures, \
+ (long)(got), (long)(want), __FILE__, __LINE__); \
+ } \
+ } while (0)
+
+static inline void note(const char *fmt, ...)
+{
+ va_list ap;
+
+ fputs(" # ", stdout);
+ va_start(ap, fmt);
+ vprintf(fmt, ap);
+ va_end(ap);
+ fputc('\n', stdout);
+}
+
+/* Where the fixtures are written. UNIT_TMPDIR is there for a build that runs
+ * the tests somewhere the source tree is not writable. */
+static inline void test_init(const char *argv0)
+{
+ const char *dir = getenv("UNIT_TMPDIR");
+
+ (void)argv0;
+ snprintf(tmproot, sizeof(tmproot), "%s", dir != NULL ? dir : "test/tmp");
+}
+
+static inline int test_report(const char *what)
+{
+ printf("%s: %d checks, %d failure(s)\n", what, checks, failures);
+ if (failures != 0)
+ note("these are the semantics; the behavioural suite assumes them");
+ return failures != 0;
+}
+
+/* ------------------------------------------------------------- fixtures */
+
+/* A policy from a string. The file is unlinked straight after loading: the
+ * decisions are made in memory, and a test that left policies behind would
+ * leave them to be trusted. */
+static inline int load_text(policy_t *p, const char *text)
+{
+ static int seq;
+ char path[512];
+ FILE *f;
+ int rc;
+
+ snprintf(path, sizeof(path), "%s/unit-%d", tmproot, ++seq);
+ f = fopen(path, "w");
+ if (f == NULL) {
+ printf("not ok %d: cannot write %s\n", ++failures, path);
+ return -1;
+ }
+ fputs(text, f);
+ fclose(f);
+
+ policy_init(p, path);
+ rc = policy_load(p);
+ unlink(path);
+ return rc;
+}
+
+static inline void expect_allow(const policy_t *p, const char *user, const char *repo,
+ level_t need, const char *ref)
+{
+ decision_t d;
+
+ authz_check(p, user, repo, need, ref, &d);
+ checks++;
+ if (!d.allowed) {
+ failures++;
+ printf("not ok %d: expected %s %s %s %s to be allowed, got: %s\n", failures,
+ user, repo, authz_level_name(need), ref != NULL ? ref : "(no ref)", d.why);
+ }
+}
+
+static inline void expect_deny(const policy_t *p, const char *user, const char *repo,
+ level_t need, const char *ref, const char *needle)
+{
+ decision_t d;
+
+ authz_check(p, user, repo, need, ref, &d);
+ checks++;
+ if (d.allowed) {
+ failures++;
+ printf("not ok %d: expected %s %s %s %s to be denied\n", failures, user, repo,
+ authz_level_name(need), ref != NULL ? ref : "(no ref)");
+ } else if (needle != NULL && strstr(d.why, needle) == NULL) {
+ failures++;
+ printf("not ok %d: denial said \"%s\", expected it to mention \"%s\"\n",
+ failures, d.why, needle);
+ }
+}
+
+#endif /* TEST_H */
diff --git a/tests/run.sh b/tests/run.sh
deleted file mode 100644
index 1d8aa7d..0000000
--- a/tests/run.sh
+++ /dev/null
@@ -1,801 +0,0 @@
-#!/bin/sh
-# run.sh - drive git-authz the way git does, without git.
-#
-# Everything a receive hook knows, this feeds it: an identity in the
-# environment, "old new ref" lines on stdin, the repository as the working
-# directory. What only the object store knows - whether old is an ancestor of
-# new, and which branch HEAD points at - is supplied explicitly (--ff, --head),
-# so the suite is deterministic and needs no repository, no commits, and no
-# git. hook.c asks git those questions in production; the answers it gets are
-# what the tests below pin the consequences of.
-#
-# POSIX sh: this runs under dash, busybox ash and bash alike, because the
-# container it has to survive is Alpine.
-
-AUTHZ=${AUTHZ:-./git-authz}
-if [ ! -x "$AUTHZ" ]; then
- printf 'run.sh: no executable at %s (run it from the package root, or set AUTHZ)\n' "$AUTHZ" >&2
- exit 1
-fi
-# A hook is a symlink to the program, so the link has to be written with a path
-# that means the same thing from inside tests/tmp.
-AUTHZ_ABS=$(CDPATH= cd -- "$(dirname -- "$AUTHZ")" && pwd)/$(basename -- "$AUTHZ")
-
-TMP=${TMPDIR:-/tmp}/git-authz-run.$$
-P=$TMP/authz.conf
-passed=0
-failed=0
-skipped=0
-
-cleanup() {
- chmod -R u+rwX "$TMP" 2>/dev/null
- rm -rf "$TMP"
-}
-trap 'cleanup; exit 130' INT TERM
-trap cleanup EXIT
-
-mkdir -p "$TMP" || exit 1
-
-ZERO=0000000000000000000000000000000000000000
-A=1111111111111111111111111111111111111111
-B=2222222222222222222222222222222222222222
-C=3333333333333333333333333333333333333333
-
-# ------------------------------------------------------------------ harness
-
-section() { printf '\n# %s\n' "$1"; }
-
-ok() { passed=$((passed + 1)); }
-bad() { failed=$((failed + 1)); printf 'not ok: %s\n' "$1"; }
-skip_it() {
- skipped=$((skipped + 1))
- printf 'ok (skipped): %s - %s\n' "$1" "$2"
-}
-
-# expect
-expect() {
- if [ "$4" != "$2" ]; then
- bad "$1: exit $4, wanted $2 (said: $5)"
- elif [ "$3" != "-" ] && ! contains "$5" "$3"; then
- bad "$1: said \"$5\", wanted it to contain \"$3\""
- else
- ok
- fi
-}
-
-# expect_not
-expect_not() {
- if [ "$4" != "$2" ]; then
- bad "$1: exit $4, wanted $2 (said: $5)"
- elif [ "$3" != "-" ] && contains "$5" "$3"; then
- bad "$1: said \"$5\", which contains the forbidden \"$3\""
- else
- ok
- fi
-}
-
-contains() {
- case $1 in
- *"$2"*) return 0 ;;
- *) return 1 ;;
- esac
-}
-
-# The policy from a here-document.
-policy() {
- cat >"$P"
-}
-
-# check [ref]
-do_check() {
- "$AUTHZ" --policy "$POLICY" check "$@" 2>&1
-}
-
-# Which policy the two drivers below read. Overridden per test with
-# "out=$(POLICY=something push ...)", which is a subshell and therefore does not
-# leak into the next test.
-POLICY=$P
-
-# push [extra authorize args] (ref lines on stdin)
-push() {
- user=$1
- repo=$2
- shift 2
- if [ "$user" = none ]; then
- env -u GIT_HOST_USER -u REMOTE_USER GIT_HOST_REPO="$repo" \
- "$AUTHZ" --policy "$POLICY" authorize --no-git "$@" 2>&1
- else
- GIT_HOST_USER="$user" GIT_HOST_REPO="$repo" \
- "$AUTHZ" --policy "$POLICY" authorize --no-git "$@" 2>&1
- fi
-}
-
-# hook (invoked through the hook name, as git does, with
-# the repository as the working directory and git's own argument in hand)
-# Reached the way core.hooksPath reaches it: by name, with the repository as the
-# working directory, git's own argument in hand (which is not ours, and must not
-# be read as one), and no --policy anywhere - the hook gets its policy from
-# GIT_HOST_AUTHZ or the default path, exactly as a deployed one does.
-hook() {
- user=$1
- dir=$2
- if [ "$user" = none ]; then
- ( cd "$dir" && env -u GIT_HOST_USER -u REMOTE_USER GIT_HOST_AUTHZ="$POLICY" \
- "$TMP/bin/pre-receive" "$dir" 2>&1 )
- else
- ( cd "$dir" && env GIT_HOST_USER="$user" GIT_HOST_AUTHZ="$POLICY" \
- "$TMP/bin/pre-receive" "$dir" 2>&1 )
- fi
-}
-
-# ------------------------------------------------------------------ fixture
-
-section "fixture"
-
-cat >"$TMP/authz.conf" <<'EOF'
-# who may move which refs
-alice * admin # the one account that can do anything
-@all * write refs/heads/feature/* # anyone the policy names: working branches
-frank team write refs/heads/main refs/tags/* # releases, and nothing else
-bob team write refs/heads/feature/*
-carol team force refs/heads/feature/*
-dave team admin
-erin secrets admin
-EOF
-
-# A repository directory, and the hook name this binary is reached under.
-mkdir -p "$TMP/root/team.git" "$TMP/bin"
-ln -sf "$AUTHZ_ABS" "$TMP/bin/pre-receive"
-if [ ! -x "$TMP/bin/pre-receive" ]; then
- cp "$AUTHZ" "$TMP/bin/pre-receive"
-fi
-chmod 755 "$TMP/bin/pre-receive"
-if [ ! -L "$TMP/bin/pre-receive" ]; then
- skip_it "hook reached by its own name" "cannot make a symlink here"
-fi
-
-# A policy with no '@all' line, for the tests that need a repository and a
-# stranger rather than a policy that reaches everywhere.
-cat >"$TMP/plain.conf" <<'EOF'
-alice * admin
-bob team write refs/heads/feature/*
-EOF
-
-out=$("$AUTHZ" --policy "$P" validate --no-host 2>&1)
-rc=$?
-expect "the fixture policy is sound" 0 "is in force" "$rc" "$out"
-
-# ---------------------------------------------------------------- usage
-
-section "usage and exit codes"
-
-out=$("$AUTHZ" --policy "$P" 2>&1); rc=$?
-expect "no command is a usage error" 64 "usage:" "$rc" "$out"
-out=$("$AUTHZ" --policy "$P" frobnicate 2>&1); rc=$?
-expect "unknown command" 64 "unknown command" "$rc" "$out"
-out=$("$AUTHZ" --policy "$P" check alice web 2>&1); rc=$?
-expect "check wants three arguments" 64 "wants " "$rc" "$out"
-out=$("$AUTHZ" --policy "$P" check alice web read 2>&1); rc=$?
-expect "'read' is not a level" 64 "unknown access level" "$rc" "$out"
-out=$("$AUTHZ" --help 2>&1); rc=$?
-expect "--help" 0 "Exit status" "$rc" "$out"
-out=$("$AUTHZ" --policy "$P" authorize --wat 2>&1 &1); rc=$?
-expect "a granted admin can write anything" 0 "allow" "$rc" "$out"
-
-# ------------------------------------------------------------------ check
-
-section "check: the ladder"
-
-out=$(do_check bob team write refs/heads/feature/login); rc=$?
-expect "write on a listed pattern" 0 "allow" "$rc" "$out"
-out=$(do_check bob team force refs/heads/feature/login); rc=$?
-expect "write is not force" 1 "needs force" "$rc" "$out"
-out=$(do_check carol team force refs/heads/feature/login); rc=$?
-expect "force on a listed pattern" 0 "allow" "$rc" "$out"
-out=$(do_check carol team admin refs/heads/feature/login); rc=$?
-expect "force is not admin" 1 "needs admin" "$rc" "$out"
-out=$(do_check dave team admin refs/heads/anything); rc=$?
-expect "admin on the whole repository" 0 "allow" "$rc" "$out"
-out=$(do_check dave team write refs/heads/anything); rc=$?
-expect "admin answers a write too" 0 "allow" "$rc" "$out"
-out=$(POLICY="$TMP/plain.conf" do_check erin other write refs/heads/dev); rc=$?
-expect "a stranger to the repository" 1 "no rule for other" "$rc" "$out"
-out=$(do_check erin other write refs/heads/feature/x); rc=$?
-expect "unless '@all' reaches that repository too" 0 "allow" "$rc" "$out"
-
-section "check: names mean what they say"
-
-out=$(do_check bob team.git write refs/heads/feature/x); rc=$?
-expect "team.git is team" 0 "allow" "$rc" "$out"
-out=$(do_check bob team write main); rc=$?
-expect "a bare name is a branch" 1 "refs/heads/main" "$rc" "$out"
-out=$(do_check bob team write heads/main); rc=$?
-expect "heads/main is the same branch" 1 "refs/heads/main" "$rc" "$out"
-out=$(do_check bob team write refs/heads/main); rc=$?
-expect "and so is refs/heads/main" 1 "refs/heads/main" "$rc" "$out"
-out=$(do_check bob team write v1); rc=$?
-expect "a bare v1 is a branch, never a tag" 1 "refs/heads/v1" "$rc" "$out"
-out=$(do_check bob team write tags/v1); rc=$?
-expect "and it takes tags/v1 to mean a tag" 1 "refs/tags/v1" "$rc" "$out"
-out=$(do_check dave team write refs/tags/v1); rc=$?
-expect "an admin can tag" 0 "allow" "$rc" "$out"
-out=$(do_check carol team write refs/heads/feature/deep/nested); rc=$?
-expect "* crosses a slash" 0 "allow" "$rc" "$out"
-
-section "check: a rule covers what it names, and nothing else"
-
-out=$(do_check bob team write refs/heads/main); rc=$?
-expect "bob's rules never name main" 1 "but not on refs/heads/main" "$rc" "$out"
-out=$(do_check carol team force refs/heads/main); rc=$?
-expect "and neither do carol's, at any level" 1 "but not on refs/heads/main" "$rc" "$out"
-out=$(do_check frank team write refs/heads/main); rc=$?
-expect "frank's rule does name it, so he may move it" 0 "allow" "$rc" "$out"
-out=$(do_check frank team write refs/heads/feature/x); rc=$?
-expect "and he reaches the '@all' rule like anyone else named" 0 "allow" "$rc" "$out"
-out=$(do_check frank team force refs/heads/main); rc=$?
-expect "naming a ref is not the same as holding every level on it" 1 "needs force" "$rc" "$out"
-out=$(do_check dave team force refs/heads/main); rc=$?
-expect "an unscoped admin covers main too" 0 "allow" "$rc" "$out"
-out=$(do_check bob team write refs/tags/v1); rc=$?
-expect "tags have to be named as well" 1 "refs/tags/v1" "$rc" "$out"
-out=$(do_check frank team write refs/tags/v1); rc=$?
-expect "and frank named his" 0 "allow" "$rc" "$out"
-
-section "check: refs that are neither branch nor tag"
-
-out=$(do_check bob team write refs/pull/1/head); rc=$?
-expect "a fetch ref needs admin" 1 "needs admin" "$rc" "$out"
-out=$(do_check dave team write refs/pull/1/head); rc=$?
-expect "an admin moves anything" 0 "allow" "$rc" "$out"
-out=$(do_check alice secrets write refs/notes/x); rc=$?
-expect "notes are for admins" 0 "allow" "$rc" "$out"
-
-section "check: '@all' is everyone the policy names"
-
-out=$(do_check carol web write refs/heads/feature/any); rc=$?
-expect "a named user reaches the @all rule" 0 "allow" "$rc" "$out"
-out=$(do_check nobody team write refs/heads/dev); rc=$?
-expect "a name the policy does not have gets nothing" 1 "no rule" "$rc" "$out"
-out=$(do_check '' team write refs/heads/dev); rc=$?
-expect "an empty identity is no identity" 1 "no identity" "$rc" "$out"
-
-section "check: the question with no ref"
-
-out=$(do_check dave team force); rc=$?
-expect "an unscoped admin has force somewhere" 0 "allow" "$rc" "$out"
-out=$(do_check bob team write); rc=$?
-expect "a scoped rule is 'somewhere'" 0 "some refs" "$rc" "$out"
-out=$(do_check bob team force); rc=$?
-expect "but not at a level he lacks" 1 "no force" "$rc" "$out"
-out=$(do_check alice web admin); rc=$?
-expect "an unscoped rule is 'all of'" 0 "all of web" "$rc" "$out"
-
-# -------------------------------------------------------- authorize (hook)
-
-section "authorize: what git hands the hook"
-
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
- push bob team --explain); rc=$?
-expect "creating a branch is write" 0 "allow" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/notes/x |
- push bob team); rc=$?
-expect "a ref that is neither a branch nor a tag is for admins" 1 "needs admin" "$rc" "$out"
-out=$(POLICY="$TMP/plain.conf" printf '%s %s %s\n' "$ZERO" "$A" refs/heads/release |
- POLICY="$TMP/plain.conf" push bob team); rc=$?
-expect "a branch outside the pattern is denied" 1 "deny" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
- push bob team --ff=yes --explain); rc=$?
-expect "a fast-forward is write" 0 "allow" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
- push bob team --ff=no); rc=$?
-expect "a rewrite is force" 1 "needs force" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
- push carol other --ff=no); rc=$?
-expect "a rewrite in a repository she only reaches through @all" 1 "needs force" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
- push bob team); rc=$?
-expect "deleting is force" 1 "needs force" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
- push carol team); rc=$?
-expect "carol may delete her own kind of branch" 0 "-" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
- push carol team --head=refs/heads/dev); rc=$?
-expect "deleting the default branch is admin" 1 "needs admin" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
- push carol team --head=refs/heads/other); rc=$?
-expect "deleting a branch she holds force on is allowed" 0 "-" "$rc" "$out"
-out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
- push carol team --head=refs/heads/other); rc=$?
-expect "and only force on that ref will do, not force somewhere else" 1 \
- "carol holds force on team, but not on refs/heads/dev" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
- push dave team --head=refs/heads/dev); rc=$?
-expect "an admin deletes the default branch" 0 "-" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
- env PATH=/nonexistent GIT_HOST_USER=carol GIT_HOST_REPO=team \
- "$AUTHZ" --policy "$P" authorize 2>&1); rc=$?
-expect "a HEAD that cannot be read is charged as the default branch" 1 "needs admin" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
- push bob team --ff=auto); rc=$?
-expect "with no git to ask, an update is a rewrite" 1 "needs force" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$A" "$B" refs/pull/7/head |
- push dave team); rc=$?
-expect "an admin writes a fetch ref" 0 "-" "$rc" "$out"
-
-section "authorize: identity"
-
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | push none team); rc=$?
-expect "anonymous is denied" 1 "no identity" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
- env -u GIT_HOST_USER REMOTE_USER=bob GIT_HOST_REPO=team \
- "$AUTHZ" --policy "$P" authorize --no-git 2>&1); rc=$?
-expect "REMOTE_USER is an identity too (http)" 0 "-" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
- env GIT_HOST_USER=carol REMOTE_USER=bob GIT_HOST_REPO=team \
- "$AUTHZ" --policy "$P" authorize --no-git --explain 2>&1); rc=$?
-expect "GIT_HOST_USER wins over REMOTE_USER" 0 "carol has write" "$rc" "$out"
-
-out=$(POLICY="$TMP/plain.conf" printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
- POLICY="$TMP/plain.conf" push bob team --explain); rc=$?
-expect "the named repository is allowed" 0 "on team" "$rc" "$out"
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
- POLICY="$TMP/plain.conf" push bob other --explain); rc=$?
-expect "another repository is not" 1 "no rule for other" "$rc" "$out"
-
-section "authorize: several refs, and the first denial"
-
-out=$(printf '%s %s %s\n%s %s %s\n%s %s %s\n' \
- "$ZERO" "$A" refs/heads/feature/one \
- "$ZERO" "$B" refs/heads/feature/two \
- "$ZERO" "$C" refs/heads/feature/three |
- push bob team --explain); rc=$?
-expect "a whole push of allowed refs" 0 "feature/three" "$rc" "$out"
-
-out=$(printf '%s %s %s\n%s %s %s\n' \
- "$ZERO" "$A" refs/heads/feature/one \
- "$ZERO" "$B" refs/heads/main |
- push bob team); rc=$?
-expect "one ref nobody may stop the whole push" 1 "refs/heads/main" "$rc" "$out"
-expect_not "and the refs before it are not reported either" 1 "feature/one" "$rc" "$out"
-
-out=$(printf '%s %s %s\n%s %s %s\n' \
- "$ZERO" "$A" refs/heads/main \
- "$ZERO" "$B" refs/heads/feature/one |
- push bob team); rc=$?
-expect "the refusal says who, what and what it would take" 1 \
- "bob holds write on team, but not on refs/heads/main" "$rc" "$out"
-expect_not "and stops at the first refusal" 1 "feature/one" "$rc" "$out"
-
-section "authorize: input that is not a push"
-
-out=$(printf 'nonsense\n' | push bob team); rc=$?
-expect "a line with too few fields" 1 "expected" "$rc" "$out"
-out=$(printf '%s %s %s\n' "$A" "$B" heads/feature/x | push bob team); rc=$?
-expect "a refname that is not a refname" 1 "not an update git would send" "$rc" "$out"
-out=$(printf '%s %s %s\n' notanoid "$B" refs/heads/x | push bob team); rc=$?
-expect "an object id that is not one" 1 "not an update git would send" "$rc" "$out"
-out=$(printf '' | push bob team); rc=$?
-expect "no refs at all is nothing to deny" 0 "-" "$rc" "$out"
-out=$(printf '\n\n' | push bob team); rc=$?
-expect "blank lines are skipped" 0 "-" "$rc" "$out"
-
-# --------------------------------------------------------- the hook by name
-
-section "authorize: reached as pre-receive"
-
-out=$(POLICY="$TMP/plain.conf" printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
- POLICY="$TMP/plain.conf" hook bob "$TMP/root/team.git"); rc=$?
-expect "the hook name selects authorize, and git's argument is ignored" 0 "-" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/release |
- POLICY="$TMP/plain.conf" hook bob "$TMP/root/team.git"); rc=$?
-expect "and denies what authorize denies, on the repository it found" 1 \
- "bob holds write on team, but not on refs/heads/release" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
- POLICY="$TMP/plain.conf" hook none "$TMP/root/team.git"); rc=$?
-expect "an anonymous key is denied at the hook" 1 "no identity" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
- POLICY="$TMP/broken.conf" hook bob "$TMP/root/team.git"); rc=$?
-expect "a hook on a policy that does not parse stops the push" 3 "deny" "$rc" "$out"
-
-# the repository, taken from the working directory under GIT_REPO_ROOT
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
- ( cd "$TMP/root/team.git" && env -u GIT_HOST_REPO GIT_HOST_USER=bob \
- GIT_REPO_ROOT="$TMP/root" "$AUTHZ_ABS" --policy "$P" authorize --no-git --explain 2>&1 )); rc=$?
-expect "GIT_REPO_ROOT plus the cwd names the repository" 0 "on team" "$rc" "$out"
-
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
- ( cd "$TMP/root/team.git" && env -u GIT_HOST_REPO GIT_HOST_USER=bob \
- GIT_REPO_ROOT=/var/lib/git "$AUTHZ_ABS" --policy "$P" authorize --no-git --explain 2>&1 )); rc=$?
-expect "and the last path component works when it is not the root" 0 "team" "$rc" "$out"
-
-# -------------------------------------------------------------- fail closed
-
-section "fail closed"
-
-printf 'alice web admin\nbroken line\n' >"$TMP/broken.conf"
-out=$("$AUTHZ" --policy "$TMP/broken.conf" check alice web admin refs/heads/x 2>&1); rc=$?
-expect "a policy that does not parse denies" 3 "does not parse" "$rc" "$out"
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/x |
- env GIT_HOST_USER=alice GIT_HOST_REPO=web "$AUTHZ" --policy "$TMP/broken.conf" \
- authorize --no-git 2>&1); rc=$?
-expect "and the hook denies" 3 "deny" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/no-such.conf" check alice web admin 2>&1); rc=$?
-expect "a policy that is not there denies" 3 "cannot be read" "$rc" "$out"
-out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/x |
- env GIT_HOST_USER=alice GIT_HOST_REPO=web "$AUTHZ" --policy "$TMP/no-such.conf" \
- authorize 2>&1); rc=$?
-expect "and the hook denies" 3 "deny" "$rc" "$out"
-
-printf 'alice web admin\n' >"$TMP/secret.conf"
-chmod 000 "$TMP/secret.conf"
-if [ "$(id -u)" = 0 ]; then
- skip_it "an unreadable policy denies" "running as root, who can read anything"
-else
- out=$("$AUTHZ" --policy "$TMP/secret.conf" check alice web admin 2>&1); rc=$?
- expect "an unreadable policy denies" 3 "cannot be read" "$rc" "$out"
-fi
-chmod 644 "$TMP/secret.conf"
-
-# ---------------------------------------------------------------- validate
-
-section "validate: the policy alone"
-
-POLICY=$P
-out=$("$AUTHZ" --policy "$P" validate --no-host 2>&1); rc=$?
-expect "the fixture passes" 0 "is in force" "$rc" "$out"
-expect_not "with nothing to complain about" 0 "warn " "$rc" "$out"
-
-out=$("$AUTHZ" --policy "$TMP/broken.conf" validate --no-host 2>&1); rc=$?
-expect "a broken policy is reported with its line number" 3 ":2: error" "$rc" "$out"
-
-cat >"$TMP/noise.conf" <<'EOF'
-@all web write
-bob missing write refs/heads/*
-EOF
-out=$("$AUTHZ" --policy "$TMP/noise.conf" validate --no-host 2>&1); rc=$?
-expect "warnings do not fail a validate" 0 "warning(s)" "$rc" "$out"
-contains "$out" "@all the whole of web" || bad "an unscoped @all should be warned about"
-out=$("$AUTHZ" --policy "$TMP/noise.conf" validate --no-host --strict 2>&1); rc=$?
-expect "--strict makes warnings fatal" 1 "warning" "$rc" "$out"
-
-out=$("$AUTHZ" --policy "$TMP/nope.conf" validate --no-host 2>&1); rc=$?
-expect "a missing policy is not a valid policy" 3 "cannot be read" "$rc" "$out"
-
-section "validate: the host around it"
-
-mkhost() {
- H=$TMP/host
- rm -rf "$H"
- mkdir -p "$H/hooks" "$H/repos/team.git" "$H/repos/secrets.git" "$H/secrets"
- ln -sf "$AUTHZ_ABS" "$H/hooks/pre-receive" 2>/dev/null || cp "$AUTHZ" "$H/hooks/pre-receive"
- printf '[core]\n\thooksPath = %s/hooks\n' "$H" >"$H/gitconfig"
- printf 'PermitUserEnvironment yes\nForceCommand /usr/bin/git-shell\nAuthorizedKeysFile %s/secrets/authorized_keys\n' "$H" >"$H/sshd_config"
- printf 'git:x:1000:1000::/var/lib/git:/usr/bin/git-shell\n' >"$H/passwd"
- printf 'ssh-ed25519 AAAA bob\n' >"$H/secrets/authorized_keys"
- printf 'environment="GIT_HOST_USER=bob" ssh-ed25519 BBBB\n' >>"$H/secrets/authorized_keys"
- printf 'bob:$2y$05$hash\n' >"$H/secrets/htpasswd"
- cat >"$H/authz.conf" <<'POL'
-alice * admin
-bob team write refs/heads/feature/*
-POL
-}
-
-host_validate() {
- "$AUTHZ" --policy "$H/authz.conf" validate \
- --root "$H/repos" --hook-dir "$H/hooks" --gitconfig "$H/gitconfig" \
- --sshd-config "$H/sshd_config" --passwd "$H/passwd" \
- --keys "$H/secrets/authorized_keys" --htpasswd "$H/secrets/htpasswd" "$@" 2>&1
-}
-
-mkhost
-out=$(host_validate); rc=$?
-expect "a wired-up host passes" 0 "is in force" "$rc" "$out"
-expect "and says what it checked" 0 "ForceCommand" "$rc" "$out"
-
-sed "s|hooksPath = .*|hooksPath = /somewhere/else|" "$H/gitconfig" >"$H/gitconfig2"
-mv "$H/gitconfig2" "$H/gitconfig"
-out=$(host_validate); rc=$?
-expect "the wrong core.hooksPath is an error" 1 "checked by something else" "$rc" "$out"
-
-sed "s|hooksPath|hooks-path|" "$H/gitconfig" >"$H/gitconfig2"
-mv "$H/gitconfig2" "$H/gitconfig"
-out=$(host_validate); rc=$?
-expect "no core.hooksPath at all is an error" 1 "does not set core.hooksPath" "$rc" "$out"
-
-mkhost
-mv "$H/hooks/pre-receive" "$H/hooks/away"
-out=$(host_validate); rc=$?
-expect "a missing hook is an error" 1 "every push would be accepted unchecked" "$rc" "$out"
-mv "$H/hooks/away" "$H/hooks/pre-receive"
-
-mkhost
-sed "s/PermitUserEnvironment yes/PermitUserEnvironment no/" "$H/sshd_config" >"$H/s2"
-mv "$H/s2" "$H/sshd_config"
-out=$(host_validate); rc=$?
-expect "PermitUserEnvironment no means anonymous pushes" 1 "anonymous" "$rc" "$out"
-
-mkhost
-sed "s|^ForceCommand.*|# no force command|" "$H/sshd_config" >"$H/s2"
-mv "$H/s2" "$H/sshd_config"
-out=$(host_validate); rc=$?
-expect "no ForceCommand is an error" 1 "interactive shell" "$rc" "$out"
-
-mkhost
-sed "s|git-shell|/bin/sh|" "$H/passwd" >"$H/p2"
-mv "$H/p2" "$H/passwd"
-out=$(host_validate); rc=$?
-expect "a git account with a real shell is an error" 1 "not git-shell" "$rc" "$out"
-
-mkhost
-printf 'environment="GIT_HOST_USER=ghost" ssh-ed25519 CCCC\n' >"$H/secrets/authorized_keys"
-out=$(host_validate); rc=$?
-expect "a key identity with no rule is a warning" 0 "ghost" "$rc" "$out"
-out=$(host_validate --strict); rc=$?
-expect "and fatal under --strict" 1 "ghost" "$rc" "$out"
-
-mkhost
-printf 'ssh-ed25519 AAAAanonymouskey\n' >>"$H/secrets/authorized_keys"
-out=$(host_validate); rc=$?
-expect "an identity-less key is a warning" 0 "no GIT_HOST_USER" "$rc" "$out"
-
-mkhost
-printf 'mallory:$2y$05$hash\n' >>"$H/secrets/htpasswd"
-out=$(host_validate); rc=$?
-expect "an htpasswd user with no rule is a warning" 0 "change nothing" "$rc" "$out"
-
-mkhost
-printf 'bob team write\n' >"$H/authz.conf"
-mkdir -p "$H/repos/orphan.git"
-out=$(host_validate); rc=$?
-expect "a repository nobody can push to is a warning" 0 "orphan" "$rc" "$out"
-
-mkhost
-printf 'bob nowhere write\n' >>"$H/authz.conf"
-out=$(host_validate); rc=$?
-expect "a rule on a repository that is not there is a warning" 0 "nowhere" "$rc" "$out"
-
-mkhost
-printf 'alice web admin\nbob nowhere write refs/heads/*\n' >"$H/authz.conf"
-printf 'mallory:$2y$05$x\n' >"$H/secrets/htpasswd"
-mkdir -p "$H/repos/orphan.git"
-out=$(host_validate); rc=$?
-if contains "$out" "orphan" && contains "$out" "mallory" &&
- contains "$out" "nowhere" && contains "$out" "web"; then
- ok
-else
- bad "several warnings at once: said $out"
-fi
-expect "warnings alone still let the host serve" 0 "is in force" "$rc" "$out"
-
-# -------------------------------------------------------------- edit (grant)
-
-section "grant and revoke"
-
-cat >"$TMP/edit.conf" <<'EOF'
-# policy, kept in git and rolled out as a ConfigMap
-alice * admin # break glass
-bob team write refs/heads/feature/*
-EOF
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant carol team force refs/heads/feature/* 2>&1); rc=$?
-expect "grant appends" 0 "appended" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check carol team force refs/heads/feature/x 2>&1); rc=$?
-expect "and the grant is in force immediately" 0 "allow" "$rc" "$out"
-if grep -q '^# policy, kept in git' "$TMP/edit.conf" &&
- grep -q 'break glass' "$TMP/edit.conf"; then
- ok
-else
- bad "the comments survived neither"
-fi
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team admin 2>&1); rc=$?
-expect "grant replaces the pair's rule" 0 "replaced line 3" "$rc" "$out"
-n=$(grep -c '^bob' "$TMP/edit.conf")
-[ "$n" = 1 ] && ok || bad "bob's rule is not collapsed into one line ($n lines)"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check bob team admin refs/heads/main 2>&1); rc=$?
-expect "and the new level applies" 0 "allow" "$rc" "$out"
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" revoke bob team 2>&1); rc=$?
-expect "revoke removes the rule" 0 "revoked bob" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check bob team admin refs/heads/dev 2>&1); rc=$?
-expect "and the access is gone" 1 "no rule" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" revoke bob team 2>&1); rc=$?
-expect "revoking nothing reports nothing" 1 "no rule for bob" "$rc" "$out"
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant frank team write refs/heads/main refs/tags/release/* 2>&1); rc=$?
-expect "grant can name several refs" 0 "granted frank" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check frank team write refs/heads/main 2>&1); rc=$?
-expect "and the list is in force" 0 "allow" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check frank team write refs/heads/dev 2>&1); rc=$?
-expect "and covers nothing else of his own" 1 "but not on refs/heads/dev" "$rc" "$out"
-if grep -q '^frank team write refs/heads/main refs/tags/release/\*$' "$TMP/edit.conf"; then
- ok
-else
- bad "the refs did not land on one line as written"
-fi
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team nope 2>&1); rc=$?
-expect "grant rejects a level that is not one" 64 "unknown access level" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob group/team write 2>&1); rc=$?
-expect "grant rejects a repository name with a slash in it" 64 "invalid repo pattern" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write '!refs/heads/main' 2>&1); rc=$?
-expect "grant has no deny" 64 "no deny" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" protect refs/heads/main 2>&1); rc=$?
-expect "protect is gone, and says it is unknown" 64 "unknown command" "$rc" "$out"
-printf 'set protected-refs refs/heads/main\n' >>"$TMP/edit.conf"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check alice team admin refs/heads/main 2>&1); rc=$?
-expect "a policy that still carries protected-refs is refused, not ignored" 3 "no longer exists" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" validate --no-host 2>&1); rc=$?
-expect "and validate says every push is denied" 3 "not used" "$rc" "$out"
-grep -v 'protected-refs' "$TMP/edit.conf" > "$TMP/edit.conf.clean" &&
- cp "$TMP/edit.conf.clean" "$TMP/edit.conf"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" validate --no-host 2>&1); rc=$?
-expect "cleaned, it validates again" 0 "is in force" "$rc" "$out"
-
-cp "$TMP/edit.conf" "$TMP/edit.conf.orig"
-out=$("$AUTHZ" --policy "$TMP/broken.conf" grant bob team write 2>&1); rc=$?
-expect "editing a policy that does not parse is refused" 3 "does not parse" "$rc" "$out"
-cmp -s "$TMP/edit.conf" "$TMP/edit.conf.orig" || bad "a refused edit still changed the file"
-
-section "edit: where the result goes"
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant erin secrets admin --out "$TMP/other.conf" 2>&1); rc=$?
-expect "--out writes elsewhere" 0 "$TMP/other.conf" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/other.conf" check erin secrets admin refs/heads/main 2>&1); rc=$?
-expect "and the new file works" 0 "allow" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check erin secrets admin refs/heads/main 2>&1); rc=$?
-expect "and the original does not have it" 1 "no rule" "$rc" "$out"
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant erin secrets admin --stdout 2>&1 >/dev/null); rc=$?
-expect "--stdout reports on stderr" 0 "untouched" "$rc" "$out"
-cp "$TMP/edit.conf" "$TMP/edit.conf.bak"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" revoke carol team --stdout 2>/dev/null); rc=$?
-expect "--stdout prints the new file" 0 "alice * admin" "$rc" "$out"
-cmp -s "$TMP/edit.conf" "$TMP/edit.conf.bak" && ok || bad "--stdout modified the policy"
-if contains "$out" "alice * admin" && contains "$out" "break glass"; then
- ok
-else
- bad "--stdout lost the comments or the settings"
-fi
-
-# A file whose *directory* is read-only cannot be replaced, which is what a
-# read-only ConfigMap mount means in practice. A 0444 file in a writable
-# directory is a different thing and worth pinning too: the grant succeeds and
-# the mode survives, because it is the directory that decides.
-cp "$TMP/edit.conf" "$TMP/ro.conf"
-chmod 444 "$TMP/ro.conf"
-out=$("$AUTHZ" --policy "$TMP/ro.conf" grant bob team admin 2>&1); rc=$?
-if [ "$(id -u)" = 0 ]; then
- skip_it "an unwritable directory is refused" "running as root, who defeats chmod"
- expect "the mode of a 0444 policy survives a write" 0 "granted" "$rc" "$out"
- mode=$(ls -l "$TMP/ro.conf" | cut -c2-10)
- [ "$mode" = "r--r--r--" ] && ok || bad "a 0444 policy came back as $mode"
-else
- mkdir -p "$TMP/rodir"
- cp "$TMP/edit.conf" "$TMP/rodir/policy.conf"
- chmod 555 "$TMP/rodir"
- out=$("$AUTHZ" --policy "$TMP/rodir/policy.conf" grant bob team admin 2>&1); rc=$?
- expect "a policy in a read-only directory cannot be replaced" 1 "cannot write beside" "$rc" "$out"
- chmod 755 "$TMP/rodir"
-fi
-out=$("$AUTHZ" --policy "$TMP/ro.conf" grant bob team admin --out - 2>/dev/null); rc=$?
-expect "--out works whatever the input's mode is" 0 "bob team admin" "$rc" "$out"
-
-section "edit: leaving the file alone"
-
-cat >"$TMP/mode.conf" <<'EOF'
-alice * admin
-EOF
-chmod 640 "$TMP/mode.conf"
-out=$("$AUTHZ" --policy "$TMP/mode.conf" grant bob team write 2>&1); rc=$?
-expect "a grant succeeds" 0 "granted" "$rc" "$out"
-mode=$(ls -l "$TMP/mode.conf" | cut -c2-10)
-[ "$mode" = "rw-r-----" ] && ok || bad "the mode changed to $mode"
-left=$(ls -a "$TMP" | grep -c '^\.authz')
-[ "$left" = 0 ] && ok || bad "$left temporary file(s) left behind"
-
-printf 'alice * admin' >"$TMP/nonl.conf" # no final newline
-out=$("$AUTHZ" --policy "$TMP/nonl.conf" grant bob team write 2>&1); rc=$?
-expect "a file without a trailing newline is editable" 0 "granted" "$rc" "$out"
-[ "$(tail -c 1 "$TMP/nonl.conf" | wc -l)" = 1 ] && ok || bad "the last line still has no newline"
-
-out=$("$AUTHZ" --policy "$TMP/fresh.conf" grant alice '*' admin 2>&1); rc=$?
-expect "grant can create the first rule of a new policy" 0 "granted" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/fresh.conf" check alice anything admin refs/heads/main 2>&1); rc=$?
-expect "and the new policy works" 0 "allow" "$rc" "$out"
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write --out "$TMP/nodir/x.conf" 2>&1); rc=$?
-expect "writing where the directory is not there fails" 1 "cannot write beside" "$rc" "$out"
-
-# ------------------------------------------------------------------ install
-
-section "install"
-
-if ! command -v install >/dev/null 2>&1 || ! command -v make >/dev/null 2>&1; then
- skip_it "make install" "no make or install(1) here"
- skip_it "the hook the installer leaves" "no make or install(1) here"
- skip_it "an existing policy is not overwritten" "no make or install(1) here"
-else
- S=$TMP/stage
- out=$(make install DESTDIR="$S" 2>&1); rc=$?
- expect "make install into a DESTDIR" 0 "pre-receive" "$rc" "$out"
-
- # The hook has to be a relative link: staged, unpacked, or shipped in an
- # image, it must point at the binary that came with it rather than at
- # whatever occupies that absolute path on the machine reading it.
- link=$(ls -l "$S/usr/local/share/git-host/hooks/pre-receive")
- case $link in
- *'= /'*) bad "the installed hook is an absolute symlink: $link" ;;
- *'pre-receive -> ../../'*) ok ;;
- *) bad "the installed hook is not a relative symlink: $link" ;;
- esac
-
- # End to end through the installed name, with the installed policy, in a
- # repository under an installed root: nothing but the paths from the
- # installer here.
- mkdir -p "$S/var/lib/git/team.git"
- out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
- ( cd "$S/var/lib/git/team.git" && env GIT_HOST_USER=bob \
- GIT_REPO_ROOT="$S/var/lib/git" GIT_HOST_AUTHZ="$S/etc/git-host/authz.conf" \
- "$S/usr/local/share/git-host/hooks/pre-receive" "." 2>&1 )); rc=$?
- expect "the installed hook allows what the policy allows" 0 "-" "$rc" "$out"
-
- out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/main |
- ( cd "$S/var/lib/git/team.git" && env GIT_HOST_USER=bob \
- GIT_REPO_ROOT="$S/var/lib/git" GIT_HOST_AUTHZ="$S/etc/git-host/authz.conf" \
- "$S/usr/local/share/git-host/hooks/pre-receive" "." 2>&1 )); rc=$?
- expect "and denies what it denies" 1 "deny" "$rc" "$out"
- contains "$out" "pre-receive:" || bad "the hook should say so in its own name: $out"
-
- printf 'someone * admin\n' >"$S/etc/git-host/authz.conf"
- out=$(make install DESTDIR="$S" 2>&1); rc=$?
- expect "installing again does not fail" 0 "" "$rc" "$out"
- if grep -q 'someone' "$S/etc/git-host/authz.conf" &&
- [ -f "$S/etc/git-host/authz.conf.new" ]; then
- ok
- else
- bad "an existing policy was overwritten, or the new example is nowhere"
- fi
-fi
-
-# ------------------------------------------------------------ the shipped policy
-
-if [ -f authz.conf ]; then
- section "the example policy that ships with it"
-
- out=$("$AUTHZ" --policy authz.conf validate --no-host 2>&1); rc=$?
- expect "it validates" 0 "is in force" "$rc" "$out"
- out=$("$AUTHZ" --policy authz.conf check alice anything admin refs/heads/main 2>&1); rc=$?
- expect "alice can do anything" 0 "allow" "$rc" "$out"
- out=$("$AUTHZ" --policy authz.conf check bob team write refs/heads/feature/x 2>&1); rc=$?
- expect "bob works on features" 0 "allow" "$rc" "$out"
- out=$("$AUTHZ" --policy authz.conf check bob team write refs/heads/main 2>&1); rc=$?
- expect "bob cannot touch main" 1 "but not on refs/heads/main" "$rc" "$out"
- out=$("$AUTHZ" --policy authz.conf check nobody team write refs/heads/x 2>&1); rc=$?
- expect "a stranger cannot" 1 "no rule" "$rc" "$out"
-fi
-
-# ------------------------------------------------------------------ summary
-
-printf '\n%s\n' "passed $passed, failed $failed, skipped $skipped"
-[ "$failed" = 0 ] || exit 1
-exit 0
diff --git a/tests/unit.c b/tests/unit.c
deleted file mode 100644
index cf55282..0000000
--- a/tests/unit.c
+++ /dev/null
@@ -1,545 +0,0 @@
-/* unit.c - the pieces, tested directly.
- *
- * This links the matcher, not the command line: what a glob does, what a ref
- * normalises to, what a policy parses into and what it therefore decides. The
- * behavioural suite (tests/run.sh) drives the program the way git does; this
- * file is for the cases that would need a whole push to reach otherwise.
- *
- * No test framework, because a test suite with a dependency is a test suite
- * that does not run at 3am. Failures print "not ok" with the expression.
- */
-#include "../src/authz.h"
-
-#include
-#include
-#include
-#include
-#include
-#include
-
-static int checks;
-static int failures;
-static char tmproot[128];
-
-#define CHECK(cond) \
- do { \
- checks++; \
- if (!(cond)) { \
- failures++; \
- printf("not ok %d: %s [%s:%d]\n", failures, #cond, __FILE__, __LINE__);\
- } \
- } while (0)
-
-#define CHECK_STR(got, want) \
- do { \
- checks++; \
- if (strcmp((got), (want)) != 0) { \
- failures++; \
- printf("not ok %d: got \"%s\", want \"%s\" [%s:%d]\n", failures, \
- (got), (want), __FILE__, __LINE__); \
- } \
- } while (0)
-
-#define CHECK_INT(got, want) \
- do { \
- checks++; \
- if ((got) != (want)) { \
- failures++; \
- printf("not ok %d: got %ld, want %ld [%s:%d]\n", failures, \
- (long)(got), (long)(want), __FILE__, __LINE__); \
- } \
- } while (0)
-
-static void note(const char *fmt, ...)
-{
- va_list ap;
-
- fputs(" # ", stdout);
- va_start(ap, fmt);
- vprintf(fmt, ap);
- va_end(ap);
- fputc('\n', stdout);
-}
-
-/* ------------------------------------------------------------- fixtures */
-
-/* A policy from a string. The file is unlinked straight after loading: the
- * decisions are made in memory, and a test that left policies behind would
- * leave them to be trusted. */
-static int load_text(policy_t *p, const char *text)
-{
- static int seq;
- char path[512];
- FILE *f;
- int rc;
-
- snprintf(path, sizeof(path), "%s/unit-%d", tmproot, ++seq);
- f = fopen(path, "w");
- if (f == NULL) {
- printf("not ok %d: cannot write %s\n", ++failures, path);
- return -1;
- }
- fputs(text, f);
- fclose(f);
-
- policy_init(p, path);
- rc = policy_load(p);
- unlink(path);
- return rc;
-}
-
-static void expect_allow(const policy_t *p, const char *user, const char *repo,
- level_t need, const char *ref)
-{
- decision_t d;
-
- authz_check(p, user, repo, need, ref, &d);
- checks++;
- if (!d.allowed) {
- failures++;
- printf("not ok %d: expected %s %s %s %s to be allowed, got: %s\n", failures,
- user, repo, authz_level_name(need), ref != NULL ? ref : "(no ref)", d.why);
- }
-}
-
-static void expect_deny(const policy_t *p, const char *user, const char *repo,
- level_t need, const char *ref, const char *needle)
-{
- decision_t d;
-
- authz_check(p, user, repo, need, ref, &d);
- checks++;
- if (d.allowed) {
- failures++;
- printf("not ok %d: expected %s %s %s %s to be denied\n", failures, user, repo,
- authz_level_name(need), ref != NULL ? ref : "(no ref)");
- } else if (needle != NULL && strstr(d.why, needle) == NULL) {
- failures++;
- printf("not ok %d: denial said \"%s\", expected it to mention \"%s\"\n",
- failures, d.why, needle);
- }
-}
-
-/* ------------------------------------------------------------------ tests */
-
-static void test_glob(void)
-{
- CHECK(glob_match("*", ""));
- CHECK(glob_match("*", "anything/at/all"));
- CHECK(!glob_match("", "x"));
- CHECK(glob_match("", ""));
- CHECK(glob_match("refs/heads/*", "refs/heads/main"));
- CHECK(glob_match("refs/heads/*", "refs/heads/a/b/c")); /* '*' crosses '/' */
- CHECK(!glob_match("refs/heads/*", "refs/tags/v1"));
- CHECK(glob_match("refs/heads/feature/*", "refs/heads/feature/login"));
- CHECK(!glob_match("refs/heads/feature/*", "refs/heads/featurey"));
- CHECK(glob_match("refs/tags/v?", "refs/tags/v1"));
- CHECK(!glob_match("refs/tags/v?", "refs/tags/v12"));
- CHECK(glob_match("refs/heads/[ab]*", "refs/heads/beta"));
- CHECK(glob_match("refs/heads/[ab]*", "refs/heads/alpha"));
- CHECK(!glob_match("refs/heads/[ab]*", "refs/heads/delta"));
- CHECK(glob_match("refs/heads/[!ab]*", "refs/heads/delta"));
- CHECK(!glob_match("refs/heads/[!ab]*", "refs/heads/alpha"));
- CHECK(glob_match("refs/heads/[a-c]1", "refs/heads/b1"));
- CHECK(!glob_match("refs/heads/[a-c]1", "refs/heads/d1"));
- CHECK(glob_match("refs/heads/]", "refs/heads/]")); /* ']' first is a member */
- CHECK(glob_match("refs/heads/a[-]z", "refs/heads/a-z")); /* trailing '-' literal */
- CHECK(glob_match("refs/heads/[", "refs/heads/[")); /* unclosed is literal */
- CHECK(glob_match("a*b*c", "aXXbYYc"));
- CHECK(!glob_match("a*b*c", "aXXbYY"));
- CHECK(glob_match("*.git", "team.git"));
- CHECK(glob_match("team*", "team"));
- CHECK(glob_match("refs/heads/ma?n", "refs/heads/main"));
- CHECK(!glob_match("refs/heads/main", "refs/heads/MAIN")); /* case matters */
- CHECK(glob_match("refs/*/x", "refs/a/b/x"));
- CHECK(glob_match("**", "x")); /* redundant stars */
- CHECK(!glob_match("refs/heads/*", ""));
-}
-
-static void test_normalisation(void)
-{
- char *s;
-
- s = authz_norm_ref("main");
- CHECK_STR(s, "refs/heads/main");
- free(s);
- s = authz_norm_ref("heads/main");
- CHECK_STR(s, "refs/heads/main");
- free(s);
- s = authz_norm_ref("refs/heads/main");
- CHECK_STR(s, "refs/heads/main");
- free(s);
- s = authz_norm_ref("v1.0");
- CHECK_STR(s, "refs/heads/v1.0"); /* a bare name is never a tag */
- free(s);
- s = authz_norm_ref("tags/v1.0");
- CHECK_STR(s, "refs/tags/v1.0");
- free(s);
- s = authz_norm_ref("notes/commits");
- CHECK_STR(s, "refs/notes/commits");
- free(s);
- s = authz_norm_ref("*");
- CHECK_STR(s, "refs/*");
- free(s);
- s = authz_norm_ref("all");
- CHECK_STR(s, "refs/*");
- free(s);
- s = authz_norm_ref("refs");
- CHECK_STR(s, "refs/*");
- free(s);
-
- s = authz_norm_repo("web.git");
- CHECK_STR(s, "web");
- free(s);
- s = authz_norm_repo("web");
- CHECK_STR(s, "web");
- free(s);
- s = authz_norm_repo("*");
- CHECK_STR(s, "*");
- free(s);
- s = authz_norm_repo(".git");
- CHECK_STR(s, ".git"); /* a repository called ".git" is not a suffix */
- free(s);
-
- CHECK_INT(authz_level_of("write"), LV_WRITE);
- CHECK_INT(authz_level_of("force"), LV_FORCE);
- CHECK_INT(authz_level_of("admin"), LV_ADMIN);
- CHECK_INT(authz_level_of("deny"), -1);
- CHECK_INT(authz_level_of("read"), -1);
- CHECK_STR(authz_level_name(LV_NONE), "none");
- CHECK(authz_level_of("admin") > authz_level_of("force"));
- CHECK(authz_level_of("force") > authz_level_of("write"));
-
- CHECK(authz_user_pattern_ok("alice"));
- CHECK(authz_user_pattern_ok("a.b-c_1"));
- CHECK(authz_user_pattern_ok("@all"));
- CHECK(authz_user_pattern_ok("a[bc]d"));
- CHECK(!authz_user_pattern_ok(""));
- CHECK(!authz_user_pattern_ok("a/b")); /* a path is not a user name */
- CHECK(!authz_user_pattern_ok("a$b"));
- CHECK(!authz_user_pattern_ok("-x"));
- CHECK(authz_repo_pattern_ok("team"));
- CHECK(authz_repo_pattern_ok("*"));
- CHECK(authz_repo_pattern_ok("team-*"));
- CHECK(!authz_repo_pattern_ok("group/team")); /* flat names only */
- CHECK(!authz_repo_pattern_ok(""));
- CHECK(!authz_repo_pattern_ok("a$b"));
-}
-
-static void test_parse(void)
-{
- policy_t p;
- int rc;
-
- rc = load_text(&p,
- "# a comment\n"
- "\n"
- " \n"
- "alice web admin # trailing comment\n"
- "bob web write refs/heads/feature/* refs/heads/dev\n"
- "@all team write main\n");
- CHECK_INT(rc, 0);
- CHECK_INT(p.errors, 0);
- CHECK_INT(p.nrules, 3);
- CHECK_STR(p.rules[0].user, "alice");
- CHECK_STR(p.rules[0].repo, "web");
- CHECK_INT(p.rules[0].level, LV_ADMIN);
- CHECK_INT(p.rules[0].nrefs, 0);
- CHECK_INT(p.rules[0].line, 4);
- CHECK_STR(p.rules[1].refs[0], "refs/heads/feature/*");
- CHECK_INT(p.rules[1].nrefs, 2);
- CHECK_STR(p.rules[2].refs[0], "refs/heads/main"); /* normalised at parse time */
- policy_free(&p);
-
- /* Errors: each of these must stop the file being used at all. The complaints
- * are asserted as counts, and the parser writes them to stderr as it goes, so
- * stderr is set aside here - a wall of expected errors would hide the failures.
- * The descriptor is redirected rather than freopen'd, because freopen would
- * quietly make stderr block-buffered and hand the whole wall back at exit. */
- {
- int keep = dup(2);
- int devnull = open("/dev/null", O_WRONLY);
-
- if (devnull >= 0) {
- dup2(devnull, 2);
- close(devnull);
- }
-
- rc = load_text(&p, "alice web everything\n");
- CHECK_INT(rc, -1);
- CHECK_INT(p.errors, 1);
- policy_free(&p);
-
- rc = load_text(&p, "alice web/extra write\n");
- CHECK_INT(rc, -1);
- policy_free(&p);
-
- rc = load_text(&p, "alice web write !refs/heads/main\n");
- CHECK_INT(rc, -1);
- policy_free(&p);
-
- rc = load_text(&p, "alice web\n");
- CHECK_INT(rc, -1);
- policy_free(&p);
-
- rc = load_text(&p, "set other-thing refs/heads/main\n");
- CHECK_INT(rc, -1);
- policy_free(&p);
-
- /* Removed settings are refused by name, not ignored: a policy that
- * still carries one would otherwise mean "everyone with write can fast
- * -forward main" while looking like it said the opposite. */
- rc = load_text(&p, "set protected-refs refs/heads/main\n");
- CHECK_INT(rc, -1);
- policy_free(&p);
-
- rc = load_text(&p, "set protected-refs.web refs/heads/main\n");
- CHECK_INT(rc, -1);
- policy_free(&p);
-
- rc = load_text(&p, "alice web write refs/heads/main\nbob web nope refs/heads/dev\n");
- CHECK_INT(rc, -1); /* one bad line poisons the file: nothing is used */
- policy_free(&p);
-
- fflush(stderr);
- dup2(keep, 2);
- close(keep);
- }
-
- /* There is no settings list any more: a rule says what it says. */
- rc = load_text(&p, "alice * admin\n");
- CHECK_INT(rc, 0);
- CHECK_INT(p.nrules, 1);
- policy_free(&p);
-}
-
-static void test_known_users(void)
-{
- policy_t p;
- int rc;
-
- rc = load_text(&p,
- "alice web admin\n"
- "bob web write\n"
- "@all web write refs/heads/*\n");
- CHECK_INT(rc, 0);
- CHECK(authz_user_known(&p, "alice"));
- CHECK(authz_user_known(&p, "bob"));
- CHECK(!authz_user_known(&p, "carol")); /* '@all' never invents a name */
- CHECK(!authz_user_known(&p, ""));
- policy_free(&p);
-
- rc = load_text(&p, "@all web write\n");
- CHECK_INT(rc, 0);
- CHECK(!authz_user_known(&p, "alice")); /* nobody is known yet */
- policy_free(&p);
-
- rc = load_text(&p, "ali* web write\n");
- CHECK_INT(rc, 0);
- CHECK(authz_user_known(&p, "alice")); /* a pattern is still a name */
- CHECK(!authz_user_known(&p, "bob"));
- policy_free(&p);
-}
-
-static void test_need_for(void)
-{
- char raised[64];
-
- /* A branch or a tag costs exactly what the rule said: there is no list
- * elsewhere in the file that can raise the price of one. */
- CHECK_INT(authz_need_for("refs/heads/dev", LV_WRITE, raised, sizeof(raised)),
- LV_WRITE);
- CHECK_STR(raised, "");
- CHECK_INT(authz_need_for("refs/heads/main", LV_WRITE, raised, sizeof(raised)),
- LV_WRITE);
- CHECK_INT(authz_need_for("refs/tags/v1", LV_WRITE, raised, sizeof(raised)),
- LV_WRITE);
- CHECK_INT(authz_need_for("refs/heads/feature/x", LV_FORCE, raised, sizeof(raised)),
- LV_FORCE);
-
- /* Anything outside refs/heads and refs/tags is for maintainers, whatever a
- * rule appears to grant about it. */
- CHECK_INT(authz_need_for("refs/pull/1/head", LV_WRITE, raised, sizeof(raised)),
- LV_ADMIN);
- CHECK_STR(raised, "neither a branch nor a tag");
- CHECK_INT(authz_need_for("refs/pull/1/head", LV_FORCE, raised, sizeof(raised)),
- LV_ADMIN);
- CHECK_INT(authz_need_for("refs/notes/x", LV_WRITE, raised, sizeof(raised)),
- LV_ADMIN);
- CHECK_INT(authz_need_for("refs/notes/x", LV_ADMIN, raised, sizeof(raised)),
- LV_ADMIN);
- CHECK_STR(raised, ""); /* admin is already the top */
-}
-
-static void test_check(void)
-{
- policy_t p;
- int rc;
-
- rc = load_text(&p,
- "alice * admin\n"
- "@all * write refs/heads/feature/*\n"
- "frank team write refs/heads/main refs/tags/*\n"
- "bob team write refs/heads/feature/*\n"
- "carol team force refs/heads/feature/*\n"
- "dave team admin\n"
- "erin secrets admin\n");
- CHECK_INT(rc, 0);
-
- /* the ladder */
- expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/feature/x");
- expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/feature/x", "needs force");
- expect_deny(&p, "bob", "team", LV_ADMIN, "refs/heads/feature/x", "needs admin");
- expect_allow(&p, "carol", "team", LV_FORCE, "refs/heads/feature/x");
- expect_allow(&p, "dave", "team", LV_ADMIN, "refs/heads/main");
-
- /* strongest match wins, additively */
- expect_allow(&p, "dave", "team", LV_WRITE, "refs/heads/feature/x");
- expect_allow(&p, "alice", "anything", LV_ADMIN, "refs/heads/main");
-
- /* '@all' reaches the named, and nobody else. Note that the '@all *' rule
- * below reaches every repository, so a named user is not confined to the
- * repository their own rule names - that is what the rule says. */
- expect_allow(&p, "carol", "web", LV_WRITE, "refs/heads/feature/any");
- expect_allow(&p, "erin", "web", LV_WRITE, "refs/heads/feature/any");
- expect_deny(&p, "nobody", "web", LV_WRITE, "refs/heads/any", "no rule");
- expect_deny(&p, "", "web", LV_WRITE, "refs/heads/any", "no identity");
- expect_deny(&p, NULL, "web", LV_WRITE, "refs/heads/any", "no identity");
-
- /* main is not special: it is refused here because no rule of bob's or
- * carol's names it, and allowed for dave because his names everything. */
- expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/main",
- "but not on refs/heads/main");
- expect_deny(&p, "carol", "team", LV_FORCE, "refs/heads/main",
- "but not on refs/heads/main");
- expect_allow(&p, "dave", "team", LV_WRITE, "refs/heads/main");
- /* and the person whose rule does name main, and nothing else, gets main */
- expect_allow(&p, "frank", "team", LV_WRITE, "refs/heads/main");
- /* frank reaches the '@all' rule like anyone else the policy names, so what
- * he lacks is a branch nobody's rule names, and a rewrite of his own */
- expect_allow(&p, "frank", "team", LV_WRITE, "refs/heads/feature/x");
- expect_deny(&p, "frank", "team", LV_WRITE, "refs/heads/dev",
- "but not on refs/heads/dev");
- expect_deny(&p, "frank", "team", LV_FORCE, "refs/heads/main", "needs force");
-
- /* refs that are not branches or tags: only an admin, at any request level */
- expect_allow(&p, "dave", "team", LV_WRITE, "refs/pull/1/head");
- expect_allow(&p, "dave", "team", LV_ADMIN, "refs/pull/1/head");
- expect_deny(&p, "bob", "team", LV_ADMIN, "refs/notes/x", "needs admin");
-
- /* the same branch, three spellings */
- expect_deny(&p, "bob", "team", LV_WRITE, "main", "but not on refs/heads/main");
- expect_deny(&p, "bob", "team", LV_WRITE, "heads/main",
- "but not on refs/heads/main");
-
- /* repository matching: .git is the same repository */
- expect_allow(&p, "bob", "team.git", LV_WRITE, "refs/heads/feature/x");
- expect_allow(&p, "erin", "secrets.git", LV_ADMIN, "refs/heads/main");
-
- /* the question without a ref */
- expect_allow(&p, "dave", "team", LV_FORCE, NULL);
- expect_allow(&p, "alice", "team", LV_ADMIN, NULL);
- expect_allow(&p, "bob", "team", LV_WRITE, NULL);
- expect_deny(&p, "bob", "team", LV_FORCE, NULL, "no force");
- expect_allow(&p, "erin", "team", LV_WRITE, NULL); /* via '@all *' */
- expect_deny(&p, "nobody", "team", LV_WRITE, NULL, "no write");
-
- /* what a denial names: the level held, and the level needed */
- {
- decision_t d;
-
- authz_check(&p, "bob", "team", LV_ADMIN, "refs/heads/feature/x", &d);
- CHECK(!d.allowed);
- CHECK_INT(d.need, LV_ADMIN);
- CHECK(strstr(d.why, "bob has write") != NULL);
- CHECK(strstr(d.why, "needs admin") != NULL);
-
- authz_check(&p, "nobody", "team", LV_WRITE, "refs/heads/dev", &d);
- CHECK(!d.allowed);
- CHECK(strstr(d.why, "nobody has no rule for team") != NULL);
- }
- policy_free(&p);
-}
-
-static void test_scope_edges(void)
-{
- policy_t p;
- int rc;
-
- /* a rule with no refs is the whole repository: every branch, main included,
- * which is exactly why naming the refs is the honest way to write one */
- rc = load_text(&p, "bob team write\n");
- CHECK_INT(rc, 0);
- expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/anything");
- expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/main");
- /* write on everything still is not force */
- expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/main", "needs force");
- policy_free(&p);
-
- /* a bare name is a branch, and only a branch */
- rc = load_text(&p, "bob web write refs/heads/*\n");
- CHECK_INT(rc, 0);
- expect_allow(&p, "bob", "web", LV_WRITE, "v1"); /* refs/heads/v1 */
- expect_deny(&p, "bob", "web", LV_WRITE, "refs/tags/v1", "needs write");
- expect_deny(&p, "bob", "web", LV_WRITE, "tags/v1", "needs write");
- policy_free(&p);
-
- /* a user may hold several rules for one repository; all of them count */
- rc = load_text(&p,
- "bob team write refs/heads/dev\n"
- "bob team force refs/heads/feature/*\n");
- CHECK_INT(rc, 0);
- expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/dev");
- expect_allow(&p, "bob", "team", LV_FORCE, "refs/heads/feature/x");
- /* A refusal names the level held on the ref in question, not the strongest
- * level held somewhere in the repository: "you have force here" next to a
- * refusal is a sentence nobody can act on. */
- expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/dev",
- "bob has write on team refs/heads/dev; it needs force");
- expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/other",
- "but not on refs/heads/other");
- policy_free(&p);
-
- /* naming refs in a rule is the whole of the mechanism: what is not named is
- * not granted, and the refusal says so rather than blaming a list */
- rc = load_text(&p, "bob web write refs/heads/feature/* refs/heads/dev\n");
- CHECK_INT(rc, 0);
- expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/dev");
- expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/feature/x");
- expect_deny(&p, "bob", "web", LV_WRITE, "refs/heads/main", "but not on");
- expect_deny(&p, "bob", "api", LV_WRITE, "refs/heads/dev", "no rule");
- policy_free(&p);
-
- /* reachable-by-nobody detection, which validate reports */
- rc = load_text(&p, "bob team write\n");
- CHECK_INT(rc, 0);
- CHECK(authz_repo_reachable(&p, "team"));
- CHECK(authz_repo_reachable(&p, "team.git"));
- CHECK(!authz_repo_reachable(&p, "other"));
- policy_free(&p);
-
- rc = load_text(&p, "bob * write\n");
- CHECK_INT(rc, 0);
- CHECK(authz_repo_reachable(&p, "anything"));
- policy_free(&p);
-}
-
-int main(void)
-{
- const char *dir = getenv("UNIT_TMPDIR");
-
- snprintf(tmproot, sizeof(tmproot), "%s", dir != NULL ? dir : "tests/tmp");
-
- test_glob();
- test_normalisation();
- test_parse();
- test_known_users();
- test_need_for();
- test_check();
- test_scope_edges();
-
- printf("unit: %d checks, %d failure(s)\n", checks, failures);
- if (failures != 0)
- note("these are the semantics; the behavioural suite assumes them");
- return failures != 0;
-}
--
cgit v1.2.3