source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/AttAuthority/AttAuthorityClientTest.py @ 2350

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

ndg.security.test/ndg/security/test/AttAuthority/siteAAttAuthorityProperties.xml,
ndg.security.test/ndg/security/test/AttAuthority/siteBAttAuthorityProperties.xml,
ndg.security.test/ndg/security/test/AttAuthority/attAuthorityClientTest.cfg:
minor changes to test settings

ndg.security.test/ndg/security/test/AttAuthority/AttAuthorityClientTest.py:
remove debug statements

Tests/dewsBinaryDataGet/binaryDataGet.py: set URI for command line

ndg.security.common/ndg/security/common/wsSecurity.py: if not in PEM format, remove any whitespace for
where the encoded cert has multiple line breaks.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2"""NDG Attribute Authority client unit tests
3
4NERC Data Grid Project
5
6@author P J Kershaw 05/05/05, major update 16/01/07
7
8@copyright (C) 2007 CCLRC & NERC
9
10@license This software may be distributed under the terms of the Q Public
11License, version 1.0 or later.
12"""
13
14__revision__ = '$Id$'
15
16import unittest
17import os, sys, getpass
18from ConfigParser import SafeConfigParser
19
20from ndg.security.common.AttAuthority import AttAuthorityClient
21from ndg.security.common.AttCert import AttCertRead
22
23
24class AttAuthorityClientTestCase(unittest.TestCase):
25    userPriKeyPwd = None
26   
27    def setUp(self):
28
29        configParser = SafeConfigParser()
30        configParser.read("./attAuthorityClientTest.cfg")
31       
32        self.cfg = {}
33        for section in configParser.sections():
34            self.cfg[section] = dict(configParser.items(section))
35
36        tracefile = sys.stderr
37
38        if self.userPriKeyPwd is None:
39            try:
40                if self.cfg['setUp'].get('userprikeypwd') is None:
41                    self.userPriKeyPwd = getpass.getpass(\
42                            prompt="\nsetUp - client private key password: ")
43                else:
44                    self.userPriKeyPwd=self.cfg['setUp'].get('clntprikeypwd')
45            except KeyboardInterrupt:
46                sys.exit(0)
47
48        # Instantiate WS proxy
49        self.clnt = AttAuthorityClient(uri=self.cfg['setUp']['uri'],
50               signingCertFilePath=self.cfg['setUp'].get('usercertfilepath'),
51               signingPriKeyFilePath=self.cfg['setUp'].get('userprikeyfilepath'),
52               signingPriKeyPwd=self.userPriKeyPwd,
53               tracefile=sys.stderr)
54           
55   
56    def test1GetX509Cert(self):
57        '''test1GetX509Cert: retrieve Attribute Authority's X.509 cert.'''
58        resp = self.clnt.getX509Cert()
59        print "Attribute Authority X.509 cert.:\n" + resp
60
61    def test2GetHostInfo(self):
62        """test2GetHostInfo: retrieve info for AA host"""
63        hostInfo = self.clnt.getHostInfo()
64        print "Host Info:\n %s" % hostInfo
65       
66
67    def test3GetTrustedHostInfo(self):
68        """test3GetTrustedHostInfo: retrieve trusted host info matching a
69        given role"""
70        trustedHostInfo = self.clnt.getTrustedHostInfo(\
71                                 self.cfg['test3GetTrustedHostInfo']['role'])
72        print "Trusted Host Info:\n %s" % trustedHostInfo
73
74
75    def test4GetTrustedHostInfoWithNoRole(self):
76        """test4GetTrustedHostInfoWithNoRole: retrieve trusted host info
77        irrespective of role"""
78        trustedHostInfo = self.clnt.getTrustedHostInfo()
79        print "Trusted Host Info:\n %s" % trustedHostInfo
80
81
82    def test5GetAttCert(self):       
83        """test5GetAttCert: Request attribute certificate from NDG Attribute
84        Authority Web Service."""
85   
86        # Read user Certificate into a string ready for passing via WS
87        try:
88            userCertFilePath = \
89                self.cfg['test5GetAttCert'].get('issuingusercertfilepath')
90            userCertTxt = open(userCertFilePath, 'r').read()
91       
92        except TypeError:
93            # No issuing cert set
94            userCertTxt = None
95               
96        except IOError, ioErr:
97            raise "Error reading certificate file \"%s\": %s" % \
98                                    (ioErr.filename, ioErr.strerror)
99
100        # Make attribute certificate request
101        attCert = self.clnt.getAttCert(userCert=userCertTxt)
102       
103        print "Attribute Certificate: \n\n:" + str(attCert)
104       
105        attCert.filePath = self.cfg['test5GetAttCert']['attcertfilepath']
106        attCert.write()
107       
108    def test6GetAttCertWithUserIdSet(self):       
109        """test6GetAttCertWithUserIdSet: Request attribute certificate from
110        NDG Attribute Authority Web Service setting a specific user Id
111        independent of the signer of the SOAP request."""
112   
113        # Read user Certificate into a string ready for passing via WS
114        try:
115            userCertFilePath = \
116    self.cfg['test6GetAttCertWithUserIdSet'].get('issuingusercertfilepath')
117            userCertTxt = open(userCertFilePath, 'r').read()
118       
119        except TypeError:
120            # No issuing cert set
121            userCertTxt = None
122               
123        except IOError, ioErr:
124            raise "Error reading certificate file \"%s\": %s" % \
125                                    (ioErr.filename, ioErr.strerror)
126
127        # Make attribute certificate request
128        userId = self.cfg['test6GetAttCertWithUserIdSet']['userid']
129        attCert = self.clnt.getAttCert(userId=userId,
130                                       userCert=userCertTxt)
131       
132        print "Attribute Certificate: \n\n:" + str(attCert)
133       
134        attCert.filePath = self.cfg['test5GetAttCert']['attcertfilepath']
135        attCert.write()
136
137    def test7GetMappedAttCert(self):       
138        """test7GetMappedAttCert: Request mapped attribute certificate from
139        NDG Attribute Authority Web Service."""
140   
141        # Read user Certificate into a string ready for passing via WS
142        try:
143            userCertFilePath = \
144            self.cfg['test7GetMappedAttCert'].get('issuingusercertfilepath')
145            userCertTxt = open(userCertFilePath, 'r').read()
146       
147        except TypeError:
148            # No issuing cert set
149            userCertTxt = None
150               
151        except IOError, ioErr:
152            raise "Error reading certificate file \"%s\": %s" % \
153                                    (ioErr.filename, ioErr.strerror)
154   
155   
156        # Simlarly for Attribute Certificate
157        try:
158            userAttCert = AttCertRead(\
159                self.cfg['test7GetMappedAttCert']['userattcertfilepath'])
160           
161        except IOError, ioErr:
162            raise "Error reading attribute certificate file \"%s\": %s" %\
163                                    (ioErr.filename, ioErr.strerror)
164
165        try:
166            if self.cfg['test7GetMappedAttCert'].get('userprikeypwd') is None:
167                userPriKeyPwd = getpass.getpass(\
168                            prompt="\nsetUp - client private key password: ")
169            else:
170                userPriKeyPwd = \
171                        self.cfg['test7GetMappedAttCert'].get('userprikeypwd')
172        except KeyboardInterrupt:
173            sys.exit(0)
174       
175        # Make client to site B Attribute Authority
176        clnt = AttAuthorityClient(
177uri=self.cfg['test7GetMappedAttCert']['uri'], 
178signingCertFilePath=self.cfg['test7GetMappedAttCert'].get('usercertfilepath'),
179signingPriKeyFilePath=self.cfg['test7GetMappedAttCert'].get('userprikeyfilepath'),
180signingPriKeyPwd=userPriKeyPwd,
181tracefile=sys.stderr)
182   
183        # Make attribute certificate request
184        attCert = clnt.getAttCert(userCert=userCertTxt,
185                                  userAttCert=userAttCert)
186        print "Attribute Certificate: \n\n:" + str(attCert)
187 
188 
189#_____________________________________________________________________________       
190class AttAuthorityClientTestSuite(unittest.TestSuite):
191    def __init__(self):
192        map = map(AttAuthorityClientTestCase,
193                  (
194                    "test1GetX509Cert",
195                    "test2GetHostInfo",
196                    "test3GetTrustedHostInfo",
197                    "test4GetTrustedHostInfoWithNoRole",
198                    "test5GetAttCert",
199                    "test6GetAttCertWithUserIdSet",
200                    "test7GetMappedAttCert",
201                  ))
202        unittest.TestSuite.__init__(self, map)
203                                       
204if __name__ == "__main__":
205    unittest.main()
Note: See TracBrowser for help on using the repository browser.