def cache_get_key(token): if not can_connect_to_database(): return None try: key = Key.get_by_token(token) except Exception as e: logging.getLogger(__name__).error('No access to table key (error: {})'.format(e)) g.can_connect_to_database = False return None return key
def uncached_get_user(token): logging.getLogger(__name__).debug('Get User from token (uncached)') if not can_connect_to_database(): logging.getLogger(__name__).debug('Cannot connect to database, we set User to None') return None try: user = User.get_from_token(token, datetime.datetime.now()) except Exception as e: logging.getLogger(__name__).error('No access to table User (error: {})'.format(e)) g.can_connect_to_database = False return None return user
def _get_models(self): if app.config['DISABLE_DATABASE']: return None if not can_connect_to_database(): return self.instance_db try: instance_db = models.Instance.get_by_name(self.name) except Exception as e: logging.getLogger(__name__).error('No access to table instance (error: {})'.format(e)) g.can_connect_to_database = False return self.instance_db self.instance_db = instance_db return self.instance_db
def update_from_db(self): if not can_connect_to_database(): self.logger.debug('Database is not accessible') self.last_update = datetime.datetime.utcnow() return if not self.db_configs_getter: return if self.last_update + datetime.timedelta( seconds=self.update_interval) > datetime.datetime.utcnow(): return configs = self.db_configs_getter() self.init(configs) self.last_update = datetime.datetime.utcnow()
def _filter_authorized_instances(self, instances, api): if not instances: return None # During the period database is not accessible, all the instances are valid for the user. if not can_connect_to_database(): return instances user = authentication.get_user(token=authentication.get_token()) valid_instances = [ i for i in instances if authentication.has_access( i.name, abort=False, user=user, api=api) ] if not valid_instances: context = 'User has no access to any instance' authentication.abort_request(user, context) return valid_instances
def get(self, region=None, lon=None, lat=None): region_str = i_manager.get_region(region, lon, lat) response = protobuf_to_dict(i_manager.dispatch( {}, "status", instance_name=region_str), use_enum_labels=True) instance = i_manager.instances[region_str] add_common_status(response, instance) response['status']['parameters'] = instance response['status'][ 'traveler_profiles'] = travelers_profile.TravelerProfile.get_profiles_by_coverage( region_str) logging.getLogger(__name__).info("status reponse: %s", response) response['status']['configuration_database'] = ( 'connected' if can_connect_to_database() else 'no_SQL_connection') return response, 200
def has_access(region, api, abort, user): """ Check the Authorization of the current user for this region and this API. If abort is True, the request is aborted with the appropriate HTTP code. Warning: Please this function is cached therefore it should not be dependent of the request context, so keep it as a pure function. """ # if jormungandr is on public mode or database is not accessible, we skip the authentication process logging.getLogger(__name__).debug('User "has_access" to region/api not cached') if current_app.config.get('PUBLIC', False) or (not can_connect_to_database()): return True if not user: # no user --> no need to continue, we can abort, a user is mandatory even for free region # To manage database error of the following type we should fetch one more time from database # Can connect to database but at least one table/attribute is not accessible due to transaction problem if can_read_user(): context = 'User is undefined, but table users is accessible in database' abort_request(user=user, context=context) else: return True try: model_instance = Instance.get_by_name(region) except Exception as e: logging.getLogger(__name__).error('No access to table Instance (error: {})'.format(e)) g.can_connect_to_database = False return True if not model_instance: if abort: raise RegionNotFound(region) return False if (model_instance.is_free and user.have_access_to_free_instances) or user.has_access( model_instance.id, api ): return True else: if abort: context = 'User has no permission to access this api {} or instance {}'.format( api, model_instance.id ) abort_request(user=user, context=context) else: return False
def update_config(self): if ( self._last_update + datetime.timedelta(seconds=self._update_interval) > datetime.datetime.utcnow() or not self._providers_getter ): return logger = logging.getLogger(__name__) # If database is not accessible we update the value of self._last_update and exit if not can_connect_to_database(): logger.debug('Database is not accessible') self._last_update = datetime.datetime.utcnow() return logger.debug('updating bss providers') self._last_update = datetime.datetime.utcnow() try: providers = self._providers_getter() except Exception as e: logger.exception('No access to table bss_provider (error: {})'.format(e)) # database is not accessible, so let's use the values already present in self._bss_providers and # self._bss_providers_legacy return if not providers: logger.debug('all providers have be disabled') self._bss_providers = {} self._bss_providers_last_update = {} return logger.debug('updating with %s', providers) for provider in providers: # it's a new bss provider or it has been updated, we add it if ( provider.id not in self._bss_providers_last_update or provider.last_update() > self._bss_providers_last_update[provider.id] ): self.update_provider(provider) # remove deleted providers for to_delete in set(self._bss_providers.keys()) - {p.id for p in providers}: del self._bss_providers[to_delete] del self._bss_providers_last_update[to_delete] logger.info('deleting bss provider %s', to_delete)
def get(self): response = { "jormungandr_version": __version__, "regions": [], "bss_providers": [ provider.status() for provider in bss_provider_manager.get_providers() ], } regions = i_manager.get_regions() for key_region in regions: req = request_pb2.Request() instance = i_manager.instances[key_region] req.requested_api = type_pb2.STATUS try: resp = instance.send_and_receive(req, timeout=1000) raw_resp_dict = protobuf_to_dict(resp, use_enum_labels=True) add_common_status(raw_resp_dict, instance) resp_dict = CommonStatusSerializer( raw_resp_dict['status']).data except DeadSocketException: resp_dict = { "status": "dead", "realtime_proxies": [], "error": { "code": "dead_socket", "value": "The region {} is dead".format(key_region) }, } resp_dict['region_id'] = key_region response['regions'].append(resp_dict) # if we use our implementation of redisCache display the status cache_status_op = getattr(cache.cache, "status", None) if callable(cache_status_op): response['redis'] = cache_status_op() response[ 'configuration_database'] = 'connected' if can_connect_to_database( ) else 'no_SQL_connection' return response
def update_config(self): """ Update list of external services from db """ if (self._last_update + datetime.timedelta( seconds=self._update_interval) > datetime.datetime.utcnow() or not self._external_service_getter): return # If database is not accessible we update the value of self._last_update and exit if not can_connect_to_database(): self.logger.debug('Database is not accessible') self._last_update = datetime.datetime.utcnow() return self.logger.debug('Updating external services from db') self._last_update = datetime.datetime.utcnow() try: services = self._external_service_getter() except Exception as e: self.logger.error( 'No access to table external_service (error: {})'.format(e)) # database is not accessible, so let's use the values already present in self._external_services_legacy return if not services: self.logger.debug( 'No external service/All external services disabled in db') self._external_services_last_update = {} return if not self._need_update(services): return # We update all the services if any one of them is updated self._external_services_from_db = {} for service in services: self._update_external_service(service) # If any external service is present in database, service from configuration file in no more used. self._external_services_legacy = self._external_services_from_db
def add_info_newrelic(response, *args, **kwargs): try: record_custom_parameter('navitia-request-id', request.id) if can_connect_to_database(): token = get_token() user = get_user(token=token, abort_if_no_token=False) app_name = get_app_name(token) if user: record_custom_parameter('user_id', str(user.id)) record_custom_parameter('token_name', app_name) record_custom_parameter('version', __version__) coverages = get_used_coverages() if coverages: record_custom_parameter('coverage', coverages[0]) except: logger = logging.getLogger(__name__) logger.exception('error while reporting to newrelic:') return response
def update_config(self): """ Update list of equipment providers from db """ if ( self._last_update + datetime.timedelta(seconds=self._update_interval) > datetime.datetime.utcnow() or not self._providers_getter ): return # If database is not accessible we update the value of self._last_update and exit if not can_connect_to_database(): self.logger.debug('Database is not accessible') self._last_update = datetime.datetime.utcnow() return self.logger.debug('Updating equipment providers from db') self._last_update = datetime.datetime.utcnow() try: providers = self._providers_getter() except Exception as e: self.logger.exception('No access to table equipments_provider (error: {})'.format(e)) # database is not accessible, so let's use the values already present in self._equipment_providers and # self._equipment_providers_legacy return if not providers: self.logger.debug('No providers/All providers disabled in db') self._equipment_providers = {} self._equipment_providers_last_update = {} return for provider in providers: if ( provider.id not in self._equipment_providers_last_update or provider.last_update() > self._equipment_providers_last_update[provider.id] ): self._update_provider(provider)
def make_traveler_profile(cls, coverage, traveler_type): # type: (Text, Text) -> TravelerProfile """ travelers_profile factory method, Return a traveler_profile constructed with default params or params retrieved from db """ if app.config[str( 'DISABLE_DATABASE')] or not can_connect_to_database(): return default_traveler_profiles[traveler_type] # retrieve TravelerProfile from db with coverage and traveler_type # if the record doesn't exist, we use pre-defined default traveler profile try: model = models.TravelerProfile.get_by_coverage_and_type( coverage, traveler_type) except Exception as e: logging.getLogger(__name__).error( 'No access to table traveler_profile (error: {})'.format(e)) # If the table is not accessible return the default traveler profile for given traveler type return default_traveler_profiles[traveler_type] if model is None: return default_traveler_profiles[traveler_type] return cls( traveler_type=traveler_type, walking_speed=model.walking_speed, bike_speed=model.bike_speed, car_speed=model.car_speed, bss_speed=model.bss_speed, wheelchair=model.wheelchair, max_walking_duration_to_pt=model.max_walking_duration_to_pt, max_bss_duration_to_pt=model.max_bss_duration_to_pt, max_bike_duration_to_pt=model.max_bike_duration_to_pt, max_car_duration_to_pt=model.max_car_duration_to_pt, first_section_mode=model.first_section_mode, last_section_mode=model.last_section_mode, is_from_db=True, )
def update_config(self): """ Update list of ridesharing services from db """ if (self._last_update + datetime.timedelta( seconds=self._update_interval) > datetime.datetime.utcnow() or not self._rs_services_getter): return # If database is not accessible we update the value of self._last_update and exit if not can_connect_to_database(): self.logger.debug('Database is not accessible') self._last_update = datetime.datetime.utcnow() return self.logger.debug('Updating ridesharing services from db') self._last_update = datetime.datetime.utcnow() try: services = self._rs_services_getter() except Exception as e: self.logger.exception( 'Failure to retrieve ridesharing services configuration (error: {})' .format(e)) # database is not accessible, so let's use the values already present in self._ridesharing_services_legacy return if not services: self.logger.debug('No ridesharing services available in db') self._ridesharing_services = {} self._ridesharing_services_last_update = {} return for service in services: if (service.id not in self._ridesharing_services_last_update or service.last_update() > self._ridesharing_services_last_update[service.id]): self._update_ridesharing_service(service)
def _can_connect_to_database(self): return can_connect_to_database()