summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Dockerfile61
-rw-r--r--create-repo.sh28
-rw-r--r--entrypoint.sh66
-rw-r--r--git-host-key181
-rw-r--r--git-http-backend.cgi12
-rw-r--r--sshd_config16
6 files changed, 312 insertions, 52 deletions
diff --git a/Dockerfile b/Dockerfile
index d92efca..b604336 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,5 +1,31 @@
+# The git host: cgit, sshd and lighttpd around the access control executable.
+#
+# podman build -t git-host .
+#
+# Two stages: the runtime image carries no compiler, and copies what the
+# program's own `make install` staged, so image paths cannot drift from build
+# paths. The access control program is not configured here - it is built from
+# its own repository (git-host-c / access-control) and installed by its own
+# installer; this file only assembles the server that runs it.
+#
+# The build stage is Alpine so the binary is musl-linked like the rest of the
+# image. A glibc binary reports "no such file" for a file that is plainly there.
+
+
+FROM alpine:3.22.4 AS build
+ARG GIT_HOST_C_URI=https://git.jnsn.dev/cgit/access-control.git
+ARG GIT_HOST_C_REF=main
+RUN apk add --no-cache build-base git
+WORKDIR /src
+RUN git clone --depth 1 --branch "${GIT_HOST_C_REF}" "${GIT_HOST_C_URI}" /src
+# The suite runs against the musl build, which is the only place the two meet.
+RUN make -j2 && make test && make install DESTDIR=/install
+
+
FROM alpine:3.22.4
+# git-daemon is not a typo: it is the package that ships git-http-backend and
+# git-shell.
RUN apk add --no-cache \
cgit \
git \
@@ -11,7 +37,7 @@ RUN apk add --no-cache \
RUN addgroup -g 1000 -S git \
&& adduser -u 1000 -S -D -G git -h /var/lib/git -s /usr/bin/git-shell git \
- && adduser git tty\
+ && adduser git tty \
&& passwd -u git \
&& mkdir -p \
/etc/git-host/secrets \
@@ -24,20 +50,33 @@ RUN addgroup -g 1000 -S git \
/var/www/localhost/htdocs \
&& chown -R git:git /var/lib/git
-COPY entrypoint.sh /usr/local/bin/entrypoint.sh
-COPY git-http-backend.cgi /usr/local/libexec/git-http-backend.cgi
-COPY create-repo.sh /usr/local/bin/create-repo
-COPY sshd_config /etc/ssh/sshd_config
-COPY cgitrc /etc/cgitrc
-COPY lighttpd.conf /etc/lighttpd/lighttpd.conf
-COPY auth.conf /etc/lighttpd/auth.conf
-COPY healthz /var/www/localhost/htdocs/healthz
+# The access control executable, its example policy and the pre-receive hook
+# symlink, all placed by the program's own installer.
+
+COPY --from=build /install/ /
+
+COPY entrypoint.sh /usr/local/bin/entrypoint.sh
+COPY git-host-key /usr/local/bin/git-host-key
+COPY create-repo.sh /usr/local/bin/create-repo
+COPY git-http-backend.cgi /usr/local/libexec/git-http-backend.cgi
+COPY sshd_config /etc/ssh/sshd_config
+COPY cgitrc /etc/cgitrc
+COPY lighttpd.conf /etc/lighttpd/lighttpd.conf
+COPY auth.conf /etc/lighttpd/auth.conf
+COPY healthz /var/www/localhost/htdocs/healthz
-RUN chmod 0755 /usr/local/bin/entrypoint.sh /usr/local/libexec/git-http-backend.cgi /usr/local/bin/create-repo \
+RUN chmod 0755 \
+ /usr/local/bin/entrypoint.sh \
+ /usr/local/bin/git-host-key \
+ /usr/local/bin/create-repo \
+ /usr/local/libexec/git-http-backend.cgi \
+ && chmod 0644 /etc/git-host/authz.conf \
&& chown git:git /var/lib/git /run/lighttpd /var/tmp/lighttpd /var/www/localhost/htdocs \
&& chmod 0755 /run/git-host /run/git-host/secrets \
+ && git config --file /etc/gitconfig core.hooksPath /usr/local/share/git-host/hooks \
&& grep -q "^git:" /etc/passwd \
- && lighttpd -tt -f /etc/lighttpd/lighttpd.conf
+ && lighttpd -tt -f /etc/lighttpd/lighttpd.conf \
+ && /usr/local/bin/git-authz validate
EXPOSE 8080 2222
VOLUME ["/var/lib/git"]
diff --git a/create-repo.sh b/create-repo.sh
index 5bc213a..5f80c92 100644
--- a/create-repo.sh
+++ b/create-repo.sh
@@ -1,13 +1,23 @@
#!/bin/sh
+# Create a bare repository. Nothing is installed in it: every repository reaches
+# the shared hook through core.hooksPath, so one made by hand is enforced the same.
+#
+# The policy is a read-only mount, so this only prints the line to add.
+
set -eu
GIT_REPO_ROOT="${GIT_REPO_ROOT:-/var/lib/git}"
GIT_USER="${GIT_USER:-git}"
GIT_GROUP="${GIT_GROUP:-git}"
+DEFAULT_BRANCH="${GIT_HOST_DEFAULT_BRANCH:-main}"
usage() {
- echo "usage: create-repo <name> [description]" >&2
- echo "example: create-repo myrepo 'My application repository'" >&2
+ cat >&2 <<USAGE
+usage: create-repo <name> [description]
+
+Creates $GIT_REPO_ROOT/<name>.git - bare, owned by $GIT_USER, HEAD at
+refs/heads/$DEFAULT_BRANCH - and prints the policy line to add.
+USAGE
exit 64
}
@@ -18,26 +28,28 @@ description="${2:-Unnamed repository}"
case "${name}" in
*/* | .* | *" "* | *".."* )
- echo "invalid repository name: ${name}" >&2
+ printf 'invalid repository name: %s\n' "${name}" >&2
exit 64
;;
esac
case "${name}" in
*.git) repo_name="${name}" ;;
- *) repo_name="${name}.git" ;;
+ *) repo_name="${name}.git" ;;
esac
repo_path="${GIT_REPO_ROOT}/${repo_name}"
if [ -e "${repo_path}" ]; then
- echo "repository already exists: ${repo_path}" >&2
+ printf 'repository already exists: %s\n' "${repo_path}" >&2
exit 1
fi
install -d -o "${GIT_USER}" -g "${GIT_GROUP}" "${repo_path}"
-git init --bare "${repo_path}" >/dev/null
-printf '%s\n' "${description}" > "${repo_path}/description"
+git init --bare -b "${DEFAULT_BRANCH}" "${repo_path}" >/dev/null
+printf '%s\n' "${description}" >"${repo_path}/description"
chown -R "${GIT_USER}:${GIT_GROUP}" "${repo_path}"
-echo "created ${repo_path}"
+printf 'created %s\n' "${repo_path}"
+printf 'policy line to add: <user> %s write refs/heads/*\n' "${name%.git}"
+printf ' (edit the policy source and roll the ConfigMap; nothing here writes it)'
diff --git a/entrypoint.sh b/entrypoint.sh
index 3737a7e..df33b47 100644
--- a/entrypoint.sh
+++ b/entrypoint.sh
@@ -1,41 +1,49 @@
#!/bin/sh
set -Eeuo pipefail
-# Copy over the secrets to fix permissions
-mkdir -p /run/git-host/secrets
-cp /etc/git-host/secrets/authorized_keys /run/git-host/secrets/authorized_keys
-cp /etc/git-host/secrets/htpasswd /run/git-host/secrets/htpasswd
-cp /etc/git-host/secrets/ssh_host_ed25519_key /run/git-host/secrets/ssh_host_ed25519_key
-cp /etc/git-host/secrets/ssh_host_ed25519_key.pub /run/git-host/secrets/ssh_host_ed25519_key.pub
-cp /etc/git-host/secrets/ssh_host_rsa_key /run/git-host/secrets/ssh_host_rsa_key
-cp /etc/git-host/secrets/ssh_host_rsa_key.pub /run/git-host/secrets/ssh_host_rsa_key.pub
-chown -R root:root /run/git-host/secrets
-chown root:git /run/git-host/secrets/authorized_keys
-chown root:git /run/git-host/secrets/htpasswd
-chown root:root /run/git-host/secrets/ssh_host_ed25519_key
-chown root:root /run/git-host/secrets/ssh_host_ed25519_key.pub
-chown root:root /run/git-host/secrets/ssh_host_rsa_key
-chown root:root /run/git-host/secrets/ssh_host_rsa_key.pub
-chmod 0755 /run/git-host /run/git-host/secrets
-chmod 0640 /run/git-host/secrets/authorized_keys
-chmod 0640 /run/git-host/secrets/htpasswd
-chmod 0600 /run/git-host/secrets/ssh_host_ed25519_key
-chmod 0644 /run/git-host/secrets/ssh_host_ed25519_key.pub
-chmod 0600 /run/git-host/secrets/ssh_host_rsa_key
-chmod 0644 /run/git-host/secrets/ssh_host_rsa_key.pub
-
-# Validate
-/usr/sbin/sshd -t -f /etc/ssh/sshd_config
-lighttpd -tt -f /etc/lighttpd/lighttpd.conf
-
-# Start servers
+SECRETS_SRC=/etc/git-host/secrets
+SECRETS_DIR=/run/git-host/secrets
+
+need_secret() { # <name> [mode] [owner-group]
+ name="$1"; mode="${2:-0600}"; group="${3:-root}"
+ if [ ! -r "$SECRETS_SRC/$name" ]; then
+ printf 'entrypoint: missing secret %s\n' "$SECRETS_SRC/$name" >&2
+ printf 'entrypoint: mount it there (a Kubernetes Secret works as-is)\n' >&2
+ exit 1
+ fi
+ cp "$SECRETS_SRC/$name" "$SECRETS_DIR/$name"
+ chown "root:$group" "$SECRETS_DIR/$name"
+ chmod "$mode" "$SECRETS_DIR/$name"
+}
+
+# Mounted secrets arrive with whatever mode the volume plugin chose. sshd refuses
+# keys that are too loose; lighttpd must read htpasswd as the git user.
+
+mkdir -p "$SECRETS_DIR"
+need_secret authorized_keys 0640 git
+need_secret htpasswd 0640 git
+need_secret ssh_host_ed25519_key
+need_secret ssh_host_ed25519_key.pub 0644
+need_secret ssh_host_rsa_key
+need_secret ssh_host_rsa_key.pub 0644
+chmod 0755 /run/git-host "$SECRETS_DIR"
+
+# The single most important line in the container: without it a repository carries
+# no policy and pushes go unchecked. The policy is never copied, so a ConfigMap
+# update takes effect on the next push.
+
+git config --file /etc/gitconfig core.hooksPath "${GIT_HOST_HOOK_DIR:-/usr/local/share/git-host/hooks}"
+
+# Do not serve a host that cannot enforce anything.
+
+git-authz validate
+
/usr/sbin/sshd -D -e -f /etc/ssh/sshd_config &
sshd_pid=$!
lighttpd -D -f /etc/lighttpd/lighttpd.conf &
lighttpd_pid=$!
-# Wait for them to end, kill if either does
terminate() {
kill "${sshd_pid}" "${lighttpd_pid}" 2>/dev/null || true
}
diff --git a/git-host-key b/git-host-key
new file mode 100644
index 0000000..4c7c2dc
--- /dev/null
+++ b/git-host-key
@@ -0,0 +1,181 @@
+#!/bin/sh
+# ssh public keys that carry their own identity. sshd only knows the OS account,
+# which every user shares, so identity goes in the key's option list:
+#
+# command="/usr/bin/git-shell",...,environment="GIT_HOST_USER=alice"
+#
+# A key without it is anonymous to the policy and every push with it is denied.
+
+set -eu
+
+KEYS="${GIT_HOST_AUTHORIZED_KEYS:-/etc/git-host/secrets/authorized_keys}"
+RUNTIME_KEYS="${GIT_HOST_AUTHORIZED_KEYS_RUNTIME:-/run/git-host/secrets/authorized_keys}"
+SSH_SHELL="${GIT_HOST_SSH_SHELL:-/usr/bin/git-shell}"
+POLICY="${GIT_HOST_AUTHZ:-/etc/git-host/authz.conf}"
+KEY_TYPES='ssh-rsa ssh-dss ssh-ed25519 ssh-ed448 ecdsa-sha2-nistp256 ecdsa-sha2-nistp384 ecdsa-sha2-nistp521 sk-ssh-ed25519@openssh.com sk-ecdsa-sha2-nistp256@openssh.com'
+
+usage() {
+ cat <<'USAGE'
+git-host-key - manage ssh keys and the user identity attached to them
+
+Usage:
+ git-host-key add <user> [keyfile|-] add key(s) owned by <user> (default: stdin)
+ git-host-key list user, fingerprint and comment per key
+ git-host-key rm <user> remove every key of <user>
+
+Each key is written with the option prefix above, so a push made with one arrives
+named rather than anonymous. Nothing re-checks that afterwards: a key edited in
+by hand can lose the identity option, and the name a key carries still needs a
+rule in the policy file.
+
+Files:
+ GIT_HOST_AUTHORIZED_KEYS authoritative file (default /etc/git-host/secrets/authorized_keys)
+ GIT_HOST_AUTHORIZED_KEYS_RUNTIME file sshd reads (default /run/git-host/secrets/authorized_keys)
+
+After editing, the runtime copy is refreshed and sshd picks it up on the next
+connection (no reload needed). A key only names somebody; what they may do comes
+from the policy file, which is a read-only mount in a deployed container: edit
+your source copy and roll the ConfigMap that provides it.
+USAGE
+}
+
+key_options() {
+ printf 'command="%s",no-port-forwarding,no-agent-forwarding,no-X11-forwarding,no-user-rc,environment="GIT_HOST_USER=%s"' "$SSH_SHELL" "$1"
+}
+
+identity_of() {
+ printf '%s' "$1" | sed -n 's/.*GIT_HOST_USER=\([^",[:space:]]*\).*/\1/p'
+}
+
+# strip any pre-existing option list, returning "<type> <base64> [comment]"
+strip_options() {
+ printf '%s\n' "$1" | awk -v types="$KEY_TYPES" '
+ BEGIN { n = split(types, t, " "); for (i = 1; i <= n; i++) is[t[i]] = 1 }
+ {
+ for (i = 1; i <= NF; i++) {
+ if (is[$i]) {
+ out = ""
+ for (j = i; j <= NF; j++) out = out (j > i ? " " : "") $j
+ print out
+ found = 1
+ exit
+ }
+ }
+ }
+ END { if (!found) exit 1 }'
+}
+
+valid_key_line() {
+ _body="$(strip_options "$1")" || return 1
+ _tmp="$(mktemp)"
+ printf '%s\n' "$_body" >"$_tmp"
+ if command -v ssh-keygen >/dev/null 2>&1; then
+ ssh-keygen -l -f "$_tmp" >/dev/null 2>&1 || { rm -f "$_tmp"; return 1; }
+ fi
+ rm -f "$_tmp"
+ return 0
+}
+
+fingerprint_of() {
+ _body="$(strip_options "$1")" || return 1
+ _tmp="$(mktemp)"
+ printf '%s\n' "$_body" >"$_tmp"
+ if command -v ssh-keygen >/dev/null 2>&1; then
+ ssh-keygen -l -f "$_tmp" 2>/dev/null | awk '{print $2}'
+ else
+ printf 'unknown\n'
+ fi
+ rm -f "$_tmp"
+}
+
+sync_runtime() {
+ [ -f "$KEYS" ] || return 0
+ if [ ! -d "$(dirname "$RUNTIME_KEYS")" ]; then return 0; fi
+ if install -m 0640 "$KEYS" "$RUNTIME_KEYS.tmp" 2>/dev/null; then
+ mv -f "$RUNTIME_KEYS.tmp" "$RUNTIME_KEYS" 2>/dev/null || rm -f "$RUNTIME_KEYS.tmp"
+ else
+ printf 'git-host-key: warning: could not refresh %s (run as root and re-run, or restart the container)\n' "$RUNTIME_KEYS" >&2
+ fi
+}
+
+write_keys() {
+ _tmp="$(mktemp)"
+ printf '%s\n' "$1" >"$_tmp"
+ install -m 0600 "$_tmp" "$KEYS" || { rm -f "$_tmp"; exit 1; }
+ rm -f "$_tmp"
+ sync_runtime
+}
+
+cmd_add() {
+ [ "$#" -ge 1 ] || { usage; exit 64; }
+ user="$1"
+ src="${2:--}"
+ case "$user" in
+ ''|*[!A-Za-z0-9._@-]*) printf 'git-host-key: invalid user name: %s\n' "$user" >&2; exit 64 ;;
+ esac
+ [ -e "$KEYS" ] || { mkdir -p "$(dirname "$KEYS")"; : >"$KEYS"; }
+
+ _added=0
+ _out="$(cat "$KEYS")"
+ while IFS= read -r line; do
+ case "$line" in ''|\#*) continue ;; esac
+ if ! valid_key_line "$line"; then
+ printf 'git-host-key: skipping unparseable key: %s\n' "$(printf '%s' "$line" | cut -c1-60)" >&2
+ continue
+ fi
+ body="$(strip_options "$line")"
+ _out="${_out:+$_out
+}$(key_options "$user") $body"
+ _added=$((_added + 1))
+ done <"$src"
+
+ [ "$_added" -gt 0 ] || { printf 'git-host-key: no usable key found in %s\n' "$src" >&2; exit 1; }
+ write_keys "$_out"
+ printf 'git-host-key: added %s key(s) for %s\n' "$_added" "$user"
+ if ! grep -Eq "^[[:space:]]*${user}[[:space:]]" "$POLICY" 2>/dev/null; then
+ printf 'git-host-key: note: %s has no policy rule yet, so their pushes will be denied.\n' "$user" >&2
+ printf 'git-host-key: add "%s <repo> write <ref pattern>" to the policy source.\n' "$user" >&2
+ fi
+}
+
+cmd_list() {
+ [ -f "$KEYS" ] || return 0
+ _n=0
+ while IFS= read -r line; do
+ case "$line" in ''|\#*) continue ;; esac
+ _n=$((_n + 1))
+ _u="$(identity_of "$line")"
+ [ -n "$_u" ] || _u='<no identity>'
+ printf '%s\t%s\t%s\n' "$_u" "$(fingerprint_of "$line")" "$(printf '%s' "$line" | awk '{print $NF}')"
+ done <"$KEYS"
+ [ "$_n" -gt 0 ] || printf 'git-host-key: no keys in %s\n' "$KEYS" >&2
+}
+
+cmd_rm() {
+ [ "$#" -ge 1 ] || { usage; exit 64; }
+ user="$1"
+ [ -f "$KEYS" ] || exit 0
+ _new=''
+ _removed=0
+ while IFS= read -r line; do
+ case "$line" in ''|\#*) continue ;; esac
+ if [ "$(identity_of "$line")" = "$user" ]; then
+ _removed=$((_removed + 1))
+ continue
+ fi
+ _new="${_new:+$_new
+}$line"
+ done <"$KEYS"
+ write_keys "${_new:+$_new
+}"
+ printf 'git-host-key: removed %s key(s) of %s\n' "$_removed" "$user"
+}
+
+cmd="${1:-}"; shift || true
+case "$cmd" in
+ add) cmd_add "$@" ;;
+ list) cmd_list "$@" ;;
+ rm) cmd_rm "$@" ;;
+ -h|--help|'') usage ;;
+ *) printf 'git-host-key: unknown command: %s\n' "$cmd" >&2; usage >&2; exit 64 ;;
+esac
diff --git a/git-http-backend.cgi b/git-http-backend.cgi
index 960ebc0..c69758b 100644
--- a/git-http-backend.cgi
+++ b/git-http-backend.cgi
@@ -1,8 +1,12 @@
#!/bin/sh
+# A .cgi-named door onto the stock backend, because mod_cgi dispatches by
+# extension. No policy in here: writes are decided by the pre-receive hook.
+#
+# It must stay a plain exec. REMOTE_USER is the identity the hooks authorise
+# from, and rewriting the environment here denies every push.
set -eu
-export GIT_PROJECT_ROOT="${GIT_PROJECT_ROOT:-/var/lib/git}"
-export GIT_HTTP_EXPORT_ALL="${GIT_HTTP_EXPORT_ALL:-1}"
-export HOME="${HOME:-/home/git}"
+BACKEND="${GIT_HOST_HTTP_BACKEND:-/usr/libexec/git-core/git-http-backend}"
+[ -x "$BACKEND" ] || { printf 'Status: 500 Internal Server Error\r\n\r\n' >&2; exit 1; }
-exec /usr/libexec/git-core/git-http-backend
+exec "$BACKEND"
diff --git a/sshd_config b/sshd_config
index 3b5c369..fca7e7b 100644
--- a/sshd_config
+++ b/sshd_config
@@ -3,7 +3,23 @@ ListenAddress 0.0.0.0
HostKey /run/git-host/secrets/ssh_host_ed25519_key
HostKey /run/git-host/secrets/ssh_host_rsa_key
PidFile /run/sshd/sshd.pid
+
+# One OS account for everyone; the name behind a key comes from the key itself:
+#
+# environment="GIT_HOST_USER=alice",command="/usr/bin/git-shell",... ssh-ed25519 ...
+
AuthorizedKeysFile /run/git-host/secrets/authorized_keys
+
+# Without this sshd silently drops the environment= option and every push arrives
+# anonymous. The usual objection does not apply: there is no login shell here
+# (ForceCommand below), no writable files and no user rc file.
+
+PermitUserEnvironment yes
+
+# For a hand-added key that forgot its command= option: git transport, no shell.
+
+ForceCommand /usr/bin/git-shell
+
PasswordAuthentication no
KbdInteractiveAuthentication no
ChallengeResponseAuthentication no