def __init__(self,
                 hec_input_name,
                 session_key,
                 scheme=None,
                 host=None,
                 port=None,
                 hec_uri=None,
                 hec_token=None,
                 **context):
        super(HECEventWriter, self).__init__()
        self._session_key = session_key

        if not all([scheme, host, port]):
            scheme, host, port = get_splunkd_access_info()

        if hec_uri and hec_token:
            scheme, host, hec_port = utils.extract_http_scheme_host_port(
                hec_uri)
        else:
            hec_port, hec_token = self._get_hec_config(hec_input_name,
                                                       session_key, scheme,
                                                       host, port, **context)

        if not context.get('pool_connections'):
            context['pool_connections'] = 10

        if not context.get('pool_maxsize'):
            context['pool_maxsize'] = 10

        self._rest_client = rest_client.SplunkRestClient(hec_token,
                                                         app='-',
                                                         scheme=scheme,
                                                         host=host,
                                                         port=hec_port,
                                                         **context)
Beispiel #2
0
 def handleEdit(self, confInfo):
     logger.info("start edit setup configure.")
     scheme, host, port = utils.extract_http_scheme_host_port(
         scc.getMgmtUri())
     conf_mgr = conf.ConfManager(self.getSessionKey(),
                                 self.appName,
                                 scheme=scheme,
                                 host=host,
                                 port=port)
     ta_conf_file = get_or_create_conf_file(conf_mgr, setup_const.myta_conf)
     customized_conf_file = get_or_create_conf_file(
         conf_mgr, setup_const.myta_customized_conf)
     all_origin_settings = ta_conf_file.get_all()
     all_settings = utils.escape_json_control_chars(
         self.callerArgs.data[setup_const.all_settings][0])
     all_settings = json.loads(all_settings)
     # write global and proxy settings
     self._updateGlobalSettings(setup_const.global_settings, all_settings,
                                all_origin_settings, ta_conf_file)
     self._updateGlobalSettings(setup_const.proxy_settings, all_settings,
                                all_origin_settings, ta_conf_file)
     # write customized settings
     customized_conf_file = get_or_create_conf_file(
         conf_mgr, setup_const.myta_customized_conf)
     self._updateConfStanzas(
         all_settings.get(setup_const.myta_customized_settings, {}),
         customized_conf_file, self.encrypt_fields_customized)
     logger.info("edit setup configure is done")
Beispiel #3
0
    def handleList(self, confInfo):
        logger.info("start list setup configure.")
        scheme, host, port = utils.extract_http_scheme_host_port(
            scc.getMgmtUri())
        conf_mgr = conf.ConfManager(self.getSessionKey(),
                                    self.appName,
                                    scheme=scheme,
                                    host=host,
                                    port=port)
        ta_conf_file = get_or_create_conf_file(conf_mgr, setup_const.myta_conf)
        # read globala and proxy settings
        all_settings = ta_conf_file.get_all()
        if not all_settings:
            all_settings = {}
        self._setNoneValues(all_settings.get(setup_const.global_settings, {}))
        # customized conf
        customized_conf_file = get_or_create_conf_file(
            conf_mgr, setup_const.myta_customized_conf)
        settings = customized_conf_file.get_all()
        all_settings[setup_const.myta_customized_settings] = settings

        self._clearPasswords(all_settings, self.cred_fields)
        all_settings = filter_eai_property(all_settings)
        all_settings = json.dumps(all_settings)
        all_settings = utils.escape_json_control_chars(all_settings)
        confInfo[setup_const.myta_settings].append(setup_const.all_settings,
                                                   all_settings)
        logger.info("list setup configure is done.")
Beispiel #4
0
    def from_server_uri(cls, server_uri: str, session_key: str,
                        **context: Any) -> "ServerInfo":
        """Creates ServerInfo class using server_uri and session_key.

        Note: splunktalib uses these parameters to create it's ServerInfo class,
        so this method should ease the transition from splunktalib to solnlib.

        Arguments:
            server_uri: splunkd URI.
            session_key: Splunk access token.
            context: Other configurations for Splunk rest client.

        Returns:
            An instance of `ServerInfo`.

        Raises:
            ValueError: server_uri is in the wrong format.
        """
        scheme, host, port = utils.extract_http_scheme_host_port(server_uri)
        return ServerInfo(
            session_key,
            scheme=scheme,
            host=host,
            port=port,
            **context,
        )
Beispiel #5
0
 def __init__(self, uri, session_key, logger=None):
     self.__uri = uri
     self.__session_key = session_key
     self.__logger = logger
     self.scheme, self.host, self.port = utils.extract_http_scheme_host_port(
         self.__uri)
     self.__cached_global_settings = {}
     self.__global_config = None
Beispiel #6
0
 def __init__(self, uri, session_key, logger=None):
     self.__uri = uri
     self.__session_key = session_key
     self.__logger = logger
     self.scheme, self.host, self.port = utils.extract_http_scheme_host_port(
         self.__uri)
     self.encrypt_fields_credential = (setup_const.password, )
     self.encrypt_fields_customized = (setup_const.password, )
     self.cred_confs = ((setup_const.myta_credential_settings,
                         setup_const.myta_credential_conf), )
     self.__cached_global_settings = None
Beispiel #7
0
def get_session_key(splunk_info):
    if splunk_info.session_key:
        return splunk_info.session_key

    scheme, host, port = sutils.extract_http_scheme_host_port(
        splunk_info.mgmt_uri)
    service = sc.connect(scheme=scheme,
                         host=host,
                         port=port,
                         username=splunk_info.username,
                         password=splunk_info.password)
    return service.token
