0

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?

Amin Ba
  • 1,603
  • 1
  • 13
  • 38
  • related question https://stackoverflow.com/questions/72118230/how-to-solve-kong-first-reqtest-delay-problem-how-to-restrict-access-to-some-ro – Amin Ba May 08 '22 at 16:49

2 Answers2

0

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
  • You are right. When not set, the plugin always activates. When set to a string, the plugin will activate exclusively on requests containing either a header or a query parameter that is named the string. can you provide an example yml that examines a condition and terminates a request? – Amin Ba May 23 '22 at 02:01
  • You will find an example based on the plugins part that you have to add to your example in the answer ;). – Daniel Kocot May 23 '22 at 22:16
  • well, what does `condition-header` mean? what condition? – Amin Ba May 24 '22 at 23:52
0

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

Chalukya J
  • 31
  • 4