Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace .splunkrc with .env file in test and examples #427

Merged
merged 3 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Splunk host (default: localhost)
host=localhost
# Splunk admin port (default: 8089)
port=8089
# Splunk username
username=admin
# Splunk password
password=changed!
# Access scheme (default: https)
scheme=https
# Your version of Splunk (default: 6.2)
version=8.0
# Bearer token for authentication
#bearerToken="<Bearer-token>"
# Session key for authentication
#sessionKey="<Session-Key>"
9 changes: 0 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- name: Create .splunkrc file
run: |
cd ~
echo host=localhost > .splunkrc
echo port=8089 >> .splunkrc
echo username=admin >> .splunkrc
echo password=changed! >> .splunkrc
echo scheme=https >> .splunkrc
echo version=${{ matrix.splunk }} >> .splunkrc
- name: Install tox
run: pip install tox
- name: Test Execution
Expand Down
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ test_smoke_no_app:
@echo "$(ATTN_COLOR)==> test_smoke_no_app $(NO_COLOR)"
@tox -e py27,py37 -- -m "smoke and not app"

.PHONY: splunkrc
splunkrc:
@echo "$(ATTN_COLOR)==> splunkrc $(NO_COLOR)"
@echo "To make a .splunkrc:"
@echo " [SPLUNK_INSTANCE_JSON] | python scripts/build-splunkrc.py ~/.splunkrc"

.PHONY: splunkrc_default
splunkrc_default:
@echo "$(ATTN_COLOR)==> splunkrc_default $(NO_COLOR)"
@python scripts/build-splunkrc.py ~/.splunkrc
.PHONY: env
env:
@echo "$(ATTN_COLOR)==> env $(NO_COLOR)"
@echo "To make a .env:"
@echo " [SPLUNK_INSTANCE_JSON] | python scripts/build-env.py"

.PHONY: env_default
env_default:
@echo "$(ATTN_COLOR)==> env_default $(NO_COLOR)"
@python scripts/build-env.py

