本文档采用自动化机器翻译技术翻译。 尽管我们力求提供准确的译文,但不对翻译内容的完整性、准确性或可靠性作出任何保证。 若出现任何内容不一致情况,请以原始 英文 版本为准,且原始英文版本为权威文本。

这是尚未发布的文档。 Admission Controller 1.34-dev.

原始策略

v1.9.0 开始,SUSE Security Admission Controller 支持在 Kubernetes 集群外部编写和执行策略的能力,作为通用策略评估引擎。 策略服务器公开了 /validate_raw 端点,可用于验证任意 JSON 文档与 Admission Controller 策略的匹配。

在本指南中,我们将使用以下原始策略:

请确保策略作者在元数据中将策略标记为 policyType: raw。 您可以使用 kwctl 检查元数据。

kwctl inspect ghcr.io/kubewarden/tests/raw-mutation-policy:v0.1.0

在 Kubernetes 外部运行策略服务器

策略服务器可以作为独立容器在 Kubernetes 外部运行。

首先,创建一个 policies.yml 文件,并包含以下内容:

raw-validation:
  module: ghcr.io/kubewarden/tests/raw-validation-policy:v0.1.0
  settings:
    validUsers:
      - alice
      - bob
    validActions:
      - read
      - write
    validResources:
      - orders
      - products

raw-mutation:
  module: ghcr.io/kubewarden/tests/raw-mutation-policy:v0.1.0
  allowedToMutate: true
  settings:
    forbiddenResources:
      - privateResource
      - secretResource
    defaultResource: publicResource

要启动策略服务器:

# Create a docker volume to store the policies
docker volume create --driver local \
                --opt type=tmpfs \
                --opt device=tmpfs \
                --opt o=ui=65533 \
                policy-store

# Start the policy server
docker run --rm -it \
    -p 3000:3000 \
    -v $(pwd)/policies.yml:/policies.yml \
    -v policy-store:/registry \
    ghcr.io/kubewarden/policy-server:1.9.0 \
    --ignore-kubernetes-connection-failure=true
标志 --ignore-kubernetes-connection-failure=true 是在没有 Kubernetes 的情况下启动策略服务器所必需的。 但是,可以在 Kubernetes 中启动策略服务器并使用原始验证端点。 原始策略可以访问上下文感知的 capabilities,就像标准策略一样。

在 Kubernetes 中运行策略服务器而不使用 Admission Controller 控制器

无法使用由 Admission Controller 控制器管理的策略服务器实例来托管原始策略。 控制器将不允许用户更改策略服务器 ConfigMap 以添加原始策略,因为它会试图协调并还原更改。 因此,必须启动一个专用的策略服务器。

创建一个 policy-server.yaml 文件,并包含以下内容:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: policy-server-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: policy-server
  template:
    metadata:
      labels:
        app: policy-server
    spec:
      containers:
        - name: policy-server
          image: ghcr.io/kubewarden/policy-server:v1.9.0
          ports:
            - containerPort: 3000
          volumeMounts:
            - name: policy-store
              mountPath: /registry
            - name: policies-config
              mountPath: /policies.yml
              subPath: policies.yml
      volumes:
        - name: policy-store
          emptyDir: {}
        - name: policies-config
          configMap:
            name: policies-configmap
---
apiVersion: v1
kind: Service
metadata:
  name: policy-server-service
spec:
  selector:
    app: policy-server
  ports:
    - protocol: TCP
      port: 3000
      targetPort: 3000
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: policies-configmap
data:
  policies.yml: |
    raw-validation:
      module: ghcr.io/kubewarden/tests/raw-validation-policy:v0.1.0
      settings:
        validUsers:
          - alice
          - bob
        validActions:
          - read
          - write
        validResources:
          - orders
          - products
    raw-mutation:
      module: ghcr.io/kubewarden/tests/raw-mutation-policy:v0.1.0
      allowedToMutate: true
      settings:
        forbiddenResources:
          - privateResource
          - secretResource
        defaultResource: publicResource

应用配置:

kubectl apply -f policy-server.yaml

部署的策略服务器实例将能够访问可由上下文感知策略使用的 Kubernetes 资源。 对 Kubernetes 资源的访问级别由用于运行策略服务器工作负载的服务账户决定。

在前面的示例中,部署规范中未定义服务账户;因此将使用 default 服务账户。

使用 validate_raw 端点

Validation

原始验证端点在 /validate_raw 处公开,并接受 POST 请求。 由于我们已经部署了一个服务,我们可以设置端口转发以通过 kubectl port-forward service/policy-server-service 3000:3000 -n default 访问它。

让我们尝试根据 raw-validation 策略验证一个 JSON 文档:

curl -X POST \
  http://localhost:3000/validate_raw/raw-validation \
  -H 'Content-Type: application/json' \
  -d '{
  "request": {
    "user": "alice",
    "action": "read",
    "resource": "customers"
  }
}'

请求将不会被接受,因为 alice 尚未被授予对 customers 资源的访问:

{
  "response": {
    "uid": "",
    "allowed": false,
    "auditAnnotations": null,
    "warnings": null
  }
}

让我们再试一次,使用一个有效的资源:

curl -X POST \
  http://localhost:3000/validate_raw/raw-validation \
  -H 'Content-Type: application/json' \
  -d '{
  "request": {
    "user": "alice",
    "action": "read",
    "resource": "orders"
  }
}'

这次,请求将被接受:

{
  "response": {
    "uid": "",
    "allowed": true,
    "auditAnnotations": null,
    "warnings": null
  }
}

如果请求负载中提供了 uid 字段,它将作为响应的一部分返回。

变更

现在,让我们尝试根据 raw-mutation 策略变更一个 JSON 文档:

curl -X POST \
  http://localhost:3000/validate_raw/raw-mutation \
  -H 'Content-Type: application/json' \
  -d '{
  "request": {
    "user": "alice",
    "action": "read",
    "resource": "privateResource"
  }
}'

请求将被变更,响应将包含一个 JSONPatch:

{
  "response": {
    "uid": "",
    "allowed": true,
    "patchType": "JSONPatch",
    "patch": "W3sib3AiOiJyZXBsYWNlIiwicGF0aCI6Ii9yZXNvdXJjZSIsInZhbHVlIjoicHVibGljUmVzb3VyY2UifV0=",
    "auditAnnotations": null,
    "warnings": null
  }
}

编写原始策略

与验证 Kubernetes 资源的策略类似,原始策略是使用 Admission Controller SDK 在 WebAssembly 中编写的。 如果您有兴趣编写原始策略,请参考特定语言的文档以获取更多信息: