kong.request.get_uri_captures() has no data
cs1hsb opened this issue · 10 comments
Hi
Trying to use kong.request.get_uri_captures()
as described here and here
(I think ngx.ctx.router_matches
is out of date?)
I am doing this using a python plugin running in a KONG docker container.
When I try and call kong.request.get_uri_captures()
I get the KONG error:
023/05/26 07:34:15 [error] 1256#0: *610 [kong] mp_rpc.lua:347 [py-test-capture-plugin] no data, client: 192.168.224.1, server: kong, request: "GET /apiv1/students/2000 HTTP/1.1", host: "localhost:8000"
My plugin works fine for other operations and functionality and the matched group
regex path I have configured for the Route in kong.yml
seems to correctly pass through requests to the plugin.
(I'm sure I'm also getting the mapping from lua to pyhon wrong for the return captures table
but I would have thought I would progress past kong.request.get_uri_captures()
in my plugin...)
I'm using kong:3.3.0-alpine
docker hub image and I can see that kong-python-pluginserver 0.33
is running in the container.
Running KONG on debug with CMD ["kong", "start", "--vv"]
hasn't given me any clues that I can see either.
Any help/advice greatly appreciated. thanks :)
FILES
py-test-capture-plugin.py
#!/usr/bin/env python3
import os
import kong_pdk.pdk.kong as kong
Schema = (
{"message": {"type": "string"}},
)
version = '0.1.0'
priority = 0
class Plugin(object):
def __init__(self, config):
self.config = config
def access(self, kong: kong.kong):
try:
kong.log("--------------------------------------------------------------------")
kong.log("--------------------------")
# errors here
captures = kong.request.get_uri_captures()
kong.log("--------------------------")
named = captures.named
kong.log("--------------------------")
kong.log(str(named.items()));
kong.log("--------------------------")
# below all works when above code commented out
kong.service.request.set_header("x-hello-from-python", kong.request.get_path())
kong.service.request.set_header("x-python-pid", str(os.getpid()))
params = {"wooId" : "132323323", "cardId" : "888888"}
kong.log("--------------------------------------------------------------------")
kong.service.request.set_query(params)
except Exception as ex:
kong.log.error(ex)
# add below section to allow this plugin optionally be running in a dedicated process
if __name__ == "__main__":
from kong_pdk.cli import start_dedicated_server
start_dedicated_server("hello-plugin", Plugin, version, priority, Schema)
kong.yml
_format_version: "3.0"
_transform: true
services:
- name: test-api-1
url: http://postman-echo.com/get
plugins:
routes:
- name: test-api-1-students
paths:
- ~/apiv1/students/(?<studentid>\d+)
plugins:
- name: py-test-capture-plugin
Dockerfile-kong
FROM kong:3.3.0-alpine
USER root
# Example for Python
# PYTHONWARNINGS=ignore is needed to build gevent on Python 3.9
RUN apk update && \
apk add python3 py3-pip python3-dev musl-dev libffi-dev gcc g++ file make && \
PYTHONWARNINGS=ignore pip3 install kong-pdk
docker-compose.yml
version: '3.1'
services:
kong:
container_name: kong
build:
dockerfile: Dockerfile-kong
volumes:
# declarative db-less config
- ./kong/config:/etc/kong/declarative
# custom plugins
- ./kong/py-plugins:/opt/conf/kong-py-plugins
environment:
# Use the config defined in kong.yml
KONG_DATABASE: "off"
KONG_DECLARATIVE_CONFIG: /etc/kong/declarative/kong.yml
# Show all output in the terminal when running with Docker
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_ADMIN_LISTEN: 0.0.0.0:8001, 0.0.0.0:8444 ssl
# Enable the py plugin server
KONG_PLUGINSERVER_NAMES: py
KONG_PLUGINSERVER_PY_SOCKET: /usr/local/kong/python_pluginserver.sock
KONG_PLUGINSERVER_PY_START_CMD: "/usr/bin/kong-python-pluginserver --no-lua-style --plugins-directory /opt/conf/kong-py-plugins"
KONG_PLUGINSERVER_PY_QUERY_CMD: "/usr/bin/kong-python-pluginserver --no-lua-style --plugins-directory /opt/conf/kong-py-plugins --dump-all-plugins"
# Allow plugins to be used. The plugin name is your py file name
KONG_PLUGINS: "bundled, py-test-capture-plugin"
ports:
- 8000:8000
- 8443:8443
- 8001:8001
- 8444:8444
networks:
- api-network
networks:
api-network:
Test request
curl -i --request GET 'http://localhost:8000/apiv1/students/2000'
Hi - could someone help here please?
Many thanks