Skip to content

Commit 519ad53

Browse files
committed
chore: add docs, changes after review
1 parent e2a5712 commit 519ad53

File tree

17 files changed

+2262
-38
lines changed

17 files changed

+2262
-38
lines changed

docs/release_7_0_0.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Removed usage of btool command from solnlib
2+
3+
As of version 7.0.0, the `btool` command has been removed from solnlib. Configuration stanzas and keys should now be accessed via the REST API.
4+
Additionally, the `splunkenv` module can only be used in environments where Splunk is installed, as it relies on Splunk-specific methods for making internal calls.
5+
6+
## Session key is now mandatory in some of the functions
7+
8+
The affected functions now require a valid `session_key` to operate correctly. While solnlib attempts to retrieve the `session_key` automatically,
9+
there are scenarios—such as within a modular input script—where this is not possible. In such cases, you must explicitly provide the `session_key`
10+
to ensure proper authorization. Affected functions are:
11+
12+
* `get_splunk_host_info`
13+
* `get_splunkd_access_info`
14+
* `get_scheme_from_hec_settings`
15+
* `get_splunkd_uri`
16+
* `get_conf_key_value`
17+
* `get_conf_stanza`
18+
19+
## Changed arguments in `get_conf_key_value` and `get_conf_stanza`
20+
21+
As of version 7.0.0, the following changes have been made to the function:
22+
23+
`get_conf_key_value` now requires 4 mandatory arguments:
24+
25+
* `conf_name`
26+
* `stanza`
27+
* `key`
28+
* `app_name` (new)
29+
30+
`get_conf_stanza` now requires 3 mandatory arguments:
31+
32+
* `conf_name`
33+
* `stanza`
34+
* `app_name` (new)
35+
36+
Both functions also accept the following optional arguments:
37+
38+
* `session_key` - Used for authentication. If not provided, a 401 Unauthorized error may occur depending on the context.
39+
* `users` - Limits results returned by the configuration endpoint. Defaults to `nobody`.
40+
* `raw_output` - If set to `True`, the full decoded JSON response is returned.
41+
This should be enabled when `app_name` is set to the global context `(/-/)`, as the Splunk REST API may return multiple entries in that case.
42+
43+
## The `get_session_key` function has been removed from solnlib
44+
45+
This function relied on reading the `scheme`, `host` and `port` using the deprecated btool utility, which is no longer supported.
46+

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ 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
3435
- References:
3536
- modular_input:
3637
- "checkpointer.py": modular_input/checkpointer.md

solnlib/splunkenv.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
except ImportError:
3535

3636
def simpleRequest(*args, **kwargs):
37-
return None, None
37+
raise ImportError("This module requires Splunk to be installed.")
3838

3939

4040
try:
@@ -81,8 +81,6 @@ def msp(*args, **kwargs):
8181
os.path.join("var", "run", "splunk", "lookup_tmp"),
8282
]
8383

84-
use_btool = bool(use_btool)
85-
8684

