Skip to content

Commit

Permalink
Fix and improve scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
loranmutafov committed Feb 26, 2023
1 parent 47add70 commit 2e61149
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ ARG GID=4096

ENV HOST_KEYS_PATH_PREFIX="/usr"
ENV HOST_KEYS_PATH="${HOST_KEYS_PATH_PREFIX}/etc/ssh"
ENV BASTION_USER=${USER}
ENV BASTION_GROUP=${GROUP}

COPY bastion /usr/sbin/bastion
COPY setup-keys.sh /usr/sbin/setup-keys.sh
Expand Down
8 changes: 4 additions & 4 deletions bastion
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env sh

HOST_KEYS_PATH_PREFIX="${HOST_KEYS_PATH_PREFIX:='/'}"
HOST_KEYS_PATH="${HOST_KEYS_PATH:='/etc/ssh'}"

SETUP_KEYS_PATH="${SETUP_KEYS_PATH:='/etc/bastion/ssh-keys'}"
SETUP_KEYS_PATH="${SETUP_KEYS_PATH:=/etc/bastion/ssh-keys}"
if [ -f "$SETUP_KEYS_PATH" ] || [ -L "$SETUP_KEYS_PATH" ]; then
./setup-keys.sh
fi

HOST_KEYS_PATH_PREFIX="${HOST_KEYS_PATH_PREFIX:=/}"
HOST_KEYS_PATH="${HOST_KEYS_PATH:=/etc/ssh}"

if [ "$PUBKEY_AUTHENTICATION" == "false" ]; then
CONFIG_PUBKEY_AUTHENTICATION="-o PubkeyAuthentication=no"
else
Expand Down
26 changes: 15 additions & 11 deletions setup-keys.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#!/bin/sh
while IFS= read -r line
do
IFS=';' read -r username publickey <<EOF
# Escape into (subshell), because we're modifying the IFS
(
while IFS= read -r line
# Yet another (subshell), because we're modifying the IFS we're using
do (
IFS=':' read -r username publickey <<EOF
$line
EOF
adduser -D -h "/home/${username}" -s /bin/ash -g "${username} service" \
-G "${GROUP}" "${username}"
adduser -D -h "/home/${username}" -s /bin/ash -g "${username} service" \
-G "${BASTION_GROUP}" "${username}"

mkdir -p "/home/${username}/.ssh"
echo "${publickey}" > "/home/${username}/.ssh/authorized_keys"
mkdir -p "/home/${username}/.ssh"
echo "${publickey}" > "/home/${username}/.ssh/authorized_keys"

chown -R "${username}":"${GROUP}" "/home/${username}/.ssh"
chmod 700 "/home/${username}/.ssh"
chmod 600 "/home/${username}/.ssh/authorized_keys"
done <"${SETUP_KEYS_PATH}"
chown -R "${username}":"${BASTION_GROUP}" "/home/${username}/.ssh"
chmod 700 "/home/${username}/.ssh"
chmod 600 "/home/${username}/.ssh/authorized_keys"
) done <"${SETUP_KEYS_PATH}"
)

0 comments on commit 2e61149

Please sign in to comment.