Skip to content

Commit 20f22c8

Browse files
authored
Merge pull request #1 from calesanz/testing
Testing
2 parents 12d9194 + 5bd5aaa commit 20f22c8

15 files changed

+374
-15
lines changed

bin/postversioncontrolrestore.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
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, File
13+
from splunklib.searchcommands.validators import Validator, Boolean
1414
from splunklib.binding import HTTPError
1515

1616
class OrValidator(Validator):
@@ -31,6 +31,13 @@ def format(self, value):
3131
except:
3232
return self.b.format(value)
3333

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
3441

3542
splunkLogsDir = os.environ['SPLUNK_HOME'] + "/var/log/splunk"
3643
#Setup the logging
@@ -80,7 +87,8 @@ class SVCPostRestore(GeneratingCommand):
8087
restoreAsUser = Option(require=True)
8188
scope = Option(require=True)
8289
timeout = Option(require=True)
83-
sslVerify = Option(require=False, default=False, validate=OrValidator(File(), Boolean()))
90+
sslVerify = Option(require=False, default=False, validate=OrValidator(Boolean(), Filename()))
91+
requestingAddress = Option(require=False, default=False)
8492

8593
def generate(self):
8694
"""
@@ -108,6 +116,8 @@ def generate(self):
108116
body['restoreAsUser'] = self.restoreAsUser
109117
body['scope'] = self.scope
110118
body['timeout'] = self.timeout
119+
if self.requestingAddress:
120+
body['requestingAddress'] = self.requestingAddress
111121

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

bin/splunkversioncontrol_backup_class.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ def run_script(self):
10371037
headers={'Authorization': 'Splunk %s' % config['session_key']}
10381038

10391039
url = 'https://localhost:8089/services/shcluster/captain/info?output_mode=json'
1040-
res = requests.get(url, headers=headers, verify=self.sslVerify)
1040+
res = requests.get(url, headers=headers, verify=False)
10411041
if (res.status_code == 503):
10421042
logger.debug("i=\"%s\" Non-shcluster / standalone instance, safe to run on this node" % (self.stanzaName))
10431043
elif (res.status_code != requests.codes.ok):

bin/splunkversioncontrol_rest_restore.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@
5959

6060
class SVCRestore(splunk.rest.BaseRestHandler):
6161

62-
def query_back_for_user_and_permissions(self, authorization_token, *, sslVerify):
62+
def query_back_for_user_and_permissions(self, requestingAddress, authorization_token, *, sslVerify):
6363
headers = { "Authorization" : authorization_token }
6464

6565
#Run a query back against the source system to check the username/role
66-
remoteAddr = self.request['remoteAddr']
67-
url = "https://" + remoteAddr + ":8089/services/authentication/current-context?output_mode=json"
66+
if requestingAddress:
67+
remoteAddr = requestingAddress
68+
else:
69+
remoteAddr = "https://" + self.request['remoteAddr'] + ":8089"
70+
url = remoteAddr + "/services/authentication/current-context?output_mode=json"
6871
logger.info("Received remote request checking username and role related to the token on url=%s" % (url))
6972
logger.debug("token=%s" % (authorization_token))
7073

@@ -160,7 +163,12 @@ def handle_POST(self):
160163
else:
161164
time_wait = 600
162165

163-
username, roles = self.query_back_for_user_and_permissions(payload['Authorization'][0], sslVerify=sslVerify)
166+
if 'requestingAddress' in payload:
167+
requestingAddress = payload['requestingAddress'][0]
168+
else:
169+
requestingAddress = None
170+
171+
username, roles = self.query_back_for_user_and_permissions(requestingAddress, payload['Authorization'][0], sslVerify=sslVerify)
164172
logger.info("username=%s roles=%s" % (username, roles))
165173

166174
app = payload['app'][0]
@@ -207,7 +215,7 @@ def handle_POST(self):
207215
headers = { "Authorization" : "Splunk " + self.request['systemAuth'] }
208216
curtime = calendar.timegm(time.gmtime())
209217
url = "https://localhost:8089/servicesNS/nobody/SplunkVersionControl/storage/collections/data/splunkversioncontrol_rest_restore_status"
210-
res = self.runHttpRequest(url, headers, None, "get", "checking kvstore collection splunkversioncontrol_rest_restore_status", sslVerify=sslVerify)
218+
res = self.runHttpRequest(url, headers, None, "get", "checking kvstore collection splunkversioncontrol_rest_restore_status", sslVerify=False)
211219
if not res:
212220
return
213221

@@ -216,14 +224,14 @@ def handle_POST(self):
216224
if not len(res) == 0:
217225
if not 'start_time' in res[0]:
218226
logger.warn("Warning invalid kvstore data, will wipe it and continue in collection splunkversioncontrol_rest_restore_status on url=%s, value returned res=\"%s\"" % (url, payload))
219-
self.runHttpRequest(url, headers, None, 'delete', 'wiping kvstore splunkversioncontrol_rest_restore_status', sslVerify=sslVerify)
227+
self.runHttpRequest(url, headers, None, 'delete', 'wiping kvstore splunkversioncontrol_rest_restore_status', sslVerify=False)
220228
else:
221229
kvstore_start_time = res[0]['start_time']
222230
target_time = curtime - time_wait
223231
if kvstore_start_time < target_time:
224232
logger.warn("Found existing entry from %s but time is %s, this is past the limit of current time minus %s (%s)" % (kvstore_start_time, curtime, time_wait, target_time))
225233
#More than 10 minutes ago, delete the entry and move on
226-
self.runHttpRequest(url, headers, None, "delete", "wiping kvstore splunkversioncontrol_rest_restore_status due to record %s older than %s time period" % (kvstore_start_time, target_time), sslVerify=sslVerify)
234+
self.runHttpRequest(url, headers, None, "delete", "wiping kvstore splunkversioncontrol_rest_restore_status due to record %s older than %s time period" % (kvstore_start_time, target_time), sslVerify=False)
227235
else:
228236
removal_target = kvstore_start_time + time_wait + 1
229237
logger.warn("Attempted to run but found a running restore instance with time=%s and current_time=%s, will delete and move on after current_time_minus=%s seconds (override_time=%s)" % (kvstore_start_time, curtime, time_wait, removal_target))
@@ -234,7 +242,7 @@ def handle_POST(self):
234242
payload = json.dumps({ 'start_time': curtime })
235243
headers['Content-Type'] = 'application/json'
236244
#update kvstore with runtime
237-
res = self.runHttpRequest(url, headers, payload, 'post', 'updating kvstore collection splunkversioncontrol_rest_restore_status', sslVerify=sslVerify)
245+
res = self.runHttpRequest(url, headers, payload, 'post', 'updating kvstore collection splunkversioncontrol_rest_restore_status', sslVerify=False)
238246
if not res:
239247
return res
240248

@@ -246,7 +254,7 @@ def handle_POST(self):
246254
self.response.write("Restore has failed to complete successfully in app %s, object of type %s, with name %s, from tag %s, scope %s with restoreAsUser %s and your username of %s. Message is %s" % (app, type, obj_name, tag, scope, restoreAsUser, username, message))
247255
logger.warn("Restore has failed to complete successfully in app=%s, object of type=%s, with name=%s, from tag=%s, scope=%s with restoreAsUser=%s and requested by username=%s, message=%s" % (app, type, obj_name, tag, scope, restoreAsUser, username, message))
248256

249-
self.runHttpRequest(url, headers, None, 'delete', 'wiping kvstore splunkversioncontrol_rest_restore_status after completed run', sslVerify=sslVerify)
257+
self.runHttpRequest(url, headers, None, 'delete', 'wiping kvstore splunkversioncontrol_rest_restore_status after completed run', sslVerify=False)
250258

251259
#Run a Splunk query via the search/jobs endpoint
252260
def runSearchJob(self, url, appname, headers, auth, username, earliest_time, *, sslVerify=False):

bin/splunkversioncontrol_utility.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def get_password(password, session_key, logger, *, sslVerify=False):
5555
url = "https://localhost:8089/servicesNS/-/" + context + "/storage/passwords?output_mode=json&f=clear_password&search=" + password
5656
logger.debug("Trying url=%s with session_key to obtain name=%s" % (url, password))
5757
headers = {'Authorization': 'Splunk %s' % session_key}
58-
res = requests.get(url, headers=headers, verify=sslVerify)
58+
res = requests.get(url, headers=headers, verify=False)
5959
dict = json.loads(res.text)
6060
clear_password = False
6161
if not 'entry' in dict:
@@ -73,7 +73,7 @@ def get_password(password, session_key, logger, *, sslVerify=False):
7373

7474
url = "https://localhost:8089/servicesNS/-/-/storage/passwords?output_mode=json&f=clear_password&count=0&search=" + password
7575
logger.debug("Trying url=%s with session_key to obtain name=%s" % (url, password))
76-
res = requests.get(url, headers=headers, verify=sslVerify)
76+
res = requests.get(url, headers=headers, verify=False)
7777
dict = json.loads(res.text)
7878
if not 'entry' in dict:
7979
logger.warn("dict=%s did not contain the entries expected on url=%s while looking for password=%s" % (dict, url, password))

default/data/ui/views/splunkversioncontrol_restore_dynamic.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
<table>
130130
<title>SplunkVersionControl Results (please wait a few minutes for results)</title>
131131
<search>
132-
<query>| postversioncontrolrestore app="$app$" obj_name="$name$" restoreAsUser="$restoreAsUser$" scope="$scope$" splunk_vc_name=`splunk_vc_name` tag="$tag$" url=`splunk_vc_url` type="$type$" timeout=`splunk_vc_timeout` sslVerify=`sslVerify`</query>
132+
<query>| postversioncontrolrestore app="$app$" obj_name="$name$" restoreAsUser="$restoreAsUser$" scope="$scope$" splunk_vc_name=`splunk_vc_name` tag="$tag$" url=`splunk_vc_url` type="$type$" timeout=`splunk_vc_timeout` sslVerify=`sslVerify` requestingAddress=`requestingAddress`</query>
133133
<earliest>-5m</earliest>
134134
<latest>now</latest>
135135
<sampleRatio>1</sampleRatio>

default/macros.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ iseval = 0
2222
[sslVerify]
2323
definition = false
2424
iseval = 0
25+
26+
[requestingAddress]
27+
definition = false
28+
iseval = 0
29+

test/bkp_default.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
#ansible_post_tasks: file:///tmp/install_git.yml
3+
splunk:
4+
conf:
5+
- key: inputs
6+
value:
7+
directory: /opt/splunk/etc/apps/SplunkVersionControl/local
8+
content:
9+
splunkversioncontrol_backup://SH:
10+
srcURL: https://splunk_sh:8089
11+
srcUsername: admin
12+
srcPassword: g8nGXBQBF
13+
gitTempDir: /opt/splunk/vcs/git_tmp_backup
14+
gitRepoURL: file:///opt/splunk/vcs/backup.git
15+
sslVerify: /cert_dir/ca.crt
16+
debugMode: true
17+
useLocalAuth: false
18+
interval: 60
19+
splunkversioncontrol_restore://SH:
20+
destURL: https://splunk_sh:8089
21+
destUsername: admin
22+
destPassword: g8nGXBQBF
23+
gitTempDir: /opt/splunk/vcs/git_tmp_restore
24+
gitRepoURL: file:///opt/splunk/vcs/backup.git
25+
sslVerify: /cert_dir/ca.crt
26+
debugMode: true
27+
useLocalAuth: false
28+
auditLogsLookupBackTime: -2h
29+
timewait: 30
30+
- key: server
31+
value:
32+
directory: /opt/splunk/etc/system/local/
33+
content:
34+
sslConfig:
35+
enableSplunkdSSL: true
36+
serverCert: /cert_dir/splunk_moc.pem
37+
sslRootCAPath: /cert_dir/ca.crt
38+
sslPassword: password

test/certificates/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.crt
2+
*.key
3+
*.pem
4+
*.csr
5+
castuff

test/certificates/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM alpine:latest
2+
3+
RUN apk update && \
4+
apk add --no-cache openssl bash && \
5+
rm -rf "/var/cache/apk/*"
6+
7+
COPY createca.sh createcerts.sh entrypoint.sh /scripts/
8+
9+
ENTRYPOINT [ "bash", "/scripts/entrypoint.sh" ]

test/certificates/createca.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
set -e
2+
CA_KEY=ca.key
3+
CA_CRT=ca.crt
4+
SUBJ="/C=CH/ST=Bern/L=Bern/O=Example Company/CN=CA Domain1"
5+
6+
if [ ! -f $CA_KEY ]; then
7+
openssl genrsa -out $CA_KEY 4096
8+
fi
9+
10+
if [ ! -f $CA_CRT ]; then
11+
openssl req -new -x509 -key ${CA_KEY} -out ${CA_CRT} -subj "$SUBJ" -days 3000
12+
fi
13+

0 commit comments

Comments
 (0)