Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions kubernetes/stream/ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,12 @@ def create_websocket(configuration, url, headers=None, _request_timeout=None):
# We just need to pass the Authorization, ignore all the other
# http headers we get from the generated code
header = []
if headers and 'authorization' in headers:
header.append("authorization: %s" % headers['authorization'])
if headers and 'sec-websocket-protocol' in headers:
lower_headers = {k.lower(): v for k, v in headers.items()} if headers else {}
if 'authorization' in lower_headers:
header.append("authorization: %s" % lower_headers['authorization'])
if 'sec-websocket-protocol' in lower_headers:
header.append("sec-websocket-protocol: %s" %
headers['sec-websocket-protocol'])
lower_headers['sec-websocket-protocol'])
else:
header.append("sec-websocket-protocol: %s,%s" % (V5_CHANNEL_PROTOCOL, V4_CHANNEL_PROTOCOL))

Expand Down Expand Up @@ -631,8 +632,8 @@ def websocket_proxycare(connect_opt, configuration, url, headers):
proxy_url = urlparse(configuration.proxy)
connect_opt.update({'http_proxy_host': proxy_url.hostname, 'http_proxy_port': proxy_url.port})
if configuration.proxy_headers:
for key,value in configuration.proxy_headers.items():
if key == 'proxy-authorization' and value.startswith('Basic'):
for key, value in configuration.proxy_headers.items():
if key.lower() == 'proxy-authorization' and value.startswith('Basic'):
parts = value.split()
if len(parts) < 2:
raise ApiValueError(
Expand Down
50 changes: 50 additions & 0 deletions kubernetes/stream/ws_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,56 @@ def test_create_websocket_header(self):
expected_header = f"sec-websocket-protocol: {V5_CHANNEL_PROTOCOL},{V4_CHANNEL_PROTOCOL}"
self.assertIn(expected_header, kwargs['header'])

def test_create_websocket_header_case_insensitive(self):
"""Verify Authorization and Sec-WebSocket-Protocol headers with custom casing are preserved."""
with patch.object(ws_client_module, 'WebSocket', autospec=True) as mock_ws_cls:
mock_ws = mock_ws_cls.return_value

headers = {
'Authorization': 'Bearer test-bearer-token',
'Sec-WebSocket-Protocol': 'v5.channel.k8s.io'
}
ws_client_module.create_websocket(
self.config_mock, "ws://test", headers=headers
)

mock_ws.connect.assert_called_once()
call_args = mock_ws.connect.call_args
kwargs = call_args[1]
self.assertIn('header', kwargs)
self.assertIn('authorization: Bearer test-bearer-token', kwargs['header'])
self.assertIn('sec-websocket-protocol: v5.channel.k8s.io', kwargs['header'])

def test_create_websocket_uppercase_authorization_header(self):
"""Verify all-caps AUTHORIZATION header is recognized."""
with patch.object(ws_client_module, 'WebSocket', autospec=True) as mock_ws_cls:
mock_ws = mock_ws_cls.return_value

headers = {
'AUTHORIZATION': 'Basic dXNlcjpwYXNz'
}
ws_client_module.create_websocket(
self.config_mock, "ws://test", headers=headers
)

mock_ws.connect.assert_called_once()
kwargs = mock_ws.connect.call_args[1]
self.assertIn('authorization: Basic dXNlcjpwYXNz', kwargs['header'])
expected_proto = f"sec-websocket-protocol: {V5_CHANNEL_PROTOCOL},{V4_CHANNEL_PROTOCOL}"
self.assertIn(expected_proto, kwargs['header'])

def test_websocket_proxycare_case_insensitive(self):
"""Verify proxy-authorization header casing in proxy_headers is handled."""
config = Configuration()
config.proxy = "http://proxy.example.com:8080"
config.proxy_headers = {
'Proxy-Authorization': 'Basic dXNlcjpwYXNzd29yZA=='
}
connect_opt = {}
result = websocket_proxycare(connect_opt, config, "ws://test", headers=None)
self.assertIn('http_proxy_auth', result)
self.assertEqual(result['http_proxy_auth'], ('user', 'password'))

def test_create_websocket_tuple_timeout(self):
"""Verify _request_timeout tuple sets connect timeout and
resets socket timeout to read timeout."""
Expand Down