Skip to content

Commit 2501a37

Browse files
authored
fix setting the authorized keys when there are more than one in the env var (axolotl-ai-cloud#1626)
1 parent e6937e8 commit 2501a37

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

scripts/cloud-entrypoint.sh

+41-8
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,53 @@ echo "Exporting environment variables..."
55
printenv | grep -E '^RUNPOD_|^PATH=|^_=' | sed 's/^\(.*\)=\(.*\)$/export \1="\2"/' >> /etc/rp_environment
66
echo 'source /etc/rp_environment' >> ~/.bashrc
77

8-
if [[ $PUBLIC_KEY ]]; then
9-
# runpod
8+
add_keys_to_authorized() {
9+
local key_value=$1
10+
11+
# Create the ~/.ssh directory and set permissions
1012
mkdir -p ~/.ssh
1113
chmod 700 ~/.ssh
12-
echo $PUBLIC_KEY >> ~/.ssh/authorized_keys
14+
15+
# Create the authorized_keys file if it doesn't exist
16+
touch ~/.ssh/authorized_keys
17+
18+
# Initialize an empty key variable
19+
local key=""
20+
21+
# Read the key variable word by word
22+
for word in $key_value; do
23+
# Check if the word looks like the start of a key
24+
if [[ $word == ssh-* ]]; then
25+
# If there's a key being built, add it to the authorized_keys file
26+
if [[ -n $key ]]; then
27+
echo $key >> ~/.ssh/authorized_keys
28+
fi
29+
# Start a new key
30+
key=$word
31+
else
32+
# Append the word to the current key
33+
key="$key $word"
34+
fi
35+
done
36+
37+
# Add the last key to the authorized_keys file
38+
if [[ -n $key ]]; then
39+
echo $key >> ~/.ssh/authorized_keys
40+
fi
41+
42+
# Set the correct permissions
43+
chmod 600 ~/.ssh/authorized_keys
1344
chmod 700 -R ~/.ssh
45+
}
46+
47+
if [[ $PUBLIC_KEY ]]; then
48+
# runpod
49+
add_keys_to_authorized "$PUBLIC_KEY"
1450
# Start the SSH service in the background
1551
service ssh start
16-
elif [ -n "$SSH_KEY" ]; then
52+
elif [[ $SSH_KEY ]]; then
1753
# latitude.sh
18-
mkdir -p ~/.ssh
19-
chmod 700 ~/.ssh
20-
echo $SSH_KEY >> ~/.ssh/authorized_keys
21-
chmod 700 -R ~/.ssh
54+
add_keys_to_authorized "$SSH_KEY"
2255
# Start the SSH service in the background
2356
service ssh start
2457
else

0 commit comments

Comments
 (0)