Add Toshio's fix to use fedora-db-access.

This commit is contained in:
Ricky Zhou (周家杰) 2007-11-29 22:10:30 -07:00
parent 63873e2903
commit dca1acaade

View file

@ -31,26 +31,76 @@ from random import Random
import sha
from base64 import b64encode
import sys
import os
dbName = 'fastest'
class AuthError(Exception):
pass
def retrieve_db_info(dbKey):
'''Retrieve information to connect to the db from the filesystem.
Arguments:
:dbKey: The string identifying the database entry in the config file.
Returns: A dictionary containing the values to use in connecting to the
database.
Exceptions:
:IOError: Returned if the config file is not on the system.
:AuthError: Returned if there is no record found for dbKey in the
config file.
'''
# Open a filehandle to the config file
if os.environ.has_key('HOME') and os.path.isfile(
os.path.join(os.environ.get('HOME'), '.fedora-db-access')):
fh = file(os.path.join(
os.environ.get('HOME'), '.fedora-db-access'), 'r')
elif os.path.isfile('/etc/sysconfig/fedora-db-access'):
fh = file('/etc/sysconfig/fedora-db-access', 'r')
else:
raise IOError, 'fedora-db-access file does not exist.'
# Read the file until we get the information for the requested db
dbInfo = None
for line in fh.readlines():
if not line:
break
line = line.strip()
if not line or line[0] == '#':
continue
pieces = line.split(None, 1)
if len(pieces) < 2:
continue
if pieces[0] == dbKey:
dbInfo = eval(pieces[1])
break
if fh:
fh.close()
if not dbInfo:
raise AuthError, 'Authentication source "%s" not configured' % (dbKey,)
return dbInfo
class Server(object):
def __init__(self, server=None, who=None, password=None):
### FIXME: Before deploy, get the default server, user, and password
# from the fedora-db-access file.
server = server or 'localhost'
who = who or 'cn=directory manager'
password = password or 'fedoraproject'
try:
dbInfo = retrieve_db_info(dbName)
except IOError:
raise AuthError, 'Authentication config file fedora-db-access is' \
' not available'
server = server or dbInfo['host'] or 'localhost'
who = 'cn=%s' % (who or dbInfo['user'])
# Some db connections have no password
password = password or dbInfo.get('password')
self.ldapConn = ldap.open(server)
self.ldapConn.simple_bind_s(who, password)
def add(self, base, attributes):
''' Add a new group record to LDAP instance '''
attributes=[ (k, v) for k,v in attributes.items() ]
self.ldapConn.add_s(base, attributes)
self.ldapConn.add_s(base, attributes.items())
def delete(self, base):
''' Delete target base '''