示例#1
0
    def get_session_key(username,
                        password,
                        splunkd_uri="https://localhost:8089"):
        """
        Get session key by using login username and passwrod
        :return: session_key if successful, None if failed
        """

        eid = "".join((splunkd_uri, "/services/auth/login"))
        postargs = {
            "username": username,
            "password": password,
        }

        response, content = rest.splunkd_request(eid,
                                                 None,
                                                 method="POST",
                                                 data=postargs)

        if response is None and content is None:
            raise CredException("Get session key failed.")

        xml_obj = xdm.parseString(content)
        session_nodes = xml_obj.getElementsByTagName("sessionKey")
        if not session_nodes:
            raise CredException("Invalid username or password.")
        session_key = session_nodes[0].firstChild.nodeValue
        if not session_key:
            raise CredException("Get session key failed.")
        return session_key
示例#2
0
    def downloadStoryLive(self, resp):
        """Download post-live stories of a followed user's tray.

        Download the post-live stories of a followed user.

        Args:
            resp: JSON dictionary of reel from IG API

        Returns:
            None
        """
        try:
            for index, item in enumerate(resp["post_live"]["post_live_items"]):
                logging.debug('    ' + str(index))
                username = item["user"]["username"]
                userpk = item["user"]["pk"]
                for bindex, broadcast in enumerate(item["broadcasts"]):
                    logging.debug('        ' + str(bindex))
                    timestamp = broadcast["published_time"]
                    postid = broadcast["media_id"]
                    dash = broadcast["dash_manifest"]
                    dashxml = xml.parseString(dash)
                    elements = dashxml.getElementsByTagName("BaseURL")
                    for eindex, element in enumerate(elements):
                        for node in element.childNodes:
                            if node.nodeType == node.TEXT_NODE:
                                url = node.data
                                mediatype = 3
                                path = self.formatPath(
                                    username, userpk, timestamp,
                                    postid + "_" + str(eindex), mediatype)
                                self.getFile(url, path)
        except KeyError:  # No "post_live" key
            logging.debug('    ' + 'No live stories.')
def output_tensor_names_map(graph: Graph, xml_file_name: str):
    mapping = Element('mapping')
    for node in graph:
        node = Node(graph, node)
        if node.has_valid('fw_tensor_debug_info') and node.has_valid(
                'ie_tensor_name'):
            for fw_tensor_debug_info in node.fw_tensor_debug_info:
                # Check that debug info has valid fw attrs
                if not all(attr is not None for attr in fw_tensor_debug_info):
                    continue
                map = SubElement(mapping, 'map')
                fw = SubElement(map, 'framework')
                ie = SubElement(map, 'IR')

                fw.set('name', fw_tensor_debug_info[0])
                fw.set('out_port_id', str(fw_tensor_debug_info[1]))

                if node.has_valid('ie_tensor_name'):
                    ie.set('name', node.ie_tensor_name)
                if node.has_valid('ie_tensor_port'):
                    ie.set('out_port_id', str(node.ie_tensor_port))
                if node.has_valid('ie_tensor_id'):
                    ie.set('id', str(node.ie_tensor_id))
    with open(xml_file_name, 'w') as file:
        file.write(parseString(tostring(mapping)).toprettyxml())
示例#4
0
async def get_price_rub_gold_cbr(
        timeout: Union[int, float] = 12,
        session: Optional[aiohttp.ClientSession] = None) -> float:
    """Get price of 1 mg Gold from Russian Central Bank; return value is RUB."""

    if not session:
        session = aiohttp.ClientSession(raise_for_status=True)

    # cbr metall codes: (1 - gold, 2 - silver, 3 - platinum, 4 - palladium)
    # cbr may return an empty value on Monday, so request 2 days history
    today = date.today()
    date1 = (today - timedelta(days=7)).strftime('%d/%m/%Y')
    date2 = today.strftime('%d/%m/%Y')
    # date_req1 — date_req2 = Date range
    payload = {'date_req1': date1, 'date_req2': date2}

    async with session.get('http://www.cbr.ru/scripts/xml_metall.asp',
                           params=payload,
                           timeout=timeout) as result:

        dom = parseString(await result.text())
        price = []

        for element in dom.getElementsByTagName('Record'):
            if element.getAttribute('Code') == '1':
                price.append(
                    element.getElementsByTagName('Buy')
                    [0].childNodes[0].data.split(',')[0])

        # return value is grams, so divide to 1000
        return float(price[0]) / 1000
示例#5
0
 def __init__(self, source):
     """
     @param source: A string containing an XML document.
     @type source: str
     """
     self._doc = parseString(source)
     self._root = DotNode(self._doc.documentElement)
示例#6
0
 def _run_subdoc_seq(self, input: bytes) -> bytes:
     doc = parseString(input)
     # depth-first search (TODO: check the alg)
     parents = [doc.documentElement]
     while parents:
         v = parents.pop()
         for w in v.childNodes:
             if URIRef(w.namespaceURI
                       ) in self.script.transformer.source_namespaces:
                 str = w.toxml('utf-8')
                 str2 = RunCommand(self.script, self.interpreters).run(
                     str, self.adjust_params(w))
                 frag = parseString(str2)
                 v.replaceChild(w, frag.documentElement)
             parents.append(w)
     return doc.toxml('utf-8')
示例#7
0
def inject_content_types(fd, drawing_name):
    """Inject content-types in the XML file."""

    x = xml.parse(fd).getroot()
    png = False

    for elem in list(x):
        if elem.attrib['ContentType'] == 'image/png':
            png = True

    fd.seek(0)
    content_types_xml = fd.read()
    s = parseString(content_types_xml)

    if not png:
        index = 0
        x = s.lastChild
        for child in x.childNodes:
            if child.nodeName == 'Default':
                index += 1

        refChild = x.childNodes[index]
        newChild = s.createElement('Default')
        newChild.setAttribute('Extension', 'png')
        newChild.setAttribute('ContentType', 'image/png')

        x.insertBefore(newChild, refChild)

    attr = {
        'ContentType': 'application/vnd.openxmlformats-'
        'officedocument.drawing+xml',
        'PartName': f"/xl/drawings/{drawing_name}.xml"
    }

    return create_child(s, 'Override', attr)
示例#8
0
    def __init__(self):
        description = get_param('robot_description')

        self.giskard_wrapper = GiskardWrapper()

        self.free_joints = {}
        self.joint_list = [
        ]  # for maintaining the original order of the joints
        self.dependent_joints = get_param("dependent_joints", {})
        self.use_mimic = get_param('~use_mimic_tags', True)
        self.use_small = get_param('~use_smallest_joint_limits', True)

        self.zeros = get_param("zeros")

        #self.pub_def_positions = get_param("publish_default_positions", True)
        #self.pub_def_vels = get_param("publish_default_velocities", False)
        #self.pub_def_efforts = get_param("publish_default_efforts", False)

        msg = rospy.wait_for_message(u'/whole_body_controller/state',
                                     JointTrajectoryControllerState)
        self.giskard_joints = msg.joint_names

        robot = minidom.parseString(description)
        if robot.getElementsByTagName('COLLADA'):
            self.init_collada(robot)
        else:
            self.init_urdf(robot)
示例#9
0
def inject_content_types(content_xml):
    """Add new content-types to the document."""

    s = parseString(content_xml)
    a = s.firstChild

    for tag in a.childNodes:
        if tag.hasAttribute('PartName'):
            if tag.getAttribute('PartName') == '/word/document.xml':
                tag.setAttribute(
                    'ContentType', 'application/vnd.ms-word'
                    '.document.macroEnabled.'
                    'main+xml')

    attr = {
        'Extension': 'bin',
        'ContentType': 'application/vnd.ms-office.vbaProject'
    }

    create_child(s, 'Default', attr)

    attr = {
        'PartName': '/word/vbaData.xml',
        'ContentType': 'application/vnd.ms-word.vbaData+xml'
    }

    return create_child(s, 'Override', attr)
示例#10
0
def getDisplayConnection(api, machineId):
    '''
    If machine is not running or there is not a display, will return NONE
    SPICE connections should check that 'type' is 'SPICE'
    '''
    md = minidom.parseString(api.VMInfo(machineId)[1])
    try:
        graphics = md.getElementsByTagName('GRAPHICS')[0]

        type_ = graphics.getElementsByTagName('TYPE')[0].childNodes[0].data
        port = graphics.getElementsByTagName('PORT')[0].childNodes[0].data
        try:
            passwd = graphics.getElementsByTagName('PASSWD')[0].childNodes[0].data
        except Exception:
            passwd = ''

        host = md.getElementsByTagName('HISTORY_RECORDS')[0].lastChild.getElementsByTagName('HOSTNAME')[0].childNodes[0].data
        return {
            'type': type_,
            'host': host,
            'port': int(port),
            'passwd': passwd
        }

    except Exception:
        return None  # No SPICE connection
