Webhook 模式

Webhook 是一种 HTTP 回调:即当某事发生时触发的 HTTP POST 请求;通过 HTTP POST 实现的简单事件通知。实现 Webhook 的 Web 应用程序将在特定事件发生时向某个 URL POST 消息。

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

配置文件格式

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

配置文件使用 kubeconfig 文件格式。文件中 "users" 指代 API 服务器 Webhook,"clusters" 指代远端服务。

使用 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 Server 会 POST 一个 JSON 序列化的 authorization.k8s.io/v1beta1 SubjectAccessReview 对象,其中描述了正在执行的操作。该对象包含描述试图发起请求的用户以及有关所访问资源或请求属性的详细信息。

请注意,Webhook API 对象与其他 Kubernetes API 对象遵循相同的版本兼容性规则。实现者应该了解 beta 对象的兼容性承诺可能较宽松,并应检查请求的 "apiVersion" 字段以确保正确反序列化。此外,API Server 必须启用 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 不允许或对该请求“没有意见”,但如果配置了其他授权者,它们将有机会允许该请求。如果没有其他授权者,或者它们都不允许该请求,则该请求被拒绝(forbidden)。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"
    ]
  }
}
特性状态: Kubernetes v1.32 [beta] (默认启用: true)

启用 AuthorizeWithSelectors 特性后,请求中的字段和标签选择器会被传递给授权 Webhook。如果需要,Webhook 可以根据限定范围的字段和标签选择器做出授权决策。

SubjectAccessReview API 文档提供了授权 Webhook 如何解释和处理这些字段的指南,特别是如何使用已解析的要求而非原始选择器字符串,以及如何安全地处理无法识别的操作符。

{
  "apiVersion": "authorization.k8s.io/v1beta1",
  "kind": "SubjectAccessReview",
  "spec": {
    "resourceAttributes": {
      "verb": "list",
      "group": "",
      "resource": "pods",
      "fieldSelector": {
        "requirements": [
          {"key":"spec.nodeName", "operator":"In", "values":["mynode"]}
        ]
      },
      "labelSelector": {
        "requirements": [
          {"key":"example.com/mykey", "operator":"In", "values":["myvalue"]}
        ]
      }
    },
    "user": "jane",
    "group": [
      "group1",
      "group2"
    ]
  }
}

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

欲了解更多信息,请参阅SubjectAccessReview API 文档webhook.go 实现

上次修改时间 2024 年 6 月 26 日下午 2:12 PST:KEP-4601: alpha docs (5dab30d474)