def savesettings(self, allopts, token): if str(token) != str(self.token): return self.error("Invalid token (" + str(self.token) + ").") try: dbh = SpiderFootDb(self.config) # Reset config to default if allopts == "RESET": dbh.configClear() # Clear it in the DB self.config = deepcopy(self.defaultConfig) # Clear in memory else: useropts = json.loads(allopts) cleanopts = dict() for opt in useropts.keys(): cleanopts[opt] = self.cleanUserInput([useropts[opt]])[0] currentopts = deepcopy(self.config) # Make a new config where the user options override # the current system config. sf = SpiderFoot(self.config) self.config = sf.configUnserialize(cleanopts, currentopts) dbh.configSet(sf.configSerialize(currentopts)) except Exception as e: return self.error( "Processing one or more of your inputs failed: " + str(e)) templ = Template(filename='dyn/opts.tmpl', lookup=self.lookup) self.token = random.randint(0, 99999999) return templ.render(opts=self.config, pageid='SETTINGS', updated=True, docroot=self.docroot, token=self.token)
def savesettingsraw(self, allopts, token): if str(token) != str(self.token): return json.dumps(["ERROR", "Invalid token (" + str(self.token) + ")."]) try: dbh = SpiderFootDb(self.config) # Reset config to default if allopts == "RESET": dbh.configClear() # Clear it in the DB self.config = deepcopy(self.defaultConfig) # Clear in memory else: useropts = json.loads(allopts) cleanopts = dict() for opt in useropts.keys(): cleanopts[opt] = self.cleanUserInput([useropts[opt]])[0] currentopts = deepcopy(self.config) # Make a new config where the user options override # the current system config. sf = SpiderFoot(self.config) self.config = sf.configUnserialize(cleanopts, currentopts) dbh.configSet(sf.configSerialize(currentopts)) except Exception as e: return json.dumps(["ERROR", "Processing one or more of your inputs failed: " + str(e)]) return json.dumps(["SUCCESS", ""])
def savesettingsraw(self, allopts, token): if str(token) != str(self.token): return json.dumps( ["ERROR", "Invalid token (" + str(self.token) + ")."]) try: dbh = SpiderFootDb(self.config) # Reset config to default if allopts == "RESET": dbh.configClear() # Clear it in the DB self.config = deepcopy(self.defaultConfig) # Clear in memory else: useropts = json.loads(allopts) cleanopts = dict() for opt in list(useropts.keys()): cleanopts[opt] = self.cleanUserInput([useropts[opt]])[0] currentopts = deepcopy(self.config) # Make a new config where the user options override # the current system config. sf = SpiderFoot(self.config) self.config = sf.configUnserialize(cleanopts, currentopts) dbh.configSet(sf.configSerialize(self.config)) except Exception as e: return json.dumps([ "ERROR", "Processing one or more of your inputs failed: " + str(e) ]) return json.dumps(["SUCCESS", ""])
def savesettings(self, allopts, token): if str(token) != str(self.token): return self.error("Invalid token (" + str(self.token) + ").") try: dbh = SpiderFootDb(self.config) # Reset config to default if allopts == "RESET": dbh.configClear() # Clear it in the DB self.config = deepcopy(self.defaultConfig) # Clear in memory else: useropts = json.loads(allopts) cleanopts = dict() for opt in useropts.keys(): cleanopts[opt] = self.cleanUserInput([useropts[opt]])[0] currentopts = deepcopy(self.config) # Make a new config where the user options override # the current system config. sf = SpiderFoot(self.config) self.config = sf.configUnserialize(cleanopts, currentopts) dbh.configSet(sf.configSerialize(currentopts)) except Exception as e: return self.error("Processing one or more of your inputs failed: " + str(e)) templ = Template(filename='dyn/opts.tmpl', lookup=self.lookup) self.token = random.randint(0, 99999999) return templ.render(opts=self.config, pageid='SETTINGS', updated=True, docroot=self.docroot, token=self.token)
def savesettings(self, allopts, token, configFile=None): if str(token) != str(self.token): return self.error("Invalid token (" + str(self.token) + ").") if configFile: # configFile seems to get set even if a file isn't uploaded if configFile.file: contents = configFile.file.read() if type(contents) == bytes: contents = contents.decode("utf-8") try: tmp = dict() for line in contents.split("\n"): if "=" not in line: continue l = line.strip().split("=") if len(l) == 1: l[1] = "" tmp[l[0]] = l[1] allopts = json.dumps(tmp) except BaseException as e: return self.error( "Failed to parse input file. Was it generated from SpiderFoot? (" + str(e) + ")") try: dbh = SpiderFootDb(self.config) # Reset config to default if allopts == "RESET": dbh.configClear() # Clear it in the DB self.config = deepcopy(self.defaultConfig) # Clear in memory else: useropts = json.loads(allopts) cleanopts = dict() for opt in list(useropts.keys()): cleanopts[opt] = self.cleanUserInput([useropts[opt]])[0] currentopts = deepcopy(self.config) # Make a new config where the user options override # the current system config. sf = SpiderFoot(self.config) self.config = sf.configUnserialize(cleanopts, currentopts) dbh.configSet(sf.configSerialize(self.config)) except Exception as e: return self.error( "Processing one or more of your inputs failed: " + str(e)) templ = Template(filename='dyn/opts.tmpl', lookup=self.lookup) self.token = random.SystemRandom().randint(0, 99999999) return templ.render(opts=self.config, pageid='SETTINGS', updated=True, docroot=self.docroot, token=self.token)
def test_config_set_should_set_config_opts(self): """ Test configSet(self, optMap=dict()) """ sfdb = SpiderFootDb(self.default_options, False) opts = dict() opts['example'] = 'example non-default config opt' sfdb.configSet(opts) config = sfdb.configGet() self.assertIsInstance(config, dict) self.assertIn('example', config)
def test_configSet_argument_optmap_of_invalid_type_should_raise_TypeError(self): """ Test configSet(self, optMap=dict()) """ sfdb = SpiderFootDb(self.default_options, False) invalid_types = [None, "", list()] for invalid_type in invalid_types: with self.subTest(invalid_type=invalid_type): with self.assertRaises(TypeError) as cm: scan_result_event = sfdb.scanResultEventUnique("", invalid_type, None) sfdb.configSet(invalid_type)
def savesettings(self, allopts, token, configFile=None): if str(token) != str(self.token): return self.error("Invalid token (" + str(self.token) + ").") if configFile: # configFile seems to get set even if a file isn't uploaded if configFile.file: contents = configFile.file.read() try: tmp = dict() for line in contents.split("\n"): if "=" not in line: continue l = line.strip().split("=") if len(l) == 1: l[1] = "" tmp[l[0]] = l[1] allopts = json.dumps(tmp) except BaseException as e: return self.error("Failed to parse input file. Was it generated from SpiderFoot? (" + str(e) + ")") try: dbh = SpiderFootDb(self.config) # Reset config to default if allopts == "RESET": dbh.configClear() # Clear it in the DB self.config = deepcopy(self.defaultConfig) # Clear in memory else: useropts = json.loads(allopts) cleanopts = dict() for opt in useropts.keys(): cleanopts[opt] = self.cleanUserInput([useropts[opt]])[0] currentopts = deepcopy(self.config) # Make a new config where the user options override # the current system config. sf = SpiderFoot(self.config) self.config = sf.configUnserialize(cleanopts, currentopts) dbh.configSet(sf.configSerialize(self.config)) except Exception as e: return self.error("Processing one or more of your inputs failed: " + str(e)) templ = Template(filename='dyn/opts.tmpl', lookup=self.lookup) self.token = random.randint(0, 99999999) return templ.render(opts=self.config, pageid='SETTINGS', updated=True, docroot=self.docroot, token=self.token)
def test_config_clear_should_clear_config(self): """ Test configClear(self) """ sfdb = SpiderFootDb(self.default_options, False) opts = dict() opts['example'] = 'example non-default config opt' sfdb.configSet(opts) config = sfdb.configGet() self.assertIsInstance(config, dict) self.assertIn('example', config) sfdb.configClear() config = sfdb.configGet() self.assertIsInstance(config, dict) self.assertNotIn('example', config)
def savesettingsraw(self, allopts, token): """Save settings, also used to completely reset them to default Args: allopts: TBD token: TBD """ if str(token) != str(self.token): return json.dumps( ["ERROR", "Invalid token (" + str(self.token) + ")."]) # Reset config to default if allopts == "RESET": if self.reset_settings(): return json.dumps(["SUCCESS", ""]) else: return json.dumps(["ERROR", "Failed to reset settings"]) # Save settings try: dbh = SpiderFootDb(self.config) useropts = json.loads(allopts) cleanopts = dict() for opt in list(useropts.keys()): cleanopts[opt] = self.cleanUserInput([useropts[opt]])[0] currentopts = deepcopy(self.config) # Make a new config where the user options override # the current system config. sf = SpiderFoot(self.config) self.config = sf.configUnserialize(cleanopts, currentopts) dbh.configSet(sf.configSerialize(self.config)) except Exception as e: return json.dumps([ "ERROR", "Processing one or more of your inputs failed: %s" % e ]) return json.dumps(["SUCCESS", ""])
def savesettings(self, allopts): try: dbh = SpiderFootDb(self.config) # Reset config to default if allopts == "RESET": dbh.configClear() # Clear it in the DB self.config = deepcopy(self.defaultConfig) # Clear in memory else: useropts = json.loads(allopts) currentopts = deepcopy(self.config) # Make a new config where the user options override # the current system config. sf = SpiderFoot(self.config) self.config = sf.configUnserialize(useropts, currentopts) dbh.configSet(sf.configSerialize(currentopts)) except Exception as e: return self.error("Processing one or more of your inputs failed: " + str(e)) templ = Template(filename='dyn/opts.tmpl', lookup=self.lookup) return templ.render(opts=self.config, pageid='SETTINGS', updated=True)
def savesettings(self, allopts, token, configFile=None): """Save settings, also used to completely reset them to default Args: allopts: TBD token: TBD configFile: TBD """ if str(token) != str(self.token): return self.error("Invalid token (" + str(self.token) + ").") if configFile: # configFile seems to get set even if a file isn't uploaded if configFile.file: contents = configFile.file.read() if type(contents) == bytes: contents = contents.decode("utf-8") try: tmp = dict() for line in contents.split("\n"): if "=" not in line: continue opt_array = line.strip().split("=") if len(opt_array) == 1: opt_array[1] = "" tmp[opt_array[0]] = '='.join(opt_array[1:]) allopts = json.dumps(tmp) except BaseException as e: return self.error( "Failed to parse input file. Was it generated from SpiderFoot? (%s)" % e) # Reset config to default if allopts == "RESET": if self.reset_settings(): raise cherrypy.HTTPRedirect("/opts?updated=1") else: return self.error("Failed to reset settings") # Save settings try: dbh = SpiderFootDb(self.config) useropts = json.loads(allopts) cleanopts = dict() for opt in list(useropts.keys()): cleanopts[opt] = self.cleanUserInput([useropts[opt]])[0] currentopts = deepcopy(self.config) # Make a new config where the user options override # the current system config. sf = SpiderFoot(self.config) self.config = sf.configUnserialize(cleanopts, currentopts) dbh.configSet(sf.configSerialize(self.config)) except Exception as e: return self.error( "Processing one or more of your inputs failed: %s" % e) raise cherrypy.HTTPRedirect("/opts?updated=1")