示例#11
0
def getDisplayConnection(api, machineId):
    '''
    If machine is not running or there is not a display, will return NONE
    SPICE connections should check that 'type' is 'SPICE'
    '''
    md = minidom.parseString(api.VMInfo(machineId)[1])
    try:
        graphics = md.getElementsByTagName('GRAPHICS')[0]

        type_ = graphics.getElementsByTagName('TYPE')[0].childNodes[0].data
        port = graphics.getElementsByTagName('PORT')[0].childNodes[0].data
        try:
            passwd = graphics.getElementsByTagName(
                'PASSWD')[0].childNodes[0].data
        except Exception:
            passwd = ''

        host = md.getElementsByTagName('HISTORY_RECORDS')[
            0].lastChild.getElementsByTagName('HOSTNAME')[0].childNodes[0].data
        return {
            'type': type_,
            'host': host,
            'port': int(port),
            'passwd': passwd
        }

    except Exception:
        return None  # No SPICE connection
示例#12
0
def _write_platform_definition(root_xml: XMLElement) -> None:
    # Write the Simgrid / Batsim compliant platform to an output file
    with open('platform.xml', 'w') as out_f:
        out_f.write(mxml.parseString(
            (f'<!DOCTYPE platform SYSTEM "https://simgrid.org/simgrid.dtd">'
             f'{exml.tostring(root_xml).decode()}')).toprettyxml(indent='  ',
                                                                 encoding='utf-8').decode())
def process_tile_metadata(contents: str) -> Dict:
    """
    Tile xml metadata format, as described by
    xmlns https://psd-14.sentinel2.eo.esa.int/PSD/S2_PDI_Level-1C_Tile_Metadata.xsd
    """
    root = minidom.parseString(contents)

    resolution = min(
        int(i.attributes["resolution"].value)
        for i in root.getElementsByTagName("Size"))
    return {
        "datetime":
        _value(root, "SENSING_TIME"),
        "eo:cloud_cover":
        _value(root, "CLOUDY_PIXEL_PERCENTAGE", type_=float),
        "eo:gsd":
        resolution,
        "eo:sun_azimuth":
        _value(root, "Mean_Sun_Angle", "AZIMUTH_ANGLE", type_=float),
        "eo:sun_elevation":
        _value(root, "Mean_Sun_Angle", "ZENITH_ANGLE", type_=float),
        "odc:processing_datetime":
        _value(root, "ARCHIVING_TIME"),
        "sentinel:datastrip_id":
        _value(root, "DATASTRIP_ID"),
        "sentinel:sentinel_tile_id":
        _value(root, "TILE_ID"),
    }
示例#14
0
def convert(folder):
    root = Element('SokobanLevels')
    title = SubElement(root, 'Title')
    title.text = folder.split('/')[-1]
    desc = SubElement(root, 'Description')
    desc.text = folder.split('/')[-1]
    collection = SubElement(root, 'LevelCollection')
    i = 1

    def natural_keys(text):
        return [int(c) if c.isdigit() else c for c in re.split(r'(\d+)', text)]

    for file in sorted(os.listdir(folder), key=natural_keys):
        if not file.endswith('.txt'):
            continue

        level = SubElement(collection, 'Level', {'Id': str(i)})

        with open('/'.join([folder, file]), 'r') as f:
            for r in f.readlines():
                line = SubElement(level, 'L')
                line.text = r[:-1].rstrip()

        i += 1

    s = ElementTree.tostring(root, encoding='utf-8')
    s = b'<!DOCTYPE SokobanLevels SYSTEM "SokobanLev.dtd">' + s
    parsed = minidom.parseString(s)
    xml = parsed.toprettyxml(indent='  ', encoding='utf-8', newl='\n')

    with open(f'{folder}.slc', 'wb') as f:
        f.write(xml)
示例#15
0
def parse_modinput_configs(config_str):
    """
    @config_str: modinput XML configuration feed by splunkd
    @return: meta_config and stanza_config
    """

    import defusedxml.minidom as xdm

    meta_configs = {
        "server_host": None,
        "server_uri": None,
        "session_key": None,
        "checkpoint_dir": None,
    }
    root = xdm.parseString(config_str)
    doc = root.documentElement
    for tag in meta_configs.keys():
        nodes = doc.getElementsByTagName(tag)
        if not nodes:
            log.logger.error("Invalid config, missing %s section", tag)
            raise Exception("Invalid config, missing %s section", tag)

        if nodes[0].firstChild and nodes[0].firstChild.nodeType == nodes[0].TEXT_NODE:
            meta_configs[tag] = nodes[0].firstChild.data
        else:
            log.logger.error("Invalid config, expect text ndoe")
            raise Exception("Invalid config, expect text ndoe")

    if doc.nodeName == "input":
        configs = _parse_modinput_configs(doc, "configuration", "stanza")
    else:
        configs = _parse_modinput_configs(root, "items", "item")
    return meta_configs, configs
示例#16
0
def checkPublished(api, templateId):
    """
    checks if the template is fully published (images are ready...)
    """
    try:
        imgs = dict(((i[1], i[0]) for i in api.enumImages()))

        info = api.templateInfo(templateId)[1]
        template = minidom.parseString(info).getElementsByTagName('TEMPLATE')[0]
        logger.debug('XML: {}'.format(template.toxml()))

        for dsk in template.getElementsByTagName('DISK'):
            imgIds = dsk.getElementsByTagName('IMAGE_ID')
            if len(imgIds) == 0:
                try:
                    node = dsk.getElementsByTagName('IMAGE')[0].childNodes[0]
                except IndexError:
                    continue
                imgId = imgs[node.data]
            else:
                node = imgIds[0].childNodes[0]
                imgId = node.data

            logger.debug('Found {} for checking'.format(imgId))

            state = api.imageInfo(imgId)[0]['IMAGE']['STATE']
            if state in ('0', '4'):
                return False
            elif state != '1':  # If error is not READY
                raise Exception('Error publishing. Image is in an invalid state. (Check it and delete it if not needed anymore)')
    except Exception:
        logger.exception('Exception checking published')
        raise

    return True
示例#17
0
    def add_x509_key_descriptors(metadata, cert=None):
        """
        Adds the x509 descriptors (sign/encriptation) to the metadata
        The same cert will be used for sign/encrypt

        :param metadata: SAML Metadata XML
        :type metadata: string

        :param cert: x509 cert
        :type cert: string

        :returns: Metadata with KeyDescriptors
        :rtype: string
        """
        if cert is None or cert == '':
            return metadata
        try:
            xml = parseString(metadata)
        except Exception as e:
            raise Exception('Error parsing metadata. ' + e.message)

        formated_cert = OneLogin_Saml2_Utils.format_cert(cert, False)
        x509_certificate = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS,
                                               'ds:X509Certificate')
        content = xml.createTextNode(formated_cert)
        x509_certificate.appendChild(content)

        key_data = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS,
                                       'ds:X509Data')
        key_data.appendChild(x509_certificate)

        key_info = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS,
                                       'ds:KeyInfo')
        key_info.appendChild(key_data)

        key_descriptor = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS,
                                             'md:KeyDescriptor')

        entity_descriptor = xml.getElementsByTagName('md:EntityDescriptor')[0]

        sp_sso_descriptor = entity_descriptor.getElementsByTagName(
            'md:SPSSODescriptor')[0]
        sp_sso_descriptor.insertBefore(key_descriptor.cloneNode(True),
                                       sp_sso_descriptor.firstChild)
        sp_sso_descriptor.insertBefore(key_descriptor.cloneNode(True),
                                       sp_sso_descriptor.firstChild)

        signing = xml.getElementsByTagName('md:KeyDescriptor')[0]
        signing.setAttribute('use', 'signing')

        encryption = xml.getElementsByTagName('md:KeyDescriptor')[1]
        encryption.setAttribute('use', 'encryption')

        signing.appendChild(key_info)
        encryption.appendChild(key_info.cloneNode(True))

        signing.setAttribute('xmlns:ds', OneLogin_Saml2_Constants.NS_DS)
        encryption.setAttribute('xmlns:ds', OneLogin_Saml2_Constants.NS_DS)

        return xml.toxml()
