attempt to retrieve peer gid within a try/except block
[sfa.git] / sfa / server / registry.py
1 #
2 # Registry is a SfaServer that implements the Registry interface
3 #
4 ### $Id$
5 ### $URL$
6 #
7
8 import tempfile
9 import os
10 import time
11 import sys
12
13 from sfa.util.server import SfaServer
14 from sfa.util.faults import *
15 from sfa.util.storage import *
16 from sfa.trust.gid import GID
17 from sfa.util.table import SfaTable
18 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
19 import sfa.util.soapprotocol as soapprotocol
20  
21 # GeniLight client support is optional
22 try:
23     from egeni.geniLight_client import *
24 except ImportError:
25     GeniClientLight = None            
26
27 ##
28 # Registry is a SfaServer that serves registry and slice operations at PLC.
29 class Registry(SfaServer):
30     ##
31     # Create a new registry object.
32     #
33     # @param ip the ip address to listen on
34     # @param port the port to listen on
35     # @param key_file private key filename of registry
36     # @param cert_file certificate filename containing public key (could be a GID file)
37     
38     def __init__(self, ip, port, key_file, cert_file):
39         SfaServer.__init__(self, ip, port, key_file, cert_file)
40         self.server.interface = 'registry' 
41
42
43 ##
44 # Registries is a dictionary of registry connections keyed on the registry
45 # hrn
46
47 class Registries(dict):
48
49     default_fields = {
50         'hrn': '',
51         'addr': '', 
52         'port': '', 
53     }
54
55     def __init__(self, api, file = "/etc/sfa/registries.xml"):
56         dict.__init__(self, {})
57         self.api = api
58         
59         # create default connection dict
60         registries_dict = {'registries': {'registry': [default_fields]}}
61
62         # load config file
63         self.registry_info = XmlStorage(file, registries_dict)
64         self.registry_info.load()
65         self.interfaces = self.registry_info['registries']['registry']
66         if not isinstance(self.interfaces, list):
67             self.interfaces = [self.interfaces]
68         
69         # Attempt to get any missing peer gids
70         # There should be a gid file in /etc/sfa/trusted_roots for every
71         # peer registry found in in the registries.xml config file. If there
72         # are any missing gids, request a new one from the peer registry.
73         gids_current = self.api.auth.trusted_cert_list.get_list()
74         hrns_current = [gid.get_hrn() for gid in gids_found] 
75         hrns_expected = self.interfaces.keys()
76         new_hrns = set(hrns_current).difference(hrns_expected)
77         
78         self.get_peer_gids(new_hrns)
79
80         # update the local db records for these registries
81         self.update_db_records()
82         
83         # create connections to the registries
84         self.update(self.get_connections(interfaces))
85
86     def get_peer_gids(self, new_hrns):
87         """
88         Install trusted gids from the specified interfaces.  
89         """
90         if not new_hrns:
91             return
92
93         trusted_certs_dir = self.api.config.get_trustedroots_dir()
94         for new_hrn in new_hrns:
95             try:
96                 # get gid from the registry
97                 registry = self.get_connections(self.interfaces[new_hrn])[new_hrn]
98                 trusted_gids = registry.get_trusted_certs()
99                 # default message
100                 message = "interface: registry\tunable to retrieve and install trusted gid for %s" % new_hrn 
101                 if trusted_gids:
102                     # the gid we want shoudl be the first one in the list, but lets 
103                     # make sure
104                     for trusted_gid in trusted_gids:
105                         gid = GID(string=trusted_gids[0])
106                         if gid.get_hrn() == new_hrn:
107                             gid_filename = os.path.join(trusted_certs_dir, '%s.gid' % new_hrn)
108                             gid.save_to_file(gid_filename, save_parents=True)
109                             message = "interface: registry\tinstalled trusted gid for %s" % \
110                                 (new_hrn)
111                 # log the message
112                 self.api.logger.info(message)
113             except:
114                 message = "interface: registry\tunable to retrieve and install trusted gid for %s" % new_hrn 
115                 self.api.logger.info(message)
116         
117         # reload the trusted certs list
118         self.api.auth.load_trusted_certs()
119
120     def update_db_records(self):
121         """
122         Make sure there is a record in the local db for allowed registries
123         defined in the config file (registries.xml). Removes old records from
124         the db.         
125         """
126         # get hrns we expect to find
127         hrns_expected = self.interfaces.keys()
128
129         # get hrns that actually exist in the db
130         table = SfaTable()
131         records = table.find({'type': 'sa'})
132         hrns_found = [record['hrn'] for record in records]
133         
134         # remove old records
135         for record in records:
136             if record['hrn'] not in hrns_expected:
137                 table.remove(record)
138
139         # add new records
140         for hrn in hrns_expected:
141             if hrn not in hrns_found:
142                 record = {
143                     'hrn': hrn,
144                     'type': 'sa',
145                 }
146             table.insert(record)
147                         
148  
149     def get_connections(self, registries):
150         """
151         read connection details for the trusted peer registries from file return 
152         a dictionary of connections keyed on interface hrn. 
153         """
154         connections = {}
155         required_fields = self.default_fields.keys()
156         if not isinstance(registries, []):
157             registries = [registries]
158         for registry in registries:
159             # make sure the required fields are present and not null
160             for key in required_fields
161                 if not registry.get(key):
162                     continue 
163             hrn, address, port = registry['hrn'], registry['addr'], registry['port']
164             url = 'http://%(address)s:%(port)s' % locals()
165             # check which client we should use
166             # sfa.util.xmlrpcprotocol is default
167             client_type = 'xmlrpcprotocol'
168             if registry.has_key('client') and \
169                registry['client'] in ['geniclientlight'] and \
170                GeniClientLight:
171                 client_type = 'geniclientlight'
172                 connections[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file) 
173             else:
174                 connections[hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)
175
176         # set up a connection to the local registry
177         address = self.api.config.SFA_REGISTRY_HOST
178         port = self.api.config.SFA_REGISTRY_PORT
179         url = 'http://%(address)s:%(port)s' % locals()
180         local_registry = {'hrn': self.api.hrn, 'addr': address, 'port': port}
181         connections[self.api.hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)            
182         return connections