def doSaveMyName(self, *args, **kwargs): """ Action method. """ login = kwargs['username'] self.preferred_servers = [ s.strip() for s in kwargs.get('preferred_servers', []) ] if not self.known_servers: self.known_servers = known_servers.by_host() if not self.preferred_servers: try: for srv in str(config.conf().getData( 'services/identity-propagate/preferred-servers') ).split(','): if srv.strip(): self.preferred_servers.append(srv.strip()) except: pass self.min_servers = max( settings.MinimumIdentitySources(), config.conf().getInt('services/identity-propagate/min-servers') or settings.MinimumIdentitySources()) self.max_servers = min( settings.MaximumIdentitySources(), config.conf().getInt('services/identity-propagate/max-servers') or settings.MaximumIdentitySources()) lg.out(4, 'id_registrator.doSaveMyName [%s]' % login) lg.out(4, ' known_servers=%s' % self.known_servers) lg.out(4, ' preferred_servers=%s' % self.preferred_servers) lg.out(4, ' min_servers=%s' % self.min_servers) lg.out(4, ' max_servers=%s' % self.max_servers) bpio.WriteTextFile(settings.UserNameFilename(), login)
def _is_healthy(self, ping_results): # TODO: for now simply check that first identity server in my identity is alive # we can implement more "aggressive" checks later... # for example if any of my sources is not alive -> replace him also # also we can check here if I have enough sources according to those configs: # services/identity-propagate/min-servers # services/identity-propagate/max-servers if not ping_results: return False if not bool(ping_results[0]): return False unique_idurls = [] for idurl in ping_results: if idurl not in unique_idurls: unique_idurls.append(idurl) min_servers = max( settings.MinimumIdentitySources(), config.conf().getInt('services/identity-propagate/min-servers') or settings.MinimumIdentitySources(), ) max_servers = min( settings.MaximumIdentitySources(), config.conf().getInt('services/identity-propagate/max-servers') or settings.MaximumIdentitySources(), ) if _Debug: lg.args(_DebugLevel, min_servers=min_servers, max_servers=max_servers, ping_results=ping_results, unique_idurls=unique_idurls) if len(unique_idurls) != len(ping_results): return False if len(unique_idurls) < min_servers: return False if len(unique_idurls) > max_servers: return False return True
def doSaveMyName(self, arg): """ Action method. """ try: login = arg['username'] except: login = arg[0] if len(arg) > 1: self.preferred_servers = map(lambda s: s.strip(), arg[1].split(',')) if not self.known_servers: self.known_servers = known_servers.by_host() if not self.preferred_servers: try: from main import config for srv in str(config.conf().getData('services/identity-propagate/preferred-servers')).split(','): if srv.strip(): self.preferred_servers.append(srv.strip()) except: pass self.min_servers = max( settings.MinimumIdentitySources(), config.conf().getInt('services/identity-propagate/min-servers') or settings.MinimumIdentitySources()) self.max_servers = min( settings.MaximumIdentitySources(), config.conf().getInt('services/identity-propagate/max-servers') or settings.MaximumIdentitySources()) lg.out(4, 'id_registrator.doSaveMyName [%s]' % login) lg.out(4, ' known_servers=%s' % self.known_servers) lg.out(4, ' preferred_servers=%s' % self.preferred_servers) lg.out(4, ' min_servers=%s' % self.min_servers) lg.out(4, ' max_servers=%s' % self.max_servers) bpio.WriteFile(settings.UserNameFilename(), login)
def isCorrect(self): """ Do some checking on the object fields. """ if len(self.contacts) == 0: return False if len(self.sources) == 0: return False if not self.publickey: return False if not self.signature: return False if not self.revision: return False original_sources = self.getSources(as_originals=True) if len(set(original_sources)) != len(self.sources): lg.warn('original identity sources are duplicated: %r' % original_sources) return False if len(original_sources) > settings.MaximumIdentitySources(): lg.warn('too much sources') return False if len(original_sources) < settings.MinimumIdentitySources(): lg.warn('too few sources') return False try: int(self.revision) except: lg.warn('identity revision: %s' % self.revision) return False names = set() for source in original_sources: if not source: lg.warn('found empty source') return False proto, host, port, filename = nameurl.UrlParse(source) if filename.count('/'): lg.warn("incorrect identity name: %s" % filename) return False name, justxml = filename.split('.') names.add(name) # SECURITY check that name is simple if justxml != "xml": lg.warn("incorrect identity name: %s" % filename) return False if len(name) > settings.MaximumUsernameLength(): lg.warn("incorrect identity name: %s" % filename) return False if len(name) < settings.MinimumUsernameLength(): lg.warn("incorrect identity name: %s" % filename) return False for c in name: if c not in settings.LegalUsernameChars(): lg.warn("incorrect identity name: %s" % filename) return False if len(names) > 1: lg.warn('names are not consistent: %s' % str(names)) return False return True
def isCorrect(self): """ Do some checking on the object fields. """ if len(self.contacts) == 0: return False if len(self.sources) == 0: return False if self.publickey == '': return False if self.signature == '': return False if self.revision == '': return False if len(self.sources) > settings.MaximumIdentitySources(): lg.warn('too much sources') return False if len(self.sources) < settings.MinimumIdentitySources(): lg.warn('too few sources') return False try: int(self.revision) except: lg.warn('identity revision: %s' % self.revision) return False names = set() for source in self.sources: proto, host, port, filename = nameurl.UrlParse(source) if filename.count('/'): lg.warn("identity name: %s" % filename) return False name, justxml = filename.split('.') names.add(name) # SECURITY check that name is simple if justxml != "xml": lg.warn("identity name: %s" % filename) return False if len(name) > settings.MaximumUsernameLength(): lg.warn("identity name: %s" % filename) return False if len(name) < settings.MinimumUsernameLength(): lg.warn("identity name: %s" % filename) return False for c in name: if c not in settings.LegalUsernameChars(): lg.warn("identity name: %s" % filename) return False if len(names) > 1: lg.warn('names are not consistant: %s' % str(names)) return False return True
def doRebuildMyIdentity(self, *args, **kwargs): """ Action method. """ min_servers = max( settings.MinimumIdentitySources(), config.conf().getInt('services/identity-propagate/min-servers') or settings.MinimumIdentitySources(), ) max_servers = min( settings.MaximumIdentitySources(), config.conf().getInt('services/identity-propagate/max-servers') or settings.MaximumIdentitySources(), ) current_sources = my_id.getLocalIdentity().getSources( as_originals=True) new_sources = [] new_idurl = args[0] if _Debug: lg.args( _DebugLevel, current_sources=current_sources, alive_idurls=self.alive_idurls, new_idurl=new_idurl, ) # first get rid of "dead" sources for current_idurl in current_sources: if current_idurl not in self.alive_idurls: continue new_sources.append(current_idurl) if self.force and len(new_sources) == len(current_sources): # do not increase number of identity sources, only rotate them new_sources.pop(0) # and add new "good" source to the end of the list if new_idurl and new_idurl not in new_sources: new_sources.append(new_idurl) if _Debug: lg.args(_DebugLevel, new_sources=new_sources, min_servers=min_servers, max_servers=max_servers) if len(new_sources) > max_servers: all_new_sources = list(new_sources) new_sources = new_sources[max(0, len(new_sources) - max_servers):] lg.warn('skip %d identity sources, require maximum %d sources' % ( len(all_new_sources) - len(new_sources), max_servers, )) if len(new_sources) < min_servers: additional_sources = self.possible_sources[:min_servers - len(new_sources)] if additional_sources: lg.warn('additional sources to be used: %r' % additional_sources) new_sources.extend(additional_sources) if len(new_sources) < min_servers: lg.warn('not enough identity sources, need to rotate again') self.automat('need-more-sources') return if _Debug: lg.args(_DebugLevel, new_sources=new_sources) my_id.rebuildLocalIdentity( new_sources=new_sources, new_revision=self.new_revision, ) self.rotated = True self.automat('my-id-updated')