Skip to content

Commit fef1d80

Browse files
feat: HECEventWriter can get scheme from HEC global settings (#351)
Jira: https://splunk.atlassian.net/browse/ADDON-68827 Add schema calculations based on global HEC settings for HECEventWriter --------- Co-authored-by: Artem Rys <[email protected]>
1 parent 5a71f28 commit fef1d80

File tree

6 files changed

+84
-8
lines changed

6 files changed

+84
-8
lines changed

solnlib/modular_input/event_writer.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from .. import splunk_rest_client as rest_client
3333
from .. import utils
3434
from ..hec_config import HECConfig
35-
from ..splunkenv import get_splunkd_access_info
35+
from ..splunkenv import get_splunkd_access_info, get_scheme_from_hec_settings
3636
from ..utils import retry
3737
from .event import HECEvent, XMLEvent
3838

@@ -203,6 +203,7 @@ def __init__(
203203
port: int = None,
204204
hec_uri: str = None,
205205
hec_token: str = None,
206+
global_settings_schema: bool = True,
206207
logger: logging.Logger = None,
207208
**context: dict
208209
):
@@ -217,6 +218,7 @@ def __init__(
217218
hec_uri: (optional) If hec_uri and hec_token are provided, they will
218219
higher precedence than hec_input_name.
219220
hec_token: (optional) HEC token.
221+
global_settings_schema: (optional) if True, scheme will be set based on HEC global settings, default False.
220222
logger: Logger object.
221223
context: Other configurations for Splunk rest client.
222224
"""
@@ -237,6 +239,9 @@ def __init__(
237239
hec_input_name, session_key, scheme, host, port, **context
238240
)
239241

242+
if global_settings_schema:
243+
scheme = get_scheme_from_hec_settings()
244+
240245
if not context.get("pool_connections"):
241246
context["pool_connections"] = 10
242247

@@ -249,7 +254,10 @@ def __init__(
249254

250255
@staticmethod
251256
def create_from_token(
252-
hec_uri: str, hec_token: str, **context: dict
257+
hec_uri: str,
258+
hec_token: str,
259+
global_settings_schema: bool = False,
260+
**context: dict
253261
) -> "HECEventWriter":
254262
"""Given HEC URI and HEC token, create HECEventWriter object. This
255263
function simplifies the standalone mode HECEventWriter usage (not in a
@@ -258,6 +266,7 @@ def create_from_token(
258266
Arguments:
259267
hec_uri: HTTP Event Collector URI, like https://localhost:8088.
260268
hec_token: HTTP Event Collector token.
269+
global_settings_schema: (optional) if True, scheme will be set based on HEC global settings, default False.
261270
context: Other configurations.
262271
263272
Returns:
@@ -272,12 +281,17 @@ def create_from_token(
272281
None,
273282
hec_uri=hec_uri,
274283
hec_token=hec_token,
284+
global_settings_schema=global_settings_schema,
275285
**context
276286
)
277287

278288
@staticmethod
279289
def create_from_input(
280-
hec_input_name: str, splunkd_uri: str, session_key: str, **context: dict
290+
hec_input_name: str,
291+
splunkd_uri: str,
292+
session_key: str,
293+
global_settings_schema: bool = False,
294+
**context: dict
281295
) -> "HECEventWriter":
282296
"""Given HEC input stanza name, splunkd URI and splunkd session key,
283297
create HECEventWriter object. HEC URI and token etc will be discovered
@@ -289,6 +303,7 @@ def create_from_input(
289303
hec_input_name: Splunk HEC input name.
290304
splunkd_uri: Splunkd URI, like https://localhost:8089
291305
session_key: Splunkd access token.
306+
global_settings_schema: (optional) if True, scheme will be set based on HEC global settings, default False.
292307
context: Other configurations.
293308
294309
Returns:
@@ -297,7 +312,13 @@ def create_from_input(
297312

298313
scheme, host, port = utils.extract_http_scheme_host_port(splunkd_uri)
299314
return HECEventWriter(
300-
hec_input_name, session_key, scheme, host, port, **context
315+
hec_input_name,
316+
session_key,
317+
scheme,
318+
host,
319+
port,
320+
global_settings_schema=global_settings_schema,
321+
**context
301322
)
302323

303324
@staticmethod
@@ -306,6 +327,7 @@ def create_from_token_with_session_key(
306327
session_key: str,
307328
hec_uri: str,
308329
hec_token: str,
330+
global_settings_schema: bool = False,
309331
**context: dict
310332
) -> "HECEventWriter":
311333
"""Given Splunkd URI, Splunkd session key, HEC URI and HEC token,
@@ -318,6 +340,7 @@ def create_from_token_with_session_key(
318340
session_key: Splunkd access token.
319341
hec_uri: Http Event Collector URI, like https://localhost:8088.
320342
hec_token: Http Event Collector token.
343+
global_settings_schema: (optional) if True, scheme will be set based on HEC global settings, default False.
321344
context: Other configurations.
322345
323346
Returns:
@@ -333,6 +356,7 @@ def create_from_token_with_session_key(
333356
port,
334357
hec_uri=hec_uri,
335358
hec_token=hec_token,
359+
global_settings_schema=global_settings_schema,
336360
**context
337361
)
338362

solnlib/modular_input/modular_input.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class ModularInput(metaclass=ABCMeta):
107107
# Input name of Splunk HEC, must be overridden if use_hec_event_writer
108108
# is True
109109
hec_input_name = None
110+
hec_global_settings_schema = False
110111

111112
def __init__(self):
112113
# Validate properties
@@ -230,6 +231,7 @@ def _create_event_writer(self):
230231
scheme=self.server_scheme,
231232
host=self.server_host,
232233
port=self.server_port,
234+
global_settings_schema=self.hec_global_settings_schema,
233235
)
234236
except binding.HTTPError:
235237
logging.error(

solnlib/splunkenv.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"get_splunk_host_info",
3232
"get_splunk_bin",
3333
"get_splunkd_access_info",
34+
"get_scheme_from_hec_settings",
3435
"get_splunkd_uri",
3536
"get_conf_key_value",
3637
"get_conf_stanza",
@@ -198,6 +199,30 @@ def get_splunkd_access_info() -> Tuple[str, str, int]:
198199
return scheme, host, port
199200

200201

202+
def get_scheme_from_hec_settings() -> str:
203+
"""Get scheme from HEC global settings.
204+
205+
Returns:
206+
scheme (str)
207+
"""
208+
try:
209+
ssl_enabled = get_conf_key_value("inputs", "http", "enableSSL")
210+
except KeyError:
211+
raise KeyError(
212+
"Cannot get enableSSL setting form conf: 'inputs' and stanza: '[http]'. "
213+
"Verify that your Splunk instance has the inputs.conf file with the correct [http] stanza. "
214+
"For more information see: "
215+
"https://docs.splunk.com/Documentation/Splunk/9.2.0/Data/UseHECusingconffiles"
216+
)
217+
218+
if is_true(ssl_enabled):
219+
scheme = "https"
220+
else:
221+
scheme = "http"
222+
223+
return scheme
224+
225+
201226
def get_splunkd_uri() -> str:
202227
"""Get splunkd uri.
203228

tests/unit/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ def communicate(self, input=None):
5757
file_path = op.sep.join(
5858
[cur_dir, "data/mock_splunk/etc/system/default/server.conf"]
5959
)
60+
elif self._conf == "inputs":
61+
file_path = op.sep.join(
62+
[
63+
cur_dir,
64+
"data/mock_splunk/etc/apps/splunk_httpinput/local/inputs.conf",
65+
]
66+
)
6067
else:
6168
file_path = op.sep.join(
6269
[cur_dir, "data/mock_splunk/etc/system/default/web.conf"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[http]
2+
disabled = 0
3+
enableSSL = 0

tests/unit/test_modular_input_event_writer.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,35 @@ def mock_post_2(
207207
# test that post is called 2 times
208208
assert len(times_post_called) == 2
209209

210+
for i in range(4):
211+
ev = create_hec_event_writer(i)
212+
assert ev._rest_client.scheme == "https"
213+
for i in range(4):
214+
ev = create_hec_event_writer(i, hec=True)
215+
assert ev._rest_client.scheme == "http"
210216

211-
def create_hec_event_writer(i):
217+
218+
def create_hec_event_writer(i, hec=False):
212219
if i == 1:
213220
return HECEventWriter.create_from_input(
214-
"HECTestInput", "https://localhost:8089", common.SESSION_KEY
221+
"HECTestInput",
222+
"https://localhost:8089",
223+
common.SESSION_KEY,
224+
global_settings_schema=hec,
215225
)
216226
elif i == 2:
217227
return HECEventWriter.create_from_token_with_session_key(
218228
"https://localhost:8089",
219229
common.SESSION_KEY,
220230
"https://localhost:8090",
221231
"test_token",
232+
global_settings_schema=hec,
222233
)
223234
elif i == 3:
224-
return HECEventWriter.create_from_token("https://localhost:8090", "test_token")
235+
return HECEventWriter.create_from_token(
236+
"https://localhost:8090", "test_token", global_settings_schema=hec
237+
)
225238
else:
226-
return HECEventWriter("HECTestInput", common.SESSION_KEY)
239+
return HECEventWriter(
240+
"HECTestInput", common.SESSION_KEY, global_settings_schema=hec
241+
)

0 commit comments

Comments
 (0)