blob: feca575bc8745a43a15e7b702f731ccbe2b8ffc7 [file] [log] [blame]
Mohammed Naserf3f59a72023-01-15 21:02:04 -05001#!/bin/bash
2
3{{/*
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/}}
16
17set -ex
18
19IFS=','
20for KEY_TYPE in $KEY_TYPES; do
21 KEY_PATH=/etc/ssh/ssh_host_${KEY_TYPE}_key
22 if [[ ! -f "${KEY_PATH}" ]]; then
23 ssh-keygen -q -t ${KEY_TYPE} -f ${KEY_PATH} -N ""
24 fi
25done
26IFS=''
27
28subnet_address="{{- .Values.network.ssh.from_subnet -}}"
Oleksandr K.24c88fd2024-12-08 22:28:50 -080029
30if [ -z "${subnet_address}" ] ; then
31 subnet_address="0.0.0.0/0"
32fi
33listen_interface=$(ip -4 route list ${subnet_address} | awk -F 'dev' '{ print $2; exit }' | awk '{ print $1 }') || exit 1
34listen_address=$(ip a s $listen_interface | grep 'inet ' | awk '{print $2}' | awk -F "/" '{print $1}' | head -1)
35
Mohammed Naserf3f59a72023-01-15 21:02:04 -050036cat > /tmp/sshd_config_extend <<EOF
Oleksandr K.24c88fd2024-12-08 22:28:50 -080037ListenAddress $listen_address
Mohammed Naserf3f59a72023-01-15 21:02:04 -050038PasswordAuthentication no
39Match Address $subnet_address
40 PermitRootLogin without-password
41EOF
42cat /tmp/sshd_config_extend >> /etc/ssh/sshd_config
43
44rm /tmp/sshd_config_extend
45
Oleksandr Kozachenkoc0022be2023-05-23 20:36:21 +020046mkdir -p /run/sshd
47
Mohammed Naserf3f59a72023-01-15 21:02:04 -050048exec /usr/sbin/sshd -D -e -o Port=$SSH_PORT