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 "$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. printenv SSH_CLIENT_PUBLIC_KEYS | while IFS=$'\n' read -r key; do
  17. authorize_key "$REPO_PATH" "$key" ""
  18. done
  19. unset SSH_CLIENT_PUBLIC_KEYS
  20. # https://borgbackup.readthedocs.io/en/stable/usage/notes.html#append-only-mode
  21. printenv SSH_CLIENT_PUBLIC_KEYS_APPEND_ONLY | while IFS=$'\n' read -r key; do
  22. authorize_key "$REPO_PATH" "$key" " --append-only"
  23. done
  24. unset SSH_CLIENT_PUBLIC_KEYS_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. printenv "SSH_CLIENT_PUBLIC_KEYS_${repo_name}" | while IFS=$'\n' read -r key; do
  35. authorize_key "$repo_path" "$key" ""
  36. done
  37. unset "SSH_CLIENT_PUBLIC_KEYS_${repo_name}"
  38. printenv "SSH_CLIENT_PUBLIC_KEYS_APPEND_ONLY_${repo_name}" | while IFS=$'\n' read -r key; do
  39. authorize_key "$repo_path" "$key" " --append-only"
  40. done
  41. unset "SSH_CLIENT_PUBLIC_KEYS_APPEND_ONLY_${repo_name}"
  42. done < <(printenv | grep '^REPO_PATH_')
  43. set -x
  44. exec "$@"