Skip to content

A Bug in the VAP engine regarding DELETE behavior. #4411

@Hlinbit

Description

@Hlinbit

What steps did you take and what happened:
The template:

apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
  name: k8sservicedeleteprotection
  annotations:
    description: "Prevent Services in specified namespaces from being deleted."
spec:
  crd:
    spec:
      names:
        kind: K8sServiceDeleteProtection
      validation:
        legacySchema: true
        openAPIV3Schema:
          type: object
          properties:
            protectedNamespaces:
              type: array
              description: "Namespaces in which Service deletion is denied."
              items:
                type: string
  targets:
    - target: admission.k8s.gatekeeper.sh
      operations: ["DELETE"]
      code:
        - engine: K8sNativeValidation
          source:
            generateVAP: true
            validations:
              # Pass: Non-Service resources or resources outside protected namespaces.
              - expression: >-
                  !has(variables.anyObject) || !has(variables.anyObject.metadata)
                  || variables.anyObject.kind != 'Service'
                  || !variables.params.protectedNamespaces.exists(n, n == variables.anyObject.metadata.namespace)
                messageExpression: >-
                  "Deleting Service in protected namespace is not allowed: "
                  + variables.anyObject.metadata.namespace
                  + "/" + variables.anyObject.metadata.name

Generated VAP(Some irrelevant content has been omitted.):

spec:
    matchConstraints:
    matchPolicy: Equivalent
    namespaceSelector: {}
    objectSelector: {}
    resourceRules:
    - apiGroups:
      - '*'
      apiVersions:
      - '*'
      operations:
      - CREATE
      - UPDATE
      resources:
      - '*'
      scope: '*'
  paramKind:
    apiVersion: constraints.gatekeeper.sh/v1beta1
    kind: K8sServiceDeleteProtection
  validations:
  - expression: '!has(variables.anyObject) || !has(variables.anyObject.metadata) ||
      variables.anyObject.kind != ''Service'' || !variables.params.protectedNamespaces.exists(n,
      n == variables.anyObject.metadata.namespace)'
    messageExpression: '"Deleting Service in protected namespace is not allowed: "
      + variables.anyObject.metadata.namespace + "/" + variables.anyObject.metadata.name'
  variables:
  - expression: 'has(request.operation) && request.operation == "DELETE" && object
      == null ? oldObject : object'
    name: anyObject
  - expression: '!has(params.spec) ? null : !has(params.spec.parameters) ? null: params.spec.parameters'
    name: params

What did you expect to happen:
Expected generated VAP(Some irrelevant content has been omitted.):

spec:
    matchConstraints:
    matchPolicy: Equivalent
    namespaceSelector: {}
    objectSelector: {}
    resourceRules:
    - apiGroups:
      - '*'
      apiVersions:
      - '*'
      operations:
      - DELETE
      resources:
      - '*'
      scope: '*'

Anything else you would like to add:

Environment:

  • Gatekeeper version: v3.21.1
  • Kubernetes version: v1.35.1 (kind)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions