def testApplicationCache(self): a = Application() a.name = "Test App" a.client_secret = "abc" a.id = "test-id" a.created_by = "Nick" a.save() application_cache().load_from_db() self.assertIn(a, application_cache().all())
def authorize(self, request, *args, **kwargs): cfg = general_config() application_object = cfg.application_object if application_object is None: logger.error("You are trying to use the proxy.authentication.AppKeyProvider with no application_object set!") return AuthenticationError(error_code=500, message="Internal error") else: try: obj = application_cache().get(request.REQUEST.get('client_id', "MISSING-CLIENT-ID-xyz")) if obj is None: return AuthenticationError(error_code=401, message="Invalid client_id") return obj except Exception as e: return AuthenticationError(error_code=401, message="Invalid client_id") return AuthenticationError(error_code=401, message="Invalid client_id")
def wrap(request, *args, **kwargs): client_id = request.GET.get('client_id', None) signature = request.GET.get('signature', None) verify = request.GET.get('verify', None) if client_id is None or signature is None or verify is None: return HttpResponse(json.dumps({"error": "422", "message": "required parameter missing"}), content_type="application/json", status=422) try: app = application_cache().get(client_id) except: return HttpResponse(json.dumps({"error": "4031", "message": "auth failure"}), content_type="application/json", status=403) verified_signature = application_hasher(app, verify) if verified_signature != signature: return HttpResponse(json.dumps({"error": "4032", "message": "auth failure"}), content_type="application/json", status=403) return f(request, app, *args, **kwargs)
def init_global_config(general_json_config): if hasattr(general_config(), "configuration_complete"): return start = datetime.now() errors = "no" logger.info(colored("-------------------------------------------------", "green")) logger.info(colored("Face/Off Version %s" % proxy.__version__, "green")) logger.info(colored("I'd like to take his face... off.", "green")) logger.info(colored("-------------------------------------------------", "green")) global_faceoff_config = general_config() global_faceoff_config.domain_name = general_json_config.get('domain_name', None) env_domain = os.environ.get("FACEOFF_DOMAIN_NAME", None) overrode_domain_from_env = False if env_domain is not None: global_faceoff_config.domain_name = env_domain overrode_domain_from_env = True if global_faceoff_config.domain_name is None: logger.warning("No domain name in your config, which could cause problems with facaded APIs that need it") else: logger.info("Configuring Face/Off with domain name: %s, Overrode from FACEOFF_DOMAIN_NAME env variable: %s" % (global_faceoff_config.domain_name, overrode_domain_from_env)) global_faceoff_config.timeout = float(general_json_config.get('timeout', '.500')) logger.info("Configuring Face/Off with timeout of %s" % global_faceoff_config.timeout) application_object_name = general_json_config.get('application_object', None) if application_object_name is not None: try: application_object = load_class_from_name(application_object_name) global_faceoff_config.application_object = application_object logger.info("Configuring Face/Off with application object: %s " % application_object_name) except Exception as e: errors = "some" logger.error("Could not load application object with name %s" % (application_object_name,)) else: global_faceoff_config.application_object = None logger.warning("No application object! Can't use AppKeyProvider authentication handle") analytic_classes = general_json_config.get('analytics', []) analytics = ChainedAnalytics() for analytic_class in analytic_classes: if isinstance(analytic_class, dict): function = analytic_class.get('function') parameters = analytic_class.get('parameters') elif isinstance(analytic_class, str): function = analytic_class parameters = {} else: logger.error("Could not load analytics class with config %s" % (analytic_class,)) errors = "some" continue try: analytics.add_analytic(load_class_from_name(function)(**parameters)) except Exception as e: errors = "some" logger.error("Could not load analytics class with name %s because of %s" % (analytic_class, e)) global_faceoff_config.analytics = analytics logger.info("Configuring Face/Off with analytics classes: %s", analytic_classes) try: user_provider_config = general_json_config.get('user_provider', {}) user_function = user_provider_config.get('function') parameters = user_provider_config.get('parameters', {}) global_faceoff_config.user_provider = load_class_from_name(user_function)(**parameters) logger.info("Configuring Face/Off with user provider config: %s", user_provider_config) except Exception as e: if general_json_config.get('user_provider') is None: logger.warning("There is no user provider class, this means Face/Off can't protect endpoints by consumer keys") else: errors = "some" logger.error("Could not load user provider class with config %s" % (general_json_config.get('user_provider'))) user_provider_servers = general_json_config.get('user_provider_servers', []) global_faceoff_config.user_provider_servers = user_provider_servers health_check = general_json_config.get('health_check_storage', {}).get('implementation', None) if health_check is not None: redis_health_check_config = general_json_config.get('health_check_storage') global_faceoff_config.health_check = load_class_from_name(redis_health_check_config.get('implementation'))(**general_json_config.get('health_check_storage')) else: logger.warning("You have no healthcheck implementation. This probably means you have no healthchecks!") global_faceoff_config.health_check = None health_check_url = general_json_config.get("self_health_check_url", "proxy-check") logger.info("Proxy self-check URL is %s", health_check_url) if "cache" in general_json_config: global_faceoff_config.cache = CacheManager(general_json_config.get('cache')) else: global_faceoff_config.cache = NoCacheManager() application_cache().load_from_db() # fill application_cache logger.info("Application in memory cache loaded with %s applications" % (len(application_cache().all()),)) stop = datetime.now() if errors == "some": color = "red" else: color = "green" global_faceoff_config.configuration_complete = True global_faceoff_config.errors_during_init = errors == "some" logger.info(colored("-------------------------------------------------", color)) logger.info(colored("Face/Off started in %sms with %s errors" % ((stop - start).microseconds, errors), color)) logger.info(colored("-------------------------------------------------", color))
def update_application_cache(sender, **kwargs): application_cache().load_from_db()