天天看點

KONG網關 — 插件開發

雖然kong提供了那麼多的插件,但是不可能百分百滿足了對于網關的需求,在很多系統中不可能吧使用者體系用到kong提供的權限驗證體系,包括對于一套系統完整的RBAC系統大家都有不同的改進,以及請求加密參數校驗等等,是以對于擴充性對于網關的定義需要有一定的能力的,kong是通過nginx+lua開發的通過lua腳本就可以擴充插件來完成網關的目的

附上:

喵了個咪的部落格:

w-blog.cn kong官網: https://konghq.com/ konga官網: https://github.com/pantsel/konga

PS:Kong版本必須 >= 1.0.0才能正常使用konga

一,開發環境準備

首先需要開發環境,docker并不适合作為開發環境來使用,容器重新開機内容丢失等問題對kong-lua插件開發來說不友好,容器内部的結構也存在差別,是以我們需要使用liunx機器來進行開發

wget https://bintray.com/kong/kong-community-edition-rpm/download_file?file_path=centos/7/kong-community-edition-1.0.3.el7.noarch.rpm           
sudo yum install epel-release
sudo yum install kong-community-edition-1.0.3.el7.noarch.rpm --nogpgcheck           
kong version
1.0.3           

準備資料庫

mkdir -p /app/docker/postgres
cd /app/docker/postgres/
vim docker-compose.yml           
version: '2.1'
services:
  db:
    image: postgres:9.6
    environment:
      POSTGRES_DB: kong
      POSTGRES_PASSWORD: kong
      POSTGRES_USER: kong
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "kong"]
      interval: 30s
      timeout: 30s
      retries: 3
    restart: on-failure
    stdin_open: true
    tty: true
    ports:
      - "5432:5432/tcp"           
docker-compose up -d           

初始化配置檔案

cp /etc/kong/kong.conf.default /etc/kong/kong.conf
vim /etc/kong/kong.conf           

按照之前營運容器時候注入的環境變量來修改配置檔案

admin_access_log = logs/admin_access.log
admin_error_log = logs/error.log 
admin_listen = 0.0.0.0:8001
database = postgres
pg_database = kong
pg_host = 127.0.0.1
pg_password = kong
pg_user = kong           

啟動kong,初始化資料庫

kong migrations bootstrap -c /etc/kong/kong.conf           

啟動kong

kong start -c /etc/kong/kong.conf           

通路kong

curl localhost:8001           

運作konga

docker run -p 1337:1337 \
             --name konga \
             -e "NODE_ENV=production" \
             pantsel/konga:0.14.1           

最後就是使用你常用的idea裝好lua擴充就可以了

二、開發擴充

這兩個檔案是必須有的

handler.lua   // 一個實作的接口。每個函數都由Kong在請求的生命周期中的所需時刻運作。
 schema.lua    // 儲存插件配置的架構,以便使用者隻能輸入有效的配置值。           

還有其他的擴充檔案

complete-plugin
├── api.lua             // 定義Admin API中可用的端點清單,以與插件處理的實體自定義實體進行互動。
├── daos.lua            // 定義DAO(資料庫通路對象)清單,這些DAO是插件所需并存儲在資料存儲區中的自定義實體的抽象。
├── handler.lua         
├── migrations          // 給定資料存儲的相應遷移。隻有當您的插件必須在資料庫中存儲自定義實體并通過 daos.lua定義的其中一個DAO與它們進行互動時,才需要進行遷移。
│ ├── cassandra.lua
│ └── postgres.lua
└── schema.lua           
mkdir http-rewrite
cd http-rewrite
vim handler.lua
vim schema.lua           
return {
    no_consumer = true,
    fields = {
        regex = { type = "string" },
        replacement = { type = "string" },
        flag = {type = "string"},
    },
    self_check = function(schema, plugin_t, dao, is_update)
        -- TODO: add check
        return true
    end
}           
local BasePlugin = require "kong.plugins.base_plugin"
local json = require("cjson")
local RewriteHandler = BasePlugin:extend()
local ngx = ngx

RewriteHandler.PRIORITY = 2000
RewriteHandler.VERSION = "0.1.0"

-- 傳入參數conf是這個插件存放在資料庫中配置
function RewriteHandler:access(conf)
    RewriteHandler.super.access(self)

    local host = ngx.var.host
    ngx.log(ngx.DEBUG, "http-rewrite plugin, host is: ", host, " ,uri is: ",
        ngx.var.request_uri, " ,config is: ", json.encode(conf))

    local replace,n,err  = ngx.re.sub(ngx.var.request_uri, conf.regex, conf.replacement)
    if replace and n == 0 then
        return
    end

    if err then
        ngx.log(ngx.ERR, "http-rewrite plugin, ngx.re.sub err: ",err, " ,host is: ", host, " ,uri is: ",
            ngx.var.request_uri, " ,config is: ", json.encode(conf))
        return
    end

    ngx.log(ngx.DEBUG, "http-rewrite plugin, replace is: ",replace)
    if conf.flag == "redirect" then
        ngx.redirect(replace,302)
    elseif conf.flag == "permanent" then
        ngx.redirect(replace,301)
    end
end

function RewriteHandler:new()
    RewriteHandler.super.new(self, "http-rewrite")
end

return RewriteHandler           
## 在使用新插件之前,需要更新一下資料庫:
kong migrations up -c /etc/kong/kong.conf
kong start -c /etc/kong/kong.conf           

開啟插件

此時就開啟了我們自己開發的插件了

也可以通過konga工具開啟

配置可以在其中看到

此時通路

http://172.16.1.82:8000/abc/test

會被跳轉到

http://172.16.1.82:8000/redirect//test

到這裡就已經完成了一個基礎插件的開發,更多的一些系統函數可以通過官方插件開發手冊來使用