-
Notifications
You must be signed in to change notification settings - Fork 20
/
global_config_update.py
177 lines (149 loc) · 7.11 KB
/
global_config_update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#
# Copyright 2024 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 logging
from typing import Any, Dict, Tuple, List
from splunk_add_on_ucc_framework import global_config as global_config_lib
logger = logging.getLogger("ucc_gen")
def _version_tuple(version: str) -> Tuple[str, ...]:
"""
convert string into tuple to compare version
Args:
version_str : raw string
Returns:
tuple : version into tupleformat
"""
filled = []
for point in version.split("."):
filled.append(point.zfill(8))
return tuple(filled)
def _handle_biased_terms(conf_entities: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
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(global_config: global_config_lib.GlobalConfig) -> None:
for tab in global_config.tabs:
conf_entities = tab.get("entity")
if conf_entities is None:
continue
tab["entity"] = _handle_biased_terms(conf_entities)
for service in global_config.inputs:
conf_entities = service.get("entity")
service["entity"] = _handle_biased_terms(conf_entities)
global_config.update_schema_version("0.0.1")
def _handle_dropping_api_version_update(
global_config: global_config_lib.GlobalConfig,
) -> None:
if global_config.meta.get("apiVersion"):
del global_config.meta["apiVersion"]
global_config.update_schema_version("0.0.3")
def _handle_alert_action_updates(global_config: global_config_lib.GlobalConfig) -> None:
if global_config.has_alerts():
updated_alerts = []
for alert in global_config.alerts:
modified_alert = {}
for k, v in alert.items():
if k in ["activeResponse", "adaptiveResponse"]:
# set default values for the below properties
v["supportsAdhoc"] = v.get("supportsAdhoc", False)
v["supportsCloud"] = v.get("supportsCloud", True)
if k == "activeResponse":
logger.warning(
"'activeResponse' is deprecated. Please use 'adaptiveResponse' instead."
)
modified_alert["adaptiveResponse"] = v
else:
modified_alert[k] = v
# in either case, we create a new list and fill it with updated alerts, if any
updated_alerts.append(modified_alert)
global_config._content["alerts"] = updated_alerts
global_config.dump(global_config.original_path)
def handle_global_config_update(global_config: global_config_lib.GlobalConfig) -> None:
"""Handle changes in globalConfig file."""
current_schema_version = global_config.schema_version
version = current_schema_version if current_schema_version else "0.0.0"
logger.info(f"Current globalConfig schema version is {current_schema_version}")
if _version_tuple(version) < _version_tuple("0.0.1"):
_handle_biased_terms_update(global_config)
global_config.dump(global_config.original_path)
logger.info("Updated globalConfig schema to version 0.0.1")
if _version_tuple(version) < _version_tuple("0.0.2"):
for tab in global_config.tabs:
if tab.name == "account":
conf_entities = tab.get("entity")
if conf_entities is None:
continue
oauth_state_enabled_entity = {}
for entity in conf_entities:
if entity.get("field") == "oauth_state_enabled":
logger.warning(
"oauth_state_enabled field is no longer a separate "
"entity since UCC version 5.0.0. It is now an "
"option in the oauth field. Please update the "
"globalConfig file accordingly."
)
oauth_state_enabled_entity = entity
if entity.get("field") == "oauth" and not entity.get(
"options", {}
).get("oauth_state_enabled"):
entity["options"]["oauth_state_enabled"] = False
if oauth_state_enabled_entity:
conf_entities.remove(oauth_state_enabled_entity)
tab_options = tab.get("options", {})
if tab_options.get("onChange"):
logger.error(
"The onChange option is no longer supported since UCC "
"version 5.0.0. You can use custom hooks to implement "
"these actions."
)
del tab_options["onChange"]
if tab_options.get("onLoad"):
logger.error(
"The onLoad option is no longer supported since UCC "
"version 5.0.0. You can use custom hooks to implement "
"these actions."
)
del tab_options["onLoad"]
if global_config.has_inputs():
for service in global_config.inputs:
service_options = service.get("options", {})
if service_options.get("onChange"):
logger.error(
"The onChange option is no longer supported since UCC "
"version 5.0.0. You can use custom hooks to implement "
"these actions."
)
del service_options["onChange"]
if service_options.get("onLoad"):
logger.error(
"The onLoad option is no longer supported since UCC "
"version 5.0.0. You can use custom hooks to implement "
"these actions."
)
del service_options["onLoad"]
global_config.update_schema_version("0.0.2")
global_config.dump(global_config.original_path)
logger.info("Updated globalConfig schema to version 0.0.2")
if _version_tuple(version) < _version_tuple("0.0.3"):
_handle_dropping_api_version_update(global_config)
global_config.dump(global_config.original_path)
logger.info("Updated globalConfig schema to version 0.0.3")
_handle_alert_action_updates(global_config)