manila-image-elements/elements/manila-ssh/install.d/fetch-public-ssh-keys.sh
Tom Barron 53b7969bb5 Set .ssh permissions correctly
Owner was being set to manila but group was neglected.

Closes-Bug: #1480870
Closes-Bug: #1646135
Change-Id: I9bc4197221840b33ff5ea56a69d1dd282ee9003b
2018-12-31 02:14:52 +00:00

41 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
set -x
MANILA_USER="manila"
MANILA_USER_HOME="/home/$MANILA_USER"
MANILA_USER_SSH_DIR="$MANILA_USER_HOME/.ssh"
if [ ! -d $MANILA_USER_SSH_DIR ]; then
mkdir -p $MANILA_USER_SSH_DIR
chmod 700 $MANILA_USER_SSH_DIR
chown -R $MANILA_USER:$MANILA_USER $MANILA_USER_SSH_DIR
fi
# Fetch public key using HTTP
ATTEMPTS=60 # Wait up to 5 minutes
FAILED=0
while [ ! -f $MANILA_USER_SSH_DIR/authorized_keys ]; do
curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /tmp/metadata-key 2>/dev/null
if [ $? -eq 0 ]; then
cat /tmp/metadata-key >> $MANILA_USER_SSH_DIR/authorized_keys
chmod 0600 $MANILA_USER_SSH_DIR/authorized_keys
chown $MANILA_USER $MANILA_USER_SSH_DIR/authorized_keys
rm -f /tmp/metadata-key
echo "Successfully retrieved public key from instance metadata"
echo "*****************"
echo "AUTHORIZED KEYS"
echo "*****************"
cat $MANILA_USER_SSH_DIR/authorized_keys
echo "*****************"
else
FAILED=`expr $FAILED + 1`
if [ $FAILED -ge $ATTEMPTS ]; then
echo "Failed to retrieve public key from instance metadata after $FAILED attempts, quitting"
exit 1
fi
echo "Could not retrieve public key from instance metadata (attempt #$FAILED/$ATTEMPTS), retrying in 5 seconds..."
sleep 5
fi
done