Restrict access to a Service or a Route (or the deprecated API entity) by either whitelisting or blacklisting IP addresses. Single IPs, multiple IPs or ranges in CIDR notation like 10.10.10.0/24 can be used.


Terminology

  • plugin: a plugin executing actions inside Kong before or after a request has been proxied to the upstream API.
  • API: (deprecated) the Kong entity representing your upstream service placed behind Kong, for which Kong proxies requests to.
  • Service: the Kong entity representing an external upstream API or microservice.
  • Route: the Kong entity representing a way to map downstream requests to upstream services.
  • Consumer: the Kong entity representing a developer or machine using the API. When using Kong, a Consumer only communicates with Kong which proxies every call to the said upstream API.
  • Credential: a unique string associated with a Consumer, also referred to as an API key.
  • upstream service: this refers to your own API/service sitting behind Kong, to which client requests are forwarded.

Configuration

Enabling the plugin for a Service

Configure on top of a Service by executing the following request on your Kong server:

$ curl -X POST http://kong:8001/services/{service}/plugins \
    --data "name=ip-restriction"  \
    --data "config.whitelist=54.13.21.1, 143.1.0.0/24"
  • service: the id or name of the Service that this plugin configuration will target.

Enabling the plugin for a Route

Configure on top of a Route with:

$ curl -X POST http://kong:8001/routes/{route_id}/plugins \
    --data "name=ip-restriction"  \
    --data "config.whitelist=54.13.21.1, 143.1.0.0/24"
  • route_id: the id of the Route that this plugin configuration will target.

Enabling the plugin for an API

If you are using the deprecated API entity, you can configure on top of an API by executing the following request on your Kong server:

$ curl -X POST http://kong:8001/apis/{api}/plugins \
    --data "name=ip-restriction"  \
    --data "config.whitelist=54.13.21.1, 143.1.0.0/24"
  • api: either id or name of the API that this plugin configuration will target.

Enabling the plugin for a Consumer

You can use the http://localhost:8001/plugins endpoint to target Consumers:

$ curl -X POST http://kong:8001/plugins \
    --data "name=ip-restriction" \
    --data "consumer_id={consumer_id}"  \
    --data "config.whitelist=54.13.21.1, 143.1.0.0/24"

Where consumer_id: The id of the Consumer we want to associate with this plugin.

You can combine adding consumer_id and service_id in the same request.

Once applied, any user with a valid credential can access the Service/API. To restrict usage to only some of the authenticated users, also add the ACL plugin (not covered here) and create whitelist or blacklist groups of users.

Global plugins

All plugins can be configured using the http:/kong:8001/plugins/ endpoint. A plugin which is not associated to any API, Service, Route or Consumer is considered "global", and will be run on every request. Read the Plugin Reference and the Plugin Precedence sections for more information.

Parameters

Here's a list of all the parameters which can be used in this plugin's configuration:

form parameterdefaultdescription
nameThe name of the plugin to use, in this case ip-restriction
api_idThe id of the API which this plugin will target.
service_idThe id of the Service which this plugin will target.
route_idThe id of the Route which this plugin will target.
consumer_idThe id of the Consumer which this plugin will target.
config.whitelist
semi-optional

Comma separated list of IPs or CIDR ranges to whitelist. One of config.whitelist or config.blacklist must be specified.

config.blacklist
semi-optional

Comma separated list of IPs or CIDR ranges to blacklist. One of config.whitelist or config.blacklist must be specified.

Note that the whitelist and blacklist models are mutually exclusive in their usage, as they provide complimentary approaches. That is, you cannot configure the plugin with both whitelist and blacklist configurations. An whitelist provides a positive security model, in which the configured CIDR ranges are allowed access to the resource, and all others are inherently rejected. By contrast, a blacklist configuration provides a negative security model, in which certain CIDRS are explicitly denied access to the resource (and all others are inherently allowed).

Keep up with the latest features