Changeset 6063
- Timestamp:
- 27/11/09 15:52:29 (11 years ago)
- Location:
- TI12-security/trunk/python
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
TI12-security/trunk/python/ndg_security_common/ndg/security/common/authz/msi.py
r6062 r6063 926 926 927 927 except Exception, e: 928 log.error("SAML Attribute Query : %s",928 log.error("SAML Attribute Query %s: %s", 929 929 type(e), traceback.format_exc()) 930 930 return Response(Response.DECISION_INDETERMINATE, -
TI12-security/trunk/python/ndg_security_common/ndg/security/common/credentialwallet.py
r6062 r6063 169 169 CREDENTIAL_TYPE_ATTRNAME = 'type' 170 170 171 __ slots__= (171 __ATTRIBUTE_NAMES = ( 172 172 ID_ATTRNAME, 173 173 ITEM_ATTRNAME, … … 176 176 CREDENTIAL_TYPE_ATTRNAME 177 177 ) 178 __slots__ = __ATTRIBUTE_NAMES 178 179 __slots__ += tuple(["_CredentialContainer__%s" % n for n in __slots__]) 179 180 … … 217 218 218 219 def _setCredential(self, value): 219 if self.type is not None and not isinstance(value, self.type): 220 # Safeguard type attribute referencing for unpickling process - this 221 # method may be called before type attribute has been set 222 _type = getattr(self, 223 CredentialContainer.CREDENTIAL_TYPE_ATTRNAME, 224 None) 225 226 if _type is not None and not isinstance(value, _type): 220 227 raise TypeError('Expecting %r type for "credential" attribute; ' 221 228 'got %r' % type(value)) … … 256 263 def __getstate__(self): 257 264 '''Enable pickling''' 258 return dict([(attrName, getattr(self, attrName)) 259 for attrName in self.__class__.__slots__]) 265 thisDict = dict([(attrName, getattr(self, attrName)) 266 for attrName in CredentialContainer.__ATTRIBUTE_NAMES]) 267 268 return thisDict 260 269 261 270 def __setstate__(self, attrDict): 262 271 '''Enable pickling for use with beaker.session''' 263 for attr, val in attrDict.items(): 264 setattr(self, attr, val) 272 try: 273 for attr, val in attrDict.items(): 274 setattr(self, attr, val) 275 except Exception, e: 276 pass 265 277 266 278 … … 269 281 """ 270 282 CONFIG_FILE_OPTNAMES = ("userId", ) 271 272 __slots__ = ( 273 "credentials", 274 "credentialsKeyedByURI", 275 ) 276 __slots__ += CONFIG_FILE_OPTNAMES 277 __slots__ += tuple(["_CredentialWalletBase__%s" % name 278 for name in __slots__]) 279 del name 283 __slots__ = ("__credentials", "__credentialsKeyedByURI", "__userId") 284 285 # Helper for __getstate__ 286 __PROPERTIES = ("credentials", "credentialsKeyedByURI", "userId") 287 288 # __slots__ = READ_ONLY_ATTRIBUTES 289 # __slots__ += CONFIG_FILE_OPTNAMES 290 # __PRIVATE_ATTR_PREFIX = "_CredentialWalletBase__" 291 # __slots__ += tuple([__PRIVATE_ATTR_PREFIX + name for name in __slots__]) 292 # del name 280 293 281 294 def __init__(self): … … 386 399 def __getstate__(self): 387 400 '''Enable pickling for use with beaker.session''' 388 return dict([(attrName, getattr(self, attrName)) 389 for attrName in self.__class__.__slots__]) 390 401 _dict = {} 402 for attrName in CredentialWalletBase.__slots__: 403 # Ugly hack to allow for derived classes setting private member 404 # variables 405 if attrName.startswith('__'): 406 attrName = "_CredentialWalletBase" + attrName 407 408 _dict[attrName] = getattr(self, attrName) 409 410 return _dict 411 391 412 def __setstate__(self, attrDict): 392 413 '''Enable pickling for use with beaker.session''' 393 for attr , val in attrDict.items():394 setattr(self, attr , val)414 for attrName, val in attrDict.items(): 415 setattr(self, attrName, val) 395 416 396 417 … … 558 579 559 580 return True 560 581 582 def __getstate__(self): 583 '''Enable pickling for use with beaker.session''' 584 _dict = super(SAMLCredentialWallet, self).__getstate__() 585 586 for attrName in SAMLCredentialWallet.__slots__: 587 # Ugly hack to allow for derived classes setting private member 588 # variables 589 if attrName.startswith('__'): 590 attrName = "_SAMLCredentialWallet" + attrName 591 592 _dict[attrName] = getattr(self, attrName) 593 594 return _dict 595 561 596 562 597 class NDGCredentialWallet(CredentialWalletBase): … … 670 705 __metaclass__ = _MetaCredentialWallet 671 706 707 # Names that may be set in a properties file 672 708 propertyDefaults = dict( 673 709 userX509Cert=None, … … 683 719 mapFromTrustedHosts=False, 684 720 rtnExtAttCertList=True, 685 attCertRefreshElapse=7200, 721 attCertRefreshElapse=7200 722 ) 723 724 __slots__ = dict( 725 _cfg=None, 726 _dn=None, 727 _userPriKeyPwd=None, 728 _attributeAuthorityClnt=None, 729 _attributeAuthorityURI=None, 730 sslCACertFilePathList=[], 686 731 wssCfgFilePath=None, 687 732 wssCfgSection='DEFAULT', 688 733 wssCfgPrefix='', 689 wssCfgKw={}) 690 691 _protectedAttrs = [ 692 '_userX509Cert', 693 '_userX509CertFilePath', 694 '_userPriKey', 695 '_userPriKeyFilePath', 696 '_userPriKeyPwd', 697 '_issuingX509Cert', 698 '_issuingX509CertFilePath', 699 '_attributeAuthorityClnt', 700 '_attributeAuthority', 701 '_attributeAuthorityURI', 702 '_caCertFilePathList', 703 '_mapFromTrustedHosts', 704 '_rtnExtAttCertList', 705 '_attCertRefreshElapse', 706 '_cfg', 707 '_dn' 708 ] 709 710 __slots__ = propertyDefaults.keys() + _protectedAttrs 711 734 wssCfgKw={} 735 ) 736 __slots__.update(dict([("_" + n, v) for n, v in propertyDefaults.items()])) 737 del n 712 738 def __init__(self, 713 739 cfg=None, … … 726 752 from 727 753 @type cfgPrefix: basestring 728 @param cfgPrefix: apply a prefix to all NDGCredentialWallet config params729 so that if placed in a file with other parameters they can be754 @param cfgPrefix: apply a prefix to all NDGCredentialWallet config 755 params so that if placed in a file with other parameters they can be 730 756 distinguished 731 757 @type cfgKw: dict … … 736 762 super(NDGCredentialWallet, self).__init__() 737 763 738 # Initialise attributes - 1st protected ones 739 attr = {}.fromkeys(NDGCredentialWallet._protectedAttrs) 740 741 # ... then properties 742 attr.update(NDGCredentialWallet.propertyDefaults) 743 for k, v in attr.items(): 764 # Initialise attributes 765 for k, v in NDGCredentialWallet.__slots__.items(): 744 766 setattr(self, k, v) 745 767 … … 760 782 761 783 # Make a connection to the Credentials Repository 762 if self. credentialRepository is None:784 if self._credentialRepository is None: 763 785 log.info('Applying default CredentialRepository %r for user ' 764 786 '"%s"' % (NullCredentialRepository, self.userId)) 765 self. credentialRepository = NullCredentialRepository()787 self._credentialRepository = NullCredentialRepository() 766 788 else: 767 789 log.info('Checking CredentialRepository for credentials for user ' 768 790 '"%s"' % self.userId) 769 791 770 if not issubclass(self. credentialRepository, CredentialRepository):792 if not issubclass(self._credentialRepository, CredentialRepository): 771 793 raise CredentialWalletError("Input Credential Repository " 772 794 "instance must be of a class " … … 777 799 # Check for valid attribute certificates for the user 778 800 try: 779 self. credentialRepository.auditCredentials(self.userId)780 userCred=self. credentialRepository.getCredentials(self.userId)801 self._credentialRepository.auditCredentials(self.userId) 802 userCred=self._credentialRepository.getCredentials(self.userId) 781 803 782 804 except Exception, e: … … 809 831 self.audit() 810 832 833 def __getstate__(self): 834 '''Enable pickling for use with beaker.session''' 835 _dict = super(NDGCredentialWallet, self).__getstate__() 836 837 for attrName in SAMLCredentialWallet.__slots__: 838 # Ugly hack to allow for derived classes setting private member 839 # variables 840 if attrName.startswith('__'): 841 attrName = "_NDGCredentialWallet" + attrName 842 843 _dict[attrName] = getattr(self, attrName) 844 845 return _dict 846 811 847 def parseConfig(self, cfg, prefix='', section='DEFAULT'): 812 848 '''Extract parameters from cfg config object''' … … 1258 1294 # authorisation credentials. This allows credentials for previous 1259 1295 # sessions to be re-instated 1260 if self. credentialRepository and bUpdateCredentialRepository:1296 if self._credentialRepository and bUpdateCredentialRepository: 1261 1297 self.updateCredentialRepository() 1262 1298 … … 1292 1328 log.debug("NDGCredentialWallet.updateCredentialRepository ...") 1293 1329 1294 if not self. credentialRepository:1330 if not self._credentialRepository: 1295 1331 raise CredentialWalletError("No Credential Repository has been " 1296 1332 "created for this wallet") … … 1304 1340 if i.id == -1] 1305 1341 1306 self. credentialRepository.addCredentials(self.userId, attCertList)1342 self._credentialRepository.addCredentials(self.userId, attCertList) 1307 1343 1308 1344 def _createAttributeAuthorityClnt(self, attributeAuthorityURI): -
TI12-security/trunk/python/ndg_security_common/ndg/security/common/soap/client.py
r5741 r6063 216 216 if response.code != httplib.OK: 217 217 output = response.read() 218 excep = HTTPException("Response is: %d %s" % (response.code, 219 response.msg)) 218 excep = HTTPException("Response for request to [%s] is: %d %s" % 219 (soapRequest.url, 220 response.code, 221 response.msg)) 220 222 excep.urllib2Response = response 221 223 raise excep 222 224 223 if response.headers.typeheader not in \224 UrlLib2SOAPClient.RESPONSE_CONTENT_TYPES:225 if (response.headers.typeheader not in 226 UrlLib2SOAPClient.RESPONSE_CONTENT_TYPES): 225 227 responseType = ', '.join(UrlLib2SOAPClient.RESPONSE_CONTENT_TYPES) 226 228 excep = SOAPResponseError("Expecting %r response type; got %r for " -
TI12-security/trunk/python/ndg_security_test/ndg/security/test/config/attributeauthority/sitea/siteAUserRoles.py
r6062 r6063 81 81 SAML_ASSERTION_LIFETIME = 8*60*60 82 82 83 VALID_USER_IDS = ("https://openid.localhost/philip.kershaw",) 83 VALID_USER_IDS = ("https://openid.localhost/philip.kershaw", 84 "https://localhost:7443/openid/PhilipKershaw") 84 85 VALID_REQUESTOR_IDS = ( 85 86 str(X500DN.fromString("/O=Site A/CN=Authorisation Service")), -
TI12-security/trunk/python/ndg_security_test/ndg/security/test/integration/authz_lite/policy.xml
r5447 r6063 1 1 <?xml version="1.0" encoding="UTF-8"?> 2 <Policy PolicyId=" pyDAP" xmlns="urn:ndg:security:authz:1.0:policy">2 <Policy PolicyId="AuthZ Lite - Authorisation Integration Tests" xmlns="urn:ndg:security:authz:1.1:policy"> 3 3 <Description>Restrict access for Authorization integration tests</Description> 4 4 … … 6 6 <URIPattern>^/test_securedURI*$</URIPattern> 7 7 <Attributes> 8 <Attribute>urn:siteA:security:authz:1.0:attr:staff</Attribute> 8 <Attribute> 9 <Name>urn:siteA:security:authz:1.0:attr:staff</Name> 10 <AttributeAuthorityURI>https://localhost:7443/AttributeAuthority/saml</AttributeAuthorityURI> 11 </Attribute> 9 12 </Attributes> 10 <AttributeAuthority>11 <uri>http://localhost:7443/AttributeAuthority</uri>12 </AttributeAuthority>13 13 </Target> 14 14 <Target> 15 15 <URIPattern>^/test_accessDeniedToSecuredURI$</URIPattern> 16 16 <Attributes> 17 <Attribute>urn:siteA:security:authz:1.0:attr:forbidden</Attribute> 18 <Attribute>urn:siteA:security:authz:1.0:attr:keepout</Attribute> 17 <Attribute> 18 <Name>urn:siteA:security:authz:1.0:attr:forbidden</Name> 19 <AttributeAuthorityURI>https://localhost:7443/AttributeAuthority/saml</AttributeAuthorityURI> 20 </Attribute> 21 <Attribute> 22 <Name>urn:siteA:security:authz:1.0:attr:keepout</Name> 23 <AttributeAuthorityURI>https://localhost:7443/AttributeAuthority/saml</AttributeAuthorityURI> 24 </Attribute> 19 25 </Attributes> 20 <AttributeAuthority>21 <uri>http://localhost:7443/AttributeAuthority</uri>22 </AttributeAuthority>23 26 </Target> 24 27 </Policy> -
TI12-security/trunk/python/ndg_security_test/ndg/security/test/integration/authz_lite/securedapp.ini
r6059 r6063 55 55 56 56 # Set redirect for OpenID Relying Party in the Security Services app instance 57 authN.redirectURI = http ://localhost:7443/verify57 authN.redirectURI = https://localhost:7443/verify 58 58 # Test with an SSL endpoint 59 59 #authN.redirectURI = https://localhost/verify … … 76 76 77 77 [filter:AuthorizationFilter] 78 paste.filter_app_factory=ndg.security.server.wsgi.authz: AuthorizationMiddleware.filter_app_factory78 paste.filter_app_factory=ndg.security.server.wsgi.authz:SAMLAuthorizationMiddleware.filter_app_factory 79 79 prefix = authz. 80 80 policy.filePath = %(here)s/policy.xml … … 83 83 # retrieve subject attributes from the Attribute Authority associated with the 84 84 # resource to be accessed 85 pip.sslCACertFilePathList=86 85 87 # List of CA certificates used to verify the signatures of 88 # Attribute Certificates retrieved 89 pip.caCertFilePathList=%(testConfigDir)s/ca/ndg-test-ca.crt 90 91 # 92 # WS-Security Settings for call to Attribute Authority to retrieve user 93 # attributes 94 95 # Signature of an outbound message 96 97 # Certificate associated with private key used to sign a message. The sign 98 # method will add this to the BinarySecurityToken element of the WSSE header. 99 # binSecTokValType attribute must be set to 'X509' or 'X509v3' ValueType. 100 # As an alternative, use signingCertChain - see below... 101 102 # PEM encode cert 103 pip.wssecurity.signingCertFilePath=%(testConfigDir)s/pki/wsse-server.crt 104 105 # PEM encoded private key file 106 pip.wssecurity.signingPriKeyFilePath=%(testConfigDir)s/pki/wsse-server.key 107 108 # Password protecting private key. Leave blank if there is no password. 109 pip.wssecurity.signingPriKeyPwd= 110 111 # For signature verification. Provide a space separated list of file paths 112 pip.wssecurity.caCertFilePathList=%(testConfigDir)s/ca/ndg-test-ca.crt 113 114 # ValueType for the BinarySecurityToken added to the WSSE header 115 pip.wssecurity.reqBinSecTokValType=X509v3 116 117 # Add a timestamp element to an outbound message 118 pip.wssecurity.addTimestamp=True 86 # If omitted, DN of SSL Cert is used 87 pip.attributeQuery.issuerName = 88 pip.attributeQuery.clockSkew = 0. 89 pip.attributeQuery.queryAttributes.0 = urn:siteA:security:authz:1.0:attr, , http://www.w3.org/2001/XMLSchema#string 90 pip.attributeQuery.sslCACertDir=%(testConfigDir)s/ca 91 pip.attributeQuery.sslCertFilePath=%(testConfigDir)s/pki/test.crt 92 pip.attributeQuery.sslPriKeyFilePath=%(testConfigDir)s/pki/test.key 119 93 120 94 # Logging configuration -
TI12-security/trunk/python/ndg_security_test/ndg/security/test/integration/authz_lite/securityservices.ini
r5984 r6063 17 17 portNum = 7443 18 18 hostname = localhost 19 scheme = http 19 scheme = https 20 20 baseURI = %(scheme)s://%(hostname)s:%(portNum)s 21 21 openIDProviderIDBase = /openid … … 311 311 # Settings for custom AttributeInterface derived class to get user roles for given 312 312 # user ID 313 #attributeAuthority.attributeInterface.modFilePath: %(testConfigDir)s/attributeauthority/sitea314 attributeAuthority.attributeInterface.modName: ndg.security.test.integration.authz_lite.attributeinterface313 attributeAuthority.attributeInterface.modFilePath: %(testConfigDir)s/attributeauthority/sitea 314 attributeAuthority.attributeInterface.modName: siteAUserRoles 315 315 attributeAuthority.attributeInterface.className: TestUserRoles 316 316 -
TI12-security/trunk/python/ndg_security_test/ndg/security/test/integration/authz_lite/securityservicesapp.py
r5779 r6063 12 12 import os 13 13 from os.path import dirname, abspath, join 14 15 from OpenSSL import SSL 14 16 17 from ndg.security.test.unit import BaseTestCase 15 18 from ndg.security.test.unit.wsgi import PasteDeployAppServer 19 20 INI_FILEPATH = 'securityservices.ini' 16 21 17 22 # To start run … … 25 30 port = 7443 26 31 27 cfgFileName='securityservices.ini' 28 cfgFilePath = os.path.join(dirname(abspath(__file__)), cfgFileName) 29 server = PasteDeployAppServer(cfgFilePath=cfgFilePath, port=port) 32 cfgFileName = INI_FILEPATH 33 cfgFilePath = os.path.join(dirname(abspath(__file__)), cfgFileName) 34 35 certFilePath = os.path.join(BaseTestCase.NDGSEC_TEST_CONFIG_DIR, 36 'pki', 37 'localhost.crt') 38 priKeyFilePath = os.path.join(BaseTestCase.NDGSEC_TEST_CONFIG_DIR, 39 'pki', 40 'localhost.key') 41 42 ssl_context = SSL.Context(SSL.SSLv23_METHOD) 43 ssl_context.set_options(SSL.OP_NO_SSLv2) 44 45 ssl_context.use_privatekey_file(priKeyFilePath) 46 ssl_context.use_certificate_file(certFilePath) 47 48 server = PasteDeployAppServer(cfgFilePath=cfgFilePath, 49 port=port, 50 ssl_context=ssl_context) 30 51 server.start() -
TI12-security/trunk/python/ndg_security_test/ndg/security/test/unit/credentialwallet/test_credentialwallet.py
r6062 r6063 17 17 from string import Template 18 18 from cStringIO import StringIO 19 import cPickle as pickle 20 19 21 from elementtree import ElementTree 20 22 … … 46 48 ndg.security.common.credentialwallet.NDGCredentialWallet class. 47 49 """ 50 THIS_DIR = os.path.dirname(__file__) 51 PICKLE_FILENAME = 'NDGCredentialWalletPickle.dat' 52 PICKLE_FILEPATH = os.path.join(THIS_DIR, PICKLE_FILENAME) 53 48 54 def __init__(self, *arg, **kw): 49 55 super(NDGCredentialWalletTestCase, self).__init__(*arg, **kw) … … 108 114 109 115 credWallet.attributeAuthority = None 110 credWallet. credentialRepository = None116 credWallet._credentialRepository = None 111 117 credWallet.mapFromTrustedHosts = False 112 118 credWallet.rtnExtAttCertList = True … … 159 165 attCert = credWallet.getAttCert() 160 166 except CredentialWalletAttributeRequestDenied, e: 161 print(" SUCCESS- obtained expected result: %s" % e)167 print("ok - obtained expected result: %s" % e) 162 168 return 163 169 … … 196 202 print("Attribute Certificate:\n%s" % attCert) 197 203 204 def test08Pickle(self): 205 credWallet = NDGCredentialWallet(cfg=self.cfg.get('setUp', 206 'cfgFilePath')) 207 208 outFile = open(NDGCredentialWalletTestCase.PICKLE_FILEPATH, 'w') 209 pickle.dump(credWallet, outFile) 210 outFile.close() 211 212 inFile = open(NDGCredentialWalletTestCase.PICKLE_FILEPATH) 213 unpickledCredWallet = pickle.load(inFile) 214 self.assert_(unpickledCredWallet.userId == credWallet.userId) 215 198 216 199 217 class SAMLCredentialWalletTestCase(BaseTestCase): … … 201 219 CONFIG_FILENAME = 'test_samlcredentialwallet.cfg' 202 220 CONFIG_FILEPATH = os.path.join(THIS_DIR, CONFIG_FILENAME) 221 PICKLE_FILENAME = 'SAMLCredentialWalletPickle.dat' 222 PICKLE_FILEPATH = os.path.join(THIS_DIR, PICKLE_FILENAME) 203 223 204 224 ASSERTION_STR = ( … … 321 341 wallet.addCredential(self._createAssertion(issuerName="MySite")) 322 342 self.assert_(len(wallet.credentials) == 2) 343 344 def test06Pickle(self): 345 wallet = self._addCredential() 346 outFile = open(SAMLCredentialWalletTestCase.PICKLE_FILEPATH, 'w') 347 pickle.dump(wallet, outFile) 348 outFile.close() 349 350 inFile = open(SAMLCredentialWalletTestCase.PICKLE_FILEPATH) 351 unpickledWallet = pickle.load(inFile) 352 self.assert_(unpickledWallet.credentialsKeyedByURI.get( 353 SAMLCredentialWalletTestCase.SITEA_ATTRIBUTEAUTHORITY_SAML_URI)) 323 354 324 355 -
TI12-security/trunk/python/ndg_security_test/setup.py
r6009 r6063 29 29 url = 'http://proj.badc.rl.ac.uk/ndg/wiki/Security', 30 30 license = 'BSD - See LICENCE file for details', 31 install_requires = 'pyOpenSSL', # Required for paster to run under SSL 31 32 packages = find_packages(), 32 33 namespace_packages = ['ndg', 'ndg.security'],
Note: See TracChangeset
for help on using the changeset viewer.