Validating policies
The Kubewarden policy server receives:
- Kubernetes
AdmissionReview
objects from the Kubernetes API server. It then forwards the value of itsrequest
attribute, of typeAdmissionRequest
, to the policy to be evaluated.
or:
- A JSON
request
attribute containing the free-form request document, in case of a raw policy. Check the Raw policies section for more details.
The policy evaluates the request
and states whether it should be accepted or not.
When the request is rejected,
the policy might provide the explanation message and an error code to be shown to the end user.
By convention, of the policy-server
project,
the guest has to expose a function named validate
,
through the waPC guest SDK,
so that the policy-server
(waPC host) can invoke it.
The validate
function receives a ValidationRequest
JSON object and returns a ValidationResponse
JSON object.
The ValidationRequest
object​
The ValidationRequest
is a JSON object that is received by the validate
function.
It looks like:
{
"request": <AdmissionReview.request data> | <RawReviewRequest.request data>,
"settings": {
# your policy configuration
}
}
The settings
key points to a free-form JSON document holds the policy
specific settings.
The previous chapter focused on policies and settings.
An example​
Given the following Kubernetes AdmissionReview
:
Expand to see AdmissionReview
The ValidationRequest
object would look like:
Expand to see the ValidationRequest
The ValidationResponse
object​
The validate
function returns the outcome of its validation using a ValidationResponse
object.
The ValidationResponse
is structured in the following way:
{
# mandatory
"accepted": <boolean>,
# optional, ignored if accepted - recommended for rejections
"message": <string>,
# optional, ignored if accepted
"code": <integer>,
# optional, used by mutation policies
"mutated_object": <string>
}
These message
and code
attributes can be specified when the request is not accepted.
The message
is a free-form textual error and code
represents an HTTP error code.
If the request is accepted,
the message
and code
values are ignored by the Kubernetes API server if present.
If message
or code
are provided,
and the request is not accepted,
then the Kubernetes API server returns this information, as part of the body of the error, to the Kubernetes API client that issued the rejected request.
The mutated_object
is an optional field used only by mutating policies.
This is the topic of the next chapter.