forked from dbcli/pgcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ssh_tunnel.py
More file actions
206 lines (162 loc) · 7.63 KB
/
Copy pathtest_ssh_tunnel.py
File metadata and controls
206 lines (162 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os
from unittest.mock import patch, MagicMock, ANY
import pytest
from configobj import ConfigObj
from click.testing import CliRunner
from sshtunnel import SSHTunnelForwarder
from pgcli.main import cli, PGCli
from pgcli.pgexecute import PGExecute
@pytest.fixture
def mock_ssh_tunnel_forwarder() -> MagicMock:
mock_ssh_tunnel_forwarder = MagicMock(SSHTunnelForwarder, local_bind_ports=[1111], autospec=True)
with patch(
"pgcli.main.sshtunnel.SSHTunnelForwarder",
return_value=mock_ssh_tunnel_forwarder,
) as mock:
yield mock
@pytest.fixture
def mock_pgexecute() -> MagicMock:
with patch.object(PGExecute, "__init__", return_value=None) as mock_pgexecute:
yield mock_pgexecute
def test_ssh_tunnel(mock_ssh_tunnel_forwarder: MagicMock, mock_pgexecute: MagicMock) -> None:
# Test with just a host
tunnel_url = "some.host"
db_params = {
"database": "dbname",
"host": "db.host",
"user": "db_user",
"passwd": "db_passwd",
}
expected_tunnel_params = {
"local_bind_address": ("127.0.0.1",),
"remote_bind_address": (db_params["host"], 5432),
"ssh_address_or_host": (tunnel_url, 22),
"logger": ANY,
}
pgcli = PGCli(ssh_tunnel_url=tunnel_url)
pgcli.connect(**db_params)
mock_ssh_tunnel_forwarder.assert_called_once_with(**expected_tunnel_params)
mock_ssh_tunnel_forwarder.return_value.start.assert_called_once()
mock_pgexecute.assert_called_once()
call_args, call_kwargs = mock_pgexecute.call_args
# Original host is preserved for .pgpass lookup, hostaddr has tunnel endpoint
assert call_args[0] == db_params["database"]
assert call_args[1] == db_params["user"]
assert call_args[2] == db_params["passwd"]
assert call_args[3] == db_params["host"] # original host preserved
assert call_args[4] == pgcli.ssh_tunnel.local_bind_ports[0]
assert call_kwargs.get("hostaddr") == "127.0.0.1"
mock_ssh_tunnel_forwarder.reset_mock()
mock_pgexecute.reset_mock()
# Test with a full url and with a specific db port
tunnel_user = "tunnel_user"
tunnel_passwd = "tunnel_pass"
tunnel_host = "some.other.host"
tunnel_port = 1022
tunnel_url = f"ssh://{tunnel_user}:{tunnel_passwd}@{tunnel_host}:{tunnel_port}"
db_params["port"] = 1234
expected_tunnel_params["remote_bind_address"] = (
db_params["host"],
db_params["port"],
)
expected_tunnel_params["ssh_address_or_host"] = (tunnel_host, tunnel_port)
expected_tunnel_params["ssh_username"] = tunnel_user
expected_tunnel_params["ssh_password"] = tunnel_passwd
pgcli = PGCli(ssh_tunnel_url=tunnel_url)
pgcli.connect(**db_params)
mock_ssh_tunnel_forwarder.assert_called_once_with(**expected_tunnel_params)
mock_ssh_tunnel_forwarder.return_value.start.assert_called_once()
mock_pgexecute.assert_called_once()
call_args, call_kwargs = mock_pgexecute.call_args
assert call_args[3] == db_params["host"] # original host preserved
assert call_args[4] == pgcli.ssh_tunnel.local_bind_ports[0]
assert call_kwargs.get("hostaddr") == "127.0.0.1"
mock_ssh_tunnel_forwarder.reset_mock()
mock_pgexecute.reset_mock()
# Test with DSN
dsn = f"user={db_params['user']} password={db_params['passwd']} host={db_params['host']} port={db_params['port']}"
pgcli = PGCli(ssh_tunnel_url=tunnel_url)
pgcli.connect(dsn=dsn)
mock_ssh_tunnel_forwarder.assert_called_once_with(**expected_tunnel_params)
mock_pgexecute.assert_called_once()
call_args, call_kwargs = mock_pgexecute.call_args
# DSN should contain original host AND hostaddr for tunnel
dsn_arg = call_args[5]
assert f"host={db_params['host']}" in dsn_arg
assert "hostaddr=127.0.0.1" in dsn_arg
assert f"port={pgcli.ssh_tunnel.local_bind_ports[0]}" in dsn_arg
def test_ssh_tunnel_preserves_original_host_for_pgpass(mock_ssh_tunnel_forwarder: MagicMock, mock_pgexecute: MagicMock) -> None:
"""Verify that the original hostname is preserved for .pgpass lookup."""
tunnel_url = "bastion.example.com"
original_host = "production.db.example.com"
pgcli = PGCli(ssh_tunnel_url=tunnel_url)
pgcli.connect(database="mydb", host=original_host, user="dbuser", passwd="dbpass")
call_args, call_kwargs = mock_pgexecute.call_args
assert call_args[3] == original_host # host preserved
assert call_kwargs.get("hostaddr") == "127.0.0.1" # tunnel endpoint
def test_ssh_tunnel_with_dsn_preserves_host(mock_ssh_tunnel_forwarder: MagicMock, mock_pgexecute: MagicMock) -> None:
"""DSN connections should include hostaddr for tunnel while preserving host."""
tunnel_url = "bastion.example.com"
dsn = "host=production.db.example.com port=5432 dbname=mydb user=dbuser"
pgcli = PGCli(ssh_tunnel_url=tunnel_url)
pgcli.connect(dsn=dsn)
call_args, call_kwargs = mock_pgexecute.call_args
dsn_arg = call_args[5]
assert "host=production.db.example.com" in dsn_arg
assert "hostaddr=127.0.0.1" in dsn_arg
def test_no_ssh_tunnel_does_not_set_hostaddr(mock_pgexecute: MagicMock) -> None:
"""Without SSH tunnel, hostaddr should not be set."""
pgcli = PGCli()
pgcli.connect(database="mydb", host="localhost", user="user", passwd="pass")
call_args, call_kwargs = mock_pgexecute.call_args
assert "hostaddr" not in call_kwargs
def test_cli_with_tunnel() -> None:
runner = CliRunner()
tunnel_url = "mytunnel"
with patch.object(PGCli, "__init__", autospec=True, return_value=None) as mock_pgcli:
runner.invoke(cli, ["--ssh-tunnel", tunnel_url])
mock_pgcli.assert_called_once()
call_args, call_kwargs = mock_pgcli.call_args
assert call_kwargs["ssh_tunnel_url"] == tunnel_url
def test_config(tmpdir: os.PathLike, mock_ssh_tunnel_forwarder: MagicMock, mock_pgexecute: MagicMock) -> None:
pgclirc = str(tmpdir.join("rcfile"))
tunnel_user = "tunnel_user"
tunnel_passwd = "tunnel_pass"
tunnel_host = "tunnel.host"
tunnel_port = 1022
tunnel_url = f"{tunnel_user}:{tunnel_passwd}@{tunnel_host}:{tunnel_port}"
tunnel2_url = "tunnel2.host"
config = ConfigObj()
config.filename = pgclirc
config["ssh tunnels"] = {}
config["ssh tunnels"][r"\.com$"] = tunnel_url
config["ssh tunnels"][r"^hello-"] = tunnel2_url
config.write()
# Unmatched host
pgcli = PGCli(pgclirc_file=pgclirc)
pgcli.connect(host="unmatched.host")
mock_ssh_tunnel_forwarder.assert_not_called()
# Host matching first tunnel
pgcli = PGCli(pgclirc_file=pgclirc)
pgcli.connect(host="matched.host.com")
mock_ssh_tunnel_forwarder.assert_called_once()
call_args, call_kwargs = mock_ssh_tunnel_forwarder.call_args
assert call_kwargs["ssh_address_or_host"] == (tunnel_host, tunnel_port)
assert call_kwargs["ssh_username"] == tunnel_user
assert call_kwargs["ssh_password"] == tunnel_passwd
mock_ssh_tunnel_forwarder.reset_mock()
# Host matching second tunnel
pgcli = PGCli(pgclirc_file=pgclirc)
pgcli.connect(host="hello-i-am-matched")
mock_ssh_tunnel_forwarder.assert_called_once()
call_args, call_kwargs = mock_ssh_tunnel_forwarder.call_args
assert call_kwargs["ssh_address_or_host"] == (tunnel2_url, 22)
mock_ssh_tunnel_forwarder.reset_mock()
# Host matching both tunnels (will use the first one matched)
pgcli = PGCli(pgclirc_file=pgclirc)
pgcli.connect(host="hello-i-am-matched.com")
mock_ssh_tunnel_forwarder.assert_called_once()
call_args, call_kwargs = mock_ssh_tunnel_forwarder.call_args
assert call_kwargs["ssh_address_or_host"] == (tunnel_host, tunnel_port)
assert call_kwargs["ssh_username"] == tunnel_user
assert call_kwargs["ssh_password"] == tunnel_passwd