/
test_ssh_config.py
221 lines (153 loc) · 6.7 KB
/
test_ssh_config.py
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Standard Library
import io
import sys
from unittest import mock
# External Dependencies
import paramiko
import pytest
from exec_helpers import _ssh_helpers as ssh_helpers
HOST = "127.128.0.1"
PORT = 22
USER = "user"
IDENTIFY_FILES = ["/tmp/ssh/id_dsa", "/tmp/ssh/id_rsa", "/tmp/ssh/id_ecdsa", "/tmp/ssh/id_ed25519"]
PROXY_JUMP_1 = "127.127.0.1"
PROXY_JUMP_2 = "127.0.0.1"
SYSTEM_REAL_SSH_CONFIG = """
Host *
SendEnv LANG LC_*
HashKnownHosts yes
GSSAPIAuthentication yes
"""
SSH_CONFIG_ALL_NO_PROXY = f"""
Host 127.128.*.*
Port {PORT}
User {USER}
IdentityFile {IDENTIFY_FILES[0]}
IdentityFile {IDENTIFY_FILES[1]}
IdentityFile {IDENTIFY_FILES[2]}
IdentityFile {IDENTIFY_FILES[3]}
ControlPath ~/.ssh/.control-%r@%h:%p
ControlMaster auto
Compression yes
"""
SSH_CONFIG_PROXY_COMMAND = """
Host 127.128.*.*
ProxyCommand ssh -q -A 127.127.0.1 nc %h %p
"""
SSH_CONFIG_PROXY_JUMP = f"""
Host 127.128.*.*
ProxyJump {PROXY_JUMP_1}
"""
SSH_CONFIG_MULTI_PROXY_JUMP = f"""
Host 127.128.*.*
ProxyJump {PROXY_JUMP_1}
Host {PROXY_JUMP_1}
ProxyJump {PROXY_JUMP_2}
"""
SSH_CONFIG_OVERRIDE_HOSTNAME = f"""
HOST {PROXY_JUMP_1}
Hostname {PROXY_JUMP_2}
"""
@pytest.fixture
def no_system_ssh_config(mocker):
conf_sys: mock.MagicMock = mocker.patch("exec_helpers._ssh_helpers.SSH_CONFIG_FILE_SYSTEM", autospec=True)
conf_sys.exists.return_value = False
@pytest.fixture
def no_user_ssh_config(mocker):
conf_user: mock.MagicMock = mocker.patch("exec_helpers._ssh_helpers.SSH_CONFIG_FILE_USER", autospec=True)
conf_user.exists.return_value = False
@pytest.fixture
def system_ssh_config(mocker) -> mock.MagicMock:
conf_sys: mock.MagicMock = mocker.patch("exec_helpers._ssh_helpers.SSH_CONFIG_FILE_SYSTEM", autospec=True)
conf_sys.exists.return_value = True
return conf_sys.open
@pytest.fixture
def user_ssh_config(mocker) -> mock.MagicMock:
conf_sys: mock.MagicMock = mocker.patch("exec_helpers._ssh_helpers.SSH_CONFIG_FILE_USER", autospec=True)
conf_sys.exists.return_value = True
return conf_sys.open
def test_no_configs(no_system_ssh_config, no_user_ssh_config):
config = ssh_helpers.parse_ssh_config(None, HOST)
assert config == {HOST: {"hostname": HOST}}
host_config = config[HOST]
assert host_config == ssh_helpers.SSHConfig(hostname=HOST)
assert host_config != object()
assert host_config.port is None
assert host_config.user is None
assert host_config.identityfile == ()
assert host_config.proxycommand is None
assert host_config.proxyjump is None
assert host_config.controlpath is None
assert host_config.controlmaster is None
@pytest.mark.xfail(sys.version_info[:2] == (3, 6), reason="Patching of config file is not functional")
def test_simple_config(system_ssh_config, user_ssh_config):
mock.mock_open(system_ssh_config, read_data=SYSTEM_REAL_SSH_CONFIG)
mock.mock_open(user_ssh_config, SSH_CONFIG_ALL_NO_PROXY)
config = ssh_helpers.parse_ssh_config(None, HOST)
host_config = config[HOST]
assert host_config.hostname == HOST
assert host_config.port == PORT
assert host_config.user == USER
assert host_config.identityfile == tuple(IDENTIFY_FILES)
assert host_config.controlpath == f"~/.ssh/.control-{USER}@{HOST}:{PORT}"
assert not host_config.controlmaster # auto => False
@pytest.mark.xfail(sys.version_info[:2] == (3, 6), reason="Patching of config file is not functional")
def test_simple_override_proxy_command(system_ssh_config, user_ssh_config):
mock.mock_open(system_ssh_config, SSH_CONFIG_ALL_NO_PROXY)
mock.mock_open(user_ssh_config, SSH_CONFIG_PROXY_COMMAND)
config = ssh_helpers.parse_ssh_config(None, HOST)
host_config = config[HOST]
assert host_config.hostname == HOST
assert host_config.proxycommand == f"ssh -q -A {PROXY_JUMP_1} nc {HOST} {PORT}"
assert host_config.as_dict == host_config
assert ssh_helpers.SSHConfig.from_ssh_config(host_config.as_dict) == host_config
@pytest.mark.xfail(sys.version_info[:2] == (3, 6), reason="Patching of config file is not functional")
def test_simple_override_single_proxy_jump(system_ssh_config, user_ssh_config):
mock.mock_open(system_ssh_config, SSH_CONFIG_ALL_NO_PROXY)
mock.mock_open(user_ssh_config, SSH_CONFIG_PROXY_JUMP)
config = ssh_helpers.parse_ssh_config(None, HOST)
host_config = config[HOST]
assert host_config.hostname == HOST
assert host_config.proxycommand is None
assert host_config.proxyjump == PROXY_JUMP_1
assert PROXY_JUMP_1 in config
assert config[PROXY_JUMP_1].hostname == PROXY_JUMP_1
assert host_config.as_dict == host_config
assert ssh_helpers.SSHConfig.from_ssh_config(host_config.as_dict) == host_config
@pytest.mark.xfail(sys.version_info[:2] == (3, 6), reason="Patching of config file is not functional")
def test_simple_override_chain_proxy_jump(system_ssh_config, user_ssh_config):
mock.mock_open(system_ssh_config, SSH_CONFIG_ALL_NO_PROXY)
mock.mock_open(user_ssh_config, SSH_CONFIG_MULTI_PROXY_JUMP)
config = ssh_helpers.parse_ssh_config(None, HOST)
host_config = config[HOST]
assert host_config.hostname == HOST
assert host_config.proxycommand is None
assert host_config.proxyjump == PROXY_JUMP_1
assert PROXY_JUMP_1 in config
assert config[PROXY_JUMP_1].hostname == PROXY_JUMP_1
assert config[PROXY_JUMP_1].proxyjump == PROXY_JUMP_2
assert PROXY_JUMP_2 in config
assert config[PROXY_JUMP_2].hostname == PROXY_JUMP_2
assert config[PROXY_JUMP_2].proxyjump is None
# Rebuild possibility even with chains
config_as_dict = {host: conf.as_dict for host, conf in config.items()}
assert ssh_helpers.parse_ssh_config(config_as_dict, HOST) == config
def test_simple_override_hostname(no_system_ssh_config, no_user_ssh_config):
paramiko_config = paramiko.SSHConfig()
paramiko_config.parse(io.StringIO(SSH_CONFIG_OVERRIDE_HOSTNAME))
config = ssh_helpers.parse_ssh_config(paramiko_config, PROXY_JUMP_1)
assert PROXY_JUMP_1 in config
assert config[PROXY_JUMP_1].hostname == PROXY_JUMP_2
assert PROXY_JUMP_2 in config
assert config[PROXY_JUMP_1] == config[PROXY_JUMP_2]
def test_negative(no_system_ssh_config, no_user_ssh_config):
with pytest.raises(ValueError):
ssh_helpers.SSHConfig(HOST, port=0)
with pytest.raises(ValueError):
ssh_helpers.SSHConfig(HOST, port=65536)
with pytest.raises(ValueError):
ssh_helpers.SSHConfig(HOST, proxycommand=f"ssh -q -A {PROXY_JUMP_1} nc {HOST} {PORT}", proxyjump=PROXY_JUMP_1)
def test_negative_read(system_ssh_config, no_user_ssh_config):
system_ssh_config.side_effect = RuntimeError()
config = ssh_helpers.parse_ssh_config(None, HOST)
assert config == {HOST: {"hostname": HOST}}