Skip to content

Commit 8ce74ab

Browse files
committed
Support self.service in ModularInput's validate_input
We get access to the credentials there so lets expose it to users.
1 parent bf6ed38 commit 8ce74ab

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class GeneratorTest(GeneratingCommand):
132132

133133
#### Accessing instance metadata in scripts
134134

135-
- 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.
135+
- 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.
136136

137137
```python
138138
from splunklib.modularinput import Script

splunklib/modularinput/script.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class Script(metaclass=ABCMeta):
3535
"""
3636

3737
def __init__(self):
38-
self._input_definition = None
38+
self._server_uri = None
39+
self._session_key = None
3940
self._service = None
4041

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

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

8487
if args[1].lower() == "--validate-arguments":
8588
validation_definition = ValidationDefinition.parse(input_stream)
89+
self._server_uri = validation_definition.metadata["server_uri"]
90+
self._session_key = validation_definition.metadata["session_key"]
8691
try:
8792
self.validate_input(validation_definition)
8893
return 0
@@ -119,19 +124,16 @@ def service(self):
119124
if self._service is not None:
120125
return self._service
121126

122-
if self._input_definition is None:
127+
if self._server_uri is None and self._session_key is None:
123128
return None
124129

125-
splunkd_uri = self._input_definition.metadata["server_uri"]
126-
session_key = self._input_definition.metadata["session_key"]
127-
128-
splunkd = urlsplit(splunkd_uri, allow_fragments=False)
130+
splunkd = urlsplit(self._server_uri, allow_fragments=False)
129131

130132
self._service = Service(
131133
scheme=splunkd.scheme,
132134
host=splunkd.hostname,
133135
port=splunkd.port,
134-
token=session_key,
136+
token=self._session_key,
135137
)
136138

137139
return self._service

tests/system/test_apps/modularinput_app/bin/modularinput.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
class ModularInput(Script):
2424
"""
25-
This app provides an example of a modular input that
25+
This app provides an example of a modular input that
2626
can be used in Settings => Data inputs => Local inputs => modularinput
2727
"""
2828

@@ -44,18 +44,29 @@ def get_scheme(self):
4444
return scheme
4545

4646
def validate_input(self, definition):
47+
self.check_service_access()
48+
4749
url = definition.parameters[self.endpoint_arg]
4850
parsed = parse.urlparse(url)
4951
if parsed.scheme != "https":
5052
raise ValueError(f"non-supported scheme {parsed.scheme}")
5153

5254
def stream_events(self, inputs, ew):
55+
self.check_service_access()
56+
5357
for input_name, input_item in list(inputs.inputs.items()):
5458
event = Event()
5559
event.stanza = input_name
5660
event.data = "example message"
5761
ew.write_event(event)
5862

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

6071
if __name__ == "__main__":
6172
sys.exit(ModularInput().run(sys.argv))

tests/unit/modularinput/test_script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def stream_events(self, inputs, ew):
258258
"ERROR Some error - "
259259
"Traceback (most recent call last): "
260260
' File "...", line 123, in run_script '
261-
" self.stream_events(self._input_definition, event_writer) "
261+
" self.stream_events(input_definition, event_writer) "
262262
' File "...", line 123, in stream_events '
263263
' raise RuntimeError("Some error") '
264264
"RuntimeError: Some error "

0 commit comments

Comments
 (0)