a09b6654429ae45c37dd0cbf468ccc1aa04d018c
[sfa.git] / sfa / senslab / table_slab.py
1 #
2 # implements support for SFA records stored in db tables
3 #
4 # TODO: Use existing PLC database methods? or keep this separate?
5
6 import ldap
7 from sfa.trust.gid import *
8 from sfa.util.record import *
9 from sfa.util.config import *
10 from sfa.util.filter import *
11 from sfa.trust.hierarchy import *
12 from sfa.trust.certificate import *
13 from sfa.trust.auth import *
14 from sfa.senslab.OARrestapi import *
15
16 class SfaTable(list):
17     authname=""
18     def __init__(self, record_filter = None):
19         self.oar = OARapi()
20         self.ldapserv=ldap.open("192.168.0.251")
21         self.senslabauth=Hierarchy()
22         config=Config()
23         self.authname=config.SFA_REGISTRY_ROOT_AUTH
24         print >>sys.stderr,"AUTHNAME :  ",self.authname
25         authinfo=self.senslabauth.get_auth_info(self.authname)
26         
27         self.auth=Auth()
28         gid=authinfo.get_gid_object()
29
30     def exists(self):
31         return True
32
33     def db_fields(self, obj=None):
34         return dict( [ ] )
35
36     @staticmethod
37     def is_writable (key,value,dict):
38         # if not mentioned, assume it's writable (e.g. deleted ...)
39         if key not in dict: return True
40         # if mentioned but not linked to a Parameter object, idem
41         if not isinstance(dict[key], Parameter): return True
42         # if not marked ro, it's writable
43         if not dict[key].ro: return True
44
45         return False
46
47
48     def create(self):
49         return True
50     
51     def remove(self, record):
52         return 0
53
54     def insert(self, record):
55         return 0
56
57     def update(self, record):
58         return 0
59
60     def quote_string(self, value):
61         return str(self.db.quote(value))
62
63     def quote(self, value):
64         return self.db.quote(value)
65     
66     def ldapFind(self, record_filter = None, columns=None):
67
68         results = []
69         
70         #first, ldap for users
71
72         if 'authority' in record_filter:
73                 # ask for authority
74                 if record_filter['authority']==self.authname:
75                         # which is SFA_REGISTRY_ROOT_AUTH
76                         # request all records which are under our authority, ie all ldap entries
77                         ldapfilter="cn=*"
78                 else:
79                         #which is NOT SFA_REGISTRY_ROOT_AUTH
80                         return []
81         else :
82                 if not 'hrn' in record_filter:
83                         print >>sys.stderr,"find : don't know how to handle filter ",record_filter
84                         return []
85                 else:
86                         hrns=[]
87                         h=record_filter['hrn']
88                         if  isinstance(h,list):
89                                 hrns=h
90                         else : 
91                                 hrns.append(h)
92         
93                         ldapfilter="(|"
94                         for hrn in hrns:
95                                 splited_hrn=hrn.split(".")
96                                 if splited_hrn[0] != "SFA_REGISTRY_ROOT_AUTH" :
97                                         print >>sys.stderr,"i know nothing about",hrn
98                                 else :
99                                         login=splited_hrn[1]
100                                         ldapfilter+="(uid="
101                                         ldapfilter+=login
102                                         ldapfilter+=")"
103                         ldapfilter+=")"
104         
105         
106         rindex=self.ldapserv.search("ou=people,dc=senslab,dc=info",ldap.SCOPE_SUBTREE,ldapfilter, ['mail','givenName', 'sn', 'uid','sshPublicKey'])
107         ldapresponse=self.ldapserv.result(rindex,1)
108         for ldapentry in ldapresponse[1]:
109                 hrn=self.authname+"."+ldapentry[1]['uid'][0]
110                 uuid=create_uuid() 
111                 
112                 RSA_KEY_STRING=ldapentry[1]['sshPublicKey'][0]
113                 
114                 pkey=convert_public_key(RSA_KEY_STRING)
115                 
116                 gid=self.senslabauth.create_gid("urn:publicid:IDN+"+self.authname+"+user+"+ldapentry[1]['uid'][0], uuid, pkey, CA=False)
117                 
118                 parent_hrn = get_authority(hrn)
119                 parent_auth_info = self.senslabauth.get_auth_info(parent_hrn)
120
121                 results.append(  {      
122                         'type': 'user',
123 #                       'email': ldapentry[1]['mail'][0],
124 #                       'first_name': ldapentry[1]['givenName'][0],
125 #                       'last_name': ldapentry[1]['sn'][0],
126 #                       'phone': 'none',
127                         'gid': gid.save_to_string(),
128                         'serial': 'none',
129                         'authority': self.authname,
130                         'peer_authority': '',
131                         'pointer' : '',
132                         'hrn': hrn,
133                         'date_created' : 'none',
134                         'last_updated': 'none'
135                         } )
136         return results
137
138     def oarFind(self, record_filter = None, columns=None):
139         results=[]
140         node_ids=[]
141
142         if 'authority' in record_filter:
143                 # ask for authority
144                 if record_filter['authority']== self.authname :
145                         # which is senslab
146                         print>> sys.stderr , "ET MERDE !!!!"
147                         node_ids=""
148                 else:
149                         # which is NOT senslab
150                         return []
151         else :
152                 if not 'hrn' in record_filter:
153                         print >>sys.stderr,"find : don't know how to handle filter ",record_filter
154                         return []
155                 else:
156                         hrns=[]
157                         h=record_filter['hrn']
158                         if  isinstance(h,list):
159                                 hrns=h
160                         else : 
161                                 hrns.append(h)
162         
163                         for hrn in hrns:
164                                 head,sep,tail=hrn.partition(".")
165                                 if head != self.authname :
166                                         print >>sys.stderr,"i know nothing about",hrn
167                                 else :
168                                         node_ids.append(tail)
169
170         node_list = self.oar.GetNodes( node_ids)
171
172         for node in node_list:
173                 hrn=self.authname+"."+node['hostname']
174                 results.append(  {      
175                         'type': 'node',
176 #                       'email': ldapentry[1]['mail'][0],
177 #                       'first_name': ldapentry[1]['givenName'][0],
178 #                       'last_name': ldapentry[1]['sn'][0],
179 #                       'phone': 'none',
180 #                       'gid': gid.save_to_string(),
181 #                       'serial': 'none',
182                         'authority': self.authname,
183                         'peer_authority': '',
184                         'pointer' : '',
185                         'hrn': hrn,
186                         'date_created' : 'none',
187                         'last_updated': 'none'
188                         } )     
189         
190         return results
191     
192     def find(self, record_filter = None, columns=None):
193         # senslab stores its users in an ldap dictionnary
194         # and nodes in a oar scheduller database
195         # both should be interrogated.
196         print >>sys.stderr,"find : ",record_filter
197         if not isinstance(record_filter,dict):
198                 print >>sys.stderr,"find : record_filter is not a dict"
199                 print >>sys.stderr,record_filter.__class__
200                 return []
201         allResults=[]
202         if 'type' in record_filter:
203                 if record_filter['type'] == 'slice':
204                         print >>sys.stderr,"find : don't know how to handle slices yet"
205                         return []
206                 if record_filter['type'] == 'authority':
207                         if  'hrn' in  record_filter and record_filter['hrn']==self.authname:
208                                 return []
209                         else:
210                                 print >>sys.stderr,"find which authority ?"
211                                 return []
212                 if record_filter['type'] == 'user':
213                         return self.ldapFind(record_filter, columns)
214                 if record_filter['type'] == 'node':
215                         return self.ldapFind(record_filter, columns)
216                 else:
217                         print >>sys.stderr,"unknown type to find : ", record_filter['type']
218                         return []
219         else:
220                 allResults = self.ldapFind(record_filter, columns)
221                 allResults+= self.oarFind(record_filter, columns)
222         
223         return allResults
224     
225     def findObjects(self, record_filter = None, columns=None):
226  
227         print >>sys.stderr,"find : ",record_filter
228         #        print record_filter['type']
229         #        if record_filter['type'] in  ['authority']:
230         #            print "findObjectAuthority"
231         results = self.find(record_filter, columns) 
232         result_rec_list = []
233         for result in results:
234                 if result['type'] in ['authority']:
235                         result_rec_list.append(AuthorityRecord(dict=result))
236                 elif result['type'] in ['node']:
237                         result_rec_list.append(NodeRecord(dict=result))
238                 elif result['type'] in ['slice']:
239                         result_rec_list.append(SliceRecord(dict=result))
240                 elif result['type'] in ['user']:
241                         result_rec_list.append(UserRecord(dict=result))
242                 else:
243                         result_rec_list.append(SfaRecord(dict=result))
244         
245         return result_rec_list
246
247
248     def drop(self):
249         return 0
250     
251     def sfa_records_purge(self):
252         return 0
253