Compiling go plugin

I am running tyk-gateway version 2.9.2. From log startup :

| time=“Jan 13 13:25:01” level=info msg=“Tyk Gateway started (v2.9.2)” prefix=main

I compile a Go plugin (go source liftet from tyk.io documentation) using :
docker run -v c:/Users/XLA/IdeaProjects/ping-access-token-auth/goSrc:/go/src/plugin-build tykio/tyk-plugin-compiler:v2.9.2 pingaccess.so

But I get an error :
time=“Jan 13 13:25:01” level=error msg=“Could not load Go-plugin” error=“plugin.Open(”/opt/custom/plugin/pingaccess"): plugin was built with a different version of package github.com/TykTechnologies/tyk/regexp" mwPath=“/opt/custom/plugin/pingaccess.so” mwSymbolName=PingAccess

What am I doing wrong?

Which version of tyk are you running?

Can you please try building with this tool Docker to avoid mismatch?
Will wait for you to let me know if it works :slight_smile:

Both version of tyk (2.9.2) and the fact that i am already using l Docker is already stated quite clearly in op?

Hello, the error is probably due to some module aliasing. Would you be able to share the source of your plugin?

On a related note, are you able to compile the sample plugin //tyk.io/docs/plugins/supported-languages/golang/ from the docs and use it in your environment?

The plugin tested was copied straight from the docs :

package main

import (
  "net/http"

  "github.com/TykTechnologies/tyk/ctx"
  "github.com/TykTechnologies/tyk/headers"
  "github.com/TykTechnologies/tyk/user"
)

func getSessionByKey(key string) *user.SessionState {
  // here goes our logic to check if passed API key is valid and appropriate key session can be retrieved

  // perform auth (only one token "abc" is allowed)
  if key != "abc" {
    return nil
  }

  // return session
  return &user.SessionState{
    OrgID: "default",
    Alias: "abc-session",
  }
}

func MyPluginAuthCheck(rw http.ResponseWriter, r *http.Request) {
  // try to get session by API key
  key := r.Header.Get(headers.Authorization)
  session := getSessionByKey(key)
  if session == nil {
    // auth failed, reply with 403
    rw.WriteHeader(http.StatusForbidden)
    return
  }

  // auth was successful, add session and key to request's context so other middle-wares can use it
  ctx.SetSession(r, session, key, true)
}

func main() {}

The sample does not run either :
_1 | time=“Jan 14 09:01:13” level=info msg=“API bind on custom port:0” prefix=main
tyk-gateway_1 | time=“Jan 14 09:01:14” level=error msg=“Could not load Go-plugin” error=“plugin.Open(”"): realpath failed" mwPath= mwSymbolName=

There is an error in the docker command on the documentation page :
docker run -v pwd:/go/src/plugin-build tyk-plugin-compiler:2.9.0 post`

Is has unpaired ’ and does not produce a “post.so” file as documented. It produces a “post” file.

I use this instead :
docker run -v c:/path/plugin:/go/src/plugin-build tykio/tyk-plugin-compiler:v2.9.2 post.so

:grinning: I have tested it myself and got the same so have updated our docs. Command line to create the plugin was not accurate by letzya · Pull Request #1004 · TykTechnologies/tyk-docs · GitHub

Thanks for raising it! feel free to open us PRs if you want to :grinning:

You solved the typo in the docs.

But what about the real problem here ???

tyk-gateway_1 | time=“Jan 17 08:11:51” level=error msg=“Could not load Go-plugin” error=“plugin.Open(”/opt/custom/plugin/pingaccess"): plugin was built with a different version of package github.com/TykTechnologies/tyk/regexp" mwPath=“/opt/custom/plugin/pingaccess.so” mwSymbolName=PingAccess

Hi
Yes, tried it and it doesn’t work for me as well if I’m using Tyk’s libraries. I’m looking for a solution.

Till then - can you try to clone tyk repo and create your plugin under this repo? make sure you use the same version.
Thanks

OK so according to our product team there’s some bug which prevents proper usage of tyk packages. It is planned to be fixed in the next patch release in 2.9.3.

If you want use Go plugin with our docker or official binary, you can’t use Tyk packages.
If you do - you can clone our repo, build it with Tyk and run the binary (you can probably build your own container, but you can’t use our release docker till we fix it)

Thanks

Time frame for 2.9.3 ?

try it now
docker pull tykio/tyk-plugin-compiler:v2.9.3-rc1

I managed to compile and run a plugin using that docker image. Had to tweak tyk-gateway version to match.

Hi
Last night I tried v2.9.3 and it worked well with a few of Tyk’s libraries.
There’s also PR in our GitHub tyk-doc repo to clarify the command
Will try to share my repo tonight.