Skip to content

Commit d3fd37b

Browse files
author
Cornel-Cristian Cruceru
committed
fix SSL through SSH jump
1 parent 640f174 commit d3fd37b

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

changelog.md

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
Upcoming Release (TBD)
2+
======================
3+
4+
Bug Fixes:
5+
----------
6+
7+
* fix SSL through SSH jump host by using a true python socket for a tunnel
8+
9+
Internal:
10+
---------
11+
12+
Features:
13+
---------
14+
15+
116
1.28.0 (2024/11/10)
217
======================
318

mycli/AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Contributors:
9898
* Houston Wong
9999
* Mohamed Rezk
100100
* Ryosuke Kazami
101+
* Cornel Cruceru
101102

102103

103104
Created by:

mycli/packages/paramiko_stub/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ def __getattr__(self, name):
1313
import sys
1414
from textwrap import dedent
1515
print(dedent("""
16-
To enable certain SSH features you need to install paramiko:
16+
To enable certain SSH features you need to install paramiko and sshtunnel:
1717
18-
pip install paramiko
18+
pip install paramiko sshtunnel
1919
2020
It is required for the following configuration options:
2121
--list-ssh-config

mycli/sqlexecute.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
decoders)
1111
try:
1212
import paramiko
13+
import sshtunnel
1314
except ImportError:
1415
from mycli.packages.paramiko_stub import paramiko
1516

@@ -189,19 +190,24 @@ def connect(self, database=None, user=None, password=None, host=None,
189190
)
190191

191192
if ssh_host:
192-
client = paramiko.SSHClient()
193-
client.load_system_host_keys()
194-
client.set_missing_host_key_policy(paramiko.WarningPolicy())
195-
client.connect(
196-
ssh_host, ssh_port, ssh_user, ssh_password,
197-
key_filename=ssh_key_filename
198-
)
199-
chan = client.get_transport().open_channel(
200-
'direct-tcpip',
201-
(host, port),
202-
('0.0.0.0', 0),
203-
)
204-
conn.connect(chan)
193+
##### paramiko.Channel is a bad socket implementation overall if you want SSL through an SSH tunnel
194+
#####
195+
# instead let's open a tunnel and rewrite host:port to local bind
196+
try:
197+
chan = sshtunnel.SSHTunnelForwarder(
198+
(ssh_host, ssh_port),
199+
ssh_username=ssh_user,
200+
ssh_pkey=ssh_key_filename,
201+
ssh_password=ssh_password,
202+
remote_bind_address=(host, port)
203+
)
204+
chan.start()
205+
206+
conn.host=chan.local_bind_host
207+
conn.port=chan.local_bind_port
208+
conn.connect()
209+
except Exception as e:
210+
raise e
205211

206212
if hasattr(self, 'conn'):
207213
self.conn.close()

requirements-dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ colorama>=0.4.1
1010
git+https://github.com/hayd/pep8radius.git # --error-status option not released
1111
click>=7.0
1212
paramiko==2.11.0
13+
sshtunnel==0.4.0
1314
pyperclip>=1.8.1
1415
importlib_resources>=5.0.0
1516
pyaes>=1.6.1

0 commit comments

Comments
 (0)