cleanup
[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/%s"%(self.rspec_style())
54
55     # the hrn for the root authority
56     def root_hrn (self):
57         return self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
58
59     # the hrn for the auth/site
60     def auth_hrn (self):
61         return "%s.%s"%(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 "%s.%s"%(self.auth_hrn(),name)
66
67     # xxx this needs tweaks with more recent versions of sfa that have pgv2 as the default ?
68     # dir_name is local and will be pushed later on by TestPlc
69     # by default set SFI_USER to the pi, we'll overload this
70     # on the command line when needed
71     def sfi_configure (self,dir_name):
72         plc_spec=self.test_plc.plc_spec
73         # cheat a bit: retrieve the global SFA spec from the plc obj
74         sfa_spec=self.test_plc.plc_spec['sfa']
75         # fetch keys in config spec and expose to sfi
76         for spec_name in ['pi_spec','user_spec']:
77             user_spec=self.auth_sfa_spec[spec_name]
78             user_leaf=user_spec['name']
79             key_name=user_spec['key_name']
80             key_spec = self.test_plc.locate_key (key_name)
81             for (kind,ext) in [ ('private', 'pkey'), ('public', 'pub') ] :
82                 contents=key_spec[kind]
83                 file_name=os.path.join(dir_name,self.obj_hrn(user_leaf))+"."+ext
84                 fileconf=open(file_name,'w')
85                 fileconf.write (contents)
86                 fileconf.close()
87                 utils.header ("(Over)wrote %s"%file_name)
88         #
89         file_name=dir_name + os.sep + 'sfi_config'
90         fileconf=open(file_name,'w')
91         SFI_AUTH=self.auth_hrn()
92         fileconf.write ("SFI_AUTH='%s'"%SFI_AUTH)
93         fileconf.write('\n')
94         # default is to run as a PI
95         SFI_USER=self.obj_hrn(self.auth_sfa_spec['pi_spec']['name'])
96         fileconf.write ("SFI_USER='%s'"%SFI_USER)
97         fileconf.write('\n')
98         SFI_REGISTRY='http://' + sfa_spec['SFA_REGISTRY_HOST'] + ':12345/'
99         fileconf.write ("SFI_REGISTRY='%s'"%SFI_REGISTRY)
100         fileconf.write('\n')
101         SFI_SM='http://' + sfa_spec['SFA_SM_HOST'] + ':12347/'
102         fileconf.write ("SFI_SM='%s'"%SFI_SM)
103         fileconf.write('\n')
104         fileconf.close()
105         utils.header ("(Over)wrote %s"%file_name)
106
107     # using sfaadmin to bootstrap
108     def sfa_add_site (self, options):
109         "bootstrap a site using sfaadmin"
110         command="sfaadmin reg register -t authority -x %s"%self.auth_hrn()
111         return self.test_plc.run_in_guest(command)==0
112
113     def sfa_add_pi (self, options):
114         "bootstrap a PI user for that site"
115         pi_spec = self.auth_sfa_spec['pi_spec']
116         pi_hrn=self.obj_hrn(pi_spec['name'])
117         pi_mail=pi_spec['email']
118         # as installed by sfi_config
119         pi_key=os.path.join(self.sfi_path(),self.obj_hrn(pi_spec['name']+'.pub'))
120         command="sfaadmin reg register -t user -x %s --email %s --key %s"%(pi_hrn,pi_mail,pi_key)
121         if self.test_plc.run_in_guest(command)!=0: return False
122         command="sfaadmin reg update -t authority -x %s --pi %s"%(self.auth_hrn(),pi_hrn)
123         return self.test_plc.run_in_guest(command)==0
124
125     # run as pi
126     def sfi_pi (self, command):
127         pi_name=self.auth_sfa_spec['pi_spec']['name']
128         return "sfi -d %s -u %s %s"%(self.sfi_path(),self.obj_hrn(pi_name), command,)
129     # the sfi command line option to run as a regular user
130     def sfi_user (self, command):
131         user_name=self.auth_sfa_spec['user_spec']['name']
132         return "sfi -d %s -u %s %s"%(self.sfi_path(),self.obj_hrn(user_name), command,)
133
134     # user management
135     @user_sfa_mapper
136     def sfa_add_user (self, *args, **kwds): pass
137     @user_sfa_mapper
138     def sfa_update_user (self, *args, **kwds): pass
139     @user_sfa_mapper
140     def sfa_delete_user (self, *args, **kwds): pass
141
142     def sfi_list (self, options):
143         "run (as regular user) sfi list (on Registry)"
144         return \
145             self.test_plc.run_in_guest(self.sfi_user("list -r %s"%self.root_hrn()))==0 and \
146             self.test_plc.run_in_guest(self.sfi_user("list %s"%(self.auth_hrn())))==0
147
148     def sfi_show_slice (self, options):
149         "run (as PI) sfi show -n <slice> (on Registry)"
150         slice_spec=self.auth_sfa_spec['slice_spec']
151         slice_hrn=self.obj_hrn(slice_spec['name'])
152         return \
153             self.test_plc.run_in_guest(self.sfi_pi("show -n %s"%slice_hrn))==0
154
155     def sfi_show_site (self, options):
156         "run (as regular user) sfi show (on Registry)"
157         return \
158             self.test_plc.run_in_guest(self.sfi_user("show %s"%(self.auth_hrn())))==0
159
160     # those are step names exposed as methods of TestPlc, hence the _sfa
161     @slice_sfa_mapper
162     def sfa_add_slice (self, *args, **kwds): pass
163     @slice_sfa_mapper
164     def sfa_renew_slice (self, *args, **kwds): pass
165     @slice_sfa_mapper
166     def sfa_discover (self, *args, **kwds): pass
167     @slice_sfa_mapper
168     def sfa_create_slice (self, *args, **kwds): pass
169     @slice_sfa_mapper
170     def sfa_check_slice_plc (self, *args, **kwds): pass
171     @slice_sfa_mapper
172     def sfa_update_slice (self, *args, **kwds): pass
173     @slice_sfa_mapper
174     def sfa_delete_slice (self, *args, **kwds): pass
175     @slice_sfa_mapper
176     def ssh_slice_sfa     (self, *args, **kwds): pass
177