First policy¶
The Kyverno Authz Server uses ValidatingPolicy resources to define authorization rules.
Policies for Envoy and HTTP authorization follow the same structure and logic bu have subtle differences.
Key Concepts¶
- Evaluation Mode: Set to
EnvoyorHTTPto determine the request type - Failure Policy: Controls behavior when policy evaluation fails (
FailorIgnore) - Match Conditions: Optional CEL expressions for fine-grained request filtering
- Variables: Reusable named expressions available throughout the policy
- Validation Rules: CEL expressions that return authorization decisions (or
nullto continue processing with the next rule)
Example policy¶
The policy below does the following:
- Parse the incoming request
authorizationheader to decode a bearer token. - Decoding uses the
jwtCEL lib. - Based on the presence, validity of the token, and roles contained in the claims, the policy will make a decision to allow or deny the request.
apiVersion: policies.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
name: quick-start
spec:
evaluation:
mode: Envoy # (1)!
failurePolicy: Fail # (2)!
variables: # (3)!
- name: authorization
expression: object.attributes.request.http.headers[?"authorization"].orValue("").split(" ")
- name: token
expression: >
size(variables.authorization) == 2 && variables.authorization[0].lowerAscii() == "bearer"
? jwt.Decode(variables.authorization[1], "secret")
: null
validations: # (4)!
# request not authenticated -> 401
- expression: >
variables.token == null || !variables.token.Valid
? envoy.Denied(401).Response()
: null
# request authenticated but not admin role -> 403
- expression: >
variables.token.Claims.?role.orValue("") != "admin"
? envoy.Denied(403).Response()
: null
# request authenticated and admin role -> 200
- expression: >
envoy.Allowed().Response()
- Evaluation Mode: Set to
EnvoyorHTTPto determine the request type - Failure Policy: Controls behavior when policy evaluation fails (
FailorIgnore) - Variables: Reusable named expressions available throughout the policy
- Validation Rules: CEL expressions that return authorization decisions (or
nullto continue processing with the next rule)
apiVersion: policies.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
name: quick-start
spec:
evaluation:
mode: HTTP # (1)!
failurePolicy: Fail # (2)!
variables: # (3)!
- name: authorization
expression: object.attributes.header[?"authorization"].orValue("").split(" ")
- name: token
expression: >
size(variables.authorization) == 2 && variables.authorization[0].lowerAscii() == "bearer"
? jwt.Decode(variables.authorization[1], "secret")
: null
validations: # (4)!
# request not authenticated -> allowed
- expression: >
variables.token == null || !variables.token.Valid
? http.Allowed().Response()
: null
# request authenticated but not admin role -> denied
- expression: >
variables.token.Claims.?role.orValue("") != "admin"
? http.Denied("authenticated but not an admin").Response()
: null
# request authenticated and admin role -> allowed
- expression: >
http.Allowed().Response()
- Evaluation Mode: Set to
EnvoyorHTTPto determine the request type - Failure Policy: Controls behavior when policy evaluation fails (
FailorIgnore) - Variables: Reusable named expressions available throughout the policy
- Validation Rules: CEL expressions that return authorization decisions (or
nullto continue processing with the next rule)