示例#18
0
    def _step(self):
        all_namespaces = set()

        self.state.dom = parseString(self.state.xml_text)

        # depth-first search
        parents = [self.state.dom.documentElement]
        while parents:
            v = parents.pop()
            if v.namespaceURI is not None:
                all_namespaces.add(URIRef(v.namespaceURI))
            if v.attributes:
                for a in v.attributes.values():
                    if a.namespaceURI is not None:
                        all_namespaces.add(URIRef(a.namespaceURI))
            for w in v.childNodes:
                parents.append(w)

        # hack
        self.state.all_namespaces = frozenset(
            filter(lambda x: x != URIRef('http://www.w3.org/2000/xmlns/'),
                   all_namespaces))  # TODO: Is it worth to freeze?

        if self.state.all_namespaces <= self.state.opts.target_namespaces:
            return False  # The transformation finished!

        try:
            RealNextScript(self.state, self.interpreters).step()
        except StopIteration:
            try:
                next(self.state.next_asset)
            except StopIteration:
                raise AssetsExhausted()

        return True
示例#19
0
    def render(self, encoding=None, fragment=False, pretty=False, nsmap=None):
        """Produce XML from this model's instance data.

        A unicode string will be returned if any of the objects contain
        unicode values; specifying the 'encoding' argument forces generation
        of a bytestring.

        By default a complete XML document is produced, including the
        leading "<?xml>" declaration.  To generate an XML fragment set
        the 'fragment' argument to True.
        """
        if nsmap is None:
            nsmap = {}
        data = []
        if not fragment:
            if encoding:
                s = '<?xml version="1.0" encoding="%s" ?>' % (encoding,)
                data.append(s)
            else:
                data.append('<?xml version="1.0" ?>')
        data.extend(self._render(nsmap))
        xml = "".join(data)
        if pretty:
            xml = minidom.parseString(xml).toprettyxml()
        if encoding:
            xml = xml.encode(encoding)
        return xml
示例#20
0
def getNetInfo(api: 'client.OpenNebulaClient', machineId: str, networkId: typing.Optional[str] = None) -> typing.Tuple[str, str]:
    '''
    Get the MAC and the IP for the network and machine. If network is None, for the first network
    '''
    # md = minidom.parseString(api.call('vm.info', int(machineId)))
    md = minidom.parseString(api.VMInfo(machineId).xml)
    node = md

    try:
        for nic in md.getElementsByTagName('NIC'):
            netId = nic.getElementsByTagName('NETWORK_ID')[0].childNodes[0].data
            if networkId is None or int(netId) == int(networkId):
                node = nic
                break
    except Exception:
        raise Exception('No network interface found on template. Please, add a network and republish.')

    logger.debug(node.toxml())

    # Default, returns first MAC found (or raise an exception if there is no MAC)
    try:
        try:
            ip = node.getElementsByTagName('IP')[0].childNodes[0].data
        except Exception:
            ip = ''

        return (node.getElementsByTagName('MAC')[0].childNodes[0].data, ip)
    except Exception:
        raise Exception('No network interface found on template. Please, add a network and republish.')
示例#21
0
def append_ir_info(file: str,
                   meta_info: dict = dict(),
                   mean_data: [list, None] = None,
                   input_names: list = None):
    path_to_xml = file + ".xml"
    path_to_bin = file + ".bin"

    et = ET.parse(path_to_xml)
    net = et.getroot()

    if mean_data:
        mean_offset, mean_size = serialize_mean_image(path_to_bin,
                                                      mean_data=mean_data)
        create_pre_process_block_for_image(net, input_names, mean_offset,
                                           mean_size)

    add_meta_data(net, meta_info)

    for elem in et.iter():
        if elem.text:
            elem.text = elem.text.strip()
        if elem.tail:
            elem.tail = elem.tail.strip()

    pretty_xml_as_string = parseString(tostring(net)).toprettyxml()
    with open(path_to_xml, 'wb') as file:
        file.write(bytes(pretty_xml_as_string, "UTF-8"))
示例#22
0
 def _run_simple_seq(self, input: bytes) -> bytes:
     while True:
         doc = parseString(input)
         # depth-first search (TODO: check the alg)
         parents = [doc.documentElement]
         found = False
         while parents and not found:
             v = parents.pop()
             for w in v.childNodes:
                 if URIRef(w.namespaceURI
                           ) in self.script.transformer.source_namespaces:
                     found = True
                     break
                 else:
                     # TODO: https://bugs.python.org/issue34306
                     if any(
                             URIRef(a.namespaceURI) in
                             self.script.transformer.source_namespaces
                             for a in w.attributes.values()):
                         found = True
                         break
                 parents.append(w)
         if not found:
             return input  # TODO: Check XML validity? (point this in the specification)
         input = RunCommand(self.script,
                            self.interpreters).run(input, self.params)
示例#23
0
    def parse_file_to_get_array_map(self, file_name):
        """Parses a file and gets array map.

        Given a file, parse it to get array and pool(srp).

        .. code:: ini

          <EMC>
          <RestServerIp>10.108.246.202</RestServerIp>
          <RestServerPort>8443</RestServerPort>
          <RestUserName>smc</RestUserName>
          <RestPassword>smc</RestPassword>
          <SSLCert>/path/client.cert</SSLCert>
          <SSLVerify>/path/to/certfile.pem</SSLVerify>
          <PortGroups>
              <PortGroup>OS-PORTGROUP1-PG</PortGroup>
          </PortGroups>
          <Array>000198700439</Array>
          <SRP>SRP_1</SRP>
          </EMC>

        :param file_name: the configuration file
        :returns: list
        """
        LOG.warning("Use of xml file in backend configuration is deprecated "
                    "in Queens and will not be supported in future releases.")
        kwargs = {}
        my_file = open(file_name, 'r')
        data = my_file.read()
        my_file.close()
        dom = minidom.parseString(data)
        try:
            connargs = self._get_connection_info(dom)
            portgroup = self._get_random_portgroup(dom)
            serialnumber = self._process_tag(dom, 'Array')
            if serialnumber is None:
                LOG.error("Array Serial Number must be in the file %(file)s.",
                          {'file': file_name})
            srp_name = self._process_tag(dom, 'SRP')
            if srp_name is None:
                LOG.error("SRP Name must be in the file %(file)s.",
                          {'file': file_name})
            slo = self._process_tag(dom, 'ServiceLevel')
            workload = self._process_tag(dom, 'Workload')
            kwargs = (
                {'RestServerIp': connargs['RestServerIp'],
                 'RestServerPort': connargs['RestServerPort'],
                 'RestUserName': connargs['RestUserName'],
                 'RestPassword': connargs['RestPassword'],
                 'SSLCert': connargs['SSLCert'],
                 'SSLVerify': connargs['SSLVerify'],
                 'SerialNumber': serialnumber,
                 'srpName': srp_name,
                 'PortGroup': portgroup})
            if slo is not None:
                kwargs.update({'ServiceLevel': slo, 'Workload': workload})

        except IndexError:
            pass
        return kwargs
示例#24
0
def getNetInfo(api, machineId, networkId=None):
    '''
    Changes the mac address of first nic of the machine to the one specified
    '''
    # md = minidom.parseString(api.call('vm.info', int(machineId)))
    md = minidom.parseString(api.VMInfo(machineId)[1])
    node = md

    try:
        for nic in md.getElementsByTagName('NIC'):
            netId = nic.getElementsByTagName(
                'NETWORK_ID')[0].childNodes[0].data
            if networkId is None or int(netId) == int(networkId):
                node = nic
                break
    except Exception:
        raise Exception(
            'No network interface found on template. Please, add a network and republish.'
        )

    logger.debug(node.toxml())

    # Default, returns first MAC found (or raise an exception if there is no MAC)
    try:
        try:
            ip = node.getElementsByTagName('IP')[0].childNodes[0].data
        except Exception:
            ip = ''

        return (node.getElementsByTagName('MAC')[0].childNodes[0].data, ip)
    except Exception:
        raise Exception(
            'No network interface found on template. Please, add a network and republish.'
        )
示例#25
0
def getNetInfo(api, machineId, networkId=None):
    '''
    Changes the mac address of first nic of the machine to the one specified
    '''
    # md = minidom.parseString(api.call('vm.info', int(machineId)))
    md = minidom.parseString(api.VMInfo(machineId)[1])
    node = md

    try:
        for nic in md.getElementsByTagName('NIC'):
            netId = nic.getElementsByTagName('NETWORK_ID')[0].childNodes[0].data
            if networkId is None or int(netId) == int(networkId):
                node = nic
                break
    except Exception:
        raise Exception('No network interface found on template. Please, add a network and republish.')

    logger.debug(node.toxml())

    # Default, returns first MAC found (or raise an exception if there is no MAC)
    try:
        try:
            ip = node.getElementsByTagName('IP')[0].childNodes[0].data
        except Exception:
            ip = ''

        return (node.getElementsByTagName('MAC')[0].childNodes[0].data, ip)
    except Exception:
        raise Exception('No network interface found on template. Please, add a network and republish.')
