-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathGSRequestTest.py
More file actions
216 lines (170 loc) · 7.91 KB
/
Copy pathGSRequestTest.py
File metadata and controls
216 lines (170 loc) · 7.91 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
207
208
209
210
211
212
213
214
215
216
import unittest
import ssl
import socket
from unittest.mock import MagicMock, patch
from GSSDK import GSRequest, ValidHTTPSConnection, SSLUtils
class TestResolveMtlsDomain(unittest.TestCase):
def _make_request(self, api_domain=None):
request = GSRequest(
apiKey="test_key",
secretKey="test_secret",
apiMethod="accounts.getAccountInfo",
useHTTPS=True,
certFile="cert.pem",
keyFile="key.pem"
)
if api_domain:
request.setAPIDomain(api_domain)
return request
def test_default_domain(self):
request = self._make_request()
self.assertEqual(request._resolve_mtls_domain(), "mtls.us1.gigya.com")
def test_us1(self):
request = self._make_request("us1.gigya.com")
self.assertEqual(request._resolve_mtls_domain(), "mtls.us1.gigya.com")
def test_eu1(self):
request = self._make_request("eu1.gigya.com")
self.assertEqual(request._resolve_mtls_domain(), "mtls.eu1.gigya.com")
def test_eu2(self):
request = self._make_request("eu2.gigya.com")
self.assertEqual(request._resolve_mtls_domain(), "mtls.eu2.gigya.com")
def test_au1(self):
request = self._make_request("au1.gigya.com")
self.assertEqual(request._resolve_mtls_domain(), "mtls.au1.gigya.com")
def test_global(self):
request = self._make_request("global.gigya.com")
self.assertEqual(request._resolve_mtls_domain(), "mtls.global.gigya.com")
def test_staging_domain(self):
request = self._make_request("us1-st1.gigya.com")
self.assertEqual(request._resolve_mtls_domain(), "mtls.us1-st1.gigya.com")
def test_empty_string_resets_to_default(self):
"""setAPIDomain('') resets to default us1.gigya.com, so resolves to mtls.us1.gigya.com"""
request = self._make_request()
request.setAPIDomain("")
self.assertEqual(request._resolve_mtls_domain(), "mtls.us1.gigya.com")
def test_bare_domain(self):
"""bare domain (gigya.com) - first segment is gigya, so resolves to mtls.gigya.gigya.com"""
request = self._make_request("gigya.com")
self.assertEqual(request._resolve_mtls_domain(), "mtls.gigya.gigya.com")
def test_leading_dot(self):
"""leading dot (.gigya.com) - first segment is empty, fallback kicks in"""
request = self._make_request()
request._apiDomain = ".gigya.com"
self.assertEqual(request._resolve_mtls_domain(), "mtls.us1.gigya.com")
class TestHasMtlsConfig(unittest.TestCase):
def test_both_provided(self):
request = GSRequest(
apiKey=None, secretKey=None, apiMethod="accounts.getAccountInfo",
useHTTPS=True, certFile="cert.pem", keyFile="key.pem"
)
self.assertTrue(request._has_mtls_config())
def test_no_cert(self):
request = GSRequest(
apiKey="key", secretKey="secret", apiMethod="accounts.getAccountInfo",
useHTTPS=True, certFile=None, keyFile=None
)
self.assertFalse(request._has_mtls_config())
def test_only_cert_no_key(self):
request = GSRequest(
apiKey=None, secretKey=None, apiMethod="accounts.getAccountInfo",
useHTTPS=True, certFile="cert.pem", keyFile=None
)
self.assertFalse(request._has_mtls_config())
def test_only_key_no_cert(self):
request = GSRequest(
apiKey=None, secretKey=None, apiMethod="accounts.getAccountInfo",
useHTTPS=True, certFile=None, keyFile="key.pem"
)
self.assertFalse(request._has_mtls_config())
class TestSendDomainResolution(unittest.TestCase):
"""Test that send() sets _domain correctly for mTLS vs regular requests"""
def test_mtls_overrides_domain(self):
request = GSRequest(
apiKey=None, secretKey=None, apiMethod="accounts.getAccountInfo",
useHTTPS=True, certFile="cert.pem", keyFile="key.pem"
)
request.setAPIDomain("eu1.gigya.com")
# Call send — it will fail on the actual HTTP call, but domain is set before that
request.send(timeout=1)
self.assertEqual(request._domain, "mtls.eu1.gigya.com")
def test_regular_request_uses_namespace_domain(self):
request = GSRequest(
apiKey="key", secretKey="secret", apiMethod="accounts.getAccountInfo",
useHTTPS=True
)
request.setAPIDomain("eu1.gigya.com")
request.send(timeout=1)
self.assertEqual(request._domain, "accounts.eu1.gigya.com")
class TestValidHTTPSConnectionSNI(unittest.TestCase):
"""
Tests for the SNI fix in ValidHTTPSConnection.connect().
When routing through an HTTPS proxy (CONNECT tunnel), urllib sets:
self.host = proxy address
self._tunnel_host = real destination hostname
The fix ensures the real destination is used as SNI, not the proxy.
"""
def _make_connection(self, host, tunnel_host=None):
ctx = SSLUtils.createSSLContext(False, None, None)
conn = ValidHTTPSConnection(host, context=ctx)
conn._tunnel_host = tunnel_host
conn.timeout = 5
conn.source_address = None
return conn
def _mock_socket(self):
sock = MagicMock()
sock.setsockopt = MagicMock()
return sock
def test_sni_uses_tunnel_host_when_proxy_is_set(self):
"""When going through a proxy, SNI must be the destination host, not the proxy."""
conn = self._make_connection("127.0.0.1", tunnel_host="accounts.us1.gigya.com")
mock_sock = self._mock_socket()
captured_sni = []
def fake_wrap(sock, server_hostname=None):
captured_sni.append(server_hostname)
raise ConnectionAbortedError("stop after SNI capture")
conn._context.wrap_socket = fake_wrap
with patch("socket.create_connection", return_value=mock_sock):
with patch.object(conn, "_tunnel"):
try:
conn.connect()
except ConnectionAbortedError:
pass
self.assertEqual(captured_sni, ["accounts.us1.gigya.com"],
"SNI must be the tunnel destination, not the proxy address")
def test_sni_uses_host_when_no_proxy(self):
"""Without a proxy, SNI must be self.host (direct connection, no tunnel)."""
conn = self._make_connection("accounts.us1.gigya.com", tunnel_host=None)
mock_sock = self._mock_socket()
captured_sni = []
def fake_wrap(sock, server_hostname=None):
captured_sni.append(server_hostname)
raise ConnectionAbortedError("stop after SNI capture")
conn._context.wrap_socket = fake_wrap
with patch("socket.create_connection", return_value=mock_sock):
try:
conn.connect()
except ConnectionAbortedError:
pass
self.assertEqual(captured_sni, ["accounts.us1.gigya.com"],
"SNI must be self.host for direct connections")
def test_sni_not_proxy_ip(self):
"""Regression: the proxy IP must never appear as SNI."""
conn = self._make_connection("10.0.0.1", tunnel_host="accounts.eu1.gigya.com")
mock_sock = self._mock_socket()
captured_sni = []
def fake_wrap(sock, server_hostname=None):
captured_sni.append(server_hostname)
raise ConnectionAbortedError("stop after SNI capture")
conn._context.wrap_socket = fake_wrap
with patch("socket.create_connection", return_value=mock_sock):
with patch.object(conn, "_tunnel"):
try:
conn.connect()
except ConnectionAbortedError:
pass
self.assertNotEqual(captured_sni[0], "10.0.0.1",
"Proxy IP must never be sent as SNI")
self.assertEqual(captured_sni[0], "accounts.eu1.gigya.com",
"SNI must be the real destination hostname")
if __name__ == '__main__':
unittest.main()