1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
git-authz - who may move which refs
===================================
One C program that decides, on every push, whether the person on the other end
of the connection is allowed to move the refs they are trying to move. C99 and
libc, no dependencies, one binary, one plain-text policy file.
make
make test
make install DESTDIR=/tmp/stage
It exists because a git host that authenticates pushes but not *what* is pushed
has one ACL for the whole server, and the only way to widen it is to hand
somebody the key to everything. Here: reads stay anonymous, and writing is
per user, per repository, per ref pattern.
Commands
--------
git-authz [--policy PATH] check <user> <repo> <write|force|admin> [ref]
git-authz [--policy PATH] authorize [options]
git-authz [--policy PATH] validate [options]
git-authz [--policy PATH] grant <user> <repo> <level> [ref ...]
git-authz [--policy PATH] revoke <user> <repo>
`check` answers one question and is for humans and scripts. `authorize` is what
git invokes. `validate` asks whether the host is wired to run the policy at all.
The last two edit the file.
Exit status is the interface, and it does not change:
0 allowed
1 denied
3 no usable policy - the host is broken, not this person
64 usage error
Three is separate from one on purpose. A push refused because the policy file
was not mounted produces one line in a log somewhere; a push refused because the
user is not allowed produces a message to a colleague. They must not sound the
same, and a monitor must not have to parse prose to tell them apart.
The policy
----------
<user> <repo> <write|force|admin> [ <ref pattern> ... ]
alice * admin
@all * write refs/heads/*
bob team write refs/heads/feature/*
carol team force refs/heads/feature/*
dave team admin
Levels are monotone: admin implies force implies write. Rules are additive and
the strongest match wins; there is no `deny`, so access is taken away by deleting
the rule that granted it, which means the file always says everything the file
grants.
`<repo>` is flat: a name, optionally with a `*`, and `*` alone for everything.
Names are compared with `.git` removed, so `team` and `team.git` are one
repository.
A ref pattern is a glob in which `*` crosses `/`; `?`, `[abc]`, `[a-z]` and
`[!abc]` work. Refs are normalised before matching, so `main`, `heads/main` and
`refs/heads/main` are the same branch, and a bare name is *always* a branch - a
tag has to be written `refs/tags/v1` or `tags/v1`. A rule with no patterns covers
the whole repository, refs and all.
`@all` means everyone the policy already names somewhere. It is not a wildcard
for anonymous users: a name that appears nowhere in the file reaches nothing, so
adding a rule for a person is the whole of onboarding and `@all` cannot be used
to hand out access to someone who was never meant to have any.
What an update costs
--------------------
The hook reads `<old> <new> <ref>` lines on stdin, exactly as git writes them,
and works out what the update *is* before it asks who may do it:
create a ref write
fast-forward write
rewrite (non-fast-forward) force
delete a ref force
delete the branch HEAD points at admin
a ref outside refs/heads, refs/tags admin
That is the whole of it. A rule says what it says: there is no second list
somewhere else in the file that can make one ref cost more than another, so the
answer to "who can move main" is the lines that name main, and nothing else.
Which means an unscoped rule really does cover main:
dave team admin # every ref in team, main included
and a rule that means to leave main alone has to say so by naming what it covers
instead:
bob team write refs/heads/feature/* # main is not named, so not granted
Anything that is not a branch or a tag - `refs/notes`, `refs/merge-requests`,
whatever a tool invents next - is admin-only whatever a rule appears to grant
about it. That is the one price the program raises on its own, and it is the one
the policy cannot name in advance: it is the part of the namespace no policy was
written with in mind, and the safe reading of "not mentioned" is "not granted".
Identity
--------
There is one operating-system account, `git`, as there is on any host that
wants ssh to be usable. A person is not the account; a person is the key:
environment="GIT_HOST_USER=alice",restrict ssh-ed25519 AAAA... alice@laptop
Over HTTP the identity is `REMOTE_USER`, set by the web server's basic auth and
passed through the CGI shim to the backend, which passes it to the hook.
A push with no identity is denied, whatever the policy says. `@all` does not
rescue it, because whoever is anonymous is not somebody the policy named.
Two facts belong to git
-----------------------
Whether `old` is an ancestor of `new`, and which branch `HEAD` points at, are
facts about the object store. The hook asks git, and only git:
git merge-base --is-ancestor <old> <new>
git symbolic-ref -q HEAD
Everything else - who this is, what the policy says, what the update costs - it
works out itself, so a push is denied rather than allowed if git is missing, or
the repository is unreadable, or the answer does not come:
* an update whose ancestry cannot be determined is treated as a rewrite, which
costs `force`;
* a deletion whose HEAD cannot be determined is treated as the default branch,
which costs `admin`.
`--ff=yes|no` and `--head=REF|none` supply those answers directly, and `--no-git`
asks nothing at all. That is how the test suite pins the consequences of every
answer without building a repository for each one.
Wiring
------
core.hooksPath = /usr/local/share/git-host/hooks
-> pre-receive, a relative symlink to the binary, whose name selects
authorize mode
`make install` puts the binary in `$(BINDIR)`, the example policy in
`$(SYSCONFDIR)/authz.conf` (leaving an existing one alone), and the symlink in
`$(HOOKDIR)/pre-receive`.
`validate` is what an entrypoint runs before it starts serving:
git-authz validate || exit 1
It reports a policy that does not parse, a policy no user can push to any
repository with, a repository nobody can push to, a missing or non-executable
hook, a `core.hooksPath` pointing somewhere else, `PermitUserEnvironment no`, no
`ForceCommand`, a `git` account with a real shell, key identities with no rule,
and htpasswd users with no rule. Warnings do not stop it; errors do, and
`--strict` makes warnings stop it too. `--no-host` checks the policy without
looking at the machine, which is what you want from CI on a checkout.
The point of that last set of checks is the failure mode where the policy is
fine, the server is up, and every push is anonymous - because ssh was not told to
accept environment variables, or the hook path moved, and nothing said so.
Editing the file
----------------
git-authz grant bob team write refs/heads/feature/*
git-authz revoke bob team
Each writes the file atomically - a temporary in the same directory, then a
rename, keeping the mode and owner - and preserves everything else in the file,
comments and blank lines included, so a grant does not undo the ordering an
operator laid out. `grant` collapses several rules for the same user and
repository into one line, in the place of the first, because the strongest wins
and a second rule that does nothing is a trap for the next reader.
A file that does not exist yet is started, with the grammar written at the top of
it; a file that does not parse is refused, because editing something the parser
cannot read would rewrite the parts that parse and keep the damage.
The deployed file is a read-only mount - a ConfigMap - by design: the hook should
not be able to change who can push, and neither should whatever else runs as uid
1000. These commands are for the copy in git. Pointed at a read-only file they
fail with that explanation rather than writing somewhere surprising, and `--out`
or `--stdout` writes the result elsewhere without touching the input.
Reading this as an operator
---------------------------
who may write to team?
git-authz check bob team write refs/heads/feature/login
why was my push refused?
Because the message ends with what it would have taken:
deny: bob holds write on team, but not on refs/heads/main;
that ref needs write
or, when the ref is covered by a rule but costs more than it looks:
deny: bob has write on team refs/notes/x; it needs admin
(neither a branch nor a tag)
which is the whole design in one line: a refusal that names the level held,
the ref, the level required and why, so the fix is obvious to the person who
hit it instead of a ticket to whoever set this up.
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 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
of it: argument parsing, exit codes, messages, and the editing commands writing
files.
`make clean && make sanitizer` runs both suites under AddressSanitizer and UBSan.
Everything frees what it reads, which matters only because it keeps that run
empty.
|