Skip to content

Commit 182a6ee

Browse files
authored
Update aws-ssm-ec2-proxy-command.sh
1 parent e13e3b5 commit 182a6ee

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

aws-ssm-ec2-proxy-command.sh

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#
2222
# Open SSH Connection
2323
# ssh <INSTANCE_USER>@<INSTANCE_ID>
24-
#
24+
#
2525
# Ensure AWS CLI environment variables are set properly
2626
# e.g. AWS_PROFILE='default' ssh ec2-user@i-xxxxxxxxxxxxxxxx
2727
#
@@ -52,13 +52,19 @@ aws ssm send-command \
5252
--document-name 'AWS-RunShellScript' \
5353
--comment "Add an SSH public key to authorized_keys for ${ssh_public_key_timeout} seconds" \
5454
--parameters commands="\"
55-
mkdir -p ~${ssh_user}/.ssh && cd ~${ssh_user}/.ssh || exit 1
56-
55+
ssh_user_home=\$(getent passwd '${ssh_user}' | cut -d: -f6)
56+
if [ -z \\\"\${ssh_user_home}\\\" ]
57+
then
58+
exit 1
59+
fi
60+
61+
mkdir -p \\\"\${ssh_user_home}/.ssh\\\" && cd \\\"\${ssh_user_home}/.ssh\\\" || exit 1
62+
5763
authorized_key='${ssh_public_key} ssm-session'
5864
echo \\\"\${authorized_key}\\\" >> authorized_keys
59-
65+
6066
sleep ${ssh_public_key_timeout}
61-
67+
6268
grep -v -F \\\"\${authorized_key}\\\" authorized_keys > .authorized_keys
6369
mv .authorized_keys authorized_keys
6470
\""

0 commit comments

Comments
 (0)