Skip to main content

Policy Authoring

Policy authoring with Kong Enterprise Gateway System defines which HTTP APIs should be allowed or denied. Those policies are enforced by Kong Enterprise Gateway System, and a rejected API request is never seen by the service.

Pre-installed Policies

When the Kong Enterprise Gateway System is installed, an ingress policy is automatically installed. To see the policy, navigate to your Kong Enterprise Gateway System in the Styra DAS UI and click the Ingress folder. The ingress policy either allows or denies incoming traffic.

Write Ingress Policy

When authoring policies, use Rego, Open Policy Agent's policy language.

For an introduction to Rego, Styra recommends you review the following materials:

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
}