More Cleanup of code

Moved code in AuthManager init to new so it isn't called multiple times
Changed AuthManager flag to specify class name as well as module name
Added exception for missing auth_driver
Changed import to use "recommended" style for nested imports
  http://docs.python.org/dev/library/functions.html#__import__
This commit is contained in:
Vishvananda Ishaya
2010-07-25 11:20:09 -07:00
parent d329fb487c
commit 39758dc406
6 changed files with 29 additions and 43 deletions

View File

@@ -78,7 +78,7 @@ def main():
FLAGS.network_size = 32
FLAGS.fake_libvirt=True
FLAGS.fake_network=True
FLAGS.auth_driver='nova.auth.fakeldapdriver'
FLAGS.auth_driver='nova.auth.ldapdriver.FakeLdapDriver'
action = argv[1]
if action in ['add','del','old']:
mac = argv[2]

View File

@@ -1,32 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Fake Auth driver for ldap
"""
from nova.auth import ldapdriver
class AuthDriver(ldapdriver.AuthDriver):
"""Ldap Auth driver
Defines enter and exit and therefore supports the with/as syntax.
"""
def __init__(self):
self.ldap = __import__('nova.auth.fakeldap', fromlist=True)

View File

@@ -17,7 +17,7 @@
# under the License.
"""
Auth driver for ldap
Auth driver for ldap. Includes FakeLdapDriver.
It should be easy to create a replacement for this driver supporting
other backends by creating another class that exposes the same
@@ -25,6 +25,7 @@ public methods.
"""
import logging
import sys
from nova import exception
from nova import flags
@@ -61,7 +62,7 @@ flags.DEFINE_string('ldap_developer',
# to define a set interface for AuthDrivers. I'm delaying
# creating this now because I'm expecting an auth refactor
# in which we may want to change the interface a bit more.
class AuthDriver(object):
class LdapDriver(object):
"""Ldap Auth driver
Defines enter and exit and therefore supports the with/as syntax.
@@ -471,3 +472,10 @@ class AuthDriver(object):
"""Convert uid to dn"""
return 'uid=%s,%s' % (dn, FLAGS.ldap_user_subtree)
class FakeLdapDriver(LdapDriver):
"""Fake Ldap Auth driver"""
def __init__(self):
__import__('nova.auth.fakeldap')
self.ldap = sys.modules['nova.auth.fakeldap']

View File

@@ -24,6 +24,7 @@ import logging
import os
import shutil
import string
import sys
import tempfile
import uuid
import zipfile
@@ -75,7 +76,7 @@ flags.DEFINE_string('credential_cert_subject',
flags.DEFINE_string('vpn_ip', '127.0.0.1',
'Public IP for the cloudpipe VPN servers')
flags.DEFINE_string('auth_driver', 'fakeldapdriver',
flags.DEFINE_string('auth_driver', 'nova.auth.ldapdriver.AuthDriver',
'Driver that auth manager uses')
class AuthBase(object):
@@ -320,16 +321,25 @@ class AuthManager(object):
"""
_instance=None
def __new__(cls, *args, **kwargs):
"""Returns the AuthManager singleton with driver set
__init__ is run every time AuthManager() is called, so we need to do
any constructor related stuff here. The driver that is specified
in the flagfile is loaded here.
"""
if not cls._instance:
cls._instance = super(AuthManager, cls).__new__(
cls, *args, **kwargs)
mod_str, sep, driver_str = FLAGS.auth_driver.rpartition('.')
try:
mod = __import__(mod_str)
cls._instance.driver = getattr(sys.modules[mod_str],
driver_str)
except (ImportError, AttributeError):
raise exception.Error('Auth driver %s cannot be found'
% FLAGS.auth_driver)
return cls._instance
def __init__(self, *args, **kwargs):
"""Imports the driver module and saves the Driver class"""
mod = __import__(FLAGS.auth_driver, fromlist=True)
self.driver = mod.AuthDriver
def authenticate(self, access, signature, params, verb='GET',
server_string='127.0.0.1:8773', path='/',
verify_signature=True):

View File

@@ -24,5 +24,5 @@ FLAGS.fake_libvirt = True
FLAGS.fake_storage = True
FLAGS.fake_rabbit = True
FLAGS.fake_network = True
FLAGS.auth_driver = 'nova.auth.fakeldapdriver'
FLAGS.auth_driver = 'nova.auth.ldapdriver.FakeLdapDriver'
FLAGS.verbose = True

View File

@@ -37,7 +37,7 @@ class NetworkTestCase(test.TrialTestCase):
self.flags(fake_libvirt=True,
fake_storage=True,
fake_network=True,
auth_driver='nova.auth.fakeldapdriver',
auth_driver='nova.auth.ldapdriver.FakeLdapDriver',
network_size=32)
logging.getLogger().setLevel(logging.DEBUG)
self.manager = manager.AuthManager()