示例#26
0
def search(search_engine):
    try:
        count = int(request.args.get('num', 10))
        qformat = request.args.get('format', 'json').lower()
        qtype = request.args.get('type', '')
        if qformat not in ('json', 'xml', 'csv'):
            abort(400, 'Not Found - undefined format')

        engine = search_engine
        if engine not in scrapers:
            error = [404, 'Incorrect search engine', engine]
            return bad_request(error)

        query = request.args.get('query')
        if not query:
            error = [400, 'Not Found - missing query', qformat]
            return bad_request(error)

        # first see if we can get the results for the cache
        engine_and_query = engine + ':' + query
        result = lookup(engine_and_query)
        if result:
            print("cache hit: {}".format(engine_and_query))
        else:
            result = feed_gen(query, engine, count, qtype)
            if result:
                # store the result in the cache to speed up future searches
                store(engine_and_query, result)
            else:
                error = [404, 'No response', engine_and_query]
                return bad_request(error)

        try:
            unicode  # unicode is undefined in Python 3 so NameError is raised
            for line in result:
                line['link'] = line['link'].encode('utf-8')
                if 'title' in line:
                    line['title'] = line['title'].encode('utf-8')
                if 'desc' in line:
                    line['desc'] = line['desc'].encode('utf-8')
        except NameError:
            pass  # Python 3 strings are already Unicode
        if qformat == 'json':
            return jsonify(result)
        elif qformat == 'csv':
            csvfeed = '"'
            csvfeed += '","'.join(result[0].keys())
            for line in result:
                csvfeed += '"\n"'
                csvfeed += '","'.join(line.values())
            csvfeed += '"'
            return Response(csvfeed)

        xmlfeed = dicttoxml(result, custom_root='channel', attr_type=False)
        xmlfeed = parseString(xmlfeed).toprettyxml()
        return Response(xmlfeed, mimetype='application/xml')
    except Exception as e:
        print(e)
        return jsonify(errorObj)
示例#27
0
文件: igd.py 项目: kefkahacks/pupy
 def custom(self, args):
     args.input_args
     iargs = json.loads(args.input_args)
     resp_xml = self.igdc.customAction(args.method_name, iargs, args.svc)
     if self.igdc.pprint:
         xml = minidom.parseString(resp_xml)
         xml.toprettyxml()
     else:
         resp_xml
示例#28
0
def prettify(elem: Element,
             indent: str = "\t",
             doctype: str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"):
    """Return a pretty-printed XML string for the Element.  """
    rough_string = ElementTree.tostring(elem, 'utf-8')
    reparsed = parseString(rough_string)
    reparsed = reparsed.toprettyxml(indent=indent).split("\n")[1:]
    reparsed.insert(0, doctype)
    return "\n".join(reparsed)
示例#29
0
 def create_xml(self):
     self._build_xml()
     tree = ET.ElementTree(self.root)
     path_xml = os.path.join(os.getcwd(), 'example.xml')
     tree.write(path_xml)
     xml = minidom.parseString(ET.tostring(tree.getroot())).toprettyxml()
     f = open(path_xml, 'w', encoding='utf-8')
     f.write(xml)
     f.close()
示例#30
0
    def add_x509_key_descriptors(metadata, cert=None, add_encryption=True):
        """
        Adds the x509 descriptors (sign/encryption) to the metadata
        The same cert will be used for sign/encrypt

        :param metadata: SAML Metadata XML
        :type metadata: string

        :param cert: x509 cert
        :type cert: string

        :param add_encryption: Determines if the KeyDescriptor[use="encryption"] should be added.
        :type add_encryption: boolean

        :returns: Metadata with KeyDescriptors
        :rtype: string
        """
        if cert is None or cert == '':
            return metadata
        try:
            xml = parseString(metadata.encode('utf-8'), forbid_dtd=True)
        except Exception as e:
            raise Exception('Error parsing metadata. ' + e.message)

        formated_cert = OneLogin_Saml2_Utils.format_cert(cert, False)
        x509_certificate = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS, 'ds:X509Certificate')
        content = xml.createTextNode(formated_cert)
        x509_certificate.appendChild(content)

        key_data = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS, 'ds:X509Data')
        key_data.appendChild(x509_certificate)

        key_info = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS, 'ds:KeyInfo')
        key_info.appendChild(key_data)

        key_descriptor = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS, 'md:KeyDescriptor')

        entity_descriptor = xml.getElementsByTagName('md:EntityDescriptor')[0]

        sp_sso_descriptor = entity_descriptor.getElementsByTagName('md:SPSSODescriptor')[0]
        sp_sso_descriptor.insertBefore(key_descriptor.cloneNode(True), sp_sso_descriptor.firstChild)
        if add_encryption:
            sp_sso_descriptor.insertBefore(key_descriptor.cloneNode(True), sp_sso_descriptor.firstChild)

        signing = xml.getElementsByTagName('md:KeyDescriptor')[0]
        signing.setAttribute('use', 'signing')
        signing.appendChild(key_info)
        signing.setAttribute('xmlns:ds', OneLogin_Saml2_Constants.NS_DS)

        if add_encryption:
            encryption = xml.getElementsByTagName('md:KeyDescriptor')[1]
            encryption.setAttribute('use', 'encryption')
            encryption.appendChild(key_info.cloneNode(True))
            encryption.setAttribute('xmlns:ds', OneLogin_Saml2_Constants.NS_DS)

        return xml.toxml()
示例#31
0
def search(search_engine):
    try:
        count = int(request.args.get('num', 10))
        qformat = request.args.get('format', 'json').lower()
        if qformat not in ('json', 'xml', 'csv'):
            abort(400, 'Not Found - undefined format')

        engine = search_engine
        if engine not in scrapers:
            err = [404, 'Incorrect search engine', engine]
            return bad_request(err)

        query = request.args.get('query')
        if not query:
            err = [400, 'Not Found - missing query', qformat]
            return bad_request(err)

        result = feedgen(query, engine, count)
        if not result:
            err = [404, 'No response', qformat]
            return bad_request(err)

        if db['queries'].find({query: query}).limit(1) is False:
            db['queries'].insert({
                "query": query,
                "engine": engine,
                "qformat": qformat
            })

        try:
            unicode  # unicode is undefined in Python 3 so NameError is raised
            for line in result:
                line['link'] = line['link'].encode('utf-8')
                line['title'] = line['title'].encode('utf-8')
                if 'desc' in line:
                    line['desc'] = line['desc'].encode('utf-8')
        except NameError:
            pass  # Python 3 strings are already Unicode
        if qformat == 'json':
            return jsonify(result)

        elif qformat == 'csv':
            csvfeed = '"'
            csvfeed += '","'.join(result[0].keys())
            for line in result:
                csvfeed += '"\n"'
                csvfeed += '","'.join(line.values())
            csvfeed += '"'
            return Response(csvfeed)

        xmlfeed = dicttoxml(result, custom_root='channel', attr_type=False)
        xmlfeed = parseString(xmlfeed).toprettyxml()
        return Response(xmlfeed, mimetype='application/xml')
    except Exception as e:
        print(e)
        return jsonify(errorObj)
示例#32
0
def _get_node_from_xml(xml_data, tag_name):
    """Extract tag data from xml data."""
    log.debug('xml={}'.format(xml_data))

    try:
        dom1 = parseString(xml_data)
        childnode = dom1.getElementsByTagName(tag_name)[0]
    except:
        _log_error_and_exit("Received bad XML, can not find tag {}".format(tag_name), xml_data)
    return childnode.firstChild.data
示例#33
0
def submitview(request):
    if request.method == 'POST':
        form = SubmitAppForm(request.POST)
        if form.is_valid():
            h = httplib2.Http(".cache")
            #try:
            resp, content = h.request(form.cleaned_data['xml'], "GET")
            xml = parseString(content)
            #except:
            # FIXME: Give the user notice and reason
            # return HttpResponseRedirect("/")

            if(xml.documentElement.localName == 'interface' and
               xml.documentElement.namespaceURI ==
               "http://zero-install.sourceforge.net/2004/injector/interface" ):
                # Woohoo Zero Install feed, now extract data
                # FIXME: Extract data only in English, for now
                short = lng = xml.documentElement.\
                      getElementsByTagName("category")[0].firstChild.nodeValue
                for category in App.CATEGORY_OPTIONS:
                    s, l = category
                    if l == lng:
                        short = s

                appform = AppForm({
                    "name": xml.documentElement.\
                            getElementsByTagName("name")[0].firstChild.nodeValue,
                    "description": xml.documentElement.\
                                   getElementsByTagName("description")[0].firstChild.nodeValue,
                    "xml": form.cleaned_data['xml'],
                    "homepage": xml.documentElement.\
                                getElementsByTagName("homepage")[0].firstChild.nodeValue,
                    "uploader": request.user.id,
                    "category": short,
                })
                if appform.is_valid():
                    appform.save()
                    return render(request, 'apps/submit.html', {
                        "message": "Success!",
                        "form": SubmitAppForm(),
                    })
                else:
                    return render(request, 'apps/invalid-coded-form')
            else:
                # Not a Zero Install feed
                return render(request, 'apps/nope')
        else:
            return render(request, 'apps/submit.html', {
                "form": form,
            })
    else:
        form = SubmitAppForm()
        return render(request, 'apps/submit.html', {
            "form": form,
        })