Beispiel #8
0
 def _init_ckpt(self):
     if self.ckpt is None:
         if 'AOB_TEST' in os.environ:
             ckpt_dir = self.context_meta.get('checkpoint_dir', tempfile.mkdtemp())
             if not os.path.exists(ckpt_dir):
                 os.makedirs(ckpt_dir)
             self.ckpt = checkpointer.FileCheckpointer(ckpt_dir)
         else:
             if 'server_uri' not in self.context_meta:
                 raise ValueError('server_uri not found in input meta.')
             if 'session_key' not in self.context_meta:
                 raise ValueError('session_key not found in input meta.')
             dscheme, dhost, dport = sutils.extract_http_scheme_host_port(self.context_meta[
                                                                              'server_uri'])
             self.ckpt = checkpointer.KVStoreCheckpointer(self.app + "_checkpointer",
                                                          self.context_meta['session_key'], self.app,
                                                          scheme=dscheme, host=dhost, port=dport)
Beispiel #9
0
def test_extract_http_scheme_host_port(monkeypatch):
    h1 = "https://localhost:8089"
    scheme, host, port = utils.extract_http_scheme_host_port(h1)
    assert scheme == "https" and host == "localhost" and port == 8089

    h2 = "https://localhost:8089/"
    scheme, host, port = utils.extract_http_scheme_host_port(h2)
    assert scheme == "https" and host == "localhost" and port == 8089

    h3 = "https://localhost:8089/servicesNS/"
    scheme, host, port = utils.extract_http_scheme_host_port(h3)
    assert scheme == "https" and host == "localhost" and port == 8089

    h1 = "http://localhost:8089"
    scheme, host, port = utils.extract_http_scheme_host_port(h1)
    assert scheme == "http" and host == "localhost" and port == 8089

    h2 = "http://localhost:8089/"
    scheme, host, port = utils.extract_http_scheme_host_port(h2)
    assert scheme == "http" and host == "localhost" and port == 8089

    h3 = "http://localhost:8089/servicesNS/"
    scheme, host, port = utils.extract_http_scheme_host_port(h3)
    assert scheme == "http" and host == "localhost" and port == 8089

    invalid = "localhost:8089"
    try:
        scheme, host, port = utils.extract_http_scheme_host_port(invalid)
    except ValueError:
        pass
    else:
        assert 0

    invalid = None
    try:
        scheme, host, port = utils.extract_http_scheme_host_port(invalid)
    except Exception:
        pass
    else:
        assert 0
Beispiel #10
0
    def __init__(self, splunk_info, appname, owner='nobody'):
        '''
        :param splunk_info: `SplunkInfo` object
        :param appname: application/addon name
        :param owner: owner of the configuration, 'nobody' means globally
        shared
        '''

        self.appname = appname
        self.owner = owner
        self.splunk_info = splunk_info
        session_key = get_session_key(splunk_info)
        scheme, host, port = sutils.extract_http_scheme_host_port(
            splunk_info.mgmt_uri)
        self.rest_client = src.SplunkRestClient(session_key=session_key,
                                                app=appname,
                                                owner=owner,
                                                scheme=scheme,
                                                host=host,
                                                port=port)
    def validate(splunk):
        if not isinstance(splunk, dict):
            logger.error(
                'Invalid splunk-info file. Expect splunk as a dict object',
                got=splunk)
            raise ValueError('Invalid splunk-info file')

        requires = ['hostname', 'mgmt_uri', 'username', 'password']
        check_required(splunk, requires, 'Invalid splunk-info file')

        scheme, host, port = sutils.extract_http_scheme_host_port(
            splunk["mgmt_uri"])
        # Verify username, password, and mgmt_uri
        try:
            sc.connect(scheme=scheme, host=host, port=port,
                       username=splunk['username'],
                       password=splunk['password'])
        except Exception as e:
            logger.error('Invalid splunk-info file, credentials or URI are '
                         'probably wrong', error=e.message)
            raise ValueError('Invalid splunk-info file')
Beispiel #12
0
 def handleEdit(self, confInfo):
     logger.info("start edit setup configure.")
     scheme, host, port = utils.extract_http_scheme_host_port(
         scc.getMgmtUri())
     conf_mgr = conf.ConfManager(self.getSessionKey(),
                                 self.appName,
                                 scheme=scheme,
                                 host=host,
                                 port=port)
     ta_conf_file = get_or_create_conf_file(conf_mgr, setup_const.myta_conf)
     customized_conf_file = get_or_create_conf_file(
         conf_mgr, setup_const.myta_customized_conf)
     all_origin_settings = ta_conf_file.get_all()
     all_settings = utils.escape_json_control_chars(
         self.callerArgs.data[setup_const.all_settings][0])
     all_settings = json.loads(all_settings)
     # write global and proxy settings
     self._updateGlobalSettings(setup_const.global_settings, all_settings,
                                all_origin_settings, ta_conf_file)
     # write customized settings
     customized_conf_file = get_or_create_conf_file(
         conf_mgr, setup_const.myta_customized_conf)
     self._updateConfStanzas(
         all_settings.get(setup_const.myta_customized_settings, {}),
         customized_conf_file, self.encrypt_fields_customized)
     # write account credential settings
     for cred, conf_file in self.cred_confs:
         cred_conf_file = get_or_create_conf_file(conf_mgr, conf_file)
         creds = all_settings.get(cred, {})
         if creds == setup_const.ignore_backend_req:
             logger.info("Ignore backend rest request")
             continue
         if creds:
             self._updateConfStanzas(creds, cred_conf_file,
                                     self.encrypt_fields_credential)
     logger.info("edit setup configure is done")