.PHONY: up
up:
Expand Down
33 changes: 10 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ You'll need `docker` and `docker-compose` to get up and running using this metho
```
make up SPLUNK_VERSION=8.0
make wait_up
make splunkrc_default
make test
make down
```
Expand Down Expand Up @@ -91,13 +90,13 @@ service = client.connect(host=<host_url>, token=<session_key>, autologin=True)
```

###
#### Create a .splunkrc convenience file
#### Update a .env file

To connect to Splunk Enterprise, many of the SDK examples and unit tests take command-line arguments that specify values for the host, port, and login credentials for Splunk Enterprise. For convenience during development, you can store these arguments as key-value pairs in a text file named **.splunkrc**. Then, the SDK examples and unit tests use the values from the **.splunkrc** file when you don't specify them.
To connect to Splunk Enterprise, many of the SDK examples and unit tests take command-line arguments that specify values for the host, port, and login credentials for Splunk Enterprise. For convenience during development, you can store these arguments as key-value pairs in a **.env** file. Then, the SDK examples and unit tests use the values from the **.env** file when you don't specify them.

>**Note**: Storing login credentials in the **.splunkrc** 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.
>**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.

To use this convenience file, create a text file with the following format:
here is an example of .env file:

# Splunk Enterprise host (default: localhost)
akaila-splunk marked this conversation as resolved.
Show resolved Hide resolved
host=localhost
Expand All @@ -106,27 +105,15 @@ To use this convenience file, create a text file with the following format:
# Splunk Enterprise username
username=admin
# Splunk Enterprise password
password=changeme
password=changed!
# Access scheme (default: https)
scheme=https
# Your version of Splunk Enterprise
version=8.0

Save the file as **.splunkrc** in the current user's home directory.

* For example on OS X, save the file as:

~/.splunkrc

* On Windows, save the file as:

C:\Users\currentusername\.splunkrc

You might get errors in Windows when you try to name the file because ".splunkrc" appears to be a nameless file with an extension. You can use the command line to create this file by going to the **C:\Users\\&lt;currentusername&gt;** directory and entering the following command:

Notepad.exe .splunkrc

Click **Yes**, then continue creating the file.
# Bearer token for authentication
#bearerToken=<Bearer-token>
# Session key for authentication
#sessionKey=<Session-Key>

#### Run the examples

Expand All @@ -144,7 +131,7 @@ Using Session key

python examplename.py --sessionKey="<value>"

If you saved your login credentials in the **.splunkrc** file, you can omit those arguments:
If you saved your login credentials in the **.env** file, you can omit those arguments:

python examplename.py

Expand Down
2 changes: 1 addition & 1 deletion examples/analytics/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def main():

argv = sys.argv[1:]

splunk_opts = utils.parse(argv, {}, ".splunkrc", usage=usage)
splunk_opts = utils.parse(argv, {}, ".env", usage=usage)
tracker = AnalyticsTracker("cli_app", splunk_opts.kwargs)

#tracker.track("test_event", "abc123", foo="bar", bar="foo")
Expand Down
2 changes: 1 addition & 1 deletion examples/analytics/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def main():

argv = sys.argv[1:]

opts = utils.parse(argv, {}, ".splunkrc", usage=usage)
opts = utils.parse(argv, {}, ".env", usage=usage)
retriever = AnalyticsRetriever(opts.args[0], opts.kwargs)

#events = retriever.events()
Expand Down
2 changes: 1 addition & 1 deletion examples/analytics/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def application(name):
def main():
argv = sys.argv[1:]

opts = utils.parse(argv, {}, ".splunkrc")
opts = utils.parse(argv, {}, ".env")
global splunk_opts
splunk_opts = opts.kwargs

Expand Down
2 changes: 1 addition & 1 deletion examples/async/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def main(argv):
usage = "async.py <sync | async>"

# Parse the command line args.
opts = parse(argv, {}, ".splunkrc")
opts = parse(argv, {}, ".env")

# We have to see if we got either the "sync" or
# "async" command line arguments.
Expand Down
2 changes: 1 addition & 1 deletion examples/binding1.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def search(self, query, **kwargs):
return self.context.post("search/jobs/export", search=query, **kwargs)

def main(argv):
opts = parse(argv, {}, ".splunkrc")
opts = parse(argv, {}, ".env")
context = connect(**opts.kwargs)
service = Service(context)
assert service.apps().status == 200
Expand Down
2 changes: 1 addition & 1 deletion examples/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def main():
commands = ['create', 'delete', 'list']

# parse args, connect and setup
opts = parse(argv, {}, ".splunkrc", usage=usage)
opts = parse(argv, {}, ".env", usage=usage)
service = connect(**opts.kwargs)
program = Program(service)

Expand Down
2 changes: 1 addition & 1 deletion examples/dashboard/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def iterate(job):

def main(argv):
# Parse the command line args.
opts = parse(argv, {}, ".splunkrc")
opts = parse(argv, {}, ".env")

# Connect to Splunk
service = client.connect(**opts.kwargs)
Expand Down
2 changes: 1 addition & 1 deletion examples/event_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")

def main():
opts = parse(sys.argv[1:], {}, ".splunkrc")
opts = parse(sys.argv[1:], {}, ".env")
service = connect(**opts.kwargs)

for item in service.event_types:
Expand Down
2 changes: 1 addition & 1 deletion examples/explorer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ To run, simply execute:

./explorer.py

It will pick up all relevant values from your .splunkrc, or you can pass them
It will pick up all relevant values from your .env, or you can pass them
in on the command line. You can see help by adding `--help` to the exectuion.

The API Explorer will open up a browser window that will show you a drop down
Expand Down
2 changes: 1 addition & 1 deletion examples/explorer/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def main(argv):
},
}

opts = utils.parse(argv, redirect_port_args, ".splunkrc", usage=usage)
opts = utils.parse(argv, redirect_port_args, ".env", usage=usage)

args = [("scheme", opts.kwargs["scheme"]),
("host", opts.kwargs["host"]),
Expand Down
2 changes: 1 addition & 1 deletion examples/export/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def export(options, service):

def main():
""" main entry """
options = parse(sys.argv[1:], CLIRULES, ".splunkrc")
options = parse(sys.argv[1:], CLIRULES, ".env")

if options.kwargs['omode'] not in OUTPUT_MODES:
print("output mode must be one of %s, found %s" % (OUTPUT_MODES,
Expand Down
2 changes: 1 addition & 1 deletion examples/fired_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")

def main():
opts = parse(sys.argv[1:], {}, ".splunkrc")
opts = parse(sys.argv[1:], {}, ".env")
service = connect(**opts.kwargs)

for group in service.fired_alerts:
Expand Down
2 changes: 1 addition & 1 deletion examples/follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def follow(job, count, items):

def main():
usage = "usage: follow.py <search>"
opts = utils.parse(sys.argv[1:], {}, ".splunkrc", usage=usage)
opts = utils.parse(sys.argv[1:], {}, ".env", usage=usage)

if len(opts.args) != 1:
utils.error("Search expression required", 2)
Expand Down
2 changes: 1 addition & 1 deletion examples/genevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def main():
print("must supply an index name")
sys.exit(1)

opts = parse(argv, RULES, ".splunkrc", usage=usage)
opts = parse(argv, RULES, ".env", usage=usage)
service = connect(**opts.kwargs)

if opts.kwargs['ingest'] not in INGEST_TYPE:
Expand Down
2 changes: 1 addition & 1 deletion examples/get_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")

def main(argv):
opts = parse(argv, {}, ".splunkrc")
opts = parse(argv, {}, ".env")
service = client.connect(**opts.kwargs)

# Execute a simple search, and store the sid
Expand Down
2 changes: 1 addition & 1 deletion examples/handlers/handler_certs.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def request(url, message, **kwargs):

return request

opts = utils.parse(sys.argv[1:], RULES, ".splunkrc")
opts = utils.parse(sys.argv[1:], RULES, ".env")
ca_file = opts.kwargs['ca_file']
service = client.connect(handler=handler(ca_file), **opts.kwargs)
pprint([app.name for app in service.apps])
Expand Down
2 changes: 1 addition & 1 deletion examples/handlers/handler_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def request(url, message, **kwargs):
return response
return request

opts = utils.parse(sys.argv[1:], {}, ".splunkrc")
opts = utils.parse(sys.argv[1:], {}, ".env")
service = client.connect(handler=handler(), **opts.kwargs)
pprint([app.name for app in service.apps])
2 changes: 1 addition & 1 deletion examples/handlers/handler_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def handler(proxy):
urllib.request.install_opener(opener)
return request

opts = utils.parse(sys.argv[1:], RULES, ".splunkrc")
opts = utils.parse(sys.argv[1:], RULES, ".env")
proxy = opts.kwargs['proxy']
try:
service = client.connect(handler=handler(proxy), **opts.kwargs)
Expand Down
2 changes: 1 addition & 1 deletion examples/handlers/handler_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def request(url, message, **kwargs):
'body': BytesIO(response.read())
}

opts = utils.parse(sys.argv[1:], {}, ".splunkrc")
opts = utils.parse(sys.argv[1:], {}, ".env")
service = client.connect(handler=request, **opts.kwargs)
pprint([app.name for app in service.apps])

2 changes: 1 addition & 1 deletion examples/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def main():
options = argv[:index]
command = argv[index:]

opts = parse(options, {}, ".splunkrc", usage=usage, epilog=HELP_EPILOG)
opts = parse(options, {}, ".env", usage=usage, epilog=HELP_EPILOG)
service = connect(**opts.kwargs)
program = Program(service)
program.run(command)
Expand Down
2 changes: 1 addition & 1 deletion examples/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")

if __name__ == "__main__":
opts = parse(sys.argv[1:], {}, ".splunkrc")
opts = parse(sys.argv[1:], {}, ".env")
service = client.connect(**opts.kwargs)

content = service.info
Expand Down
2 changes: 1 addition & 1 deletion examples/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")

def main():
opts = parse(sys.argv[1:], {}, ".splunkrc")
opts = parse(sys.argv[1:], {}, ".env")
service = connect(**opts.kwargs)

for item in service.inputs:
Expand Down
2 changes: 1 addition & 1 deletion examples/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def main():
options = argv[:index]
command = argv[index:]

opts = parse(options, {}, ".splunkrc", usage=usage, epilog=HELP_EPILOG)
opts = parse(options, {}, ".env", usage=usage, epilog=HELP_EPILOG)
service = connect(**opts.kwargs)
program = Program(service)
program.run(command)
Expand Down
2 changes: 1 addition & 1 deletion examples/kvstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")

def main():
opts = parse(sys.argv[1:], {}, ".splunkrc")
opts = parse(sys.argv[1:], {}, ".env")
opts.kwargs["owner"] = "nobody"
opts.kwargs["app"] = "search"
service = connect(**opts.kwargs)
Expand Down
2 changes: 1 addition & 1 deletion examples/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

def main(argv):
usage = "usage: %prog [options]"
opts = parse(argv, {}, ".splunkrc", usage=usage)
opts = parse(argv, {}, ".env", usage=usage)
service = client.connect(**opts.kwargs)

for logger in service.loggers:
Expand Down
2 changes: 1 addition & 1 deletion examples/oneshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def pretty(response):

def main():
usage = "usage: oneshot.py <search>"
opts = utils.parse(sys.argv[1:], {}, ".splunkrc", usage=usage)
opts = utils.parse(sys.argv[1:], {}, ".env", usage=usage)
if len(opts.args) != 1:
utils.error("Search expression required", 2)

Expand Down
2 changes: 1 addition & 1 deletion examples/saved_search/saved_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
def main(argv):
""" main entry """
usage = 'usage: %prog --help for options'
opts = utils.parse(argv, RULES, ".splunkrc", usage=usage)
opts = utils.parse(argv, RULES, ".env", usage=usage)

context = binding.connect(**opts.kwargs)
operation = None
Expand Down
2 changes: 1 addition & 1 deletion examples/saved_searches.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


def main():
opts = parse(sys.argv[1:], {}, ".splunkrc")
opts = parse(sys.argv[1:], {}, ".env")
service = connect(**opts.kwargs)

for saved_search in service.saved_searches:
Expand Down
2 changes: 1 addition & 1 deletion examples/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def cmdline(argv, flags, **kwargs):
"""A cmdopts wrapper that takes a list of flags and builds the
corresponding cmdopts rules to match those flags."""
rules = dict([(flag, {'flags': ["--%s" % flag]}) for flag in flags])
return parse(argv, rules, ".splunkrc", **kwargs)
return parse(argv, rules, ".env", **kwargs)

def main(argv):
usage = 'usage: %prog [options] "search"'
Expand Down
2 changes: 1 addition & 1 deletion examples/search_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def cmdline(argv, flags, **kwargs):
"""A cmdopts wrapper that takes a list of flags and builds the
corresponding cmdopts rules to match those flags."""
rules = dict([(flag, {'flags': ["--%s" % flag]}) for flag in flags])
return parse(argv, rules, ".splunkrc", **kwargs)
return parse(argv, rules, ".env", **kwargs)

def modes(argv):
opts = cmdline(argv, [])
Expand Down
Loading