<s> import pywintypes <EOL> import re <EOL> from win32com import client <EOL> from xml . etree import ElementTree <EOL> from xml . sax import saxutils <EOL> CBT_HARDENING_LEVEL_NONE = "<STR_LIT:none>" <EOL> CBT_HARDENING_LEVEL_RELAXED = "<STR_LIT>" <EOL> CBT_HARDENING_LEVEL_STRICT = "<STR_LIT:strict>" <EOL> LISTENER_PROTOCOL_HTTP = "<STR_LIT>" <EOL> LISTENER_PROTOCOL_HTTPS = "<STR_LIT>" <EOL> class WinRMConfig ( object ) : <EOL> _SERVICE_AUTH_URI = '<STR_LIT>' <EOL> _SERVICE_LISTENER_URI = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> _SERVICE_CERTMAPPING_URI = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _get_wsman_session ( self ) : <EOL> wsman = client . Dispatch ( '<STR_LIT>' ) <EOL> return wsman . CreateSession ( ) <EOL> def _get_node_tag ( self , tag ) : <EOL> return re . match ( "<STR_LIT>" , tag ) . groups ( <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> def _parse_listener_xml ( self , data_xml ) : <EOL> if not data_xml : <EOL> return None <EOL> listening_on = [ ] <EOL> data = { "<STR_LIT>" : listening_on } <EOL> tree = ElementTree . fromstring ( data_xml ) <EOL> for node in tree : <EOL> tag = self . _get_node_tag ( node . tag ) <EOL> if tag == "<STR_LIT>" : <EOL> listening_on . append ( node . text ) <EOL> elif tag == "<STR_LIT>" : <EOL> if node . text == "<STR_LIT:true>" : <EOL> value = True <EOL> else : <EOL> value = False <EOL> data [ tag ] = value <EOL> elif tag == "<STR_LIT>" : <EOL> data [ tag ] = int ( node . text ) <EOL> else : <EOL> data [ tag ] = node . text <EOL> return data <EOL> def _parse_cert_mapping_xml ( self , data_xml ) : <EOL> if not data_xml : <EOL> return None <EOL> data = { } <EOL> tree = ElementTree . fromstring ( data_xml ) <EOL> for node in tree : <EOL> tag = self . _get_node_tag ( node . tag ) <EOL> if tag == "<STR_LIT>" : <EOL> if node . text == "<STR_LIT:true>" : <EOL> value = True <EOL> else : <EOL> value = False <EOL> data [ tag ] = value <EOL> else : <EOL> data [ tag ] = node . text <EOL> return data <EOL> def _get_xml_bool ( self , value ) : <EOL> if value : <EOL> return "<STR_LIT:true>" <EOL> else : <EOL> return "<STR_LIT:false>" <EOL> def _get_resource ( self , resource_uri ) : <EOL> session = self . _get_wsman_session ( ) <EOL> try : <EOL> return session . Get ( resource_uri ) <EOL> except pywintypes . com_error as ex : <EOL> if len ( ex . excepinfo ) > <NUM_LIT:5> and ex . excepinfo [ <NUM_LIT:5> ] == - <NUM_LIT> : <EOL> return None <EOL> else : <EOL> raise <EOL> def _delete_resource ( self , resource_uri ) : <EOL> session = self . _get_wsman_session ( ) <EOL> session . Delete ( resource_uri ) <EOL> def _create_resource ( self , resource_uri , data_xml ) : <EOL> session = self . _get_wsman_session ( ) <EOL> session . Create ( resource_uri , data_xml ) <EOL> def get_cert_mapping ( self , issuer , subject , uri = "<STR_LIT:*>" ) : <EOL> resource_uri = self . _SERVICE_CERTMAPPING_URI % { '<STR_LIT>' : issuer , <EOL> '<STR_LIT>' : subject , <EOL> '<STR_LIT>' : uri } <EOL> return self . _parse_cert_mapping_xml ( self . _get_resource ( resource_uri ) ) <EOL> def delete_cert_mapping ( self , issuer , subject , uri = "<STR_LIT:*>" ) : <EOL> resource_uri = self . _SERVICE_CERTMAPPING_URI % { '<STR_LIT>' : issuer , <EOL> '<STR_LIT>' : subject , <EOL> '<STR_LIT>' : uri } <EOL> self . _delete_resource ( resource_uri ) <EOL> def create_cert_mapping ( self , issuer , subject , username , password , <EOL> uri = "<STR_LIT:*>" , enabled = True ) : <EOL> resource_uri = self . _SERVICE_CERTMAPPING_URI % { '<STR_LIT>' : issuer , <EOL> '<STR_LIT>' : subject , <EOL> '<STR_LIT>' : uri } <EOL> escaped_password = saxutils . escape ( password ) <EOL> escaped_username = saxutils . escape ( username ) <EOL> self . _create_resource ( <EOL> resource_uri , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % { '<STR_LIT>' : self . _get_xml_bool ( enabled ) , <EOL> '<STR_LIT:username>' : escaped_username , <EOL> '<STR_LIT:password>' : escaped_password } ) <EOL> def get_listener ( self , protocol = LISTENER_PROTOCOL_HTTPS , address = "<STR_LIT:*>" ) : <EOL> resource_uri = self . _SERVICE_LISTENER_URI % { '<STR_LIT>' : protocol , <EOL> '<STR_LIT:address>' : address } <EOL> return self . _parse_listener_xml ( self . _get_resource ( resource_uri ) ) <EOL> def delete_listener ( self , protocol = LISTENER_PROTOCOL_HTTPS , address = "<STR_LIT:*>" ) : <EOL> resource_uri = self . _SERVICE_LISTENER_URI % { '<STR_LIT>' : protocol , <EOL> '<STR_LIT:address>' : address } <EOL> self . _delete_resource ( resource_uri ) <EOL> def create_listener ( self , protocol = LISTENER_PROTOCOL_HTTPS , <EOL> cert_thumbprint = None , address = "<STR_LIT:*>" , enabled = True ) : <EOL> resource_uri = self . _SERVICE_LISTENER_URI % { '<STR_LIT>' : protocol , <EOL> '<STR_LIT:address>' : address } <EOL> self . _create_resource ( <EOL> resource_uri , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % { "<STR_LIT>" : self . _get_xml_bool ( enabled ) , <EOL> "<STR_LIT>" : cert_thumbprint } ) <EOL> def set_auth_config ( self , basic = None , kerberos = None , negotiate = None , <EOL> certificate = None , credSSP = None , <EOL> cbt_hardening_level = None ) : <EOL> tag_map = { '<STR_LIT>' : basic , <EOL> '<STR_LIT>' : kerberos , <EOL> '<STR_LIT>' : negotiate , <EOL> '<STR_LIT>' : certificate , <EOL> '<STR_LIT>' : credSSP , <EOL> '<STR_LIT>' : cbt_hardening_level } <EOL> session = self . _get_wsman_session ( ) <EOL> data_xml = session . Get ( self . _SERVICE_AUTH_URI ) <EOL> ns = { '<STR_LIT>' : <EOL> '<STR_LIT>' } <EOL> tree = ElementTree . fromstring ( data_xml ) <EOL> for ( tag , value ) in tag_map . items ( ) : <EOL> if value is not None : <EOL> node = tree . find ( '<STR_LIT>' % tag , namespaces = ns ) <EOL> new_value = self . _get_xml_bool ( value ) <EOL> if node . text . lower ( ) != new_value : <EOL> node . text = new_value <EOL> data_xml = ElementTree . tostring ( tree ) <EOL> session . Put ( self . _SERVICE_AUTH_URI , data_xml ) </s>
