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

kong.Response.Exit not picking up response header map #90

Closed benpattavios closed 7 months ago

benpattavios commented 2 years ago

I've successfully set up a Kong Go custom plugin, but am finding that my kong.Response.Exit responses don't have any headers. Take a simple Go plugin, called go-plugin-test.go:

package main

import (
    "fmt"

    "github.com/Kong/go-pdk"
    "github.com/Kong/go-pdk/server"
)

type Config struct {
    Message string
}

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

const Version = "1.0.0"
const Priority = 1

func main() {
    server.StartServer(New, Version, Priority)
}

func (conf Config) Access(kong *pdk.PDK) {
    body := fmt.Sprintf("{\"message\": \"%s\"}", conf.Message)
    headers := make(map[string][]string)
    headers["Content-Type"] = append(headers["Content-Type"], "application/json")
    headers["X-Hello-From-Go"] = append(headers["X-Hello-From-Go"], "hello")
    kong.Response.Exit(200, body, headers)
}

I've been running it with kong.yaml:

_format_version: "2.1"
_transform: true

services:
- name: test-service
  url: https://httpbin.org/get
  routes:
  - name: test-route
    paths:
    - /test-service
plugins:
  - name: go-plugin-test
    service: test-service
    config:
      message: "hello world"

A simple kong.conf:

plugins = bundled,go-plugin-test
pluginserver_names = go-plugin-test
pluginserver_go_plugin_test_query_cmd = /usr/local/bin/go-plugin-test -dump
admin_listen = 127.0.0.1:8001 reuseport backlog=16384, 127.0.0.1:8444 http2 ssl reuseport backlog=16384

And a Dockerfile:

FROM kong:latest

USER root

ENV KONG_DATABASE=off
ENV KONG_DECLARATIVE_CONFIG=kong.yml
ENV KONG_ADMIN_LISTEN="0.0.0.0:8001, 0.0.0.0:8444 ssl"

COPY go-plugin-test /usr/local/bin/go-plugin-test
COPY kong.yml /
COPY kong.conf /etc/kong/kong.conf

EXPOSE 8000
EXPOSE 8001

ENTRYPOINT [ "/docker-entrypoint.sh" ]
CMD [ "kong", "docker-start" ]

I've built & run with:

> GOOS=linux GOARCH=amd64 go build .
> docker build -t kong-test .
> docker run -p 8000:8000 kong-test

And if I curl the endpoint, I get the 200 response I want, but no Content-Type, or X-Hello-From-Go header:

> curl -v localhost:8000/test-service
*   Trying ::1:8000...
* Connected to localhost (::1) port 8000 (#0)
> GET /test-service HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.77.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Fri, 28 Jan 2022 17:46:10 GMT
< Connection: keep-alive
< Content-Length: 26
< X-Kong-Response-Latency: 54
< Server: kong/2.7.0
<
* Connection #0 to host localhost left intact
{"message": "hello world"}
javierguerragiraldez commented 2 years ago

seems related to #83, which was fixed by Kong/kong#8267

gszr commented 7 months ago

Closing this due to the lack of activity since Javier's reply. Please reopen if the issue persists in newer Kong versions.