Skip to content

Commit 3455a0c

Browse files
author
Corentin ARNOULD
committed
Add support for certificate auth.
1 parent 1ee06fe commit 3455a0c

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

readme-vars.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ opt_param_env_vars:
3030
- {env_var: "PUBLIC_KEY_FILE", env_value: "/path/to/file", desc: "Optionally specify a file containing the public key (works with docker secrets)."}
3131
- {env_var: "PUBLIC_KEY_DIR", env_value: "/path/to/directory/containing/_only_/pubkeys", desc: "Optionally specify a directory containing the public keys (works with docker secrets)."}
3232
- {env_var: "PUBLIC_KEY_URL", env_value: "https://github.com/username.keys", desc: "Optionally specify a URL containing the public key."}
33+
- {env_var: "TRUSTED_CA", env_value: "yourtrustedca", desc: "Optional trusted certificate authority, which will automatically be added to trusted_ca."}
34+
- {env_var: "TRUSTED_CA_FILE", env_value: "/path/to/file", desc: "Optionally specify a file containing the trusted certificate authorities (works with docker secrets)."}
35+
- {env_var: "TRUSTED_CA_DIR", env_value: "/path/to/directory/containing/_only_/ca", desc: "Optionally specify a directory containing the certificate authorities (works with docker secrets)."}
36+
- {env_var: "TRUSTED_CA_URL", env_value: "https://github.com/ca.pub", desc: "Optionally specify a URL containing the certificate authority."}
3337
- {env_var: "SUDO_ACCESS", env_value: "false", desc: "Set to `true` to allow `linuxserver.io`, the ssh user, sudo access. Without `USER_PASSWORD` set, this will allow passwordless sudo access."}
3438
- {env_var: "PASSWORD_ACCESS", env_value: "false", desc: "Set to `true` to allow user/password ssh access. You will want to set `USER_PASSWORD` or `USER_PASSWORD_FILE` as well."}
3539
- {env_var: "USER_PASSWORD", env_value: "password", desc: "Optionally set a sudo password for `linuxserver.io`, the ssh user. If this or `USER_PASSWORD_FILE` are not set but `SUDO_ACCESS` is set to true, the user will have passwordless sudo access."}
@@ -44,6 +48,8 @@ app_setup_block: |
4448
If `PUBLIC_KEY` or `PUBLIC_KEY_FILE`, or `PUBLIC_KEY_DIR` variables are set, the specified keys will automatically be added to `authorized_keys`. If not, the keys can manually be added to `/config/.ssh/authorized_keys` and the container should be restarted.
4549
Removing `PUBLIC_KEY` or `PUBLIC_KEY_FILE` variables from docker run environment variables will not remove the keys from `authorized_keys`. `PUBLIC_KEY_FILE` and `PUBLIC_KEY_DIR` can be used with docker secrets.
4650
51+
If one or more of the `TRUSTED_CA_*` variables are set, the certificates will be concatenated before being passed to `TrustedUserCAKeys`. If a CA is removed from the variables it will be removed from the list at the next container restart.
52+
4753
We provide the ability to set and allow password based access via the `PASSWORD_ACCESS` and `USER_PASSWORD` variables, though we as an organization discourage using password auth for public facing ssh endpoints.
4854
4955
Connect to server via `ssh -i /path/to/private/key -p PORT USER_NAME@SERVERIP`
@@ -117,6 +123,7 @@ init_diagram: |
117123
"openssh-server:latest" <- Base Images
118124
# changelog
119125
changelogs:
126+
- {date: "15.04.26:", desc: "Add support for certificate auth."}
120127
- {date: "28.12.25:", desc: "Rebase to Alpine 3.23."}
121128
- {date: "05.07.25:", desc: "Rebase to Alpine 3.22."}
122129
- {date: "10.02.25:", desc: "Add support for sshd_config.d"}

root/etc/s6-overlay/s6-rc.d/init-openssh-server-config/run

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,53 @@ if [[ -d "$PUBLIC_KEY_DIR" ]]; then
128128
done
129129
fi
130130

131+
# set trusted certificate authority in file
132+
echo -n "" >/config/sshd/trusted_ca
133+
134+
if [[ -n "$TRUSTED_CA" ]]; then
135+
if ! grep -q "${TRUSTED_CA}" /config/sshd/trusted_ca; then
136+
echo "$TRUSTED_CA" >> /config/sshd/trusted_ca
137+
echo "Trusted CA from env variable added"
138+
fi
139+
fi
140+
141+
if [[ -n "$TRUSTED_CA_URL" ]]; then
142+
TRUSTED_CA_DOWNLOADED=$(curl -s "$TRUSTED_CA_URL")
143+
if ! grep -q "$TRUSTED_CA_DOWNLOADED" /config/sshd/trusted_ca; then
144+
echo "$TRUSTED_CA_DOWNLOADED" >> /config/sshd/trusted_ca
145+
echo "Trusted CA downloaded from '$TRUSTED_CA_URL' added"
146+
fi
147+
fi
148+
149+
if [[ -n "$TRUSTED_CA_FILE" ]] && [[ -f "$TRUSTED_CA_FILE" ]]; then
150+
TRUSTED_CA2=$(cat "$TRUSTED_CA_FILE")
151+
if ! grep -q "$TRUSTED_CA2" /config/sshd/trusted_ca; then
152+
echo "$TRUSTED_CA2" >> /config/sshd/trusted_ca
153+
echo "Trusted CA from file added"
154+
fi
155+
fi
156+
157+
if [[ -d "$TRUSTED_CA_DIR" ]]; then
158+
for F in "${TRUSTED_CA_DIR}"/*; do
159+
TRUSTED_CAN=$(cat "$F")
160+
if ! grep -q "$TRUSTED_CAN" /config/sshd/trusted_ca; then
161+
echo "$TRUSTED_CAN" >> /config/sshd/trusted_ca
162+
echo "Trusted CA from file '$F' added"
163+
fi
164+
done
165+
fi
166+
167+
if [[ -s /config/sshd/trusted_ca ]]; then
168+
sed -i '/^#TrustedUserCAKeys/c\TrustedUserCAKeys /config/sshd/trusted_ca' /config/sshd/sshd_config
169+
sed -i '/^TrustedUserCAKeys/c\TrustedUserCAKeys /config/sshd/trusted_ca' /config/sshd/sshd_config
170+
171+
if ! grep -q "^TrustedUserCAKeys" /config/sshd/sshd_config; then
172+
echo "TrustedUserCAKeys /config/sshd/trusted_ca" >>/config/sshd/sshd_config
173+
fi
174+
else
175+
sed -i 's/^TrustedUserCAKeys/#TrustedUserCAKeys' /config/sshd/sshd_config
176+
fi
177+
131178
# back up old log files processed by logrotate
132179
if [[ -f /config/logs/openssh/openssh.log ]]; then
133180
mv /config/logs/openssh /config/logs/openssh.old.logs
@@ -148,6 +195,8 @@ chmod 700 \
148195
/config/.ssh
149196
chmod 600 \
150197
/config/.ssh/authorized_keys
198+
chmod 644 \
199+
/config/sshd/trusted_ca
151200

152201
lsiown -R root:"${USER_NAME}" \
153202
/config/sshd

0 commit comments

Comments
 (0)