From 6dadf73305c0a3d779dd625a8349b4a7341df18d Mon Sep 17 00:00:00 2001 From: Sam Morrison Date: Fri, 5 Apr 2013 11:41:42 +1100 Subject: Encode consoleauth token in utf-8 to make it a str This is needed for memcached backend, because the key cannot be a unicode object in any of the exposed functions. Fixes bug 1164784 Co-authored-by: Stanislaw Pitucha Change-Id: Ieb1429116d96eefe97564fe92631063dc16117cb --- nova/consoleauth/manager.py | 2 +- nova/tests/consoleauth/test_consoleauth.py | 61 +++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/nova/consoleauth/manager.py b/nova/consoleauth/manager.py index 80a6d447f..26bdcd1d4 100644 --- a/nova/consoleauth/manager.py +++ b/nova/consoleauth/manager.py @@ -118,7 +118,7 @@ class ConsoleAuthManager(manager.Manager): def delete_tokens_for_instance(self, context, instance_uuid): tokens = self._get_tokens_for_instance(instance_uuid) for token in tokens: - self.mc.delete(token) + self.mc.delete(token.encode('UTF-8')) self.mc.delete(instance_uuid.encode('UTF-8')) def get_backdoor_port(self, context): diff --git a/nova/tests/consoleauth/test_consoleauth.py b/nova/tests/consoleauth/test_consoleauth.py index 024434c0a..14f95ab0d 100644 --- a/nova/tests/consoleauth/test_consoleauth.py +++ b/nova/tests/consoleauth/test_consoleauth.py @@ -20,6 +20,7 @@ Tests for Consoleauth Code. """ +import mox from nova.consoleauth import manager from nova import context from nova.openstack.common import log as logging @@ -40,7 +41,7 @@ class ConsoleauthTestCase(test.TestCase): def test_tokens_expire(self): # Test that tokens expire correctly. self.useFixture(test.TimeOverride()) - token = 'mytok' + token = u'mytok' self.flags(console_token_ttl=1) self._stub_validate_console_port(True) @@ -61,8 +62,8 @@ class ConsoleauthTestCase(test.TestCase): fake_validate_console_port) def test_multiple_tokens_for_instance(self): - tokens = ["token" + str(i) for i in xrange(10)] - instance = "12345" + tokens = [u"token" + str(i) for i in xrange(10)] + instance = u"12345" self._stub_validate_console_port(True) @@ -75,8 +76,8 @@ class ConsoleauthTestCase(test.TestCase): self.assertTrue(self.manager.check_token(self.context, token)) def test_delete_tokens_for_instance(self): - instance = "12345" - tokens = ["token" + str(i) for i in xrange(10)] + instance = u"12345" + tokens = [u"token" + str(i) for i in xrange(10)] for token in tokens: self.manager.authorize_console(self.context, token, 'novnc', '127.0.0.1', '8080', 'host', @@ -90,20 +91,20 @@ class ConsoleauthTestCase(test.TestCase): self.assertFalse(self.manager.check_token(self.context, token)) def test_wrong_token_has_port(self): - token = 'mytok' + token = u'mytok' self._stub_validate_console_port(False) self.manager.authorize_console(self.context, token, 'novnc', '127.0.0.1', '8080', 'host', - instance_uuid='instance') + instance_uuid=u'instance') self.assertFalse(self.manager.check_token(self.context, token)) def test_console_no_instance_uuid(self): - self.manager.authorize_console(self.context, "token", 'novnc', + self.manager.authorize_console(self.context, u"token", 'novnc', '127.0.0.1', '8080', 'host', instance_uuid=None) - self.assertFalse(self.manager.check_token(self.context, "token")) + self.assertFalse(self.manager.check_token(self.context, u"token")) def test_get_backdoor_port(self): self.manager.backdoor_port = 59697 @@ -111,6 +112,48 @@ class ConsoleauthTestCase(test.TestCase): self.assertEqual(port, self.manager.backdoor_port) +class ControlauthMemcacheEncodingTestCase(test.TestCase): + def setUp(self): + super(ControlauthMemcacheEncodingTestCase, self).setUp() + self.manager = manager.ConsoleAuthManager() + self.context = context.get_admin_context() + self.u_token = u"token" + self.u_instance = u"instance" + + def test_authorize_console_encoding(self): + self.mox.StubOutWithMock(self.manager.mc, "set") + self.mox.StubOutWithMock(self.manager.mc, "get") + self.manager.mc.set(mox.IsA(str), mox.IgnoreArg(), mox.IgnoreArg() + ).AndReturn(True) + self.manager.mc.get(mox.IsA(str)).AndReturn(None) + self.manager.mc.set(mox.IsA(str), mox.IgnoreArg()).AndReturn(True) + + self.mox.ReplayAll() + + self.manager.authorize_console(self.context, self.u_token, 'novnc', + '127.0.0.1', '8080', 'host', + self.u_instance) + + def test_check_token_encoding(self): + self.mox.StubOutWithMock(self.manager.mc, "get") + self.manager.mc.get(mox.IsA(str)).AndReturn(None) + + self.mox.ReplayAll() + + self.manager.check_token(self.context, self.u_token) + + def test_delete_tokens_for_instance_encoding(self): + self.mox.StubOutWithMock(self.manager.mc, "delete") + self.mox.StubOutWithMock(self.manager.mc, "get") + self.manager.mc.get(mox.IsA(str)).AndReturn('["token"]') + self.manager.mc.delete(mox.IsA(str)).AndReturn(True) + self.manager.mc.delete(mox.IsA(str)).AndReturn(True) + + self.mox.ReplayAll() + + self.manager.delete_tokens_for_instance(self.context, self.u_instance) + + class CellsConsoleauthTestCase(ConsoleauthTestCase): """Test Case for consoleauth w/ cells enabled.""" -- cgit