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

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

test/SessionMgrClientTest.py: copied from old Tests/SecurityClient?.py into new unit tests package
structure
common/SessionMgr/init.py: delete to make way for old SecurityClient?.py code to replace it.

  • 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
21
22from Cookie import SimpleCookie
23
24from ndg.security.common.SessionMgrClient import *
25
26
27class SecurityClientTestCase(unittest.TestCase):
28   
29    def setUp(self):
30        self.config = {}
31
32        # Gabriel settings
33        gabrielConfig = {}
34        gabrielConfig['smWSDL'] = 'http://gabriel.bnsc.rl.ac.uk/sessionMgr.wsdl'
35        gabrielConfig['aaWSDL'] = 'http://gabriel.bnsc.rl.ac.uk/attAuthority.wsdl'
36
37       
38        gabrielConfig['newUserName'] = 'BugsBunny'           
39        gabrielConfig['userName'] = 'gabriel'
40        gabrielConfig['trustedHostRequiredRole'] = 'academic'
41
42        gabrielConfig['aaPubKeyFilePath'] = None
43
44        # Public key of session manager used to encrypt requests
45        # If no public key is set, it will be retrieved using the
46        # getPubKey WS method
47        gabrielConfig['smPubKeyFilePath'] = None
48
49       
50        # Glue settings
51        glueConfig = {}
52        glueConfig['smWSDL'] = 'http://glue.badc.rl.ac.uk/sessionMgr.wsdl'
53        glueConfig['aaWSDL'] = 'http://glue.badc.rl.ac.uk/attAuthority.wsdl'
54
55       
56        glueConfig['newUserName'] = 'YosemiteSam'           
57        glueConfig['userName'] = 'lawrence'
58        glueConfig['trustedHostRequiredRole'] = 'acsoe'
59        #glueConfig['trustedHostRequiredRole'] = 'coapec'
60
61        glueConfig['aaPubKeyFilePath'] = None
62
63        # Public key of session manager used to encrypt requests
64        # If no public key is set, it will be retrieved using the
65        # getPubKey WS method
66        glueConfig['smPubKeyFilePath'] = None
67
68
69        # Uncomment for required test
70        self.config = gabrielConfig
71        #self.config = glueConfig
72
73       
74        self.__clntPriKeyPwd = open("./tmp2").read().strip()
75
76        clntPubKeyFilePath = "./Junk-cert.pem"
77        clntPriKeyFilePath = "./Junk-key.pem"
78        traceFile = None#sys.stderr
79       
80        # Initialise the Session Manager client connection
81        # Omit traceFile keyword to leave out SOAP debug info
82        self.sessClnt = SessionMgrClient(smURI=self.config['smWSDL'],
83                        smPubKeyFilePath=self.config['smPubKeyFilePath'],
84                        clntPubKeyFilePath=clntPubKeyFilePath,
85                        clntPriKeyFilePath=clntPriKeyFilePath,
86                        traceFile=traceFile) 
87       
88        self.sessCookie = None
89        self.proxyCert = None
90
91
92    def testAddUser(self):
93        """Add a new user ID to the MyProxy repository"""
94       
95        # Note the pass-phrase is read from the file tmp.  To pass
96        # explicitly as a string use the 'pPhrase' keyword instead
97        self.sessClnt.addUser(self.config['newUserName'], 
98                              pPhraseFilePath="./tmp",
99                              clntPriKeyPwd=self.__clntPriKeyPwd)
100        print "Added user '%s'" % self.config['newUserName']
101       
102
103    def testCookieConnect(self):
104        """testCookieConnect: Connect as if acting as a browser client -
105        a cookie is returned"""
106       
107        # Note the pass-phrase is read from the file tmp.  To pass
108        # explicitly as a string use the 'pPhrase' keyword instead
109        sSessCookie = self.sessClnt.connect(self.config['userName'], 
110                                    pPhraseFilePath="./tmp",
111                                    clntPriKeyPwd=self.__clntPriKeyPwd)
112        self.sessCookie = SimpleCookie(sSessCookie)
113        print "User '%s' connected to Session Manager:\n%s" % \
114            (self.config['userName'], sSessCookie)
115           
116
117    def testProxyCertConnect(self):
118        """testProxyCertConnect: Connect as a command line client -
119        a proxyCert is returned"""
120             
121        self.proxyCert = self.sessClnt.connect(self.config['userName'], 
122                                      pPhraseFilePath="./tmp",
123                                      createServerSess=True,
124                                      getCookie=False,
125                                      clntPriKeyPwd=self.__clntPriKeyPwd)
126        print "User '%s' connected to Session Manager:\n%s" % \
127            (self.config['userName'], self.proxyCert)
128
129
130    def testCookieReqAuthorisation(self):
131        """testCookieReqAuthorisation: make an authorisation request using
132        a cookie as authentication credential"""
133       
134        self.testCookieConnect()
135        authResp = self.sessClnt.reqAuthorisation(\
136                    sessID=self.sessCookie['NDG-ID1'].value, 
137                    aaWSDL=self.config['aaWSDL'],
138                    encrSessMgrWSDLuri=self.sessCookie['NDG-ID2'].value,
139                    clntPriKeyPwd=self.__clntPriKeyPwd)
140                                                             
141        # The authorisation response is returned as an object which
142        # behaves like a python dictionary.  See
143        # ndg.security.SessionMgrIO.AuthorisationResp
144        if 'errMsg' in authResp:
145            print "Authorisation failed for user: %s" % authResp['errMsg']           
146        else:
147            print "User authorised"
148           
149        print authResp
150
151
152    def testProxyCertReqAuthorisation(self):
153        """testProxyCertReqAuthorisation: make an authorisation request using
154        a proxy cert as authentication credential"""
155        self.testProxyCertConnect()
156       
157        # Request an attribute certificate from an Attribute Authority
158        # using the proxyCert returned from connect()
159        authResp = self.sessClnt.reqAuthorisation(\
160                                     proxyCert=self.proxyCert,
161                                     aaWSDL=self.config['aaWSDL'],
162                                     clntPriKeyPwd=self.__clntPriKeyPwd)
163                                             
164        # The authorisation response is returned as an object which
165        # behaves like a python dictionary.  See
166        # ndg.security.SessionMgrIO.AuthorisationResp
167        if 'errMsg' in authResp:
168            print "Authorisation failed for user %s" % authResp['errMsg']           
169        else:
170            print "User authorised"
171           
172        print authResp
173
174
175    def testGetPubKey(self):
176        "testGetPubKey: return the Session Manager's public key"
177        pubKey = self.sessClnt.getPubKey()
178                                             
179        print "Public Key:\n" + pubKey
180           
181           
182#_____________________________________________________________________________       
183class SessionMgrClientTestSuite(unittest.TestSuite):
184   
185    def __init__(self):
186        map = map(SessionMgrClientTestCase,
187                  (
188                    "testAddUser",
189                    "testConnect",
190                    "testDisconnect",
191                    "testReqAuthorisation",
192                    "testGetPubKey",
193                  ))
194        unittest.TestSuite.__init__(self, map)
195           
196                                                   
197if __name__ == "__main__":
198    unittest.main()       
Note: See TracBrowser for help on using the repository browser.