Reputation: 1
using docker to run the Kong ,I am created the custom Plugin To Validate the header
Key :Test123
using docker to run the Kong ,I am created the custom Plugin To Validate the header
Key :Test123
\---------------------------handler.lua-------------------------------------------
local kong = kong
local CustomPlugin = {
PRIORITY = 1000,
VERSION = “0.1”,
}
function CustomPlugin:access(conf)
CustomPlugin.super.access(self)
– Validate the static header
local header_value = kong.request.get_header(“Key”)
if header_value \~= “Test123” then
– Return an error response if the header is missing or invalid
return kong.response.exit(401, { message = “Unauthorized: Invalid or missing ‘Key’ header” })
end
– If the header is valid, the request will continue to the upstream service
kong.log.debug(“Header validation passed. Continuing to the upstream service.”)
end
return CustomPlugin
\--------------------------------------schema--------------------------------------
local typedefs = require “kong.db.schema.typedefs”
return {
name = “custom-plugin”,
fields = {
{ config = {
type = “record”,
fields = {}
} }
}
}
\----------------------------/etc/kong/kong.conf--------------------------------------------
plugins = bundled,custom-plugin
Used cmd
docker cp ./custom-plugin c3b65b77458c:/usr/local/share/lua/5.1/kong/plugins
docker cp ./kong c3b65b77458c:/usr/local/etc
and restart the container but Not working any solution?
Upvotes: 0
Views: 131
Reputation: 3363
Kong has some really good documentation on creating new plugins, I suggest you follow that: https://docs.konghq.com/gateway/latest/plugin-development/get-started/
I took your plugin and got it to show in the plugin list and tested its logic too, I would do the following:
NOTE: I ran this on Kong OSS
CustomPlugin
and custom-plugin
if you want and copy your files across. I made a few changes that werent right (and be careful of Windows characters for -
and "
)Firstly this line needs to change if header_value \~= “Test123” then
to if header_value ~= “Test123” then
And secondly in your schema.lua I would follow the format and defaults from the project above
local typedefs = require "kong.db.schema.typedefs"
local PLUGIN_NAME = "custom-plugin"
local schema = {
name = PLUGIN_NAME,
fields = {
-- the 'fields' array is the top-level entry with fields defined by Kong
{ consumer = typedefs.no_consumer }, -- this plugin cannot be configured on a consumer (typical for auth plugins)
{ protocols = typedefs.protocols_http },
{ config = {
-- The 'config' record is the custom part of the plugin schema
type = "record",
fields = {},
},
},
},
}
return schema
I changed the Dockerfile to use OSS version of Kong instead
FROM kong/kong:3.7
Build the new image using the following command
docker build --force-rm --no-cache -t kong-gateway_my-plugin:3.7-0.0.1 .
You can bring everything up using KONG_DATABASE=postgres KONG_DOCKER_TAG=kong-gateway_my-plugin:3.7-0.0.1 docker compose --profile database up -d
. The KONG_PLUGINS
env var is being set in the image so no need to set it in your docker compose
I have tested this and it works and I can see your plugin in Kong Manager. I created a simple service with the plugin and it does what you expect as well
Here is the Kong YAML
_format_version: "3.0"
services:
- connect_timeout: 60000
enabled: true
host: httpbin.org
name: httpbin
path: /anything
port: 443
protocol: https
read_timeout: 60000
retries: 5
routes:
- https_redirect_status_code: 426
name: httpbin
path_handling: v0
paths:
- /demo
plugins:
- enabled: true
name: custom-plugin
protocols:
- grpc
- grpcs
- http
- https
preserve_host: false
protocols:
- http
- https
regex_priority: 0
request_buffering: true
response_buffering: true
strip_path: true
write_timeout: 60000
And testing it
~ % http :8000/demo
HTTP/1.1 401 Unauthorized
Connection: keep-alive
Content-Length: 63
Content-Type: application/json; charset=utf-8
Date: Thu, 29 Aug 2024 07:37:37 GMT
Server: kong/3.7.1
X-Kong-Request-Id: 791727449efce37e9480880c361b4d5a
X-Kong-Response-Latency: 0
{
"message": "Unauthorized: Invalid or missing ‘Key’ header"
}
~ % http :8000/demo Key:Test12
HTTP/1.1 401 Unauthorized
Connection: keep-alive
Content-Length: 63
Content-Type: application/json; charset=utf-8
Date: Thu, 29 Aug 2024 07:37:43 GMT
Server: kong/3.7.1
X-Kong-Request-Id: 3152863624953f23f08e1d01d81b2304
X-Kong-Response-Latency: 2
{
"message": "Unauthorized: Invalid or missing ‘Key’ header"
}
~ % http :8000/demo Key:Test123
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 590
Content-Type: application/json
Date: Thu, 29 Aug 2024 07:37:45 GMT
Server: gunicorn/19.9.0
Via: kong/3.7.1
X-Kong-Proxy-Latency: 3
X-Kong-Request-Id: b52ddf6fcaa90115e3018d6d01ac0674
X-Kong-Upstream-Latency: 490
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"Key": "Test123",
"User-Agent": "HTTPie/3.2.2",
"X-Amzn-Trace-Id": "Root=1-66d02549-78154f64339407db0d2d580f",
"X-Forwarded-Host": "localhost",
"X-Forwarded-Path": "/demo",
"X-Forwarded-Prefix": "/demo",
"X-Kong-Request-Id": "b52ddf6fcaa90115e3018d6d01ac0674"
},
"json": null,
"method": "GET",
"origin": "192.168.65.1, 82.132.213.98",
"url": "https://localhost/anything"
}
Upvotes: 0