source: TI12-security/trunk/python/ndg.security.server/ndg/security/server/AttAuthority/__init__.py @ 2136

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.server/ndg/security/server/AttAuthority/__init__.py@2136
Revision 2136, 37.2 KB checked in by pjkersha, 13 years ago (diff)

python/ndg.security.server/setup.py:

  • comment out Twisted from install - won't do egg install
  • updated long description

python/ndg.security.server/ndg/security/server/AttAuthority/server-config.tac:

  • added verifyingCertFilePath keyword to SignatureHandler? initialisation
  • added SSL capability

python/conf/attAuthorityProperties.xml,
python/ndg.security.test/ndg/security/test/AttAuthority/siteAAttAuthorityProperties.xml,
python/ndg.security.test/ndg/security/test/AttAuthority/siteBAttAuthorityProperties.xml,
python/ndg.security.server/ndg/security/server/AttAuthority/init.py:
added element names for reading SSL settings from properties file.

python/ndg.security.server/ndg/security/server/SessionMgr/server-config.tac:
added verifyingCertFilePath keyword to SignatureHandler? initialisation

python/conf/sessionMgrProperties.xml,
python/ndg.security.test/ndg/security/test/SessionMgr/sessionMgrProperties.xml,
python/ndg.security.server/ndg/security/server/SessionMgr/init.py:
added clntCertFile properties file element name for setting certificate for
verifying incoming SOAP messages.

python/ndg.security.server/ndg/security/server/SessionMgr/Makefile:
corrected typo.

python/ndg.security.server/ndg/security/server/MyProxy.py:
Put OpenSSLConfig and OpenSSLConfigError classes into their own package
'openssl' so that they can also be used by the Certificate Authority client.

python/www/html/certificateAuthority.wsdl,
python/ndg.security.server/ndg/security/server/ca/CertificateAuthority_services_server.py,
python/ndg.security.common/ndg/security/common/ca/CertificateAuthority_services_types.py,
python/ndg.security.common/ndg/security/common/ca/CertificateAuthority_services.py: updated operations to issueCert, revokeCert and getCRL.

python/ndg.security.test/ndg/security/test/AttAuthority/attAuthorityClientTest.cfg: changed address of service to connect to.

python/ndg.security.test/ndg/security/test/SessionMgr/sessionMgrClientTest.cfg:
alternative username connection settings

python/ndg.security.common/ndg/security/common/AttAuthority/init.py:
fixed typos in error message and comments.

ython/ndg.security.common/ndg/security/common/XMLSec.py: changed call to
getAttributeNodeNS to getAttributeNode for retrieving reference element URI
attribute.

python/ndg.security.common/ndg/security/common/ca/init.py: code for
Certificate Authority client

python/ndg.security.common/ndg/security/common/wsSecurity.py:

  • tidied up imports
  • added properties for setting keywords to reference and SignedInfo? C14N
  • changed sign method so that it is truely configurable allow use of inclusive or exclusive C14N based on the keywords set for reference and SignedInfo? C14N calls.
  • swapped calls to getAttributeNodeNS with getAttributeNode where appropriate.

java/DEWS/AttAuthority/appClientModule/META-INF/ibm-webservicesclient-bnd.xmi,
java/DEWS/AttAuthority/build/classes/META-INF/ibm-webservicesclient-bnd.xmi:
updated to that request generator correctly places X.509 cert in
BinarySecurityToken? element.

java/DEWS/AttAuthority/appClientModule/Main.java,
java/DEWS/AttAuthority/appClientjava/DEWS/AttAuthority/appClientModule/META-INF/ibm-webservicesclient-bnd.xmiModule/Main.java:
include calls to getX509Cert and getAttCert methods.

java/DEWS/SessionMgr/build/classes/META-INF/ibm-webservicesclient-bnd.xmi,
java/DEWS/SessionMgr/appClientModule/META-INF/ibm-webservicesclient-bnd.xmi:
updates for testing Session MAnager client

