4d8305257e7776d935f0a1e88660d22a69b42909
[tests.git] / system / TestAuthSfa.py
1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA 
3 #
4 import os, os.path
5
6 import utils
7 from TestSsh import TestSsh
8 from TestUserSfa import TestUserSfa
9 from TestSliceSfa import TestSliceSfa
10
11
12 def slice_sfa_mapper (method):
13     def actual(self,*args, **kwds):
14         # used to map on several slices...
15         overall=True
16         slice_method = TestSliceSfa.__dict__[method.__name__]
17         slice_spec = self.auth_sfa_spec['slice_spec']
18         test_slice_sfa = TestSliceSfa(self, slice_spec)
19         if not slice_method(test_slice_sfa, *args, **kwds): overall=False
20         return overall
21     # restore the doc text
22     actual.__doc__=TestSliceSfa.__dict__[method.__name__].__doc__
23     return actual
24
25
26 def user_sfa_mapper (method):
27     def actual(self,*args, **kwds):
28         overall=True
29         user_method = TestUserSfa.__dict__[method.__name__]
30         for user_spec in [ self.auth_sfa_spec['user_spec'] ]:
31             test_user_sfa = TestUserSfa(self,user_spec)
32             if not user_method(test_user_sfa, *args, **kwds): overall=False
33         return overall
34     # restore the doc text
35     actual.__doc__ = TestUserSfa.__dict__[method.__name__].__doc__
36     return actual
37
38
39 class TestAuthSfa:
40
41     def __init__ (self, test_plc, auth_sfa_spec):
42         self.test_plc = test_plc
43         self.auth_sfa_spec = auth_sfa_spec
44         self.test_ssh = TestSsh(self.test_plc.test_ssh)
45 #        # shortcuts
46         self.login_base = self.auth_sfa_spec['login_base']
47 #        self.piuser = self.auth_sfa_spec['piuser']
48 #        self.regularuser = self.auth_sfa_spec['regularuser']
49     
50     def rspec_style (self): return self.auth_sfa_spec['rspec_style']
51
52     def sfi_path (self):
53         return "/root/sfi/{}".format(self.rspec_style())
54
55     # the hrn for the root authority
56     def root_hrn (self):
57         return self.test_plc.plc_spec['sfa']['settings']['SFA_REGISTRY_ROOT_AUTH']
58
59     # the hrn for the auth/site
60     def auth_hrn (self):
61         return "{}.{}".format(self.root_hrn(), self.login_base)
62
63     # something in this site (users typically); for use by classes for subobjects
64     def obj_hrn (self, name):
65         return "{}.{}".format(self.auth_hrn(), name)
66
67     def regular_user_hrn(self):
68         return self.obj_hrn(self.auth_sfa_spec['user_spec']['name'])
69     def slice_hrn(self):
70         return self.obj_hrn(self.auth_sfa_spec['slice_spec']['name'])
71
72     # xxx this needs tweaks with more recent versions of sfa that have pgv2 as the default ?
73     # dir_name is local and will be pushed later on by TestPlc
74     # by default set SFI_USER to the pi, we'll overload this
75     # on the command line when needed
76     def sfi_configure (self,dir_name):
77         plc_spec=self.test_plc.plc_spec
78         # cheat a bit: retrieve the global SFA spec from the plc obj
79         sfa_spec=self.test_plc.plc_spec['sfa']
80         # fetch keys in config spec and expose to sfi
81         for spec_name in ['pi_spec','user_spec']:
82             user_spec=self.auth_sfa_spec[spec_name]
83             user_leaf=user_spec['name']
84             key_name=user_spec['key_name']
85             key_spec = self.test_plc.locate_key (key_name)
86             for (kind,ext) in [ ('private', 'pkey'), ('public', 'pub') ] :
87                 contents=key_spec[kind]
88                 file_name=os.path.join(dir_name,self.obj_hrn(user_leaf))+"."+ext
89                 fileconf=open(file_name,'w')
90                 fileconf.write (contents)
91                 fileconf.close()
92                 utils.header ("(Over)wrote {}".format(file_name))
93         #
94         file_name=dir_name + os.sep + 'sfi_config'
95         fileconf=open(file_name,'w')
96         SFI_AUTH=self.auth_hrn()
97         fileconf.write ("SFI_AUTH='{}'".format(SFI_AUTH))
98         fileconf.write('\n')
99         # default is to run as a PI
100         SFI_USER=self.obj_hrn(self.auth_sfa_spec['pi_spec']['name'])
101         fileconf.write("SFI_USER='{}'".format(SFI_USER))
102         fileconf.write('\n')
103         SFI_REGISTRY='http://{}:{}/'.format(sfa_spec['settings']['SFA_REGISTRY_HOST'], 12345)
104         fileconf.write("SFI_REGISTRY='{}'".format(SFI_REGISTRY))
105         fileconf.write('\n')
106         SFI_SM='http://{}:{}/'.format(sfa_spec['settings']['SFA_SM_HOST'], sfa_spec['sfi-connects-to-port'])
107         fileconf.write("SFI_SM='{}'".format(SFI_SM))
108         fileconf.write('\n')
109         fileconf.close()
110         utils.header ("(Over)wrote {}".format(file_name))
111
112     # using sfaadmin to bootstrap
113     def sfa_register_site (self, options):
114         "bootstrap a site using sfaadmin"
115         command="sfaadmin reg register -t authority -x {}".format(self.auth_hrn())
116         return self.test_plc.run_in_guest(command)==0
117
118     def sfa_register_pi (self, options):
119         "bootstrap a PI user for that site"
120         pi_spec = self.auth_sfa_spec['pi_spec']
121         pi_hrn=self.obj_hrn(pi_spec['name'])
122         pi_mail=pi_spec['email']
123         # as installed by sfi_config
124         pi_key=os.path.join(self.sfi_path(),self.obj_hrn(pi_spec['name']+'.pub'))
125         command="sfaadmin reg register -t user -x {} --email {} --key {}".format(pi_hrn, pi_mail, pi_key)
126         if self.test_plc.run_in_guest(command)!=0: return False
127         command="sfaadmin reg update -t authority -x {} --pi {}".format(self.auth_hrn(), pi_hrn)
128         return self.test_plc.run_in_guest(command) == 0
129
130     # run as pi
131     def sfi_pi (self, command):
132         pi_name=self.auth_sfa_spec['pi_spec']['name']
133         return "sfi -d {} -u {} {}".format(self.sfi_path(), self.obj_hrn(pi_name), command)
134     # the sfi command line option to run as a regular user
135     def sfi_user (self, command):
136         user_name=self.auth_sfa_spec['user_spec']['name']
137         return "sfi -d {} -u {} {}".format(self.sfi_path(), self.obj_hrn(user_name), command)
138
139     # user management
140     @user_sfa_mapper
141     def sfa_register_user (self, *args, **kwds): pass
142     @user_sfa_mapper
143     def sfa_update_user (self, *args, **kwds): pass
144     @user_sfa_mapper
145     def sfa_delete_user (self, *args, **kwds): pass
146
147     def sfa_remove_user_from_slice (self, options):
148         "remove regular user from slice"
149         command="update -t slice -x {} -r none".format(self.slice_hrn())
150         # xxx should check result other than visually
151         return self.test_plc.run_in_guest(self.sfi_pi(command))==0
152
153     def sfa_insert_user_in_slice (self, options):
154         "defines regular user as unique user in slice"
155         command="update -t slice -x {} -r {}".format(self.slice_hrn(), self.regular_user_hrn())
156         # xxx should check result other than visually
157         return self.test_plc.run_in_guest(self.sfi_pi(command))==0
158
159     def sfi_list (self, options):
160         "run (as regular user) sfi list (on Registry)"
161         return \
162             self.test_plc.run_in_guest(self.sfi_user("list -r {}".format(self.root_hrn()))) == 0 and \
163             self.test_plc.run_in_guest(self.sfi_user("list {}".format(self.auth_hrn()))) == 0
164
165     def sfi_show_site (self, options):
166         "run (as regular user) sfi show (on Registry)"
167         return \
168             self.test_plc.run_in_guest(self.sfi_user("show {}".format(self.auth_hrn()))) == 0
169
170
171     def sfi_show_slice (self, options):
172         "run (as PI) sfi show -n <slice> (on Registry)"
173         return \
174             self.test_plc.run_in_guest(self.sfi_pi("show -n {}".format(self.slice_hrn()))) == 0
175
176     # checks if self.regular_user is found in registry's reg-researchers
177     def sfi_show_slice_researchers (self, options):
178         "run (as PI) sfi show <slice> -k researcher -k reg-researchers (on Registry)"
179         return \
180             self.test_plc.run_in_guest(self.sfi_pi("show {} -k researcher -k reg-researchers".format(self.slice_hrn()))) == 0
181         
182
183     # those are step names exposed as methods of TestPlc, hence the _sfa
184     @slice_sfa_mapper
185     def sfa_register_slice (self, *args, **kwds): pass
186     @slice_sfa_mapper
187     def sfa_renew_slice (self, *args, **kwds): pass
188     @slice_sfa_mapper
189     def sfa_get_expires (self, *args, **kwds): pass
190     @slice_sfa_mapper
191     def sfa_discover (self, *args, **kwds): pass
192     @slice_sfa_mapper
193     def sfa_rspec (self, *args, **kwds): pass
194     @slice_sfa_mapper
195     def sfa_allocate (self, *args, **kwds): pass
196     @slice_sfa_mapper
197     def sfa_allocate_empty (self, *args, **kwds): pass
198     @slice_sfa_mapper
199     def sfa_provision (self, *args, **kwds): pass
200     @slice_sfa_mapper
201     def sfa_provision_empty (self, *args, **kwds): pass
202     @slice_sfa_mapper
203     def sfa_check_slice_plc (self, *args, **kwds): pass
204     @slice_sfa_mapper
205     def sfa_check_slice_plc_empty (self, *args, **kwds): pass
206     @slice_sfa_mapper
207     def sfa_update_slice (self, *args, **kwds): pass
208     @slice_sfa_mapper
209     def sfa_delete_slice (self, *args, **kwds): pass
210     @slice_sfa_mapper
211     def ssh_slice_sfa     (self, *args, **kwds): pass
212