Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class GeneratorTest(GeneratingCommand):

#### Accessing instance metadata in scripts

- The `service` metadata object is created from the `splunkd` URI and session key passed to the command invocation on the modular input stream respectively, and is available as soon as the `<script_name>.stream_events()` method is called.
- The `service` metadata object is created from the `splunkd` URI and session key passed to the command invocation on the modular input stream respectively, and is available as soon as the `<script_name>.stream_events()` or `<script_name>.validate_input()` method is called.

```python
from splunklib.modularinput import Script
Expand Down
20 changes: 11 additions & 9 deletions splunklib/modularinput/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class Script(metaclass=ABCMeta):
"""

def __init__(self):
self._input_definition = None
self._server_uri = None
self._session_key = None
self._service = None

def run(self, args):
Expand Down Expand Up @@ -63,8 +64,10 @@ def run_script(self, args, event_writer, input_stream):
# This script is running as an input. Input definitions will be
# passed on stdin as XML, and the script will write events on
# stdout and log entries on stderr.
self._input_definition = InputDefinition.parse(input_stream)
self.stream_events(self._input_definition, event_writer)
input_definition = InputDefinition.parse(input_stream)
self._server_uri = input_definition.metadata["server_uri"]
self._session_key = input_definition.metadata["session_key"]
self.stream_events(input_definition, event_writer)
event_writer.close()
return 0

Expand All @@ -83,6 +86,8 @@ def run_script(self, args, event_writer, input_stream):

if args[1].lower() == "--validate-arguments":
validation_definition = ValidationDefinition.parse(input_stream)
self._server_uri = validation_definition.metadata["server_uri"]
self._session_key = validation_definition.metadata["session_key"]
try:
self.validate_input(validation_definition)
return 0
Expand Down Expand Up @@ -119,19 +124,16 @@ def service(self):
if self._service is not None:
return self._service

if self._input_definition is None:
if self._server_uri is None and self._session_key is None:
return None

splunkd_uri = self._input_definition.metadata["server_uri"]
session_key = self._input_definition.metadata["session_key"]

splunkd = urlsplit(splunkd_uri, allow_fragments=False)
splunkd = urlsplit(self._server_uri, allow_fragments=False)

self._service = Service(
scheme=splunkd.scheme,
host=splunkd.hostname,
port=splunkd.port,
token=session_key,
token=self._session_key,
)

return self._service
Expand Down
13 changes: 12 additions & 1 deletion tests/system/test_apps/modularinput_app/bin/modularinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class ModularInput(Script):
"""
This app provides an example of a modular input that
This app provides an example of a modular input that
can be used in Settings => Data inputs => Local inputs => modularinput
"""

Expand All @@ -44,18 +44,29 @@ def get_scheme(self):
return scheme

def validate_input(self, definition):
self.check_service_access()

url = definition.parameters[self.endpoint_arg]
parsed = parse.urlparse(url)
if parsed.scheme != "https":
raise ValueError(f"non-supported scheme {parsed.scheme}")

def stream_events(self, inputs, ew):
self.check_service_access()

for input_name, input_item in list(inputs.inputs.items()):
event = Event()
event.stanza = input_name
event.data = "example message"
ew.write_event(event)

def check_service_access(self):
# Both validate_input and stream_events should have access to the Splunk
# instance that executed the modular input.
if self.service is None:
raise Exception("self.Service == None")
self.service.info # make sure that we are properly authenticated and self.service works


if __name__ == "__main__":
sys.exit(ModularInput().run(sys.argv))
2 changes: 1 addition & 1 deletion tests/unit/modularinput/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def stream_events(self, inputs, ew):
"ERROR Some error - "
"Traceback (most recent call last): "
' File "...", line 123, in run_script '
" self.stream_events(self._input_definition, event_writer) "
" self.stream_events(input_definition, event_writer) "
' File "...", line 123, in stream_events '
' raise RuntimeError("Some error") '
"RuntimeError: Some error "
Expand Down