<s> import shutil <EOL> import subprocess <EOL> import tempfile <EOL> import logging <EOL> import time <EOL> import hashlib <EOL> import uuid <EOL> from OpenSSL import crypto <EOL> class EtcdProcessHelper ( object ) : <EOL> def __init__ ( <EOL> self , <EOL> base_directory , <EOL> proc_name = '<STR_LIT>' , <EOL> port_range_start = <NUM_LIT> , <EOL> internal_port_range_start = <NUM_LIT> , <EOL> cluster = False , <EOL> tls = False <EOL> ) : <EOL> self . base_directory = base_directory <EOL> self . proc_name = proc_name <EOL> self . port_range_start = port_range_start <EOL> self . internal_port_range_start = internal_port_range_start <EOL> self . processes = { } <EOL> self . cluster = cluster <EOL> self . schema = '<STR_LIT>' <EOL> if tls : <EOL> self . schema = '<STR_LIT>' <EOL> def run ( self , number = <NUM_LIT:1> , proc_args = [ ] ) : <EOL> if number > <NUM_LIT:1> : <EOL> initial_cluster = "<STR_LIT:U+002C>" . join ( [ "<STR_LIT>" . format ( slot , '<STR_LIT>' , self . internal_port_range_start + slot ) for slot in range ( <NUM_LIT:0> , number ) ] ) <EOL> proc_args . extend ( [ <EOL> '<STR_LIT>' , initial_cluster , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] ) <EOL> else : <EOL> proc_args . extend ( [ <EOL> '<STR_LIT>' , '<STR_LIT>' . format ( self . internal_port_range_start ) , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] ) <EOL> for i in range ( <NUM_LIT:0> , number ) : <EOL> self . add_one ( i , proc_args ) <EOL> def stop ( self ) : <EOL> log = logging . getLogger ( ) <EOL> for key in [ k for k in self . processes . keys ( ) ] : <EOL> self . kill_one ( key ) <EOL> def add_one ( self , slot , proc_args = None ) : <EOL> log = logging . getLogger ( ) <EOL> directory = tempfile . mkdtemp ( <EOL> dir = self . base_directory , <EOL> prefix = '<STR_LIT>' % slot ) <EOL> log . debug ( '<STR_LIT>' % directory ) <EOL> client = '<STR_LIT>' % ( self . schema , self . port_range_start + slot ) <EOL> peer = '<STR_LIT>' % ( '<STR_LIT>' , self . internal_port_range_start <EOL> + slot ) <EOL> daemon_args = [ <EOL> self . proc_name , <EOL> '<STR_LIT>' , directory , <EOL> '<STR_LIT>' , '<STR_LIT>' % slot , <EOL> '<STR_LIT>' , peer , <EOL> '<STR_LIT>' , peer , <EOL> '<STR_LIT>' , client , <EOL> '<STR_LIT>' , client <EOL> ] <EOL> if proc_args : <EOL> daemon_args . extend ( proc_args ) <EOL> daemon = subprocess . Popen ( daemon_args ) <EOL> log . debug ( '<STR_LIT>' % daemon . pid ) <EOL> log . debug ( '<STR_LIT>' % daemon_args ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> self . processes [ slot ] = ( directory , daemon ) <EOL> def kill_one ( self , slot ) : <EOL> log = logging . getLogger ( ) <EOL> data_dir , process = self . processes . pop ( slot ) <EOL> process . kill ( ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> log . debug ( '<STR_LIT>' , process . pid ) <EOL> shutil . rmtree ( data_dir ) <EOL> log . debug ( '<STR_LIT>' % data_dir ) <EOL> class TestingCA ( object ) : <EOL> @ classmethod <EOL> def create_test_ca_certificate ( cls , cert_path , key_path , cn = None ) : <EOL> k = crypto . PKey ( ) <EOL> k . generate_key ( crypto . TYPE_RSA , <NUM_LIT> ) <EOL> cert = crypto . X509 ( ) <EOL> if not cn : <EOL> serial = uuid . uuid4 ( ) . int <EOL> else : <EOL> md5_hash = hashlib . md5 ( ) <EOL> md5_hash . update ( cn . encode ( '<STR_LIT:utf-8>' ) ) <EOL> serial = int ( md5_hash . hexdigest ( ) , <NUM_LIT> ) <EOL> cert . get_subject ( ) . CN = cn <EOL> cert . get_subject ( ) . C = "<STR_LIT>" <EOL> cert . get_subject ( ) . ST = "<STR_LIT>" <EOL> cert . get_subject ( ) . L = "<STR_LIT>" <EOL> cert . get_subject ( ) . O = "<STR_LIT>" <EOL> cert . get_subject ( ) . OU = "<STR_LIT>" <EOL> cert . set_serial_number ( serial ) <EOL> cert . gmtime_adj_notBefore ( <NUM_LIT:0> ) <EOL> cert . gmtime_adj_notAfter ( <NUM_LIT> ) <EOL> cert . set_issuer ( cert . get_subject ( ) ) <EOL> cert . set_pubkey ( k ) <EOL> cert . add_extensions ( [ <EOL> crypto . X509Extension ( "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) , False , <EOL> "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) ) , <EOL> crypto . X509Extension ( "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) , False , <EOL> "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) ) , <EOL> crypto . X509Extension ( "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) , False , <EOL> "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) , <EOL> subject = cert ) , <EOL> ] ) <EOL> cert . add_extensions ( [ <EOL> crypto . X509Extension ( <EOL> "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) , False , <EOL> "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) , issuer = cert ) <EOL> ] ) <EOL> cert . sign ( k , '<STR_LIT>' ) <EOL> with open ( cert_path , '<STR_LIT:w>' ) as f : <EOL> f . write ( crypto . dump_certificate ( crypto . FILETYPE_PEM , cert ) <EOL> . decode ( '<STR_LIT:utf-8>' ) ) <EOL> with open ( key_path , '<STR_LIT:w>' ) as f : <EOL> f . write ( crypto . dump_privatekey ( crypto . FILETYPE_PEM , k ) <EOL> . decode ( '<STR_LIT:utf-8>' ) ) <EOL> return cert , k <EOL> @ classmethod <EOL> def create_test_certificate ( cls , ca , ca_key , cert_path , key_path , cn = None ) : <EOL> k = crypto . PKey ( ) <EOL> k . generate_key ( crypto . TYPE_RSA , <NUM_LIT> ) <EOL> cert = crypto . X509 ( ) <EOL> if not cn : <EOL> serial = uuid . uuid4 ( ) . int <EOL> else : <EOL> md5_hash = hashlib . md5 ( ) <EOL> md5_hash . update ( cn . encode ( '<STR_LIT:utf-8>' ) ) <EOL> serial = int ( md5_hash . hexdigest ( ) , <NUM_LIT> ) <EOL> cert . get_subject ( ) . CN = cn <EOL> cert . get_subject ( ) . C = "<STR_LIT>" <EOL> cert . get_subject ( ) . ST = "<STR_LIT>" <EOL> cert . get_subject ( ) . L = "<STR_LIT>" <EOL> cert . get_subject ( ) . O = "<STR_LIT>" <EOL> cert . get_subject ( ) . OU = "<STR_LIT>" <EOL> cert . add_extensions ( [ <EOL> crypto . X509Extension ( <EOL> "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) , <EOL> False , <EOL> "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) ) , <EOL> crypto . X509Extension ( <EOL> "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) , <EOL> False , <EOL> "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) ) , <EOL> crypto . X509Extension ( <EOL> "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) , <EOL> False , <EOL> "<STR_LIT>" . encode ( '<STR_LIT:ascii>' ) ) , <EOL> ] ) <EOL> cert . gmtime_adj_notBefore ( <NUM_LIT:0> ) <EOL> cert . gmtime_adj_notAfter ( <NUM_LIT> ) <EOL> cert . set_issuer ( ca . get_subject ( ) ) <EOL> cert . set_pubkey ( k ) <EOL> cert . set_serial_number ( serial ) <EOL> cert . sign ( ca_key , '<STR_LIT>' ) <EOL> with open ( cert_path , '<STR_LIT:w>' ) as f : <EOL> f . write ( crypto . dump_certificate ( crypto . FILETYPE_PEM , cert ) <EOL> . decode ( '<STR_LIT:utf-8>' ) ) <EOL> with open ( key_path , '<STR_LIT:w>' ) as f : <EOL> f . write ( crypto . dump_privatekey ( crypto . FILETYPE_PEM , k ) <EOL> . decode ( '<STR_LIT:utf-8>' ) ) </s>
