Hi,
I’ve been playing a little with the middleware and I have a few things that I can’t get to work or don’t understand quite well enough.
I have an on-premise installation.
So here we go, I started by creating a JS middleware using the folders and it worked pretty well. I managed to create a redirection based on a set of rules (conditons). But I feel like the JS middleware is meant only for light things and in my next project it might not be enough (example, Auth linked to LDAP).
So I went on to try Python middleware. I created a project with a manifest.json and a middleware.py files. Then used the tyk-cli to create a bundle, set it in the API description via the Dashboard UI and I got the Auth plugin from the demo to work. Cool !
Now I’m trying to use another Hook than the Auth one. Post or Pre. I updated my python file like this :
from tyk.decorators import *
from gateway import TykGateway as tyk
@Hook
def MyAuthMiddleware(request, session, metadata, spec):
log(“hahahahaha”)
auth_header = request.get_header(‘Authorization’)
if auth_header == ‘47a0c79c427728b3df4af62b9228c8ae’:
session.rate = 1000.0
session.per = 1.0
metadata[“token”] = “47a0c79c427728b3df4af62b9228c8ae”
return request, session, metadata
@Hook
def MyPreMiddleware(request, session, spec):
print(“my_middleware: MyPreMiddleware”)
return request, session
@Hook
def MyPostMiddleware(request, session, spec):
print(“my_middleware: MyPostMiddleware”)
return request, session
and my manifest like this :
{
“file_list”: [
“middleware.py”
],
“custom_middleware”: {
“pre”:[{
“name”: “MyPreMiddleware”,
“require_session”: false
}],
“post”:[{
“name”: “MyPostMiddleware”,
“require_session”: false
}],
“driver”: “python”,
“auth_check”: {
“name”: “MyAuthMiddleware”
}
}
}
I updated my bundle in the API config, restarted my tyk-gateway-python service and my Auth plugin still works.
Except I get these errors :
time=“Oct 20 14:57:05” level=error msg=“Can’t dispatch ‘MyPreMiddleware’, hook is not defined.”
time=“Oct 20 14:57:05” level=warning msg=“Incorrect key expiry setting detected, correcting”
time=“Oct 20 14:57:05” level=error msg=“Can’t dispatch ‘MyPostMiddleware’, hook is not defined.”
I do not understand why…
Also, I do not see my log(“hahahahaha”) in the service logs. Am I missing something ?
Another thing, since I added my bundle I get an error about my JS middleware that I didn’t get before. Does that mean that you cannot have both at the same time ?
Error is : time=“Oct 20 15:10:50” level=error msg=“Can’t dispatch ‘action’, hook is not defined.”
Quick kinda related question : Is it possible to have a bundle for JS middleware in the same way ? Also, I couldn’t get the HTTP method with the JS code, is there a way to retrieve it ?
Basically my project will have 2 plugins need. An LDAP connector and some rules for redirecting some endpoint to another if some conditions are met, those conditions will be based on data from a third endpoint.
Thanks a lot for any input on any of that !
Cheers,
Robert