<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import base64 <EOL> import hashlib <EOL> import commands <EOL> from subprocess import call <EOL> from error import InputErrorException , SignatureException <EOL> from oshelpers import readContentOf , isValidFile <EOL> import paramiko . agent <EOL> from paramiko . rsakey import RSAKey <EOL> from paramiko . ssh_exception import PasswordRequiredException <EOL> from paramiko . message import Message <EOL> def is_key_valid ( key ) : <EOL> """<STR_LIT>""" <EOL> if not isValidFile ( key ) : <EOL> return False <EOL> file_content = readContentOf ( key ) <EOL> if not file_content : <EOL> return False <EOL> if file_content [ : <NUM_LIT:8> ] != '<STR_LIT>' : <EOL> return False <EOL> return True <EOL> def generate_rsa_keys ( ) : <EOL> """<STR_LIT>""" <EOL> ssh_path = get_default_ssh_dir ( ) <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> return generate_rsa_key_manually ( ssh_path ) <EOL> if os . path . exists ( get_default_ssh_key_path ( ) ) : <EOL> return False <EOL> error_code = commands . getstatusoutput ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> if error_code != <NUM_LIT:0> : <EOL> return False <EOL> call ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , ssh_path + "<STR_LIT>" ] ) <EOL> return True <EOL> def generate_rsa_key_manually ( user_ssh_path , key_file_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if key_file_name == "<STR_LIT>" : <EOL> if not os . path . exists ( user_ssh_path ) : <EOL> os . mkdir ( user_ssh_path , <NUM_LIT:0> <NUM_LIT> ) <EOL> key_base_name = key_file_name . rstrip ( "<STR_LIT>" ) <EOL> private_key = generate_private_rsa_key_file ( user_ssh_path , key_base_name ) <EOL> generate_public_rsa_key_file ( private_key , user_ssh_path , key_base_name ) <EOL> return True <EOL> def generate_private_rsa_key_file ( ssh_path , key_base_name ) : <EOL> """<STR_LIT>""" <EOL> import warnings <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> from paramiko import RSAKey <EOL> key = RSAKey . generate ( <NUM_LIT> ) <EOL> private_key_file_name = ssh_path + "<STR_LIT:/>" + key_base_name <EOL> key . write_private_key_file ( private_key_file_name ) <EOL> return key <EOL> def generate_public_rsa_key_file ( private_key , ssh_path , key_base_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> username = os . getlogin ( ) <EOL> except AttributeError : <EOL> username = os . getenv ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> hostname = os . uname ( ) [ <NUM_LIT:1> ] <EOL> except AttributeError : <EOL> hostname = os . getenv ( '<STR_LIT>' , '<STR_LIT:localhost>' ) <EOL> pubkey_string = '<STR_LIT>' . format ( <EOL> private_key . get_base64 ( ) , <EOL> username , <EOL> hostname ) <EOL> public_key_filename = ssh_path + "<STR_LIT:/>" + key_base_name + "<STR_LIT>" <EOL> pubkey = open ( public_key_filename , '<STR_LIT:w>' ) <EOL> pubkey . write ( pubkey_string ) <EOL> pubkey . close ( ) <EOL> return True <EOL> def create_new_default_ssh_keys ( ) : <EOL> """<STR_LIT>""" <EOL> question = raw_input ( '<STR_LIT>' ) <EOL> if question . lower ( ) != '<STR_LIT:yes>' : <EOL> raise InputErrorException ( '<STR_LIT>' ) <EOL> if not generate_rsa_keys ( ) : <EOL> raise InputErrorException ( '<STR_LIT>' ) <EOL> return True <EOL> def ask_user_to_use_default_ssh_public_key ( ) : <EOL> """<STR_LIT>""" <EOL> default_rsa_public_key = get_default_ssh_key_path ( ) <EOL> if not isValidFile ( default_rsa_public_key ) : <EOL> return "<STR_LIT>" <EOL> question = raw_input ( "<STR_LIT>" . format ( default_rsa_public_key ) + <EOL> '<STR_LIT>' ) <EOL> if question . lower ( ) != '<STR_LIT:yes>' : <EOL> raise InputErrorException ( '<STR_LIT>' ) <EOL> return <NUM_LIT:0> <EOL> def get_public_key_fingerprint ( key_path ) : <EOL> if not key_path : <EOL> key_path = get_default_ssh_key_path ( ) <EOL> try : <EOL> public_key = open ( key_path ) . read ( ) <EOL> except IOError : <EOL> return None <EOL> key = base64 . b64decode ( public_key . strip ( ) . split ( ) [ <NUM_LIT:1> ] . encode ( '<STR_LIT:ascii>' ) ) <EOL> fp_plain = hashlib . md5 ( key ) . hexdigest ( ) <EOL> return '<STR_LIT::>' . join ( a + b for a , b in zip ( fp_plain [ : : <NUM_LIT:2> ] , fp_plain [ <NUM_LIT:1> : : <NUM_LIT:2> ] ) ) <EOL> def get_default_ssh_dir ( ) : <EOL> return os . path . join ( os . path . expanduser ( '<STR_LIT>' ) , "<STR_LIT>" ) <EOL> def get_default_ssh_key_path ( ) : <EOL> return get_default_ssh_dir ( ) + "<STR_LIT>" <EOL> def get_key_from_agent ( fingerprint ) : <EOL> a = paramiko . agent . Agent ( ) <EOL> for key in a . get_keys ( ) : <EOL> fp = base64 . b16encode ( key . get_fingerprint ( ) ) <EOL> if fp == fingerprint . replace ( '<STR_LIT::>' , '<STR_LIT>' ) . upper ( ) : <EOL> return key <EOL> return None <EOL> def sign_token ( key_path , fingerprint , data ) : <EOL> pkey = get_key_from_agent ( fingerprint ) <EOL> if not pkey : <EOL> if not os . path . exists ( key_path [ : - <NUM_LIT:4> ] ) : <EOL> raise SignatureException ( '<STR_LIT>' ) <EOL> try : <EOL> pkey = RSAKey . from_private_key_file ( key_path [ : - <NUM_LIT:4> ] ) <EOL> except PasswordRequiredException : <EOL> raise SignatureException ( '<STR_LIT>' ) <EOL> if not pkey : <EOL> raise SignatureException ( '<STR_LIT>' ) <EOL> try : <EOL> s = str ( pkey . sign_ssh_data ( data ) ) <EOL> m = Message ( s ) <EOL> m . rewind ( ) <EOL> if not m . get_string ( ) == '<STR_LIT>' : <EOL> raise SignatureException ( '<STR_LIT>' ) <EOL> return base64 . b64encode ( m . get_string ( ) ) <EOL> except Exception : <EOL> raise SignatureException ( '<STR_LIT>' ) </s>
