This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import logging | ||
import os | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class HecConfiguration: | ||
endpoints_env_variable_name = "SPLUNK_HEC_URL" | ||
authentication_token_env_variable_name = "SPLUNK_HEC_TOKEN" | ||
enable_ssl_env_variable_name = "SPLUNK_HEC_TLS_VERIFY" | ||
|
||
def __init__(self): | ||
urls = os.environ.get(HecConfiguration.endpoints_env_variable_name) | ||
if urls is None: | ||
raise ValueError( | ||
f"{HecConfiguration.endpoints_env_variable_name} environment variable undefined" | ||
) | ||
self._urls_list = urls.split() | ||
|
||
authentication_token = os.environ.get( | ||
HecConfiguration.authentication_token_env_variable_name | ||
) | ||
if authentication_token is None: | ||
raise ValueError( | ||
f"{HecConfiguration.authentication_token_env_variable_name} environment variable undefined" | ||
) | ||
self._authentication_token = authentication_token | ||
|
||
enable_ssl = os.environ.get(HecConfiguration.enable_ssl_env_variable_name) | ||
if enable_ssl is None: | ||
raise ValueError( | ||
f"{HecConfiguration.enable_ssl_env_variable_name} environment variable undefined" | ||
) | ||
self._enable_ssl = True if enable_ssl.lower() == "yes" else False | ||
|
||
def get_endpoints(self): | ||
return self._urls_list | ||
|
||
def get_authentication_token(self): | ||
return self._authentication_token | ||
|
||
def is_ssl_enabled(self): | ||
return self._enable_ssl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import os | ||
from unittest import TestCase | ||
|
||
from splunk_connect_for_snmp_traps.manager.hec_config import HecConfiguration | ||
|
||
|
||
class TestHecConfiguration(TestCase): | ||
def setUp(self): | ||
env_vars = (HecConfiguration.endpoints_env_variable_name, | ||
HecConfiguration.authentication_token_env_variable_name, | ||
HecConfiguration.enable_ssl_env_variable_name) | ||
for env_var in env_vars: | ||
if os.environ.get(env_var) is not None: | ||
del os.environ[env_var] | ||
|
||
def test_no_env_variable_defined_for_hec(self): | ||
self.assertRaises(ValueError, HecConfiguration) | ||
|
||
def test_only_hec_endpoints_defined(self): | ||
os.environ[HecConfiguration.endpoints_env_variable_name] = 'http://127.0.0.1:8088/services/hec' | ||
self.assertRaises(ValueError, HecConfiguration) | ||
|
||
def test_hec_endpoints_and_token_defined(self): | ||
os.environ[HecConfiguration.endpoints_env_variable_name] = 'http://127.0.0.1:8088/services/hec' | ||
os.environ[HecConfiguration.authentication_token_env_variable_name] = '12345678' | ||
self.assertRaises(ValueError, HecConfiguration) | ||
|
||
def test_all_environment_variables_defined_multiple_urls(self): | ||
os.environ[ | ||
HecConfiguration.endpoints_env_variable_name] = 'http://127.0.0.1:8088/services/hec1 ' \ | ||
'http://127.0.0.1:8088/services/hec2 ' | ||
os.environ[HecConfiguration.authentication_token_env_variable_name] = '12345678' | ||
os.environ[HecConfiguration.enable_ssl_env_variable_name] = 'Yes' | ||
|
||
config = HecConfiguration() | ||
self.assertEqual(config.get_endpoints(), | ||
['http://127.0.0.1:8088/services/hec1', 'http://127.0.0.1:8088/services/hec2']) | ||
self.assertEqual(config.get_authentication_token(), '12345678') | ||
self.assertTrue(config.is_ssl_enabled()) | ||
|
||
def test_all_environment_variables_defined_ssl_enabled(self): | ||
os.environ[HecConfiguration.endpoints_env_variable_name] = 'http://127.0.0.1:8088/services/hec' | ||
os.environ[HecConfiguration.authentication_token_env_variable_name] = '12345678' | ||
os.environ[HecConfiguration.enable_ssl_env_variable_name] = 'Yes' | ||
|
||
config = HecConfiguration() | ||
self.assertEqual(config.get_endpoints(), ['http://127.0.0.1:8088/services/hec']) | ||
self.assertEqual(config.get_authentication_token(), '12345678') | ||
self.assertTrue(config.is_ssl_enabled()) | ||
|
||
def test_all_environment_variables_defined_ssl_disabled(self): | ||
os.environ[HecConfiguration.endpoints_env_variable_name] = 'http://127.0.0.1:8088/services/hec' | ||
os.environ[HecConfiguration.authentication_token_env_variable_name] = '12345678' | ||
os.environ[HecConfiguration.enable_ssl_env_variable_name] = 'No' | ||
|
||
config = HecConfiguration() | ||
self.assertEqual(config.get_endpoints(), ['http://127.0.0.1:8088/services/hec']) | ||
self.assertEqual(config.get_authentication_token(), '12345678') | ||
self.assertFalse(config.is_ssl_enabled()) |