示例#34
0
def generate_ie_ir(graph: Graph,
                   file_name: str,
                   input_names: tuple = (),
                   mean_offset: tuple = (),
                   mean_size: tuple = (),
                   meta_info: dict = dict()):
    """
    Extracts IE/IR attributes from kind='op' nodes in three ways:
      (1) node.IE xml scheme that set correspondance from existing attributes to generated xml elements
      (2) input/output edges that don't have 'bin' attributes are transformed to input/output ports
      (3) input edges that has 'bin' attributes are handled in special way like weights/biases

    Args:
        graph: nx graph with FW-independent model
        file_name: name of the resulting IR
        input_names: names of input layers of the topology to add mean file to
        input_name: name of the layer which is referenced from pre-processing block if any
        mean_values: tuple of mean values for channels in RGB order
        scale_values:  tuple of mean values for channels in RGB order
        mean_offset: offset in binary file, where mean file values start
        mean_size: size of the mean file
    """
    net = Element('net')
    net.set('name', graph.name)
    net.set('version', str((graph.graph['ir_version'])))
    net.set(
        'batch', '1'
    )  # TODO substitute real batches here (is it a number or is it an index?)

    if mean_size or mean_offset:
        create_pre_process_block_for_image(net, input_names, mean_offset,
                                           mean_size)

    if 'mean_values' in graph.graph.keys():
        for input_name, values in graph.graph['mean_values'].items():
            create_pre_process_block(net, input_name, values)

    unsupported = UnsupportedOps(graph)

    serialize_network(graph, net, unsupported)
    add_quantization_statistics(graph, net)
    add_meta_data(net, meta_info)
    xml_string = tostring(net)
    xml_doc = parseString(xml_string)
    pretty_xml_as_string = xml_doc.toprettyxml()
    if len(unsupported.unsupported):
        log.debug('Partially correct IR XML:\n{}'.format(pretty_xml_as_string))
        unsupported.report(
            log.error,
            "List of operations that cannot be converted to Inference Engine IR:"
        )
        raise Error('Part of the nodes was not converted to IR. Stopped. ' +
                    refer_to_faq_msg(24))
    with open(file_name, 'wb') as file:
        file.write(bytes(pretty_xml_as_string, "UTF-8"))
def convert_json_to_xml(args):
    if re.match("https?://", args.path):
        data = requests.get(args.path).content
    else:
        with open(args.path, "rb") as f:
            data = f.read()

    print(
        minidom.parseString(
            ElementTree.tostring(create_xml_tree(
                json.loads(data)))).toprettyxml(indent=" " * 4))
示例#36
0
 def get_id(request):
     """
     Returns the ID of the Logout Request
     :param request: Logout Request Message
     :type request: string|DOMDocument
     :return: string ID
     :rtype: str object
     """
     if isinstance(request, Document):
         dom = request
     else:
         dom = parseString(request)
     return dom.documentElement.getAttribute('ID')
示例#37
0
文件: __init__.py 项目: allfro/dexml
 def _make_xml_node(xml):
     """Transform a variety of input formats to an XML DOM node."""
     try:
         ntype = xml.nodeType
     except AttributeError:
         if isinstance(xml, bytes):
             try:
                 xml = minidom.parseString(xml)
             except Exception, e:
                 raise XmlError(e)
         elif isinstance(xml, unicode):
             try:
                 #  Try to grab the "encoding" attribute from the XML.
                 #  It probably won't exist, so default to utf8.
                 encoding = _XML_ENCODING_RE.match(xml)
                 if encoding is None:
                     encoding = "utf8"
                 else:
                     encoding = encoding.group(1)
                 xml = minidom.parseString(xml.encode(encoding))
             except Exception, e:
                 raise XmlError(e)
示例#38
0
    def __init__(self, settings, response=None):
        """
        Constructs a Logout Response object (Initialize params from settings
        and if provided load the Logout Response.

        Arguments are:
            * (OneLogin_Saml2_Settings)   settings. Setting data
            * (string)                    response. An UUEncoded SAML Logout
                                                    response from the IdP.
        """
        self.__settings = settings
        if response is not None:
            self.__logout_response = OneLogin_Saml2_Utils.decode_base64_and_inflate(response)
            self.document = parseString(self.__logout_response)
示例#39
0
    def _vrts_parse_xml_file(self, filename):
        """VRTS target info.

        <VRTS>
        <VrtsTargets>
            <Target>
                <Name>iqn.2017-02.com.veritas:target03</Name>
                <PortalIP>10.182.174.188</PortalIP>
            </Target>
            <Target>
                <Name>iqn.2017-02.com.veritas:target04</Name>
                <PortalIP>10.182.174.189</PortalIP>
            </Target>
        </VrtsTargets>
        </VRST>

        :param filename: the configuration file
        :returns: list
        """
        myfile = open(filename, 'r')
        data = myfile.read()
        myfile.close()
        dom = minidom.parseString(data)

        mylist = []
        target = {}

        try:
            for trg in dom.getElementsByTagName('Target'):
                target['name'] = (trg.getElementsByTagName('Name')[0]
                                  .childNodes[0].nodeValue)
                target['portal_ip'] = (trg.getElementsByTagName('PortalIP')[0]
                                       .childNodes[0].nodeValue)
                target['auth'] = (trg.getElementsByTagName('Authentication')[0]
                                  .childNodes[0].nodeValue)
                if target['auth'] == '1':
                    target['auth_user'] = (trg.getElementsByTagName
                                           ('Auth_username')[0]
                                           .childNodes[0].nodeValue)
                    target['auth_password'] = (trg.getElementsByTagName
                                               ('Auth_password')[0]
                                               .childNodes[0].nodeValue)

                mylist.append(target)
                target = {}
        except IndexError:
            pass

        return mylist
示例#40
0
    def write_result():
        """
        Writes the Batsim formatted platform.
        """

        def doctype():
            """
            Provides SimGrid doctype.
            """

            return "<!DOCTYPE platform SYSTEM \"https://simgrid.org/simgrid.dtd\">"

        with open(args.output_xml, "w", ) as output_f:
            output_f.write(xml_format.parseString("{}{}".format(doctype(),
                xml.tostring(xml_tree).decode())).toprettyxml(indent="  ",
                    encoding="utf-8").decode())
示例#41
0
    def validate_xml(xml, schema, debug=False):
        """
        Validates a xml against a schema
        :param xml: The xml that will be validated
        :type: string|DomDocument
        :param schema: The schema
        :type: string
        :param debug: If debug is active, the parse-errors will be showed
        :type: bool
        :returns: Error code or the DomDocument of the xml
        :rtype: string
        """
        assert isinstance(xml, basestring) or isinstance(xml, Document) or isinstance(xml, etree._Element)
        assert isinstance(schema, basestring)

        if isinstance(xml, Document):
            xml = xml.toxml()
        elif isinstance(xml, etree._Element):
            xml = tostring(xml)

        # Switch to lxml for schema validation
        try:
            dom = fromstring(str(xml))
        except Exception:
            return 'unloaded_xml'

        schema_file = join(dirname(__file__), 'schemas', schema)
        f_schema = open(schema_file, 'r')
        schema_doc = etree.parse(f_schema)
        f_schema.close()
        xmlschema = etree.XMLSchema(schema_doc)

        if not xmlschema.validate(dom):
            if debug:
                stderr.write('Errors validating the metadata')
                stderr.write(':\n\n')
                for error in xmlschema.error_log:
                    stderr.write('%s\n' % error.message)

            return 'invalid_xml'

        return parseString(etree.tostring(dom))
