Webhook 模式

WebHook 是一个 HTTP 回调:当某些事情发生时发生的 HTTP POST;通过 HTTP POST 进行的简单事件通知。实现 WebHook 的 Web 应用程序将在某些事情发生时向 URL POST 一条消息。

指定时,模式 Webhook 会导致 Kubernetes 在确定用户权限时查询外部 REST 服务。

配置文件格式

模式 Webhook 需要一个用于 HTTP 配置的文件,通过 --authorization-webhook-config-file=SOME_FILENAME 标志指定。

配置文件使用 kubeconfig 文件格式。在文件中,“用户”是指 API 服务器 WebHook,“集群”是指远程服务。

使用 HTTPS 客户端身份验证的配置示例

# Kubernetes API version
apiVersion: v1
# kind of the API object
kind: Config
# clusters refers to the remote service.
clusters:
  - name: name-of-remote-authz-service
    cluster:
      # CA for verifying the remote service.
      certificate-authority: /path/to/ca.pem
      # URL of remote service to query. Must use 'https'. May not include parameters.
      server: https://authz.example.com/authorize

# users refers to the API Server's webhook configuration.
users:
  - name: name-of-api-server
    user:
      client-certificate: /path/to/cert.pem # cert for the webhook plugin to use
      client-key: /path/to/key.pem          # key matching the cert

# kubeconfig files require a context. Provide one for the API Server.
current-context: webhook
contexts:
- context:
    cluster: name-of-remote-authz-service
    user: name-of-api-server
  name: webhook

请求有效负载

在面临授权决策时,API 服务器会 POST 一个 JSON 序列化的 authorization.k8s.io/v1beta1 SubjectAccessReview 对象,描述该操作。此对象包含描述尝试发出请求的用户的字段,以及有关所访问资源的详细信息或请求属性。

请注意,Webhook API 对象遵循与其他 Kubernetes API 对象相同的 版本兼容性规则。实现者应了解 Beta 对象的兼容性承诺较弱,并检查请求的 “apiVersion” 字段以确保正确反序列化。此外,API 服务器必须启用 authorization.k8s.io/v1beta1 API 扩展组 (--runtime-config=authorization.k8s.io/v1beta1=true)。

示例请求正文

{
  "apiVersion": "authorization.k8s.io/v1beta1",
  "kind": "SubjectAccessReview",
  "spec": {
    "resourceAttributes": {
      "namespace": "kittensandponies",
      "verb": "get",
      "group": "unicorn.example.org",
      "resource": "pods"
    },
    "user": "jane",
    "group": [
      "group1",
      "group2"
    ]
  }
}

远程服务应填写请求的 status 字段,并响应允许或拒绝访问。响应正文的 spec 字段将被忽略,可以省略。允许响应将返回

{
  "apiVersion": "authorization.k8s.io/v1beta1",
  "kind": "SubjectAccessReview",
  "status": {
    "allowed": true
  }
}

对于拒绝访问,有两种方法。

在大多数情况下,首选第一种方法,表示授权 Webhook 不允许或对请求“没有意见”,但如果配置了其他授权者,则会给它们一个允许请求的机会。如果没有其他授权者,或者没有一个授权者允许请求,则禁止该请求。Webhook 将返回

{
  "apiVersion": "authorization.k8s.io/v1beta1",
  "kind": "SubjectAccessReview",
  "status": {
    "allowed": false,
    "reason": "user does not have read access to the namespace"
  }
}

第二种方法立即拒绝,短路其他已配置授权者的评估。这仅应由详细了解集群的完整授权者配置的 Webhook 使用。Webhook 将返回

{
  "apiVersion": "authorization.k8s.io/v1beta1",
  "kind": "SubjectAccessReview",
  "status": {
    "allowed": false,
    "denied": true,
    "reason": "user does not have read access to the namespace"
  }
}

对非资源路径的访问作为以下形式发送

{
  "apiVersion": "authorization.k8s.io/v1beta1",
  "kind": "SubjectAccessReview",
  "spec": {
    "nonResourceAttributes": {
      "path": "/debug",
      "verb": "get"
    },
    "user": "jane",
    "group": [
      "group1",
      "group2"
    ]
  }
}

非资源路径包括:/api/apis/metrics/logs/debug/healthz/livez/openapi/v2/readyz/version. 客户端需要访问 /api/api/*/apis/apis/*/version 以发现服务器上存在哪些资源和版本。可以拒绝访问其他非资源路径,而无需限制对 REST API 的访问。

有关进一步的文档,请参阅 authorization.v1beta1 API 对象和 webhook.go

上次修改时间:2024 年 2 月 18 日上午 10:07 PST:重新排序 authn/authz 页面 (9f327512c6)