如何在 Websocket API 中传递 Authorization Bearer Token

ani*_*ish 8 xterm websocket kubernetes

我正在尝试使用 Websocket 执行 kubernetes pod,根据 kubernetes 文档,它可以通过传递承载 THETOKEN来实现

当使用来自 http 客户端的承载令牌身份验证时,API 服务器需要一个值为 Bearer THETOKEN 的 Authorization 标头

下面是wscat将 Header Value 传递--header "Authorization: Bearer $TOKEN"给 pod 建立 exec 并且连接成功的示例

/ # wscat  --header "Authorization: Bearer $TOKEN"  -c "wss://api.0cloud0.com/api/v1/namespaces/ba410a7474380169a5ae230d8e784535/pods/txaclqhshg
-6f69577c74-jxbwn/exec?stdin=1&stdout=1&stderr=1&tty=1&command=sh"
Run Code Online (Sandbox Code Playgroud)

但是当涉及到来自 Web 浏览器的Websocket API连接时

如何根据文档在 web Socket 中传递这个 Beaer Token 没有传递自定义标头的标准方法

在 API 查询中尝试了 URI 查询参数access_token= Bearer TOKEN它不起作用并且身份验证被 403 拒绝

wss://api.0cloud0.com/api/v1/namespaces/ba410a7474380169a5ae230d8e784535/pods/txaclqhshg-%206f69577c74-jxbwn/exec?stdout=1&stdin=1&stderr=1&tty=1&command=%2Fbin%2Fsh&command=-i&access_token=$TOKEN
Run Code Online (Sandbox Code Playgroud)

Tlm*_*aK0 1

我以前从未在 kubernetes 中使用过 websocket,但这里是有关 websocket 浏览器客户端的令牌身份验证方法的文档https://github.com/kubernetes/kubernetes/pull/47740

您必须在子协议参数中发送令牌,并使用 Base64 编码的令牌。

所以应该是:

wscat  -s "base64url.bearer.authorization.k8s.io.$TOKEN_IN_BASE64","base64.binary.k8s.io" -c "wss://api.0cloud0.com/api/v1/namespaces/ba410a7474380169a5ae230d8e784535/pods/txaclqhshg
-6f69577c74-jxbwn/exec?stdin=1&stdout=1&stderr=1&tty=1&command=sh"
Run Code Online (Sandbox Code Playgroud)