Kong / go-pdk

Write Kong plugins in Go! 🦍
https://pkg.go.dev/github.com/Kong/go-pdk
Apache License 2.0
146 stars 50 forks source link

Configuring corporate proxy for go-pdk plugin #76

Closed silverstr89 closed 3 years ago

silverstr89 commented 3 years ago

I'm trying to configure corporate proxy for go kong plugin

here is my code:

package main

import (
    "github.com/Kong/go-pdk"
)

type Config struct {
    Apikey string
}

func New() interface{} {
    return &Config{}
}

func (conf Config) Access(kong *pdk.PDK) {

    err := kong.Nginx.SetCtx("balancer_address.host", "127.0.0.1")
    if err != nil {
        kong.Log.Err(err.Error())
    }
    errC := kong.Nginx.SetCtx("balancer_address.port","8080")
    if errC != nil {
        kong.Log.Err(errC)
    }

    key, err := kong.Request.GetQueryArg("key")
    apiKey := conf.Apikey

    if err != nil {
        kong.Log.Err(err.Error())
    }

    x := make(map[string][]string)
    x["Content-Type"] = append(x["Content-Type"], "application/json")

    if apiKey != key {
        kong.Response.Exit(403, "Youu have no correct key", x)
    }
}

of course i created test proxy server at my host: 127.0.0.1:8080

but it's not working

by plugin doc: kong.Nginx.SetCtx() sets a value in the ngx.ctx request context table

i made my go plugin by example https://github.com/tfabien/kong-forward-proxy/blob/master/src/access.lua where ngx.ctx.balancer_address.host = plugin_conf.proxy_host ngx.ctx.balancer_address.port = plugin_conf.proxy_port are configuring proxy

so, anyway it's not working

my dockerfile is

FROM kong/go-plugin-tool:2.0.4-alpine-latest AS builder

RUN mkdir -p /tmp/key-checker/

COPY . /tmp/key-checker/

RUN cd /tmp/key-checker/ && \
    go get github.com/Kong/go-pdk && \
    go mod init kong-go-plugin && \
    go get -d -v github.com/Kong/go-pluginserver && \
    go build github.com/Kong/go-pluginserver && \
    go build -buildmode plugin key-checker.go

FROM kong:2.3-alpine

RUN mkdir /tmp/go-plugins
COPY --from=builder  /tmp/key-checker/go-pluginserver /usr/local/bin/go-pluginserver
COPY --from=builder  /tmp/key-checker/key-checker.so /tmp/go-plugins

ENV KONG_PLUGINSERVER_NAMES="go"
ENV KONG_PLUGINSERVER_GO_SOCKET="/tmp/go-plugins/go_pluginserver.sock"
ENV KONG_PLUGINSERVER_GO_START_CMD="/usr/local/bin/go-pluginserver -kong-prefix /tmp/go-plugins/ -plugins-directory /tmp/go-plugins"
ENV KONG_PLUGINSERVER_GO_QUERY_CMD="/usr/local/bin/go-pluginserver -dump-all-plugins -plugins-directory /tmp/go-plugins"

COPY config.yml /tmp/config.yml

USER root
RUN chmod -R 777 /tmp
RUN /usr/local/bin/go-pluginserver -version && \
    cd /tmp/go-plugins && \
    /usr/local/bin/go-pluginserver -dump-plugin-info key-checker
USER kong

starting docker by:

docker run -ti --rm --name kong-go-plugins \
  -e "KONG_DATABASE=off" \
  -e "KONG_GO_PLUGINS_DIR=/tmp/go-plugins" \
  -e "KONG_DECLARATIVE_CONFIG=/tmp/config.yml" \
  -e "KONG_GO_PLUGINSERVER_EXE=/usr/local/bin/go-pluginserver" \
  -e "KONG_PLUGINS=key-checker" \
  -e "KONG_PROXY_LISTEN=0.0.0.0:8000" \
  -p 8000:8000 \
  kong-demo

how can i resolve it?

javierguerragiraldez commented 3 years ago

Hi @KentAVP, currently the kong.Nginx.SetCtx() method doesn't support "deep" keys. Calling it as kong.Nginx.SetCtx("balancer_address.host", "127.0.0.1") is equivalent in Lua to ngx.ctx["balancer_address.host"] = "127.0.0.1", and not ngx.ctx.balancer_address.host = "127.0.0.1"

On a separate note, I'm not sure that setting the ctx is a documented behaviour of the balancer; it looks more like abusing some private variables, and wouldn't be surprised if a future release breaks this use. Can it be done via documented methods, like kong.Service.SetUpstream() or kong.Service.SetTarget()?

silverstr89 commented 3 years ago

hi @javierguerragiraldez ! ok, i got it. i already found solution. at Access method i made my own http client and use corporate proxy inside this client.

upstream and target doesn't helps to me