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