tjtjtjのメモ

自分のためのメモです

kong プラグイン書いてみた

ヘッダ 'asdf:asdf' がなければ 400 をレスポンスするプラグインを書いてみた。

ソース

/kong-plugin/kong/plugins/myplugin/handler.lua

local plugin_name = ({...})[1]:match("^kong%.plugins%.([^%.]+)")
local plugin = require("kong.plugins.base_plugin"):extend()
local kong = kong

plugin.PRIORITY = 1000

function plugin:new()
  plugin.super.new(self, plugin_name)
end

function plugin:access(plugin_conf)
  plugin.super.access(self)

  -- your custom code here
  local v = kong.request.get_header("asdf")
  if not v then
    return kong.response.exit(400, { message = "asdf is empty" })
  end 
  if v ~= "asdf" then
    return kong.response.exit(401, { message = "asdf is not asdf" })
  end 
end

return plugin

/kong-plugin/spec/myplugin/01-access_spec.lua

:
    describe("request", function()
      it("gets 200", function()
        local r = assert(client:send {
          method = "GET",
          path = "/request",  -- makes mockbin return the entire request
          headers = {
            host = "test1.com",
            asdf = "asdf"
          }
        })
        assert.response(r).has.status(200)
      end)
    end)

    describe("request", function()
      it("gets 400", function()
        local r = assert(client:send {
          method = "GET",
          path = "/request",  -- makes mockbin return the entire request
          headers = {
            host = "test1.com"
          }
        })
        assert.response(r).has.status(400)
      end)
    end)

    describe("request", function()
      it("gets 401", function()
        local r = assert(client:send {
          method = "GET",
          path = "/request",  -- makes mockbin return the entire request
          headers = {
            host = "test1.com",
            asdf = "qwer"
          }
        })
        assert.response(r).has.status(409)   <--- Failure
      end)
    end)
:

lint

lint は luacheckで。typo してみた。

vagrant@ubuntu-bionic:/kong-plugin$ luacheck .
Checking kong/plugins/myplugin/handler.lua        1 error

    kong/plugins/myplugin/handler.lua:66:12: expected 'then' near 'theeeen'

Checking kong/plugins/myplugin/schema.lua         OK
Checking spec/myplugin/01-access_spec.lua         2 warnings

    spec/myplugin/01-access_spec.lua:21:13: unused variable bp
    spec/myplugin/01-access_spec.lua:21:17: unused variable route1

Total: 2 warnings / 1 error in 3 files

typo 修正後

vagrant@ubuntu-bionic:/kong-plugin$ luacheck .
Checking kong/plugins/myplugin/handler.lua        OK
Checking kong/plugins/myplugin/schema.lua         OK
Checking spec/myplugin/01-access_spec.lua         2 warnings

    spec/myplugin/01-access_spec.lua:21:13: unused variable bp
    spec/myplugin/01-access_spec.lua:21:17: unused variable route1

Total: 2 warnings / 0 errors in 3 files

テスト

テストは bin/busted

$ cd /kong
$ bin/busted /kong-plugin/spec
●●◼●●◼
4 successes / 2 failures / 0 errors / 0 pending : 13.785849 seconds

Failure → /kong-plugin/spec/myplugin/01-access_spec.lua @ 109
myplugin: (access) [#postgres] request gets 401
/kong-plugin/spec/myplugin/01-access_spec.lua:118: Invalid response status code.
Status expected:
(number) 409
Status received:
(number) 401
Body:
(string) '{"message":"asdf is not asdf"}'
409

Failure → /kong-plugin/spec/myplugin/01-access_spec.lua @ 109
myplugin: (access) [#cassandra] request gets 401
/kong-plugin/spec/myplugin/01-access_spec.lua:118: Invalid response status code.
Status expected:
(number) 409
Status received:
(number) 401
Body:
(string) '{"message":"asdf is not asdf"}'
409

動かしてみる

make し kong 起動

$ cd /kong
$ make dev
$ bin/kong start

ヘッダ asdf なし

$ curl -i http://localhost:8000
HTTP/1.1 400 Bad Request
Date: Fri, 04 Oct 2019 12:05:11 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Content-Length: 27
Server: kong/1.3.0

{"message":"asdf is empty"}

ヘッダ asdf:zxcv

$ curl -i -H "asdf:zxvc" http://localhost:8000
HTTP/1.1 401 Unauthorized
Date: Fri, 04 Oct 2019 12:07:34 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Content-Length: 30
Server: kong/1.3.0

{"message":"asdf is not asdf"}

ヘッダ asdf:asdf

$ curl -i -H "asdf:asdf" http://localhost:8000
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 967
Connection: keep-alive
Server: openresty/1.15.8.2
Date: Fri, 04 Oct 2019 12:08:12 GMT
:

kong、なんとかなるものだな。