def init_user_db(cfg): global user_db if not cfg.access_control: user_db = Fake_User_DB() return user_db try: if os.path.exists(cfg.user_db_path): mode = 'w' # anydbm doc: open existing database for reading and writing log.info('user DB located, path: %s' % (cfg.user_db_path)) else: mode = 'n' # anydbm doc: create a new, empty database, open for reading and writing log.info('user DB missing, generating one: path: %s' % (cfg.user_db_path)) user_db = User_DB(db_path=cfg.user_db_path) user_db.init(mode=mode) except Exception as e: log.exception('failed to init user_db, configured user_db path: %s' % (cfg.user_db_path)) raise e log.info('user DB initialized: path: %s, user-count: %s' % (cfg.user_db_path, user_db.user_count())) return user_db
def init_pw_db(cfg, user_pw_list_file, user_db_path, ugid_str='www-data'): """ @param ugid_str: shared uid, gid set on generated file """ if os.path.exists(user_db_path): print('user_db_path already exists, aborting: ' + user_db_path) return user_db = User_DB(db_path=user_db_path) user_db.init(mode='n') # always create a new, empty database, open for reading and writing assert len(salt) > 8, 'server-key not found or too short' print('using config secret_key for salt generation: ' + salt[:3] + '...') u_count = 0 with open(user_pw_list_file, 'r') as f: for line in f: if re.match('(^#)|(\s+$)', line): continue kv_arr = line.split(',') if 5 != len(kv_arr): raise Exception('failed to parse first-name,last-name,email,user,pw line: ' + line) first_name, last_name, rz_username, email_address, pw_plaintext = map(str.strip, kv_arr) add_user_login(user_db=user_db, self=cfg.secret_key, first_name=first_name, last_name=last_name, rz_username=rz_username, email_address=email_address, pw_plaintext=pw_plaintext) print('user_db: added entry: rz_username: %s, pw: %s...' % (rz_username, pw_plaintext[:3])) u_count = u_count + 1 user_db.shutdown() ugid = pwd.getpwnam(ugid_str).pw_uid os.chown(user_db_path, ugid, ugid) print('user_db generated: path: %s, user-count: %d' % (user_db_path, u_count))
def init_pw_db(cfg, user_pw_list_file, user_db_path, ugid_str='www-data'): """ @param ugid_str: shared uid, gid set on generated file """ if os.path.exists(user_db_path): print('user_db_path already exists, aborting: ' + user_db_path) return user_db = User_DB(db_path=user_db_path) user_db.init( mode='n' ) # always create a new, empty database, open for reading and writing assert len(salt) > 8, 'server-key not found or too short' print('using config secret_key for salt generation: ' + salt[:3] + '...') u_count = 0 with open(user_pw_list_file, 'r') as f: for line in f: if re.match('(^#)|(\s+$)', line): continue kv_arr = line.split(',') if 5 != len(kv_arr): raise Exception( 'failed to parse first-name,last-name,email,user,pw line: ' + line) first_name, last_name, rz_username, email_address, pw_plaintext = map( str.strip, kv_arr) add_user_login(user_db=user_db, self=cfg.secret_key, first_name=first_name, last_name=last_name, rz_username=rz_username, email_address=email_address, pw_plaintext=pw_plaintext) print('user_db: added entry: rz_username: %s, pw: %s...' % (rz_username, pw_plaintext[:3])) u_count = u_count + 1 user_db.shutdown() ugid = pwd.getpwnam(ugid_str).pw_uid os.chown(user_db_path, ugid, ugid) print('user_db generated: path: %s, user-count: %d' % (user_db_path, u_count))
def test_db_lifecycle(self): tmp_file = tempfile.NamedTemporaryFile(prefix='rz_userdb_', dir='/tmp', suffix='_db') tmp_file.close() u_first_name = 'bob' u_email = '*****@*****.**' pw_hash = '' u_account = User_Account(first_name=u_first_name, last_name=u_first_name, rz_username=u_email, email_address=u_email, pw_hash=pw_hash) user_db = User_DB(db_path=tmp_file.name) user_db.init(mode='c') uid = user_db.user_add(u_account) user_db.user_add_role(uid, 'admin') user_db.shutdown() # reload & validate user_db = User_DB(db_path=tmp_file.name) user_db.init() # lookup_user__by_uid ret_uid, ret_u = user_db.lookup_user__by_uid(uid) self.assertEqual(ret_uid, uid) self.assertEqual(u_email, ret_u.email_address) self.assertEqual(u_email, ret_u.rz_username) self.assertEqual(u_first_name, ret_u.first_name) self.assertFalse(hasattr(ret_u, 'pw_hash')) # lookup_user__by_username ret_uid, ret_u = user_db.lookup_user__by_email_address(u_email) self.assertEqual(ret_uid, uid) self.assertEqual(u_email, ret_u.rz_username) self.assertEqual(u_email, ret_u.email_address) user_db.dump_to_file__str() user_db.shutdown()
def open_existing_user_db(user_db_path): user_db = User_DB(db_path=user_db_path) user_db.init(mode='w') return user_db