Tyk Cloud Python Middleware

Hi,
Is it possible to use custom python libraries when writting a Python middleware?. I need to authenticate my backend service wich is exposed on GCP and the easiest way is to use the google-auth library.

Thanks

Hi,

Yes, you can vendor the libraries and include them in the zip bundle.
Remember to let python know about them though. Here’s an example from a plugin were I needed to vendor the jwt library.

import os
import sys
bundle_dir = os.path.abspath(os.path.dirname(__file__))
for lib_dir in [ 'vendor/lib/python3.7/site-packages/' ]:
  vendor_dir = os.path.join(bundle_dir, lib_dir)
  sys.path.append(vendor_dir)

import jwt

Cheers,
Pete

Hi,
Thanks for your response. what happens if the library isn’t pure python? for example if the library needs cryptography which is a compiled c module.
Is there a way to know which libraries come allready installed in the cloud gateway?
Thanks

Hi,

Your vendor directory becomes part of the path that python looks in for libraries so anything that can go into site-packages can be vendored and loaded.

We don’t guarantee stability or consistency in the modules in the base image so it’s best to vendor anything unusual that you need. It’s just the base python 3.7 at the moment.

Cheers,
Pete

Hi,
I know that anything can be loaded, the problem I see is that the c compiled modules depend on the c standard library and the c runtime installed in the OS. So if I compile a python library in some linux kernel with some gcc version, it may not work in another linux distro.

Sorry, I misunderstood.

It would be safest to statically link your libraries to avoid the doubt.

Cheers,
Pete