source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/SessionMgr/SessionMgrClientTest.py @ 1998

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

python/ndg.security.test/ndg/security/test/SessionMgr/SessionMgrClientTest.py and
python/ndg.security.test/ndg/security/test/SessionMgr/sessionMgrClientTest.cfg: added a config file to enable
easy modification of parameters such as Session Manager URI and account details.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2
3"""Test harness for NDG Session Manager client - makes requests for
4authentication and authorisation.  An Attribute Authority and Simple CA
5services must be running for the reqAuthorisation and addUser tests
6
7NERC Data Grid Project
8
9P J Kershaw 23/02/06
10
11Renamed from SessionClientTest.py 27/0/4/06
12Moved and renamed SessionMgrClientTest.py 23/11/06
13
14Copyright (C) 2006 CCLRC & NERC
15
16This software may be distributed under the terms of the Q Public License,
17version 1.0 or later.
18"""
19import unittest
20import os, sys, getpass
21 
22from ndg.security.common.SessionMgr import SessionMgrClient
23from ndg.security.common.SessionCookie import SessionCookie
24
25
26class SessionMgrClientTestCase(unittest.TestCase):
27   
28    def setUp(self):
29       
30        configParser = SafeConfigParser()
31        configParser.read("./sessionMgrClientTest.cfg")
32       
33        self.cfg = {}
34        for section in configParser.sections():
35            self.cfg[section] = dict(configParser.items(section))
36
37        tracefile = sys.stderr
38       
39        # Initialise the Session Manager client connection
40        # Omit traceFile keyword to leave out SOAP debug info
41        self.clnt = SessionMgrClient(uri=self.cfg['setup']['smuri'],
42                smCertFilePath=self.cfg['setup']['smcertfilepath'],
43                clntCertFilePath=self.cfg['setup']['clntcertfilepath'],
44                clntPriKeyFilePath=self.cfg['setup']['clntprikeyfilepath'],
45                clntPriKeyPwd=clntPriKeyPwd,
46                tracefile=tracefile) 
47       
48        self.sessCookie = None
49        self.proxyCert = None
50
51
52    def testAddUser(self):
53        """Add a new user ID to the MyProxy repository"""
54       
55        passphrase = self.cfg['testAddUser'].get('passphrase') or \
56            getpass.getpass(prompt="\ntestAddUser pass-phrase for new user: ")
57           
58        # Note the pass-phrase is read from the file tmp.  To pass
59        # explicitly as a string use the 'pPhrase' keyword instead
60        self.clnt.addUser(self.cfg['testAddUser']['username'], 
61                          pPhrase=passphrase)
62        print "Added user '%s'" % self.cfg['testAddUser']['username']
63       
64
65    def testCookieConnect(self):
66        """testCookieConnect: Connect as if acting as a browser client -
67        a cookie is returned"""
68
69        passphrase = self.cfg['testCookieConnect'].get('passphrase') or \
70        getpass.getpass(prompt="\ntestCookieConnect pass-phrase for user: ")
71
72        sSessCookie = self.clnt.connect(\
73                                    self.cfg['testCookieConnect']['username'], 
74                                    pPhrase=passphrase)
75
76        self.sessCookie = SessionCookie(sSessCookie)
77        print "User '%s' connected to Session Manager:\n%s" % \
78            (self.cfg['testCookieConnect']['username'], sSessCookie)
79           
80
81    def testProxyCertConnect(self):
82        """testProxyCertConnect: Connect as a command line client -
83        a proxyCert is returned"""
84
85        passphrase = self.cfg['testProxyCertConnect'].get('passphrase') or \
86            getpass.getpass(\
87                    prompt="\ntestProxyCertConnect pass-phrase for user: ")
88
89        self.proxyCert = self.clnt.connect(\
90                               self.cfg['testProxyCertConnect']['username'], 
91                               pPhrase=passphrase,
92                               createServerSess=True,
93                               getCookie=False)
94        print "User '%s' connected to Session Manager:\n%s" % \
95            (self.cfg['testProxyCertConnect']['username'], self.proxyCert)
96           
97
98    def testCookieDisconnect(self):
99        """testCookieDisconnect: disconnect as if acting as a browser client -
100        a cookie is returned"""
101       
102        self.testCookieConnect()
103       
104        self.clnt.disconnect(sessCookie=str(self.sessCookie))
105        print "User disconnected from Session Manager:\n%s" % self.sessCookie
106           
107
108    def testProxyCertDisconnect(self):
109        """testProxyCertDisconnect: Connect as a command line client -
110        a proxyCert is returned"""
111       
112        self.testProxyCertConnect()
113           
114        self.clnt.disconnect(proxyCert=self.proxyCert)
115        print "User disconnected from Session Manager:\n%s" % self.proxyCert
116
117
118    def testCookieReqAuthorisation(self):
119        """testCookieReqAuthorisation: make an authorisation request using
120        a cookie as authentication credential"""
121       
122        self.testCookieConnect()
123        attCert, statusCode, extAttCertList = self.clnt.reqAuthorisation(\
124            sessID=self.sessCookie.sessionID, 
125            attAuthorityURI=self.cfg['testCookieReqAuthorisation']['aauri'],
126            encrSessionMgrURI=self.sessCookie.encrSessionMgrURI)
127       
128        print "Attribute Certificate:\n%s" % attCert 
129        print "User authorisation: %s" % statusCode
130        print "External Attribute Certificate List:\n%s" % extAttCertList
131
132
133    def testCookieReqAuthorisationWithExtAttCertList(self):
134        """testCookieReqAuthorisation: make an authorisation request using
135        a cookie as authentication credential"""
136       
137        self.testCookieConnect()
138       
139        aaURI = \
140            self.cfg['testCookieReqAuthorisationWithExtAttCertList']['aauri']
141           
142        attCert, statusCode, extAttCertList = self.clnt.reqAuthorisation(\
143                        sessID=self.sessCookie.sessionID, 
144                        attAuthorityURI=aaURI,
145                        encrSessionMgrURI=self.sessCookie.encrSessionMgrURI,
146                        extAttCertList=['AC1', 'AC2', 'AC3'])
147         
148        print "Attribute Certificate:\n%s" % attCert 
149        print "User authorisation: %s" % statusCode
150        print "External Attribute Certificate List:\n%s" % extAttCertList
151
152
153    def testProxyCertReqAuthorisation(self):
154        """testProxyCertReqAuthorisation: make an authorisation request using
155        a proxy cert as authentication credential"""
156        self.testProxyCertConnect()
157       
158        # Request an attribute certificate from an Attribute Authority
159        # using the proxyCert returned from connect()
160       
161        aaURI = self.cfg['testProxyCertReqAuthorisation']['aauri']
162        attCert, statusCode, extAttCertList = self.clnt.reqAuthorisation(\
163                                                 proxyCert=self.proxyCert,
164                                                 attAuthorityURI=aaURI)
165         
166        print "Attribute Certificate:\n%s" % attCert 
167        print "User authorisation: %s" % statusCode
168        print "External Attribute Certificate List:\n%s" % extAttCertList
169
170
171    def testGetX509Cert(self):
172        "testGetX509Cert: return the Session Manager's X.509 Cert."
173        cert = self.clnt.getX509Cert()
174                                             
175        print "Session Manager X.509 Certificate:\n" + cert
176           
177           
178#_____________________________________________________________________________       
179class SessionMgrClientTestSuite(unittest.TestSuite):
180   
181    def __init__(self):
182        map = map(SessionMgrClientTestCase,
183                  (
184                    "testAddUser",
185                    "testCookieConnect",
186                    "testProxyCertConnect",
187                    "testCookieDisconnect",
188                    "testProxyCertDisconnect",
189                    "testCookieReqAuthorisation",
190                    "testProxyCertReqAuthorisation",
191                    "testGetX509Cert",
192                  ))
193        unittest.TestSuite.__init__(self, map)
194           
195                                                   
196if __name__ == "__main__":
197    unittest.main()       
Note: See TracBrowser for help on using the repository browser.