def commit(self): # data to save # - important that this fails immediately when nvram overflows from common import settings obj = self.serialize() v = settings.get('multisig', []) orig = v.copy() if not v or self.storage_idx == -1: # create self.storage_idx = len(v) v.append(obj) else: # update: no provision for changing fingerprints assert sorted(k for k, v in v[self.storage_idx][2]) == self.xfps v[self.storage_idx] = obj settings.set('multisig', v) # save now, rather than in background, so we can recover # from out-of-space situation try: settings.save() except: # back out change; no longer sure of NVRAM state try: settings.set('multisig', orig) settings.save() except: pass # give up on recovery raise MultisigOutOfSpace
def new_main_secret(self, raw_secret, chain=None): # Main secret has changed: reset the settings+their key, # and capture xfp/xpub from common import settings import stash # capture values we have already old_values = dict(settings.curr_dict) print('old_values = {}'.format(old_values)) settings.set_key(raw_secret) settings.load() print('after load = {}'.format(settings.curr_dict)) # merge in settings, including what chain to use, timeout, etc. settings.merge(old_values) print('after merge = {}'.format(settings.curr_dict)) # Recalculate xfp/xpub values (depends both on secret and chain) with stash.SensitiveValues(raw_secret) as sv: if chain is not None: sv.chain = chain sv.capture_xpub() print('before save = {}'.format(settings.curr_dict)) # Need to save values with new AES key settings.save() print('after save = {}'.format(settings.curr_dict))
def _static_processing(self): logger.info("Static processing thread starting") idx = -1 # resume processing with the last image the user looked at last_img = settings.get('processing/last_img', None) for i, image_name in enumerate(self.images): if image_name == last_img: self.idx = i break while True: with self.condition: # wait until the user hits a key while idx == self.idx and not self.do_stop and not self.do_refresh: self.condition.wait() if self.do_stop: break idx = self.idx self.do_refresh = False # if the index is valid, then process an image if idx < len(self.images) and idx >= 0: image_name = self.images[idx] logger.info("Opening %s" % image_name) img = cv2.imread(image_name) if img is None: logger.error("Error opening %s: could not read file" % (image_name)) self.idx += self.idx_increment continue try: target_data = self.detector.processImage(img) except: logutil.log_exception(logger, 'error processing image') else: settings.set('processing/last_img', image_name) settings.save() logger.info('Finished processing') # note that you cannot typically interact with the UI # from another thread -- but this function is special self.camera_widget.set_target_data(target_data) logger.info("Static processing thread exiting")
def on_delete_thresh_button_clicked(self, widget): name = self.get_selected_threshold_setting() # don't allow deleting builtins! if name in ['Competition', 'Pit']: util.show_error(None, "Cannot delete builtin settings!") return if util.yesno(None, "Delete setting %s?" % name) == gtk.RESPONSE_YES: for i, row in enumerate(self.thresh_model): if name == row[0]: del self.thresh_model[i] break settings.remove_option('camera/thresholds/%s' % name) settings.save()
def on_delete_thresh_button_clicked(self, widget): name = self.get_selected_threshold_setting() # don't allow deleting builtins! if name in [k for k, v, d in self.settings]: util.show_error(None, "Cannot delete builtin settings!") return if util.yesno(None, "Delete setting %s?" % name) == gtk.RESPONSE_YES: for i, row in enumerate(self.thresh_model): if name == row[0]: del self.thresh_model[i] break settings.remove_option('camera/thresholds/%s' % name) settings.save()
def delete(self): # remove saved entry # - important: not expecting more than one instance of this class in memory from common import settings assert self.storage_idx >= 0 # safety check expect_idx = self.find_match(self.M, self.N, self.xfps) assert expect_idx == self.storage_idx lst = settings.get('multisig', []) del lst[self.storage_idx] settings.set('multisig', lst) settings.save() self.storage_idx = -1
async def ensure_auth(hvac, role): """ Check the current authentication state of the system, including AWS and Docker. SSH is not included here, since its developer-only. """ if role == 'snowbot': role = 'robot' last_aws_auth = settings.hatch.aws_auth_time hours = (time.time() - last_aws_auth) / 3600 log.verbose(f'Hours since last AWS auth: {hours}') if hours > properties.aws_auth_period or True: if not await auth_aws(hvac, role): return settings.reload() settings.hatch.aws_auth_time = time.time() settings.save() # Docker last_docker_auth = settings.hatch.docker_auth_time hours = (time.time() - last_docker_auth) / 3600 log.verbose(f'Hours since last Docker auth: {hours}') if hours > properties.docker_auth_period or True: if not await auth_docker(): return settings.reload() settings.hatch.docker_auth_time = time.time() settings.save() # SSH keys if not await ssh_key_valid(): await sign_ssh_key(hvac)
async def restore_from_dict(vals): # Restore from a dict of values. Already JSON decoded. # Reboot on success, return string on failure from common import pa, dis, settings from pincodes import SE_SECRET_LEN #print("Restoring from: %r" % vals) # step1: the private key # - prefer raw_secret over other values # - TODO: fail back to other values try: chain = chains.get_chain(vals.get('chain', 'BTC')) assert 'raw_secret' in vals raw = bytearray(SE_SECRET_LEN) rs = vals.pop('raw_secret') if len(rs) % 2: rs += '0' x = a2b_hex(rs) raw[0:len(x)] = x # check we can decode this right (might be different firmare) opmode, bits, node = stash.SecretStash.decode(raw) assert node # verify against xprv value (if we have it) if 'xprv' in vals: check_xprv = chain.serialize_private(node) assert check_xprv == vals['xprv'], 'xprv mismatch' except Exception as e: return ('Unable to decode raw_secret and ' 'restore the seed value!\n\n\n' + str(e)) ls = None if 'long_secret' in vals: try: ls = a2b_hex(vals.pop('long_secret')) except Exception as exc: sys.print_exception(exc) # but keep going. dis.fullscreen("Saving...") dis.progress_bar_show(.25) # clear (in-memory) settings and change also nvram key # - also captures xfp, xpub at this point pa.change(new_secret=raw) # force the right chain pa.new_main_secret(raw, chain) # updates xfp/xpub # NOTE: don't fail after this point... they can muddle thru w/ just right seed if ls is not None: try: pa.ls_change(ls) except Exception as exc: sys.print_exception(exc) # but keep going # restore settings from backup file for idx, k in enumerate(vals): dis.progress_bar_show(idx / len(vals)) if not k.startswith('setting.'): continue if k == 'xfp' or k == 'xpub': continue settings.set(k[8:], vals[k]) # write out settings.save() if ('hsm_policy' in vals): import hsm hsm.restore_backup(vals['hsm_policy']) await ux_show_story( 'Everything has been successfully restored. ' 'We must now reboot to install the ' 'updated settings and/or seed.', title='Success!') from machine import reset reset()
if table is None or not processor.is_live_feed(): processor.start() # gtk main dashboard.show_all() #gtk.threads_init() #gtk.threads_enter() gtk.main() #gtk.threads_leave() logger.info('Shutting down Kwarqs Dashboard') settings.save() # shutdown anything needed here, like the logger processor.stop() ql.stop() except Exception as e: if __name__ == '__main__': import traceback traceback.print_exc() try: import msvcrt except ImportError:
if table is None or not back_processor.is_live_feed(): back_processor.start() # gtk main #gtk.threads_init() #gtk.threads_enter() gtk.main() #gtk.threads_leave() logger.info('Shutting down the driver station') settings.save() # shutdown anything needed here, like the logger back_processor.stop() front_processor.stop() ql.stop() except Exception as e: if __name__ == '__main__': import traceback traceback.print_exc() try: