Policy Authoring
Policy authoring with Kong Enterprise Gateway defines which HTTP APIs should be allowed or denied. Those policies are enforced by Kong Enterprise Gateway, and a rejected API request is never seen by the service.
Pre-installed Policies
When you add the Kong Enterprise Gateway system, and ingress policy is automatically installed. In the Styra DAS, click the ingress folder in your Kong Enterprise Gateway system to see the policy.
- Ingress: Policy that either allows or denies incoming traffic.
Write Ingress Policy
Use OPA's policy language, Rego, to author policies.
The following resources provide additional information on Rego:
Styra Academy: Styra Academy.
When writing Rego policies, you need to know the format of the JSON that is provided to each of your policies as input. The following shows a sample input provided by Kong Gateway for ingress policy. This sample includes HTTP method, requested path, source address, and headers with destination address.
"input": {
"client_ip": "172.17.0.1",
"request": {
"http": {
"headers": {
"accept": "*/*",
"authorization": "Basic Ym9iOnBhc3N3b3Jk",
"host": "kong-quickstart-kong-proxy.kong.svc.cluster.local",
"user-agent": "curl/7.74.0-DEV"
},
"host": "kong-quickstart-kong-proxy.kong.svc.cluster.local",
"method": "GET",
"path": "/finance/salary/alice",
"port": 80,
"querystring": {},
"scheme": "http",
"tls": {}
}
}
}
When writing policies, the allow or deny rules are written to describe the conditions under which a request is allowed or denied. By default, requests are all allowed, so you must write policy to deny them.
For example, if you want to allow all GET
requests and deny all POST
requests to the root path, then write the following allow
and deny
rules.
# allow GET requests to the root path
allow {
input.input.request.http.method == "GET"
input.input.request.http.path == "/"
}
# deny POST requests to the root path
deny {
input.input.request.http.method == "POST"
input.input.request.http.path == "/"
}
The decision mapping for Kong Enterprise Gateway systems relies on the presence of well-known fields to correctly parse the results. The decision mapper expects the allowed field in the result.
{
"result": {
"allowed": false // boolean value to determine if decision was Allowed or Denied
// an absence of this value will mark the decision as Unknown
}
}
The Kong Enterprise Gateway system expects a boolean value in the result field in the response from OPA for decision enforcement.
{
"result": false
}