Changeset 4045


Ignore:
Timestamp:
16/07/08 14:46:39 (11 years ago)
Author:
pjkersha
Message:

tidying up etree.py and elementtreeproxy.py ElementTree WS-Security signature handler and ZSI interface class respectively.

Location:
TI12-security/trunk/python
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • TI12-security/trunk/python/Tests/etreewss/client/fred_eg.py

    r4038 r4045  
    77soapNSURI = 'http://schemas.xmlsoap.org/soap/envelope/' 
    88 
    9 env = ElementTree.Element("{%s}Envelope" % soapNSURI)  
    10 env.set("xmlns:SOAP-ENV", soapNSURI) 
     9envElem = ElementTree.Element("{%s}Envelope" % soapNSURI)  
     10envElem.set("xmlns:SOAP-ENV", soapNSURI) 
    1111 
    12 hdr = ElementTree.Element("{%s}Header" % soapNSURI) 
    13 env.set("xmlns:SOAP-ENV", soapNSURI) 
    14 env.append(hdr) 
     12hdrElem = ElementTree.Element("{%s}Header" % soapNSURI) 
     13envElem.set("xmlns:SOAP-ENV", soapNSURI) 
     14envElem.append(hdrElem) 
    1515 
    16 body = ElementTree.Element("{%s}Body" % soapNSURI) 
    17 env.set("xmlns:SOAP-ENV", soapNSURI) 
    18 env.append(body) 
     16bodyElem = ElementTree.Element("{%s}Body" % soapNSURI) 
     17envElem.set("xmlns:SOAP-ENV", soapNSURI) 
     18envElem.append(bodyElem) 
    1919wsuNS='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' 
    20 env.set('xmlns:wsu', wsuNS) 
    21 env.set('URI', "http://blah") 
    22 #body.set('wsu:Id', '#body') 
    23 body.set('{%s}Id' % wsuNS, '#body') 
    24 ns = {'wsu': wsuNS} 
    25 print "wsu:Id elements = %s" % env.findall('.//*[@wsu:Id]', namespaces=ns) 
    26 print "Calling C14N 1st time ..." 
     20envElem.set('xmlns:wsu', wsuNS) 
     21envElem.set('URI', "http://blah") 
     22bodyElem.set('{%s}Id' % wsuNS, '#body') 
     23 
     24ndgSecNS = "urn:ndg:security:test:wssecurity" 
     25echoElem = ElementTree.Element("{%s}Echo" % ndgSecNS) 
     26echoElem.set('xmlns:ns0', ndgSecNS) 
     27bodyElem.append(echoElem) 
     28 
     29echoInElem = ElementTree.Element("{%s}EchoIn" % ndgSecNS) 
     30echoInElem.text = "hello" 
     31echoElem.append(echoInElem) 
     32 
     33print "Calling Inclusive C14N  ..." 
    2734f = StringIO() 
    28 ElementC14N.write(ElementC14N.build_scoped_tree(env), f) 
     35ElementC14N.write(ElementC14N.build_scoped_tree(envElem), f) 
    2936print f.getvalue() 
    3037 
    31 print "Calling C14N 2nd time ..." 
     38print "Calling Exclusive C14N for body element ..." 
    3239g = StringIO() 
    33 ElementC14N.write(ElementC14N.build_scoped_tree(env), g) 
     40ElementC14N.write(ElementC14N.build_scoped_tree(envElem), g, exclusive=True, 
     41                  subset=bodyElem) 
    3442c14n = g.getvalue() 
    3543print c14n 
    36 filePath = './c14n.xml' 
    37 open(filePath, 'w').write(c14n) 
    38  
    39 env2 = ElementC14N.parse(filePath) 
    40 print "wsu:Id elements = %s" % env2.findall('.//*[@wsu:Id]', namespaces=ns) 
    41  
    42 print "Calling C14N for echo message ..." 
    43 import pdb;pdb.set_trace() 
    44  
    45 etree = ElementC14N.parse('/home/pjkersha/Desktop/soap-etree.xml') 
    46 f = StringIO() 
    47 elem = etree.getroot() 
    48 elem._scope = etree._scope 
    49 ElementC14N.write(ElementC14N.build_scoped_tree(elem), f) 
    50 print f.getvalue() 
    5144 
    5245 
  • TI12-security/trunk/python/ndg.security.common/ndg/security/common/wssecurity/etree.py

    r4038 r4045  
    855855        soapHdrElem.set("xmlns:%s" % 'wsse', OASIS.WSSE) 
    856856        soapHdrElem.set("xmlns:%s" % 'wsse11', OASIS.WSSE11) 
    857         soapHdrElem.set("xmlns:%s" % 'wsu', WSU.UTILITY) 
     857#        soapHdrElem.set("xmlns:%s" % 'wsu', WSU.UTILITY) 
     858        self._soapEnvElem.set("xmlns:%s" % 'wsu', WSU.UTILITY) 
    858859        soapHdrElem.set("xmlns:%s" % 'ds', DSIG.BASE) 
    859860         
     
    968969         
    969970        # Add Reference to body so that it can be included in the signature 
    970         soapBodyElem.set('xmlns:wsu', WSU.UTILITY) 
     971        #soapBodyElem.set('xmlns:wsu', WSU.UTILITY) - results in duplicate xmlns declarations 
    971972        soapBodyElem.set('{%s}Id' % WSU.UTILITY, 'body') 
    972973 
  • TI12-security/trunk/python/ndg.security.common/ndg/security/common/zsi_utils/elementtreeproxy.py

    r4038 r4045  
    2323    _soap_enc_nsuri = SOAP.ENC 
    2424 
    25     def __init__(self, message=None, etree=None, elem=None): 
     25    def __init__(self, message=None, rootElem=None, elem=None): 
    2626        '''Initialize''' 
    27         self._etree = etree 
    2827        self._elem = elem 
     28        self._rootElem = rootElem 
     29        self._etree = None 
     30        self._rootETree = None 
    2931         
    3032        # Flag to enable correct behaviour for DOM childNodes 
    31         self._treeRoot = False 
    32          
    33         if isinstance(etree, ElementTree.ElementTree) and elem is None: 
    34             self._elem = etree.getroot() 
    35         elif isinstance(elem, ElementTree.Element) and etree is None: 
     33        self._treeRootSet = False 
     34         
     35        if isinstance(elem, ElementTree.Element): 
    3636            self._etree = ElementTree.ElementTree(element=elem) 
     37         
     38        if isinstance(rootElem, ElementTree.Element): 
     39            self._rootETree = ElementTree.ElementTree(element=rootElem) 
    3740             
    3841         
     
    6265            # Search for matching prefix 
    6366            matchingPrefix = None 
    64             for elem in self._elem.getiterator(): 
     67            for elem in self._rootElem.getiterator(): 
    6568                for k, v in elem.items(): 
    6669                    if k.startswith("xmlns:") and v == namespaceURI: 
    6770                        # Namespace declaration found 
    6871                        matchingPrefix = k[6:] 
     72                        matchingElem = elem 
    6973                        break 
    7074 
    71             elem = ElementTree.Element("{%s}%s" % (namespaceURI, localName)) 
     75            newElem = ElementTree.Element("{%s}%s" % (namespaceURI, localName)) 
    7276 
    7377            if not matchingPrefix: 
    7478                matchingPrefix = prefix 
    75  
    76             elem.set("xmlns:%s" % matchingPrefix, namespaceURI)                 
     79                 
     80                # No prefix found so add namespace declaration  
     81                newElem.set("xmlns:%s" % matchingPrefix, namespaceURI) 
    7782        else: 
    7883            assert prefix, "Prefix must be set - no namespaceURI was provided" 
    79  
    80             # Search for matching NS 
    81             for elem in self._etree.getiterator(): 
    82                 for k, v in elem.items(): 
    83                     if k.startswith("xmlns:") and k[6:] == prefix: 
    84                         namespaceURI  = v 
    85                         break 
    86                      
    87             elem = ElementTree.Element("{%s}%s:%s" % (namespaceURI,  
    88                                                       prefix, 
    89                                                       localName)) 
    90          
    91              
    92         self._elem.append(elem) 
    93          
    94         eproxy = ElementTreeProxy(etree=self._etree, elem=elem) 
     84         
     85        self._elem.append(newElem)         
     86        eproxy = ElementTreeProxy(rootElem=self._rootElem, elem=newElem) 
    9587 
    9688        return eproxy 
     
    109101        self._etree = ElementTree.ElementTree(element=self._elem) 
    110102        self._elem.set("xmlns:%s" % prefix, namespaceURI) 
     103        self._rootElem = self._elem 
     104        self._rootETree = self._etree 
    111105         
    112106    def getElement(self, namespaceURI, localName): 
     
    161155         
    162156    def canonicalize(self, **kw): 
    163         # Make copy as this is a destructive process - attributes get deleted! 
    164 #        etree = self._etree.copy() 
    165 #        root = ElementTree.ElementTree(etree) 
    166 #     
    167 #        root._scope = {} 
    168 #        root._parent=dict((c, p) for p in etree.getiterator() for c in p) 
    169 #     
    170 #        # build scope map 
    171 #        for e in self._etree.getiterator(): 
    172 #            scope = [] 
    173 #            for k in e.keys(): 
    174 #                if k.startswith("xmlns:"): 
    175 #                    # move xmlns prefix to scope map 
    176 #                    scope.append((k[6:], e.get(k))) 
    177 #                    del e.attrib[k] 
    178 #            if scope: 
    179 #                root._scope[e] = scope 
    180 #     
    181 #        # Save as C14N 
    182 #        f = StringIO() 
    183 #        ElementC14N.write(root, f, **kw) 
    184 #        c14n = f.getvalue() 
    185 #        return c14n 
    186  
    187 #        root = ElementTree.ElementTree(self._etree) 
    188 #     
    189 #        root._scope = {} 
    190 #        root._parent=dict((c, p) for p in self._etree.getiterator() for c in p) 
    191 #     
    192 #        # build scope map 
    193 #        for e in self._etree.getiterator(): 
    194 #            scope = [] 
    195 #            for k in e.keys(): 
    196 #                if k.startswith("xmlns:"): 
    197 #                    # move xmlns prefix to scope map 
    198 #                    scope.append((k[6:], e.get(k))) 
    199 #                    del e.attrib[k] 
    200 #            if scope: 
    201 #                root._scope[e] = scope 
    202 #     
    203 #        # Save as C14N 
    204 #        f = StringIO() 
    205 #        ElementC14N.write(root, f, **kw) 
    206 #        c14n = f.getvalue() 
    207 #        return c14n 
    208  
    209 #        f = StringIO() 
    210 #        ElementC14N.write(ElementC14N.build_scoped_tree(self._elem), f, **kw) 
    211 #        c14n = f.getvalue() 
    212  
     157        '''Canonicalize using ElementC14N.write - see ElementC14N for details 
     158        of keyword options''' 
     159         
    213160        f = StringIO() 
    214161 
     
    217164        # In the latter case a call to build the scope is required 
    218165        if hasattr(self._etree, '_scope'): 
    219             ElementC14N.write(self._etree, f, **kw) 
     166            ElementC14N.write(self._rootETree, f, **kw) 
    220167        else: 
    221             ElementC14N.write(_build_scoped_tree(self._elem), f,  
     168            ElementC14N.write(ElementC14N.build_scoped_tree(self._rootElem), 
     169                              f, 
    222170                              **kw) 
    223171             
     
    246194        self._etree = ElementC14N.parse(fInput) 
    247195        self._elem = self._etree.getroot() 
    248         self._treeRoot = True 
     196        self._treeRootSet = True 
    249197        return self 
    250198     
     
    255203         
    256204        # Check for top of tree 
    257         if self._treeRoot:  
     205        if self._treeRootSet:  
    258206            # Return a copy of the top level element with _treeRoot set to  
    259207            # False so that the next time this method is called the children of 
    260208            # the top level element will be returned instead 
    261             return [ElementTreeProxy(etree=self._etree)] 
     209            return [ElementTreeProxy(elem=self._elem, rootElem=self._rootElem)] 
    262210        else: 
    263             return [ElementTreeProxy(elem=elem, etree=self._etree) \ 
     211            return [ElementTreeProxy(elem=elem, rootElem=self._rootElem) \ 
    264212                    for elem in list(self._elem)] 
    265213                 
     
    289237        are not included in ET 
    290238         
    291         TODO: alter parser to keep xmlns declarations''' 
     239        TODO: Use attrib Element attribute + get xmlns declarations from 
     240        _scope attribute.  Returning [] to ParsedSoap satisifes the interface 
     241        for doc creation but is only a temp fix ''' 
    292242        return [] 
    293243     
     
    296246from copy import deepcopy 
    297247 
    298 def _build_scoped_tree(_elem): 
     248def _build_scoped_tree(elem): 
    299249     
    300250    # Make a copy because attributes are to be deleted. 
    301     elem = deepcopy(_elem) 
    302     # Deep copy misses out 'attrib' Element attribute 
    303     import pdb;pdb.set_trace() 
    304     for e, _e in zip(elem.getiterator(), _elem.getiterator()): 
    305         e.attrib = _e.attrib.copy() 
     251#    elem = deepcopy(_elem) 
     252#     
     253#    # Deep copy misses out 'attrib' Element attribute 
     254#    for e, _e in zip(elem.getiterator(), _elem.getiterator()): 
     255#        if e.tag != _e.tag: 
     256#            raise AttributeError("Tags don't match") 
     257#        e.attrib = _e.attrib.copy() 
     258#         
    306259         
    307260    root = ElementTree.ElementTree(elem) 
     
    315268                # move xmlns prefix to scope map 
    316269                scope.append((k[6:], e.get(k))) 
    317                 del e.attrib[k] 
     270                #del e.attrib[k] 
    318271        if scope: 
    319272            root._scope[e] = scope 
Note: See TracChangeset for help on using the changeset viewer.