Skip to content

splunk_rest_client.py

This module proxy all REST call to splunklib SDK, it handles proxy, certs etc in this centralized location.

All clients should use SplunkRestProxy to do REST call instead of calling splunklib SDK directly in business logic code.

MAX_REQUEST_RETRIES = 5 module-attribute

__all__ = ['SplunkRestClient'] module-attribute

SplunkRestClient

Bases: client.Service

Splunk REST client.

Source code in solnlib/splunk_rest_client.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
class SplunkRestClient(client.Service):
    """Splunk REST client."""

    def __init__(
        self,
        session_key: str,
        app: str,
        owner: str = "nobody",
        scheme: str = None,
        host: str = None,
        port: int = None,
        **context: dict,
    ):
        """Initializes SplunkRestClient.

        Arguments `scheme`, `host` and `port` are optional in the Splunk
        environment (when environment variable SPLUNK_HOME is set). In this
        situation `get_splunkd_access_info` will be used to set `scheme`,
        `host` and `port`. In case of using `SplunkRestClient` outside of
        Splunk environment - `scheme`, `host` and `port` should be provided.

        Arguments:
            session_key: Splunk access token.
            app: App name of namespace.
            owner: Owner of namespace, default is `nobody`.
            scheme: The access scheme, default is None.
            host: The host name, default is None.
            port: The port number, default is None.
            context: Other configurations, it can contain `proxy_hostname`,
                `proxy_port`, `proxy_username`, `proxy_password`, then proxy will
                be accounted and setup, all REST APIs to splunkd will be through
                the proxy. If `context` contains `key_file`, `cert_file`, then
                certification will be accounted and setup, all REST APIs to splunkd
                will use certification. If `context` contains `pool_connections`,
                `pool_maxsize`, then HTTP connection will be pooled.

        Raises:
            ValueError: if scheme, host or port are invalid.
        """
        # Only do splunkd URI discovery in SPLUNK env (SPLUNK_HOME is set).
        if not all([scheme, host, port]) and os.environ.get("SPLUNK_HOME"):
            scheme, host, port = get_splunkd_access_info()
        if os.environ.get("SPLUNK_HOME") is None:
            if not all([scheme, host, port]):
                raise ValueError(
                    "scheme, host, port should be provided outside of Splunk environment"
                )

        validate_scheme_host_port(scheme, host, port)
        if host == "[::1]":
            host = "::1"

        handler = _request_handler(context)
        super().__init__(
            handler=handler,
            scheme=scheme,
            host=host,
            port=port,
            token=session_key,
            app=app,
            owner=owner,
            autologin=True,
        )

__init__(session_key, app, owner='nobody', scheme=None, host=None, port=None, **context)

Initializes SplunkRestClient.

Arguments scheme, host and port are optional in the Splunk environment (when environment variable SPLUNK_HOME is set). In this situation get_splunkd_access_info will be used to set scheme, host and port. In case of using SplunkRestClient outside of Splunk environment - scheme, host and port should be provided.

Parameters:

Name Type Description Default
session_key str

Splunk access token.

required
app str

App name of namespace.

required
owner str

Owner of namespace, default is nobody.

'nobody'
scheme str

The access scheme, default is None.

None
host str

The host name, default is None.

None
port int

The port number, default is None.

None
context dict

Other configurations, it can contain proxy_hostname, proxy_port, proxy_username, proxy_password, then proxy will be accounted and setup, all REST APIs to splunkd will be through the proxy. If context contains key_file, cert_file, then certification will be accounted and setup, all REST APIs to splunkd will use certification. If context contains pool_connections, pool_maxsize, then HTTP connection will be pooled.

{}

Raises:

Type Description
ValueError

if scheme, host or port are invalid.

Source code in solnlib/splunk_rest_client.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
def __init__(
    self,
    session_key: str,
    app: str,
    owner: str = "nobody",
    scheme: str = None,
    host: str = None,
    port: int = None,
    **context: dict,
):
    """Initializes SplunkRestClient.

    Arguments `scheme`, `host` and `port` are optional in the Splunk
    environment (when environment variable SPLUNK_HOME is set). In this
    situation `get_splunkd_access_info` will be used to set `scheme`,
    `host` and `port`. In case of using `SplunkRestClient` outside of
    Splunk environment - `scheme`, `host` and `port` should be provided.

    Arguments:
        session_key: Splunk access token.
        app: App name of namespace.
        owner: Owner of namespace, default is `nobody`.
        scheme: The access scheme, default is None.
        host: The host name, default is None.
        port: The port number, default is None.
        context: Other configurations, it can contain `proxy_hostname`,
            `proxy_port`, `proxy_username`, `proxy_password`, then proxy will
            be accounted and setup, all REST APIs to splunkd will be through
            the proxy. If `context` contains `key_file`, `cert_file`, then
            certification will be accounted and setup, all REST APIs to splunkd
            will use certification. If `context` contains `pool_connections`,
            `pool_maxsize`, then HTTP connection will be pooled.

    Raises:
        ValueError: if scheme, host or port are invalid.
    """
    # Only do splunkd URI discovery in SPLUNK env (SPLUNK_HOME is set).
    if not all([scheme, host, port]) and os.environ.get("SPLUNK_HOME"):
        scheme, host, port = get_splunkd_access_info()
    if os.environ.get("SPLUNK_HOME") is None:
        if not all([scheme, host, port]):
            raise ValueError(
                "scheme, host, port should be provided outside of Splunk environment"
            )

    validate_scheme_host_port(scheme, host, port)
    if host == "[::1]":
        host = "::1"

    handler = _request_handler(context)
    super().__init__(
        handler=handler,
        scheme=scheme,
        host=host,
        port=port,
        token=session_key,
        app=app,
        owner=owner,
        autologin=True,
    )