Error while trying to access Session object in POST middlware plugin


#1

Hi,

I am trying to setup a basic POST middleware plugin that has access to the session object so I can add a few custom fields to the meta_data.

My API definition(OpenKeyless) has
“custom_middleware”: {
“post”: [
{
“name”: “sampleMiddleware”,
“path”: “middleware/sampleMiddleware.js”,
“require_session”: true
}
]
},

For now, I am using the sampleMiddleware.js file as it is. I haven’t changed anything in that file yet.

The JSVM is enabled, the middleware js loads properly as well.
However, when I hit my API endpoint, the docker logs show an error
interface conversion: interface is nil, not main.SessionState
goroutine 166951 [running]:
net/http.(*conn).serve.func1(0xc820f7f1e0, 0x7fbbbad67f70, 0xc822ab55d8)
/usr/local/go/src/net/http/server.go:1287 +0xb5
main.(*DynamicMiddleware).ProcessRequest(0xc822d80260, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260, 0xb96ae0, 0x0, 0x0, 0x0, 0x0)
/home/tyk/go/src/github.com/lonelycode/tyk/plugins.go:105 +0x8b4
main.CreateMiddleware.func1.1(0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/home/tyk/go/src/github.com/lonelycode/tyk/middleware.go:44 +0x178
net/http.HandlerFunc.ServeHTTP(0xc820dc8040, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1422 +0x3a
main.CreateMiddleware.func1.1(0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/home/tyk/go/src/github.com/lonelycode/tyk/middleware.go:54 +0x26f
net/http.HandlerFunc.ServeHTTP(0xc820dc8580, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1422 +0x3a
main.CreateMiddleware.func1.1(0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/home/tyk/go/src/github.com/lonelycode/tyk/middleware.go:54 +0x26f
net/http.HandlerFunc.ServeHTTP(0xc820dc8780, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1422 +0x3a
main.CreateMiddleware.func1.1(0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/home/tyk/go/src/github.com/lonelycode/tyk/middleware.go:54 +0x26f
net/http.HandlerFunc.ServeHTTP(0xc820dc8800, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1422 +0x3a
main.CreateMiddleware.func1.1(0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/home/tyk/go/src/github.com/lonelycode/tyk/middleware.go:54 +0x26f
net/http.HandlerFunc.ServeHTTP(0xc820dc8840, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1422 +0x3a
main.CreateMiddleware.func1.1(0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/home/tyk/go/src/github.com/lonelycode/tyk/middleware.go:54 +0x26f
net/http.HandlerFunc.ServeHTTP(0xc820dc8ac0, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1422 +0x3a
main.CreateMiddleware.func1.1(0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/home/tyk/go/src/github.com/lonelycode/tyk/middleware.go:54 +0x26f
net/http.HandlerFunc.ServeHTTP(0xc820dc8bc0, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1422 +0x3a
main.CreateMiddleware.func1.1(0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/home/tyk/go/src/github.com/lonelycode/tyk/middleware.go:54 +0x26f
net/http.HandlerFunc.ServeHTTP(0xc820dc8d40, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1422 +0x3a
main.CreateMiddleware.func1.1(0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/home/tyk/go/src/github.com/lonelycode/tyk/middleware.go:54 +0x26f
net/http.HandlerFunc.ServeHTTP(0xc820dc8d80, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1422 +0x3a
main.CreateMiddleware.func1.1(0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/home/tyk/go/src/github.com/lonelycode/tyk/middleware.go:54 +0x26f
net/http.HandlerFunc.ServeHTTP(0xc820dc8e00, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1422 +0x3a
github.com/gorilla/mux.(*Router).ServeHTTP(0xc8201ef400, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/home/tyk/go/src/github.com/gorilla/mux/mux.go:98 +0x29e
net/http.(*ServeMux).ServeHTTP(0xc821d815f0, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1699 +0x17d
net/http.serverHandler.ServeHTTP(0xc8209037a0, 0x7fbbb872dc00, 0xc820f7f340, 0xc821b3d260)
/usr/local/go/src/net/http/server.go:1862 +0x19e
net/http.(*conn).serve(0xc820f7f1e0)
/usr/local/go/src/net/http/server.go:1361 +0xbee
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:1910 +0x3f6

The POST plugin works when I have require_session: false in my api definition. It fails only when I set it to true.

Could you please point me in the right direction.

Thanks,
Prashanth


#2

There is no session object for open API’s. So nothing is sent into the middleware, which means it has nothing to return.


#3

Thanks Martin.

I tried OAuth flow and it works now.

Wondering if the session is available if a policy it attached to an Open keyless API.


#4

Open API’s don’t create sessions, you’d need to manually create it in the middleware.

Though then there’s no rate limiting or anything since there’s nothing to look up.

You could have a closed API and dynamically create a session in a preprocessor middleware (that’s how the IP rate limiter works)