Skip to content

Commit 2e3b096

Browse files
Revert "BREAKING CHANGE: remove btool and replace it with splunk.rest"
1 parent a17a399 commit 2e3b096

27 files changed

+266
-695
lines changed

docs/release_7_0_0.md

Lines changed: 0 additions & 46 deletions
This file was deleted.

mkdocs.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ plugins:
3131
nav:
3232
- Home: index.md
3333
- Release 6.0.0: release_6_0_0.md
34-
- Release 7.0.0: release_7_0_0.md
3534
- References:
3635
- modular_input:
3736
- "checkpointer.py": modular_input/checkpointer.md

solnlib/_settings.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

solnlib/credentials.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@
1616

1717
"""This module contains Splunk credential related interfaces."""
1818

19+
import json
1920
import re
2021
import warnings
2122
from typing import Dict, List
2223

2324
from splunklib import binding, client
2425

2526
from . import splunk_rest_client as rest_client
27+
from .net_utils import validate_scheme_host_port
28+
from .splunkenv import get_splunkd_access_info
2629
from .utils import retry
2730

2831
__all__ = [
2932
"CredentialException",
3033
"CredentialNotExistException",
3134
"CredentialManager",
35+
"get_session_key",
3236
]
3337

3438

@@ -335,3 +339,56 @@ def _get_all_passwords(self) -> List[Dict[str, str]]:
335339
)
336340
passwords = self._storage_passwords.list(count=-1)
337341
return self._get_clear_passwords(passwords)
342+
343+
344+
@retry(exceptions=[binding.HTTPError])
345+
def get_session_key(
346+
username: str,
347+
password: str,
348+
scheme: str = None,
349+
host: str = None,
350+
port: int = None,
351+
**context: dict,
352+
) -> str:
353+
"""Get splunkd access token.
354+
355+
Arguments:
356+
username: The Splunk account username, which is used to authenticate the Splunk instance.
357+
password: The Splunk account password.
358+
scheme: (optional) The access scheme, default is None.
359+
host: (optional) The host name, default is None.
360+
port: (optional) The port number, default is None.
361+
context: Other configurations for Splunk rest client.
362+
363+
Returns:
364+
Splunk session key.
365+
366+
Raises:
367+
CredentialException: If username/password are invalid.
368+
ValueError: if scheme, host or port are invalid.
369+
370+
Examples:
371+
>>> get_session_key('user', 'password')
372+
"""
373+
validate_scheme_host_port(scheme, host, port)
374+
375+
if any([scheme is None, host is None, port is None]):
376+
scheme, host, port = get_splunkd_access_info()
377+
378+
uri = "{scheme}://{host}:{port}/{endpoint}".format(
379+
scheme=scheme, host=host, port=port, endpoint="services/auth/login"
380+
)
381+
_rest_client = rest_client.SplunkRestClient(
382+
None, "-", "nobody", scheme, host, port, **context
383+
)
384+
try:
385+
response = _rest_client.http.post(
386+
uri, username=username, password=password, output_mode="json"
387+
)
388+
except binding.HTTPError as e:
389+
if e.status != 401:
390+
raise
391+
392+
raise CredentialException("Invalid username/password.")
393+
394+
return json.loads(response.body.read())["sessionKey"]

solnlib/modular_input/event_writer.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import threading
2424
import time
2525
import traceback
26-
import warnings
2726
from abc import ABCMeta, abstractmethod
2827
from random import randint
2928
from typing import List, Union
@@ -40,16 +39,6 @@
4039
__all__ = ["ClassicEventWriter", "HECEventWriter"]
4140

4241

43-
deprecation_msg = (
44-
"Function 'create_from_token' is deprecated and incompatible with 'global_settings_schema=True'. "
45-
"Use 'create_from_token_with_session_key' instead."
46-
)
47-
48-
49-
class FunctionDeprecated(Exception):
50-
pass
51-
52-
5342
class EventWriter(metaclass=ABCMeta):
5443
"""Base class of event writer."""
5544

@@ -244,13 +233,13 @@ def __init__(
244233
scheme, host, hec_port = utils.extract_http_scheme_host_port(hec_uri)
245234
else:
246235
if not all([scheme, host, port]):
247-
scheme, host, port = get_splunkd_access_info(self._session_key)
236+
scheme, host, port = get_splunkd_access_info()
248237
hec_port, hec_token = self._get_hec_config(
249238
hec_input_name, session_key, scheme, host, port, **context
250239
)
251240

252241
if global_settings_schema:
253-
scheme = get_scheme_from_hec_settings(self._session_key)
242+
scheme = get_scheme_from_hec_settings()
254243

255244
if not context.get("pool_connections"):
256245
context["pool_connections"] = 10
@@ -283,11 +272,6 @@ def create_from_token(
283272
Created HECEventWriter.
284273
"""
285274

286-
warnings.warn(deprecation_msg, DeprecationWarning, stacklevel=2)
287-
288-
if global_settings_schema:
289-
raise FunctionDeprecated(deprecation_msg)
290-
291275
return HECEventWriter(
292276
None,
293277
None,

solnlib/server_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(
7878
"""
7979
is_localhost = False
8080
if not all([scheme, host, port]) and os.environ.get("SPLUNK_HOME"):
81-
scheme, host, port = get_splunkd_access_info(session_key)
81+
scheme, host, port = get_splunkd_access_info()
8282
is_localhost = (
8383
host == "localhost" or host == "127.0.0.1" or host in ("::1", "[::1]")
8484
)

solnlib/splunk_rest_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def __init__(
221221
"""
222222
# Only do splunkd URI discovery in SPLUNK env (SPLUNK_HOME is set).
223223
if not all([scheme, host, port]) and os.environ.get("SPLUNK_HOME"):
224-
scheme, host, port = get_splunkd_access_info(session_key)
224+
scheme, host, port = get_splunkd_access_info()
225225
if os.environ.get("SPLUNK_HOME") is None:
226226
if not all([scheme, host, port]):
227227
raise ValueError(

0 commit comments

Comments
 (0)