1 | #!/usr/bin/env python |
---|
2 | """Unit tests for Credential Wallet class |
---|
3 | |
---|
4 | NERC Data Grid Project |
---|
5 | """ |
---|
6 | __author__ = "P J Kershaw" |
---|
7 | __date__ = "03/10/08" |
---|
8 | __copyright__ = "(C) 2008 STFC & NERC" |
---|
9 | __license__ = \ |
---|
10 | """This software may be distributed under the terms of the Q Public |
---|
11 | License, version 1.0 or later.""" |
---|
12 | __contact__ = "P.J.Kershaw@rl.ac.uk" |
---|
13 | __revision__ = '$Id$' |
---|
14 | |
---|
15 | import unittest |
---|
16 | import os, sys, getpass, re |
---|
17 | import traceback |
---|
18 | |
---|
19 | from ndg.security.common.utils.ConfigFileParsers import \ |
---|
20 | CaseSensitiveConfigParser |
---|
21 | from ndg.security.common.X509 import X509CertParse |
---|
22 | from ndg.security.common.CredWallet import CredWallet, \ |
---|
23 | CredWalletAttributeRequestDenied |
---|
24 | |
---|
25 | from os.path import expandvars as xpdVars |
---|
26 | from os.path import join as jnPath |
---|
27 | mkPath = lambda file: jnPath(os.environ['NDGSEC_CREDWALLET_UNITTEST_DIR'],file) |
---|
28 | |
---|
29 | import logging |
---|
30 | logging.basicConfig(level=logging.DEBUG) |
---|
31 | |
---|
32 | |
---|
33 | class CredWalletTestCase(unittest.TestCase): |
---|
34 | """Unit test case for ndg.security.common.CredWallet.CredWallet class. |
---|
35 | |
---|
36 | """ |
---|
37 | |
---|
38 | def setUp(self): |
---|
39 | |
---|
40 | if 'NDGSEC_INT_DEBUG' in os.environ: |
---|
41 | import pdb |
---|
42 | pdb.set_trace() |
---|
43 | |
---|
44 | if 'NDGSEC_CREDWALLET_UNITTEST_DIR' not in os.environ: |
---|
45 | os.environ['NDGSEC_CREDWALLET_UNITTEST_DIR'] = \ |
---|
46 | os.path.abspath(os.path.dirname(__file__)) |
---|
47 | |
---|
48 | self.cfg = CaseSensitiveConfigParser() |
---|
49 | configFilePath = jnPath(os.environ['NDGSEC_CREDWALLET_UNITTEST_DIR'], |
---|
50 | "credWalletTest.cfg") |
---|
51 | self.cfg.read(configFilePath) |
---|
52 | |
---|
53 | |
---|
54 | def test1ReadOnlyClassVariables(self): |
---|
55 | |
---|
56 | try: |
---|
57 | CredWallet.accessDenied = 'yes' |
---|
58 | self.fail("accessDenied class variable should be read-only") |
---|
59 | except Exception, e: |
---|
60 | print("PASS - accessDenied class variable is read-only") |
---|
61 | |
---|
62 | try: |
---|
63 | CredWallet.accessGranted = False |
---|
64 | self.fail("accessGranted class variable should be read-only") |
---|
65 | except Exception, e: |
---|
66 | print("PASS - accessGranted class variable is read-only") |
---|
67 | |
---|
68 | assert(not CredWallet.accessDenied) |
---|
69 | assert(CredWallet.accessGranted) |
---|
70 | |
---|
71 | |
---|
72 | def test2SetAttributes(self): |
---|
73 | |
---|
74 | credWallet = CredWallet() |
---|
75 | credWallet.userX509Cert = \ |
---|
76 | '''-----BEGIN CERTIFICATE----- |
---|
77 | MIICazCCAdSgAwIBAgICAPcwDQYJKoZIhvcNAQEEBQAwLzEMMAoGA1UEChMDTkRH |
---|
78 | MQ0wCwYDVQQLEwRCQURDMRAwDgYDVQQDEwdUZXN0IENBMB4XDTA4MDEwNDEwMTk0 |
---|
79 | N1oXDTA5MDEwMzEwMTk0N1owLDEMMAoGA1UEChMDTkRHMQ0wCwYDVQQLEwRCQURD |
---|
80 | MQ0wCwYDVQQDEwR0ZXN0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA |
---|
81 | rpbuNUHWVRwhjHzhTOdym+fcZdmD7HbaeoFdef2V//Wj41xMieMZy9XQft2dFBDY |
---|
82 | ZIHLElojVhZTHoowMkwXxsmLt7hZF8fL7j3ssU/lflM9E0Uk2dZxaAt97zXEruEH |
---|
83 | JoNqHTEQlH0qMALfuUrAaZEIXHDdTQDNRJl4oXvjJWaqS8Y5Je8QREThIE5hRd9F |
---|
84 | oUlgfMNNnwzLyIH7s0KBci2yryeubAG/Qig5LkulbpnhxYLCcLvs3THQ3kO5qYYb |
---|
85 | B0g11YOBgshZ0SpNwEEyhDzHUt3Ii2XmAh25/II08BR61fhMZvSJ/tVGJY4HfWG7 |
---|
86 | B4PZzYwo5vn/tYH1mk7w5QIDAQABoxUwEzARBglghkgBhvhCAQEEBAMCBPAwDQYJ |
---|
87 | KoZIhvcNAQEEBQADgYEAFKEdr2FwlposAGRDHBMX9d48TKm1gXzOMEvReTYIaq46 |
---|
88 | aMpDDuApsbjpRqohvKIrngGa2e1p81tOTL5kbuusNjcNsagXkNgeO6qcGZCly/Bl |
---|
89 | 9Kxfynaned5jmgWgoxJP7VtOynvlLqJfrS/cEwOWDYpyPjJDRx2cZgEd3P4WfYI= |
---|
90 | -----END CERTIFICATE----- |
---|
91 | ''' |
---|
92 | print("userCert=%s" % credWallet.userX509Cert) |
---|
93 | credWallet.userId = 'ndg-user' |
---|
94 | print("userId=%s" % credWallet.userId) |
---|
95 | |
---|
96 | try: |
---|
97 | credWallet.blah = 'blah blah' |
---|
98 | self.fail("Attempting to set attribute not in __slots__ class " |
---|
99 | "variable should fail") |
---|
100 | except AttributeError: |
---|
101 | print("PASS - expected AttributeError when setting attribute " |
---|
102 | "not in __slots__ class variable") |
---|
103 | |
---|
104 | credWallet.caCertFilePathList=None |
---|
105 | credWallet.attributeAuthorityURI='http://localhost/AttributeAuthority' |
---|
106 | |
---|
107 | credWallet.attributeAuthority = None |
---|
108 | credWallet.credentialRepository = None |
---|
109 | credWallet.mapFromTrustedHosts = False |
---|
110 | credWallet.rtnExtAttCertList = True |
---|
111 | credWallet.attCertRefreshElapse = 7200 |
---|
112 | |
---|
113 | def test3GetAttCertWithUserId(self): |
---|
114 | |
---|
115 | credWallet = CredWallet(cfg=self.cfg.get('setUp', 'cfgFilePath')) |
---|
116 | attCert = credWallet.getAttCert() |
---|
117 | |
---|
118 | # No user X.509 cert is set so the resulting Attribute Certificate |
---|
119 | # user ID should be the same as that set for the wallet |
---|
120 | assert(attCert.userId == credWallet.userId) |
---|
121 | print "Attribute Certificate:\n%s" % attCert |
---|
122 | |
---|
123 | def test4GetAttCertWithUserX509Cert(self): |
---|
124 | |
---|
125 | credWallet = CredWallet(cfg=self.cfg.get('setUp', 'cfgFilePath')) |
---|
126 | |
---|
127 | # Set a test individual user certificate to override the client |
---|
128 | # cert. and private key in WS-Security settings in the config file |
---|
129 | credWallet.userX509Cert = """ |
---|
130 | -----BEGIN CERTIFICATE----- |
---|
131 | MIICazCCAdSgAwIBAgICAPcwDQYJKoZIhvcNAQEEBQAwLzEMMAoGA1UEChMDTkRH |
---|
132 | MQ0wCwYDVQQLEwRCQURDMRAwDgYDVQQDEwdUZXN0IENBMB4XDTA4MDEwNDEwMTk0 |
---|
133 | N1oXDTA5MDEwMzEwMTk0N1owLDEMMAoGA1UEChMDTkRHMQ0wCwYDVQQLEwRCQURD |
---|
134 | MQ0wCwYDVQQDEwR0ZXN0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA |
---|
135 | rpbuNUHWVRwhjHzhTOdym+fcZdmD7HbaeoFdef2V//Wj41xMieMZy9XQft2dFBDY |
---|
136 | ZIHLElojVhZTHoowMkwXxsmLt7hZF8fL7j3ssU/lflM9E0Uk2dZxaAt97zXEruEH |
---|
137 | JoNqHTEQlH0qMALfuUrAaZEIXHDdTQDNRJl4oXvjJWaqS8Y5Je8QREThIE5hRd9F |
---|
138 | oUlgfMNNnwzLyIH7s0KBci2yryeubAG/Qig5LkulbpnhxYLCcLvs3THQ3kO5qYYb |
---|
139 | B0g11YOBgshZ0SpNwEEyhDzHUt3Ii2XmAh25/II08BR61fhMZvSJ/tVGJY4HfWG7 |
---|
140 | B4PZzYwo5vn/tYH1mk7w5QIDAQABoxUwEzARBglghkgBhvhCAQEEBAMCBPAwDQYJ |
---|
141 | KoZIhvcNAQEEBQADgYEAFKEdr2FwlposAGRDHBMX9d48TKm1gXzOMEvReTYIaq46 |
---|
142 | aMpDDuApsbjpRqohvKIrngGa2e1p81tOTL5kbuusNjcNsagXkNgeO6qcGZCly/Bl |
---|
143 | 9Kxfynaned5jmgWgoxJP7VtOynvlLqJfrS/cEwOWDYpyPjJDRx2cZgEd3P4WfYI= |
---|
144 | -----END CERTIFICATE----- |
---|
145 | """ |
---|
146 | credWallet.userPriKey = """ |
---|
147 | -----BEGIN RSA PRIVATE KEY----- |
---|
148 | MIIEowIBAAKCAQEArpbuNUHWVRwhjHzhTOdym+fcZdmD7HbaeoFdef2V//Wj41xM |
---|
149 | ieMZy9XQft2dFBDYZIHLElojVhZTHoowMkwXxsmLt7hZF8fL7j3ssU/lflM9E0Uk |
---|
150 | 2dZxaAt97zXEruEHJoNqHTEQlH0qMALfuUrAaZEIXHDdTQDNRJl4oXvjJWaqS8Y5 |
---|
151 | Je8QREThIE5hRd9FoUlgfMNNnwzLyIH7s0KBci2yryeubAG/Qig5LkulbpnhxYLC |
---|
152 | cLvs3THQ3kO5qYYbB0g11YOBgshZ0SpNwEEyhDzHUt3Ii2XmAh25/II08BR61fhM |
---|
153 | ZvSJ/tVGJY4HfWG7B4PZzYwo5vn/tYH1mk7w5QIDAQABAoIBAQCQdxly/iBxWo60 |
---|
154 | Jh1zukxOj4QCzwLnps1P8z27FMeK/eJ33scCjeWpkios4An7MZktSW0UqXt135E1 |
---|
155 | wxjwdaBzABDZm/Q0xkGLyLfTXI5EgnIWQO+mRVifxGqXhsFSB6gYCUPEFfZnOE6x |
---|
156 | XZ9sPluKvtTRUR79eb1glzGHRfEF31eBQdPkATA011twBNL3ApULxjlnFBch1LXD |
---|
157 | lldbYb9wWV9Bcl9ftJ7Sr4kJ7gqiETWRgKuyMMwGfhIrr8PXB/oq9VOAGg+XSQQY |
---|
158 | +0sm1URfh/N5Q7ES+dgOR4MTCn8LUFW859OqY5QZidqDxg/fTNNt6znx0FZcGfbd |
---|
159 | oDJV6Oc9AoGBAOgjNePWgxiDYJohNWATs7fUXvT4cGrR6TdJKXd3T8bVp+AO94au |
---|
160 | vM9iOZiCfQNRxGYHA25EfwflaF3yKLOvlsK7k1ewRvQ4Hqi/MRyRxIhPmLYCkavl |
---|
161 | FOKHV3UeLItpRJMzjU4OBq2k1g3uC22ZYWWXFaYmP+KSW5ICq0v8M4SfAoGBAMCJ |
---|
162 | UqbPP8MPht36P43dZJDX+GlPlhWcXrWCD0ePX0wExEBeg+M0GqHTWrz4OwSzHTY0 |
---|
163 | XPwPqm2kEICIhHyK/BSZ09CMOdHwUc3gRZULCrSnTkEcJY+XY9IftYcVXIL2xFfx |
---|
164 | qXqiLe7Le7p2mscSKXUM4uE4Vz16JHDE3Kh3Gnf7AoGAdi2WvcrzKoOXpl/JoIPn |
---|
165 | NmrzfJsOABOlOvQQHDWtc3hJ4pM8CGDk1l8XG0EzC4GRDq/7WyOb2BU+MLWbav61 |
---|
166 | LaX4uOeQ97uqQBY1lmnPN+XtxJtCNdSF8V0ddQ5Ldx28P4Q7J8WUOMp1/tl1D/LJ |
---|
167 | 1sI3z0Ihu+Luo0Kgmipmv9kCgYB+eTZL0RQHZCmpovsgi2/GHbhWJStnosIr5PV4 |
---|
168 | gluNKgxoZC2qj812w8l1HHJYUfg8ZQU3pmrDfuRAKm0tCncwaSPUeGh62axC2rGa |
---|
169 | iBhONyCWcJDT1BSEMMQjqgqNFOBBDMPRhLs7g3sRL1vYrLuC4iYe382e2p8ZXJe+ |
---|
170 | Kg6/BQKBgDlFDM9m/9A11PIlh/ir0KXUqtPA1q+Hn629BRsbbsH2HW+kj018RLT+ |
---|
171 | SgRwhrqFtF5HCMXEh0ez/RyHHoMiVnan9jpLtGEdE8ojJnISjvkIyLUCCJdq8HYC |
---|
172 | 25UDHqKuoqHBiXWazfZ6MOlcIm6vp1FpVDygu59JHPROMxW+BAg/ |
---|
173 | -----END RSA PRIVATE KEY----- |
---|
174 | """ |
---|
175 | credWallet.createAttributeAuthorityClnt() |
---|
176 | attCert = credWallet.getAttCert() |
---|
177 | |
---|
178 | # A user X.509 cert. was set so this cert's DN should be set in the |
---|
179 | # userId field of the resulting Attribute Certificate |
---|
180 | assert(attCert.userId == str(credWallet.userX509Cert.dn)) |
---|
181 | print "Attribute Certificate:\n%s" % attCert |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | def test5GetAttCertRefusedWithUserCert(self): |
---|
186 | |
---|
187 | credWallet = CredWallet(cfg=self.cfg.get('setUp', 'cfgFilePath')) |
---|
188 | credWallet.userX509CertFilePath = self.cfg.get('setUp', |
---|
189 | 'userX509CertFilePath') |
---|
190 | credWallet.userPriKeyFilePath = self.cfg.get('setUp', |
---|
191 | 'userPriKeyFilePath') |
---|
192 | |
---|
193 | # Set AA URI AFTER user PKI settings so that these are picked in the |
---|
194 | # implicit call to create a new AA Client when the URI is set |
---|
195 | credWallet.attributeAuthorityURI = self.cfg.get('setUp', |
---|
196 | 'attributeAuthorityURI') |
---|
197 | try: |
---|
198 | attCert = credWallet.getAttCert() |
---|
199 | except CredWalletAttributeRequestDenied, e: |
---|
200 | print "SUCCESS - obtained expected result: %s" % e |
---|
201 | return |
---|
202 | |
---|
203 | self.fail("Request allowed from Attribute Authority where user is NOT " |
---|
204 | "registered!") |
---|
205 | |
---|
206 | def test6GetMappedAttCertWithUserId(self): |
---|
207 | |
---|
208 | # Call Site A Attribute Authority where user is registered |
---|
209 | credWallet = CredWallet(cfg=self.cfg.get('setUp', 'cfgFilePath')) |
---|
210 | attCert = credWallet.getAttCert() |
---|
211 | |
---|
212 | # Use Attribute Certificate cached in wallet to get a mapped |
---|
213 | # Attribute Certificate from Site B's Attribute Authority |
---|
214 | siteBURI = self.cfg.get('setUp', 'attributeAuthorityURI') |
---|
215 | attCert = credWallet.getAttCert(attributeAuthorityURI=siteBURI) |
---|
216 | |
---|
217 | print("Mapped Attribute Certificate from Site B Attribute " |
---|
218 | "Authority:\n%s" % attCert) |
---|
219 | |
---|
220 | |
---|
221 | if __name__ == "__main__": |
---|
222 | unittest.main() |
---|