示例#42
0
def remove(api, templateId):
    """
    Removes a template from ovirt server

    Returns nothing, and raises an Exception if it fails
    """
    try:
        # First, remove Images (wont be possible if there is any images already in use, but will try)
        # Now copy cloned images if possible
        try:
            imgs = dict(((i[1], i[0]) for i in api.enumImages()))

            info = api.templateInfo(templateId)[1]
            template = minidom.parseString(info).getElementsByTagName('TEMPLATE')[0]
            logger.debug('XML: {}'.format(template.toxml()))

            for dsk in template.getElementsByTagName('DISK'):
                imgIds = dsk.getElementsByTagName('IMAGE_ID')
                if len(imgIds) == 0:
                    try:
                        node = dsk.getElementsByTagName('IMAGE')[0].childNodes[0]
                    except IndexError:
                        continue
                    imgId = imgs[node.data]
                else:
                    node = imgIds[0].childNodes[0]
                    imgId = node.data

                logger.debug('Found {} for cloning'.format(imgId))

                # Now delete the image
                api.deleteImage(imgId)  # api.call('image.delete', int(imgId))

        except:
            logger.exception('Exception cloning image')

        api.deleteTemplate(templateId)  # api.call('template.delete', int(templateId))
    except Exception as e:
        logger.error('Removing template on OpenNebula: {}'.format(e))
示例#43
0
    def _layer_attributes(self, url, layer):
        errors = set()

        request = _Request()
        request.registry.settings = self.config
        # static schema will not be supported
        url = get_url2("Layer", url, request, errors)
        if len(errors) > 0:
            print("\n".join(errors))
            return []

        wms_getcap_url = add_url_params(url, {
            "SERVICE": "WMS",
            "VERSION": "1.1.1",
            "REQUEST": "GetCapabilities",
        })

        hostname = urlsplit(url).hostname
        if url not in self.wmscap_cache:
            print("Get WMS GetCapabilities for URL: {}".format(url))
            self.wmscap_cache[url] = None

            # forward request to target (without Host Header)
            http = httplib2.Http()
            h = {}
            if hostname == "localhost":  # pragma: no cover
                h["Host"] = self.package["host"]
            try:
                resp, content = http.request(wms_getcap_url, method="GET", headers=h)

                try:
                    self.wmscap_cache[url] = WebMapService(None, xml=content)
                except Exception as e:
                    print(colorize(
                        "ERROR! an error occurred while trying to "
                        "parse the GetCapabilities document.",
                        RED))
                    print(colorize(str(e), RED))
                    print("URL: {}\nxml:\n{}".format(wms_getcap_url, content))
                    if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") != "TRUE":
                        raise
            except Exception as e:  # pragma: no cover
                print(colorize(str(e), RED))
                print(colorize(
                    "ERROR! Unable to GetCapabilities from URL: {}".format(wms_getcap_url),
                    RED,
                ))
                if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") != "TRUE":
                    raise

        wmscap = self.wmscap_cache[url]

        wfs_descrfeat_url = add_url_params(url, {
            "SERVICE": "WFS",
            "VERSION": "1.1.0",
            "REQUEST": "DescribeFeatureType",
        })

        if url not in self.featuretype_cache:
            print("Get WFS DescribeFeatureType for URL: {}".format(wfs_descrfeat_url))
            self.featuretype_cache[url] = None

            # forward request to target (without Host Header)
            http = httplib2.Http()
            h = {}
            if hostname == "localhost":  # pragma: no cover
                h["Host"] = self.package["host"]
            try:
                resp, content = http.request(wfs_descrfeat_url, method="GET", headers=h)
            except Exception as e:  # pragma: no cover
                print(colorize(str(e), RED))
                print(colorize(
                    "ERROR! Unable to DescribeFeatureType from URL: {}".format(wfs_descrfeat_url),
                    RED,
                ))
                if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") == "TRUE":
                    return []
                else:
                    raise

            if resp.status < 200 or resp.status >= 300:  # pragma: no cover
                print(colorize(
                    "ERROR! DescribeFeatureType from URL {} return the error: {1:d} {}".format(
                        wfs_descrfeat_url, resp.status, resp.reason
                    ),
                    RED,
                ))
                if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") == "TRUE":
                    return []
                else:
                    raise Exception("Aborted")

            try:
                describe = parseString(content)
                featurestype = {}
                self.featuretype_cache[url] = featurestype
                for type_element in describe.getElementsByTagNameNS(
                    "http://www.w3.org/2001/XMLSchema", "complexType"
                ):
                    featurestype[type_element.getAttribute("name")] = type_element
            except ExpatError as e:
                print(colorize(
                    "ERROR! an error occurred while trying to "
                    "parse the DescribeFeatureType document.",
                    RED
                ))
                print(colorize(str(e), RED))
                print("URL: {}\nxml:\n{}".format(wfs_descrfeat_url, content))
                if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") == "TRUE":
                    return []
                else:
                    raise
            except AttributeError:
                print(colorize(
                    "ERROR! an error occurred while trying to "
                    "read the Mapfile and recover the themes.",
                    RED
                ))
                print("URL: {}\nxml:\n{}".format(wfs_descrfeat_url, content))
                if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") == "TRUE":
                    return []
                else:
                    raise
        else:
            featurestype = self.featuretype_cache[url]

        if featurestype is None:
            return []

        layers = [layer]
        if wmscap is not None and layer in list(wmscap.contents):
            layer_obj = wmscap[layer]
            if len(layer_obj.layers) > 0:
                layers = [l.name for l in layer_obj.layers]

        attributes = []
        for sub_layer in layers:
            # Should probably be adapted for other king of servers
            type_element = featurestype.get("{}Type".format(sub_layer))
            if type_element is not None:
                for element in type_element.getElementsByTagNameNS(
                    "http://www.w3.org/2001/XMLSchema", "element"
                ):
                    if not element.getAttribute("type").startswith("gml:"):
                        attributes.append(element.getAttribute("name"))

        return attributes, layers
示例#44
0
    def add_sign(xml, key, cert, debug=False, sign_algorithm=OneLogin_Saml2_Constants.RSA_SHA1):
        """
        Adds signature key and senders certificate to an element (Message or
        Assertion).

        :param xml: The element we should sign
        :type: string | Document

        :param key: The private key
        :type: string

        :param cert: The public
        :type: string

        :param debug: Activate the xmlsec debug
        :type: bool

        :param sign_algorithm: Signature algorithm method
        :type sign_algorithm: string
        """
        if xml is None or xml == "":
            raise Exception("Empty string supplied as input")
        elif isinstance(xml, etree._Element):
            elem = xml
        elif isinstance(xml, Document):
            xml = xml.toxml()
            elem = fromstring(xml.encode("utf-8"))
        elif isinstance(xml, Element):
            xml.setAttributeNS(
                unicode(OneLogin_Saml2_Constants.NS_SAMLP), "xmlns:samlp", unicode(OneLogin_Saml2_Constants.NS_SAMLP)
            )
            xml.setAttributeNS(
                unicode(OneLogin_Saml2_Constants.NS_SAML), "xmlns:saml", unicode(OneLogin_Saml2_Constants.NS_SAML)
            )
            xml = xml.toxml()
            elem = fromstring(xml.encode("utf-8"))
        elif isinstance(xml, basestring):
            elem = fromstring(xml.encode("utf-8"))
        else:
            raise Exception("Error parsing xml string")

        if debug:
            xmlsec.set_error_callback(print_xmlsec_errors)

        # Sign the metadata with our private key.
        sign_algorithm_transform_map = {
            OneLogin_Saml2_Constants.DSA_SHA1: xmlsec.TransformDsaSha1,
            OneLogin_Saml2_Constants.RSA_SHA1: xmlsec.TransformRsaSha1,
            OneLogin_Saml2_Constants.RSA_SHA256: xmlsec.TransformRsaSha256,
            OneLogin_Saml2_Constants.RSA_SHA384: xmlsec.TransformRsaSha384,
            OneLogin_Saml2_Constants.RSA_SHA512: xmlsec.TransformRsaSha512,
        }
        sign_algorithm_transform = sign_algorithm_transform_map.get(sign_algorithm, xmlsec.TransformRsaSha1)

        signature = Signature(xmlsec.TransformExclC14N, sign_algorithm_transform)

        issuer = OneLogin_Saml2_Utils.query(elem, "//saml:Issuer")
        if len(issuer) > 0:
            issuer = issuer[0]
            issuer.addnext(signature)
        else:
            elem[0].insert(0, signature)

        ref = signature.addReference(xmlsec.TransformSha1)
        ref.addTransform(xmlsec.TransformEnveloped)
        ref.addTransform(xmlsec.TransformExclC14N)

        key_info = signature.ensureKeyInfo()
        key_info.addX509Data()

        dsig_ctx = xmlsec.DSigCtx()
        sign_key = xmlsec.Key.loadMemory(key, xmlsec.KeyDataFormatPem, None)

        file_cert = OneLogin_Saml2_Utils.write_temp_file(cert)
        sign_key.loadCert(file_cert.name, xmlsec.KeyDataFormatCertPem)
        file_cert.close()

        dsig_ctx.signKey = sign_key
        dsig_ctx.sign(signature)

        newdoc = parseString(etree.tostring(elem, encoding="unicode").encode("utf-8"))

        signature_nodes = newdoc.getElementsByTagName("Signature")

        for signature in signature_nodes:
            signature.removeAttribute("xmlns")
            signature.setAttribute("xmlns:ds", OneLogin_Saml2_Constants.NS_DS)
            if not signature.tagName.startswith("ds:"):
                signature.tagName = "ds:" + signature.tagName
            nodes = signature.getElementsByTagName("*")
            for node in nodes:
                if not node.tagName.startswith("ds:"):
                    node.tagName = "ds:" + node.tagName

        return newdoc.saveXML(newdoc.firstChild)
