Write plugins in Python

The Python PDK

Kong Gateway support for Python plugin development is provided by the kong-python-pdk library. The library provides a plugin server and Kong-specific functions to interface with Kong Gateway.

Install

To install the plugin server and PDK globally, use pip:

  1. pip3 install kong-pdk

Development

A Kong Gateway Python plugin implementation has following attributes:

  1. Schema = (
  2. { "message": { "type": "string" } },
  3. )
  4. version = '0.1.0'
  5. priority = 0
  6. class Plugin(object):
  7. pass
  • A class named Plugin defines the class that implements this plugin.
  • A dictionary called Schema that defines expected values and data types of the plugin.
  • The variables version and priority that define the version number and priority of execution respectively.

Note: This repository contains example Python plugins and an API reference.

Phase handlers

You can implement custom logic to be executed at various points of the request processing lifecycle. To execute custom code during the access phase, define a function named access:

  1. class Plugin(object):
  2. def __init__(self, config):
  3. self.config = config
  4. def access(self, kong):
  5. pass

You can implement custom logic during the following phases using the same function signature:

  • certificate
  • rewrite
  • access
  • response
  • preread
  • log

The presence of the response handler automatically enables the buffered proxy mode.

Type hints

Support for type hints is available. To use type hints and autocomplete in an IDE, add the kong parameter to the phase handler function:

  1. import kong_pdk.pdk.kong as kong
  2. class Plugin(object):
  3. def __init__(self, config):
  4. self.config = config
  5. def access(self, kong: kong.kong):
  6. host, err = kong.request.get_header("host")

Embedded server

To use the embedded server, use the following code:

  1. if __name__ == "__main__":
  2. from kong_pdk.cli import start_dedicated_server
  3. start_dedicated_server("py-hello", Plugin, version, priority)

The first argument to start_dedicated_server defines the plugin name and must be unique.

Example configuration

To load plugins using the kong.conf configuration file, you have to map existing Kong Gateway properties to aspects of your plugin. Below are two examples of loading plugins within kong.conf.

  1. pluginserver_names = my-plugin,other-one
  2. pluginserver_my_plugin_socket = /usr/local/kong/my-plugin.socket
  3. pluginserver_my_plugin_start_cmd = /path/to/my-plugin.py
  4. pluginserver_my_plugin_query_cmd = /path/to/my-plugin.py --dump
  5. pluginserver_other_one_socket = /usr/local/kong/other-one.socket
  6. pluginserver_other_one_start_cmd = /path/to/other-one.py
  7. pluginserver_other_one_query_cmd = /path/to/other-one.py -dump

The socket and start command settings coincide with their defaults and can be omitted:

  1. pluginserver_names = my-plugin,other-one
  2. pluginserver_my_plugin_query_cmd = /path/to/my-plugin --dump
  3. pluginserver_other_one_query_cmd = /path/to/other-one --dump

Concurrency model

The Python plugin server and the embedded server support concurrency. By default, the server starts in multi-threading mode.

If your workload is IO intensive, you can use the Gevent model by passing the -g flag to start_cmd in kong.conf. If your workload is CPU intensive, consider the multi-processing model by passing the -m flag to start_cmd in kong.conf.

More Information