<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> try : <EOL> from pyinotify import pyinotify <EOL> except ImportError : <EOL> import pyinotify <EOL> from ganeti import asyncnotifier <EOL> from ganeti import compat <EOL> from ganeti import http <EOL> from ganeti . http . auth import HttpServerRequestAuthentication <EOL> from ganeti import pathutils <EOL> from ganeti . rapi import auth <EOL> from ganeti . rapi . auth import users_file <EOL> class FileEventHandler ( asyncnotifier . FileEventHandlerBase ) : <EOL> def __init__ ( self , wm , path , cb ) : <EOL> """<STR_LIT>""" <EOL> asyncnotifier . FileEventHandlerBase . __init__ ( self , wm ) <EOL> self . _cb = cb <EOL> self . _filename = os . path . basename ( path ) <EOL> mask = ( pyinotify . EventsCodes . ALL_FLAGS [ "<STR_LIT>" ] | <EOL> pyinotify . EventsCodes . ALL_FLAGS [ "<STR_LIT>" ] | <EOL> pyinotify . EventsCodes . ALL_FLAGS [ "<STR_LIT>" ] | <EOL> pyinotify . EventsCodes . ALL_FLAGS [ "<STR_LIT>" ] ) <EOL> self . _handle = self . AddWatch ( os . path . dirname ( path ) , mask ) <EOL> def process_default ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . name == self . _filename : <EOL> logging . debug ( "<STR_LIT>" , event ) <EOL> self . _cb ( ) <EOL> def SetupFileWatcher ( filename , cb ) : <EOL> """<STR_LIT>""" <EOL> wm = pyinotify . WatchManager ( ) <EOL> handler = FileEventHandler ( wm , filename , cb ) <EOL> asyncnotifier . AsyncNotifier ( wm , default_proc_fun = handler ) <EOL> class BasicAuthenticator ( auth . RapiAuthenticator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_fn = None ) : <EOL> """<STR_LIT>""" <EOL> if user_fn : <EOL> self . user_fn = user_fn <EOL> return <EOL> self . users = users_file . RapiUsers ( ) <EOL> self . user_fn = self . users . Get <EOL> SetupFileWatcher ( pathutils . RAPI_USERS_FILE , <EOL> compat . partial ( self . users . Load , <EOL> pathutils . RAPI_USERS_FILE ) ) <EOL> self . users . Load ( pathutils . RAPI_USERS_FILE ) <EOL> def ValidateRequest ( self , req , handler_access , realm ) : <EOL> """<STR_LIT>""" <EOL> request_username , request_password = HttpServerRequestAuthentication . ExtractUserPassword ( req ) <EOL> if request_username is None : <EOL> raise http . HttpUnauthorized ( ) <EOL> if request_password is None : <EOL> raise http . HttpBadRequest ( message = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> user = self . user_fn ( request_username ) <EOL> if not ( user and HttpServerRequestAuthentication <EOL> . VerifyBasicAuthPassword ( request_username , <EOL> request_password , <EOL> user . password , realm ) ) : <EOL> return None <EOL> if ( not handler_access or <EOL> set ( user . options ) . intersection ( handler_access ) ) : <EOL> return request_username <EOL> raise http . HttpForbidden ( ) </s>
