编译
在kong的镜像中使用的是alpine linux,在linux上build的.so文件和go-pluginserver无法在容器中运行,需要使用golang:alpine来build
$ apk add gcc --update-cache --repository http://mirrors.ustc.edu.cn/alpine/v3.4/main/ --allow-untrusted# 编译go-pluginserver$ GOPROXY=https://goproxy.cn go get -d -v github.com/Kong/go-pluginserver$ GOPROXY=https://goproxy.cn CGO_ENABLED=0 go build github.com/Kong/go-pluginserver# 编译插件$ apk add libc-dev --update-cache --repository http://mirrors.ustc.edu.cn/alpine/v3.4/main/ --allow-untrusted$ GOPROXY=https://goproxy.cn go build -buildmode plugin auth-plugin.go
直接使用golang:alpine来做镜像
FROM golang:alpine as builderRUN apk add --no-cache git gcc libc-devRUN go get github.com/Kong/go-pluginserverRUN mkdir /go-pluginsCOPY go-key-checker.go /go-plugins/go-key-checker.goRUN go build -buildmode plugin -o /go-plugins/go-key-checker.so /go-plugins/go-key-checker.goFROM kong:2.0.1-alpineCOPY --from=builder /go/bin/go-pluginserver /usr/local/bin/go-pluginserverRUN mkdir /tmp/go-pluginsCOPY --from=builder /go-plugins/go-key-checker.so /tmp/go-plugins/go-key-checker.soCOPY config.yml /tmp/config.ymlUSER rootRUN chmod -R 775 /tmpUSER kong
Go-plugin
package mainimport ("github.com/Kong/go-pdk")// it represents to config parameters into the config.ymltype Config struct {Apikey string}func New() interface{} {return &Config{}}func (conf Config) Access(kong *pdk.PDK) {key, err := kong.Request.GetQueryArg("key")apiKey := conf.Apikeyif err != nil {kong.Log.Err(err.Error())}//it adjusts the header parameters in this way.x := make(map[string][]string)x["Content-Type"] = append(x["Content-Type"], "application/json")//If the key of the consumer is not equal to the claimed key, kong doesn't ensure the proxyif apiKey != key {kong.Response.Exit(403, "You have no correct consumer key.", x)}}
