Custom plugin configuration in kong in docker using declarative configuration

2.8k views Asked by At

I am using kong in docker in declarative mode.

I want to try the plugin kong-jwt2header from here: https://docs.konghq.com/hub/yesinteractive/kong-jwt2header/

I configured it as described in kong.yml:

plugins:
- name: kong-jwt2header
  config: 
    strip_claims: false
    token_required: true

I linked a plugin folder into docker container:

version: '3'
services:

  kong:
    container_name: kong
    image: kong:2.1.3-centos
    environment:
      - KONG_DATABASE=off
      - KONG_DECLARATIVE_CONFIG=/usr/local/kong/declarative/kong.yml
      - KONG_PROXY_ACCESS_LOG=/dev/stdout
      - KONG_ADMIN_ACCESS_LOG=/dev/stdout
      - KONG_PROXY_ERROR_LOG=/dev/stderr
      - KONG_ADMIN_ERROR_LOG=/dev/stderr
      - KONG_CUSTOM_PLUGINS=kong-jwt2header
      - KONG_ADMIN_LISTEN=0.0.0.0:7001, 0.0.0.0:7444 ssl
      - KONG_LOG_LEVEL=${KONG_LOG_LEVEL:-info}
    volumes:
      - ./kong.yml:/usr/local/kong/declarative/kong.yml
      - ./plugins:/usr/local/kong/plugins
    ports:
      - 7000:8000
      - 7001:7001
      - 7443:8443
      - 7444:7444

While starting, I get the exception that the plugin needs to be enabled.

2020/10/01 11:45:08 [error] 1#0: init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:491: error parsing declarative config file /usr/local/kong/declarative/kong.yml:
in 'services':
  - in entry 2 of 'services':
    in 'plugins':
      - in entry 1 of 'plugins':
        in 'name': plugin 'kong-jwt2header' not enabled; add it to the 'plugins' configuration property
stack traceback:
        [C]: in function 'error'
        /usr/local/share/lua/5.1/kong/init.lua:491: in function 'init'
        init_by_lua:3: in main chunk
nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:491: error parsing declarative config file /usr/local/kong/declarative/kong.yml:
in 'services':
  - in entry 2 of 'services':
    in 'plugins':
      - in entry 1 of 'plugins':
        in 'name': plugin 'kong-jwt2header' not enabled; add it to the 'plugins' configuration property
stack traceback:
        [C]: in function 'error'
        /usr/local/share/lua/5.1/kong/init.lua:491: in function 'init'
        init_by_lua:3: in main chunk

While searching in internet, I found this: https://github.com/Kong/docker-kong/issues/227

They write that I have to add it into //plugins// variable in kong.conf file like described in plugin developemet guide. (https://docs.konghq.com/1.1.x/plugin-development/distribution/#load-the-plugin)

I tried to set it in docker-compose with

     - KONG_CUSTOM_PLUGINS=kong-jwt2header

without success.

I set in docker_compose.yml the environment variable

     - KONG_PLUGINS=bundled,kong-jwt2header

now I get a new exception which says that the plugin kong-jwt2header is enabled but not installed.

I found the kong.conf.defaults inside container in /etc/kong/ and copied it outside and added the property plugins like described in docker guide and did a mount into the container.

Now I get this exception:

2020/10/01 12:33:09 [error] 1#0: init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:484: error loading plugin schemas: on plugin 'kong-jwt2header': kong-jwt2header plugin is enabled but not installed;
module 'kong.plugins.kong-jwt2header.handler' not found:No LuaRocks module found for kong.plugins.kong-jwt2header.handler
        no field package.preload['kong.plugins.kong-jwt2header.handler']

Now I have no ideas anymore. (with exception to try it without docker and without declarative approach)

How I add a custom plugin to kong inside docker container using declarative configuration?

1

There are 1 answers

0
Daniel Kocot On

You need to install plugins with luarocks inside the container.

USER root
RUN apk update && apk add git unzip luarocks
RUN luarocks install kong-oidc  
USER kong

The example works if you use kong-latest based on Alpine.