java/DEWS/SessionMgr/appClientModule/Main.java: switched username setting.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1"""NDG Attribute Authority handles security authentication and authorization
2
3NERC Data Grid Project
4
5@author P J Kershaw 15/04/05
6
7@copyright (C) 2007 CCLRC & NERC
8
9@license This software may be distributed under the terms of the Q Public
10License, version 1.0 or later.
11"""
12
13reposID = '$Id$'
14
15import types
16
17
18# Create unique names for attribute certificates
19import tempfile
20import os
21
22# Alter system path for dynamic import of user roles class
23import sys
24
25# For parsing of properties file
26import cElementTree as ElementTree
27
28# X509 Certificate handling
29from ndg.security.common.X509 import *
30
31# NDG Attribute Certificate
32from ndg.security.common.AttCert import *
33
34
35#_____________________________________________________________________________
36class AttAuthorityError(Exception):
37    """Exception handling for NDG Attribute Authority class."""
38
39
40#_____________________________________________________________________________
41class AttAuthorityAccessDenied(AttAuthorityError):
42    """NDG Attribute Authority - access denied exception.
43
44    Raise from getAttCert method where no roles are available for the user
45    but that the request is otherwise valid.  In all other error cases raise
46    AttAuthorityError"""   
47
48class AttAuthorityNoTrustedHosts(AttAuthorityError):
49    """Raise from getTrustedHosts if there are no trusted hosts defined in
50    the map configuration"""
51
52class AttAuthorityNoMatchingRoleInTrustedHosts(AttAuthorityError):
53    """Raise from getTrustedHosts if there is no mapping to any of the
54    trusted hosts for the given input role name"""
55
56
57#_____________________________________________________________________________
58class AttAuthority(dict):
59    """NDG Attribute Authority - server for allocation of user authorization
60    tokens - attribute certificates.
61   
62    @type __validKeys: list
63    @cvar __validKeys: valid configuration property keywords - properties file
64    must contain these
65   
66    @type __confDir: string
67    @cvar __confDir: configuration directory under $NDG_DIR - default location
68    for properties file
69   
70    @type __propFileName: string
71    @cvar __propFileName: default file name for properties file under
72    __confDir
73    """
74
75    # Code designed from NERC Data Grid Enterprise and Information Viewpoint
76    # documents.
77    #
78    # Also, draws from Neil Bennett's ACServer class used in the Java
79    # implementation of NDG Security
80
81    __confDir = "conf"
82    __propFileName = "attAuthorityProperties.xml"
83   
84    # valid configuration property keywords
85    __validKeys = ( 'name',
86                    'portNum',
87                    'useSSL',
88                    'sslCertFile',
89                    'sslKeyFile',
90                    'keyFile',
91                    'keyPwd',
92                    'certFile',
93                    'caCertFile',
94                    'clntCertFile',
95                    'attCertLifetime',
96                    'attCertNotBeforeOff',
97                    'attCertFilePfx',
98                    'attCertFileSfx',
99                    'mapConfigFile',
100                    'attCertDir',
101                    'dnSeparator',
102                    'userRolesModFilePath',
103                    'userRolesModName',
104                    'userRolesClassName',
105                    'userRolesPropFile')
106   
107    def __init__(self, propFilePath=None, bReadMapConfig=True):
108        """Create new NDG Attribute Authority instance
109
110        @type propFilePath: string
111        @keyword propFilePath: path to file containing Attribute Authority
112        configuration parameters.  It defaults to $NDGSEC_AA_PROPFILEPATH or
113        if not set, $NDG_DIR/conf/attAuthorityProperties.xml
114        @type bReadMapConfig: boolean
115        @keyword bReadMapConfig: by default the Map Configuration file is
116        read.  Set this flag to False to override.
117        """
118
119        # Base class initialisation
120        dict.__init__(self)
121
122        # Set from input or use defaults based or environment variables
123        self.setPropFilePath(propFilePath)
124
125        # Initialise role mapping look-ups - These are set in readMapConfig()
126        self.__mapConfig = None
127        self.__localRole2RemoteRole = None
128        self.__remoteRole2LocalRole = None
129
130
131        # Configuration file properties are held together in a dictionary
132        self.__prop = {}
133
134        # Read Attribute Authority Properties file
135        self.readProperties()
136
137        # Read the Map Configuration file
138        if bReadMapConfig:
139            self.readMapConfig()
140
141        # Instantiate Certificate object
142        self.__cert = X509Cert(self.__prop['certFile'])
143        self.__cert.read()
144
145        # Check it's valid
146        try:
147            self.__cert.isValidTime(raiseExcep=True)
148           
149        except Exception, e:
150            raise AttAuthorityError, \
151                    "Attribute Authority's certificate is invalid: " + str(e)
152       
153        # Check CA certificate
154        caCert = X509Cert(self.__prop['caCertFile'])
155        caCert.read()
156       
157        try:
158            caCert.isValidTime(raiseExcep=True)
159           
160        except Exception, e:
161            raise AttAuthorityError, "CA certificate is invalid: " + str(e)
162       
163        # Issuer details - serialise using the separator string set in the
164        # properties file
165        self.__issuer = \
166            self.__cert.dn.serialise(separator=self.__prop['dnSeparator'])
167
168        self.__issuerSerialNumber = self.__cert.serialNumber
169       
170       
171        # Load host sites custom user roles interface to enable the AA to
172        # assign roles in an attribute certificate on a getAttCert request
173        self.loadUserRolesInterface()
174       
175
176    #_________________________________________________________________________
177    def loadUserRolesInterface(self):
178        """Set-up user roles interface - load host sites custom AAUserRoles
179        derived class.  This class interfaces with the sites mechanism for
180        mapping user ID to the roles to which they are entitled.  This
181        could be via a user database"""
182
183        try:
184            try:
185                # Module file path may be None if the new module to be loaded
186                # can be found in the existing system path           
187                if self.__prop['userRolesModFilePath'] is not None:
188                    if not os.path.exists(\
189                              self.__prop['userRolesModFilePath']):
190                        raise Exception, "File path '%s' doesn't exist" % \
191                              self.__prop['userRolesModFilePath']
192                             
193                    # Temporarily extend system path ready for import
194                    sysPathBak = sys.path[:]
195                             
196                    sys.path.append(self.__prop['userRolesModFilePath'])
197               
198                # Import module name specified in properties file
199                userRolesMod = __import__(self.__prop['userRolesModName'],
200                                          globals(),
201                                          locals(),
202                                          [self.__prop['userRolesClassName']])
203   
204                userRolesClass = eval('userRolesMod.' + \
205                                     self.__prop['userRolesClassName'])
206            finally:
207                try:
208                    sys.path[:] = sysPathBak
209                except NameError:
210                    # sysPathBak may not have been defined
211                    pass
212                               
213        except Exception, e:
214            raise AttAuthorityError,'Importing User Roles module: %s' % str(e)
215
216        # Check class inherits from AAUserRoles abstract base class
217        if not issubclass(userRolesClass, AAUserRoles):
218            raise AttAuthorityError, \
219                "User Roles class %s must be derived from AAUserRoles" % \
220                self.__prop['userRolesClassName']
221
222
223        # Instantiate custom class
224        try:
225            self.__userRoles = userRolesClass(self.__prop['userRolesPropFile'])
226           
227        except Exception, e:
228            raise AttAuthorityError, \
229                "Error instantiating User Roles interface: " + str(e)
230     
231       
232    #_________________________________________________________________________
233    # Methods for Attribute Authority dictionary like behaviour       
234    def __delitem__(self, key):
235        self.__class__.__name__ + " keys cannot be removed"       
236        raise KeyError, 'Keys cannot be deleted from '+self.__class__.__name__
237
238
239    def __getitem__(self, key):
240        self.__class__.__name__ + """ behaves as data dictionary of Attribute
241        Authority properties
242        """
243        if key not in self.__prop:
244            raise KeyError, "Invalid key '%s'" % key
245       
246        return self.__prop[key]
247       
248
249    def clear(self):
250        raise KeyError, "Data cannot be cleared from "+self.__class__.__name__
251   
252    def keys(self):
253        return self.__prop.keys()
254
255    def items(self):
256        return self.__prop.items()
257
258    def values(self):
259        return self.__prop.values()
260
261    def has_key(self, key):
262        return self.__prop.has_key(key)
263
264    # 'in' operator
265    def __contains__(self, key):
266        return key in self.__prop
267
268
269    def setPropFilePath(self, val=None):
270        """Set properties file from input or based on environment variable
271        settings"""
272        if not val:
273            if 'NDGSEC_AA_PROPFILEPATH' in os.environ:
274                val = os.environ['NDGSEC_AA_PROPFILEPATH']
275               
276            elif 'NDG_DIR' in os.environ:
277                val = os.path.join(os.environ['NDG_DIR'], 
278                                   self.__class__.__confDir,
279                                   self.__class__.__propFileName)
280            else:
281                raise AttributeError, 'Unable to set default Attribute ' + \
282                    'Authority properties file path: neither ' + \
283                    '"NDGSEC_AA_PROPFILEPATH" or "NDG_DIR" environment ' + \
284                    'variables are set'
285               
286        if not isinstance(val, basestring):
287            raise AttributeError, "Input Properties file path " + \
288                                  "must be a valid string."
289     
290        self.__propFilePath = val
291       
292    # Also set up as a property
293    propFilePath = property(fset=setPropFilePath,
294                            doc="Set the path to the properties file")   
295   
296   
297    #_________________________________________________________________________
298    def getAttCert(self,
299                   proxyCert=None,
300                   proxyCertFilePath=None,
301                   userAttCert=None,
302                   userAttCertFilePath=None):
303
304        """Request a new Attribute Certificate for use in authorisation
305
306        getAttCert([proxyCert=px|proxyCertFilePath=pxFile, ]
307                   [userAttCert=cert|userAttCertFilePath=certFile])
308                   
309        @type proxyCert: string / ndg.security.common.X509.X509Cert type
310        @keyword proxyCert: base64 encoded string containing user proxy cert./
311        X.509 cert object
312       
313        @keyword proxyCertFilePath: string
314        @param proxyCertFilePath: file path to proxy certificate.
315     
316        @type userAttCert: string or AttCert type
317        @keyword userAttCert: externally provided attribute certificate from
318        another data centre.  This is only necessary if the user is not
319        registered with this attribute authority.
320                       
321        @type userAttCertFilePath: string
322        @keyword userAttCertFilePath: alternative to userAttCert except pass
323        in as a file path to an attribute certificate instead.
324       
325        @rtype: AttCert
326        @return: new attribute certificate"""
327
328
329        # Read proxy certificate
330        try:           
331            if proxyCertFilePath is not None:
332                                   
333                # Proxy Certificate input as a file
334                userProxyCert = X509Cert()
335                userProxyCert.read(proxyCertFilePath)
336               
337            elif isinstance(proxyCert, basestring):
338
339                # Proxy Certificate input as string text
340                userProxyCert = X509Cert()
341                userProxyCert.parse(proxyCert)
342               
343            elif isinstance(proxyCert, X509Cert):
344               
345                # Proxy is an NDG X509Cert type
346                userProxyCert = proxyCert
347               
348            else:
349                raise AttAuthorityError, \
350                "No input proxy certificate file path or cert text/object set"
351           
352        except Exception, e:
353            raise AttAuthorityError, "User certificate: %s" % e
354
355
356        # Check proxy certificate hasn't expired
357        try:
358            userProxyCert.isValidTime(raiseExcep=True)
359           
360        except Exception, e:
361            raise AttAuthorityError, "User Proxy Certificate is invalid: " + \
362                                    str(e)
363
364           
365        # Get Distinguished name from certificate as an X500DN type
366        userDN = userProxyCert.dn
367       
368       
369        # Make a new Attribute Certificate instance passing in certificate
370        # details for later signing
371        #
372        # Nb. new attribute certificate file path is created from the
373        # Credentials Repository
374        attCert = AttCert(filePath=self.__newAttCertFilePath())
375
376        attCert.certFilePathList = [self.__prop['certFile'],
377                                    self.__prop['caCertFile']]
378       
379        attCert.signingKeyFilePath = self.__prop['keyFile']
380        attCert.signingKeyPwd = self.__prop['keyPwd']
381       
382       
383        # Set holder's (user's) Distinguished Name
384        try:
385            attCert['holder'] = \
386                        userDN.serialise(separator=self.__prop['dnSeparator'])
387           
388        except Exception, e:
389            raise AttAuthorityError, "User DN: %s" % e
390
391       
392        # Set Issuer details from Attribute Authority
393        issuerDN = self.__cert.dn
394        try:
395            attCert['issuer'] = \
396                    issuerDN.serialise(separator=self.__prop['dnSeparator'])
397           
398        except Exception, e:
399            raise AttAuthorityError, "Issuer DN: %s" % e
400       
401        attCert['issuerName'] = self.__prop['name']
402        attCert['issuerSerialNumber'] = self.__issuerSerialNumber
403
404
405        # Set validity time
406        try:
407            attCert.setValidityTime(\
408                        lifetime=self.__prop['attCertLifetime'],
409                        notBeforeOffset=self.__prop['attCertNotBeforeOff'])
410
411            # Check against the proxy certificate's expiry
412            dtUsrProxyNotAfter = userProxyCert.notAfter
413           
414            if attCert.getValidityNotAfter(asDatetime=True) > \
415               dtUsrProxyNotAfter:
416
417                # Adjust the attribute certificate's expiry date time
418                # so that it agrees with that of the proxy certificate
419                # ... but also make ensure that the not before skew is still
420                # applied
421                attCert.setValidityTime(dtNotAfter=dtUsrProxyNotAfter,
422                        notBeforeOffset=self.__prop['attCertNotBeforeOff'])
423           
424        except Exception, e:
425            raise AttAuthorityError, "Error setting validity time: %s" % e
426       
427
428        # Check name is registered with this Attribute Authority - if no
429        # user roles are found, the user is not registered
430        userRoles = self.getRoles(str(userDN))
431        if userRoles:           
432            # Set as an Original Certificate
433            #
434            # User roles found - user is registered with this data centre
435            # Add roles for this user for this data centre
436            attCert.addRoles(userRoles)
437
438            # Mark new Attribute Certificate as an original
439            attCert['provenance'] = AttCert.origProvenance
440
441        else:           
442            # Set as a Mapped Certificate
443            #
444            # No roles found - user is not registered with this data centre
445            # Check for an externally provided certificate from another
446            # trusted data centre
447            if userAttCertFilePath:
448               
449                # Read externally provided certificate
450                try:
451                    userAttCert = AttCertRead(userAttCertFilePath)
452                   
453                except Exception, e:
454                    raise AttAuthorityError, \
455                            "Reading external Attribute Certificate: %s" % e                           
456            elif userAttCert:
457                # Allow input as a string but convert to
458                if isinstance(userAttCert, basestring):
459                    userAttCert = AttCertParse(userAttCert)
460                   
461                elif not isinstance(userAttCert, AttCert):
462                    raise AttAuthorityError, \
463                        "Expecting userAttCert as a string or AttCert type"         
464            else:
465                raise AttAuthorityAccessDenied, \
466                    "User \"%s\" is not registered " % attCert['holder'] + \
467                    "and no external attribute certificate is available " + \
468                    "to make a mapping."
469
470
471            # Check it's an original certificate - mapped certificates can't
472            # be used to make further mappings
473            if userAttCert.isMapped():
474                raise AttAuthorityError, \
475                    "External Attribute Certificate must have an " + \
476                    "original provenance in order to make further mappings."
477
478
479            # Check it's valid and signed
480            try:
481                # Give path to CA cert to allow check
482                userAttCert.certFilePathList = self.__prop['caCertFile']
483                userAttCert.isValid(raiseExcep=True)
484               
485            except Exception, e:
486                raise AttAuthorityError, \
487                            "Invalid Remote Attribute Certificate: " + str(e)       
488
489
490            # Check that's it's holder matches the user certificate DN
491            try:
492                holderDN = X500DN(dn=userAttCert['holder'])
493               
494            except Exception, e:
495                raise AttAuthorityError, \
496                                    "Error creating X500DN for holder: %s" + e
497           
498            if holderDN != userDN:
499                raise AttAuthorityError, \
500                    "User certificate and Attribute Certificate DNs " + \
501                    "don't match: %s and %s" % (userDN, holderDN)
502           
503 
504            # Get roles from external Attribute Certificate
505            trustedHostRoles = userAttCert.getRoles()
506
507
508            # Map external roles to local ones
509            localRoles = self.mapRemoteRoles2LocalRoles(\
510                                                    userAttCert['issuerName'],
511                                                    trustedHostRoles)
512            if not localRoles:
513                raise AttAuthorityAccessDenied, \
514                    "No local roles mapped to the %s roles: %s" % \
515                    (userAttCert['issuerName'], ', '.join(trustedHostRoles))
516
517            attCert.addRoles(localRoles)
518           
519           
520            # Mark new Attribute Certificate as mapped
521            attCert['provenance'] = AttCert.mappedProvenance
522
523            # End set mapped certificate block
524
525        try:
526            # Digitally sign certificate using Attribute Authority's
527            # certificate and private key
528            attCert.applyEnvelopedSignature()
529           
530            # Check the certificate is valid
531            attCert.isValid(raiseExcep=True)
532           
533            # Write out certificate to keep a record of it for auditing
534            attCert.write()
535
536            # Return the cert to caller
537            return attCert
538       
539        except Exception, e:
540            raise AttAuthorityError, "New Attribute Certificate \"%s\": %s" %\
541                                    (attCert.filePath, e)
542       
543       
544    #_________________________________________________________________________     
545    def readProperties(self):
546
547        """Read the configuration properties for the Attribute Authority.
548        Nb. if parameters for the user roles interface change
549        loadUserRolesInterface() must be called explicitly in order for the
550        changes to take effect
551
552        @type propFilePath: string
553        @keyword propFilePath: file path to properties file
554        """
555
556
557        try:
558            tree = ElementTree.parse(self.__propFilePath)
559           
560        except IOError, ioErr:
561            raise AttAuthorityError, \
562                                "Error parsing properties file \"%s\": %s" % \
563                                (ioErr.filename, ioErr.strerror)
564
565       
566        aaProp = tree.getroot()
567        if aaProp is None:
568            raise AttAuthorityError, \
569            "Parsing properties file \"%s\": root element is not defined" % \
570            self.__propFilePath
571
572
573        # Copy properties from file into a dictionary
574        self.__prop = {}
575        missingKeys = []
576        try:
577            for elem in aaProp:
578                if elem.tag in self.__class__.__validKeys:
579               
580                    if elem.tag != 'keyPwd' and elem.text: 
581                        self.__prop[elem.tag] = \
582                                        os.path.expandvars(elem.text.strip())
583                    else:
584                        self.__prop[elem.tag] = elem.text
585                else:
586                    missingKeys.append(elem.tag)
587               
588        except Exception, e:
589            raise AttAuthorityError, \
590                "Error parsing tag \"%s\" in properties file \"%s\": %s" % \
591                (elem.tag, self.__propFilePath, e)
592 
593        if missingKeys != []:
594            raise AttAuthorityError, "The following properties are " + \
595                                     "missing from the properties file: " + \
596                                     ', '.join(missingKeys)
597 
598        # Ensure Certificate time parameters are converted to numeric type
599        self.__prop['attCertLifetime'] = float(self.__prop['attCertLifetime'])
600        self.__prop['attCertNotBeforeOff'] = \
601                                    float(self.__prop['attCertNotBeforeOff'])
602
603        # Likewise port number
604        self.__prop['portNum'] = int(self.__prop['portNum'])
605       
606       
607        # Check directory path
608        try:
609            dirList = os.listdir(self.__prop['attCertDir'])
610
611        except OSError, osError:
612            raise AttAuthorityError, \
613                "Invalid directory path Attribute Certificates store: %s" % \
614                osError.strerror
615       
616       
617    #_________________________________________________________________________     
618    def readMapConfig(self, mapConfigFilePath=None):
619        """Parse Map Configuration file.
620
621        @type mapConfigFilePath: string
622        @keyword mapConfigFilePath: file path for map configuration file.  If
623        omitted, it uses member variable __prop['mapConfigFile'].
624        """
625       
626        if mapConfigFilePath is not None:
627            if not isinstance(mapConfigFilePath, basestring):
628                raise AttAuthorityError, \
629                "Input Map Configuration file path must be a valid string."
630           
631            self.__prop['mapConfigFile'] = mapConfigFilePath
632
633
634        try:
635            tree = ElementTree.parse(self.__prop['mapConfigFile'])
636            rootElem = tree.getroot()
637           
638        except IOError, e:
639            raise AttAuthorityError, \
640                            "Error parsing properties file \"%s\": %s" % \
641                            (e.filename, e.strerror)
642           
643        except Exception, e:
644            raise AttAuthorityError, \
645                "Error parsing Map Configuration file: \"%s\": %s" % \
646                (self.__prop['mapConfigFile'], e)
647
648           
649        trustedElem = rootElem.findall('trusted')
650        if not trustedElem: 
651            # Make an empty list so that for loop block below is skipped
652            # without an error 
653            trustedElem = ()
654
655        # Dictionaries:
656        # 1) to hold all the data
657        self.__mapConfig = {'thisHost': {}, 'trustedHosts': {}}
658
659        # ... look-up
660        # 2) hosts corresponding to a given role and
661        # 3) roles of external data centre to this data centre
662        self.__localRole2TrustedHost = {}
663        self.__localRole2RemoteRole = {}
664        self.__remoteRole2LocalRole = {}
665
666
667        # Information about this host
668        try:
669            thisHostElem = rootElem.findall('thisHost')[0]
670           
671        except Exception, e:
672            raise AttAuthorityError, \
673            "\"thisHost\" tag not found in Map Configuration file \"%s\"" % \
674            self.__prop['mapConfigFile']
675
676        try:
677            hostName = thisHostElem.attrib.values()[0]
678           
679        except Exception, e:
680            raise AttAuthorityError, "\"name\" attribute of \"thisHost\" " + \
681                        "tag not found in Map Configuration file \"%s\"" % \
682                        self.__prop['mapConfigFile']
683
684
685        # hostname is also stored in the AA's config file in the 'name' tag. 
686        # Check the two match as the latter is copied into Attribute
687        # Certificates issued by this AA
688        #
689        # TODO: would be better to rationalise this so that the hostname is
690        # stored in one place only.
691        #
692        # P J Kershaw 14/06/06
693        if hostName != self.__prop['name']:
694            raise AttAuthorityError, "\"name\" attribute of \"thisHost\" " + \
695                "element in Map Configuration file doesn't match " + \
696                "\"name\" element in properties file."
697       
698        self.__mapConfig['thisHost'][hostName] = \
699        {
700            'loginURI':     thisHostElem.findtext('loginURI'),
701            'aaURI':         thisHostElem.findtext('aaURI')
702        }       
703       
704       
705        # Information about trusted hosts
706        for elem in trustedElem:
707
708            roleElem = elem.findall('role')
709            if not roleElem:
710                raise AttAuthorityError("\"role\" tag not found in \"%s\"" % \
711                                        self.__prop['mapConfigFile'])
712
713            try:
714                trustedHost = elem.attrib.values()[0]
715               
716            except Exception, e:
717                raise AttAuthorityError, \
718                                    "Error reading trusted host name: %s" % e
719
720           
721            # Add signatureFile and list of roles
722            #
723            # (Currently Optional) additional tag allows query of the URI
724            # where a user would normally login at the trusted host.  Added
725            # this feature to allow users to be forwarded to their home site
726            # if they are accessing a secure resource and are not
727            # authenticated
728            #
729            # P J Kershaw 25/05/06
730            self.__mapConfig['trustedHosts'][trustedHost] = \
731            {
732                'loginURI':     elem.findtext('loginURI'),
733                'aaURI':         elem.findtext('aaURI'),
734                'role':         [dict(i.items()) for i in roleElem]
735            }
736
737                   
738            self.__localRole2RemoteRole[trustedHost] = {}
739            self.__remoteRole2LocalRole[trustedHost] = {}
740           
741            for role in self.__mapConfig['trustedHosts'][trustedHost]['role']:
742
743                localRole = role['local']
744                remoteRole = role['remote']
745               
746                # Role to host look-up
747                if localRole in self.__localRole2TrustedHost:
748                   
749                    if trustedHost not in \
750                       self.__localRole2TrustedHost[localRole]:
751                        self.__localRole2TrustedHost[localRole].\
752                                                        append(trustedHost)                       
753                else:
754                    self.__localRole2TrustedHost[localRole] = [trustedHost]
755
756
757                # Trusted Host to local role and trusted host to trusted role
758                # map look-ups
759                try:
760                    self.__remoteRole2LocalRole[trustedHost][remoteRole].\
761                                                            append(localRole)                 
762                except KeyError:
763                    self.__remoteRole2LocalRole[trustedHost][remoteRole] = \
764                                                                [localRole]
765                   
766                try:
767                    self.__localRole2RemoteRole[trustedHost][localRole].\
768                                                            append(remoteRole)                 
769                except KeyError:
770                    self.__localRole2RemoteRole[trustedHost][localRole] = \
771                                                                [remoteRole]                 
772       
773       
774    #_________________________________________________________________________     
775    def userIsRegistered(self, userDN):
776        """Check a particular user is registered with the Data Centre that the
777        Attribute Authority represents
778       
779        Nb. this method is not used internally by AttAuthority class
780       
781        @type userDN: string
782        @param userDN: user Distinguished Name
783        @rtype: bool
784        @return: True if user is registered, False otherwise"""
785        return self.__userRoles.userIsRegistered(userDN)
786       
787       
788    #_________________________________________________________________________     
789    def getRoles(self, dn):
790        """Get the roles available to the registered user identified userDN.
791
792        @type dn: string
793        @param dn: user Distinguished Name
794        @return: list of roles for the given user DN"""
795
796        # Call to AAUserRoles derived class.  Each Attribute Authority
797        # should define it's own roles class derived from AAUserRoles to
798        # define how roles are accessed
799        try:
800            return self.__userRoles.getRoles(dn)
801
802        except Exception, e:
803            raise AttAuthorityError, "Getting user roles: %s" % e
804       
805       
806    #_________________________________________________________________________     
807    def __getHostInfo(self):
808        """Return the host that this Attribute Authority represents: its ID,
809        the user login URI and WSDL address.  Call this method via the
810        'hostInfo' property
811       
812        @rtype: dict
813        @return: dictionary of host information derived from the map
814        configuration"""
815       
816        return self.__mapConfig['thisHost']
817       
818    hostInfo = property(fget=__getHostInfo, 
819                        doc="Return information about this host")
820       
821       
822    #_________________________________________________________________________     
823    def getTrustedHostInfo(self, role=None):
824        """Return a dictionary of the hosts that have trust relationships
825        with this AA.  The dictionary is indexed by the trusted host name
826        and contains AA service, login URIs and the roles that map to the
827        given input local role.
828
829        @type role: string
830        @keyword role: if set, return trusted hosts that having a mapping set
831        for this role.  If no role is input, return all the AA's trusted hosts
832        with all their possible roles
833
834        @rtype: dict
835        @return: dictionary of the hosts that have trust relationships
836        with this AA.  It returns an empty dictionary if role isn't
837        recognised"""
838                                         
839        if not self.__mapConfig or not self.__localRole2RemoteRole:
840            # This Attribute Authority has no trusted hosts
841            raise AttAuthorityNoTrustedHosts, \
842                "The %s Attribute Authority has no trusted hosts" % \
843                self.__prop['name']
844
845
846        if role is None:
847            # No role input - return all trusted hosts with their WSDL URIs
848            # and the remote roles they map to
849            #
850            # Nb. {}.fromkeys([...]).keys() is a fudge to get unique elements
851            # from a list i.e. convert the list elements to a dict eliminating
852            # duplicated elements and convert the keys back into a list.
853            trustedHostInfo = dict(\
854            [\
855                (\
856                    k, \
857                    {
858                        'aaURI':       v['aaURI'], \
859                        'loginURI':    v['loginURI'], \
860                        'role':        {}.fromkeys(\
861                            [role['remote'] for role in v['role']]\
862                        ).keys()
863                    }
864                ) for k, v in self.__mapConfig['trustedHosts'].items()
865            ])
866
867        else:           
868            # Get trusted hosts for given input local role       
869            try:
870                trustedHosts = self.__localRole2TrustedHost[role]
871            except:
872                raise AttAuthorityNoMatchingRoleInTrustedHosts, \
873                    'None of the trusted hosts have a mapping to the ' + \
874                    'input role "%s"' % role
875   
876   
877            # Get associated WSDL URI and roles for the trusted hosts
878            # identified and return as a dictionary indexed by host name
879            trustedHostInfo = dict(\
880   [(\
881        host, \
882        {
883            'aaURI':     self.__mapConfig['trustedHosts'][host]['aaURI'],
884            'loginURI': self.__mapConfig['trustedHosts'][host]['loginURI'],
885            'role':     self.__localRole2RemoteRole[host][role]
886        }\
887    ) for host in trustedHosts])
888                         
889        return trustedHostInfo
890       
891       
892    #_________________________________________________________________________     
893    def mapRemoteRoles2LocalRoles(self, trustedHost, trustedHostRoles):
894        """Map roles of trusted hosts to roles for this data centre
895
896        @type trustedHost: string
897        @param trustedHost: name of external trusted data centre
898        @type trustedHostRoles: list
899        @param trustedHostRoles:   list of external roles to map
900        @return: list of mapped roles"""
901
902        if not self.__remoteRole2LocalRole:
903            raise AttAuthorityError, "Roles map is not set - ensure " + \
904                                     "readMapConfig() has been called."
905
906
907        # Check the host name is a trusted one recorded in the map
908        # configuration
909        if not self.__remoteRole2LocalRole.has_key(trustedHost):
910            return []
911
912        # Add local roles, skipping if no mapping is found
913        localRoles = []
914        for trustedRole in trustedHostRoles:
915            if trustedRole in self.__remoteRole2LocalRole[trustedHost]:
916                localRoles.extend(\
917                        self.__remoteRole2LocalRole[trustedHost][trustedRole])
918               
919        return localRoles
920       
921       
922    #_________________________________________________________________________     
923    def __newAttCertFilePath(self):
924        """Create a new unique attribute certificate file path
925       
926        @return: string file path"""
927       
928        attCertFd, attCertFilePath = \
929                   tempfile.mkstemp(suffix=self.__prop['attCertFileSfx'],
930                                    prefix=self.__prop['attCertFilePfx'],
931                                    dir=self.__prop['attCertDir'],
932                                    text=True)
933
934        # The file is opened - close using the file descriptor returned in the
935        # first element of the tuple
936        os.close(attCertFd)
937
938        # The file path is the 2nd element
939        return attCertFilePath
940
941
942#_____________________________________________________________________________
943class AAUserRolesError(Exception):
944    """Exception handling for NDG Attribute Authority User Roles interface
945    class."""
946
947
948#_____________________________________________________________________________
949class AAUserRoles:
950    """An abstract base class to define the user roles interface to an
951    Attribute Authority.
952
953    Each NDG data centre should implement a derived class which implements
954    the way user roles are provided to its representative Attribute Authority.
955   
956    Roles are expected to indexed by user Distinguished Name (DN).  They
957    could be stored in a database or file."""
958
959    # User defined class may wish to specify a URI for a database interface or
960    # path for a user roles configuration file
961    def __init__(self, dbURI=None, filePath=None):
962        """User Roles abstract base class - derive from this class to define
963        roles interface to Attribute Authority
964       
965        @type dbURI: string
966        @keyword dbURI: database connection URI
967        @type filePath: string
968        @keyword filePath: file path for properties file containing settings
969        """
970        raise NotImplementedError, \
971            self.__init__.__doc__.replace('\n       ','')
972
973
974    def userIsRegistered(self, dn):
975        """Derived method should return True if user is known otherwise
976        False
977       
978        Nb. this method is not used by AttAuthority class and so does NOT need
979        to be implemented in a derived class.
980       
981        @type dn: string
982        @param dn: user Distinguished Name to look up.
983        @rtype: bool
984        @return: True if user is registered, False otherwise"""
985        raise NotImplementedError, \
986            self.UserIsRegistered.__doc__.replace('\n       ','')
987
988
989    def getRoles(self, dn):
990        """Derived method should return the roles for the given user's
991        DN or else raise an exception
992       
993        @type dn: string
994        @param dn: user Distinguished Name
995        @rtype: list
996        @return: list of roles for the given user DN"""
997        raise NotImplementedError, \
998            self.getRoles.__doc__.replace('\n       ','')
999                         
Note: See TracBrowser for help on using the repository browser.