Serverless Functions 插件

在 access 阶段从Kong动态运行Lua代码。

术语

  • plugin: 在请求被代理到上游API之前或之后,在Kong内部执行操作的插件。
  • Service: 表示外部 upstream API或微服务的Kong实体。
  • Route: 表示将下游请求映射到上游服务的方法的Kong实体。
  • upstream service: 这是指位于Kong后面的您自己的 API/service,转发客户端请求。

配置

此插件与具有以下协议的请求兼容:

  • http
  • https

该插件与无数据库模式部分兼容。

这些函数将被执行,但是如果配置的函数试图写入数据库,则写入将失败。

在 Service 上启用插件

使用数据库:

通过发出以下请求在Service上配置此插件:

  1. $ curl -X POST http://kong:8001/services/{service}/plugins \
  2. --data "name=serverless-functions" \
  3. --data "config.functions=[]"

不使用数据库:

通过添加此部分在服务上配置此插件执行声明性配置文件:

  1. plugins:
  2. - name: serverless-functions
  3. service: {service}
  4. config:
  5. functions: []

在这两种情况下,,{service}是此插件配置将定位的Service的idname

在 Route 上启用插件

使用数据库:

通过发出以下请求在 Route 上配置此插件:

  1. $ curl -X POST http://kong:8001/routes/{route}/plugins \
  2. --data "name=serverless-functions" \
  3. --data "config.functions=[]"

不使用数据库:

通过添加此部分在 Route 上配置此插件执行声明性配置文件:

  1. plugins:
  2. - name: serverless-functions
  3. route: {route}
  4. config:
  5. functions: []

在这两种情况下,,{route}是此插件配置将定位的 route 的idname

全局插件

  • 使用数据库: 可以使用http://kong:8001/plugins/配置所有插件。
  • 不使用数据库: 可以通过plugins:配置所有插件:声明性配置文件中的条目。

与任何 Service ,Route 或 Consumer (或API,如果您使用旧版本的Kong)无关的插件被视为“全局”,并将在每个请求上运行。有关更多信息,请阅读插件参考插件优先级部分。

参数

以下是可在此插件配置中使用的所有参数的列表:

参数 默认值 描述
name 要使用的插件的名称,在本例中为http-log
service_id 此插件将定位的 Service 的ID。
route_id 此插件将定位的 Route 的ID。
enabled true 是否将应用此插件。
config.functions [] 在访问阶段要缓存并按顺序运行的字符串化Lua代码数组。

插件名称

Serverless Functions 作为两个单独的插件来。每个插件在插件链中以不同的优先级运行。

  • pre-function
    • 在访问阶段其他插件运行之前运行。
  • post-function
    • 在访问阶段在其他插件之后运行。

演示

使用数据库

  1. 在Kong上创建一个 Service:

    1. $ curl -i -X POST http://localhost:8001/services/ \
    2. --data "name=plugin-testing" \
    3. --data "url=http://httpbin.org/headers"
    4. HTTP/1.1 201 Created
  2. 将 Route 添加到 Service :

    1. $ curl -i -X POST http://localhost:8001/services/plugin-testing/routes \
    2. --data "paths[]=/test"
    3. HTTP/1.1 201 Created
    4. ...
  3. 创建一个名为custom-auth.lua的文件,其内容如下:

    1. -- 获取请求头列表
    2. local custom_auth = kong.request.get_header("x-custom-auth")
    3. -- 如果我们的自定义身份验证头不存在,终止请求
    4. if not custom_auth then
    5. return kong.response.exit(401, "Invalid Credentials")
    6. end
    7. -- 从请求中删除自定义身份验证标头
    8. kong.service.request.clear_header('x-custom-auth')
  4. 确保文件内容:

    1. $ cat custom-auth.lua
  5. 使用带有cURL文件上传功能的插件来应用我们的Lua代码

    1. $ curl -i -X POST http://localhost:8001/services/plugin-testing/plugins \
    2. -F "name=pre-function" \
    3. -F "config.functions=@custom-auth.lua"
    4. HTTP/1.1 201 Created
    5. ...
  6. 测试没有任何头传递时,我们的Lua代码将终止请求:

    1. curl -i -X GET http://localhost:8000/test
    2. HTTP/1.1 401 Unauthorized
    3. ...
    4. "Invalid Credentials"
  7. 通过发出有效请求来测试我们刚刚应用的Lua代码:

    1. curl -i -X GET http://localhost:8000/test \
    2. --header "x-custom-auth: demo"
    3. HTTP/1.1 200 OK
    4. ...

不使用数据库

  1. 在声明性配置文件上创建Service,Route和Associated插件:

    1. services:
    2. - name: plugin-testing
    3. url: http://httpbin.org/headers
    4. routes:
    5. - service: plugin-testing
    6. paths: [ "/test" ]
    7. plugins:
    8. - name: pre-function
    9. config:
    10. functions: |
    11. -- Get list of request headers
    12. local custom_auth = kong.request.get_header("x-custom-auth")
    13. -- Terminate request early if our custom authentication header
    14. -- does not exist
    15. if not custom_auth then
    16. return kong.response.exit(401, "Invalid Credentials")
    17. end
    18. -- Remove custom authentication header from request
    19. kong.service.request.clear_header('x-custom-auth')
  2. 测试没有任何头传递时,我们的Lua代码将终止请求:

    1. curl -i -X GET http://localhost:8000/test
    2. HTTP/1.1 401 Unauthorized
    3. ...
    4. "Invalid Credentials"
  3. 通过发出有效请求来测试我们刚刚应用的Lua代码:

    1. curl -i -X GET http://localhost:8000/test \
    2. --header "x-custom-auth: demo"
    3. HTTP/1.1 200 OK
    4. ...

这只是这些插件所赋予功能的一个小例子。 我们能够将Lua代码动态注入插件 access 阶段,以动态终止或转换请求,而无需创建自定义插件或重新 reloading / redeployin部署Kong。 简而言之,无服务器功能可在访问阶段为您提供自定义插件的全部功能,而无需重新 reloading / redeployin启动Kong。

笔记

Upvalues

在插件的0.3版本之前,提供的Lua代码将作为函数运行。 从版本0.3开始,还可以返回一个函数,以允许升值。

因此,较旧的版本可以做到这一点(仍然适用于0.3及更高版本):

  1. -- this entire block is executed on each request
  2. ngx.log(ngx.ERR, "hello world")

使用此版本版本,您可以返回一个在每个请求上运行的函数,从而允许升值使请求之间保持状态:

  1. -- this runs once when Kong starts
  2. local count = 0
  3. return function()
  4. -- this runs on each request
  5. count = count + 1
  6. ngx.log(ngx.ERR, "hello world: ", count)
  7. end

缩小Lua

由于我们以字符串格式发送代码,因此建议使用curl文件上传@file.lua(请参见演示)或使用Minifier缩小Lua代码。