Skip to content

dsteinkopf/kong-response-string-replace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kong-response-string-replace

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.

Installation

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.

Docker installation

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.

Info

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).

Configuration

  • 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 ..
  • 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.

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.

Misc

Thanks to the creator of https://github.com/HappyValleyIO/kong-http-to-https-redirect.

About

A Kong plugin for string replacements in the response stream

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published