<s> """<STR_LIT>""" <EOL> from urlparse import urlparse <EOL> import eventlet <EOL> from eventlet . green import httplib <EOL> import webob <EOL> import webob . dec <EOL> import webob . exc <EOL> from nova import context <EOL> from nova import exception <EOL> from nova import flags <EOL> from nova import log as logging <EOL> from nova import utils <EOL> from nova import wsgi <EOL> from nova . api . ec2 import apirequest <EOL> from nova . api . ec2 import ec2utils <EOL> from nova . auth import manager <EOL> FLAGS = flags . FLAGS <EOL> LOG = logging . getLogger ( "<STR_LIT>" ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:5> , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:15> , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:15> , <EOL> '<STR_LIT>' ) <EOL> flags . DECLARE ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class RequestLogging ( wsgi . Middleware ) : <EOL> """<STR_LIT>""" <EOL> @ webob . dec . wsgify ( RequestClass = wsgi . Request ) <EOL> def __call__ ( self , req ) : <EOL> start = utils . utcnow ( ) <EOL> rv = req . get_response ( self . application ) <EOL> self . log_request_completion ( rv , req , start ) <EOL> return rv <EOL> def log_request_completion ( self , response , request , start ) : <EOL> apireq = request . environ . get ( '<STR_LIT>' , None ) <EOL> if apireq : <EOL> controller = apireq . controller <EOL> action = apireq . action <EOL> else : <EOL> controller = None <EOL> action = None <EOL> ctxt = request . environ . get ( '<STR_LIT>' , None ) <EOL> delta = utils . utcnow ( ) - start <EOL> seconds = delta . seconds <EOL> microseconds = delta . microseconds <EOL> LOG . info ( <EOL> "<STR_LIT>" , <EOL> seconds , <EOL> microseconds , <EOL> request . remote_addr , <EOL> request . method , <EOL> "<STR_LIT>" % ( request . script_name , request . path_info ) , <EOL> controller , <EOL> action , <EOL> response . status_int , <EOL> request . user_agent , <EOL> request . content_type , <EOL> response . content_type , <EOL> context = ctxt ) <EOL> class Lockout ( wsgi . Middleware ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , application ) : <EOL> """<STR_LIT>""" <EOL> if FLAGS . memcached_servers : <EOL> import memcache <EOL> else : <EOL> from nova import fakememcache as memcache <EOL> self . mc = memcache . Client ( FLAGS . memcached_servers , <EOL> debug = <NUM_LIT:0> ) <EOL> super ( Lockout , self ) . __init__ ( application ) <EOL> @ webob . dec . wsgify ( RequestClass = wsgi . Request ) <EOL> def __call__ ( self , req ) : <EOL> access_key = str ( req . params [ '<STR_LIT>' ] ) <EOL> failures_key = "<STR_LIT>" % access_key <EOL> failures = int ( self . mc . get ( failures_key ) or <NUM_LIT:0> ) <EOL> if failures >= FLAGS . lockout_attempts : <EOL> detail = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPForbidden ( detail = detail ) <EOL> res = req . get_response ( self . application ) <EOL> if res . status_int == <NUM_LIT> : <EOL> failures = self . mc . incr ( failures_key ) <EOL> if failures is None : <EOL> self . mc . set ( failures_key , '<STR_LIT:1>' , time = FLAGS . lockout_window * <NUM_LIT> ) <EOL> elif failures >= FLAGS . lockout_attempts : <EOL> lock_mins = FLAGS . lockout_minutes <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) <EOL> LOG . warn ( msg ) <EOL> self . mc . set ( failures_key , str ( failures ) , <EOL> time = FLAGS . lockout_minutes * <NUM_LIT> ) <EOL> return res <EOL> class NoAuth ( wsgi . Middleware ) : <EOL> """<STR_LIT>""" <EOL> @ webob . dec . wsgify ( RequestClass = wsgi . Request ) <EOL> def __call__ ( self , req ) : <EOL> if '<STR_LIT>' not in req . params : <EOL> raise webob . exc . HTTPBadRequest ( ) <EOL> user_id , _sep , project_id = req . params [ '<STR_LIT>' ] . partition ( '<STR_LIT::>' ) <EOL> project_id = project_id or user_id <EOL> remote_address = getattr ( req , '<STR_LIT>' , '<STR_LIT:127.0.0.1>' ) <EOL> if FLAGS . use_forwarded_for : <EOL> remote_address = req . headers . get ( '<STR_LIT>' , remote_address ) <EOL> ctx = context . RequestContext ( user_id , <EOL> project_id , <EOL> is_admin = True , <EOL> remote_address = remote_address ) <EOL> req . environ [ '<STR_LIT>' ] = ctx <EOL> return self . application <EOL> class Authenticate ( wsgi . Middleware ) : <EOL> """<STR_LIT>""" <EOL> @ webob . dec . wsgify ( RequestClass = wsgi . Request ) <EOL> def __call__ ( self , req ) : <EOL> try : <EOL> signature = req . params [ '<STR_LIT>' ] <EOL> access = req . params [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise webob . exc . HTTPBadRequest ( ) <EOL> auth_params = dict ( req . params ) <EOL> auth_params . pop ( '<STR_LIT>' ) <EOL> authman = manager . AuthManager ( ) <EOL> try : <EOL> ( user , project ) = authman . authenticate ( <EOL> access , <EOL> signature , <EOL> auth_params , <EOL> req . method , <EOL> req . host , <EOL> req . path ) <EOL> except ( exception . NotFound , exception . NotAuthorized , <EOL> exception . InvalidSignature ) as ex : <EOL> LOG . audit ( _ ( "<STR_LIT>" ) , unicode ( ex ) ) <EOL> raise webob . exc . HTTPForbidden ( ) <EOL> remote_address = req . remote_addr <EOL> if FLAGS . use_forwarded_for : <EOL> remote_address = req . headers . get ( '<STR_LIT>' , remote_address ) <EOL> roles = authman . get_active_roles ( user , project ) <EOL> ctxt = context . RequestContext ( user_id = user . id , <EOL> project_id = project . id , <EOL> is_admin = user . is_admin ( ) , <EOL> roles = roles , <EOL> remote_address = remote_address ) <EOL> req . environ [ '<STR_LIT>' ] = ctxt <EOL> uname = user . name <EOL> pname = project . name <EOL> msg = _ ( '<STR_LIT>' ) % locals ( ) <EOL> LOG . audit ( msg , context = req . environ [ '<STR_LIT>' ] ) <EOL> return self . application <EOL> class Requestify ( wsgi . Middleware ) : <EOL> def __init__ ( self , app , controller ) : <EOL> super ( Requestify , self ) . __init__ ( app ) <EOL> self . controller = utils . import_class ( controller ) ( ) <EOL> @ webob . dec . wsgify ( RequestClass = wsgi . Request ) <EOL> def __call__ ( self , req ) : <EOL> non_args = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> args = dict ( req . params ) <EOL> try : <EOL> action = req . params [ '<STR_LIT>' ] <EOL> version = req . params [ '<STR_LIT>' ] <EOL> if int ( version ) == <NUM_LIT:1> : <EOL> non_args . remove ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in args : <EOL> args . pop ( '<STR_LIT>' ) <EOL> for non_arg in non_args : <EOL> args . pop ( non_arg ) <EOL> except KeyError , e : <EOL> raise webob . exc . HTTPBadRequest ( ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , action ) <EOL> for key , value in args . items ( ) : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) % locals ( ) ) <EOL> api_request = apirequest . APIRequest ( self . controller , action , <EOL> req . params [ '<STR_LIT>' ] , args ) <EOL> req . environ [ '<STR_LIT>' ] = api_request <EOL> req . environ [ '<STR_LIT>' ] = args <EOL> return self . application <EOL> class Authorizer ( wsgi . Middleware ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , application ) : <EOL> super ( Authorizer , self ) . __init__ ( application ) <EOL> self . action_roles = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:all>' ] , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> } , <EOL> } <EOL> @ webob . dec . wsgify ( RequestClass = wsgi . Request ) <EOL> def __call__ ( self , req ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> controller = req . environ [ '<STR_LIT>' ] . controller . __class__ . __name__ <EOL> action = req . environ [ '<STR_LIT>' ] . action <EOL> allowed_roles = self . action_roles [ controller ] . get ( action , [ '<STR_LIT:none>' ] ) <EOL> if self . _matches_any_role ( context , allowed_roles ) : <EOL> return self . application <EOL> else : <EOL> LOG . audit ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) , context = context ) <EOL> raise webob . exc . HTTPUnauthorized ( ) <EOL> def _matches_any_role ( self , context , roles ) : <EOL> """<STR_LIT>""" <EOL> if context . is_admin : <EOL> return True <EOL> if '<STR_LIT:all>' in roles : <EOL> return True <EOL> if '<STR_LIT:none>' in roles : <EOL> return False <EOL> return any ( role in context . roles for role in roles ) <EOL> class Executor ( wsgi . Application ) : <EOL> """<STR_LIT>""" <EOL> @ webob . dec . wsgify ( RequestClass = wsgi . Request ) <EOL> def __call__ ( self , req ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> api_request = req . environ [ '<STR_LIT>' ] <EOL> result = None <EOL> try : <EOL> result = api_request . invoke ( context ) <EOL> except exception . InstanceNotFound as ex : <EOL> LOG . info ( _ ( '<STR_LIT>' ) , unicode ( ex ) , <EOL> context = context ) <EOL> ec2_id = ec2utils . id_to_ec2_id ( ex . kwargs [ '<STR_LIT>' ] ) <EOL> message = ex . message % { '<STR_LIT>' : ec2_id } <EOL> return self . _error ( req , context , type ( ex ) . __name__ , message ) <EOL> except exception . VolumeNotFound as ex : <EOL> LOG . info ( _ ( '<STR_LIT>' ) , unicode ( ex ) , <EOL> context = context ) <EOL> ec2_id = ec2utils . id_to_ec2_vol_id ( ex . kwargs [ '<STR_LIT>' ] ) <EOL> message = ex . message % { '<STR_LIT>' : ec2_id } <EOL> return self . _error ( req , context , type ( ex ) . __name__ , message ) <EOL> except exception . SnapshotNotFound as ex : <EOL> LOG . info ( _ ( '<STR_LIT>' ) , unicode ( ex ) , <EOL> context = context ) <EOL> ec2_id = ec2utils . id_to_ec2_snap_id ( ex . kwargs [ '<STR_LIT>' ] ) <EOL> message = ex . message % { '<STR_LIT>' : ec2_id } <EOL> return self . _error ( req , context , type ( ex ) . __name__ , message ) <EOL> except exception . NotFound as ex : <EOL> LOG . info ( _ ( '<STR_LIT>' ) , unicode ( ex ) , context = context ) <EOL> return self . _error ( req , context , type ( ex ) . __name__ , unicode ( ex ) ) <EOL> except exception . ApiError as ex : <EOL> LOG . exception ( _ ( '<STR_LIT>' ) , unicode ( ex ) , <EOL> context = context ) <EOL> if ex . code : <EOL> return self . _error ( req , context , ex . code , unicode ( ex ) ) <EOL> else : <EOL> return self . _error ( req , context , type ( ex ) . __name__ , <EOL> unicode ( ex ) ) <EOL> except exception . KeyPairExists as ex : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , unicode ( ex ) , <EOL> context = context ) <EOL> return self . _error ( req , context , type ( ex ) . __name__ , unicode ( ex ) ) <EOL> except exception . InvalidParameterValue as ex : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , unicode ( ex ) , <EOL> context = context ) <EOL> return self . _error ( req , context , type ( ex ) . __name__ , unicode ( ex ) ) <EOL> except exception . InvalidPortRange as ex : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , unicode ( ex ) , <EOL> context = context ) <EOL> return self . _error ( req , context , type ( ex ) . __name__ , unicode ( ex ) ) <EOL> except exception . NotAuthorized as ex : <EOL> LOG . info ( _ ( '<STR_LIT>' ) , unicode ( ex ) , <EOL> context = context ) <EOL> return self . _error ( req , context , type ( ex ) . __name__ , unicode ( ex ) ) <EOL> except exception . AssociateAddressFailed as ex : <EOL> LOG . info ( _ ( '<STR_LIT>' ) , unicode ( ex ) , <EOL> context = context ) <EOL> return self . _error ( req , context , type ( ex ) . __name__ , unicode ( ex ) ) <EOL> except exception . RunInstancesFailed as ex : <EOL> LOG . info ( _ ( '<STR_LIT>' ) , unicode ( ex ) , <EOL> context = context ) <EOL> return self . _error ( req , context , type ( ex ) . __name__ , unicode ( ex ) ) <EOL> except Exception as ex : <EOL> extra = { '<STR_LIT>' : req . environ } <EOL> LOG . exception ( _ ( '<STR_LIT>' ) , unicode ( ex ) , <EOL> extra = extra , context = context ) <EOL> return self . _error ( req , <EOL> context , <EOL> '<STR_LIT>' , <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> else : <EOL> resp = webob . Response ( ) <EOL> resp . status = <NUM_LIT:200> <EOL> resp . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> resp . body = str ( result ) <EOL> return resp <EOL> def _error ( self , req , context , code , message ) : <EOL> LOG . error ( "<STR_LIT>" , code , message , context = context ) <EOL> resp = webob . Response ( ) <EOL> resp . status = <NUM_LIT> <EOL> resp . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> resp . body = str ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( utils . utf8 ( code ) , utils . utf8 ( message ) , <EOL> utils . utf8 ( context . request_id ) ) ) <EOL> return resp <EOL> class Versions ( wsgi . Application ) : <EOL> @ webob . dec . wsgify ( RequestClass = wsgi . Request ) <EOL> def __call__ ( self , req ) : <EOL> """<STR_LIT>""" <EOL> versions = [ <EOL> '<STR_LIT:1.0>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> return '<STR_LIT>' . join ( '<STR_LIT>' % v for v in versions ) </s>