8785
class SessionKeyNotFound(Exception):
8886
pass
@@ -242,7 +240,7 @@ def get_conf_key_value(
242240
key: str,
243241
app_name: str,
244242
session_key: Optional[str] = None,
245-
user: Optional[str] = "nobody",
243+
user: str = "nobody",
246244
raw_output: Optional[bool] = False,
247245
) -> Union[str, List, dict]:
248246
"""Get value of `key` of `stanza` in `conf_name`.
@@ -288,7 +286,7 @@ def get_conf_stanza(
288286
stanza: str,
289287
app_name: str,
290288
session_key: Optional[str] = None,
291-
user: Optional[str] = "nobody",
289+
user: str = "nobody",
292290
raw_output: Optional[bool] = False,
293291
) -> dict:
294292
"""Get `stanza` in `conf_name`.
@@ -379,7 +377,7 @@ def _get_conf_stanzas_from_splunk_api(
379377
conf_name: str,
380378
app_name: str,
381379
session_key: Optional[str] = None,
382-
user: Optional[str] = "nobody",
380+
user: str = "nobody",
383381
stanza: Optional[str] = None,
384382
) -> dict:
385383
"""Get stanzas of `conf_name` using splunk API:

tests/integration/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ def setup_env():
2121
@pytest.fixture(scope="session")
2222
def session_key():
2323
return context.get_session_key()
24+
25+
26+
def mock_splunk(monkeypatch):
27+
def simple_requests(url, *args, **kwargs):
28+
from splunk.rest import simpleRequest
29+
30+
return simpleRequest(url, *args, **kwargs)
31+
32+
def make_splunkn_home(url, *args, **kwargs):
33+
from splunk.clilib.bundle_paths import make_splunkhome_path
34+
35+
return make_splunkhome_path(url, *args, **kwargs)
36+
37+
monkeypatch.setattr("solnlib.splunkenv.simpleRequest", simple_requests)
38+
monkeypatch.setattr("solnlib.splunkenv.msp", make_splunkn_home)

tests/integration/context.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,3 @@ def get_session_key():
3535
)
3636
content = response.json()
3737
return content["sessionKey"]
38-
39-
40-
def mock_splunk(monkeypatch):
41-
def simple_requests(url, *args, **kwargs):
42-
from splunk.rest import simpleRequest
43-
44-
return simpleRequest(url, *args, **kwargs)
45-
46-
def make_splunkn_home(url, *args, **kwargs):
47-
from splunk.clilib.bundle_paths import make_splunkhome_path
48-
49-
return make_splunkhome_path(url, *args, **kwargs)
50-
51-
monkeypatch.setattr("solnlib.splunkenv.simpleRequest", simple_requests)
52-
monkeypatch.setattr("solnlib.splunkenv.msp", make_splunkn_home)

tests/integration/test_conf_manager.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616

1717
import context
18+
import conftest
1819
import pytest
1920
from solnlib import conf_manager, soln_exceptions
2021
from unittest import mock
@@ -145,7 +146,7 @@ def test_conf_manager_update_conf_with_encrypted_keys():
145146

146147

147148
def test_get_log_level(monkeypatch):
148-
context.mock_splunk(monkeypatch)
149+
conftest.mock_splunk(monkeypatch)
149150

150151
session_key = context.get_session_key()
151152
expected_log_level = "DEBUG"
@@ -162,7 +163,7 @@ def test_get_log_level(monkeypatch):
162163

163164

164165
def test_get_log_level_incorrect_log_level_field(monkeypatch):
165-
context.mock_splunk(monkeypatch)
166+
conftest.mock_splunk(monkeypatch)
166167

167168
session_key = context.get_session_key()
168169
expected_log_level = "INFO"
@@ -178,7 +179,7 @@ def test_get_log_level_incorrect_log_level_field(monkeypatch):
178179

179180

180181
def test_get_proxy_dict(monkeypatch):
181-
context.mock_splunk(monkeypatch)
182+
conftest.mock_splunk(monkeypatch)
182183

183184
session_key = context.get_session_key()
184185
expected_proxy_dict = VALID_PROXY_DICT
@@ -192,7 +193,7 @@ def test_get_proxy_dict(monkeypatch):
192193

193194

194195
def test_invalid_proxy_port(monkeypatch):
195-
context.mock_splunk(monkeypatch)
196+
conftest.mock_splunk(monkeypatch)
196197

197198
session_key = context.get_session_key()
198199

@@ -208,7 +209,7 @@ def test_invalid_proxy_port(monkeypatch):
208209

209210

210211
def test_invalid_proxy_host(monkeypatch):
211-
context.mock_splunk(monkeypatch)
212+
conftest.mock_splunk(monkeypatch)
212213

213214
session_key = context.get_session_key()
214215

@@ -236,7 +237,7 @@ def test_conf_manager_exception():
236237

237238

238239
def test_conf_stanza_not_exist_exception(monkeypatch):
239-
context.mock_splunk(monkeypatch)
240+
conftest.mock_splunk(monkeypatch)
240241

241242
session_key = context.get_session_key()
242243

tests/integration/test_hec_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616

1717
import context
18+
import conftest
1819
import os.path as op
1920
import sys
2021

@@ -24,7 +25,7 @@
2425

2526

2627
def test_hec_config(monkeypatch):
27-
context.mock_splunk(monkeypatch)
28+
conftest.mock_splunk(monkeypatch)
2829

2930
session_key = context.get_session_key()
3031
config = hec_config.HECConfig(session_key)

tests/integration/test_hec_event_writer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515
#
1616
import context
17+
import conftest
1718
import os.path as op
1819
import sys
1920
import time
@@ -26,7 +27,7 @@
2627

2728

2829
def test_hec_event_writer(monkeypatch):
29-
context.mock_splunk(monkeypatch)
30+
conftest.mock_splunk(monkeypatch)
3031

3132
session_key = context.get_session_key()
3233

@@ -43,7 +44,7 @@ def test_hec_event_writer(monkeypatch):
4344

4445

4546
def test_hec_event_writes_with_non_utf_8(monkeypatch):
46-
context.mock_splunk(monkeypatch)
47+
conftest.mock_splunk(monkeypatch)
4748

4849
# To test scenario listed in https://github.com/splunk/addonfactory-solutions-library-python/pull/112.
4950
test_name = "test_hec_event_writes_with_non_utf_8"

tests/integration/test_splunkenv.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616

1717
import context
18+
import conftest
1819
import os
1920
import os.path as op
2021
import sys
@@ -24,7 +25,7 @@
2425

2526

2627
def test_splunkenv(monkeypatch):
27-
context.mock_splunk(monkeypatch)
28+
conftest.mock_splunk(monkeypatch)
2829

2930
assert "SPLUNK_HOME" in os.environ
3031

tests/integration/test_time_parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616

1717
import context
18+
import conftest
1819
import datetime
1920
import os.path as op
2021
import sys
@@ -25,7 +26,7 @@
2526

2627

2728
def test_time_parser(monkeypatch):
28-
context.mock_splunk(monkeypatch)
29+
conftest.mock_splunk(monkeypatch)
2930

3031
session_key = context.get_session_key()
3132
tp = time_parser.TimeParser(session_key)

tests/integration/test_user_access.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616

1717
import context
18+
import conftest
1819
import os.path as op
1920
import sys
2021

@@ -115,7 +116,7 @@ def test_app_capability_manager():
115116

116117

117118
def test_check_user_access(monkeypatch):
118-
context.mock_splunk(monkeypatch)
119+
conftest.mock_splunk(monkeypatch)
119120

120121
session_key = context.get_session_key()
121122
app_capabilities = {

tests/unit/common.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616
import os.path as op
1717
import socket
18+
import subprocess
1819

1920
from splunklib import binding, client
2021
from splunklib.data import record
@@ -30,6 +31,46 @@
3031

3132

3233
def mock_splunkhome(monkeypatch):
34+
class MockPopen:
35+
def __init__(
36+
self,
37+
args,
38+
bufsize=0,
39+
executable=None,
40+
stdin=None,
41+
stdout=None,
42+
stderr=None,
43+
preexec_fn=None,
44+
close_fds=False,
45+
shell=False,
46+
cwd=None,
47+
env=None,
48+
universal_newlines=False,
49+
startupinfo=None,
50+
creationflags=0,
51+
):
52+
self._conf = args[3]
53+
54+
def communicate(self, input=None):
55+
if self._conf == "server":
56+
file_path = op.sep.join(
57+
[cur_dir, "data/mock_splunk/etc/system/default/server.conf"]
58+
)
59+
elif self._conf == "inputs":
60+
file_path = op.sep.join(
61+
[
62+
cur_dir,
63+
"data/mock_splunk/etc/apps/splunk_httpinput/local/inputs.conf",
64+
]
65+
)
66+
else:
67+
file_path = op.sep.join(
68+
[cur_dir, "data/mock_splunk/etc/system/default/web.conf"]
69+
)
70+
71+
with open(file_path) as fp:
72+
return fp.read(), None
73+
3374
def simple_requests(url, *args, **kwargs):
3475
if "conf-server/sslConfig" in url:
3576
file_path = op.sep.join(
@@ -64,6 +105,7 @@ def get_session_key():
64105
monkeypatch.setenv("SPLUNK_ETC", op.join(splunk_home, "etc"))
65106
monkeypatch.setattr("solnlib.splunkenv.simpleRequest", simple_requests)
66107
monkeypatch.setattr("solnlib.splunkenv.getSessionKey", get_session_key)
108+
monkeypatch.setattr(subprocess, "Popen", MockPopen)
67109

68110

69111
def mock_serverinfo(monkeypatch):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[http]
2+
disabled = 1
3+
enableSSL = 1

0 commit comments

Comments
 (0)