Using environment variables inside plugin:
Plugins have access to the environment variables that were defined when Tyk started.
When using Go plugins you can utilize the os go library to pull environment variables into the go plugin using the function `os.Getenv("<ENV_VAR_NAME>")`.
If referencing the same variables inside containers please keep in mind how Kubernetes / secrets/ pods/ containers pick up env vars that have been updated. As these don't automatically get picked up. In this case the Gateway pods will need to be restarted/ redeployed for the new updated variables.
Here's an example of a go plugin that uses the above function: https://github.com/TykTechnologies/custom-plugin-examples/tree/master/plugins/go-postauth-oauth2_introspection
Common plugin issues:
Here some common issues that we've seen from Support tickets.
1. This issue found when you are compiling go plugins on Apple MAC M1 (silicon chips).
There are some CPU architecture compiling issues for M1, need to force Gateway image to be amd/64
time="Mar 29 17:22:28" level=error msg="Could not load Go-plugin" error="plugin.Open("plugins/go/example/example-go-plugin.so"):
/opt/tyk-gateway/plugins/go/example/example-go-plugin.so: cannot open shared object file: No such file or directory"
mwPath="plugins/go/example/example-go-plugin.so" mwSymbolName=AddHelloWorldHeader
To resolve, delete Existing Gateway docker image:
docker images | grep gateway
docker image rm <gateway-image-id>
Pull the amd/64
specific gateway image version
DOCKER_DEFAULT_PLATFORM=linux/amd64
docker pull tykio/tyk-gateway:v<gateway-version>
2. Plugin already loaded error
level=error msg="Could not load Go-plugin" error="plugin.Open(\"./middleware/CustomGoPlugin-lt.so\"):
plugin already loaded (previous failure)" mwPath=./middleware/CustomGoPlugin-lt.so mwSymbolName=AuthCheck'
This error indicates that a plugin with the same id has already been loaded on the Gateway. A common scenario in which you may experience this is if a compiled plugin .(so) file is renamed and then attempted to be used in a different API i.e. same plugin.so file, but renamed and loaded in more than one API.
It is generally recommended to pass in a build_id when compiling plugins to prevent this. Using $(date +%s)
is one way to ensure a unique build_id is assigned each time a plugin is compiled.
docker run --rm -v `pwd`:/plugin-source tykio/tyk-plugin-compiler:v5.0.0 plugin.so $(date +%s)
3. Mismatch in dependencies
The hash of the gateway dependencies needs to match that of the target Gateway for the plugin to work properly.
level=error msg="Could not load Go-plugin" error="plugin.Open(\"middleware/plugins/AddConsumerHeader_v5.0.11_linux_amd64.so\"):
plugin was built with a different version of package github.com/TykTechnologies/graphql-go-tools/pkg/engine/datasource/httpclient
(previous failure)" mwPath=middleware/plugins/AddConsumerHeader.so mwSymbolName=SetConsumerHeader
You can always retrieve the hash from the commit section of the Tyk version as shown below
Or you could find the hash of your gateway version git show-ref --tags
. When you retrieve them you can re-run the command get github.com/TykTechnologies/tyk@<hash>
as indicated in our docs
Comments
0 comments
Please sign in to leave a comment.