<s> """<STR_LIT>""" <EOL> import base64 <EOL> import hashlib <EOL> import hmac <EOL> import urllib <EOL> import boto <EOL> import boto . utils <EOL> from nova import log as logging <EOL> from nova . exception import Error <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> class Signer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , secret_key ) : <EOL> self . hmac = hmac . new ( secret_key , digestmod = hashlib . sha1 ) <EOL> if hashlib . sha256 : <EOL> self . hmac_256 = hmac . new ( secret_key , digestmod = hashlib . sha256 ) <EOL> def s3_authorization ( self , headers , verb , path ) : <EOL> """<STR_LIT>""" <EOL> c_string = boto . utils . canonical_string ( verb , path , headers ) <EOL> hmac_copy = self . hmac . copy ( ) <EOL> hmac_copy . update ( c_string ) <EOL> b64_hmac = base64 . encodestring ( hmac_copy . digest ( ) ) . strip ( ) <EOL> return b64_hmac <EOL> def generate ( self , params , verb , server_string , path ) : <EOL> """<STR_LIT>""" <EOL> if params [ '<STR_LIT>' ] == '<STR_LIT:0>' : <EOL> return self . _calc_signature_0 ( params ) <EOL> if params [ '<STR_LIT>' ] == '<STR_LIT:1>' : <EOL> return self . _calc_signature_1 ( params ) <EOL> if params [ '<STR_LIT>' ] == '<STR_LIT:2>' : <EOL> return self . _calc_signature_2 ( params , verb , server_string , path ) <EOL> raise Error ( '<STR_LIT>' % <EOL> params [ '<STR_LIT>' ] ) <EOL> @ staticmethod <EOL> def _get_utf8_value ( value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , str ) and not isinstance ( value , unicode ) : <EOL> value = str ( value ) <EOL> if isinstance ( value , unicode ) : <EOL> return value . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> return value <EOL> def _calc_signature_0 ( self , params ) : <EOL> """<STR_LIT>""" <EOL> s = params [ '<STR_LIT>' ] + params [ '<STR_LIT>' ] <EOL> self . hmac . update ( s ) <EOL> keys = params . keys ( ) <EOL> keys . sort ( cmp = lambda x , y : cmp ( x . lower ( ) , y . lower ( ) ) ) <EOL> pairs = [ ] <EOL> for key in keys : <EOL> val = self . _get_utf8_value ( params [ key ] ) <EOL> pairs . append ( key + '<STR_LIT:=>' + urllib . quote ( val ) ) <EOL> return base64 . b64encode ( self . hmac . digest ( ) ) <EOL> def _calc_signature_1 ( self , params ) : <EOL> """<STR_LIT>""" <EOL> keys = params . keys ( ) <EOL> keys . sort ( cmp = lambda x , y : cmp ( x . lower ( ) , y . lower ( ) ) ) <EOL> pairs = [ ] <EOL> for key in keys : <EOL> self . hmac . update ( key ) <EOL> val = self . _get_utf8_value ( params [ key ] ) <EOL> self . hmac . update ( val ) <EOL> pairs . append ( key + '<STR_LIT:=>' + urllib . quote ( val ) ) <EOL> return base64 . b64encode ( self . hmac . digest ( ) ) <EOL> def _calc_signature_2 ( self , params , verb , server_string , path ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> string_to_sign = '<STR_LIT>' % ( verb , server_string , path ) <EOL> if self . hmac_256 : <EOL> current_hmac = self . hmac_256 <EOL> params [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> current_hmac = self . hmac <EOL> params [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> keys = params . keys ( ) <EOL> keys . sort ( ) <EOL> pairs = [ ] <EOL> for key in keys : <EOL> val = self . _get_utf8_value ( params [ key ] ) <EOL> val = urllib . quote ( val , safe = '<STR_LIT>' ) <EOL> pairs . append ( urllib . quote ( key , safe = '<STR_LIT>' ) + '<STR_LIT:=>' + val ) <EOL> qs = '<STR_LIT:&>' . join ( pairs ) <EOL> LOG . debug ( '<STR_LIT>' , qs ) <EOL> string_to_sign += qs <EOL> LOG . debug ( '<STR_LIT>' , string_to_sign ) <EOL> current_hmac . update ( string_to_sign ) <EOL> b64 = base64 . b64encode ( current_hmac . digest ( ) ) <EOL> LOG . debug ( '<STR_LIT>' , len ( b64 ) ) <EOL> LOG . debug ( '<STR_LIT>' , b64 ) <EOL> return b64 <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> print Signer ( '<STR_LIT:foo>' ) . generate ( { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' } , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) </s>
