How to apply Go Custom Plugin in Tyk pro demo 4.3.1

Hi Team.
I have been trying to test about go custom plugins in my local environment following these documents

  • Golang plugins
    But it didn’t work well and seems out-dated for current version (4.3.1).

My environment

  • local PC: macOs monterey (go version darwin/arm64)
  • tyk-pro-demo : v.4.3.1
  • using docker
Server: Docker Desktop 4.16.0 (95345)
 Engine:
  Version:          20.10.22
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.9
  Git commit:       42c8b31
  Built:            Thu Dec 15 22:25:43 2022
  OS/Arch:          linux/arm64
  Experimental:     false

I got this error after configuring like this

API Raw definition

  "custom_middleware": {
      "pre": [
        {
          "name": "Test",
          "path": "/plugin-source/go_v4.3.1_linux_arm64.so",
          "require_session": false,
          "raw_body_only": false
        }
      ],
      "post": [],
      "post_key_auth": [],
      "auth_check": {
        "name": "",
        "path": "",
        "require_session": false,
        "raw_body_only": false
      },
      "response": [],
      "driver": "goplugin",
      "id_extractor": {
        "extract_from": "",
        "extract_with": "",
        "extractor_config": {}
      }
    },

Docker log

time="Jan 20 02:58:20" level=error msg="Unsupported extension '.so' (/plugin-source/go_v4.3.1_linux_arm64.so)" prefix=jsvm
time="Jan 20 02:58:20" level=info msg="Checking security policy: Open" api_id=028b3ec2b8414ea741f8574e702f6cc6 api_name=Agoda org_id=63c9f86f15fbf50001cc6f39
time="Jan 20 02:58:20" level=error msg="Could not load Go-plugin" error="plugin.Open("/plugin-source/go_v4.3.1_linux_arm64.so"): /plugin-source/go_v4.3.1_linux_arm64.so: invalid ELF header" mwPath="/plugin-source/go_v4.3.1_linux_arm64.so" mwSymbolName=Test

Go file

package main

import "fmt"

func Test() {
	fmt.Println("**********Test Function Called************")
}

func init() {
	fmt.Println("**********Custom Plugin Test Initialize************")
}

func main() {
}

p.s i cannot build with this command, because it didn’t match amd64/arm64 well in my local PC.

 docker run --rm -v `pwd`:/plugin-source tykio/tyk-plugin-compiler:v4.3.1 gogogo.so v1 linux arm64

so i used this command

go build -buildmode=plugin -o go_v4.3.1_linux_arm64.so

Is there anything i am missing?
Please let me know why
Thanks.

Hi @ghgh,

Welcome to the community :tada:

It is recommended to always use the tyk-plugin-compiler to build plugins.

I’ve got macOs Monterey as well, with the M1 chip. I added the ‘platform’ to my docker-compose.yml like this to get it working

  tyk-gateway:
    image: tykio/tyk-gateway:v4.3.1
    platform: linux/amd64
    ports:
    - "8080:8080"
    volumes:
    - ./middleware:/opt/tyk-gateway/middleware
    env_file:
    - ./confs/tyk.env
    networks:
    - tyk

You would need to delete the v4.3.1 container and image you have currently, so that docker can download a new one for the platform specified.

You can then build the plugin:

docker run --rm -v `pwd`:/plugin-source tykio/tyk-plugin-compiler:v4.3.1 plugin.so
2 Likes

I really appreciate for your support. It works well after applying image and platform tag for amd64

    image: tykio/tyk-gateway:v4.3.1-amd64
    container_name: tyk-gateway
    platform: linux/amd64
2 Likes