def eduid_init_app_no_db(name, config, app_class=AuthnApp): """ Create and prepare the flask app for eduID APIs with all the attributes common to all apps. * Parse and merge configurations * Add logging * Add db connection * Add eduID session :param name: The name of the instance, it will affect the configuration file loaded from the filesystem. :type name: str :param config: any additional configuration settings. Specially useful in test cases :type config: dict :param app_class: The class used to build the flask app. Should be a descendant of flask.Flask :type app_class: type :return: the flask application. :rtype: flask.Flask """ app = app_class(name) app.wsgi_app = ProxyFix(app.wsgi_app) app.request_class = Request # Init etcd config parsers common_parser = EtcdConfigParser('/eduid/webapp/common/') app_parser = EtcdConfigParser('/eduid/webapp/{!s}/'.format(name)) # Load project wide default settings app.config.from_object('eduid_webapp.settings.common') try: # Load optional app specific default settings app.config.from_object( 'eduid_webapp.{!s}.settings.common'.format(name)) except ImportError: # No app specific default config found pass # Load optional project wide settings app.config.update(common_parser.read_configuration(silent=True)) # Load optional app specific settings app.config.update(app_parser.read_configuration(silent=True)) # Load optional init time settings app.config.update(config) # Initialize shared features app = init_logging(app) app = init_exception_handlers(app) app = init_sentry(app) app.session_interface = SessionFactory(app.config) return app
def eduid_init_app_no_db(name, config, app_class=AuthnApp): """ Create and prepare the flask app for eduID APIs with all the attributes common to all apps. * Parse and merge configurations * Add logging * Add db connection * Add eduID session :param name: The name of the instance, it will affect the configuration file loaded from the filesystem. :type name: str :param config: any additional configuration settings. Specially useful in test cases :type config: dict :param app_class: The class used to build the flask app. Should be a descendant of flask.Flask :type app_class: type :return: the flask application. :rtype: flask.Flask """ app = app_class(name) app.wsgi_app = ProxyFix(app.wsgi_app) app.request_class = Request # Init etcd config parsers common_parser = EtcdConfigParser('/eduid/webapp/common/') app_parser = EtcdConfigParser('/eduid/webapp/{!s}/'.format(name)) # Load project wide default settings app.config.from_object('eduid_webapp.settings.common') try: # Load optional app specific default settings app.config.from_object('eduid_webapp.{!s}.settings.common'.format(name)) except ImportError: # No app specific default config found pass # Load optional project wide settings app.config.update(common_parser.read_configuration(silent=True)) # Load optional app specific settings app.config.update(app_parser.read_configuration(silent=True)) # Load optional init time settings app.config.update(config) # Initialize shared features app = init_logging(app) app = init_exception_handlers(app) app = init_sentry(app) app.session_interface = SessionFactory(app.config) return app
def get_etcd_config(namespace: Optional[str] = None) -> FrontConfig: if namespace is None: namespace = '/eduid/webapp/jsapps/' parser = EtcdConfigParser(namespace) config = parser.read_configuration(silent=False) config = {k.lower(): v for k, v in config.items()} return FrontConfig(**config)
def get_config(): parser = EtcdConfigParser('/eduid/webapp/jsapps/') config = parser.read_configuration(silent=True) jsconfig.update(config) return jsconfig
def get_config(): parser = EtcdConfigParser('/eduid/webapp/jsapps/') config = parser.read_configuration(silent=True) jsconfig.update(config) jsconfig['csrf_token'] = session.get_csrf_token() return jsconfig
class TestEtcdParser(unittest.TestCase): def setUp(self): self.etcd_instance = EtcdTemporaryInstance() self.ns = '/test/' self.parser = EtcdConfigParser(namespace=self.ns, host=self.etcd_instance.host, port=self.etcd_instance.port) def tearDown(self): self.etcd_instance.shutdown() def test_write(self): config = { 'test': { 'MY_BOOL': True, 'MY_STRING': 'A value', 'MY_LIST': ['One', 'Two', 3], 'MY_DICT': {'A': 'B'} } } self.parser.write_configuration(config) self.assertEqual(self.parser.get('MY_BOOL'), True) self.assertEqual(self.parser.get('MY_STRING'), 'A value') self.assertEqual(self.parser.get('MY_LIST'), ['One', 'Two', 3]) self.assertEqual(self.parser.get('MY_DICT'), {'A': 'B'}) def test_read(self): config = { 'test': { 'MY_BOOL': True, 'MY_STRING': 'A value', 'MY_LIST': ['One', 'Two', 3], 'MY_DICT': {'A': 'B'} } } self.parser.write_configuration(config) read_config = self.parser.read_configuration() self.assertEqual(config['test'], read_config) def test_set_get(self): self.parser.set('MY_SET_KEY', 'a nice value') self.assertEqual(self.parser.get('MY_SET_KEY'), 'a nice value') read_config = self.parser.read_configuration() self.assertEqual({'MY_SET_KEY': 'a nice value'}, read_config) def test_uppercase(self): config = { 'test': { 'my_bool': True, 'my_string': 'A value', 'my_list': ['One', 'Two', 3], 'my_dict': {'A': 'B'} } } self.parser.write_configuration(config) read_config = self.parser.read_configuration() for key in config['test'].keys(): self.assertIn(key.upper(), read_config.keys()) self.parser.set('my_set_key', 'a nice value') self.assertEqual(self.parser.get('MY_SET_KEY'), 'a nice value')
class TestEtcdParser(unittest.TestCase): def setUp(self): self.etcd_instance = EtcdTemporaryInstance() self.ns = '/test/' self.parser = EtcdConfigParser(namespace=self.ns, host=self.etcd_instance.host, port=self.etcd_instance.port) def tearDown(self): self.etcd_instance.shutdown() def test_write(self): config = { 'test': { 'MY_BOOL': True, 'MY_STRING': 'A value', 'MY_LIST': ['One', 'Two', 3], 'MY_DICT': { 'A': 'B' } } } self.parser.write_configuration(config) self.assertEqual(self.parser.get('MY_BOOL'), True) self.assertEqual(self.parser.get('MY_STRING'), 'A value') self.assertEqual(self.parser.get('MY_LIST'), ['One', 'Two', 3]) self.assertEqual(self.parser.get('MY_DICT'), {'A': 'B'}) def test_read(self): config = { 'test': { 'MY_BOOL': True, 'MY_STRING': 'A value', 'MY_LIST': ['One', 'Two', 3], 'MY_DICT': { 'A': 'B' } } } self.parser.write_configuration(config) read_config = self.parser.read_configuration() self.assertEqual(config['test'], read_config) def test_set_get(self): self.parser.set('MY_SET_KEY', 'a nice value') self.assertEqual(self.parser.get('MY_SET_KEY'), 'a nice value') read_config = self.parser.read_configuration() self.assertEqual({'MY_SET_KEY': 'a nice value'}, read_config) def test_uppercase(self): config = { 'test': { 'my_bool': True, 'my_string': 'A value', 'my_list': ['One', 'Two', 3], 'my_dict': { 'A': 'B' } } } self.parser.write_configuration(config) read_config = self.parser.read_configuration() for key in config['test'].keys(): self.assertIn(key.upper(), read_config.keys()) self.parser.set('my_set_key', 'a nice value') self.assertEqual(self.parser.get('MY_SET_KEY'), 'a nice value')