示例#45
0
 def __init__(self, xml_string):
     defusedxml.defuse_stdlib()
     self.dom_obj = minidom.parseString(xml_string)
示例#46
0
    def is_valid(settings, request, get_data, debug=False):
        """
        Checks if the Logout Request recieved is valid
        :param settings: Settings
        :type settings: OneLogin_Saml2_Settings
        :param request: Logout Request Message
        :type request: string|DOMDocument
        :return: If the Logout Request is or not valid
        :rtype: boolean
        """
        try:
            if isinstance(request, Document):
                dom = request
            else:
                dom = parseString(request)

            idp_data = settings.get_idp_data()
            idp_entity_id = idp_data['entityId']

            if settings.is_strict():
                res = OneLogin_Saml2_Utils.validate_xml(dom, 'saml-schema-protocol-2.0.xsd', debug)
                if not isinstance(res, Document):
                    raise Exception('Invalid SAML Logout Request. Not match the saml-schema-protocol-2.0.xsd')

                security = settings.get_security_data()

                current_url = OneLogin_Saml2_Utils.get_self_url_no_query(get_data)

                # Check NotOnOrAfter
                if dom.documentElement.hasAttribute('NotOnOrAfter'):
                    na = OneLogin_Saml2_Utils.parse_SAML_to_time(dom.documentElement.getAttribute('NotOnOrAfter'))
                    if na <= OneLogin_Saml2_Utils.now():
                        raise Exception('Timing issues (please check your clock settings)')

                # Check destination
                if dom.documentElement.hasAttribute('Destination'):
                    destination = dom.documentElement.getAttribute('Destination')
                    if destination != '':
                        if current_url not in destination:
                            raise Exception('The LogoutRequest was received at $currentURL instead of $destination')

                # Check issuer
                issuer = OneLogin_Saml2_Logout_Request.get_issuer(dom)
                if issuer is not None and issuer != idp_entity_id:
                    raise Exception('Invalid issuer in the Logout Request')

                if security['wantMessagesSigned']:
                    if 'Signature' not in get_data:
                        raise Exception('The Message of the Logout Request is not signed and the SP require it')

            if 'Signature' in get_data:
                if 'SigAlg' not in get_data:
                    sign_alg = OneLogin_Saml2_Constants.RSA_SHA1
                else:
                    sign_alg = get_data['SigAlg']

                if sign_alg != OneLogin_Saml2_Constants.RSA_SHA1:
                    raise Exception('Invalid signAlg in the recieved Logout Request')

                signed_query = 'SAMLRequest=%s' % quote_plus(get_data['SAMLRequest'])
                if 'RelayState' in get_data:
                    signed_query = '%s&RelayState=%s' % (signed_query, quote_plus(get_data['RelayState']))
                signed_query = '%s&SigAlg=%s' % (signed_query, quote_plus(sign_alg))

                if 'x509cert' not in idp_data or idp_data['x509cert'] is None:
                    raise Exception('In order to validate the sign on the Logout Request, the x509cert of the IdP is required')
                cert = idp_data['x509cert']

                if not OneLogin_Saml2_Utils.validate_binary_sign(signed_query, b64decode(get_data['Signature']), cert):
                    raise Exception('Signature validation failed. Logout Request rejected')

            return True
        except Exception as err:
            debug = settings.is_debug_active()
            if debug:
                print err
            return False
示例#47
0
def create(api, fromTemplateId, name, toDataStore):
    """
    Publish the machine (makes a template from it so we can create COWs) and returns the template id of
    the creating machine

    Args:
        fromTemplateId: id of the base template
        name: Name of the machine (care, only ascii characters and no spaces!!!)

    Returns
        Raises an exception if operation could not be acomplished, or returns the id of the template being created.

    Note:
        Maybe we need to also clone the hard disk?
    """
    templateId = None
    try:
        # First, we clone the themplate itself
        # templateId = api.call('template.clone', int(fromTemplateId), name)
        templateId = api.cloneTemplate(fromTemplateId, name)

        # Now copy cloned images if possible
        imgs = dict(((i[1], i[0]) for i in api.enumImages()))

        info = api.templateInfo(templateId)[1]
        template = minidom.parseString(info).getElementsByTagName('TEMPLATE')[0]
        logger.debug('XML: {}'.format(template.toxml()))

        counter = 0
        for dsk in template.getElementsByTagName('DISK'):
            counter += 1
            imgIds = dsk.getElementsByTagName('IMAGE_ID')
            if len(imgIds) == 0:
                fromId = False
                try:
                    node = dsk.getElementsByTagName('IMAGE')[0].childNodes[0]
                except IndexError:
                    continue  # Skip this unknown node
                imgName = node.data
                # Locate
                try:
                    imgId = imgs[imgName.strip()]
                except KeyError:
                    raise Exception('Image "{}" could not be found!. Check the opennebula template'.format(imgName.strip()))
            else:
                fromId = True
                node = imgIds[0].childNodes[0]
                imgId = node.data

            logger.debug('Found {} for cloning'.format(imgId))

            # if api.imageInfo(imgId)[0]['IMAGE']['STATE'] != '1':
            #    raise Exception('The base machines images are not in READY state')

            # Now clone the image
            imgName = sanitizeName(name + ' DSK ' + six.text_type(counter))
            newId = api.cloneImage(imgId, imgName, toDataStore)  # api.call('image.clone', int(imgId), imgName, int(toDataStore))
            # Ensure image is non persistent
            api.makePersistentImage(newId, False)
            # Now Store id/name
            if fromId is True:
                node.data = six.text_type(newId)
            else:
                node.data = imgName

        # Now update the clone
        # api.call('template.update', templateId, template.toxml())
        api.updateTemplate(templateId, template.toxml())

        return six.text_type(templateId)
    except Exception as e:
        logger.exception('Creating template on OpenNebula: {}'.format(e))
        try:
            api.deleteTemplate(templateId)  # Try to remove created template in case of fail
        except Exception:
            pass
        raise e
示例#48
0
    def generate_name_id(value, sp_nq, sp_format, cert=None, debug=False):
        """
        Generates a nameID.

        :param value: fingerprint
        :type: string

        :param sp_nq: SP Name Qualifier
        :type: string

        :param sp_format: SP Format
        :type: string

        :param cert: IdP Public Cert to encrypt the nameID
        :type: string

        :param debug: Activate the xmlsec debug
        :type: bool

        :returns: DOMElement | XMLSec nameID
        :rtype: string
        """
        doc = Document()
        name_id_container = doc.createElementNS(OneLogin_Saml2_Constants.NS_SAML, 'container')
        name_id_container.setAttribute("xmlns:saml", OneLogin_Saml2_Constants.NS_SAML)

        name_id = doc.createElement('saml:NameID')
        name_id.setAttribute('SPNameQualifier', sp_nq)
        name_id.setAttribute('Format', sp_format)
        name_id.appendChild(doc.createTextNode(value))
        name_id_container.appendChild(name_id)

        if cert is not None:
            xml = name_id_container.toxml()
            elem = fromstring(xml)

            xmlsec.initialize()

            if debug:
                xmlsec.set_error_callback(print_xmlsec_errors)

            # Load the public cert
            mngr = xmlsec.KeysMngr()
            file_cert = OneLogin_Saml2_Utils.write_temp_file(cert)
            key_data = xmlsec.Key.load(file_cert.name, xmlsec.KeyDataFormatCertPem, None)
            key_data.name = basename(file_cert.name)
            mngr.addKey(key_data)
            file_cert.close()

            # Prepare for encryption
            enc_data = EncData(xmlsec.TransformAes128Cbc, type=xmlsec.TypeEncElement)
            enc_data.ensureCipherValue()
            key_info = enc_data.ensureKeyInfo()
            # enc_key = key_info.addEncryptedKey(xmlsec.TransformRsaPkcs1)
            enc_key = key_info.addEncryptedKey(xmlsec.TransformRsaOaep)
            enc_key.ensureCipherValue()

            # Encrypt!
            enc_ctx = xmlsec.EncCtx(mngr)
            enc_ctx.encKey = xmlsec.Key.generate(xmlsec.KeyDataAes, 128, xmlsec.KeyDataTypeSession)

            edata = enc_ctx.encryptXml(enc_data, elem[0])

            newdoc = parseString(etree.tostring(edata))

            if newdoc.hasChildNodes():
                child = newdoc.firstChild
                child.removeAttribute('xmlns')
                child.removeAttribute('xmlns:saml')
                child.setAttribute('xmlns:xenc', OneLogin_Saml2_Constants.NS_XENC)
                child.setAttribute('xmlns:dsig', OneLogin_Saml2_Constants.NS_DS)

            nodes = newdoc.getElementsByTagName("*")
            for node in nodes:
                if node.tagName == 'ns0:KeyInfo':
                    node.tagName = 'dsig:KeyInfo'
                    node.removeAttribute('xmlns:ns0')
                    node.setAttribute('xmlns:dsig', OneLogin_Saml2_Constants.NS_DS)
                else:
                    node.tagName = 'xenc:' + node.tagName

            encrypted_id = newdoc.createElement('saml:EncryptedID')
            encrypted_data = newdoc.replaceChild(encrypted_id, newdoc.firstChild)
            encrypted_id.appendChild(encrypted_data)
            return newdoc.saveXML(encrypted_id)
        else:
            return doc.saveXML(name_id)
