Git Product home page Git Product logo

kong-dynamic-upstream's Introduction

Build Status

kong-dynamic-upstream

A Kong plugin that sets different upstream URLs based on API and Consumer.

The path of the user's request to the API will be appended to the end of the replacement URL. The strip_request_path parameter will be respected.

A request to an API at /foo that is set to strip the request path, http://kong/foo/bar, by a consumer with the dynamic-upstream plugin configured with a replacement_url of https://snafu:4242 will be directed to https://snafu:4242/bar.

The replacement URL must contain:

  • a scheme, eg. HTTPS
  • a hostname, eg. mockbin.com

Optionally you can include:

  • a port, eg. :8080
  • a path, eg. /path

Any existing path in the upstream_url configured for the API will not be replaced by the plugin. For example, an API with an upstream_url of http://www.google.com/foo and a dynamic-upstream plugin with a replacement_url of http://mockbin.com/bin/1234 will route consumers to http://mockbin.com/bin/1234/foo{request_path}.

Some usage examples of valid replacement URLs:


Installation

Clone the repository, navigate to the root folder and run:

make install

Edit your kong.yaml to include the plugin like so:

custom_plugins:
  - dynamic-upstream

Restart Kong.

Development

For a detailed guide on Kong plugin development, check out the official documentation at https://getkong.org/docs/0.8.x/plugin-development/.

This plugin requires Lua 5.1 and the associated version of luarocks. Once you have these installed, to get the development dependencies run the following command:

make dev

Configuration

This plugin can work when attached to All Consumers but really only makes sense when added to a specific Consumer to an API. You can add this plugin to an API and Consumer by making the following request to your Kong server:

$ curl -X POST http://kong:8001/apis/{api}/plugins \
    --data "name=dynamic-upstream" \
    --data "consumer_id=6e62b030-c931-4c72-a012-6be18df81d49" \
    --data "config.replacement_url=http://newupstream:4353/path"

api: The id or name of the API that this plugin configuration will target

Form Parameter Required Description
name required The name of the plugin to use, in this case: dynamic-upstream
consumer_id optional The id of the Consumer to redirect
config.replacement_url required The URL to replace the default upstream specified in the API

Usage Examples

This table details the expected behaviour of the plugin for most of the possible combinations of API and dynamic-upstream plugin configurations.

API upstream_url API request_path strip_request_path Requested Path dynamic-upstream replacement_url Upstream
http://google.com /foo true /foo https://localhost:9999 https://localhost:9999
http://google.com /foo false /foo https://localhost:9999 https://localhost:9999/foo
http://google.com /foo true /foo/bar https://localhost:9999 https://localhost:9999/bar
http://google.com /foo false /foo/bar https://localhost:9999 https://localhost:9999/foo/bar
http://google.com /foo true /foo https://mockbin.com/bin/1234 https://mockbin.com/bin/1234
http://google.com /foo false /foo https://mockbin.com/bin/1234 https://mockbin.com/bin/1234/foo
http://google.com /foo true /foo/bar https://mockbin.com/bin/1234 https://mockbin.com/bin/1234/bar
http://google.com /foo false /foo/bar https://mockbin.com/bin/1234 https://mockbin.com/bin/1234/foo/bar
http://google.com/teapot /foo true /foo https://localhost:9999 https://localhost:9999/teapot
http://google.com/teapot /foo false /foo https://localhost:9999 https://localhost:9999/teapot/foo
http://google.com/teapot /foo true /foo/bar https://localhost:9999 https://localhost:9999/teapot/bar
http://google.com/teapot /foo false /foo/bar https://localhost:9999 https://localhost:9999/teapot/foo/bar
http://google.com/teapot /foo true /foo https://mockbin.com/bin/1234 https://mockbin.com/bin/1234/teapot
http://google.com/teapot /foo false /foo https://mockbin.com/bin/1234 https://mockbin.com/bin/1234/teapot/foo
http://google.com/teapot /foo true /foo/bar https://mockbin.com/bin/1234 https://mockbin.com/bin/1234/teapot/bar
http://google.com/teapot /foo false /foo/bar https://mockbin.com/bin/1234 https://mockbin.com/bin/1234/teapot/foo/bar

kong-dynamic-upstream's People

Contributors

dibble avatar markyjones avatar nicklanng avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.