Question 613 of 997
Minimize Microservice VulnerabilitieseasyMultiple ChoiceObjective-mapped

How to Write a ConstraintTemplate with the violation Rego Rule

This CKS practice question tests your understanding of minimize microservice vulnerabilities. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

You are tasked with creating a ConstraintTemplate in OPA/Gatekeeper that denies pods running with the 'latest' image tag. Which Rego rule should the ConstraintTemplate include?

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

violation[{"msg": msg}] { ... }

In OPA/Gatekeeper ConstraintTemplates, the Rego rule must be named `violation` to define the logic that triggers a denial when a resource violates the constraint. The `violation` rule returns a message in the `msg` field, which Gatekeeper uses to reject the resource. Option B is correct because it follows the required syntax for Gatekeeper constraints, where `violation[{"msg": msg}]` is the standard entry point for enforcing policies.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • admit[{"msg": msg}] { ... }

    Why it's wrong here

    admit is not a standard rule; admission decisions are made by violation rules.

  • violation[{"msg": msg}] { ... }

    Why this is correct

    In OPA/Gatekeeper ConstraintTemplates, the violation rule is used to deny admission. If the rule evaluates to true, the request is denied.

    Related concept

    Read the scenario before looking for a memorised answer.

  • reject[{"msg": msg}] { ... }

    Why it's wrong here

    reject is not a standard rule name in ConstraintTemplates.

  • deny[{"msg": msg}] { ... }

    Why it's wrong here

    deny is not a standard rule name in ConstraintTemplates. The correct rule is violation.

Common exam traps

Common exam trap: answer the scenario, not the keyword

The CKS exam often tests the distinction between OPA's generic `deny` rule and Gatekeeper's specific `violation` rule, tricking candidates who confuse general OPA syntax with the required Gatekeeper constraint format.

Detailed technical explanation

How to think about this question

Under the hood, Gatekeeper evaluates the `violation` rule for each resource; if the rule produces a result (i.e., a non-empty set), the resource is denied. The `msg` field is used to provide a human-readable reason, which is included in the admission review response. A subtle behavior is that the `violation` rule must be defined within a `rego` package that matches the ConstraintTemplate's `target` (e.g., `admission.k8s.gatekeeper.sh`), and the rule's logic must reference the `input` object to access the resource's spec, such as `input.review.object.spec.containers[_].image` to check for the 'latest' tag.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A security administrator must allow nursing staff to reach a patient records server while blocking access from the guest Wi-Fi VLAN. After applying an extended ACL, traffic is still blocked from nursing workstations. The ACL was applied outbound instead of inbound on the wrong interface. Questions like this test ACL direction and placement rules.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related CKS practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free CKS practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this CKS question test?

Minimize Microservice Vulnerabilities — This question tests Minimize Microservice Vulnerabilities — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: violation[{"msg": msg}] { ... } — In OPA/Gatekeeper ConstraintTemplates, the Rego rule must be named `violation` to define the logic that triggers a denial when a resource violates the constraint. The `violation` rule returns a message in the `msg` field, which Gatekeeper uses to reject the resource. Option B is correct because it follows the required syntax for Gatekeeper constraints, where `violation[{"msg": msg}]` is the standard entry point for enforcing policies.

What should I do if I get this CKS question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

2 more ways this is tested on CKS

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Which THREE of the following are valid Rego policy constructs used in OPA Gatekeeper ConstraintTemplates to enforce security policies? (Choose three)

hard
  • A.violation[{"msg": msg}] { condition }
  • B.allow { condition }
  • C.deny[{"msg": msg}] { condition }
  • D.audit { condition }
  • E.warn[{"msg": msg}] { condition }

Why A: In OPA Gatekeeper ConstraintTemplates, the only Rego construct that directly triggers a constraint violation is `violation[{"msg": msg}] { condition }`. When the condition evaluates to true, Gatekeeper generates a violation message. The `allow` and `deny` rules are not standard constructs for enforcing constraints in Gatekeeper; they are general Rego rules used in other OPA contexts but not within ConstraintTemplates to report violations. Options D (`audit`) and E (`warn`) are also not valid Rego constructs for Gatekeeper constraints.

Variation 2. An OPA/Gatekeeper ConstraintTemplate is defined with the following Rego rule: violation[{"msg": msg}] { container := input.review.object.spec.containers[_] container.securityContext.runAsNonRoot != true msg := "Container must run as non-root" } What happens when a pod is submitted with a container that has runAsNonRoot: true?

hard
  • A.The pod is admitted but an audit log is generated
  • B.The pod is admitted
  • C.The pod is denied with a message
  • D.The pod is mutated to set runAsNonRoot

Why B: Option B is correct because the Rego rule `container.securityContext.runAsNonRoot != true` only triggers a violation when the field is not set to `true`. When `runAsNonRoot: true` is explicitly set, the condition evaluates to `false`, so no violation is generated, and the pod is admitted without any denial or mutation. OPA/Gatekeeper by default enforces constraints by denying admission; it does not mutate resources or generate audit logs unless specifically configured for dry-run or audit mode.

Keep practising

More CKS practice questions

Last reviewed: Jul 4, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

This CKS practice question is part of Courseiva's free CNCF certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the CKS exam.