winglian commited on
Commit
2501a37
1 Parent(s): e6937e8

fix setting the authorized keys when there are more than one in the env var (#1626)

Browse files
Files changed (1) hide show
  1. scripts/cloud-entrypoint.sh +41 -8
scripts/cloud-entrypoint.sh CHANGED
@@ -5,20 +5,53 @@ echo "Exporting environment variables..."
5
  printenv | grep -E '^RUNPOD_|^PATH=|^_=' | sed 's/^\(.*\)=\(.*\)$/export \1="\2"/' >> /etc/rp_environment
6
  echo 'source /etc/rp_environment' >> ~/.bashrc
7
 
8
- if [[ $PUBLIC_KEY ]]; then
9
- # runpod
 
 
10
  mkdir -p ~/.ssh
11
  chmod 700 ~/.ssh
12
- echo $PUBLIC_KEY >> ~/.ssh/authorized_keys
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  chmod 700 -R ~/.ssh
 
 
 
 
 
14
  # Start the SSH service in the background
15
  service ssh start
16
- elif [ -n "$SSH_KEY" ]; then
17
  # latitude.sh
18
- mkdir -p ~/.ssh
19
- chmod 700 ~/.ssh
20
- echo $SSH_KEY >> ~/.ssh/authorized_keys
21
- chmod 700 -R ~/.ssh
22
  # Start the SSH service in the background
23
  service ssh start
24
  else
 
5
  printenv | grep -E '^RUNPOD_|^PATH=|^_=' | sed 's/^\(.*\)=\(.*\)$/export \1="\2"/' >> /etc/rp_environment
6
  echo 'source /etc/rp_environment' >> ~/.bashrc
7
 
8
+ add_keys_to_authorized() {
9
+ local key_value=$1
10
+
11
+ # Create the ~/.ssh directory and set permissions
12
  mkdir -p ~/.ssh
13
  chmod 700 ~/.ssh
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
44
  chmod 700 -R ~/.ssh
45
+ }
46
+
47
+ if [[ $PUBLIC_KEY ]]; then
48
+ # runpod
49
+ add_keys_to_authorized "$PUBLIC_KEY"
50
  # Start the SSH service in the background
51
  service ssh start
52
+ elif [[ $SSH_KEY ]]; then
53
  # latitude.sh
54
+ add_keys_to_authorized "$SSH_KEY"
 
 
 
55
  # Start the SSH service in the background
56
  service ssh start
57
  else