编译
在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 builder
RUN apk add --no-cache git gcc libc-dev
RUN go get github.com/Kong/go-pluginserver
RUN mkdir /go-plugins
COPY go-key-checker.go /go-plugins/go-key-checker.go
RUN go build -buildmode plugin -o /go-plugins/go-key-checker.so /go-plugins/go-key-checker.go
FROM kong:2.0.1-alpine
COPY --from=builder /go/bin/go-pluginserver /usr/local/bin/go-pluginserver
RUN mkdir /tmp/go-plugins
COPY --from=builder /go-plugins/go-key-checker.so /tmp/go-plugins/go-key-checker.so
COPY config.yml /tmp/config.yml
USER root
RUN chmod -R 775 /tmp
USER kong
Go-plugin
package main
import (
"github.com/Kong/go-pdk"
)
// it represents to config parameters into the config.yml
type Config struct {
Apikey string
}
func New() interface{} {
return &Config{}
}
func (conf Config) Access(kong *pdk.PDK) {
key, err := kong.Request.GetQueryArg("key")
apiKey := conf.Apikey
if 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 proxy
if apiKey != key {
kong.Response.Exit(403, "You have no correct consumer key.", x)
}
}