πŸ”’ Security

Kyverno

Kyverno

A Kubernetes-native policy management tool. Define policies in YAML.

πŸ“– Detailed Explanation

Kyverno is a Kubernetes-native policy management tool that lets you define policies using only YAML, so there is no need to learn the Rego language. It was developed by Nirmata in 2019 and was adopted as a CNCF project.

Kyverno has 4 policy types: validate, mutate, generate, and verifyImages. validate checks resource specs, mutate injects default values, and generate automatically creates related resources.

verifyImages enforces that only images signed with Cosign or Notary are deployed. It can also verify SLSA provenance attestations, enabling supply chain security to be implemented.

Compared with OPA Gatekeeper, it has a lower learning curve and is more Kubernetes-friendly. However, Rego may be more powerful for very complex policy logic. Choose based on your team's proficiency and the complexity of your policies.

πŸ’» Code Example

# Kyverno μ„€μΉ˜
helm repo add kyverno https://kyverno.github.io/kyverno/
helm install kyverno kyverno/kyverno -n kyverno --create-namespace

# ClusterPolicy: λ¦¬μ†ŒμŠ€ μ œν•œ ν•„μˆ˜
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: require-resource-limits
spec:
  validationFailureAction: Enforce
  rules:
    - name: require-limits
      match:
        resources:
          kinds:
            - Pod
      validate:
        message: "CPU and memory limits are required"
        pattern:
          spec:
            containers:
              - resources:
                  limits:
                    cpu: "?*"
                    memory: "?*"

---
# ClusterPolicy: μ„œλͺ…λœ μ΄λ―Έμ§€λ§Œ ν—ˆμš©
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: verify-image-signature
spec:
  validationFailureAction: Enforce
  rules:
    - name: verify-cosign-signature
      match:
        resources:
          kinds:
            - Pod
          namespaces:
            - production
      verifyImages:
        - imageReferences:
            - "ghcr.io/myorg/*"
          attestors:
            - entries:
                - keyless:
                    issuer: "https://token.actions.githubusercontent.com"
                    subject: "https://github.com/myorg/*"

---
# ClusterPolicy: κΈ°λ³Έ λ ˆμ΄λΈ” μžλ™ μΆ”κ°€ (mutate)
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: add-default-labels
spec:
  rules:
    - name: add-team-label
      match:
        resources:
          kinds:
            - Deployment
      mutate:
        patchStrategicMerge:
          metadata:
            labels:
              +(managed-by): kyverno

πŸ—£οΈ How It's Said in Practice

Platform: "We're going to add an image signature verification policy with Kyverno. Using verifyImages, we can check Cosign signatures."

Dev team: "Can we use it alongside our existing Gatekeeper?"

Platform: "Yes, both are Admission Webhooks, so they can coexist. But it's best to separate their roles so they don't overlap."

Interviewer: "Please explain the differences between Kyverno and OPA Gatekeeper."

Candidate: "Kyverno writes policies in YAML, so it has a low learning curve, and it can modify and create resources with mutate and generate. Gatekeeper can express complex logic using the Rego language. Also, Kyverno natively supports image signature verification with verifyImages."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

Reviewer: "validationFailureAction is set to audit, but in production you need to change it to enforce for violations to actually be blocked."

Author: "Right, I'll switch to enforce once testing is done."

⚠️ Cautions

πŸ“š Learn More