entrypoint.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. repo_path="$(printenv "REPO_PATH_${repo_name}")"
  29. unset "REPO_PATH_${repo_name}"
  30. printenv "SSH_CLIENT_PUBLIC_KEYS_${repo_name}" | while IFS=$'\n' read -r key; do
  31. authorize_key "$repo_path" "$key" ""
  32. done
  33. unset "SSH_CLIENT_PUBLIC_KEYS_${repo_name}"
  34. printenv "SSH_CLIENT_PUBLIC_KEYS_APPEND_ONLY_${repo_name}" | while IFS=$'\n' read -r key; do
  35. authorize_key "$repo_path" "$key" " --append-only"
  36. done
  37. unset "SSH_CLIENT_PUBLIC_KEYS_APPEND_ONLY_${repo_name}"
  38. done < <(printenv | grep '^REPO_PATH_')
  39. set -x
  40. exec "$@"