def upload_image_form_f(img_form, page): """ Process an image upload form """ _file_uploads_enabled() # Make sure image uploads are enabled assert not img_form.validate_on_submit( ), 'Expected form to already be validated' # Get the file ext # https://docs.python.org/2/library/os.path.html#os.path.splitext # Not a simple text split! ext = os.path.splitext(secure_filename(img_form.img.data.filename))[1] if not EXT_MATCH.match(ext): raise BadExtensionError("Bad Extension") filename = "%s%s" % (uuid.uuid4(), ext) with session_scope(parent=db_session) as session: pfile = PageFile( title=img_form.title, desc=img_form.desc, published=img_form.published, ftype=PageFile.FILE_TYPE_IMAGE, filename=filename, page=page, ) session.add(pfile) session.commit() upload(img_form.img.data.read(), s3path=pfile.guid) return pfile.id, pfile.guid, pfile.filename
def fetch_file(id=None, guid=None, filename=None, req_published=True): """ Fetch a file from S3. Use redis cache if possible """ _file_uploads_enabled() # Make sure image uploads are enabled # Get the PageFile record assert id or guid or filename, "Expected at least id, guid, or filename to be given as args" with session_scope(parent=db_session) as session: pfq = session.query(PageFile) if id: pfq.filter_by(id=id) if guid: pfq.filter_by(guid=guid) if filename: pfq.filter_by(filename=filename) pfile = pfq.first() if not pfile: raise FileNotFoundError( "PageFile(id=%r,guid=%r,filename=%r) Not Found" % (id, guid, filename)) if req_published and not pfile.published: raise NotPublishedError("File isn't published") return dict(id=pfile.id, guid=pfile.guid, filename=pfile.filename, title=pfile.title, desc=pfile.desc, ftype=pfile.ftype, fdata=fetch_s3_file(s3path=pfile.s3path), published=pfile.published)
def validate_firewall(fw_id, p_session=db_session): """ Ensure it's a valid firewall. If so, return a Firewall object """ with session_scope(parent=p_session) as session: fw = session.query(Firewall).filter(guid=fw_id).first() if not fw: raise InvalidFWID("%r isn't a valid firewall guid" % fw_id) fw.last_seen = datetime.datetime.now() session.add(fw) session.commit() return session.query(Firewall).filter(guid=fw_id).first()
def alias_backup_gen(fw_id): """ Return an XML doc that can be restored via the pfsense backup interface. It will load all aliases. """ with session_scope(parent=db_session) as session: fw = validate_firewall(fw_id=fw_id, session=session) from fragforce.pfsense import AliasBackup import os, os.path import urllib # root_url = FW_ALIAS_PATH_FIXER.match(request.base_url).groups()[0] # # port_path = os.path.join(app.config['BASE_DIR'], 'fragforce', 'templates', 'fwaliases', 'ports') # nets_path = os.path.join(app.config['BASE_DIR'], 'fragforce', 'templates', 'fwaliases', 'nets') ab = AliasBackup() # def visit_port(aba, dirname, names): # for file_name in names: # path = os.path.join(dirname, file_name) # name = file_name.replace('.nets', '') # name = NAME_CHAR_FIX.sub('_', name) # if file_name.endswith('.ports'): # url = root_url + "/firewalls/tables/ports/" + file_name # ab.add_port_alias(name=file_name.replace('.ports', ''), url=url, update_frequency_days=1, # description="Port Table %r" % file_name) # # def visit_nets(aba, dirname, names): # for file_name in names: # path = os.path.join(dirname, file_name) # folder = os.path.split(dirname)[-1] # name = "%s_%s" % (folder, file_name.replace('.nets', '').replace('.ips', '')) # name = NAME_CHAR_FIX.sub('_', name) # if file_name.endswith('.nets'): # url = root_url + "/firewalls/tables/nets/%s/%s" % (folder, file_name) # ab.add_ip_alias(name=name, url=url, update_frequency_days=1, # description='Network Table %r' % file_name) # elif file_name.endswith('.ips'): # url = root_url + "/firewalls/tables/nets/%s/%s" % (folder, file_name) # ab.add_ip_alias(name=name, url=url, update_frequency_days=1, # description='IP Table %r' % file_name) # # os.path.walk(port_path, visit_port, ab) # os.path.walk(nets_path, visit_nets, ab) return Response(ab.render(pretty=False), mimetype='text/xml')
def tables_ports(fw_id, name, proto): with session_scope(parent=db_session) as session: fw = validate_firewall(fw_id=fw_id, session=session)