Skip to content

Commit 14aa4f6

Browse files
authored
Merge pull request #16 from calesanz/master
Enable SSL verification for inter server communication
2 parents 44939c3 + d4d3d2c commit 14aa4f6

19 files changed

+479
-60
lines changed

README/inputs.conf.spec

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ gitTempDir = <value>
99
* location where to store the output of the script on the filesystem (note this directory will be deleted/re-created but the parent dir must exist)
1010
gitRepoURL = <value>
1111
* git repository URL to store the objects (SSH URL only)
12+
sslVerify = <boolean>
13+
* Set to 'true' or 'false' to enable/disable SSL verification for REST requests to `srcUrl`. Set to a path to specify a file with valid CA. (https://2.python-requests.org/en/master/user/advanced/#ssl-cert-verification)
1214
noPrivate = <boolean>
1315
* disable the backup of user level / private objects (true/false), default false
1416
noDisabled = <boolean>
@@ -47,6 +49,9 @@ gitTempDir = <value>
4749
* location where to store the output of the script on the filesystem (note this directory will be deleted/re-created but the parent dir must exist)
4850
gitRepoURL = <value>
4951
* git repository URL to store the objects (SSH URL only)
52+
sslVerify = <boolean>
53+
* Set to 'true' or 'false' to enable/disable SSL verification for REST requests to `srcUrl`. Set to a path to specify a file with valid CA. (https://2.python-requests.org/en/master/user/advanced/#ssl-cert-verification)
54+
5055
auditLogsLookupBackTime = <value>
5156
* This is how far back the audit logs will be checked to ensure that a restore entry is valid, this should be set to your interval time or slightly more, defaults to -1h (use Splunk format)
5257
debugMode = <boolean>

bin/postversioncontrolrestore.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,35 @@
1010
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
1111

1212
from splunklib.searchcommands import dispatch, GeneratingCommand, Configuration, Option
13+
from splunklib.searchcommands.validators import Validator, Boolean
1314
from splunklib.binding import HTTPError
1415

16+
class OrValidator(Validator):
17+
def __init__(self, a, b):
18+
self.a = a
19+
self.b = b
20+
def __call__(self, value):
21+
"""Returns b if a raises an exception otherwise a."""
22+
try:
23+
return self.a.__call__(value)
24+
except ValueError:
25+
return self.b.__call__(value)
26+
27+
def format(self, value):
28+
"""Returns b if a raises an exception otherwise a."""
29+
try:
30+
return self.a.format(value)
31+
except:
32+
return self.b.format(value)
33+
34+
class Filename(Validator):
35+
# TODO Validate file path
36+
def __call__(self, value):
37+
return value
38+
39+
def format(self, value):
40+
return value
41+
1542
splunkLogsDir = os.environ['SPLUNK_HOME'] + "/var/log/splunk"
1643
#Setup the logging
1744
logging_config = dict(
@@ -60,6 +87,8 @@ class SVCPostRestore(GeneratingCommand):
6087
restoreAsUser = Option(require=True)
6188
scope = Option(require=True)
6289
timeout = Option(require=True)
90+
sslVerify = Option(require=False, default=False, validate=OrValidator(Boolean(), Filename()))
91+
requestingAddress = Option(require=False, default=False)
6392

6493
def generate(self):
6594
"""
@@ -87,14 +116,16 @@ def generate(self):
87116
body['restoreAsUser'] = self.restoreAsUser
88117
body['scope'] = self.scope
89118
body['timeout'] = self.timeout
119+
if self.requestingAddress:
120+
body['requestingAddress'] = self.requestingAddress
90121

91122
logger.info("Attempting POST request to url=%s with body=\"%s\"" % (url, body))
92123

93124
body['Authorization'] = 'Splunk ' + self._metadata.searchinfo.session_key
94125

95126
logger.debug("Using token %s" % (body['Authorization']))
96127

97-
attempt = requests.post(url, verify=False, data=body)
128+
attempt = requests.post(url, verify=self.sslVerify, data=body)
98129
if attempt.status_code != 200:
99130
logger.error("POST request failed with status_code=%s, reason=%s, text=%s on url=%s" % (attempt.status_code, attempt.reason, attempt.text, url))
100131
yield {'result': 'Unknown failure, received a non-200 response code of %s on the url %s, reason %s, text result is %s' % (attempt.status_code, url, attempt.reason, attempt.text)}

bin/splunkversioncontrol_backup.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
<title>gitRepoURL</title>
4848
<description>git repository URL to store the objects (SSH URL only)</description>
4949
</arg>
50+
<arg name="sslVerify">
51+
<title>sslVerify</title>
52+
<description>Set to 'true' or 'false' to enable/disable SSL verification for REST requests to `srcUrl`. Set to a path to specify a file with valid CA. (https://2.python-requests.org/en/master/user/advanced/#ssl-cert-verification)</description>
53+
<validation>is_bool('sslVerify')</validation>
54+
<required_on_create>false</required_on_create>
55+
</arg>
5056
<arg name="noPrivate">
5157
<title>noPrivate</title>
5258
<description>disable the backup of user level / private objects (true/false), default false</description>
@@ -203,10 +209,13 @@ def validate_arguments():
203209
else:
204210
ssh_command = "ssh"
205211

212+
sslVerify = False
213+
if 'sslVerify' in val_data:
214+
sslVerify = val_data['sslVerify']
215+
206216
#Run a sanity check and make sure we can connect into the remote Splunk instance
207217
if not useLocalAuth:
208218
url = val_data['srcURL'] + "/servicesNS/nobody/%s/search/jobs/export?search=makeresults" % (appName)
209-
#Verify=false is hardcoded to workaround local SSL issues
210219
srcUsername = val_data['srcUsername']
211220
srcPassword = val_data['srcPassword']
212221
if srcPassword.find("password:") == 0:
@@ -224,7 +233,7 @@ def validate_arguments():
224233

225234
try:
226235
logger.debug("Running query against URL %s with username %s proxies_length=%s" % (url, srcUsername, len(proxies)))
227-
res = requests.get(url, auth=(srcUsername, srcPassword), verify=False, proxies=proxies)
236+
res = requests.get(url, auth=(srcUsername, srcPassword), verify=self.sslVerify, proxies=proxies)
228237
logger.debug("End query against URL %s with username %s" % (url, srcUsername))
229238

230239
if (res.status_code != requests.codes.ok):

bin/splunkversioncontrol_backup_class.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class SplunkVersionControlBackup:
7777
gitRepoURL = None
7878
stanzaName = None
7979
lastRunEpoch = None
80+
sslVerify = False
8081

8182
# read XML configuration passed from splunkd
8283
def get_config(self):
@@ -136,8 +137,7 @@ def getAllAppsList(self):
136137
else:
137138
auth = HTTPBasicAuth(self.srcUsername, self.srcPassword)
138139

139-
#Verify=false is hardcoded to workaround local SSL issues
140-
res = requests.get(url, auth=auth, headers=headers, verify=False, proxies=self.proxies)
140+
res = requests.get(url, auth=auth, headers=headers, verify=self.sslVerify, proxies=self.proxies)
141141
if (res.status_code != requests.codes.ok):
142142
logger.fatal("i=\"%s\" Could not obtain a list of all apps, URL=%s statuscode=%s reason=%s, response=\"%s\"" % (self.stanzaName, url, res.status_code, res.reason, res.text))
143143
sys.exit(-1)
@@ -190,8 +190,7 @@ def runQueries(self, app, endpoint, type, fieldIgnoreList, aliasAttributes={}, v
190190
else:
191191
auth = HTTPBasicAuth(self.srcUsername, self.srcPassword)
192192

193-
#Verify=false is hardcoded to workaround local SSL issues
194-
res = requests.get(url, auth=auth, headers=headers, verify=False, proxies=self.proxies)
193+
res = requests.get(url, auth=auth, headers=headers, verify=self.sslVerify, proxies=self.proxies)
195194
if (res.status_code != requests.codes.ok):
196195
logger.error("i=\"%s\" URL=%s in app=%s statuscode=%s reason=%s response=\"%s\"" % (self.stanzaName, url, app, res.status_code, res.reason, res.text))
197196

@@ -433,8 +432,7 @@ def macros(self, app):
433432
else:
434433
auth = HTTPBasicAuth(self.srcUsername, self.srcPassword)
435434

436-
#Verify=false is hardcoded to workaround local SSL issues
437-
res = requests.get(url, auth=auth, headers=headers, verify=False, proxies=self.proxies)
435+
res = requests.get(url, auth=auth, headers=headers, verify=self.sslVerify, proxies=self.proxies)
438436
if (res.status_code != requests.codes.ok):
439437
logger.error("i=\"%s\" Type macro in app=%s, URL=%s statuscode=%s reason=%s, response=\"%s\"" % (self.stanzaName, app, url, res.status_code, res.reason, res.text))
440438

@@ -749,7 +747,7 @@ def runSearchJob(self, query):
749747
headers = {'Authorization': 'Splunk %s' % self.session_key }
750748
else:
751749
auth = HTTPBasicAuth(self.srcUsername, self.srcPassword)
752-
res = requests.post(url, auth=auth, headers=headers, verify=False, data=data, proxies=self.proxies)
750+
res = requests.post(url, auth=auth, headers=headers, verify=self.sslVerify, data=data, proxies=self.proxies)
753751
if (res.status_code != requests.codes.ok):
754752
logger.error("i=\"%s\" URL=%s statuscode=%s reason=%s response=\"%s\"" % (self.stanzaName, url, res.status_code, res.reason, res.text))
755753
res = json.loads(res.text)
@@ -1022,6 +1020,9 @@ def run_script(self):
10221020

10231021
self.proxies = proxies
10241022

1023+
if 'sslVerify' in config:
1024+
self.sslVerify = config['sslVerify']
1025+
10251026
#From server
10261027
self.splunk_rest = config['srcURL']
10271028
excludedList = [ "srcPassword", "session_key" ]
@@ -1035,7 +1036,6 @@ def run_script(self):
10351036

10361037
headers={'Authorization': 'Splunk %s' % config['session_key']}
10371038

1038-
#Verify=false is hardcoded to workaround local SSL issues
10391039
url = 'https://localhost:8089/services/shcluster/captain/info?output_mode=json'
10401040
res = requests.get(url, headers=headers, verify=False)
10411041
if (res.status_code == 503):

0 commit comments

Comments
 (0)