def handleList(self, confInfo): logger.info("start list") conf_mgr = conf.ConfManager(scc.getMgmtUri(), self.getSessionKey()) conf_mgr.reload_conf(c.myta_conf) conf_mgr.reload_conf(c.myta_credential_conf) conf_mgr.reload_conf(c.myta_customized_conf) # read globala and proxy settings all_settings = conf_mgr.all_stanzas_as_dicts(c.myta_conf) if not all_settings: all_settings = {} self._setNoneValues(all_settings.get(c.global_settings, {})) # read account credential settings for cred, cred_conf in self.cred_confs: ta_conf_mgr = ta_conf.TAConfManager(cred_conf, scc.getMgmtUri(), self.getSessionKey()) ta_conf_mgr.set_encrypt_keys(self.encrypt_fields_credential) creds = ta_conf_mgr.all(return_acl=False) if creds: self._setNoneValues(creds) all_settings.update({cred: creds}) # read customized settings ta_conf_mgr = ta_conf.TAConfManager(c.myta_customized_conf, scc.getMgmtUri(), self.getSessionKey()) ta_conf_mgr.set_encrypt_keys(self.encrypt_fields_customized) customized_settings = ta_conf_mgr.all(return_acl=False) all_settings.update({c.myta_customized_settings: customized_settings}) self._clearPasswords(all_settings, self.cred_fields) all_settings = json.dumps(all_settings) all_settings = utils.escape_json_control_chars(all_settings) confInfo[c.myta_settings].append(c.all_settings, all_settings) logger.info("end list")
def handleList(self, confInfo): logger.info("start list") conf_mgr = conf.ConfManager(scc.getMgmtUri(), self.getSessionKey(), app_name=self.appName) conf_mgr.reload_conf(c.myta_conf) conf_mgr.reload_conf(c.myta_cred_conf) conf_mgr.reload_conf(c.myta_forwarder_conf) all_settings = conf_mgr.all_stanzas_as_dicts(c.myta_conf) if not all_settings: all_settings = {} self._setNoneValues(all_settings.get(c.global_settings, {})) for cred, cred_conf in self.cred_confs: ta_conf_mgr = ta_conf.TAConfManager(cred_conf, scc.getMgmtUri(), self.getSessionKey(), appname=self.appName) ta_conf_mgr.set_encrypt_keys(self.encrypt_fields) creds = ta_conf_mgr.all(return_acl=False) if creds: self._setNoneValues(creds) all_settings.update({cred: creds}) self._clearPasswords(all_settings, self.cred_fields) all_settings = json.dumps(all_settings) all_settings = utils.escape_json_control_chars(all_settings) confInfo[c.myta_settings].append(c.all_settings, all_settings) logger.info("end list")
def testAll(self): mgr = tcm.TAConfManager("not_exist.conf", self.splunkd_uri, self.session_key, "search") data = { "name": "localforwarder1", "username": "******", "password": "******", } data2 = { "name": "localforwarder2", "username": "******", "password": "******", } mgr.delete(data["name"]) mgr.delete(data2["name"]) mgr.set_encrypt_keys(["username", "password"]) res = mgr.create(data) self.assertIsNone(res) res = mgr.get(data["name"]) for key, val in data.iteritems(): self.assertEqual(val, res[key]) data["username"] = "******" mgr.update(data) res = mgr.get(data["name"]) for key, val in data.iteritems(): self.assertEqual(val, res[key]) res = mgr.create(data2) self.assertIsNone(res) results = mgr.all(return_acl=False) for key, stanza in results.iteritems(): if key == data["name"]: for key, val in data.iteritems(): self.assertEqual(val, stanza[key]) elif key == data2["name"]: for key, val in data2.iteritems(): self.assertEqual(val, stanza[key]) else: self.assertTrue(False) res = mgr.delete(data["name"]) self.assertIsNone(res) res = mgr.delete(data2["name"]) self.assertIsNone(res) res = mgr.update(data) self.assertIsNone(res) res = mgr.get(data["name"]) for key, val in data.iteritems(): self.assertEqual(val, res[key]) res = mgr.delete(data["name"]) self.assertIsNone(res)
def handleEdit(self, confInfo): logger.info("start edit") conf_mgr = conf.ConfManager(scc.getMgmtUri(), self.getSessionKey(), app_name=self.appName) conf_mgr.reload_conf(c.myta_conf) conf_mgr.reload_conf(c.myta_credential_conf) conf_mgr.reload_conf(c.myta_customized_conf) all_origin_settings = conf_mgr.all_stanzas_as_dicts(c.myta_conf) all_settings = utils.escape_json_control_chars( self.callerArgs.data[c.all_settings][0]) all_settings = json.loads(all_settings) # write globala and proxy settings self._updateGlobalSettings(c.global_settings, all_settings, all_origin_settings, conf_mgr) # write account credential settings for cred, conf_file in self.cred_confs: creds = all_settings.get(cred, {}) if creds == c.ignore_backend_req: logger.info("Ignore backend rest request") continue ta_conf_mgr = ta_conf.TAConfManager(conf_file, scc.getMgmtUri(), self.getSessionKey(), appname=self.appName) ta_conf_mgr.set_encrypt_keys(self.encrypt_fields_credential) self._updateCredentials(creds, ta_conf_mgr) # write customized settings customized_settings = all_settings.get(c.myta_customized_settings, {}) ta_conf_mgr = ta_conf.TAConfManager(c.myta_customized_conf, scc.getMgmtUri(), self.getSessionKey(), appname=self.appName) ta_conf_mgr.set_encrypt_keys(self.encrypt_fields_customized) self._updateCredentials(customized_settings, ta_conf_mgr) conf_mgr.reload_conf(c.myta_conf) conf_mgr.reload_conf(c.myta_credential_conf) conf_mgr.reload_conf(c.myta_customized_conf) logger.info("end edit")
def _dispatch_tasks(self, global_settings, kafka_clusters): conf_mgr = tcm.TAConfManager(c.myta_forwarder_conf, self._meta_configs[c.server_uri], self._meta_configs[c.session_key], appname=self._appname) conf_mgr.reload() forwarders = conf_mgr.all(return_acl=False) if not self.is_dispatcher(self._server_info, forwarders): return lock = FileLock(self._meta_configs, self._appname) if not lock.locked(): return with lock: self._do_dispatch(global_settings, kafka_clusters, forwarders, conf_mgr)
def _do_disable(self, forwarder, removed_clusters, stanza_name): """ :return: false if something goes wrong, true if everything is good """ logger.info("Disable cluster=%s on forwarder=%s", stanza_name, forwarder[c.hostname]) try: session_key = self._get_forwarder_session_key(forwarder) conf_mgr = tcm.TAConfManager(c.myta_cred_conf, forwarder[c.hostname], session_key) conf_mgr.update({c.name: stanza_name, c.removed: "1"}) except Exception: logger.error("Failed to disable cluster=%s, error=%s", stanza_name, traceback.format_exc()) return False else: removed_clusters.add(stanza_name) return True
def handleEdit(self, confInfo): logger.info("start edit") conf_mgr = conf.ConfManager(scc.getMgmtUri(), self.getSessionKey(), app_name=self.appName) conf_mgr.reload_conf(c.myta_conf) conf_mgr.reload_conf(c.myta_cred_conf) conf_mgr.reload_conf(c.myta_forwarder_conf) all_origin_settings = conf_mgr.all_stanzas_as_dicts(c.myta_conf) all_settings = utils.escape_json_control_chars( self.callerArgs.data[c.all_settings][0]) all_settings = json.loads(all_settings) for stanza in (c.global_settings, c.proxy_settings): self._updateGlobalSettings(stanza, all_settings, all_origin_settings, conf_mgr) for cred, conf_file in self.cred_confs: creds = all_settings.get(cred, {}) if creds == c.ignore_backend_req: logger.info("Ignore backend rest request") continue update = False if c.backend_update_req in creds: update = True del creds[c.backend_update_req] ta_conf_mgr = ta_conf.TAConfManager(conf_file, scc.getMgmtUri(), self.getSessionKey(), appname=self.appName) ta_conf_mgr.set_encrypt_keys(self.encrypt_fields) self._updateCredentials(creds, ta_conf_mgr, update) conf_mgr.reload_conf(c.myta_conf) conf_mgr.reload_conf(c.myta_cred_conf) conf_mgr.reload_conf(c.myta_forwarder_conf) logger.info("end edit")
def is_dispatcher(self, server_info=None, forwarders=None): if not forwarders: conf_mgr = tcm.TAConfManager(c.myta_forwarder_conf, self._meta_configs[c.server_uri], self._meta_configs[c.session_key], appname=self._appname) forwarders = conf_mgr.all(return_acl=False) if not forwarders: return False if not server_info: server_info = sc.ServerInfo(self._meta_configs[c.server_uri], self._meta_configs[c.session_key]) if server_info.is_shc_member(): # In SHC env, only captain is able to dispatch the tasks if not server_info.is_captain(): logger.info("This SH is not captain, ignore task dispatching") return False return True
def _get_tasks(self): conf_mgr = tcm.TAConfManager(self.data_collection_conf(), self.metas[ggc.server_uri], self.metas[ggc.session_key], appname=self._appname) conf_mgr.reload() data_collections = conf_mgr.all(return_acl=False) if not data_collections: return [] data_collections = { k: v for k, v in data_collections.iteritems() if utils.is_false(v.get(ggc.disabled)) } global_settings = get_global_settings(self.metas[ggc.server_uri], self.metas[ggc.session_key]) google_creds = get_google_creds(self.metas[ggc.server_uri], self.metas[ggc.session_key]) return self._expand_tasks(global_settings, google_creds, data_collections)