Invoke lua script on EnRoute Standalone Gateway

Invoke lua script on EnRoute Standalone Gateway

January 22, 2021

EnRoute Universal Gateway

EnRoute Universal Gateway is a flexible API gateway built to support traditional and cloud-native use cases. It is designed to run either as an Kubernetes Ingress Gateway, Standalone Gateway, Horizontally scaling L7 API gateway or a Mesh of Gateways. EnRoute can support a wide range of [topologies](/blog/enroute-topologies/). Depending on the need of the user, the environment, the application, either one or many of these solutions can be deployed.

A consistent policy framework across all these network components makes the EnRoute Universal Gateway a versatile and powerful solution.

This article covers how to get started with the EnRoute Standalone Gateway.

To get a more detailed understanding of EnRoute Universal Gateway refer to the article here

What this article covers.

Setting up EnRoute gateway is shown in four simple steps. This is the third step where a lua script is attached to a service object. This lua script is invoked on request and response path.

Create lua filter

First we create a filter of type http_filter_lua

curl -s -X POST localhost:1323/filter \
     -d 'Filter_name=lua_filter_demo' \
     -d 'Filter_type=http_filter_lua'
{
  "data": {
    "insert_saaras_db_filter": {
      "affected_rows": 1
    }
  }
}

Next we create a file to hold lua script

cat >script.lua <<EOL
function envoy_on_request(request_handle)
   request_handle:logInfo("Hello World request");
end

function envoy_on_response(response_handle)
   response_handle:logInfo("Hello World response");
end
EOL
curl -X POST \
  -F 'Config=@script.lua' \
  http://localhost:1323/filter/lua_filter_demo/config
{
  "data": {
    "insert_saaras_db_filter": {
      "affected_rows": 1
    }
  }
}

Next we attach the lua filter to the service

curl -s -X POST localhost:1323/service/demo/filter/lua_filter_demo
{
  "data": {
    "insert_saaras_db_service_filter": {
      "affected_rows": 3
    }
  }
}

Dump service to see lua filter is attached to the service

curl -s localhost:1323/service/dump/demo
{
  "data": {
    "saaras_db_service": [
      {
        "service_id": 1,
        "service_name": "demo",
        "fqdn": "enroute.local",
        "create_ts": "2021-01-25T19:08:57.670403+00:00",
        "routes": [
          {
            "route_id": 1,
            "route_name": "gs_route",
            "route_upstreams": [
              {
                "upstream": {
                  "upstream_id": 1,
                  "upstream_name": "server1",
                  "upstream_ip": "127.0.0.1",
                  "upstream_port": 50051
                }
              }
            ],
            "route_filters": [],
            "route_prefix": "/"
          }
        ],
        "service_secrets": [
          {
            "secret": {
              "secret_id": 1,
              "secret_name": "enroute_secret",
              "secret_sni": "",
              "secret_key": "-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC8NH3o331E+NMI\nAU50Yb8jLLMXT21cn5B9OY1zXwNwpggzIt9QjkuXYxEZ3kkjnmIORL3KniYS7rbN\nyG0XjQQOwr6wZt07cyC0u1GuXyb/CaDfd5K4DujhWJyQ2VJovhMMmRck99BDfj23\n1xPl+ANDYfzMNvk4cDul41ghbSMND9SLw6w9eGu8oDLaQqqzuyqz8BvSOtnekKiA\n+7dc/Wdacf6yq/Z2+lwR8gC5tfesCO0I3hwOR7l7AZxBvlLtDo+Wv6PHSxQORCSI\nUB0+f0ZhJG/FTNxcsChZBoBI6amLgnBtDtYTQms4kjJBt1ud3hdpCr6KwQcUu9tL\n4YS+ecozAgMBAAECggEBAKUNl/qZvV+2pERZ+PcCHkF32xwvUSaw3WOlDJNImVi6\nk2hdReF82XhLrly0YAo3xaDj0lqWtadW6MSx6FKGHi/fxKIppA2WGvHFcY3t7uIu\nDILOd8KpeLzgHHa/0JEBWO1seSq3llMKrI/EtLOy/RGkJrLl500CyJzkM48lCl9R\npn8KDzb5KQKdVvb+7fM7kvNh1DtWqXReULdR2tGqW30K8LCJrC7OYfpUE1BaRsyz\nNpO7XYSIGOi50tUa7gCVuY4aNJ+22w6UX1n0yGH2ogyksR/KjoN7uQR5p2es/bLi\nHM+V/fIe+amK14fLZMxUq3wNzm4ESU5+cl21TDFWLmECgYEA9Jr1xxaY96ILbIru\nPoP8kPnhjDs8ZGidYha1UFccBLHrs6skoYVRk3Msl2H69jQMaCT8Ju+b5HJ3pUqh\n5jRxZ47UPSE391w+OvowcRPGU3E2SUeyiF4qVtVdDZXWX16P5okjfaRFO/BWcHUP\nPrdxzSMMfYtz96+iyaQJhixA0w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wcmluZ2ZpZWxkMQwwCgYDVQQKDANEaXMxFjAUBgNV\nBAMMDWVucm91dGUubG9jYWwwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB\nAQC8NH3o331E+NMIAU50Yb8jLLMXT21cn5B9OY1zXwNwpggzIt9QjkuXYxEZ3kkj\nnmIORL3KniYS7rbNyG0XjQQOwr6wZt07cyC0u1GuXyb/CaDfd5K4DujhWJyQ2VJo\nvhMMmRck99BDfj231xPl+ANDYfzMNvk4cDul41ghbSMND9SLw6w9eGu8oDLaQqqz\nuyqz8BvSOtnekKiA+7dc/Wdacf6yq/Z2+lwR8gC5tfesCO0I3hwOR7l7AZxBvlLt\nDo+Wv6PHSxQORCSIUB0+f0ZhJG/FTNxcsChZBoBI6amLgnBtDtYTQms4kjJBt1ud\n3hdpCr6KwQcUu9tL4YS+ecozAgMBAAGjUDBOMB0GA1UdDgQWBBTQD5zqF76iNbt3\ne4tOf5qLPMlsmjAfBgNVHSMEGDAWgBTQD5zqF76iNbt3e4tOf5qLPMlsmjAMBgNV\nHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQB9Msm72ts7cqHgpALbiq/hmbwR\n4JLlI3GvtKzZvnEjbZVn102Whq/e/bddnpvs4GvJLifN0pLlo1x02WPvnDX0Sisu\np5Ko2CrOUfNglAUveP/hPXR94jTia2T9rZX+3N8RRDPdgctV8GfxWOyAwc4cybkz\nNc2MK51cwWiu+C2AxX4GnM9FZnmqETuPcPexXkGBZl0mKVPW3tYE7Tml3ooUE6yQ\nF0pY+YdNJUTtJcxc1sqNh+LkHf0sjpzTrv1JnHJt/IGwj1+KbUqCRObAeszyh78E\nunwFGokTtWUTiCUQaZ7bRemvnMHbEsqYmAvQiSFOQHwnxDyOl1gfSmPAS/JZ\n-----END CERTIFICATE-----\n"
            }
          }
        ],
        "service_filters": [
          {
            "filter": {
              "filter_name": "lua_filter_demo",
              "filter_type": "http_filter_lua"
            }
          }
        ]
      }
    ]
  }
}
Send traffic

Send a request to the listener

curl -k -vvv https://enroute.local:8443 --resolve enroute.local:8443:127.0.0.1
Check envoy stats
curl -k -vvv http://localhost:9001/stats

Next steps

EnRoute standalone gateway provides simple APIs to configure Envoy proxy. Additionally you can -