diff --git a/splunk_add_on_ucc_framework/__init__.py b/splunk_add_on_ucc_framework/__init__.py index 013a0b28b..8fbde76a7 100644 --- a/splunk_add_on_ucc_framework/__init__.py +++ b/splunk_add_on_ucc_framework/__init__.py @@ -130,6 +130,37 @@ def version_tuple(version_str): filled.append(point.zfill(8)) return tuple(filled) + +def _handle_biased_terms(conf_entities: dict) -> dict: + for entity in conf_entities: + entity_option = entity.get("options") + if entity_option and "whiteList" in entity_option: + entity_option["allowList"] = entity_option.get("whiteList") + del entity_option["whiteList"] + if entity_option and "blackList" in entity_option: + entity_option["denyList"] = entity_option.get("blackList") + del entity_option["blackList"] + return conf_entities + + +def handle_biased_terms_update(schema_content: dict) -> dict: + pages = schema_content.get("pages") + ta_tabs = pages.get("configuration", {}).get("tabs", {}) + + for tab in ta_tabs: + conf_entities = tab.get("entity") + tab["entity"] = _handle_biased_terms(conf_entities) + + if "inputs" in pages: + services = pages.get("inputs", {}).get("services", {}) + for service in services: + conf_entities = service.get("entity") + service["entity"] = _handle_biased_terms(conf_entities) + + schema_content["meta"]["schemaVersion"] = "0.0.1" + return schema_content + + def handle_update(config_path): """ handle changes in globalConfig.json @@ -142,46 +173,13 @@ def handle_update(config_path): """ with open(config_path, "r") as config_file: schema_content = json.load(config_file) - # check for schemaVersion in meta, if not availble then set default 0.0.0 - version = schema_content.get("meta").get("schemaVersion","0.0.0") - # check for schemaVersion, if it's less than 0.0.1 then updating globalConfig.json - if version_tuple(version) < version_tuple("0.0.1"): - ta_tabs = schema_content.get("pages").get("configuration",{}).get("tabs",{}) + version = schema_content.get("meta").get("schemaVersion", "0.0.0") - # check in every Account tab for biased term - for tab in ta_tabs: - conf_entitties= tab.get("entity") - for entity in conf_entitties: - entity_option = entity.get("options") - if entity_option and "whiteList" in entity_option: - entity_option["allowList"] = entity_option.get("whiteList") - del entity_option["whiteList"] - if entity_option and "blackList" in entity_option: - entity_option["denyList"] = entity_option.get("blackList") - del entity_option["blackList"] - - is_inputs = ("inputs" in schema_content.get("pages")) - if is_inputs: - services = schema_content.get("pages").get("inputs",{}).get("services",{}) - # check in every Input service for biased term - for service in services: - conf_entitties= service.get("entity") - for entity in conf_entitties: - entity_option = entity.get("options") - if entity_option and "whiteList" in entity_option: - entity_option["allowList"] = entity_option.get("whiteList") - del entity_option["whiteList"] - if entity_option and "blackList" in entity_option: - entity_option["denyList"] = entity_option.get("blackList") - del entity_option["blackList"] - - # set schemaVersion to 0.0.1 as updated globalConfig.json according to new update - schema_content["meta"]["schemaVersion"]="0.0.1" - - # upadating new changes in globalConfig.json + if version_tuple(version) < version_tuple("0.0.1"): + schema_content = handle_biased_terms_update(schema_content) with open(config_path, "w") as config_file: - json.dump(schema_content,config_file, ensure_ascii=False, indent=4) + json.dump(schema_content, config_file, ensure_ascii=False, indent=4) # check for schemaVersion, if it's less than 0.0.2 then updating globalConfig.json if version_tuple(version) < version_tuple("0.0.2"): @@ -214,7 +212,6 @@ def handle_update(config_path): is_inputs = ("inputs" in schema_content.get("pages")) if is_inputs: services = schema_content.get("pages").get("inputs",{}).get("services",{}) - # check in every Input service for onSave and onLoad options for service in services: service_options = service.get("options", {}) if service_options.get("onChange"): @@ -225,7 +222,6 @@ def handle_update(config_path): del service_options["onLoad"] schema_content["meta"]["schemaVersion"] = "0.0.2" - # upadating new changes in globalConfig.json with open(config_path, "w") as config_file: json.dump(schema_content,config_file, ensure_ascii=False, indent=4) diff --git a/tests/unit/helpers.py b/tests/unit/helpers.py new file mode 100644 index 000000000..a5228602c --- /dev/null +++ b/tests/unit/helpers.py @@ -0,0 +1,26 @@ +# +# Copyright 2021 Splunk Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +import json +import os + + +def get_config(config_name: str) -> dict: + config_path = os.path.join( + os.path.dirname(os.path.realpath(__file__)), "testdata", config_name + ) + with open(config_path, "r") as f_config: + valid_config_raw = f_config.read() + return json.loads(valid_config_raw) diff --git a/tests/unit/test_config_file_update.py b/tests/unit/test_config_file_update.py new file mode 100644 index 000000000..9ed095e1f --- /dev/null +++ b/tests/unit/test_config_file_update.py @@ -0,0 +1,49 @@ +# +# Copyright 2021 Splunk Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +import unittest + +import tests.unit.helpers as helpers +from splunk_add_on_ucc_framework import handle_biased_terms_update + + +class ConfigFileUpdateTest(unittest.TestCase): + def test_handle_biased_terms_update(self): + config = helpers.get_config("config_with_biased_terms.json") + updated_config = handle_biased_terms_update(config) + expected_schema_version = "0.0.1" + self.assertEqual( + expected_schema_version, updated_config["meta"]["schemaVersion"] + ) + input_entity_1_options_keys = updated_config["pages"]["inputs"]["services"][0][ + "entity" + ][0]["options"].keys() + self.assertIn("denyList", input_entity_1_options_keys) + self.assertNotIn("blackList", input_entity_1_options_keys) + input_entity_2_options_keys = updated_config["pages"]["inputs"]["services"][0][ + "entity" + ][1]["options"].keys() + self.assertIn("allowList", input_entity_2_options_keys) + self.assertNotIn("whiteList", input_entity_2_options_keys) + configuration_entity_1_options_keys = updated_config["pages"]["configuration"][ + "tabs" + ][0]["entity"][0]["options"].keys() + self.assertIn("denyList", configuration_entity_1_options_keys) + self.assertNotIn("blackList", configuration_entity_1_options_keys) + configuration_entity_2_options_keys = updated_config["pages"]["configuration"][ + "tabs" + ][0]["entity"][1]["options"].keys() + self.assertIn("allowList", configuration_entity_2_options_keys) + self.assertNotIn("whiteList", configuration_entity_2_options_keys) diff --git a/tests/unit/test_config_validation.py b/tests/unit/test_config_validation.py index 029af2d7c..49f87397b 100644 --- a/tests/unit/test_config_validation.py +++ b/tests/unit/test_config_validation.py @@ -18,7 +18,7 @@ import unittest import jsonschema - +import tests.unit.helpers as helpers from splunk_add_on_ucc_framework import validate_config_against_schema @@ -33,10 +33,10 @@ def _get_config(config_name: str) -> dict: class ConfigValidationTest(unittest.TestCase): def test_config_validation_when_valid_config_then_no_exception(self): - config = _get_config("valid_config.json") + config = helpers.get_config("valid_config.json") validate_config_against_schema(config) def test_config_validation_when_invalid_config_then_exception(self): - config = _get_config("invalid_config_no_configuration_tabs.json") + config = helpers.get_config("invalid_config_no_configuration_tabs.json") with self.assertRaises(jsonschema.ValidationError): validate_config_against_schema(config) diff --git a/tests/unit/testdata/config_with_biased_terms.json b/tests/unit/testdata/config_with_biased_terms.json new file mode 100644 index 000000000..f95c8a442 --- /dev/null +++ b/tests/unit/testdata/config_with_biased_terms.json @@ -0,0 +1,131 @@ +{ + "pages": { + "configuration": { + "tabs": [ + { + "name": "configuration_tab", + "entity": [ + { + "type": "singleSelect", + "label": "Index 1", + "help": "An index is a type of data repository. Select the index in which you want to collect the events.", + "defaultValue": "main", + "required": true, + "validators": [ + { + "type": "string", + "maxLength": 80, + "errorMsg": "Maximum length allowed for index is 80", + "minLength": 1 + } + ], + "field": "index_1", + "options": { + "createSearchChoice": true, + "blackList": "^_.*$", + "endpointUrl": "data/indexes" + } + }, + { + "type": "singleSelect", + "label": "Index 2", + "help": "An index is a type of data repository. Select the index in which you want to collect the events.", + "defaultValue": "main", + "required": true, + "validators": [ + { + "type": "string", + "maxLength": 80, + "errorMsg": "Maximum length allowed for index is 80", + "minLength": 1 + } + ], + "field": "index_2", + "options": { + "createSearchChoice": true, + "whiteList": "^_.*$", + "endpointUrl": "data/indexes" + } + } + ], + "title": "Configuration tab" + } + ], + "title": "Configuration", + "description": "Set up your add-on" + }, + "inputs": { + "services": [ + { + "name": "example_input_one", + "entity": [ + { + "type": "singleSelect", + "label": "Index 1", + "validators": [ + { + "type": "string", + "errorMsg": "Length of index name should be between 1 and 80.", + "minLength": 1, + "maxLength": 80 + } + ], + "defaultValue": "default", + "options": { + "endpointUrl": "data/indexes", + "blackList": "^_.*$", + "createSearchChoice": true + }, + "field": "index_1", + "required": true + }, + { + "type": "singleSelect", + "label": "Index 2", + "validators": [ + { + "type": "string", + "errorMsg": "Length of index name should be between 1 and 80.", + "minLength": 1, + "maxLength": 80 + } + ], + "defaultValue": "default", + "options": { + "endpointUrl": "data/indexes", + "whiteList": "^_.*$", + "createSearchChoice": true + }, + "field": "index_2", + "required": true + } + ], + "title": "Example Input One" + } + ], + "title": "Inputs", + "description": "Manage your data inputs", + "table": { + "actions": [ + "edit", + "enable", + "delete", + "clone" + ], + "header": [ + { + "label": "Name", + "field": "name" + } + ] + } + } + }, + "meta": { + "apiVersion": "3.2.0", + "name": "Splunk_TA_UCCExample", + "restRoot": "splunk_ta_uccexample", + "version": "1.0.0", + "displayName": "Splunk UCC test Add-on" + } +}