now that we create an SFA auth to hold the sfa entities involved:
[tests.git] / system / TestAuthSfa.py
1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA 
3 #
4 import utils
5 import os, os.path
6 import datetime
7 import time
8
9 from TestKey import TestKey
10 from TestNode import TestNode
11 from TestSsh import TestSsh
12 from TestUserSfa import TestUserSfa
13 from TestSliceSfa import TestSliceSfa
14
15
16 def slice_sfa_mapper (method):
17     def actual(self,*args, **kwds):
18         overall=True
19         slice_method = TestSliceSfa.__dict__[method.__name__]
20         for slice_spec in [ self.auth_sfa_spec['slice_spec'] ]:
21             test_slice_sfa = TestSliceSfa(self,slice_spec)
22             if not slice_method(test_slice_sfa, *args, **kwds): overall=False
23         return overall
24     # restore the doc text
25     actual.__doc__=TestSliceSfa.__dict__[method.__name__].__doc__
26     return actual
27
28
29 def user_sfa_mapper (method):
30     def actual(self,*args, **kwds):
31         overall=True
32         user_method = TestUserSfa.__dict__[method.__name__]
33         for user_spec in [ self.auth_sfa_spec['user_spec'] ]:
34             test_user_sfa = TestUserSfa(self,user_spec)
35             if not user_method(test_user_sfa, *args, **kwds): overall=False
36         return overall
37     # restore the doc text
38     actual.__doc__=TestUserSfa.__dict__[method.__name__].__doc__
39     return actual
40
41
42 class TestAuthSfa:
43
44     def __init__ (self,test_plc,auth_sfa_spec):
45         self.test_plc=test_plc
46         self.auth_sfa_spec=auth_sfa_spec
47         self.test_ssh=TestSsh(self.test_plc.test_ssh)
48 #        # shortcuts
49         self.login_base=self.auth_sfa_spec['login_base']
50 #        self.piuser=self.auth_sfa_spec['piuser']
51 #        self.regularuser=self.auth_sfa_spec['regularuser']
52 #        self.slicename=self.auth_sfa_spec['slicename']
53 #        
54 #    def plc_name(self):
55 #        return self.auth_sfa_spec['plc_slicename']
56     
57     def rspec_style (self): return self.auth_sfa_spec['rspec_style']
58
59     def sfi_path (self):
60         return "/root/sfi/%s"%(self.rspec_style())
61
62     # the hrn for the root authority
63     def root_hrn (self):
64         return self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
65
66     # the hrn for the auth/site
67     def hrn (self):
68         return "%s.%s"%(self.root_hrn(),self.login_base)
69
70     # something in this site (users typically)
71     def qualified (self, name):
72 #        return "%s.%s"%(self.auth_sfa_spec['hrn_prefix'],name)
73         return "%s.%s"%(self.hrn(),name)
74
75     # xxx this needs tweaks with more recent versions of sfa that have pgv2 as the default ?
76     # dir_name is local and will be pushed later on by TestPlc
77     # by default set SFI_USER to the pi, we'll overload this
78     # on the command line when needed
79     def sfi_configure (self,dir_name):
80         plc_spec=self.test_plc.plc_spec
81         # cheat a bit: retrieve the global SFA spec from the plc obj
82         sfa_spec=self.test_plc.plc_spec['sfa']
83         # fetch keys in config spec and expose to sfi
84         for spec_name in ['pi_spec','user_spec']:
85             user_spec=self.auth_sfa_spec[spec_name]
86             user_leaf=user_spec['name']
87             key_name=user_spec['key_name']
88             key_spec = self.test_plc.locate_key (key_name)
89             for (kind,ext) in [ ('private', 'pkey'), ('public', 'pub') ] :
90                 contents=key_spec[kind]
91                 file_name=os.path.join(dir_name,self.qualified(user_leaf))+"."+ext
92                 fileconf=open(file_name,'w')
93                 fileconf.write (contents)
94                 fileconf.close()
95                 utils.header ("(Over)wrote %s"%file_name)
96         #
97         file_name=dir_name + os.sep + 'sfi_config'
98         fileconf=open(file_name,'w')
99         SFI_AUTH=self.hrn()
100         fileconf.write ("SFI_AUTH='%s'"%SFI_AUTH)
101         fileconf.write('\n')
102         # default is to run as a PI
103         SFI_USER=self.qualified(self.auth_sfa_spec['pi_spec']['name'])
104         fileconf.write ("SFI_USER='%s'"%SFI_USER)
105         fileconf.write('\n')
106         SFI_REGISTRY='http://' + sfa_spec['SFA_REGISTRY_HOST'] + ':12345/'
107         fileconf.write ("SFI_REGISTRY='%s'"%SFI_REGISTRY)
108         fileconf.write('\n')
109         SFI_SM='http://' + sfa_spec['SFA_SM_HOST'] + ':12347/'
110         fileconf.write ("SFI_SM='%s'"%SFI_SM)
111         fileconf.write('\n')
112         fileconf.close()
113         utils.header ("(Over)wrote %s"%file_name)
114
115     # using sfaadmin to bootstrap
116     def sfa_add_site (self, options):
117         "bootstrap a site using sfaadmin"
118         command="sfaadmin reg register -t authority -x %s"%self.hrn()
119         return self.test_plc.run_in_guest(command)==0
120
121     def sfa_add_pi (self, options):
122         "bootstrap a PI user for that site"
123         pi_spec = self.auth_sfa_spec['pi_spec']
124         pi_hrn=self.qualified(pi_spec['name'])
125         pi_mail=pi_spec['email']
126         # as installed by sfi_config
127         pi_key=os.path.join(self.sfi_path(),self.qualified(pi_spec['name']+'.pub'))
128         command="sfaadmin reg register -t user -x %s --email %s --key %s"%(pi_hrn,pi_mail,pi_key)
129         if self.test_plc.run_in_guest(command)!=0: return False
130         command="sfaadmin reg update -t authority -x %s --pi %s"%(self.hrn(),pi_hrn)
131         return self.test_plc.run_in_guest(command)==0
132
133     # run as pi
134     def sfi_pi (self, command):
135         pi_name=self.auth_sfa_spec['pi_spec']['name']
136         return "sfi -d %s -u %s %s"%(self.sfi_path(),self.qualified(pi_name), command,)
137     # the sfi command line option to run as a regular user
138     def sfi_user (self, command):
139         user_name=self.auth_sfa_spec['user_spec']['name']
140         return "sfi -d %s -u %s %s"%(self.sfi_path(),self.qualified(user_name), command,)
141
142     # user management
143     @user_sfa_mapper
144     def sfa_add_user (self, *args, **kwds): pass
145     @user_sfa_mapper
146     def sfa_update_user (self, *args, **kwds): pass
147     @user_sfa_mapper
148     def sfa_delete_user (self, *args, **kwds): pass
149
150     def sfi_list (self, options):
151         "run (as regular user) sfi list (on Registry)"
152         return \
153             self.test_plc.run_in_guest(self.sfi_user("list -r %s"%self.root_hrn()))==0 and \
154             self.test_plc.run_in_guest(self.sfi_user("list %s"%(self.hrn())))==0
155
156     def sfi_show (self, options):
157         "run (as regular user) sfi show (on Registry)"
158         return \
159             self.test_plc.run_in_guest(self.sfi_user("show %s"%(self.hrn())))==0
160
161     def sfi_slices (self, options):
162         "run (as regular user) sfi slices (on SM)"
163         return \
164             self.test_plc.run_in_guest(self.sfi_user("slices"))==0 
165
166     # those are step names exposed as methods of TestPlc, hence the _sfa
167     @slice_sfa_mapper
168     def sfa_add_slice (self, *args, **kwds): pass
169     @slice_sfa_mapper
170     def sfa_discover (self, *args, **kwds): pass
171     @slice_sfa_mapper
172     def sfa_create_slice (self, *args, **kwds): pass
173     @slice_sfa_mapper
174     def sfa_check_slice_plc (self, *args, **kwds): pass
175     @slice_sfa_mapper
176     def sfa_update_slice (self, *args, **kwds): pass
177     @slice_sfa_mapper
178     def sfa_delete_slice (self, *args, **kwds): pass
179     @slice_sfa_mapper
180     def ssh_slice_sfa     (self, *args, **kwds): pass
181