def __init__(self, appname, uri, session_key, service_with_tab_context=None, service_with_ta_context=None): self.__appname = appname self.__app_namespace = common_util.get_python_lib_dir_name( self.__appname) self.__logger = logger.get_input_builder_logger() self.__parent_dir = os.path.split(os.path.realpath(__file__))[0] self.__resource_dir = os.path.join(self.__parent_dir, "resources") self.__resource_lib_dir = os.path.join(self.__parent_dir, "resources_lib") self.__splunk_home = os.environ['SPLUNK_HOME'] self.__splunk_app_dir = os.path.join(self.__splunk_home, "etc", "apps") self.__current_ta_dir = os.path.join(self.__splunk_app_dir, self.__appname) self.__asset_generator = ta_static_asset_generator.AssetGenerator( self.__resource_dir, self.__current_ta_dir, self.__resource_lib_dir, app_name=self.__appname) self.__uri = uri self.__session_key = session_key if service_with_ta_context: self.__service_with_ta_context = service_with_ta_context else: self.__service_with_ta_context = common_util.create_splunk_service( session_key, uri, self.__appname) self.__logger.info('Create splunk service with args: %s, %s', session_key, uri) self.__conf_mgr = common_util.create_conf_mgr( self.__session_key, self.__uri, app=self.__appname) self.__conf_mgr_with_tab_context = common_util.create_conf_mgr( self.__session_key, self.__uri) if not service_with_tab_context: service_with_tab_context = common_util.create_splunk_service( session_key, uri) self.__service_with_tab_context = service_with_tab_context self.required_meta_keys = ['name', 'type', 'sourcetype'] self.__global_vars = None self.__input_meta_mgr = TAInputMetaMgr(appname, uri, session_key, self.__service_with_tab_context) self._ta_configuration_builder = builder_ta_configuration.TAConfigurationBuilder( self.__appname, self.__service_with_tab_context, self.__service_with_ta_context)
def __init__(self, appname, uri, session_key, service_with_tab_context=None, service_with_ta_context=None): self.__appname = appname self.__app_namespace = common_util.get_python_lib_dir_name( self.__appname) self.__logger = logger.get_alert_builder_logger() self.__parent_dir = os.path.split(os.path.realpath(__file__))[0] self.__resource_dir = os.path.join(self.__parent_dir, "resources") self.__resource_lib_dir = os.path.join(self.__parent_dir, "resources_lib") self.__splunk_home = os.environ['SPLUNK_HOME'] self.__splunk_app_dir = os.path.join(self.__splunk_home, "etc", "apps") self.__current_ta_dir = os.path.join(self.__splunk_app_dir, self.__appname) self.__asset_generator = ta_static_asset_generator.AssetGenerator( self.__resource_dir, self.__current_ta_dir, self.__resource_lib_dir, app_name=self.__appname) self.__uri = uri self.__session_key = session_key if service_with_ta_context: self.__service_with_ta_context = service_with_ta_context else: self.__service_with_ta_context = common_util.create_splunk_service( session_key, uri, self.__appname) self.__conf_mgr = common_util.create_conf_mgr(self.__session_key, self.__uri, app=self.__appname) self.__conf_mgr_with_tab_context = common_util.create_conf_mgr( self.__session_key, self.__uri) if not service_with_tab_context: service_with_tab_context = common_util.create_splunk_service( session_key, uri) self.__service_with_tab_context = service_with_tab_context self.__meta_mgr = meta_manager.create_meta_manager( session_key, uri, meta_const.ALERT_ACTION_BUILER, self.__appname) self.required_meta_keys = ['name', 'type', 'sourcetype'] self.__global_vars = None self.__basic_builder = TABasicBuilder(self.__appname, uri, session_key, self.__service_with_tab_context, self.__service_with_ta_context)
def get_appname(params): if 'appname' in params: app_name = params['appname'] else: service = common_util.create_splunk_service( cherrypy.session.get("sessionKey"), scc.getMgmtUri()) apps = [app.name for app in service.apps if app.name] conf_manager = common_util.create_conf_mgr( cherrypy.session.get("sessionKey"), scc.getMgmtUri()) conf_file = conf_manager.get_conf(tabuilder_status_conf) app_name_create = None app_name_edit = None if tab_conf_manager.is_stanza_exists(conf_file, create_ta_stanza): app_name_create = conf_file.get(create_ta_stanza).get( cca_key, None) create_in_action = conf_file.get(create_ta_stanza).get( "action", False) if create_in_action and app_name_create in apps: return app_name_create if tab_conf_manager.is_stanza_exists(conf_file, edit_ta_stanza): app_name_edit = conf_file.get(edit_ta_stanza).get(cca_key, None) if app_name_edit in apps: return app_name_edit return builder_constant.ADDON_BUILDER_APP_NAME
def set_tab_global_settings(self, action, **params): session_key = cherrypy.session.get("sessionKey") splunkd_uri = scc.getMgmtUri() settings = json.loads(params['settings']) try: encrypt_keys = ("password", "username", "proxy_username", "proxy_password") conf_mgr = common_util.create_conf_mgr(session_key, splunkd_uri) conf = conf_mgr.get_conf(builder_constant.GLOBAL_SETTING_CONF_NAME) conf_settings = conf.get_all() app_cert = AppCert(splunkd_uri, session_key, builder_constant.ADDON_BUILDER_APP_NAME) app_cert_settings = conf_settings.get("app_cert", {}).copy() app_cert_settings.update(settings.get("app_cert")) app_cert.validate_settings(app_cert_settings) for stanza, key_values in list(settings.items()): conf.update(stanza, key_values, encrypt_keys) return self.render_json({"data": settings}) except CommonException as ce: return self.render_json({ 'err_code': ce.get_err_code(), 'err_args': ce.get_options() })
def __init__(self, appname, uri, session_key, service_with_tab_context=None): self.__appname = appname self.__logger = logger.get_input_builder_logger() self.__uri = uri self.__session_key = session_key self.__conf_mgr_with_tab_context = common_util.create_conf_mgr( self.__session_key, self.__uri) self.__default_input_properties = { "index": "default", "sourcetype": "{}_sourcetype".format(self.__appname), "interval": 60, "use_external_validation": True, "streaming_mode_xml": True, } if not service_with_tab_context: service_with_tab_context = common_util.create_splunk_service( session_key, uri) self.__service_with_tab_context = service_with_tab_context self.__meta_mgr = meta_manager.create_meta_manager( session_key, uri, meta_const.DATA_INPUT_BUILDER, self.__appname) self.required_meta_keys = ['name', 'type', 'sourcetype'] self.__alert_builder = None
def get_eventtypes(self): ret = [] conf_mgr = common_util.create_conf_mgr(self.splunk_session_key, self.splunk_endpoint, "-") eventtypes = validation_utility.get_app_stanzas( conf_mgr, "eventtypes", self.app_name) tag_list = validation_utility.get_app_stanzas(conf_mgr, "tags", self.app_name) for eventtype in eventtypes: etype = {} tags = [] stanza = eventtype.get("name") for tag_dict in tag_list: if tag_dict.get("name") == "eventtype={}".format(stanza): for k, v in list(tag_dict.items()): if v == "enabled": tags.append(k.strip()) break _LOGGER.debug("Get tags: {}".format(tags)) search = eventtype.get("search") if not search: continue search_prefix = "search index=* " etype["search"] = search_prefix + search etype["tags"] = tags etype["name"] = stanza ret.append(etype) return ret
def submit_validation_job(self, action, **params): cl = cherrypy.request.headers["Content-Length"] raw_body = cherrypy.request.body.read(int(cl)) params = json.loads(raw_body) session_key = cherrypy.session.get("sessionKey") splunkd_uri = scc.getMgmtUri() appname = params["app_name"] or ta_project_meta.get_appname({}) try: vbuilder = TAValidationBuilder(splunkd_uri, session_key, appname) validators = [v.strip() for v in params["validators"].split(",")] # validate the username & password when app certification is selected if "app_cert_validation" in validators: conf_mgr = common_util.create_conf_mgr(session_key, splunkd_uri) settings = conf_mgr.get_conf( builder_constant.GLOBAL_SETTING_CONF_NAME) conf = settings.get(builder_constant.APP_CERT_STANZA) if not conf.get("username") or not conf.get("password"): ce = CommonException() ce.set_err_code(6008) raise ce # try to get the token. If failed, will throw exceptions app_cert = AppCert(splunkd_uri, session_key, builder_constant.ADDON_BUILDER_APP_NAME) app_cert.app_conf = app_cert._get_app_cert_conf( need_validation=False) app_cert.get_token() # vbuilder.remove_all_validation_data_inputs() vid = vbuilder.start_validation_job(validators) result = { "validation_id": vid, "submission_result": "success", "app_name": appname } return self.render_json(result) except CommonException as e: vbuilder.cancel_validation_job() logger.error( 'Get CommonException when starting validation. meta:%s, error:%s', params, traceback.format_exc()) return self.render_json({ 'err_code': e.get_err_code(), 'err_args': e.get_options() }) except Exception as e: vbuilder.cancel_validation_job() logger.error("Cannot start validation. error: %s", traceback.format_exc()) raise e
def set_current_editing_appname(app_name): conf_manager = common_util.create_conf_mgr( cherrypy.session.get("sessionKey"), scc.getMgmtUri()) conf_file = conf_manager.get_conf(tabuilder_status_conf) conf_file.update(edit_ta_stanza, {cca_key: app_name, "action": True}) try: conf_file.get(create_ta_stanza) conf_file.update(create_ta_stanza, {"action": False}) except ConfStanzaNotExistException as e: # update the create stanza if exist. otherwise, skip pass return app_name
def get_current_creating_app(params={}): if 'appname' in params: app_name = params['appname'] else: session = cherrypy.session.get("sessionKey") uri = scc.getMgmtUri() service = common_util.create_splunk_service(session, uri) installed_apps = [app.name for app in service.apps if app.name] conf_manager = common_util.create_conf_mgr(session, uri) current_creating_app = conf_manager.get_conf( tabuilder_status_conf).get(create_ta_stanza).get(cca_key, None) if current_creating_app in installed_apps: app_name = current_creating_app else: app_name = builder_constant.ADDON_BUILDER_APP_NAME return app_name
def get_tab_global_settings(self, action, **params): session_key = cherrypy.session.get("sessionKey") splunkd_uri = scc.getMgmtUri() try: conf_mgr = common_util.create_conf_mgr(session_key, splunkd_uri) conf = conf_mgr.get_conf(builder_constant.GLOBAL_SETTING_CONF_NAME) settings = conf.get_all() app_cert_conf = settings.get("app_cert") app_cert_conf["proxy_enabled"] = is_true( app_cert_conf["proxy_enabled"]) return self.render_json({"data": settings}) except CommonException as ce: return self.render_json({ 'err_code': ce.get_err_code(), 'err_args': ce.get_options() })
def __init__(self, splunk_uri, splunk_session_key, app_name, service): ''' the conf manager should be in the context of app_name! ''' self.logger = logger.get_builder_util_logger() self.app_name = app_name self.conf_mgr = common_util.create_conf_mgr(splunk_session_key, splunk_uri, app_name) self.default_kv = {} self.default_value_sets = ( {1, "1", "t", "true", 'y', 'yes'}, # true set {0, "0", "f", "false", "n", "no"}, # false set {None, "none", ""}, # None set ) if service: self.service = service else: self.service = common_util.create_splunk_service( splunk_session_key, splunk_uri, app_name)
def __init__(self, appname, splunkd_uri, session_key, service_with_tab_context=None, service_with_ta_context=None, ): self.appname = appname self.splunkd_uri = splunkd_uri self.session_key = session_key if service_with_ta_context: self.service = service_with_ta_context else: self.service = common_util.create_splunk_service(session_key, splunkd_uri, appname) if not service_with_tab_context: service_with_tab_context = common_util.create_splunk_service(session_key, splunkd_uri) self.meta_mgr = meta_manager.create_meta_manager(session_key, splunkd_uri, meta_const.CIM_MAPPING_BUILDER, appname) self.conf_mgr = common_util.create_conf_mgr(session_key, splunkd_uri, self.appname) self.tab_conf_mgr = tab_conf_manager.create_tab_conf_manager(session_key, splunkd_uri, appname) self.tab_conf_mgr_with_tab_context = tab_conf_manager.create_tab_conf_manager(session_key, splunkd_uri, builder_constant.ADDON_BUILDER_APP_NAME)
def execute(self, validation_context): self.splunk_endpoint = validation_context.get_splunk_endpoint() self.splunk_session_key = validation_context.get_splunk_session_key() self.service = common_util.create_splunk_service( self.splunk_session_key, self.splunk_endpoint) self.app_name = validation_context.get_global_property("app_name") # self.conf_mgr = common_util.create_conf_mgr(self.splunk_session_key, self.splunk_endpoint, self.app_name) self.tab_conf_mgr = tab_conf_manager.TabConfMgr( self.splunk_endpoint, self.splunk_session_key, self.app_name, service=None) self.ta_builder_conf_mgr = common_util.create_conf_mgr( self.splunk_session_key, self.splunk_endpoint, app=TA_BUILDER_APP_NAME) self.vid = validation_context.get_job_id() self.temp_mgr = TempManager() validation_utility.unlock_props(self.app_name)
def get_current_creating_app_sourcetype(): conf_manager = common_util.create_conf_mgr( cherrypy.session.get("sessionKey"), scc.getMgmtUri()) return conf_manager.get_conf(tabuilder_status_conf).get( create_ta_stanza).get(st_key, None)