Skip to content

Commit 38c8649

Browse files
committed
build-splunkrc script
1 parent 44fa411 commit 38c8649

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

scripts/build-splunkrc.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Copyright 2011-2020 Splunk, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
#!/usr/bin/env python
16+
17+
import sys
18+
import json
19+
import urllib.parse
20+
import os
21+
from pathlib import Path
22+
from string import Template
23+
24+
DEFAULT_CONFIG = {
25+
'host': 'localhost',
26+
'port': '8089',
27+
'username': 'admin',
28+
'password': 'changeme',
29+
'scheme': 'https',
30+
'version': '6.3'
31+
}
32+
33+
DEFAULT_SPLUNKRC_PATH = os.path.join(str(Path.home()), '.splunkrc')
34+
35+
SPLUNKRC_TEMPLATE_PATH = os.path.join(
36+
os.path.dirname(os.path.realpath(__file__)), 'templates/splunkrc.template')
37+
38+
# {
39+
# "server_roles": {
40+
# "standalone": [
41+
# {
42+
# "host": "10.224.106.158",
43+
# "ports": {
44+
# "8089/tcp": "10.224.106.158:55759",
45+
# },
46+
# "splunk": {
47+
# "user_roles": {
48+
# "admin": {
49+
# "password": "Chang3d!",
50+
# "username": "admin"
51+
# }
52+
# },
53+
# "version": "8.1.0",
54+
# "web_url": "http://10.224.106.158:55761"
55+
# }
56+
# }
57+
# ]
58+
# }
59+
# }
60+
def build_config(json_string):
61+
try:
62+
spec_config = json.loads(json_string)
63+
64+
server_config = spec_config['server_roles']['standalone'][0]
65+
splunk_config = server_config['splunk']
66+
67+
host, port = parse_hostport(server_config['ports']['8089/tcp'])
68+
69+
return {
70+
'host': host,
71+
'port': port,
72+
'username': splunk_config['user_roles']['admin']['username'],
73+
'password': splunk_config['user_roles']['admin']['password'],
74+
'version': splunk_config['version'],
75+
}
76+
except Exception as e:
77+
raise ValueError('Invalid configuration JSON string') from e
78+
79+
# Source: https://stackoverflow.com/a/53172593
80+
def parse_hostport(host_port):
81+
# urlparse() and urlsplit() insists on absolute URLs starting with "//"
82+
result = urllib.parse.urlsplit('//' + host_port)
83+
return result.hostname, result.port
84+
85+
def run(variable, splunkrc_path=None):
86+
# read JSON from input
87+
# parse the JSON
88+
input_config = build_config(variable)
89+
90+
config = {**DEFAULT_CONFIG, **input_config}
91+
92+
# build a splunkrc file
93+
with open(SPLUNKRC_TEMPLATE_PATH, 'r') as f:
94+
template = Template(f.read())
95+
96+
splunkrc_string = template.substitute(config)
97+
98+
# if no splunkrc, dry-run
99+
if not splunkrc_path:
100+
print(splunkrc_string)
101+
return
102+
103+
# write the .splunkrc file
104+
with open(splunkrc_path, 'w') as f:
105+
f.write(splunkrc_string)
106+
107+
DATA = sys.stdin.read()
108+
run(DATA, sys.argv[1] if len(sys.argv) > 1 else None)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Splunk host (default: localhost)
2+
host=$host
3+
# Splunk admin port (default: 8089)
4+
port=$port
5+
# Splunk username
6+
username=$username
7+
# Splunk password
8+
password=$password
9+
# Access scheme (default: https)
10+
scheme=$scheme
11+
# Your version of Splunk (default: 6.2)
12+
version=$version

0 commit comments

Comments
 (0)