From f35b108e172e5f5a690e8f1ea8ffc7acf2e2cc9d Mon Sep 17 00:00:00 2001 From: Kevin Baensch Date: Fri, 23 Dec 2022 09:54:21 +0100 Subject: [PATCH] Small fixes to setup script. --- .gitignore | 1 + etc/ssh/.ssh/authorized_keys | 0 setup.sh | 36 +++++++++++++----------------------- 3 files changed, 14 insertions(+), 23 deletions(-) delete mode 100644 etc/ssh/.ssh/authorized_keys diff --git a/.gitignore b/.gitignore index d483373..948284d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ etc/ssh/*key* etc/ssh/.ssh/ +config/ diff --git a/etc/ssh/.ssh/authorized_keys b/etc/ssh/.ssh/authorized_keys deleted file mode 100644 index e69de29..0000000 diff --git a/setup.sh b/setup.sh index 92e9113..217dd8d 100755 --- a/setup.sh +++ b/setup.sh @@ -19,16 +19,22 @@ check() { } setup_base() { - mkdir -p "${PROJECT_PATH}/config" "${PROJECT_PATH}/caddy_data" + printf 'Change ownership of "%s" to "%s"? (setup may fail otherwise)\n' "${PROJECT_PATH}" "${WHOAMI}" + printf 'running: "sudo chown -R %s %s"\n' "${WHOAMI}:${WHOAMI}" "${PROJECT_PATH}" + if check 'Continue? [Y/n] ' + then + sudo chown -R "${WHOAMI}:${WHOAMI}" "${PROJECT_PATH}" + fi + + + mkdir -p "${PROJECT_PATH}/config" "${PROJECT_PATH}/caddy_data" "${PROJECT_PATH}/etc/ssh/.ssh" + touch "${PROJECT_PATH}/config/Caddyfile" "${PROJECT_PATH}/etc/ssh/.ssh/authorized_keys" if [ "$(uname -s)" = 'Darwin' ] && [ ! -w '/etc/hosts' ] then printf 'On MacOS docker is run by your local user (not root).\nYour user has no write permission for "/etc/hosts".\nRunning: "sudo chown %s /etc/hosts"\n' "${WHOAMI}" if check 'Continue? [Y/n] ' then sudo chown "${WHOAMI}" '/etc/hosts' - else - printf 'Setup Aborted!\n' - exit 1 fi fi } @@ -91,31 +97,15 @@ setup_sqlproxy() { printf '"""\nHost %s\n Port 3022\n User sqlproxy\n IdentityFile ~/.ssh/%s\n"""\n' "${HOST_NAME}" "${KEY_NAME}" fi - # Fix permssions if necessary - if [[ ! -w "${PROJECT_PATH}/etc/ssh/.ssh" ]] || [[ ! -w "${PROJECT_PATH}/etc/ssh/.ssh/authorized_keys" ]] - then - printf 'Missing file permissions for authorized key file\nrunning: "sudo chown -R %s %s"\n' "${WHOAMI}:${WHOAMI}" "${PROJECT_PATH}" - if check 'Continue? [Y/n] ' - then - sudo chown -R "${WHOAMI}:${WHOAMI}" "${PROJECT_PATH}" - else - printf 'Setup Aborted!\n' - exit 1 - fi - fi - # Check if public key is already in the containers authorized_keys file PUB_KEY="$(cat ${HOME}/.ssh/${KEY_NAME}.pub)" if ! grep -qe "$(printf '%s$' "${PUB_KEY}")" "${PROJECT_PATH}/etc/ssh/.ssh/authorized_keys" then printf 'command="/sqlproxy_cli.sh" %s' "${PUB_KEY}" >> "${PROJECT_PATH}/etc/ssh/.ssh/authorized_keys" fi - # Restart sshd if permissions were changed - if [ ! -z "${WHOAMI}" ] - then - printf 'Restarting sql proxy (if running) to fix permissions.\n' - docker compose --project-directory "${PROJECT_PATH}" -f "${PROJECT_PATH}/docker-compose.yml" -f "${PROJECT_PATH}/docker-compose-sqlproxy.yml" restart sshd - fi + + printf 'Restarting sql proxy (if running) to fix permissions.\n' + docker compose --project-directory "${PROJECT_PATH}" -f "${PROJECT_PATH}/docker-compose.yml" restart sshd else printf 'Not generating client ssh key.\nPlease put your desired public keys into %s\nAlso add %s in front of your key\n' "${PROJECT_PATH}/etc/ssh/.ssh/authorized_keys" "'command=\"/sqlproxy_cli.sh\" '" fi