Kong Gateway is a Lua application designed to load and execute modules. These modules, called plugins, allow you to add more features to your implementation.
Kong provides a set of standard Lua plugins that get bundled with Kong Gateway and Konnect.
You can also develop custom plugins, adding your own custom functionality to Kong Gateway.
Kong provides an entire development environment for developing plugins, including Plugin Development Kits (or PDKs), database abstractions, migrations, and more.
Plugins consist of modules interacting with the request/response objects or streams via a PDK to implement arbitrary logic. Kong provides PDKs in the following languages:
Lua
Go
Python
JavaScript
These PDKs are sets of functions that a plugin can use to facilitate interactions between plugins and the core (or other components) of Kong.
A Kong Gateway plugin allows you to inject custom logic at several entrypoints in the lifecycle of a request,
response, or TCP stream connection as it is proxied by Kong Gateway.
Executed for every request upon its reception from a client as a rewrite phase handler.
In this phase, neither the Service nor the Consumer have been identified, hence this handler will only be executed if the plugin was configured as a global plugin.
Replaces both header_filter() and body_filter(). Executed after the whole response has been received from the upstream service, but before sending any part of it to the client.
Executed for each chunk of the response body received from the upstream service. Since the response is streamed back to the client, it can exceed the buffer size and be streamed chunk by chunk. This function can be called multiple times if the response is large. See the lua-nginx-module documentation for more details.
Executed after the WebSocket connection has been terminated.
Note: If a module implements the response function, Kong Gateway will automatically activate the “buffered proxy” mode, as if the kong.service.request.enable_buffering() function had been called. Because of a current Nginx limitation, this doesn’t work for HTTP/2 or gRPC upstreams.
To reduce unexpected behavior changes, Kong Gateway does not start if a plugin implements both response and either header_filter or body_filter.
The Stream module is used for Plugins written for TCP and UDP stream connections. It uses the following functions:
Executed during the SSL certificate serving phase of the SSL handshake.
All of those functions, except init_worker and configure, take one parameter which is given
by Kong Gateway upon its invocation: the configuration of your plugin. This parameter
is a Lua table, and contains values defined by your users, according to your
plugin’s schema (described in the schema.lua module). The configure is called with an array of all the enabled
plugin configurations for the particular plugin (or in case there is no active configurations
to plugin, a nil is passed). init_worker and configure happens outside
requests or frames, while the rest of the phases are bound to incoming request/frame.
Note that UDP streams don’t have real connections. Kong Gateway will consider all
packets with the same origin and destination host and port as a single
connection. After a configurable time without any packet, the connection is
considered closed and the log function is executed.
You can run plugins in various contexts, depending on your environment needs.
Each plugin can run globally, or be scoped to some combination of the following:
Using scopes, you can customize how Kong Gateway handles functions in your environment,
either before a request is sent to your upstream services or after it receives a response.
For example, if you apply a plugin to a single Route,
that plugin will only trigger when a request matches the Route’s specific path.
A global plugin is not associated to any Service, Route, Consumer, or Consumer Group is
considered global, and will be run on every request, regardless of any other configuration.
In self-managed Kong Gateway Enterprise, the plugin applies to every entity in a given Workspace.
In self-managed open-source Kong Gateway, the plugin applies to your entire environment.
In Konnect, the plugin applies to every entity in a given Control Plane.
A single plugin instance always runs once per request. The
configuration with which it runs depends on the entities it has been
configured for.
Plugins can be configured for various entities, combinations of entities, or even globally.
This is useful, for example, when you want to configure a plugin a certain way for most requests but make authenticated requests behave slightly differently.
Therefore, there is an order of precedence for running a plugin when it has been applied to different entities with different configurations. The number of entities configured to a specific plugin directly correlates to its priority. The more entities a plugin is configured for, the higher its order of precedence.
The complete order of precedence for plugins configured to multiple entities is:
Consumer + Route + Service: Highest precedence, affecting authenticated requests that match a specific Consumer on a particular Route and Service.
Consumer group + Service + Route: Affects groups of authenticated users across specific Services and Routes.
Consumer + Route: Targets authenticated requests from a specific Consumer on a particular Route.
Consumer + Service: Applies to authenticated requests from a specific Consumer accessing any Route within a given Service.
Consumer group + Route: Affects groups of authenticated users on specific Routes.
Consumer group + Service: Applies to all Routes within a specific Service for groups of authenticated users.
Route + Service: Targets all Consumers on a specific Route and Service.
Consumer: Applies to all requests from a specific, authenticated Consumer across all Routes and Services.
Consumer Group: Affects all Routes and Services for a designated group of authenticated users.
Route: Specific to given Route.
Service: Specific to given Service.
Globally configured plugins: Lowest precedence, applies to all requests across all Services and Routes regardless of Consumer status.
Note on precedence for Consumer Groups
When a Consumer is a member of two Consumer Groups, each with a scoped instance of the same plugin, Kong Gateway ensures deterministic behavior by executing only one of these plugin instances.
Currently, this is determined by the Consumer Group name, in alphabetical order.
For example, if you have two Consumer Groups, A and B, each with an instance of the Rate Limiting Advanced plugin, the plugin in Consumer Group A will be applied.
The specific rules that govern this behavior are not defined and are subject to change in future releases.
All of the plugins bundled with Kong Gateway have a static priority.
This can be adjusted dynamically using the plugin’s ordering configuration parameter.
You can override the priority for any Kong Gateway plugin using each plugin’s ordering configuration parameter.
This determines plugin ordering during the access phase, and lets you create dynamic dependencies between plugins.
You can choose to run a particular plugin before or after a specified plugin or list of plugins.
For example, let’s say you want to limit the amount of requests against your Gateway Service and Route before Kong requests authentication.
The following example uses the Rate Limiting Advanced plugin with the Key Auth plugin as the authentication method:
Add this section to your declarative configuration file:
In Kong Manager or Gateway Manager, go to Plugins.
Click New Plugin.
Choose a scope for the plugin:
Global, which applies the plugin to all services, routes, consumers, and consumer groups in the workspace (Kong Manager) or control plane (Gateway Manager).
Scoped, which lets you choose a specific Gateway service, route, consumer, or consumer group to apply the plugin to.
The types of entities you have available here depend on the plugin you picked.
Configure your plugin. The configuration options will depend on which plugin you picked.
For example, you may want to first transform a request with the Request Transformer plugin, then request authentication.
You can change the order of the authentication plugin (Basic Auth, in this example) so that it always runs after transformation:
Add this section to your declarative configuration file:
In Kong Manager or Gateway Manager, go to Plugins.
Click New Plugin.
Choose a scope for the plugin:
Global, which applies the plugin to all services, routes, consumers, and consumer groups in the workspace (Kong Manager) or control plane (Gateway Manager).
Scoped, which lets you choose a specific Gateway service, route, consumer, or consumer group to apply the plugin to.
The types of entities you have available here depend on the plugin you picked.
Configure your plugin. The configuration options will depend on which plugin you picked.
If using dynamic ordering, manually test all configurations, and handle this feature with care.
There are a number of considerations that can affect your environment:
Consumer scoping: If you have Consumer-scoped plugins anywhere in your Workspace or Control Plane, you can’t use dynamic plugin ordering.
Consumer mapping and dynamic plugin ordering both run in the access phase, but the order of the plugins must be determined after Consumer mapping has happened.
Kong Gateway can’t reliably change the order of the plugins in relation to mapped Consumers.
Cascading deletes: Detecting if a plugin has a dependency to a deleted plugin isn’t supported, so handle your configuration with care.
Performance: Dynamic plugin ordering requires sorting plugins during a request, which adds latency to the request.
In some cases, this might be compensated for when you run rate limiting before an expensive authentication plugin.
Re-ordering any plugin in a Workspace or Control Plane has performance implications to all other plugins within the same environment.
If possible, consider offloading plugin ordering to a separate environment.
Validation: Validating dynamic plugin ordering is a non-trivial task and would require insight into the user’s business logic.
Kong Gateway tries to catch basic mistakes, but it can’t detect all potentially dangerous configurations.
Kong has many bundled plugins available, all of which have their own specific configurations and examples. See all
Kong plugins for their individual configurations.
Here’s an example of configuration for the ACL plugin:
Add this section to your declarative configuration file:
In Kong Manager or Gateway Manager, go to Plugins.
Click New Plugin.
Choose a scope for the plugin:
Global, which applies the plugin to all services, routes, consumers, and consumer groups in the workspace (Kong Manager) or control plane (Gateway Manager).
Scoped, which lets you choose a specific Gateway service, route, consumer, or consumer group to apply the plugin to.
The types of entities you have available here depend on the plugin you picked.
Configure your plugin. The configuration options will depend on which plugin you picked.