summaryrefslogtreecommitdiffstats
path: root/entrypoint.sh
blob: fbe25024706825b30d16c4095c6482e696a57c57 (plain)
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
#!/bin/sh
set -Eeuo pipefail

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"
}

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"

# Set a git hook for authorization
git config --file /etc/gitconfig core.hooksPath "${GIT_HOST_HOOK_DIR:-/usr/local/share/git-host/hooks}"
git-authz validate

/usr/sbin/sshd -D -e -f /etc/ssh/sshd_config &
sshd_pid=$!

lighttpd -D -f /etc/lighttpd/lighttpd.conf &
lighttpd_pid=$!

terminate() {
  kill "${sshd_pid}" "${lighttpd_pid}" 2>/dev/null || true
}

trap terminate INT TERM

status=0
wait -n "${sshd_pid}" "${lighttpd_pid}" || status=$?
terminate
wait "${sshd_pid}" 2>/dev/null || true
wait "${lighttpd_pid}" 2>/dev/null || true
exit "${status}"