Skip to content

Commit 6f244f3

Browse files
committed
tools: Various fixes to set guest sshkeys script
The script would for example overwrite all existing keys in the authorized_keys file Some things in the bash script are also simplified
1 parent 37874a3 commit 6f244f3

1 file changed

Lines changed: 22 additions & 33 deletions

File tree

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/bash
22
#
33
# Init file for SSH Public Keys Download Client
44
#
@@ -12,9 +12,9 @@
1212
# to you under the Apache License, Version 2.0 (the
1313
# "License"); you may not use this file except in compliance
1414
# with the License. You may obtain a copy of the License at
15-
#
15+
#
1616
# http://www.apache.org/licenses/LICENSE-2.0
17-
#
17+
#
1818
# Unless required by applicable law or agreed to in writing,
1919
# software distributed under the License is distributed on an
2020
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -31,21 +31,17 @@ DHCP_FOLDERS="/var/lib/dhclient/* /var/lib/dhcp3/*"
3131
keys_received=0
3232
file_count=0
3333

34-
for DHCP_FILE in $DHCP_FOLDERS
35-
do
36-
if [ -f $DHCP_FILE ]
37-
then
34+
for DHCP_FILE in $DHCP_FOLDERS; do
35+
if [ -f $DHCP_FILE ]; then
3836
file_count=$((file_count+1))
3937
SSHKEY_SERVER_IP=$(grep dhcp-server-identifier $DHCP_FILE | tail -1 | awk '{print $NF}' | tr -d '\;')
4038

41-
if [ -n "$SSHKEY_SERVER_IP" ]
42-
then
39+
if [ -n "$SSHKEY_SERVER_IP" ]; then
4340
logger -t "cloud" "Sending request to ssh key server at $SSHKEY_SERVER_IP"
4441

45-
publickey=$(wget -t 3 -T 20 -O - http://$SSHKEY_SERVER_IP/latest/public-keys 2>/dev/null)
42+
publickey=$(wget -q -t 3 -T 20 -O - http://$SSHKEY_SERVER_IP/latest/public-keys)
4643

47-
if [ $? -eq 0 ]
48-
then
44+
if [ $? -eq 0 ]; then
4945
logger -t "cloud" "Got response from server at $SSHKEY_SERVER_IP"
5046
keys_received=1
5147
break
@@ -56,11 +52,10 @@ do
5652
fi
5753
done
5854

59-
if [ "$keys_received" == "0" ]
60-
then
55+
if [ "$keys_received" == "0" ]; then
6156
SSHKEY_SERVER_IP=$(nslookup data-server | grep Address |tr '\n' ' '| awk '{print $4}')
6257
logger -t "cloud" "Sending request to ssh key server at $SSHKEY_SERVER_IP"
63-
publickey=$(wget -t 3 -T 20 -O - http://data-server/latest/public-keys 2>/dev/null)
58+
publickey=$(wget -q -t 3 -T 20 -O - http://data-server/latest/public-keys)
6459
if [ $? -eq 0 ]
6560
then
6661
logger -t "cloud" "Got response from server at $SSHKEY_SERVER_IP"
@@ -70,39 +65,33 @@ then
7065
fi
7166
fi
7267

73-
# did we find the keys anywhere?
74-
if [ "$keys_received" == "0" ]
75-
then
68+
if [ "$keys_received" == "0" ]; then
7669
logger -t "cloud" "Failed to get ssh keys from any server"
7770
exit 1
7871
fi
7972

73+
if [ -z "$publickey" ]; then
74+
logger -t "cloud" "Did not receive any keys from any server"
75+
exit 1
76+
fi
8077

81-
82-
# set ssh public key
8378
homedir=$(grep ^$user /etc/passwd|awk -F ":" '{print $6}')
8479
sshdir=$homedir/.ssh
8580
authorized=$sshdir/authorized_keys
86-
restorecon=/sbin/restorecon
87-
8881

89-
if [ ! -e $sshdir ]
90-
then
82+
if [ ! -e $sshdir ]; then
9183
mkdir $sshdir
84+
chmod 700 $sshdir
9285
fi
9386

94-
if [ ! -e $authorized ]
95-
then
87+
if [ ! -e $authorized ]; then
9688
touch $authorized
89+
chmod 600 $authorized
9790
fi
9891

99-
cat $authorized|grep -v "$publickey" > $authorized
92+
cat $authorized|grep -v "$publickey"|tee $authorized > /dev/null
10093
echo "$publickey" >> $authorized
10194

102-
if [ -e $restorecon ]
103-
then
104-
$restorecon -R -v $sshdir
105-
fi
106-
107-
exit 0
95+
which restorecon && restorecon -R -v $sshdir
10896

97+
exit 0

0 commit comments

Comments
 (0)