A Kong plugin for string replacements in the response stream and response headers. This could be useful e.g. to replace internal by external URLs.
Run:
luarocks install *.rockspec
Then in the kong.yml add
custom_plugins:
- kong-response-string-replace
Run kong reload or start and add the plugin as normal.
Derive your kong images FROM kong
and add something like
FROM kong
USER root
RUN apk update && apk add git zlib zlib-dev libc-dev gcc
RUN git config --global advice.detachedHead false
RUN git clone https://github.com/dsteinkopf/kong-response-string-replace
RUN cd kong-response-string-replace && luarocks install *.rockspec
Depending on target distribution, you may need to install shared libs for botli
lua module. For example, botli lua module and shared libs can be installed like this:
RUN apk update && apk add brotli libbrotli1 libbrotli-dev
RUN luarocks install luajit-brotli
NOTE: This case was was experienced using 'bitnami/kong:2.1.4' kong bundle so it may be different for other distributions/bundles
Then put KONG_CUSTOM_PLUGINS: kong-response-string-replace
into your environment when starting the kong container.
This plugin's priority is set to 1250. So it is handled after ip-restriction, bot-detection, cors and after kong-http-to-https-redirect - but before jwt and other authentication plugins (see last paragraph in Kongo Plugin Documentation - Custom Logic).
-
content_types
:- type: list of strings
- default value: empty (= no replacement)
- example:
text/html
- List of content types the replacements should be done on. Exact match (not pattern patch) is done without charset part of content type.
-
uri_patterns
:- type: list of strings
- default value: empty (= no replacement)
- example:
%.html$
- List of patterns that match URIs of requests where the replacements shall be done.
-
body_replace_patterns
:- type: list of strings formed
PATTERN###REPLACEMENT
- default value: empty (= no body replacements to be done)
- example:
my ugly text###my nice text
,https%:%/%/internal%-server%.local%:8888###https://external-server.my.tld
- List of pattern/replacement pairs.
Patterns are Lua patterns.
So they are case sensitive.
Spaces are not ignored.
Remember to quote special characters like
.
.
- type: list of strings formed
-
header_replace_patterns
:- type: list of strings formed
HEADERNAME:PATTERN###REPLACEMENT
- default value: empty (= no header replacements to be done)
- example:
Set-Cookie:internal%-server%.local###external-server.my.tld
,Server:Apache.*$###HiddenServerName
- List of header name/pattern/replacement groups.
Patterns are Lua patterns.
So they are case sensitive.
Spaces are not ignored.
Remember to quote special characters like
.
. Header names must match exactly - case sensitive.
- type: list of strings formed
Note: Replacements (on headers and body) are done on all requests that match either the content_types or the replace_uri_patterns. So they should not be both empty.
Thanks to the creator of https://github.com/HappyValleyIO/kong-http-to-https-redirect.