def test_indent_value_error(self, log): logger_initial_config(service_name='response-operations-ui') logger = wrap_logger(logging.getLogger()) logger.error('Test') message = log.records[0].msg self.assertIn( '"event": "Test", "severity": "error", "level": "error",' ' "service": "response-operations-ui"', message)
def test_indent_type_error(self, log): os.environ['JSON_INDENT_LOGGING'] = 'abc' logger_initial_config(service_name='response-operations-ui') logger = wrap_logger(logging.getLogger()) logger.error('Test') message = log.records[0].msg self.assertIn( '"event": "Test", "severity": "error", "level": "error",' ' "service": "response-operations-ui"', message)
def test_success(self, log): os.environ['JSON_INDENT_LOGGING'] = '1' logger_initial_config(service_name='response-operations-ui') logger = wrap_logger(logging.getLogger()) logger.error('Test') message = log.records[0].msg message_contents = '\n "event": "Test",\n "severity": "error",'\ '\n "level": "error",\n "service": "response-operations-ui"' self.assertIn(message_contents, message)
def test_success(self, log): os.environ["JSON_INDENT_LOGGING"] = "1" logger_initial_config(service_name="response-operations-ui") logger = wrap_logger(logging.getLogger()) logger.error("Test") message = log.records[0].msg message_contents = ( '\n "event": "Test",\n "severity": "error",' '\n "level": "error",\n "service": "response-operations-ui"' ) self.assertIn(message_contents, message)
def create_app(config_name=None): csp_policy = copy.deepcopy(CSP_POLICY) app = Flask(__name__) app_config = f'config.{config_name or os.environ.get("APP_SETTINGS", "Config")}' app.config.from_object(app_config) if Config.WTF_CSRF_ENABLED: Talisman( app, content_security_policy=csp_policy, content_security_policy_nonce_in=["script-src"], force_https=False, # this is handled at the load balancer strict_transport_security=True, strict_transport_security_max_age=ONE_YEAR_IN_SECONDS, referrer_policy=DEFAULT_REFERRER_POLICY, frame_options="SAMEORIGIN", frame_options_allow_from=None, session_cookie_secure=True, session_cookie_http_only=True, ) app.name = "response_operations_ui" csrf = CSRFProtect(app) # Load css and js assets assets = Environment(app) if app.config["DEBUG"] or app.config["TESTING"]: assets.cache = False assets.manifest = None if not app.config["DEBUG"]: app.wsgi_app = GCPLoadBalancer(app.wsgi_app) assets.url = app.static_url_path app.jinja_env.add_extension("jinja2.ext.do") app.jinja_env.globals["hasPermission"] = user_has_permission app.register_blueprint(filter_blueprint) app.url_map.strict_slashes = False app.secret_key = app.config["RESPONSE_OPERATIONS_UI_SECRET"] logger_initial_config(service_name="response-operations-ui", log_level=app.config["LOGGING_LEVEL"]) logger = wrap_logger(logging.getLogger(__name__)) logger.info("Logger created", log_level=app.config["LOGGING_LEVEL"]) login_manager = LoginManager(app) login_manager.init_app(app) login_manager.login_view = "sign_in_bp.sign_in" @app.errorhandler(CSRFError) def handle_csrf_error(e): flash("Your session timed out", category="info") return redirect(url_for("logout_bp.logout")) @app.before_request def before_request(): session.permanent = True # set session to use PERMANENT_SESSION_LIFETIME session.modified = True # reset the session timer on every request try: csrf.protect() except Exception as e: if e.code == 400: logger.warning(e.description) logger.warning(e) @app.context_processor def inject_availability_message(): redis_avail_msg = app.config["SESSION_REDIS"] if len(redis_avail_msg.keys("AVAILABILITY_MESSAGE_RES_OPS")) == 1: return { "availability_message": redis_avail_msg.get("AVAILABILITY_MESSAGE_RES_OPS").decode( "utf-8") } return {} @login_manager.user_loader def user_loader(user_id): username = session.get("username") return User(user_id, username) # wrap in the flask server side session manager and back it by redis app.config["SESSION_REDIS"] = redis.StrictRedis( host=app.config["REDIS_HOST"], port=app.config["REDIS_PORT"], db=app.config["REDIS_DB"]) app.jinja_environment.lstrip_blocks = True if app.config["DEBUG"] or os.environ.get("JINJA_RELOAD"): app.jinja_env.auto_reload = True Session(app) setup_blueprints(app) return app
def create_app(config_name=None): csp_policy = copy.deepcopy(CSP_POLICY) app = Flask(__name__) app_config = f'config.{config_name or os.environ.get("APP_SETTINGS", "Config")}' app.config.from_object(app_config) if Config.WTF_CSRF_ENABLED: Talisman( app, content_security_policy=csp_policy, content_security_policy_nonce_in=['script-src'], force_https=False, # this is handled at the load balancer legacy_content_security_policy_header=True, strict_transport_security=True, strict_transport_security_max_age=ONE_YEAR_IN_SECONDS, referrer_policy=DEFAULT_REFERRER_POLICY, frame_options='SAMEORIGIN', frame_options_allow_from=None, session_cookie_secure=True, session_cookie_http_only=True) app.name = "response_operations_ui" CSRFProtect(app) # Load css and js assets assets = Environment(app) if app.config['DEBUG'] or app.config['TESTING']: assets.cache = False assets.manifest = None if not app.config['DEBUG']: app.wsgi_app = GCPLoadBalancer(app.wsgi_app) assets.url = app.static_url_path app.jinja_env.add_extension('jinja2.ext.do') app.register_blueprint(filter_blueprint) app.url_map.strict_slashes = False app.secret_key = app.config['RESPONSE_OPERATIONS_UI_SECRET'] logger_initial_config(service_name='response-operations-ui', log_level=app.config['LOGGING_LEVEL']) logger = wrap_logger(logging.getLogger(__name__)) logger.info('Logger created', log_level=app.config['LOGGING_LEVEL']) login_manager = LoginManager(app) login_manager.init_app(app) login_manager.login_view = "sign_in_bp.sign_in" @app.context_processor def inject_availability_message(): redis_avail_msg = app.config['SESSION_REDIS'] if len(redis_avail_msg.keys('AVAILABILITY_MESSAGE_RES_OPS')) == 1: return { "availability_message": redis_avail_msg.get('AVAILABILITY_MESSAGE_RES_OPS').decode('utf-8') } return {} @login_manager.user_loader def user_loader(user_id): username = session.get('username') return User(user_id, username) # wrap in the flask server side session manager and back it by redis app.config['SESSION_REDIS'] = redis.StrictRedis(host=app.config['REDIS_HOST'], port=app.config['REDIS_PORT'], db=app.config['REDIS_DB']) app.jinja_environment.lstrip_blocks = True if app.config['DEBUG'] or os.environ.get('JINJA_RELOAD'): app.jinja_env.auto_reload = True Session(app) setup_blueprints(app) return app
def create_app(config_name=None): app = Flask(__name__) app.name = "response_operations_ui" app_config = f'config.{config_name or os.environ.get("APP_SETTINGS", "Config")}' app.config.from_object(app_config) # Load css and js assets assets = Environment(app) if app.config['DEBUG'] or app.config['TESTING']: assets.cache = False assets.manifest = None assets.url = app.static_url_path app.jinja_env.add_extension('jinja2.ext.do') app.register_blueprint(filter_blueprint) app.url_map.strict_slashes = False app.secret_key = app.config['RESPONSE_OPERATIONS_UI_SECRET'] # Zipkin zipkin = Zipkin(app=app, sample_rate=app.config.get("ZIPKIN_SAMPLE_RATE")) requestsdefaulter.default_headers(zipkin.create_http_headers_for_new_span) logger_initial_config(service_name='response-operations-ui', log_level=app.config['LOGGING_LEVEL']) logger = wrap_logger(logging.getLogger(__name__)) logger.info('Logger created', log_level=app.config['LOGGING_LEVEL']) login_manager = LoginManager(app) login_manager.init_app(app) login_manager.login_view = "sign_in_bp.sign_in" @app.context_processor def inject_availability_message(): redis_avail_msg = app.config['SESSION_REDIS'] if len(redis_avail_msg.keys('AVAILABILITY_MESSAGE_RES_OPS')) == 1: return { "availability_message": redis_avail_msg.get('AVAILABILITY_MESSAGE_RES_OPS').decode( 'utf-8') } return {} @login_manager.user_loader def user_loader(user_id): return User(user_id) if cf.detected: with app.app_context(): # If deploying in cloudfoundry set config to use cf redis instance logger.info( 'Cloudfoundry detected, setting service configurations') service = cf.redis app.config['REDIS_HOST'] = service.credentials['host'] app.config['REDIS_PORT'] = service.credentials['port'] # wrap in the flask server side session manager and back it by redis app.config['SESSION_REDIS'] = redis.StrictRedis( host=app.config['REDIS_HOST'], port=app.config['REDIS_PORT'], db=app.config['REDIS_DB']) app.jinja_environment.lstrip_blocks = True if app.config['DEBUG'] or os.environ.get('JINJA_RELOAD'): app.jinja_env.auto_reload = True Session(app) setup_blueprints(app) return app