【问题标题】:OPA Gatekeeper use path as variable?OPA Gatekeeper 使用路径作为变量?
【发布时间】:2022-12-17 17:39:26
【问题描述】:

我正在将 OPA Gatekeeper 与 Kubernetes 一起使用,并试图定义一个模板和一个约束,以阻止具有某些字段的请求。我这样做的方法是阻止模板中该字段的路径,但是我似乎在设置此设置时遇到了一些问题。 我的模板如下:

apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
  name: test-template
spec:
  crd:
    spec:
      names:
        kind: Test-Template
      validation:
        openAPIV3Schema:
          properties:
            errorPath:
              type: object
  targets:
    - target: admission.k8s.gatekeeper.sh
      rego: |
        package testtemplate
        violation[{"msg": msg}] {
          input.parameters.errorPath == "error"
          msg := sprintf("The name %v is not allowed", [input.parameters.errorPath])
        }

而约束是:

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: Test-Template
metadata:
  name: test-constraint
spec:
  parameters:
    invalidPath: input.review.object.spec.resourceRef

部署约束错误:

body 中的 spec.parameters.invalidPath 必须是对象类型:“string”

如果输入 spec.resoureRef 设置为错误,我的目标是让它失败。

【问题讨论】:

    标签: opa open-policy-agent rego


    【解决方案1】:

    您在约束中将其命名为 invalidPath,但在策略中将其命名为 errorPath。虽然我不确定将路径本身参数化是个好主意,但您可能会这样做:

    apiVersion: constraints.gatekeeper.sh/v1beta1
    kind: Test-Template
    metadata:
      name: test-constraint
    spec:
      parameters:
        invalidPath:
        - review
        - object
        - spec
        - resourceRef
    

    apiVersion: templates.gatekeeper.sh/v1beta1
    kind: ConstraintTemplate
    metadata:
      name: test-template
    spec:
      crd:
        spec:
          names:
            kind: Test-Template
          validation:
            openAPIV3Schema:
              properties:
                invalidPath:
                  type: array
      targets:
        - target: admission.k8s.gatekeeper.sh
          rego: |
            package testtemplate
            violation[{"msg": msg}] {
              object.get(input, input.parameters.invalidPath, null) == "error"
              msg := sprintf("%v must not have value 'error'", [input.parameters.invalidPath])
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2012-11-01
      • 2014-02-07
      • 2021-08-12
      • 2014-01-20
      相关资源
      最近更新 更多