You are browsing documentation for an older version. See the latest documentation here.

Add Plugin Configuration

The following is a step by step guide for adding configuration to custom plugins on Kong Gateway.

Prerequisites

This page is the third chapter in the Getting Started guide for developing custom plugins. These instructions refer to the previous chapters in the guide and require the same developer tool prerequisites.

Step by Step

Now that you have a basic plugin project with testing, the following steps guide you through adding configuration capabilities to the plugin code.

Add configuration fields to the schema

Let’s add some configuration fields to our schema.lua file allowing us to make the plugin behavior configurable without having to redeploy or restart.

Kong Gateway provides a module of base type definitions that can help us with common types related to API gateway plugins.

At the top of the schema.lua file we include the Kong Gateway typedefs module with the statement:

  1. local typedefs = require "kong.db.schema.typedefs"

Below is a Lua snippet showing a typical configuration field that defines a field named response_header_name.

Here we use the header_name type definition which defines the field to be a string that cannot be null and conforms to the gateway rules for header names.

  1. { response_header_name = typedefs.header_name {
  2. required = false,
  3. default = "X-MyPlugin" } },

The definition also indicates that the configuration value is not required, which means it’s optional for the user when configuring the plugin. We also specify a default value that will be used when a user does not specify a value.

Place this definition in the schema.lua file within the fields array we defined earlier.

The full schema.lua now looks like the following:

  1. local typedefs = require "kong.db.schema.typedefs"
  2. local PLUGIN_NAME = "my-plugin"
  3. local schema = {
  4. name = PLUGIN_NAME,
  5. fields = {
  6. { config = {
  7. type = "record",
  8. fields = {
  9. { response_header_name = typedefs.header_name {
  10. required = false,
  11. default = "X-MyPlugin" } },
  12. },
  13. },
  14. },
  15. },
  16. }
  17. return schema

Now the plugin can accept a configuration value, let’s see how to read it from the plugin logic code.

Read configuration values from plugin code

Modify the handler.lua file response function to read the configuration value from the incoming conf parameter instead of the current hardcoded value.

The function now looks like the following:

  1. function MyPluginHandler:response(conf)
  2. kong.response.set_header(conf.response_header_name, "response")
  3. end

That’s all that’s required, the plugin can now be dynamically configured at runtime. Next, let’s validate our changes work as expected with manual and automated tests.

Manually validate configuration

In the previous chapter on testing we showed how to use Pongo to run a shell and manually validate the plugins behavior. Repeat that process here to validate the plugins new configurable behavior.

Launch the gateway and open a shell:

  1. pongo shell

From within the shell, run the database migrations and start Kong Gateway:

  1. kms

Add a test service:

  1. curl -i -s -X POST http://localhost:8001/services \
  2. --data name=example_service \
  3. --data url='http://httpbin.org'

And create our plugin instance, but this time provide the configuration value as data in the POST request to the Admin API:

  1. curl -is -X POST http://localhost:8001/services/example_service/plugins \
  2. --data 'name=my-plugin' --data 'config.response_header_name=X-CustomHeaderName'

Add a route so we can send test requests:

  1. curl -i -X POST http://localhost:8001/services/example_service/routes \
  2. --data 'paths[]=/mock' \
  3. --data name=example_route

And proxy a request through our test route:

  1. curl -i http://localhost:8000/mock/anything

This time we should see our configured header in the response:

  1. ...
  2. [X-CustomHeaderName] = 'response'
  3. ...

Now that my-plugin is configurable and we’ve validate the behavior, let’s look at how we can build an automated approach to validate our changes.

Add automated configuration testing

Without changing anything we can re-run the tests we already have with pongo run:

  1. pongo run

Our tests should continue to report successful:

  1. [ PASSED ] 2 tests.

Our test is still valid because the default value in our configurable field matches the value in our test case. Let’s see how we can validate a different value for the response_header_name field.

Modify the setup function inside the spec/01-integration_spec.lua module so that the my-plugin that is added to the database is configured with a different value for the response_header_name field.

Here is the code:

  1. -- Add the custom plugin to the test route
  2. blue_print.plugins:insert {
  3. name = PLUGIN_NAME,
  4. route = { id = test_route.id },
  5. config = {
  6. response_header_name = "X-CustomHeaderName",
  7. },
  8. }

Use Pongo to re-run the test:

  1. pongo run

If we’ve changed everything properly, our tests should fail and produce a report similar to the following:

  1. /kong-plugin/spec/my-plugin/01-integration_spec.lua:75: Expected header:
  2. (string) 'X-MyPlugin'
  3. But it was not found in:
  4. (table: 0x484c9942b180) {
  5. [Connection] = 'keep-alive'
  6. [Content-Length] = '1016'
  7. [Content-Type] = 'application/json'
  8. [Date] = 'Mon, 18 Mar 2024 13:49:06 GMT'
  9. [Server] = 'mock-upstream/1.0.0'
  10. [Via] = 'kong/3.7.x'
  11. [X-CustomHeaderName] = 'response'
  12. [X-Kong-Proxy-Latency] = '1'
  13. [X-Kong-Request-Id] = '92dfcf56b12b0d29f54ed9295aed6356'
  14. [X-Kong-Upstream-Latency] = '2'
  15. [X-Powered-By] = 'mock_upstream' }
  16. stack traceback:
  17. /kong-plugin/spec/my-plugin/01-integration_spec.lua:75: in function </kong-plugin/spec/my-plugin/01-integration_spec.lua:66>
  18. [ FAILED ] /kong-plugin/spec/my-plugin/01-integration_spec.lua:66: my-plugin: [#postgres] The response gets a 'X-MyPlugin' header (4.58 ms)

The tests fail because while we updated the plugin configuration, we did not update the expected header name in the test case. To fix the tests, modify the test assertion to match our configured header name.

Change this line:

  1. local header_value = assert.response(r).has.header("X-MyPlugin")

To expect the new configured header name:

  1. local header_value = assert.response(r).has.header("X-CustomHeaderName")

When you re-run the tests:

  1. pongo run

Pongo should report a successful test run:

  1. [ PASSED ] 2 tests.

What’s next?

You now have a complete development environment and a framework for adding capabilities to your custom plugin. Next we’ll show you how to connect to external services from within your plugin code and later show you how to deploy your plugin onto your Kong Gateway dataplanes so you can put your business logic into production.


Previous Add Plugin Testing

Next Consume External Services