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

Commit 0052a68

Browse files
authored
Merge pull request #256 from itaru2622/proxy_auth
add proxy authentication supporting for websocket (stream/ws_client.py)
2 parents b0afc93 + 59e7d11 commit 0052a68

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

Diff for: stream/ws_client.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from six import StringIO
3030

3131
from websocket import WebSocket, ABNF, enableTrace
32+
from base64 import urlsafe_b64decode
3233

3334
STDIN_CHANNEL = 0
3435
STDOUT_CHANNEL = 1
@@ -445,12 +446,27 @@ def create_websocket(configuration, url, headers=None):
445446
ssl_opts['keyfile'] = configuration.key_file
446447

447448
websocket = WebSocket(sslopt=ssl_opts, skip_utf8_validation=False)
449+
connect_opt = {
450+
'header': header
451+
}
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):
448460
if configuration.proxy:
449461
proxy_url = urlparse(configuration.proxy)
450-
websocket.connect(url, header=header, http_proxy_host=proxy_url.hostname, http_proxy_port=proxy_url.port)
451-
else:
452-
websocket.connect(url, header=header)
453-
return websocket
462+
connect_opt.update({'http_proxy_host': proxy_url.hostname, 'http_proxy_port': proxy_url.port})
463+
if configuration.proxy_headers:
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 = urlsafe_b64decode(b64value).decode().split(':')
468+
connect_opt.update({'http_proxy_auth': (auth[0], auth[1]) })
469+
return(connect_opt)
454470

455471

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

Diff for: 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)