将 kubectl 与 Kubernetes 授权模式 ABAC 结合使用

Using kubectl with Kubernetes authorization mode ABAC

我在 Ubuntu 上发送了一个 4 节点集群(1 master 3 worker)运行ning Kubernetes。我打开了 --authorization-mode=ABAC 并设置了一个包含如下条目的策略文件

{"user":"bob", "readonly": true, "namespace": "projectgino"}

我希望用户 bob 只能查看 projectgino 中的资源。我以用户 Bob 身份使用 kubectl 命令行时遇到问题。当我运行下面的命令

kubectl get pods --token=xxx --namespace=projectgino --server=https://xxx.xxx.xxx.xx:6443

我收到以下错误

error: couldn't read version from server: the server does not allow access to the requested resource

我跟踪了 kubectl 命令行代码,问题似乎是由 kubectl 在 pkg/client/helper.go 中调用函数 NegotiateVersion 引起的。这会在服务器上调用 /api 以获取 Kubernetes 的版本。此调用失败,因为其余路径不包含命名空间 projectgino。我向 pkg/auth/authorizer/abac/abac.go 添加了跟踪代码,但它在名称空间检查时失败了。

我还没有升级最新的 1.1.1 版本的 Kubernetes,但是查看代码我没有看到这方面有什么变化。

有人知道如何配置 Kubernetes 来解决这个问题吗?

这是 ABAC authorizer. The fix is in progress: #16148 中缺少的功能。

至于解决方法,来自 the authorization doc

For miscellaneous endpoints, like /version, the resource is the empty string.

所以你可以通过定义一个策略来解决:

{"user":"bob", "readonly": true, "resource": ""}

(注意资源的空字符串)授予对未版本化端点的访问权限。如果这不起作用,我不认为有一个干净的解决方法可以让你使用 kubectl 和 --authorization-mode=ABAC。