Error try to use session data in directory based middleware

Setup a new tyk 1.9 install on Centos 7.

Trying to use directory based middleware and running into a problem trying to use session data.

Our basic test script works when not using session data:
location: /opt/tyk-gateway/middleware/68f54535167c427053ac163865af8a97/post/insertVersionMiddleware.js

// Create a middleware object
var insertVersionMiddleware = new TykJS.TykMiddleware.NewMiddleware({});

 insertVersionMiddleware.NewProcessRequest(function(request, session)
         {
         log(request.Body);
         if (session.meta_data)
                 {
                 log("Got a session.meta_data object!!!");
                 }
         return insertVersionMiddleware.ReturnData(request,{});
    });

log(“insertVersion middleware initialised”);

When we try to use session data, we make the following changes:
rename file to insertVersionMiddleware_with_session.js

modified the return statement to:

    return insertVersionMiddleware.ReturnData(request,session.meta_data);

After a reload using “192.168.10.146:8080/tyk/reload/group” the middleware loads successfully, however, we receive the following errors when trying to use it:

##Reload##
Mar  1 15:15:58 localhost tyk: time="2016-03-01T15:15:58-05:00" level=info msg="Group reload: sending to channel"
Mar  1 15:15:58 localhost tyk: time="2016-03-01T15:15:58-05:00" level=info msg="Reload signal received, reloading endpoints"
Mar  1 15:15:58 localhost tyk: time="2016-03-01T15:15:58-05:00" level=info msg="Reloaded URL Structure - Success"

##API detected and loaded##
Mar  1 15:16:08 localhost tyk: time="2016-03-01T15:16:08-05:00" level=info msg="Detected 1 APIs"
Mar  1 15:16:08 localhost tyk: time="2016-03-01T15:16:08-05:00" level=info msg="--> Loading API: SecureMail Stage"
Mar  1 15:16:08 localhost tyk: time="2016-03-01T15:16:08-05:00" level=info msg="----> Tracking: (no host)"
Mar  1 15:16:08 localhost tyk: time="2016-03-01T15:16:08-05:00" level=info msg="Loading JS File: /opt/tyk-gateway/middleware/68f54535167c427053ac163865af8a97/post/insertVersionMiddleware_with_session.js"
Mar  1 15:16:08 localhost tyk: time="2016-03-01T15:16:08-05:00" level=info msg="[JSVM] [LOG]: insertVersion middleware initialised"
Mar  1 15:16:08 localhost tyk: time="2016-03-01T15:16:08-05:00" level=info msg="----> Checking security policy: Token"
Mar  1 15:16:08 localhost tyk: time="2016-03-01T15:16:08-05:00" level=info msg="Loading uptime tests..."
Mar  1 15:16:08 localhost tyk: time="2016-03-01T15:16:08-05:00" level=info msg="Loaded 0 policies "
Mar  1 15:16:08 localhost tyk: time="2016-03-01T15:16:08-05:00" level=info msg="API reload complete"

##Error thrown when API accessed##
Mar  1 15:16:17 localhost tyk: time="2016-03-01T15:16:17-05:00" level=error msg="Failed to decode middleware request data on return from VM: invalid character 'u' looking for beginning of value"
Mar  1 15:16:22 localhost tyk: time="2016-03-01T15:16:22-05:00" level=error msg="http: proxy error: http: invalid Read on closed Body"

Note, we are using APIs using developer keys (Not Open APIs).

THanks!

Thomas

Is this pre or post middleware? Pre middleware has no session.

post middleware.

Thanks!

Thomas

Does your session object actually have any meta_data? If it is nil it will not work.

We believe so. We are setting the “Authorization” header in the POST we are sending.

It appears that just adding the _with_session to the middleware file cause the error to be thrown.

We are using postman to test.

We are new to Tyk, we may be overlooking something.

Thanks!

Thomas

Can you GET the key from the session store using the API and post it here? I recon the meta_data field of your session is empty.

If you are running dashboard, just check the key out and see what’s in the metadata section.

The meta data is a sub field of the session object, so while one may be populated it doesn’t mean the other is - you need to explicitly set the metadata when you create the key.

We did not have anything in the meta-data field for the key we were using. We added some dummy data to see if this would help. Unfortunately, we are still receiving the same error after adding this meta-data. Below is a screenshot of the Key’s page from the dashboard:

Turns out someone else had the same problem you did - there was a bug in how directory based middleware for post processing were being loaded:

You could clone and build master, then drop a new binary into your install to get it to work…

Will obviously be fixed in the next version :-/

I had installed this via YUM repository, so no main,go file. Will rebuild again from source.

Thanks for all your help!

Thomas