Skip to content

Commit

Permalink
Use memcache to store consoleauth tokens
Browse files Browse the repository at this point in the history
 * Rather than store tokens in a dict, use memorycache
   so that deployers can optionally use memcached if they
   would like to run multile nova-consoleauth processes
 * Fixes bug 989337

Change-Id: I96ec4d796e53b69a494d856269e3c4e8f9b3d222
  • Loading branch information
sleepsonthefloor authored and vishvananda committed May 21, 2012
1 parent ebf5231 commit 4048158
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions nova/consoleauth/manager.py
Expand Up @@ -24,7 +24,7 @@
from nova import log as logging
from nova import manager
from nova.openstack.common import cfg
from nova import utils
from nova.openstack.common import jsonutils


LOG = logging.getLogger(__name__)
Expand All @@ -49,33 +49,29 @@ class ConsoleAuthManager(manager.Manager):

def __init__(self, scheduler_driver=None, *args, **kwargs):
super(ConsoleAuthManager, self).__init__(*args, **kwargs)
self.tokens = {}
utils.LoopingCall(self._delete_expired_tokens).start(1)

def _delete_expired_tokens(self):
now = time.time()
to_delete = []
for k, v in self.tokens.items():
if now - v['last_activity_at'] > FLAGS.console_token_ttl:
to_delete.append(k)

for k in to_delete:
LOG.audit(_("Deleting Expired Token: (%s)"), k)
del self.tokens[k]
if FLAGS.memcached_servers:
import memcache
else:
from nova.common import memorycache as memcache
self.mc = memcache.Client(FLAGS.memcached_servers,
debug=0)

def authorize_console(self, context, token, console_type, host, port,
internal_access_path):
self.tokens[token] = {'token': token,
'console_type': console_type,
'host': host,
'port': port,
'internal_access_path': internal_access_path,
'last_activity_at': time.time()}
token_dict = self.tokens[token]
token_dict = {'token': token,
'console_type': console_type,
'host': host,
'port': port,
'internal_access_path': internal_access_path,
'last_activity_at': time.time()}
data = jsonutils.dumps(token_dict)
self.mc.set(token, data, FLAGS.console_token_ttl)
LOG.audit(_("Received Token: %(token)s, %(token_dict)s)"), locals())

def check_token(self, context, token):
token_valid = token in self.tokens
token_str = self.mc.get(token)
token_valid = (token_str != None)
LOG.audit(_("Checking Token: %(token)s, %(token_valid)s)"), locals())
if token_valid:
return self.tokens[token]
return jsonutils.loads(token_str)

0 comments on commit 4048158

Please sign in to comment.