Skip to content

Commit 34421e9

Browse files
committed
Added build-env.py file and updated Makefile commands to create an env file
1 parent fbb867c commit 34421e9

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ test_smoke_no_app:
5656
@echo "$(ATTN_COLOR)==> test_smoke_no_app $(NO_COLOR)"
5757
@tox -e py27,py37 -- -m "smoke and not app"
5858

59+
.PHONY: env
60+
env:
61+
@echo "$(ATTN_COLOR)==> env $(NO_COLOR)"
62+
@echo "To make a .env:"
63+
@echo " [SPLUNK_INSTANCE_JSON] | python scripts/build-env.py"
64+
65+
.PHONY: env_default
66+
env_default:
67+
@echo "$(ATTN_COLOR)==> env_default $(NO_COLOR)"
68+
@python scripts/build-env.py
69+
5970
.PHONY: up
6071
up:
6172
@echo "$(ATTN_COLOR)==> up $(NO_COLOR)"

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ To connect to Splunk Enterprise, many of the SDK examples and unit tests take co
9696

9797
>**Note**: Storing login credentials in the **.env** file is only for convenience during development. This file isn't part of the Splunk platform and shouldn't be used for storing user credentials for production. And, if you're at all concerned about the security of your credentials, enter them at the command line rather than saving them in this file.
9898
99+
here is an example of .env file:
100+
101+
# Splunk Enterprise host (default: localhost)
102+
host=localhost
103+
# Splunk Enterprise admin port (default: 8089)
104+
port=8089
105+
# Splunk Enterprise username
106+
username=admin
107+
# Splunk Enterprise password
108+
password=changed!
109+
# Access scheme (default: https)
110+
scheme=https
111+
# Your version of Splunk Enterprise
112+
version=8.0
113+
# Bearer token for authentication
114+
#bearerToken=<Bearer-token>
115+
# Session key for authentication
116+
#sessionKey=<Session-Key>
99117

100118
#### Run the examples
101119

scripts/build-env.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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': 'changed!',
29+
'scheme': 'https',
30+
'version': '8.0'
31+
}
32+
33+
DEFAULT_ENV_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '.env')
34+
35+
ENV_TEMPLATE_PATH = os.path.join(
36+
os.path.dirname(os.path.realpath(__file__)), 'templates/env.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, env_path=None):
86+
# read JSON from input
87+
# parse the JSON
88+
input_config = build_config(variable) if variable else DEFAULT_CONFIG
89+
90+
config = {**DEFAULT_CONFIG, **input_config}
91+
92+
# build a env file
93+
with open(ENV_TEMPLATE_PATH, 'r') as f:
94+
template = Template(f.read())
95+
96+
env_string = template.substitute(config)
97+
env_path = DEFAULT_ENV_PATH if env_path is None else env_path
98+
# if no env, dry-run
99+
if not env_path:
100+
print(env_string)
101+
return
102+
103+
# write the .env file
104+
with open(env_path, 'w') as f:
105+
f.write(env_string)
106+
107+
if sys.stdin.isatty():
108+
DATA = None
109+
else:
110+
DATA = sys.stdin.read()
111+
112+
run(DATA, sys.argv[1] if len(sys.argv) > 1 else None)

scripts/templates/env.template

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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
13+
# Bearer token for authentication
14+
#bearerToken=<Bearer-token>
15+
# Session key for authentication
16+
#sessionKey=<Session-Key>

0 commit comments

Comments
 (0)