How to do conditional request termination with kong api-gateway?

1.6k Views Asked by At

I am using kong API-gateway for API management.

Suppose I have a service named alpha. I am serving kong on port 80 and alpha on port 8000 both in the same docker network named kong-net and each on a different docker container, one named kong and the other named alpha

I am using kong in a declarative DB-less mode. So my configuration should be as below in kong.yml:

_format_version: "2.1"
_transform: true

services:
  - name: alpha-live
    host: alpha
    port: 8000
    protocol: http
    path: /live
    routes:
      - name: alpha-live
        methods:
          - GET
        paths:
          - /alpha/live
        strip_path: true

What I am looking for is that when a request is received by http://kong/alpha/live

decide to either

  • terminate the request OR
  • pass it http://alpha:8000/live

based on some conditions (probably coming from SLA metrics) set on the content of the request. it might be a key, value in the header, body, etc

I there a way to do it?

There is this plugin request-termination https://docs.konghq.com/hub/kong-inc/request-termination/ but cannot use conditions.

Any idea?

2

There are 2 best solutions below

3
On

You can use the request-termination plugin because it able to be handled by trigger which can be part of the header, have a look at https://docs.konghq.com/hub/kong-inc/request-termination/#new-in-210. An example could look like this;

plugins:
- name: request-termination
  config: 
    status_code: 403
    message: So long and thanks for all the fish!
    trigger: condition-header
0
On

You can always write your own plugin in any of the languages that kong supports for writing custom plugins. As of now lua (duh), golang, js and python are supported and development is enabled via their own respective plugin development kits (PDKs) released by kong (https://docs.konghq.com/gateway/latest/reference/external-plugins/) Also to help you accomplish running your plugins at various stages (or phases as kong calls it) of the request lifecycle, there are different methods you can implement which are very well documented in the official doc

  • Certificate
  • Rewrite
  • Access
  • Response
  • Preread
  • Log

If you are familiar with golang, I'd suggest you go ahead with that as it's fairly straight forward with very minimal code. I've written one in my own use case for terminating any requests that arrive before 5s of the previous one. You can write your logic to accomplish the same with any condition you require. Although deploying it might be a challenge (because for some reason even though the pdks exist in other languages, the documentation is very much limited to scripting in lua), even I was stuck with the same trying to deploy one on a kubernetes cluster via helm, but if you're working with docker, the dockerfile and the links provided in the question of the link below would be enough for you. Hope this helps !!

Kong custom golang plugin not working in kubernetes/helm setup