Repository navigation
Negotiate WebSocket v5 in the async exec client - #2797
kubernetes-prow[bot] merged 3 commits into
Conversation
* fix: negotiate async WebSocket v5 with v4 fallback
|
can we have an e2e test? |
|
Yes, we have two, one sync, one async in the repo. /assign @aojea |
|
@brendandburns is it possible to have an e2e test that exercises the entire path end to end? |
|
@aojea e2e test added. |
| protocol_header = next( | ||
| (key for key in headers if key.lower() == 'sec-websocket-protocol'), | ||
| None, | ||
| ) | ||
| if protocol_header is None: | ||
| protocols = [V5_CHANNEL_PROTOCOL, V4_CHANNEL_PROTOCOL] | ||
| headers['sec-websocket-protocol'] = ','.join(protocols) | ||
| else: | ||
| protocols = [ | ||
| protocol.strip() | ||
| for protocol in headers[protocol_header].split(',') | ||
| if protocol.strip() | ||
| ] |
There was a problem hiding this comment.
Small thing here: if the caller passes a headers dict without sec-websocket-protocol, setting headers['sec-websocket-protocol'] mutates their dict in place. Making a shallow copy first avoids that. Also, if someone explicitly passes an empty or whitespace-only Sec-WebSocket-Protocol header, protocols ends up empty and aiohttp leaves the empty header on the wire instead of falling back or failing cleanly.
| data = msg.data | ||
| if len(data) > 1: | ||
| channel = data[0] if isinstance(data, bytes) else ord(data[0]) |
There was a problem hiding this comment.
We should check msg.type before touching msg.data. In aiohttp, async for msg in ws only stops on CLOSE/CLOSING/CLOSED, so a WSMsgType.ERROR message (for instance from a parser error or heartbeat timeout) still comes through here with msg.data set to the exception. Calling len(data) will mask the real failure with a TypeError instead of raising ws.exception() or msg.data. Non-data message types should be skipped.
| channel == CLOSE_CHANNEL | ||
| and ws.protocol == V5_CHANNEL_PROTOCOL | ||
| ): | ||
| closed_channel = ( | ||
| data[1] if isinstance(data, bytes) else ord(data[1]) | ||
| ) | ||
| closed_channels.add(closed_channel) | ||
| continue | ||
| if channel in closed_channels: |
There was a problem hiding this comment.
In v5, the close signal is strictly a 2-byte binary frame ([255, channel], matching wsstream/conn.go on the server side which checks len(data) != 2). Checking len(data) > 1 here will silently accept malformed frames like b'\xff\x01extra' as a valid close, and doing ord(data[0]) == CLOSE_CHANNEL on a text frame would treat a frame starting with 'ÿ' (U+00FF) as a close signal. We should restrict CLOSE_CHANNEL handling to WSMsgType.BINARY frames with len(data) == 2, and drop any other frame on CLOSE_CHANNEL.
| if isinstance(data, bytes): | ||
| data = data.decode('utf-8') | ||
| if data and channel in [STDOUT_CHANNEL, STDERR_CHANNEL]: | ||
| resp_all += data |
There was a problem hiding this comment.
Two issues with decoding here. First, data.decode('utf-8') runs before checking whether channel is stdout or stderr, so non-UTF-8 bytes on any ignored channel (including b'\xff\x80' on a v4 connection) will raise UnicodeDecodeError even though we were going to drop the frame.
Second, decoding each WebSocket frame independently with strict UTF-8 will fail whenever the server splits a multi-byte UTF-8 character across two frames, or if the command writes invalid UTF-8 bytes (the sync client uses 'replace' for that reason). Keeping a per-channel codecs.getincrementaldecoder('utf-8')('replace') for stdout and stderr handles interleaved frames and split characters cleanly, and we can flush a channel's decoder with final=True when its v5 close frame arrives (and flush any remaining open channels after the loop ends).
|
|
||
| class WsMock: | ||
| def __init__(self): | ||
| def __init__(self, messages=None, protocol=None): |
There was a problem hiding this comment.
WsMock currently yields WsResponse objects without a type attribute instead of aiohttp.WSMessage, which is why the WSMsgType.ERROR case wasn't caught. Updating the mock to emit WSMessage and adding unit tests for v4 ignoring b'\xff\x01' / b'\xff\x80', malformed close frames (b'\xff', b'\xff\x01\x02'), 1-byte empty frames like b'\x01', split multi-byte UTF-8 across frames (including flushing on v5 close), and WSMsgType.ERROR would lock down all of these edge cases.
| 'value=$(cat); printf "received:%s" "$value"; ' | ||
| 'printf error-output >&2'], |
There was a problem hiding this comment.
Nit: assertIn('stdout', result) and assertIn('stderr', result) would still pass if stray channel bytes or duplicated output leaked into the buffer. Asserting len(result) == len('stdout') + len('stderr') alongside those two checks (or splitting into single-stream checks) would keep it order-independent while still catching extra bytes.
|
some comments but almost there |
|
@aojea I believe all comments are addressed, please take another look. |
Address review feedback on protocol headers, message errors, strict v5 channel closes, incremental decoding, and regression coverage.
f96bc84 to
02d05d9
Compare
|
/lgtm Thanks |
|
@aojea: changing LGTM is restricted to collaborators DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
/lgtm |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: aojea, brendandburns, roycaihw The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
be35e0e
into
kubernetes-client:master
Async exec offered only v4.channel.k8s.io, causing upgrade failures with servers requiring v5. The client now offers v5 with v4 fallback and handles v5 channel-close frames in buffered responses.
Negotiation: Use aiohttp’s subprotocol negotiation and preserve explicitly configured subprotocol headers.
Sec-WebSocket-Protocol: v5.channel.k8s.io,v4.channel.k8s.io
Channel closure: Buffered responses stop collecting data from a channel after its v5 close frame while continuing to collect other channels.
Raw streams: Return the unprocessed aiohttp WebSocket; callers remain responsible for channel framing and close messages.
Fixes: #2355