def test_public_url_gets_redirect(self): try: global S3Storage from depot.io.awss3 import S3Storage except ImportError: raise SkipTest('Boto not installed') env = os.environ access_key_id = env.get('AWS_ACCESS_KEY_ID') secret_access_key = env.get('AWS_SECRET_ACCESS_KEY') if access_key_id is None or secret_access_key is None: raise SkipTest('Amazon S3 credentials not available') PID = os.getpid() NODE = str(uuid.uuid1()).rsplit('-', 1)[-1] # Travis runs multiple tests concurrently default_bucket_name = 'filedepot-%s' % (access_key_id.lower(), ) cred = (access_key_id, secret_access_key) bucket_name = 'filedepot-testfs-%s-%s-%s' % (access_key_id.lower(), NODE, PID) DepotManager.configure('awss3', {'depot.backend': 'depot.io.awss3.S3Storage', 'depot.access_key_id': access_key_id, 'depot.secret_access_key': secret_access_key, 'depot.bucket': bucket_name}) DepotManager.set_default('awss3') app = self.make_app() new_file = app.post('/create_file').json file_path = DepotManager.url_for('%(uploaded_to)s/%(last)s' % new_file) uploaded_file = app.get(file_path) assert uploaded_file.body == FILE_CONTENT, uploaded_file
def configure_filedepot(settings: Dict[str, str]) -> None: config = extract_depot_settings("kotti.depot.", settings) for conf in config: name = conf.pop("name") if name not in DepotManager._depots: DepotManager.configure(name, conf, prefix="")
def configure_filedepot(settings): config = extract_depot_settings('kotti.depot.', settings) for conf in config: name = conf.pop('name') if name not in DepotManager._depots: DepotManager.configure(name, conf, prefix='')
def setup(): setup_database() DepotManager._clear() DepotManager.configure('default', {'depot.storage_path': './lfs'}) DepotManager.configure('another', {'depot.storage_path': './lfs'}) DepotManager.alias('another_alias', 'another') DepotManager.make_middleware(None)
def setup(): setup_database() DepotManager._clear() DepotManager.configure("default", {"depot.storage_path": "./lfs"}) DepotManager.configure("another", {"depot.storage_path": "./lfs"}) DepotManager.alias("another_alias", "another") DepotManager.make_middleware(None)
def test_prevent_configuring_two_storages_with_same_name(self): DepotManager.configure('first', {'depot.storage_path': './lfs'}) try: DepotManager.configure('first', {'depot.storage_path': './lfs2'}) except RuntimeError: pass else: assert False, 'Should have raised RunetimeError here'
def configure_filedepot(self) -> None: # TODO - G.M - 2018-08-08 - [GlobalVar] Refactor Global var # of tracim_backend, Be careful DepotManager is a Singleton ! depot_storage_name = self.DEPOT_STORAGE_NAME depot_storage_path = self.DEPOT_STORAGE_DIR depot_storage_settings = {"depot.storage_path": depot_storage_path} DepotManager.configure(depot_storage_name, depot_storage_settings)
def configure_depot(): """Configure Depot.""" depot_storage_name = context.config.get_main_option('depot_storage_name') depot_storage_path = context.config.get_main_option('depot_storage_dir') depot_storage_settings = {'depot.storage_path': depot_storage_path} DepotManager.configure( depot_storage_name, depot_storage_settings, )
def configure_depot(): """Configure Depot.""" depot_storage_name = CFG.get_instance().DEPOT_STORAGE_NAME depot_storage_path = CFG.get_instance().DEPOT_STORAGE_DIR depot_storage_settings = {'depot.storage_path': depot_storage_path} DepotManager.configure( depot_storage_name, depot_storage_settings, )
def depot(temporary_directory): DepotManager.configure('default', { 'depot.backend': 'depot.io.local.LocalFileStorage', 'depot.storage_path': temporary_directory }) yield DepotManager.get() DepotManager._clear()
def configure_filedepot(settings): from kotti.util import extract_depot_settings from depot.manager import DepotManager config = extract_depot_settings("kotti.depot.", settings) for conf in config: name = conf.pop("name") if name not in DepotManager._depots: DepotManager.configure(name, conf, prefix="")
def configure_filedepot(settings): from kotti.util import extract_depot_settings from depot.manager import DepotManager config = extract_depot_settings('kotti.depot.', settings) for conf in config: name = conf.pop('name') if name not in DepotManager._depots: DepotManager.configure(name, conf, prefix='')
def depot(temporary_directory): DepotManager.configure( 'default', { 'depot.backend': 'depot.io.local.LocalFileStorage', 'depot.storage_path': temporary_directory }) yield DepotManager.get() DepotManager._clear()
def test_aliases(self): DepotManager.configure('first', {'depot.storage_path': './lfs'}) DepotManager.configure('second', {'depot.storage_path': './lfs2'}) DepotManager.alias('used_storage', 'first') storage = DepotManager.get('used_storage') assert storage.storage_path == './lfs', storage DepotManager.alias('used_storage', 'second') storage = DepotManager.get('used_storage') assert storage.storage_path == './lfs2', storage
def configure_filedepot(self): # TODO - G.M - 2018-08-08 - [GlobalVar] Refactor Global var # of tracim_backend, Be careful DepotManager is a Singleton ! depot_storage_name = self.DEPOT_STORAGE_NAME depot_storage_path = self.DEPOT_STORAGE_DIR depot_storage_settings = {'depot.storage_path': depot_storage_path} DepotManager.configure( depot_storage_name, depot_storage_settings, )
def post(self): """ .. http:post:: /products This api will be used to send sku_data_import task to queue **Example request**: .. sourcecode:: http POST /products HTTP/1.1 HEADERS File **Example response**: { "message": "SUCCESS" } .. sourcecode:: http HTTP/1.1 200 OK Vary: Accept :statuscode 200: success :statuscode 400: bad request error """ products_file = request.files["products_csv"] products_file_object = products_file.read() if not DepotManager._default_depot: DepotManager.configure( 'default', { 'depot.backend': 'depot.io.boto3.S3Storage', 'depot.access_key_id': config.get('AWS_ACCESS_KEY', None), 'depot.secret_access_key': config.get( 'AWS_SECRET_KEY', None), 'depot.bucket': 'fulfilio-files', 'depot.region_name': 'eu-central-1' }) csv_object = BulkCSVUploadMethods.create_record( dict(csv=products_file_object)) db.commit() send_csv_import_task(csv_object.id) db.commit() return make_response(render_template('operations.html'), 200)
def configure_database(self): """ Database configuration should be set here """ self.NS = NS self.NS.local = rdflib.Namespace(self.config['lod_prefix'] + '/') self.admin_db = database.engine_from_config(self.config, "admin_") self.db = database.engine_from_config(self.config, "knowledge_") self.db.app = self load_namespaces(self.db, locals()) Resource.db = self.admin_db self.vocab = Graph() #print URIRef(self.config['vocab_file']) self.vocab.load(open("default_vocab.ttl"), format="turtle") self.vocab.load(open(self.config['vocab_file']), format="turtle") self.role_api = ld.LocalResource(self.NS.prov.Role, "role", self.admin_db.store, self.vocab, self.config['lod_prefix'], RoleMixin) self.Role = self.role_api.alchemy self.user_api = ld.LocalResource(self.NS.prov.Agent, "user", self.admin_db.store, self.vocab, self.config['lod_prefix'], UserMixin) self.User = self.user_api.alchemy self.nanopub_api = ld.LocalResource(self.NS.np.Nanopublication, "pub", self.db.store, self.vocab, self.config['lod_prefix'], name="Graph") self.Nanopub = self.nanopub_api.alchemy self.classes = mapper(self.Role, self.User) self.datastore = RDFAlchemyUserDatastore(self.admin_db, self.classes, self.User, self.Role) self.security = Security(self, self.datastore, register_form=ExtendedRegisterForm) #self.mail = Mail(self) DepotManager.configure('nanopublications', self.config['nanopub_archive']) DepotManager.configure('files', self.config['file_archive']) self.file_depot = DepotManager.get('files')
def enable_depot(): import logging log = logging.getLogger('molepaw.depot') # DEPOT setup from depot.manager import DepotManager storages = { 'category_images': 'category_image', } for storage in storages: prefix = 'depot.%s.' % storage log.info('Configuring Storage %s*', prefix) DepotManager.configure(storage, tg.config, prefix) DepotManager.alias(storages[storage], storage)
def config_ready(): """ Executed once the configuration is ready. """ # don't run when setting up the database if 'setup-app' in sys.argv: return import tgscheduler tgscheduler.start_scheduler() # Configure default depot DepotManager.configure('default', tg.config) # Schedule up some syncing with pipermail from mozzarella.lib.pipermailsync import pmsync tgscheduler.scheduler.add_interval_task(action=pmsync, initialdelay=0, interval=5 * 60)
def enable_depot(app): import logging log = logging.getLogger('stroller2.depot') # DEPOT setup from depot.manager import DepotManager storages = { 'product_images': 'product_image' } for storage in storages: prefix = 'depot.%s.' % storage log.info('Configuring Storage %s*', prefix) DepotManager.configure(storage, tg.config, prefix) DepotManager.alias(storages[storage], storage) return app
def init_app(self, app): storage_conf = app.config.get("DEPOT_MEDIA_STORAGES", {}) if not storage_conf: raise LookupError(""" Media storage config is required when using the media extension. Please add `DEPOT_MEDIA_STORAGES` to the app config, """) if "media_storage" not in storage_conf: raise ValueError( "A storage named *media_storage* is required when configuring `DEPOT_STORAGES`." ) for name, opts in storage_conf.items(): if name not in DepotManager._depots: DepotManager.configure(name, opts) for storage in ("image_storage", "document_storage"): if storage not in DepotManager._depots: DepotManager.alias(storage, "media_storage") if app.config.get("SERVE_MEDIA_FILES"): self._add_serving_routes(app)
def create_depot(self): config = { 'depot.backend': self.depot_backend } if self.depot_backend.endswith('LocalFileStorage'): path = self.bound_storage_path if not path.exists(): path.mkdir() config['depot.storage_path'] = str(path) elif self.depot_backend.endswith('MemoryFileStorage'): pass else: # implementing non-local file systems is going to be more # invloved, because we do not generate external urls yet raise NotImplementedError() DepotManager.configure(self.bound_depot_id, config)
def test_public_url_gets_redirect(self): try: global S3Storage from depot.io.awss3 import S3Storage except ImportError: raise SkipTest('Boto not installed') env = os.environ access_key_id = env.get('AWS_ACCESS_KEY_ID') secret_access_key = env.get('AWS_SECRET_ACCESS_KEY') if access_key_id is None or secret_access_key is None: raise SkipTest('Amazon S3 credentials not available') PID = os.getpid() NODE = str(uuid.uuid1()).rsplit( '-', 1)[-1] # Travis runs multiple tests concurrently default_bucket_name = 'filedepot-%s' % (access_key_id.lower(), ) cred = (access_key_id, secret_access_key) bucket_name = 'filedepot-testfs-%s-%s-%s' % (access_key_id.lower(), NODE, PID) DepotManager.configure( 'awss3', { 'depot.backend': 'depot.io.awss3.S3Storage', 'depot.access_key_id': access_key_id, 'depot.secret_access_key': secret_access_key, 'depot.bucket': bucket_name }) DepotManager.set_default('awss3') app = self.make_app() new_file = app.post('/create_file').json file_path = DepotManager.url_for('%(uploaded_to)s/%(last)s' % new_file) uploaded_file = app.get(file_path, status=301) location = uploaded_file.headers['Location'] assert 'https://filedepot-testfs-' in location assert 's3.amazonaws.com' in location
def setUp(self) -> None: self._set_logger() logger.debug(self, 'Setup Test...') self.settings = plaster.get_settings(self.config_uri, self.config_section) self.config = testing.setUp(settings=self.settings) self.config.include('tracim_backend.models') DepotManager._clear() DepotManager.configure( 'test', {'depot.backend': 'depot.io.memory.MemoryFileStorage'}) settings = self.config.get_settings() self.app_config = CFG(settings) from tracim_backend.models import ( get_engine, get_session_factory, get_tm_session, ) self.engine = get_engine(settings) self.session_factory = get_session_factory(self.engine) self.init_database() self.session = get_tm_session(self.session_factory, transaction.manager)
def setUp(self) -> None: self._set_logger() logger.debug(self, "Setup Test...") self.settings = plaster.get_settings(self.config_uri, self.config_section) self.config = testing.setUp(settings=self.settings) DepotManager._clear() DepotManager.configure( "test", {"depot.backend": "depot.io.memory.MemoryFileStorage"}) settings = self.config.get_settings() self.app_config = CFG(settings) init_models(self.config, self.app_config) from tracim_backend.models.setup_models import ( get_engine, get_session_factory, get_tm_session, ) self.engine = get_engine(self.app_config) self.session_factory = get_session_factory(self.engine) self.init_database() self.session = get_tm_session(self.session_factory, transaction.manager)
def wrapper(self, *args, **kwargs): self.db = db async_task_obj = self.db.query(AsyncTask).filter( AsyncTask.id == kwargs.get("async_task_id")).first() self.async_task_obj = async_task_obj self.file_id = self.async_task_obj.payload.get('id') self.csv_object = self.db.query(BulkCSVUpload).filter( BulkCSVUpload.id == self.file_id).first() self.file_key = self.csv_object.csv.file_id self.bucket_name = "fulfilio-files" self.obj = get_file_from_s3(self.bucket_name, self.file_key) print(fn.__name__) if not DepotManager._default_depot: DepotManager.configure( 'default', { 'depot.backend': 'depot.io.boto3.S3Storage', 'depot.access_key_id': config.get('AWS_ACCESS_KEY', None), 'depot.secret_access_key': config.get( 'AWS_SECRET_KEY', None), 'depot.bucket': 'fulfilio-files', 'depot.region_name': 'eu-central-1' }) return fn(self, *args, payload=async_task_obj.payload, **kwargs)
def create_app(db_connection_string=None, testing=None): class CustomFlask(Flask): SKIP_RULES = [ # These endpoints will be removed for v3 '/v3/ws/<workspace_name>/hosts/bulk_delete/', '/v3/ws/<workspace_name>/vulns/bulk_delete/', '/v3/ws/<workspace_id>/change_readonly/', '/v3/ws/<workspace_id>/deactivate/', '/v3/ws/<workspace_id>/activate/', ] def add_url_rule(self, rule, endpoint=None, view_func=None, **options): # Flask registers views when an application starts # do not add view from SKIP_VIEWS for rule_ in CustomFlask.SKIP_RULES: if rule_ == rule: return return super().add_url_rule(rule, endpoint, view_func, **options) app = CustomFlask(__name__, static_folder=None) try: secret_key = faraday.server.config.faraday_server.secret_key except Exception: # Now when the config file does not exist it doesn't enter in this # condition, but it could happen in the future. TODO check save_new_secret_key(app) else: if secret_key is None: # This is what happens now when the config file doesn't exist. # TODO check save_new_secret_key(app) else: app.config['SECRET_KEY'] = secret_key if faraday.server.config.faraday_server.agent_registration_secret is None: save_new_agent_creation_token_secret() login_failed_message = ("Invalid username or password", 'error') app.config.update({ 'SECURITY_BACKWARDS_COMPAT_AUTH_TOKEN': True, 'SECURITY_PASSWORD_SINGLE_HASH': True, 'WTF_CSRF_ENABLED': False, 'SECURITY_USER_IDENTITY_ATTRIBUTES': [{ 'username': { 'mapper': uia_username_mapper } }], 'SECURITY_POST_LOGIN_VIEW': '/_api/session', 'SECURITY_POST_CHANGE_VIEW': '/_api/change', 'SECURITY_RESET_PASSWORD_TEMPLATE': '/security/reset.html', 'SECURITY_POST_RESET_VIEW': '/', 'SECURITY_SEND_PASSWORD_RESET_EMAIL': True, # For testing porpouse 'SECURITY_EMAIL_SENDER': "*****@*****.**", 'SECURITY_CHANGEABLE': True, 'SECURITY_SEND_PASSWORD_CHANGE_EMAIL': False, 'SECURITY_MSG_USER_DOES_NOT_EXIST': login_failed_message, 'SECURITY_TOKEN_AUTHENTICATION_HEADER': 'Authorization', # The line bellow should not be necessary because of the # CustomLoginForm, but i'll include it anyway. 'SECURITY_MSG_INVALID_PASSWORD': login_failed_message, 'SESSION_TYPE': 'filesystem', 'SESSION_FILE_DIR': faraday.server.config.FARADAY_SERVER_SESSIONS_DIR, 'SQLALCHEMY_TRACK_MODIFICATIONS': False, 'SQLALCHEMY_RECORD_QUERIES': True, # app.config['SQLALCHEMY_ECHO'] = True 'SECURITY_PASSWORD_SCHEMES': [ 'bcrypt', # This should be the default value # 'des_crypt', # 'pbkdf2_sha256', # 'pbkdf2_sha512', # 'sha256_crypt', # 'sha512_crypt', ], 'PERMANENT_SESSION_LIFETIME': datetime.timedelta(hours=int( faraday.server.config.faraday_server.session_timeout or 12)), 'SESSION_COOKIE_NAME': 'faraday_session_2', 'SESSION_COOKIE_SAMESITE': 'Lax', }) store = FilesystemStore(app.config['SESSION_FILE_DIR']) prefixed_store = PrefixDecorator('sessions_', store) KVSessionExtension(prefixed_store, app) user_logged_in.connect(user_logged_in_succesfull, app) user_logged_out.connect(expire_session, app) storage_path = faraday.server.config.storage.path if not storage_path: logger.warn( 'No storage section or path in the .faraday/config/server.ini. Setting the default value to .faraday/storage' ) storage_path = setup_storage_path() if not DepotManager.get('default'): if testing: DepotManager.configure( 'default', { 'depot.storage_path': '/tmp' # nosec }) else: DepotManager.configure('default', {'depot.storage_path': storage_path}) check_testing_configuration(testing, app) try: app.config[ 'SQLALCHEMY_DATABASE_URI'] = db_connection_string or faraday.server.config.database.connection_string.strip( "'") except AttributeError: logger.info( 'Missing [database] section on server.ini. Please configure the database before running the server.' ) except NoOptionError: logger.info( 'Missing connection_string on [database] section on server.ini. Please configure the database before running the server.' ) from faraday.server.models import db # pylint:disable=import-outside-toplevel db.init_app(app) # Session(app) # Setup Flask-Security app.user_datastore = SQLAlchemyUserDatastore( db, user_model=User, role_model=None) # We won't use flask security roles feature from faraday.server.api.modules.agent import agent_creation_api # pylint: disable=import-outside-toplevel app.limiter = Limiter(app, key_func=get_remote_address, default_limits=[]) if not testing: app.limiter.limit(faraday.server.config.limiter_config.login_limit)( agent_creation_api) app.register_blueprint(agent_creation_api) Security(app, app.user_datastore, login_form=CustomLoginForm) # Make API endpoints require a login user by default. Based on # https://stackoverflow.com/questions/13428708/best-way-to-make-flask-logins-login-required-the-default app.view_functions['security.login'].is_public = True app.view_functions['security.logout'].is_public = True app.debug = faraday.server.config.is_debug_mode() minify_json_output(app) for handler in LOGGING_HANDLERS: app.logger.addHandler(handler) app.logger.propagate = False register_blueprints(app) register_handlers(app) app.view_functions[ 'agent_creation_api.AgentCreationView:post'].is_public = True app.view_functions[ 'agent_creation_api.AgentCreationV3View:post'].is_public = True return app
def zopsedu_app(new_app): """Zopsedu bağımlılıklarını aldığı app'e ekler""" from zopsedu.auth.models.auth import load_user from zopsedu.lib.anonymous import Anonymous from zopsedu.common.app.file.views import FileView from zopsedu.common.app.sablon.views import SablonView from zopsedu.common.app.model_filter.views import FilterView from zopsedu.common.export import ExportView from zopsedu.lib.mail import init_mail from zopsedu.app import scheduler, job_store from flask_menu import Menu from flask_babel import gettext as _ # init session object login manager login_manager = ZopseduLoginManager() # login_manager.localize_callback = localize_callback login_manager.login_message = _( "Sayfayı görüntülemek için, lütfen giriş yapınız!") login_manager.needs_refresh_message = _( "Sayfayı görüntülemek için, lütfen tekrar giriş yapınız!") login_manager.init_app(new_app) login_manager.login_view = 'auth.login' login_manager.user_callback = load_user login_manager.anonymous_user = Anonymous new_app.secret_key = new_app.config['SECRET_KEY'] # depolama ayarlari DepotManager.configure( 'local', {'depot.storage_path': new_app.config['DEPOT_STORAGE_PATH']}) Babel(new_app) FileView.register(new_app) SablonView.register(new_app) FilterView.register(new_app) ExportView.register(new_app) # Depot Manager talep edilirse asagidaki gibi s3 uyumlu bir network # servisine baglanabilir. # DepotManager.configure('s3', { # 'depot.backend': 'depot.io.boto3.S3Storage', # 'depot.access_key_id': "123", # 'depot.secret_access_key': "1234", # 'depot.bucket': "bucket", # 'depot.endpoint_url': "http://localhost:8000" # }) new_app.wsgi_app = DepotManager.make_middleware(new_app.wsgi_app) JWTManager(new_app) # extendd app with flask menu Menu(app=new_app) scheduler.init_app(new_app) scheduler.scheduler.add_jobstore(jobstore=job_store) scheduler.start() # @new_app.teardown_appcontext # def session_commit(exception): # try: # DB.session.commit() # except IntegrityError as integrity_error: # new_app.logger.error(integrity_error) # DB.session.rollback() # except Exception as exc: # new_app.logger.error(exc) # DB.session.rollback() # debug toolbar # enable only if you need while developing, do not in test and prod # new_app = debug_toolbar(new_app) register_blueprints(new_app) register_signal_listeners(new_app) if new_app.config['CRUD_LOG']: from zopsedu.lib.db_event_listeners import register_db_event_listeners register_db_event_listeners() # gereksiz 415 tane endpoint(model filtrelemek icin) olusturdugundan dolayı kaldirildi # build_api(new_app) init_mail(new_app) return new_app
# base_config.sa_auth.authenticators = [('myauth', SomeAuthenticator()] # You can add more repoze.who metadata providers to fetch # user metadata. # Remember to set base_config.sa_auth.authmetadata to None # to disable authmetadata and use only your own metadata providers # base_config.sa_auth.mdproviders = [('myprovider', SomeMDProvider()] # override this if you would like to provide a different who plugin for # managing login and logout of your application base_config.sa_auth.form_plugin = None # You may optionally define a page where you want users to be redirected to # on login: base_config.sa_auth.post_login_url = "/post_login" # You may optionally define a page where you want users to be redirected to # on logout: base_config.sa_auth.post_logout_url = "/post_logout" try: # Enable DebugBar if available, install tgext.debugbar to turn it on from tgext.debugbar import enable_debugbar enable_debugbar(base_config) except ImportError: pass from depot.manager import DepotManager DepotManager.configure("default", {"depot.storage_path": "/tmp/"})
def test_changing_default_depot_works(self): DepotManager.configure('first', {'depot.storage_path': './lfs'}) DepotManager.configure('second', {'depot.storage_path': './lfs2'}) DepotManager.set_default('second') assert DepotManager.get_default() == 'second'
def config_ready(): """ Executed once the configuration is ready. """ # Configure default depot DepotManager.configure('default', tg.config)
def setup(self): DepotManager._clear() DepotManager.configure('default', {'depot.storage_path': './lfs'})
def create_app(db_connection_string=None, testing=None): app = Flask(__name__) try: secret_key = faraday.server.config.faraday_server.secret_key except Exception: # Now when the config file does not exist it doesn't enter in this # condition, but it could happen in the future. TODO check save_new_secret_key(app) else: if secret_key is None: # This is what happens now when the config file doesn't exist. # TODO check save_new_secret_key(app) else: app.config['SECRET_KEY'] = secret_key if faraday.server.config.faraday_server.agent_token is None: save_new_agent_creation_token() login_failed_message = ("Invalid username or password", 'error') app.config.update({ 'SECURITY_PASSWORD_SINGLE_HASH': True, 'WTF_CSRF_ENABLED': False, 'SECURITY_USER_IDENTITY_ATTRIBUTES': ['username'], 'SECURITY_POST_LOGIN_VIEW': '/_api/session', 'SECURITY_POST_LOGOUT_VIEW': '/_api/login', 'SECURITY_POST_CHANGE_VIEW': '/_api/change', 'SECURITY_CHANGEABLE': True, 'SECURITY_SEND_PASSWORD_CHANGE_EMAIL': False, 'SECURITY_MSG_USER_DOES_NOT_EXIST': login_failed_message, 'SECURITY_TOKEN_AUTHENTICATION_HEADER': 'Authorization', # The line bellow should not be necessary because of the # CustomLoginForm, but i'll include it anyway. 'SECURITY_MSG_INVALID_PASSWORD': login_failed_message, 'SESSION_TYPE': 'filesystem', 'SESSION_FILE_DIR': faraday.server.config.FARADAY_SERVER_SESSIONS_DIR, 'SQLALCHEMY_TRACK_MODIFICATIONS': False, 'SQLALCHEMY_RECORD_QUERIES': True, # app.config['SQLALCHEMY_ECHO'] = True 'SECURITY_PASSWORD_SCHEMES': [ 'bcrypt', # This should be the default value # 'des_crypt', 'pbkdf2_sha1', # Used by CouchDB passwords # 'pbkdf2_sha256', # 'pbkdf2_sha512', # 'sha256_crypt', # 'sha512_crypt', 'plaintext', # TODO: remove it ], 'PERMANENT_SESSION_LIFETIME': datetime.timedelta(hours=12), 'SESSION_COOKIE_NAME': 'faraday_session_2', 'SESSION_COOKIE_SAMESITE': 'Lax', }) store = FilesystemStore(app.config['SESSION_FILE_DIR']) prefixed_store = PrefixDecorator('sessions_', store) KVSessionExtension(prefixed_store, app) user_logged_out.connect(expire_session, app) storage_path = faraday.server.config.storage.path if not storage_path: logger.warn('No storage section or path in the .faraday/config/server.ini. Setting the default value to .faraday/storage') storage_path = setup_storage_path() if not DepotManager.get('default'): if testing: DepotManager.configure('default', { 'depot.storage_path': '/tmp' }) else: DepotManager.configure('default', { 'depot.storage_path': storage_path }) check_testing_configuration(testing, app) try: app.config['SQLALCHEMY_DATABASE_URI'] = db_connection_string or faraday.server.config.database.connection_string.strip("'") except AttributeError: logger.info('Missing [database] section on server.ini. Please configure the database before running the server.') except NoOptionError: logger.info('Missing connection_string on [database] section on server.ini. Please configure the database before running the server.') from faraday.server.models import db # pylint:disable=import-outside-toplevel db.init_app(app) #Session(app) # Setup Flask-Security app.user_datastore = SQLAlchemyUserDatastore( db, user_model=User, role_model=None) # We won't use flask security roles feature Security(app, app.user_datastore, login_form=CustomLoginForm) # Make API endpoints require a login user by default. Based on # https://stackoverflow.com/questions/13428708/best-way-to-make-flask-logins-login-required-the-default app.view_functions['security.login'].is_public = True app.view_functions['security.logout'].is_public = True app.debug = faraday.server.config.is_debug_mode() minify_json_output(app) for handler in LOGGING_HANDLERS: app.logger.addHandler(handler) register_blueprints(app) register_handlers(app) app.view_functions['agent_api.AgentCreationView:post'].is_public = True return app
def test_first_configured_is_default(self): DepotManager.configure('first', {'depot.storage_path': './lfs'}) DepotManager.configure('second', {'depot.storage_path': './lfs2'}) assert DepotManager.get_default() == 'first'
def file_depot(self): if self._file_depot is None: if DepotManager.get('files') is None: DepotManager.configure('files', self.config['file_archive']) self._file_depot = DepotManager.get('files') return self._file_depot
def create_app(db_connection_string=None, testing=None): app = Flask(__name__) try: secret_key = server.config.faraday_server.secret_key except Exception: # Now when the config file does not exist it doesn't enter in this # condition, but it could happen in the future. TODO check save_new_secret_key(app) else: if secret_key is None: # This is what happens now when the config file doesn't exist. # TODO check save_new_secret_key(app) else: app.config['SECRET_KEY'] = secret_key login_failed_message = ("Invalid username or password", 'error') app.config.update({ 'SECURITY_PASSWORD_SINGLE_HASH': True, 'WTF_CSRF_ENABLED': False, 'SECURITY_USER_IDENTITY_ATTRIBUTES': ['username'], 'SECURITY_POST_LOGIN_VIEW': '/_api/session', 'SECURITY_POST_LOGOUT_VIEW': '/_api/login', 'SECURITY_POST_CHANGE_VIEW': '/_api/change', 'SECURITY_CHANGEABLE': True, 'SECURITY_SEND_PASSWORD_CHANGE_EMAIL': False, 'SECURITY_MSG_USER_DOES_NOT_EXIST': login_failed_message, # The line bellow should not be necessary because of the # CustomLoginForm, but i'll include it anyway. 'SECURITY_MSG_INVALID_PASSWORD': login_failed_message, 'SESSION_TYPE': 'filesystem', 'SESSION_FILE_DIR': server.config.FARADAY_SERVER_SESSIONS_DIR, 'SQLALCHEMY_TRACK_MODIFICATIONS': False, 'SQLALCHEMY_RECORD_QUERIES': True, # app.config['SQLALCHEMY_ECHO'] = True 'SECURITY_PASSWORD_SCHEMES': [ 'bcrypt', # This should be the default value # 'des_crypt', 'pbkdf2_sha1', # Used by CouchDB passwords # 'pbkdf2_sha256', # 'pbkdf2_sha512', # 'sha256_crypt', # 'sha512_crypt', 'plaintext', # TODO: remove it ], 'PERMANENT_SESSION_LIFETIME': datetime.timedelta(hours=12), }) storage_path = server.config.storage.path if not storage_path: logger.warn('No storage section or path in the .faraday/server.ini. Setting the default value to .faraday/storage') storage_path = setup_storage_path() if not DepotManager.get('default'): if testing: DepotManager.configure('default', { 'depot.storage_path': '/tmp' }) else: DepotManager.configure('default', { 'depot.storage_path': storage_path }) check_testing_configuration(testing, app) try: app.config['SQLALCHEMY_DATABASE_URI'] = db_connection_string or server.config.database.connection_string.strip("'") except AttributeError: logger.info('Missing [database] section on server.ini. Please configure the database before running the server.') except NoOptionError: logger.info('Missing connection_string on [database] section on server.ini. Please configure the database before running the server.') from server.models import db db.init_app(app) #Session(app) # Setup Flask-Security app.user_datastore = SQLAlchemyUserDatastore( db, user_model=server.models.User, role_model=None) # We won't use flask security roles feature Security(app, app.user_datastore, login_form=CustomLoginForm) # Make API endpoints require a login user by default. Based on # https://stackoverflow.com/questions/13428708/best-way-to-make-flask-logins-login-required-the-default app.view_functions['security.login'].is_public = True app.view_functions['security.logout'].is_public = True app.debug = server.config.is_debug_mode() minify_json_output(app) for handler in LOGGING_HANDLERS: app.logger.addHandler(handler) register_blueprints(app) register_handlers(app) return app
def setup(): setup_database() DepotManager._clear() DepotManager.configure('default', {'depot.storage_path': './lfs'})
from tracim.lib.utils import lazy_ugettext as l_ import tracim from tracim import model from tracim.config import TracimAppConfig from tracim.lib.base import logger from tracim.lib.daemons import DaemonsManager from tracim.lib.daemons import MailSenderDaemon from tracim.lib.daemons import RadicaleDaemon from tracim.lib.daemons import WsgiDavDaemon from tracim.model.data import ActionDescription from tracim.model.data import ContentType from depot.manager import DepotManager DepotManager.configure('default', {'depot.storage_path': '/tmp/depot_storage_path/'}) base_config = TracimAppConfig() base_config.renderers = [] base_config.use_toscawidgets = False base_config.use_toscawidgets2 = True base_config.package = tracim #Enable json in expose base_config.renderers.append('json') #Enable genshi in expose to have a lingua franca for extensions and pluggable apps #you can remove this if you don't plan to use it. base_config.renderers.append('genshi') #Set the default renderer
""" WSGI config for depotexample project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ """ import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "depotexample.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application() # Configure a "default" storage based on DEPOT settings from django.conf import settings from depot.manager import DepotManager DepotManager.configure('default', settings.DEPOT, prefix='') # Wrap the application with depot middleware to serve files on /depot application = DepotManager.make_middleware(application)
# on login: base_config.sa_auth.post_login_url = '/post_login' # You may optionally define a page where you want users to be redirected to # on logout: base_config.sa_auth.post_logout_url = '/post_logout' # Admin configuration class AdminConfig(TGAdminConfig): allow_only = tg.predicates.has_permission('admin') # Configure default depot tg.milestones.config_ready.register( lambda: DepotManager.configure('default', tg.config)) # Variable provider: this provides a default set of variables to the templating engine def variable_provider(): d = {} if request.identity: d['luser'] = request.identity['user'] else: d['luser'] = None return d base_config.variable_provider = variable_provider
from flask import Flask, request, redirect, url_for, render_template app = Flask(__name__) # This is just an horrible way to keep around # uploaded files, but in the end we just wanted # to showcase how to setup DEPOT, not how to upload files. UPLOADED_FILES = [] @app.route("/", methods=['GET', 'POST']) def index(): if request.method == 'POST': file = request.files['file'] if file: fileid = DepotManager.get().create(file) UPLOADED_FILES.append(fileid) return redirect(url_for('index')) files = [DepotManager.get().get(fileid) for fileid in UPLOADED_FILES] return render_template('index.html', files=files) from depot.manager import DepotManager DepotManager.configure('default', {'depot.storage_path': '/tmp/'}) app.wsgi_app = DepotManager.make_middleware(app.wsgi_app) if __name__ == "__main__": app.run(host='0.0.0.0', port=5001, debug=True)
def test_alias_on_existing_storage(self): DepotManager.configure('mystorage', {'depot.storage_path': './lfs2'}) DepotManager.alias('mystorage', 'mystorage')
from dotenv import load_dotenv # load envrionment variables from '.env' file load_dotenv() import os from depot.manager import DepotManager # Configure a *default* depot to store files on MongoDB GridFS DepotManager.configure( 'default', { 'depot.backend': 'depot.io.boto3.S3Storage', 'depot.access_key_id': os.environ["AWS_ACCESS_KEY_ID"], 'depot.secret_access_key': os.environ["AWS_SECRET_ACCESS_KEY"], 'depot.bucket': os.environ["S3_BUCKET"] }) storage = DepotManager.get() pass # # Save the file and get the fileid # fileid = storage.create(open('./requirements.txt', 'rb')) # # Get the file back # stored_file = storage.get(fileid) # print(stored_file.filename) # print(stored_file.content_type)
""" Setup filedepot storage(s) """ try: path = settings['autonomie.depot_path'] except KeyError, err: logger.exception( u" !!!! You forgot to configure filedepot with an \ 'autonomie.depot_path' setting" ) raise err from depot.manager import DepotManager name = "local" if name not in DepotManager._depots: DepotManager.configure(name, {'depot.storage_path': path}) def _to_fieldstorage(fp, filename, size, **_kwds): """ Build a :class:`cgi.FieldStorage` instance. Deform's :class:`FileUploadWidget` returns a dict, but :class:`depot.fields.sqlalchemy.UploadedFileField` likes :class:`cgi.FieldStorage` objects """ f = cgi.FieldStorage() f.file = fp f.filename = filename f.type = detect_file_headers(filename) f.length = size return f