def export_imc_session(file_path, key, merge=YesOrNo.FALSE): """ Stores the credential of currently logged in IMC in current session to a file. Password will be stored in encrypted format using key. - file_path specifies the path of the credential file. - key specifies any string used for secure encryption. - merge should be set as False unless username wants to append the existing credential file with new credential. """ from ImcUtilityCore import _ImcLoginXml if file_path is None: raise ImcValidationException('[Error]: Please provide file_path') if key is None: raise ImcValidationException('[Error]: Please provide key') doc = xml.dom.minidom.Document() node_list = None if merge in ImcUtils.AFFIRMATIVE_LIST and os.path.isfile(file_path):#isfile() checks for file. exists() return true for directory as well doc = xml.dom.minidom.parse(file_path) node_list = doc.documentElement.childNodes else: doc.appendChild(doc.createElement(_ImcLoginXml.IMC_HANDLES)) h_imc_array = default_imc.values() if h_imc_array != None: for h_imc in h_imc_array: updated = False if node_list != None: for child_node in node_list: if child_node.nodeType != Node.ELEMENT_NODE: continue elem = child_node if not elem.hasAttribute(_ImcLoginXml.NAME) or not elem.hasAttribute(_ImcLoginXml.USER_NAME): continue if elem.getAttribute(_ImcLoginXml.NAME) != h_imc.name or elem.getAttribute(_ImcLoginXml.USER_NAME) != h_imc.username: continue if h_imc.nossl: elem.setAttribute(_ImcLoginXml.NO_SSL, h_imc.nossl) elif elem.hasAttribute(_ImcLoginXml.NO_SSL): elem.removeAttribute(_ImcLoginXml.NO_SSL) if (h_imc.nossl and h_imc.port != 80) or (not h_imc.nossl and h_imc.port != 443): elem.setAttribute(_ImcLoginXml.PORT, h_imc.port) elif elem.hasAttribute(_ImcLoginXml.PORT): elem.removeAttribute(_ImcLoginXml.PORT) #elem.setAttribute(_ImcLoginXml.PASSWORD, p3_encrypt(h_imc.__password,key)) elem.setAttribute(_ImcLoginXml.PASSWORD, ImcUtils.encrypt_password(h_imc.password, key)) updated = True break if updated: continue node = doc.createElement(_ImcLoginXml.IMC) attr = doc.createAttribute(_ImcLoginXml.NAME) attr.value = h_imc.name node.setAttributeNode(attr) attr = doc.createAttribute(_ImcLoginXml.USER_NAME) attr.value = h_imc.username node.setAttributeNode(attr) if h_imc.nossl: attr = doc.createAttribute(_ImcLoginXml.NO_SSL) attr.value = h_imc.nossl node.setAttributeNode(attr) if (h_imc.nossl and h_imc.port != 80) or (not h_imc.nossl and h_imc.port != 443): attr = doc.createAttribute(_ImcLoginXml.PORT) attr.value = h_imc.port node.setAttributeNode(attr) attr = doc.createAttribute(_ImcLoginXml.PASSWORD) #attr.value = p3_encrypt(h_imc.__password,key) attr.value = ImcUtils.encrypt_password(h_imc.password, key) node.setAttributeNode(attr) doc.documentElement.insertBefore(node, doc.documentElement.lastChild) xml_new = doc.toprettyxml(indent=" ") xml_new = re.sub(r"^.*?xml version.*\n", "", xml_new) xml_new = re.sub(r"\n[\s]*\n", "\n", xml_new) xml_new = re.sub(r"^(.*)", r"\1", xml_new, re.MULTILINE) f_handle = open(file_path, 'w') f_handle.write(xml_new) f_handle.close()