'how to protect some of kong routes from public access

I have this architecture for three microervices as below:

user <===> kong <===> alpha <===> kong <===> beta <===> kong <===> gamma

Because I want the inter-service communication to go through kong, I need to create routes for it:

_format_version: "2.1"
_transform: true

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

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

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

from within the alpha service, I use python's requests library to call an endpoint of beta that use python's requests library to call /live endpoint of gamma as below

alpha

@api_view(["GET"])
def beta_gamma_live(request):

    res_kong = requests.get("http://kong:8000/beta/gamma/live")

    return Response({
        "chained-alpha-beta-gamma-status-check-through-kong": res_kong.status_code,
    }, status.HTTP_200_OK)

beta

@api_view(["GET"])
def gamma_live(request):

    res_kong = requests.get("http://kong:8000/gamma/live")

    return Response({
        "chained-beta-gamma-status-check-through-kong": res_kong.status_code,
    }, status.HTTP_200_OK)

gamma

@api_view(["GET"])
def live(request):
    return Response({"status": "Success"}, status.HTTP_200_OK)

However, I want beta-gamma-live to be only accessible from within alpha, and gamma-live only accessible to be only accessible from within beta

How can I achieve this?

I don't want beta-gamma-live to answer to (be accessible by) anybody but alpha, and gamma-live to answer to (be accessible by) anybody but beta.

This is a related question I have: large delay in kong api-gateway: first request and all requests in case of interservice calls



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source