def route_undelete(firmware_id): """ Undelete a firmware entry and also restore the file from disk """ # check firmware exists in database fw = db.session.query(Firmware).filter(Firmware.firmware_id == firmware_id).first() if not fw: flash('No firmware {} exists'.format(firmware_id), 'danger') return redirect(url_for('firmware.route_firmware')) # security check if not fw.check_acl('@undelete'): flash('Permission denied: Insufficient permissions to undelete firmware', 'danger') return redirect(url_for('firmware.route_show', firmware_id=firmware_id)) # find private remote remote = db.session.query(Remote).filter(Remote.name == 'private').first() if not remote: return _error_internal('No private remote') # move file back to the right place path = os.path.join(app.config['RESTORE_DIR'], fw.filename) if os.path.exists(path): path_new = os.path.join(app.config['DOWNLOAD_DIR'], fw.filename) shutil.move(path, path_new) # put back to the private state fw.remote_id = remote.remote_id fw.events.append(FirmwareEvent(remote_id=fw.remote_id, user_id=g.user.user_id)) db.session.commit() flash('Firmware undeleted', 'info') return redirect(url_for('firmware.route_show', firmware_id=firmware_id))
def _demote_back_to_testing(fw): # from the server admin user = db.session.query(User).filter(User.username == '*****@*****.**').first() if not user: return # send email to uploading user if fw.user.get_action('notify-demote-failures'): send_email("[LVFS] Firmware has been demoted", fw.user.email_address, render_template('email-firmware-demote.txt', user=fw.user, fw=fw)) fw.mark_dirty() remote = db.session.query(Remote).filter(Remote.name == 'testing').first() remote.is_dirty = True # asynchronously sign straight away, even public remotes for r in set([remote, fw.remote]): r.is_dirty = True _async_regenerate_remote.apply_async(args=(r.remote_id,), queue='metadata', countdown=1) fw.remote_id = remote.remote_id fw.events.append(FirmwareEvent(remote_id=fw.remote_id, user_id=user.user_id)) db.session.commit() _event_log('Demoted firmware {} as reported success {}%'.format(fw.firmware_id, fw.success))
def route_affiliation_change(firmware_id): """ Changes the assigned vendor ID for the firmware """ # change the vendor if 'vendor_id' not in request.form: return _error_internal('No vendor ID specified') # find firmware fw = db.session.query(Firmware).filter( Firmware.firmware_id == firmware_id).first() if not fw: flash('No firmware matched!', 'danger') return redirect(url_for('firmware.route_firmware')) # security check if not fw.check_acl('@modify-affiliation'): flash( 'Permission denied: Insufficient permissions to change affiliation', 'danger') return redirect(url_for('firmware.route_show', firmware_id=firmware_id)) vendor_id = int(request.form['vendor_id']) if vendor_id == fw.vendor_id: flash('No affiliation change required', 'info') return redirect( url_for('firmware.route_affiliation', firmware_id=fw.firmware_id)) if not g.user.check_acl('@admin') and \ not g.user.vendor.is_affiliate_for(vendor_id) and \ vendor_id != g.user.vendor_id: flash( 'Insufficient permissions to change affiliation to {}'.format( vendor_id), 'danger') return redirect(url_for('firmware.route_show', firmware_id=firmware_id)) old_vendor = fw.vendor fw.vendor_id = vendor_id db.session.commit() # do we need to regenerate remotes? if fw.remote.name.startswith('embargo'): fw.vendor.remote.is_dirty = True fw.user.vendor.remote.is_dirty = True old_vendor.remote.is_dirty = True fw.remote_id = fw.vendor.remote.remote_id fw.events.append( FirmwareEvent(remote_id=fw.remote_id, user_id=g.user.user_id)) fw.mark_dirty() db.session.commit() flash('Changed firmware vendor', 'info') # asynchronously sign _async_regenerate_remote.apply_async(args=(fw.remote.remote_id, ), queue='metadata') return redirect(url_for('firmware.route_show', firmware_id=fw.firmware_id))
def _demote_back_to_embargo(fw): # send email to uploading user if fw.user.notify_demote_failures: send_email("[LVFS] Firmware has been demoted", fw.user.email_address, render_template('email-firmware-demote.txt', user=fw.user, fw=fw)) fw.mark_dirty() remote = fw.vendor.remote remote.is_dirty = True fw.remote_id = remote.remote_id fw.events.append(FirmwareEvent(fw.remote_id)) db.session.commit() _event_log('Demoted firmware {} as reported success {}%%'.format(fw.firmware_id, fw.success))
def _firmware_delete(fw): # find private remote remote = db.session.query(Remote).filter(Remote.name == 'deleted').first() if not remote: _event_log('No deleted remote') return # move file so it's no longer downloadable path = os.path.join(app.config['DOWNLOAD_DIR'], fw.filename) if os.path.exists(path): path_new = os.path.join(app.config['RESTORE_DIR'], fw.filename) shutil.move(path, path_new) # generate next cron run fw.mark_dirty() # mark as invalid fw.remote_id = remote.remote_id fw.events.append(FirmwareEvent(fw.remote_id, g.user.user_id))
def _demote_back_to_testing(fw): # from the server admin user = db.session.query(User).filter(User.username == '*****@*****.**').first() if not user: return # send email to uploading user if fw.user.get_action('notify-demote-failures'): send_email("[LVFS] Firmware has been demoted", fw.user.email_address, render_template('email-firmware-demote.txt', user=fw.user, fw=fw)) fw.mark_dirty() remote = db.session.query(Remote).filter(Remote.name == 'testing').first() remote.is_dirty = True fw.remote_id = remote.remote_id fw.events.append(FirmwareEvent(remote_id=fw.remote_id, user_id=user.user_id)) db.session.commit() _event_log('Demoted firmware {} as reported success {}%'.format(fw.firmware_id, fw.success))
def _upload_firmware(): # verify the user can upload if not _user_can_upload(g.user): flash('User has not signed legal agreement', 'danger') return redirect(url_for('main.route_dashboard')) # used a custom vendor_id if 'vendor_id' in request.form: try: vendor_id = int(request.form['vendor_id']) except ValueError as e: flash('Failed to upload file: Specified vendor ID %s invalid' % request.form['vendor_id'], 'warning') return redirect(url_for('upload.route_firmware')) vendor = db.session.query(Vendor).filter(Vendor.vendor_id == vendor_id).first() if not vendor: flash('Failed to upload file: Specified vendor ID not found', 'warning') return redirect(url_for('upload.route_firmware')) else: vendor = g.user.vendor # security check if not vendor.check_acl('@upload'): flash('Permission denied: Failed to upload file for vendor: ' 'User with vendor %s cannot upload to vendor %s' % (g.user.vendor.group_id, vendor.group_id), 'warning') return redirect(url_for('upload.route_firmware')) # not correct parameters if not 'target' in request.form: return _error_internal('No target') if not 'file' in request.files: return _error_internal('No file') if request.form['target'] not in ['private', 'embargo', 'testing']: return _error_internal('Target not valid') # find remote, creating if required remote_name = request.form['target'] if remote_name == 'embargo': remote = vendor.remote else: remote = db.session.query(Remote).filter(Remote.name == remote_name).first() if not remote: return _error_internal('No remote for target %s' % remote_name) # if the vendor has uploaded a lot of firmware don't start changing the rules is_strict = len(vendor.fws) < 500 # load in the archive fileitem = request.files['file'] if not fileitem: return _error_internal('No file object') try: ufile = UploadedFile(is_strict=is_strict) for cat in db.session.query(Category): ufile.category_map[cat.value] = cat.category_id for pro in db.session.query(Protocol): ufile.protocol_map[pro.value] = pro.protocol_id for verfmt in db.session.query(Verfmt): ufile.version_formats[verfmt.value] = verfmt ufile.parse(os.path.basename(fileitem.filename), fileitem.read()) except (FileTooLarge, FileTooSmall, FileNotSupported, MetadataInvalid) as e: flash('Failed to upload file: ' + str(e), 'danger') return redirect(request.url) # check the file does not already exist fw = db.session.query(Firmware)\ .filter(or_(Firmware.checksum_upload_sha1 == ufile.fw.checksum_upload_sha1, Firmware.checksum_upload_sha256 == ufile.fw.checksum_upload_sha256)).first() if fw: if fw.check_acl('@view'): flash('Failed to upload file: A file with hash %s already exists' % fw.checksum_upload_sha1, 'warning') return redirect('/lvfs/firmware/%s' % fw.firmware_id) flash('Failed to upload file: Another user has already uploaded this firmware', 'warning') return redirect(url_for('upload.route_firmware')) # check the guid and version does not already exist fws = db.session.query(Firmware).all() fws_already_exist = [] for md in ufile.fw.mds: provides_value = md.guids[0].value fw = _filter_fw_by_id_guid_version(fws, md.appstream_id, provides_value, md.version) if fw: fws_already_exist.append(fw) # all the components existed, so build an error out of all the versions if len(fws_already_exist) == len(ufile.fw.mds): if g.user.check_acl('@robot') and 'auto-delete' in request.form: for fw in fws_already_exist: if fw.remote.is_public: flash('Firmware {} cannot be autodeleted as is in remote {}'.format( fw.firmware_id, fw.remote.name), 'danger') return redirect(url_for('upload.route_firmware')) if fw.user.user_id != g.user.user_id: flash('Firmware was not uploaded by this user', 'danger') return redirect(url_for('upload.route_firmware')) for fw in fws_already_exist: flash('Firmware %i was auto-deleted due to robot upload' % fw.firmware_id) _firmware_delete(fw) else: versions_for_display = [] for fw in fws_already_exist: for md in fw.mds: if not md.version_display in versions_for_display: versions_for_display.append(md.version_display) flash('Failed to upload file: A firmware file for this device with ' 'version %s already exists' % ','.join(versions_for_display), 'danger') return redirect('/lvfs/firmware/%s' % fw.firmware_id) # check if the file dropped a GUID previously supported for umd in ufile.fw.mds: new_guids = [guid.value for guid in umd.guids] for md in db.session.query(Component).\ filter(Component.appstream_id == umd.appstream_id): if md.fw.is_deleted: continue for old_guid in [guid.value for guid in md.guids]: if old_guid in new_guids: continue fw_str = str(md.fw.firmware_id) if g.user.check_acl('@qa') or g.user.check_acl('@robot'): flash('Firmware drops GUID {} previously supported ' 'in firmware {}'.format(old_guid, fw_str), 'warning') else: flash('Firmware would drop GUID {} previously supported ' 'in firmware {}'.format(old_guid, fw_str), 'danger') return redirect(request.url) # allow plugins to copy any extra files from the source archive for cffile in ufile.cabarchive_upload.values(): ploader.archive_copy(ufile.cabarchive_repacked, cffile) # allow plugins to add files ploader.archive_finalize(ufile.cabarchive_repacked, _get_plugin_metadata_for_uploaded_file(ufile)) # dump to a file download_dir = app.config['DOWNLOAD_DIR'] if not os.path.exists(download_dir): os.mkdir(download_dir) fn = os.path.join(download_dir, ufile.fw.filename) cab_data = ufile.cabarchive_repacked.save(compress=True) with open(fn, 'wb') as f: f.write(cab_data) # create parent firmware object settings = _get_settings() target = request.form['target'] fw = ufile.fw fw.vendor = vendor fw.user = g.user fw.addr = _get_client_address() fw.remote = remote fw.checksum_signed_sha1 = hashlib.sha1(cab_data).hexdigest() fw.checksum_signed_sha256 = hashlib.sha256(cab_data).hexdigest() fw.is_dirty = True fw.failure_minimum = settings['default_failure_minimum'] fw.failure_percentage = settings['default_failure_percentage'] # fix name for md in fw.mds: name_fixed = _fix_component_name(md.name, md.developer_name_display) if name_fixed != md.name: flash('Fixed component name from "%s" to "%s"' % (md.name, name_fixed), 'warning') md.name = name_fixed # verify each component has a version format for md in fw.mds: if not md.verfmt_with_fallback: flash('Component {} does not have required LVFS::VersionFormat'.\ format(md.appstream_id), 'warning') # add to database fw.events.append(FirmwareEvent(remote_id=remote.remote_id, user_id=g.user.user_id)) db.session.add(fw) db.session.commit() # ensure the test has been added for the firmware type ploader.ensure_test_for_fw(fw) # send out emails to anyone interested for u in fw.get_possible_users_to_email: if u == g.user: continue if u.get_action('notify-upload-vendor') and u.vendor == fw.vendor: send_email("[LVFS] Firmware has been uploaded", u.email_address, render_template('email-firmware-uploaded.txt', user=u, user_upload=g.user, fw=fw)) elif u.get_action('notify-upload-affiliate'): send_email("[LVFS] Firmware has been uploaded by affiliate", u.email_address, render_template('email-firmware-uploaded.txt', user=u, user_upload=g.user, fw=fw)) flash('Uploaded file %s to %s' % (ufile.fw.filename, target), 'info') # invalidate if target == 'embargo': remote.is_dirty = True g.user.vendor.remote.is_dirty = True db.session.commit() return redirect(url_for('firmware.route_show', firmware_id=fw.firmware_id))
def route_promote(firmware_id, target): """ Promote or demote a firmware file from one target to another, for example from testing to stable, or stable to testing. """ # check valid if target not in ['stable', 'testing', 'private', 'embargo']: return _error_internal("Target %s invalid" % target) # check firmware exists in database fw = db.session.query(Firmware).filter( Firmware.firmware_id == firmware_id).first() if not fw: flash('No firmware {} exists'.format(firmware_id), 'danger') return redirect(url_for('firmware.route_firmware')) # security check if not fw.check_acl('@promote-' + target): flash('Permission denied: No QA access to {}'.format(firmware_id), 'danger') return redirect(url_for('firmware.route_show', firmware_id=firmware_id)) # vendor has to fix the problems first if target in ['stable', 'testing'] and fw.problems: probs = [] for problem in fw.problems: if problem.kind not in probs: probs.append(problem.kind) flash( 'Firmware has problems that must be fixed first: %s' % ','.join(probs), 'warning') return redirect( url_for('firmware.route_problems', firmware_id=firmware_id)) # set new remote if target == 'embargo': remote = fw.vendor.remote else: remote = db.session.query(Remote).filter(Remote.name == target).first() if not remote: return _error_internal('No remote for target %s' % target) # same as before if fw.remote.remote_id == remote.remote_id: flash('Cannot move firmware: Firmware already in that target', 'info') return redirect( url_for('firmware.route_target', firmware_id=firmware_id)) # invalidate both the remote it "came from", the one it's "going to" and # also the remote of the vendor that uploaded it remote.is_dirty = True fw.remote.is_dirty = True fw.vendor_odm.remote.is_dirty = True # invalidate the firmware as we're waiting for the metadata generation fw.mark_dirty() # some tests only run when the firmware is in stable ploader.ensure_test_for_fw(fw) # also dirty any ODM remote if uploading on behalf of an OEM if target == 'embargo' and fw.vendor != fw.user.vendor: fw.user.vendor.remote.is_dirty = True # all okay fw.remote_id = remote.remote_id fw.events.append( FirmwareEvent(remote_id=fw.remote_id, user_id=g.user.user_id)) db.session.commit() # send email for u in fw.get_possible_users_to_email: if u == g.user: continue if u.get_action('notify-promote'): send_email( "[LVFS] Firmware has been promoted", u.email_address, render_template('email-firmware-promoted.txt', user=g.user, fw=fw)) flash('Moved firmware', 'info') return redirect(url_for('firmware.route_target', firmware_id=firmware_id))