Search code examples
pythonjsonopenstackcherrypykeystone

Passing Session Object Using OpenStack - KeyStone


I'm trying to pass the session object from one file to another in order to use the same session object across all the different things I need to do (list/create VMs etc.) So far I've tried using Pickle (which hasn't worked as there is a lock instance on the object). I'm now trying to use Cherrypy to enable me to store the object as a cookie (which I'm already doing for name and fedid). But I'm getting an error which says the session object isn't JSON serializable.

How do I get past this error and is this the right way to go about passing session objects? If it's not the correct solution, how do I pass session objects (the documentation isn't exactly helpful)?

import cherrypy
import ldap
import xmlrpclib
import xml.etree.ElementTree as ET
from keystoneauth1 import loading
from keystoneauth1 import session as session
import novaclient.client as client
import json
import pickle


NOVA_VERSION="2.1"
KEYSTONE_URL = cherrypy.request.config.get("keystone")
OPENSTACK_HOST = cherrypy.request.config.get("openstack_host")
OPENSTACK_DEFAULT_DOMAIN = 
cherrypy.request.config.get("openstack_default_domain")
loader = loading.get_plugin_loader('password')
auth = loader.load_from_options(auth_url=KEYSTONE_URL, username=data.get("username"), password=data.get("password").replace(" ","%20"),user_domain_name=OPENSTACK_DEFAULT_DOMAIN)
sess = session.Session(auth=auth)
nova = client.Client(NOVA_VERSION,session=sess)
data = {
        'name'    : name,
        'fedid'   : data.get("username"),
        'sessionTest' : json.dumps(sess),
        'expires' : EXPIRE
    }

Error:

File "/usr/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/usr/lib/python2.7/site-packages/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/cherrypy/lib/jsontools.py", line 61, in json_handler
value = cherrypy.serving.request._json_inner_handler(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/cherrypy/_cpdispatch.py", line 34, in __call__
return self.callable(*self.args, **self.kwargs)
File "/var/www/frontend/controllers/api/user.py", line 102, in PUT
'sessionTest' : json.dumps(sess),
File "/usr/lib64/python2.7/json/__init__.py", line 243, in dumps
return _default_encoder.encode(obj)
File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib64/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <keystoneauth1.session.Session object at 0x7f023003c710> is not JSON serializable

Solution

  • The issue is with the loader object.

    You should load the auth credentials at the reciever

    you can pickle loader and the options but not the auth object