示例#49
0
    def add_sign(xml, key, cert, debug=False):
        """
        Adds signature key and senders certificate to an element (Message or
        Assertion).

        :param xml: The element we should sign
        :type: string | Document

        :param key: The private key
        :type: string

        :param debug: Activate the xmlsec debug
        :type: bool

        :param cert: The public
        :type: string
        """
        if xml is None or xml == '':
            raise Exception('Empty string supplied as input')
        elif isinstance(xml, etree._Element):
            elem = xml
        elif isinstance(xml, Document):
            xml = xml.toxml()
            elem = fromstring(str(xml))
        elif isinstance(xml, Element):
            xml.setAttributeNS(
                unicode(OneLogin_Saml2_Constants.NS_SAMLP),
                'xmlns:samlp',
                unicode(OneLogin_Saml2_Constants.NS_SAMLP)
            )
            xml.setAttributeNS(
                unicode(OneLogin_Saml2_Constants.NS_SAML),
                'xmlns:saml',
                unicode(OneLogin_Saml2_Constants.NS_SAML)
            )
            xml = xml.toxml()
            elem = fromstring(str(xml))
        elif isinstance(xml, basestring):
            elem = fromstring(str(xml))
        else:
            raise Exception('Error parsing xml string')

        xmlsec.initialize()

        if debug:
            xmlsec.set_error_callback(print_xmlsec_errors)

        # Sign the metadacta with our private key.
        signature = Signature(xmlsec.TransformExclC14N, xmlsec.TransformRsaSha1)

        issuer = OneLogin_Saml2_Utils.query(elem, '//saml:Issuer')
        if len(issuer) > 0:
            issuer = issuer[0]
            issuer.addnext(signature)
        else:
            elem[0].insert(0, signature)

        ref = signature.addReference(xmlsec.TransformSha1)
        ref.addTransform(xmlsec.TransformEnveloped)
        ref.addTransform(xmlsec.TransformExclC14N)

        key_info = signature.ensureKeyInfo()
        key_info.addX509Data()

        dsig_ctx = xmlsec.DSigCtx()
        sign_key = xmlsec.Key.loadMemory(key, xmlsec.KeyDataFormatPem, None)

        file_cert = OneLogin_Saml2_Utils.write_temp_file(cert)
        sign_key.loadCert(file_cert.name, xmlsec.KeyDataFormatCertPem)
        file_cert.close()

        dsig_ctx.signKey = sign_key
        dsig_ctx.sign(signature)

        newdoc = parseString(etree.tostring(elem))

        signature_nodes = newdoc.getElementsByTagName("Signature")

        for signature in signature_nodes:
            signature.removeAttribute('xmlns')
            signature.setAttribute('xmlns:ds', OneLogin_Saml2_Constants.NS_DS)
            if not signature.tagName.startswith('ds:'):
                signature.tagName = 'ds:' + signature.tagName
            nodes = signature.getElementsByTagName("*")
            for node in nodes:
                if not node.tagName.startswith('ds:'):
                    node.tagName = 'ds:' + node.tagName

        return newdoc.saveXML(newdoc.firstChild)
 def __init__(self, field, debug=False):
     self.domfield = minidom.parseString(field)
     self.debug = debug
示例#51
0
文件: parsers.py 项目: nomoketo/choo
 def printable_data(self, pretty=True):
     string = ET.tostring(self.data, 'utf-8').decode()
     if pretty:
         string = minidom.parseString(string).toprettyxml(indent='    ').split('\n', 1)[1]
     return string
示例#52
0
    def add_sign_with_id(xml, uid, key, cert, debug=False, sign_algorithm=OneLogin_Saml2_Constants.RSA_SHA1,
                         digest_algorithm=OneLogin_Saml2_Constants.SHA1):

        # thanks to https://github.com/onelogin/python-saml/pull/78/files for the help. credit to @tachang

        xmlsec.initialize()
        xmlsec.set_error_callback(print_xmlsec_errors)
        #
        sign_algorithm_transform_map = {
             OneLogin_Saml2_Constants.DSA_SHA1: xmlsec.TransformDsaSha1,
             OneLogin_Saml2_Constants.RSA_SHA1: xmlsec.TransformRsaSha1,
             OneLogin_Saml2_Constants.RSA_SHA256: xmlsec.TransformRsaSha256,
             OneLogin_Saml2_Constants.RSA_SHA384: xmlsec.TransformRsaSha384,
             OneLogin_Saml2_Constants.RSA_SHA512: xmlsec.TransformRsaSha512
         }
        sign_algorithm_transform = sign_algorithm_transform_map.get(sign_algorithm, xmlsec.TransformRsaSha1)

        signature = Signature(xmlsec.TransformExclC14N, sign_algorithm_transform)

        if xml is None or xml == '':
            raise Exception('Empty string supplied as input')
        elif isinstance(xml, etree._Element):
            doc = xml
        elif isinstance(xml, Document):
            xml = xml.toxml()
            doc= fromstring(str(xml))
        elif isinstance(xml, Element):
            xml.setAttributeNS(
                unicode(OneLogin_Saml2_Constants.NS_SAMLP),
                'xmlns:samlp',
                unicode(OneLogin_Saml2_Constants.NS_SAMLP)
            )
            xml.setAttributeNS(
                unicode(OneLogin_Saml2_Constants.NS_SAML),
                'xmlns:saml',
                unicode(OneLogin_Saml2_Constants.NS_SAML)
            )
            xml = xml.toxml()
            doc = fromstring(str(xml))
        elif isinstance(xml, basestring):
            doc = fromstring(str(xml))
        else:
            raise Exception('Error parsing xml string')

        # # ID attributes different from xml:id must be made known by the application through a call
        # # to the addIds(node, ids) function defined by xmlsec.
        xmlsec.addIDs(doc, ['ID'])

        doc.insert(0, signature)

        digest_algorithm_transform_map = {
            OneLogin_Saml2_Constants.SHA1: xmlsec.TransformSha1,
            OneLogin_Saml2_Constants.SHA256: xmlsec.TransformSha256
        }

        digest_algorithm_transform = digest_algorithm_transform_map.get(digest_algorithm, xmlsec.TransformRsaSha1)

        ref = signature.addReference(digest_algorithm_transform, uri="#%s" % uid)
        ref.addTransform(xmlsec.TransformEnveloped)
        ref.addTransform(xmlsec.TransformExclC14N)

        key_info = signature.ensureKeyInfo()
        key_info.addKeyName()
        key_info.addX509Data()

        dsig_ctx = xmlsec.DSigCtx()

        sign_key = xmlsec.Key.loadMemory(key, xmlsec.KeyDataFormatPem, None)

        from tempfile import NamedTemporaryFile
        cert_file = NamedTemporaryFile(delete=True)
        cert_file.write(cert)
        cert_file.seek(0)

        sign_key.loadCert(cert_file.name, xmlsec.KeyDataFormatPem)

        dsig_ctx.signKey = sign_key

        # # Note: the assignment below effectively copies the key
        dsig_ctx.sign(signature)

        newdoc = parseString(etree.tostring(doc))

        return newdoc.saveXML(newdoc.firstChild)