Skip to content

Commit 98c6d17

Browse files
feat: add check for existence of python scripts in user-defined handler (#1908)
**Issue number:** ADDON-82908 ### PR Type **What kind of change does this PR introduce?** * [x] Feature * [ ] Bug Fix * [ ] Refactoring (no functional or API changes) * [ ] Documentation Update * [ ] Maintenance (dependency updates, CI, etc.) ## Summary ### Changes Added a check in `global_config_validator.py` for existence of python scripts mentioned in user-defined handler. ### User experience Add-on build would fail if python script mentioned under `options/restHandlers` is not present in add-ons bin directory. ## Checklist If an item doesn't apply to your changes, leave it unchecked. ### Review * [x] self-review - I have performed a self-review of this change according to the [development guidelines](https://splunk.github.io/addonfactory-ucc-generator/contributing/#development-guidelines) * [ ] Changes are documented. The documentation is understandable, examples work [(more info)](https://splunk.github.io/addonfactory-ucc-generator/contributing/#documentation-guidelines) * [x] PR title and description follows the [contributing principles](https://splunk.github.io/addonfactory-ucc-generator/contributing/#pull-requests) * [ ] meeting - I have scheduled a meeting or recorded a demo to explain these changes (if there is a video, put a link below and in the ticket) ### Tests See [the testing doc](https://splunk.github.io/addonfactory-ucc-generator/contributing/#build-and-test). * [x] Unit - tests have been added/modified to cover the changes * [x] Smoke - tests have been added/modified to cover the changes * [ ] UI - tests have been added/modified to cover the changes * [x] coverage - I have checked the code coverage of my changes [(see more)](https://splunk.github.io/addonfactory-ucc-generator/contributing/#checking-the-code-coverage) **Demo/meeting:** *Reviewers are encouraged to request meetings or demos if any part of the change is unclear*
1 parent 0f3effd commit 98c6d17

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

splunk_add_on_ucc_framework/global_config_validator.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,19 @@ def _validate_usage_of_placeholder(self) -> None:
840840
exc_msg % ("input service", service["name"])
841841
)
842842

843+
def _validate_user_defined_rest_handler_file(self) -> None:
844+
if self._global_config.content.get("options"):
845+
for restHandler in self._global_config.content["options"]["restHandlers"]:
846+
if restHandler.get("registerHandler"):
847+
file_path = os.path.join(
848+
self._source_dir, "bin", restHandler["registerHandler"]["file"]
849+
)
850+
if not os.path.isfile(file_path):
851+
raise GlobalConfigValidatorException(
852+
f"{restHandler['registerHandler']['file']} is not present in "
853+
f"`{os.path.join(self._source_dir, 'bin')}` directory. Please ensure the file exists."
854+
)
855+
843856
def validate(self) -> None:
844857
self._validate_config_against_schema()
845858
if self._global_config.has_pages():
@@ -858,6 +871,7 @@ def validate(self) -> None:
858871
self._validate_usage_of_placeholder()
859872
self._validate_alerts()
860873
self._validate_meta_default_view()
874+
self._validate_user_defined_rest_handler_file()
861875

862876

863877
def should_warn_on_empty_validators(entity: Dict[str, Any]) -> bool:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a dummy python file for user-defined endpoint `Splunk_TA_Example_full` defined in options.

tests/unit/test_check_globalConfig.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def test_build_addon_from_global_config(config_file, caplog):
2525
"valid_config.json",
2626
"valid_config_with_custom_dashboard.json",
2727
"valid_config_only_custom_dashboard.json",
28+
"valid_config_logging_with_user_defined_handlers.json",
2829
]:
2930
pytest.skip("As these files need external dependency.")
3031

tests/unit/test_global_config_validator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def test_autocompletefields_children_support_integer_values():
8282
"invalid_config_for_custom_search_command.json",
8383
"generatetext.py is not present in `bin` directory. Please ensure the file exists.",
8484
),
85+
(
86+
"invalid_config_with_user_defined_rest_handlers.json",
87+
"file1.py is not present in `bin` directory. Please ensure the file exists.",
88+
),
8589
(
8690
"invalid_config_no_configuration_tabs.json",
8791
"[] is too short",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"options": {
3+
"restHandlers": [
4+
{
5+
"name": "name1",
6+
"endpoint": "endpoint1",
7+
"handlerType": "EAI",
8+
"resourcePresent": true,
9+
"registerHandler": {
10+
"file": "file1.py",
11+
"actions": [
12+
"list"
13+
]
14+
},
15+
"capabilities": {
16+
"post": "list_storage_passwords"
17+
}
18+
}
19+
]
20+
},
21+
"meta": {
22+
"name": "Splunk_TA_UCCExample",
23+
"restRoot": "splunk_ta_uccexample",
24+
"version": "1.0.0",
25+
"displayName": "Splunk UCC test Add-on",
26+
"schemaVersion": "0.0.3"
27+
}
28+
}

0 commit comments

Comments
 (0)