entrypoint.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/bin/sh
  2. set -eu
  3. # sync with https://github.com/fphammerle/docker-gitolite/blob/master/entrypoint.sh
  4. if [ ! -f "$SSHD_HOST_KEYS_DIR/rsa" ]; then
  5. ssh-keygen -t rsa -b 4096 -N '' -f "$SSHD_HOST_KEYS_DIR/rsa"
  6. fi
  7. if [ ! -f "$SSHD_HOST_KEYS_DIR/ed25519" ]; then
  8. ssh-keygen -t ed25519 -N '' -f "$SSHD_HOST_KEYS_DIR/ed25519"
  9. fi
  10. unset SSHD_HOST_KEYS_DIR
  11. authorize_key() {
  12. if echo -E "$2" | grep -q '^[a-z]'; then
  13. echo "command=\"/usr/bin/borg serve --restrict-to-repository '$1'$3\",restrict $2" >> ~/.ssh/authorized_keys
  14. fi
  15. }
  16. authorize_keys() {
  17. printenv "$1" | while IFS=$'\n' read -r key; do
  18. authorize_key "$2" "$key" "$3"
  19. done
  20. unset "$1"
  21. }
  22. authorize_keys SSH_CLIENT_PUBLIC_KEYS "$REPO_PATH" ""
  23. # https://borgbackup.readthedocs.io/en/stable/usage/notes.html#append-only-mode
  24. authorize_keys SSH_CLIENT_PUBLIC_KEYS_APPEND_ONLY "$REPO_PATH" " --append-only"
  25. unset REPO_PATH
  26. while IFS=$'\n' read line; do
  27. repo_name="$(echo -E "$line" | cut -d = -f 1 | cut -d _ -f 3-)"
  28. if [ "$repo_name" = "ALL" ]; then
  29. echo 'Invalid repository name "ALL". Remove environment variable REPO_PATH_ALL.'
  30. exit 1
  31. fi
  32. repo_path="$(printenv "REPO_PATH_${repo_name}")"
  33. unset "REPO_PATH_${repo_name}"
  34. authorize_keys "SSH_CLIENT_PUBLIC_KEYS_${repo_name}" "$repo_path" ""
  35. authorize_keys "SSH_CLIENT_PUBLIC_KEYS_APPEND_ONLY_${repo_name}" "$repo_path" " --append-only"
  36. done < <(printenv | grep '^REPO_PATH_')
  37. set -x
  38. exec "$@"