entrypoint.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 "$1" | grep -q '^[a-z]'; then
  13. echo "command=\"/usr/bin/borg serve$2\",restrict $1" >> ~/.ssh/authorized_keys
  14. fi
  15. }
  16. authorize_keys() {
  17. printenv "$1" | while IFS=$'\n' read -r key; do
  18. authorize_key "$key" " --restrict-to-repository '$2'$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. all_repo_restrictions=" --restrict-to-repository '$REPO_PATH'"
  26. unset REPO_PATH
  27. while IFS=$'\n' read line; do
  28. repo_name="$(echo -E "$line" | cut -d = -f 1 | cut -d _ -f 3-)"
  29. if [ "$repo_name" = "ALL" ]; then
  30. echo 'Invalid repository name "ALL". Remove environment variable REPO_PATH_ALL.'
  31. exit 1
  32. fi
  33. repo_path="$(printenv "REPO_PATH_${repo_name}")"
  34. all_repo_restrictions="$all_repo_restrictions --restrict-to-repository '$repo_path'"
  35. unset "REPO_PATH_${repo_name}"
  36. authorize_keys "SSH_CLIENT_PUBLIC_KEYS_${repo_name}" "$repo_path" ""
  37. authorize_keys "SSH_CLIENT_PUBLIC_KEYS_APPEND_ONLY_${repo_name}" "$repo_path" " --append-only"
  38. done < <(printenv | grep '^REPO_PATH_')
  39. printenv SSH_CLIENT_PUBLIC_KEYS_ALL | while IFS=$'\n' read -r key; do
  40. authorize_key "$key" "$all_repo_restrictions"
  41. done
  42. unset SSH_CLIENT_PUBLIC_KEYS_ALL
  43. set -x
  44. exec "$@"