def route_login_oauth_authorized(plugin_id): # find the plugin that can authenticate us p = ploader.get_by_id(plugin_id) if not p: _error_internal('no plugin {}'.format(plugin_id)) if not hasattr(p, 'oauth_get_data'): return _error_internal( 'no oauth support in plugin {}'.format(plugin_id)) try: data = p.oauth_get_data() if 'userPrincipalName' not in data: return _error_internal('No userPrincipalName in profile') except PluginError as e: return _error_internal(str(e)) # auth check created_account = False user = db.session.query(User).filter( User.username == data['userPrincipalName']).first() if not user: user = _create_user_for_oauth_username(data['userPrincipalName']) if user: db.session.add(user) db.session.commit() _event_log('Auto created user of type %s for vendor %s' % (user.auth_type, user.vendor.group_id)) created_account = True if not user: flash('Failed to log in: no user for %s' % data['userPrincipalName'], 'danger') return redirect(url_for('main.route_index')) if not user.auth_type: flash('Failed to log in: User account %s is disabled' % user.username, 'danger') return redirect(url_for('main.route_index')) if user.auth_type != 'oauth': flash('Failed to log in: Only some accounts can log in using OAuth', 'danger') return redirect(url_for('main.route_index')) # sync the display name if 'displayName' in data: if user.display_name != data['displayName']: user.display_name = data['displayName'] db.session.commit() # success login_user(user, remember=False) g.user = user if created_account: flash('Logged in, and created account', 'info') else: flash('Logged in', 'info') # set the access time user.atime = datetime.datetime.utcnow() db.session.commit() return redirect(url_for('main.route_dashboard'))
def _check_firmware(): # ensure the test has been added for the firmware type fws = db.session.query(Firmware).all() for fw in fws: if fw.is_deleted: continue ploader.ensure_test_for_fw(fw) db.session.commit() # make a list of all the tests that need running test_fws = {} for fw in fws: for test in fw.tests: if test.needs_running: if fw in test_fws: test_fws[fw].append(test) else: test_fws[fw] = [test] # mark all the tests as started for fw in test_fws: for test in test_fws[fw]: print('Marking test %s started for firmware %u...' % (test.plugin_id, fw.firmware_id)) test.started_ts = datetime.datetime.utcnow() db.session.commit() # process each test for fw in test_fws: for test in sorted(test_fws[fw], key=_test_priority_sort_func): plugin = ploader.get_by_id(test.plugin_id) if not plugin: _event_log('No plugin %s' % test.plugin_id) test.ended_ts = datetime.datetime.utcnow() continue if not hasattr(plugin, 'run_test_on_fw'): _event_log('No run_test_on_fw in %s' % test.plugin_id) test.ended_ts = datetime.datetime.utcnow() continue try: print('Running test %s for firmware %s' % (test.plugin_id, fw.firmware_id)) plugin.run_test_on_fw(test, fw) test.ended_ts = datetime.datetime.utcnow() # don't leave a failed task running db.session.commit() except Exception as e: # pylint: disable=broad-except test.ended_ts = datetime.datetime.utcnow() test.add_fail('An exception occurred', str(e)) # unallocate the cached blob as it's no longer needed fw.blob = None # all done db.session.commit()
def route_view(plugin_id='general'): """ Allows the admin to change details about the LVFS instance """ plugin = ploader.get_by_id(plugin_id) if not plugin: flash('No plugin {}'.format(plugin_id), 'danger') return redirect(url_for('settings.route_view')) tests_by_type = _convert_tests_for_plugin(plugin) return render_template('settings.html', category='settings', settings=_get_settings(), plugin=plugin, tests_by_type=tests_by_type)
def settings_tests(plugin_id, kind): """ Allows the admin to change details about the LVFS instance """ plugin = ploader.get_by_id(plugin_id) if not plugin: flash('No plugin {}'.format(plugin_id), 'danger') return redirect(url_for('.settings_view')) tests_by_type = _convert_tests_for_plugin(plugin) return render_template('settings-tests.html', category='settings', tests=tests_by_type[kind][:50], tests_by_type=tests_by_type, plugin=plugin)
def route_overview(): # get all the test data tests = db.session.query(Test).\ options(joinedload('attributes')). \ order_by(Test.scheduled_ts.desc()).all() plugin_ids = {} for test in tests: if test.plugin_id not in plugin_ids: plugin_ids[test.plugin_id] = [] plugin_ids[test.plugin_id].append(test) tests_pending = {} tests_running = {} tests_success = {} tests_failed = {} tests_waived = {} for plugin_id in plugin_ids: tests_pending[plugin_id] = [] tests_running[plugin_id] = [] tests_success[plugin_id] = [] tests_failed[plugin_id] = [] tests_waived[plugin_id] = [] for test in plugin_ids[plugin_id]: if test.is_pending: tests_pending[plugin_id].append(test) elif test.is_running: tests_running[plugin_id].append(test) elif test.waived_ts: tests_waived[plugin_id].append(test) elif test.success: tests_success[plugin_id].append(test) else: tests_failed[plugin_id].append(test) # get the actual Plugin for the ID plugins = {} for plugin_id in plugin_ids: plugins[plugin_id] = ploader.get_by_id(plugin_id) return render_template('test-overview.html', category='tests', plugins=plugins, plugin_ids=plugin_ids, tests_pending=tests_pending, tests_running=tests_running, tests_success=tests_success, tests_waived=tests_waived, tests_failed=tests_failed)
def route_login_oauth(plugin_id): # find the plugin that can authenticate us p = ploader.get_by_id(plugin_id) if not p: return _error_internal('no plugin {}'.format(plugin_id)) if not p.oauth_authorize: return _error_internal( 'no oauth support in plugin {}'.format(plugin_id)) try: return p.oauth_authorize( url_for('main.route_login_oauth_authorized', plugin_id=plugin_id, _external=True)) except PluginError as e: return _error_internal(str(e))
def login_oauth(plugin_id): # find the plugin that can authenticate us p = ploader.get_by_id(plugin_id) if not p: return _error_permission_denied('no plugin %s' % plugin_id) if not p.oauth_authorize: return _error_permission_denied('no oauth support in plugin %s' % plugin_id) try: return p.oauth_authorize( url_for('login_oauth_authorized', plugin_id=plugin_id, _external=True)) except PluginError as e: return _error_permission_denied(str(e))
def _test_run_all(tests=None): # make a list of the first few tests that need running if not tests: tests = db.session.query(Test)\ .filter(Test.started_ts == None)\ .order_by(Test.scheduled_ts)\ .limit(50).all() # mark all the tests as started for test in tests: print('Marking test {} started for firmware {}...'.format( test.plugin_id, test.fw.firmware_id)) test.started_ts = datetime.datetime.utcnow() db.session.commit() # process each test for test in sorted(tests, key=_test_priority_sort_func): plugin = ploader.get_by_id(test.plugin_id) if not plugin: _event_log('No plugin %s' % test.plugin_id) test.ended_ts = datetime.datetime.utcnow() continue try: print('Running test {} for firmware {}'.format( test.plugin_id, test.fw.firmware_id)) if hasattr(plugin, 'run_test_on_fw'): if hasattr(plugin, 'require_test_for_fw'): if not plugin.require_test_for_fw(test.fw): continue plugin.run_test_on_fw(test, test.fw) if hasattr(plugin, 'run_test_on_md'): for md in test.fw.mds: if hasattr(plugin, 'require_test_for_md'): if not plugin.require_test_for_md(md): continue plugin.run_test_on_md(test, md) test.ended_ts = datetime.datetime.utcnow() # don't leave a failed task running db.session.commit() except Exception as e: # pylint: disable=broad-except test.ended_ts = datetime.datetime.utcnow() test.add_fail('An exception occurred', str(e)) # all done db.session.commit()
def _check_firmware(): # make a list of all the tests that need running tests = db.session.query(Test).filter(Test.started_ts == None).all() # mark all the tests as started for test in tests: print('Marking test {} started for firmware {}...'.format( test.plugin_id, test.fw.firmware_id)) test.started_ts = datetime.datetime.utcnow() db.session.commit() # process each test for test in sorted(tests, key=_test_priority_sort_func): plugin = ploader.get_by_id(test.plugin_id) if not plugin: _event_log('No plugin %s' % test.plugin_id) test.ended_ts = datetime.datetime.utcnow() continue if not hasattr(plugin, 'run_test_on_fw'): _event_log('No run_test_on_fw in %s' % test.plugin_id) test.ended_ts = datetime.datetime.utcnow() continue try: print('Running test {} for firmware {}'.format( test.plugin_id, test.fw.firmware_id)) plugin.run_test_on_fw(test, test.fw) test.ended_ts = datetime.datetime.utcnow() # don't leave a failed task running db.session.commit() except Exception as e: # pylint: disable=broad-except test.ended_ts = datetime.datetime.utcnow() test.add_fail('An exception occurred', str(e)) # all done db.session.commit()
def _test_priority_sort_func(test): plugin = ploader.get_by_id(test.plugin_id) return plugin.priority
def format_plugin_id(tmp): return ploader.get_by_id(tmp)
from lvfs import db, ploader from lvfs.models import Test, Firmware from lvfs.pluginloader import PluginError if __name__ == '__main__': now = datetime.date.today() fn = 'chipsec-{}.csv'.format(datetime.date.isoformat(now)) with open(fn, 'w') as csvfile: fieldnames = ['filename', 'vendor', 'shards', 'msg'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() # run chipsec on each firmware file plugin = ploader.get_by_id('chipsec') for fw in db.session.query(Firmware).order_by( Firmware.firmware_id.asc()): test = Test(None) if fw.is_deleted: continue if not fw.remote.is_public: continue if not plugin._require_test_for_fw(fw): continue print('Processing {}: {} for {}'.format(fw.firmware_id, fw.filename, fw.vendor.group_id)) data = {'filename': fw.filename, 'vendor': fw.vendor.group_id} try: plugin.run_test_on_fw(test, fw)