Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 8777271

Browse files
committed
proxy authentication supporting for websocket (stream/ws_client.py), with unittest
1 parent 769bc57 commit 8777271

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

stream/ws_client.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -449,18 +449,24 @@ def create_websocket(configuration, url, headers=None):
449449
connect_opt = {
450450
'header': header
451451
}
452+
453+
if configuration.proxy or coniguration.proxy_headers:
454+
connect_opt = websocket_proxycare(connect_opt, configuration, url, headers)
455+
456+
websocket.connect(url, **connect_opt)
457+
return websocket
458+
459+
def websocket_proxycare(connect_opt, configuration, url, headers):
452460
if configuration.proxy:
453461
proxy_url = urlparse(configuration.proxy)
454462
connect_opt.update({'http_proxy_host': proxy_url.hostname, 'http_proxy_port': proxy_url.port})
455463
if configuration.proxy_headers:
456-
for key,value in configuration.proxy_headers.items():
457-
if key == 'proxy-authorization' and value.startswith('Basic'):
458-
b64value = value.split()[1]
459-
auth = b64decode(b64value).decode().split(':')
460-
connect_opt.update({'http_proxy_auth': (auth[0], auth[1]) })
461-
462-
websocket.connect(url, **connect_opt)
463-
return websocket
464+
for key,value in configuration.proxy_headers.items():
465+
if key == 'proxy-authorization' and value.startswith('Basic'):
466+
b64value = value.split()[1]
467+
auth = b64decode(b64value).decode().split(':')
468+
connect_opt.update({'http_proxy_auth': (auth[0], auth[1]) })
469+
return(connect_opt)
464470

465471

466472
def websocket_call(configuration, _method, url, **kwargs):

stream/ws_client_test.py

+29
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@
1515
import unittest
1616

1717
from .ws_client import get_websocket_url
18+
from .ws_client import websocket_proxycare
19+
from kubernetes.client.configuration import Configuration
1820

21+
try:
22+
import urllib3
23+
urllib3.disable_warnings()
24+
except ImportError:
25+
pass
26+
27+
def dictval(dict, key, default=None):
28+
try:
29+
val = dict[key]
30+
except KeyError:
31+
val = default
32+
return val
1933

2034
class WSClientTest(unittest.TestCase):
2135

@@ -32,6 +46,21 @@ def test_websocket_client(self):
3246
]:
3347
self.assertEqual(get_websocket_url(url), ws_url)
3448

49+
def test_websocket_proxycare(self):
50+
for proxy, idpass, expect_host, expect_port, expect_auth in [
51+
( None, None, None, None, None ),
52+
( 'http://proxy.example.com:8080/', None, 'proxy.example.com', 8080, None ),
53+
( 'http://proxy.example.com:8080/', 'user:pass', 'proxy.example.com', 8080, ('user','pass'))
54+
]:
55+
config = Configuration()
56+
if proxy is not None:
57+
setattr(config, 'proxy', proxy)
58+
if idpass is not None:
59+
setattr(config, 'proxy_headers', urllib3.util.make_headers(proxy_basic_auth=idpass))
60+
connect_opt = websocket_proxycare( {}, config, None, None)
61+
self.assertEqual( dictval(connect_opt,'http_proxy_host'), expect_host)
62+
self.assertEqual( dictval(connect_opt,'http_proxy_port'), expect_port)
63+
self.assertEqual( dictval(connect_opt,'http_proxy_auth'), expect_auth)
3564

3665
if __name__ == '__main__':
3766
unittest.main()

0 commit comments

Comments
 (0)