Skip to content

Commit 47953a1

Browse files
Improved proxy logging and timeout logic
1 parent a0aef5b commit 47953a1

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

sagemaker_ssh_helper/proxy.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def run_command(self, command):
9999

100100
def run_command_with_output(self, command):
101101
self.logger.info(f"Running command and capturing output: '{command}'")
102-
self._wait_for_tcp_port()
102+
self._wait_for_tcp_port(timeout=90)
103103

104104
try:
105105
# Pre-fetching the key to avoid the 'Warning: Permanently added ... to the list of known hosts' in output
@@ -149,14 +149,18 @@ def fetch_proxy_output(self):
149149

150150
def _wait_for_tcp_port(self, timeout=45):
151151
# Use 127.0.0.1 here to avoid AF_INET6 resolution that can give errors
152-
self.logger.info(f"Connecting to 127.0.0.1:{self.ssh_listen_port}")
152+
self.logger.info(f"Waiting for connection to become available on 127.0.0.1:{self.ssh_listen_port}")
153+
is_timeout = True
153154
for i in range(0, timeout):
154155
try:
155156
with socket.create_connection(("127.0.0.1", self.ssh_listen_port), 2):
157+
is_timeout = False
156158
self.logger.info(f"Connection to 127.0.0.1:{self.ssh_listen_port} is successful")
157159
break
158160
except ConnectionRefusedError:
159161
time.sleep(1)
162+
if is_timeout:
163+
self.logger.warning(f"Timeout waiting for connection on 127.0.0.1:{self.ssh_listen_port}")
160164

161165
def disconnect(self):
162166
self.logger.info(f"Disconnecting proxy and stopping SSH port forwarding")

sagemaker_ssh_helper/sm-connect-ssh-proxy

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
set -e
1111

1212
INSTANCE_ID="$1"
13-
SSH_AUTHORIZED_KEYS="$2"
13+
SSH_AUTHORIZED_KEYS_PATH="$2"
1414
shift
1515
shift
16+
PORT_FWD_ARGS=$*
17+
18+
echo "$(date -Iseconds) sm-connect-ssh-proxy: Connecting to: $INSTANCE_ID"
19+
echo "$(date -Iseconds) sm-connect-ssh-proxy: Extra args: $PORT_FWD_ARGS"
1620

1721
instance_status=$(aws ssm describe-instance-information --filters Key=InstanceIds,Values="$INSTANCE_ID" --query 'InstanceInformationList[0].PingStatus' --output text)
1822

@@ -26,16 +30,14 @@ fi
2630
# TODO: make it possible to override the default (also helps avoid race conditions)
2731
SSH_KEY=~/.ssh/sagemaker-ssh-gw
2832

29-
echo "Generating $SSH_KEY keypair with ECDSA and uploading public key to $SSH_AUTHORIZED_KEYS"
33+
echo "Generating $SSH_KEY keypair with ECDSA and uploading public key to $SSH_AUTHORIZED_KEYS_PATH"
3034

3135
echo 'yes' | ssh-keygen -t ecdsa -q -f "${SSH_KEY}" -N '' >/dev/null
32-
aws s3 cp "${SSH_KEY}.pub" "${SSH_AUTHORIZED_KEYS}"
36+
aws s3 cp "${SSH_KEY}.pub" "${SSH_AUTHORIZED_KEYS_PATH}"
3337

3438
CURRENT_REGION=$(aws configure list | grep region | awk '{print $2}')
3539
echo "Will use AWS Region: $CURRENT_REGION"
3640

37-
PORT_FWD_ARGS=$*
38-
3941
AWS_CLI_VERSION=$(aws --version)
4042
echo "AWS CLI version (should be v2): $AWS_CLI_VERSION"
4143

@@ -50,7 +52,7 @@ send_command=$(aws ssm send-command \
5052
--timeout-seconds 30 \
5153
--parameters "commands=[
5254
'mkdir -p /etc/ssh/authorized_keys.d/',
53-
'aws s3 cp --recursive \"${SSH_AUTHORIZED_KEYS}\" /etc/ssh/authorized_keys.d/',
55+
'aws s3 cp --recursive \"${SSH_AUTHORIZED_KEYS_PATH}\" /etc/ssh/authorized_keys.d/',
5456
'ls -la /etc/ssh/authorized_keys.d/',
5557
'cat /etc/ssh/authorized_keys.d/* > /etc/ssh/authorized_keys',
5658
'ls -la /etc/ssh/authorized_keys'
@@ -96,8 +98,7 @@ if [[ "$command_status" != "Success" ]]; then
9698
exit 2
9799
fi
98100

99-
echo "Connecting to $INSTANCE_ID with SSM and starting SSH port forwarding with the args: $PORT_FWD_ARGS"
100-
# TODO: remove duplicating message from SSMProxy
101+
echo "$(date -Iseconds) sm-connect-ssh-proxy: Starting SSH over SSM proxy"
101102

102103
# We don't use AWS-StartPortForwardingSession feature of SSM here, because we need port forwarding in both directions
103104
# with -L and -R parameters of SSH. This is useful for forwarding the PyCharm license server, which needs -R option.

0 commit comments

Comments
 (0)