Skip to content

Commit f98a32c

Browse files
fix: add Splunk HEC token validation for external Splunk (#877)
This PR adds validation of HEC token. It just checks if an event can be send via given HEC token to external Splunk. Fix was tested locally. The result for 3 workers are presented in the attached screenshot (with pytest-splunk-addon code from develop branch it breaks main worker , restarts it and wait indefinitely). Although splunk_external fixture is defined in the session scope it runs on each worker separately. The following question arises: is it expected behaviour, or it's something we'd like to change (for the fixture logic to be executed before pytest splits for several processes). I do not believe that we have to check if Splunk is responsive from each worker separately. That would need to be explored (such solution might not be compatible with pytest): ![image](https://github.com/user-attachments/assets/95bd1c7c-d6ed-43d7-8249-7475e4760808)
1 parent aa35e66 commit f98a32c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pytest_splunk_addon/splunk.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ def splunk_external(request):
639639
"Could not connect to Splunk HEC"
640640
"Please check the log file for possible errors."
641641
)
642+
is_valid_hec(request, splunk_info)
642643
return splunk_info
643644

644645

@@ -1016,6 +1017,36 @@ def is_responsive(url):
10161017
return False
10171018

10181019

1020+
def is_valid_hec(request, splunk):
1021+
"""
1022+
Verify if provided hec token is valid by sending simple post request.
1023+
1024+
Args:
1025+
splunk (dict): details of the Splunk instance
1026+
1027+
Returns:
1028+
None
1029+
"""
1030+
1031+
LOGGER.info(
1032+
"Validating HEC token... splunk=%s",
1033+
json.dumps(splunk),
1034+
)
1035+
response = requests.post(
1036+
url=f'{request.config.getoption("splunk_hec_scheme")}://{splunk["forwarder_host"]}:{splunk["port_hec"]}/services/collector/raw',
1037+
headers={
1038+
"Authorization": f'Splunk {request.config.getoption("splunk_hec_token")}'
1039+
},
1040+
data={"event": "test_hec", "sourcetype": "hec_token_test"},
1041+
verify=False,
1042+
)
1043+
LOGGER.debug("Status code: {}".format(response.status_code))
1044+
if response.status_code == 200:
1045+
LOGGER.info("Splunk HEC is valid.")
1046+
else:
1047+
pytest.exit("Exiting pytest due to invalid HEC token value.")
1048+
1049+
10191050
def pytest_unconfigure(config):
10201051
if PYTEST_XDIST_TESTRUNUID:
10211052
if os.path.exists(PYTEST_XDIST_TESTRUNUID + "_wait"):

0 commit comments

Comments
 (0)