<s> from subprocess import call <EOL> import os <EOL> TMPL_GEN_TS = "<STR_LIT>" <EOL> DEV_NULL = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> class Keytool : <EOL> def __init__ ( self , cadir , hostname , store_password , hosts_to_trust ) : <EOL> self . cadir = cadir <EOL> self . hostname = hostname <EOL> self . store_password = store_password <EOL> self . hosts_to_trust = hosts_to_trust <EOL> def execute_command ( self , cmd ) : <EOL> call ( cmd , shell = True , stdout = DEV_NULL , stderr = DEV_NULL ) <EOL> def validate ( self ) : <EOL> if not os . path . exists ( self . cadir ) : <EOL> return dict ( success = False , msg = "<STR_LIT>" . format ( self . cadir ) ) <EOL> elif len ( self . hosts_to_trust ) == <NUM_LIT:0> : <EOL> return dict ( success = False , msg = "<STR_LIT>" ) <EOL> else : <EOL> return dict ( success = True ) <EOL> def ensure_directory_exists ( self , dir ) : <EOL> if not os . path . exists ( dir ) : <EOL> os . mkdir ( dir ) <EOL> def get_truststore_path ( self ) : <EOL> return "<STR_LIT>" + os . sep + self . hostname + "<STR_LIT>" <EOL> def get_storepass_path ( self ) : <EOL> return self . hostname + "<STR_LIT>" <EOL> def resolve_certificate ( self , host ) : <EOL> server = "<STR_LIT>" . format ( host ) <EOL> client = "<STR_LIT>" . format ( host ) <EOL> if os . path . exists ( server ) : <EOL> return server <EOL> elif os . path . exists ( client ) : <EOL> return client <EOL> else : <EOL> return None <EOL> def build_trust_store ( self ) : <EOL> changed = False <EOL> success = True <EOL> errors = [ ] <EOL> changes = [ ] <EOL> CURDIR = os . getcwd ( ) <EOL> os . chdir ( self . cadir ) <EOL> self . ensure_directory_exists ( "<STR_LIT>" ) <EOL> truststore_path = self . get_truststore_path ( ) <EOL> storepass_path = self . get_storepass_path ( ) <EOL> if not os . path . exists ( truststore_path ) : <EOL> with open ( storepass_path , "<STR_LIT:w>" ) as storepass : <EOL> storepass . write ( self . store_password ) <EOL> try : <EOL> cmd = TMPL_GEN_TS . format ( "<STR_LIT>" , "<STR_LIT>" , truststore_path , self . hostname ) <EOL> self . execute_command ( cmd ) <EOL> changed = True <EOL> changes . append ( "<STR_LIT>" ) <EOL> for host in self . hosts_to_trust : <EOL> hostcert = self . resolve_certificate ( host ) <EOL> if not hostcert is None : <EOL> cmd = TMPL_GEN_TS . format ( host , hostcert , truststore_path , self . hostname ) <EOL> changes . append ( "<STR_LIT>" . format ( cmd ) ) <EOL> self . execute_command ( cmd ) <EOL> changed = True <EOL> changes . append ( "<STR_LIT>" . format ( host ) ) <EOL> else : <EOL> success = False <EOL> errors . append ( "<STR_LIT>" . format ( host ) ) <EOL> except Exception as e : <EOL> success = False <EOL> errors . append ( e . message ) <EOL> finally : <EOL> os . remove ( storepass_path ) <EOL> if success == False : <EOL> os . remove ( truststore_path ) <EOL> os . chdir ( CURDIR ) <EOL> return dict ( success = success , changed = changed , changes = changes , path = truststore_path , errors = errors , msg = "<STR_LIT:U+002CU+0020>" . join ( errors ) ) <EOL> def remove_trust_store ( self ) : <EOL> changed = False <EOL> changes = [ ] <EOL> CURDIR = os . getcwd ( ) <EOL> os . chdir ( self . cadir ) <EOL> truststore_path = self . get_truststore_path ( ) <EOL> if os . path . exists ( truststore_path ) : <EOL> os . remove ( truststore_path ) <EOL> changed = True <EOL> changes . append ( "<STR_LIT>" ) <EOL> os . chdir ( CURDIR ) <EOL> return dict ( success = True , changed = changed , changes = changes , msg = "<STR_LIT>" ) </s>
