<s> import json <EOL> from time import time <EOL> from swift . common . utils import public , get_hmac , get_swift_info , streq_const_time <EOL> from swift . proxy . controllers . base import Controller , delay_denial <EOL> from swift . common . swob import HTTPOk , HTTPForbidden , HTTPUnauthorized <EOL> class InfoController ( Controller ) : <EOL> """<STR_LIT>""" <EOL> server_type = '<STR_LIT>' <EOL> def __init__ ( self , app , version , expose_info , disallowed_sections , <EOL> admin_key ) : <EOL> Controller . __init__ ( self , app ) <EOL> self . expose_info = expose_info <EOL> self . disallowed_sections = disallowed_sections <EOL> self . admin_key = admin_key <EOL> self . allowed_hmac_methods = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:GET>' ] , <EOL> '<STR_LIT:GET>' : [ '<STR_LIT:GET>' ] } <EOL> @ public <EOL> @ delay_denial <EOL> def GET ( self , req ) : <EOL> return self . GETorHEAD ( req ) <EOL> @ public <EOL> @ delay_denial <EOL> def HEAD ( self , req ) : <EOL> return self . GETorHEAD ( req ) <EOL> @ public <EOL> @ delay_denial <EOL> def OPTIONS ( self , req ) : <EOL> return HTTPOk ( request = req , headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def GETorHEAD ( self , req ) : <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> if not self . expose_info : <EOL> return HTTPForbidden ( request = req ) <EOL> admin_request = False <EOL> sig = req . params . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> expires = req . params . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if sig != '<STR_LIT>' or expires != '<STR_LIT>' : <EOL> admin_request = True <EOL> if not self . admin_key : <EOL> return HTTPForbidden ( request = req ) <EOL> try : <EOL> expires = int ( expires ) <EOL> except ValueError : <EOL> return HTTPUnauthorized ( request = req ) <EOL> if expires < time ( ) : <EOL> return HTTPUnauthorized ( request = req ) <EOL> valid_sigs = [ ] <EOL> for method in self . allowed_hmac_methods [ req . method ] : <EOL> valid_sigs . append ( get_hmac ( method , <EOL> '<STR_LIT>' , <EOL> expires , <EOL> self . admin_key ) ) <EOL> is_valid_hmac = any ( streq_const_time ( valid_sig , sig ) <EOL> for valid_sig in valid_sigs ) <EOL> if not is_valid_hmac : <EOL> return HTTPUnauthorized ( request = req ) <EOL> headers = { } <EOL> if '<STR_LIT>' in req . headers : <EOL> headers [ '<STR_LIT>' ] = req . headers [ '<STR_LIT>' ] <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:U+002CU+0020>' . join ( <EOL> [ '<STR_LIT>' ] ) <EOL> info = json . dumps ( get_swift_info ( <EOL> admin = admin_request , disallowed_sections = self . disallowed_sections ) ) <EOL> return HTTPOk ( request = req , <EOL> headers = headers , <EOL> body = info , <EOL> content_type = '<STR_LIT>' ) </s>
