catocli 3.0.18__py3-none-any.whl → 3.0.24__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of catocli might be problematic. Click here for more details.

Files changed (43) hide show
  1. catocli/Utils/clidriver.py +16 -8
  2. catocli/Utils/formatter_account_metrics.py +544 -0
  3. catocli/Utils/formatter_app_stats.py +184 -0
  4. catocli/Utils/formatter_app_stats_timeseries.py +377 -0
  5. catocli/Utils/formatter_events_timeseries.py +459 -0
  6. catocli/Utils/formatter_socket_port_metrics.py +189 -0
  7. catocli/Utils/formatter_socket_port_metrics_timeseries.py +339 -0
  8. catocli/Utils/formatter_utils.py +251 -0
  9. catocli/__init__.py +1 -1
  10. catocli/clisettings.json +37 -5
  11. catocli/parsers/customParserApiClient.py +211 -66
  12. catocli/parsers/mutation_policy/__init__.py +405 -405
  13. catocli/parsers/mutation_site/__init__.py +15 -15
  14. catocli/parsers/mutation_sites/__init__.py +15 -15
  15. catocli/parsers/query_accountMetrics/README.md +90 -0
  16. catocli/parsers/query_accountMetrics/__init__.py +6 -0
  17. catocli/parsers/query_appStats/README.md +2 -2
  18. catocli/parsers/query_appStats/__init__.py +4 -2
  19. catocli/parsers/query_appStatsTimeSeries/__init__.py +4 -2
  20. catocli/parsers/query_eventsTimeSeries/README.md +280 -0
  21. catocli/parsers/query_eventsTimeSeries/__init__.py +6 -0
  22. catocli/parsers/query_policy/__init__.py +42 -42
  23. catocli/parsers/query_socketPortMetrics/README.md +44 -0
  24. catocli/parsers/query_socketPortMetrics/__init__.py +6 -0
  25. catocli/parsers/query_socketPortMetricsTimeSeries/README.md +83 -0
  26. catocli/parsers/query_socketPortMetricsTimeSeries/__init__.py +4 -2
  27. catocli/parsers/utils/export_utils.py +6 -2
  28. catocli-3.0.24.dist-info/METADATA +184 -0
  29. {catocli-3.0.18.dist-info → catocli-3.0.24.dist-info}/RECORD +37 -35
  30. {catocli-3.0.18.dist-info → catocli-3.0.24.dist-info}/top_level.txt +0 -1
  31. models/mutation.xdr.analystFeedback.json +822 -87
  32. models/query.xdr.stories.json +822 -87
  33. models/query.xdr.story.json +822 -87
  34. schema/catolib.py +89 -64
  35. catocli/Utils/csv_formatter.py +0 -663
  36. catocli-3.0.18.dist-info/METADATA +0 -124
  37. scripts/catolib.py +0 -62
  38. scripts/export_if_rules_to_json.py +0 -188
  39. scripts/export_wf_rules_to_json.py +0 -111
  40. scripts/import_wf_rules_to_tfstate.py +0 -331
  41. {catocli-3.0.18.dist-info → catocli-3.0.24.dist-info}/WHEEL +0 -0
  42. {catocli-3.0.18.dist-info → catocli-3.0.24.dist-info}/entry_points.txt +0 -0
  43. {catocli-3.0.18.dist-info → catocli-3.0.24.dist-info}/licenses/LICENSE +0 -0
@@ -1,124 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: catocli
3
- Version: 3.0.18
4
- Summary: Cato Networks cli wrapper for the GraphQL API.
5
- Home-page: https://github.com/Cato-Networks/cato-cli
6
- Author: Cato Networks
7
- Author-email: [email protected]
8
- Classifier: Programming Language :: Python :: 3
9
- Classifier: License :: OSI Approved :: Apache Software License
10
- Classifier: Operating System :: OS Independent
11
- Classifier: Programming Language :: Python :: 3.6
12
- Classifier: Programming Language :: Python :: 3.7
13
- Classifier: Programming Language :: Python :: 3.8
14
- Classifier: Programming Language :: Python :: 3.9
15
- Classifier: Programming Language :: Python :: 3.10
16
- Classifier: Programming Language :: Python :: 3.11
17
- Classifier: Programming Language :: Python :: 3.12
18
- Requires-Python: >=3.6
19
- Description-Content-Type: text/markdown
20
- License-File: LICENSE
21
- Requires-Dist: urllib3
22
- Requires-Dist: certifi
23
- Requires-Dist: six
24
- Dynamic: author
25
- Dynamic: author-email
26
- Dynamic: classifier
27
- Dynamic: description
28
- Dynamic: description-content-type
29
- Dynamic: home-page
30
- Dynamic: license-file
31
- Dynamic: requires-dist
32
- Dynamic: requires-python
33
- Dynamic: summary
34
-
35
- # Cato Networks GraphQL API CLI
36
-
37
- The package provides a simple to use CLI that reflects industry standards (such as the AWS cli), and enables customers to manage Cato Networks configurations and processes via the [Cato Networks GraphQL API](https://api.catonetworks.com/api/v1/graphql2) easily integrating into configurations management, orchestration or automation frameworks to support the DevOps model.
38
-
39
- ## Installation
40
- pip3 install catocli
41
-
42
- ## Authentication
43
-
44
- The Cato CLI uses a profile-based authentication system similar to AWS CLI. You can configure multiple profiles for different environments or accounts.
45
-
46
- ### Quick Setup
47
-
48
- ```bash
49
- # Configure your first profile (interactive)
50
- catocli configure set
51
-
52
- # Or configure non-interactively
53
- catocli configure set --cato-token "your-api-token" --account-id "12345"
54
- ```
55
-
56
- ### Profile Management
57
-
58
- ```bash
59
- # List profiles
60
- catocli configure list
61
-
62
- # Switch profiles
63
- catocli configure use prod
64
-
65
- # Show current profile
66
- catocli configure show
67
- ```
68
-
69
- ### Legacy Environment Variables (deprecated)
70
-
71
- For backward compatibility, you can still use environment variables:
72
-
73
- ```bash
74
- export CATO_TOKEN="12345-abcde-12345-abcde"
75
- export CATO_ACCOUNT_ID="12345"
76
- export CATO_DEBUG=True # Optional for debug logs
77
- ```
78
-
79
- The CLI will automatically migrate these to a default profile on first run.
80
-
81
- ### Documentation
82
-
83
- For detailed information about profile management, see [PROFILES.md](PROFILES.md).
84
-
85
- [CLICK HERE](https://support.catonetworks.com/hc/en-us/articles/4413280536081-Generating-API-Keys-for-the-Cato-API) to see how create an API key to authenticate.
86
-
87
- ## Running the CLI
88
- catocli -h
89
- catocli query -h
90
- catocli query entityLookup -h
91
- catocli query entityLookup '{"type":"country"}`
92
-
93
- // Override the accountID value as a cli argument
94
- catocli query entityLookup -accountID=12345 '{"type":"country"}`
95
-
96
- ## Check out run locally not as pip package
97
- git clone git@github.com:Cato-Networks/cato-cli.git
98
- cd cato-cli
99
- python3 -m catocli -h
100
-
101
- This CLI is a Python 3 application and has been tested with Python 3.6 -> 3.8
102
- ## Requirements:
103
- python 3.6 or higher
104
-
105
- ## Confirm your version of python if installed:
106
- Open a terminal
107
- Enter: python -V or python3 -V
108
-
109
- ## Usage:
110
- usage: catocli <resource> <command> [options]
111
-
112
- CLI for query, and mutation operations via API.
113
-
114
- Positional arguments:
115
- {entityLookup}
116
- entityLookup entityLookup reference.
117
-
118
- Optional arguments:
119
- -h, --help show this help message and exit
120
- --version show program's version number and exit
121
-
122
- ## Installing the correct version for environment:
123
- https://www.python.org/downloads/
124
-
scripts/catolib.py DELETED
@@ -1,62 +0,0 @@
1
- #!/usr/bin/env python3
2
- import json
3
- import os
4
- import sys
5
- import subprocess
6
- from typing import Any, Dict, List, Union
7
-
8
- def should_strip_id(obj: Dict[str, Any]) -> bool:
9
- if not isinstance(obj, dict):
10
- return False
11
-
12
- keys = set(obj.keys())
13
- return keys == {'name', 'id'}
14
-
15
- def strip_ids_recursive(obj: Union[Dict, List, Any]) -> Union[Dict, List, Any, None]:
16
- if isinstance(obj, dict):
17
- new_obj = {}
18
- for key, value in obj.items():
19
- ## Ignoring sections to retain id and name pair
20
- if key == 'sections':
21
- new_obj[key] = value
22
- else:
23
- processed_value = strip_ids_recursive(value)
24
- # Skip keys where the value is an empty list
25
- if processed_value != []:
26
- new_obj[key] = processed_value
27
- # Check if this object should have its id stripped
28
- if should_strip_id(new_obj):
29
- return {'name': new_obj['name']}
30
- return new_obj
31
-
32
- elif isinstance(obj, list):
33
- # Recursively process all items in the list
34
- processed_list = [strip_ids_recursive(item) for item in obj]
35
- # Remove None values from the list (if any were removed)
36
- processed_list = [item for item in processed_list if item is not None]
37
- return processed_list
38
-
39
- else:
40
- # For primitive types (str, int, bool, None), return as-is
41
- return obj
42
-
43
- ## General purpose functions
44
- def exec_cli(command):
45
- result = None
46
- try:
47
- response = subprocess.run(command, shell=True, text=True, capture_output=True)
48
- if response.returncode != 0:
49
- print(f"Command failed with return code {response.returncode}")
50
- print(f"stderr: {response.stderr}")
51
- return None
52
- result = json.loads(response.stdout)
53
- except json.JSONDecodeError as e:
54
- print(f"Failed to parse JSON response: {e}")
55
- print(f"stdout: {response.stdout}")
56
- print(f"stderr: {response.stderr}")
57
- return None
58
- except Exception as e:
59
- print(f"Failed to execute command: {e}")
60
- return None
61
- return result
62
-
@@ -1,188 +0,0 @@
1
- import sys
2
- import csv
3
- import subprocess
4
- import json
5
- import os
6
- import argparse
7
- sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
8
- from graphql_client.api.call_api import ApiClient, CallApi
9
- from graphql_client import Configuration
10
- from graphql_client.api_client import ApiException
11
- import catocli
12
-
13
- # Configuration variables
14
- DESTINATION_DIR = "config_data"
15
-
16
- def strip_ids_recursive(data):
17
- """Recursively strip id attributes from data structure"""
18
- if isinstance(data, dict):
19
- return {k: strip_ids_recursive(v) for k, v in data.items() if k != 'id'}
20
- elif isinstance(data, list):
21
- return [strip_ids_recursive(item) for item in data]
22
- else:
23
- return data
24
-
25
- def main():
26
- # Parse command line arguments
27
- parser = argparse.ArgumentParser(description='Export IFW rules to JSON')
28
- parser.add_argument('-accountID', help='Account ID to export rules from', required=True)
29
- parser.add_argument('--output-file-path', help='Full path including filename and extension for output file. If not specified, uses default: config_data/all_ifw_rules_and_sections_{account_id}.json')
30
- parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output')
31
- args = parser.parse_args()
32
-
33
- ACCOUNT_ID = args.accountID
34
-
35
- # Setup API client configuration
36
- if "CATO_TOKEN" not in os.environ:
37
- print("Missing authentication, please set the CATO_TOKEN environment variable with your api key.")
38
- sys.exit(1)
39
-
40
- configuration = Configuration()
41
- configuration.verify_ssl = False
42
- configuration.api_key["x-api-key"] = os.getenv("CATO_TOKEN")
43
- configuration.host = "{}".format(catocli.__cato_host__)
44
- configuration.accountID = ACCOUNT_ID
45
-
46
- # Set up output file path
47
- if args.output_file_path:
48
- # Use output file path if provided
49
- output_file = args.output_file_path
50
- destination_dir = os.path.dirname(output_file)
51
- if args.verbose:
52
- print(f"Using output file path: {output_file}")
53
- else:
54
- # Use default path and filename
55
- destination_dir = 'config_data'
56
- json_output_file = f"all_ifw_rules_and_sections_{ACCOUNT_ID}.json"
57
- output_file = os.path.join(destination_dir, json_output_file)
58
- if args.verbose:
59
- print(f"Using default path: {output_file}")
60
-
61
- # Create destination directory if it doesn't exist
62
- if destination_dir and not os.path.exists(destination_dir):
63
- if args.verbose:
64
- print(f"Creating directory: {destination_dir}")
65
- os.makedirs(destination_dir)
66
- policyQuery = {
67
- "query": "query policy ( $accountId:ID! ) { policy ( accountId:$accountId ) { internetFirewall { policy { enabled rules { audit { updatedTime updatedBy } rule { id name description index section { id name } enabled source { ip host { id name } site { id name } subnet ipRange { from to } globalIpRange { id name } networkInterface { id name } siteNetworkSubnet { id name } floatingSubnet { id name } user { id name } usersGroup { id name } group { id name } systemGroup { id name } } connectionOrigin country { id name } device { id name } deviceOS deviceAttributes { category type model manufacturer os osVersion } destination { application { id name } customApp { id name } appCategory { id name } customCategory { id name } sanctionedAppsCategory { id name } country { id name } domain fqdn ip subnet ipRange { from to } globalIpRange { id name } remoteAsn containers { fqdnContainer { id name } ipAddressRangeContainer { id name } } } service { standard { id name } custom { port portRange { from to } protocol } } action tracking { event { enabled } alert { enabled frequency subscriptionGroup { id name } webhook { id name } mailingList { id name } } } schedule { activeOn customTimeframePolicySchedule: customTimeframe { from to } customRecurringPolicySchedule: customRecurring { from to days } } exceptions { name source { ip host { id name } site { id name } subnet ipRange { from to } globalIpRange { id name } networkInterface { id name } siteNetworkSubnet { id name } floatingSubnet { id name } user { id name } usersGroup { id name } group { id name } systemGroup { id name } } deviceOS country { id name } device { id name } deviceAttributes { category type model manufacturer os osVersion } destination { application { id name } customApp { id name } appCategory { id name } customCategory { id name } sanctionedAppsCategory { id name } country { id name } domain fqdn ip subnet ipRange { from to } globalIpRange { id name } remoteAsn containers { fqdnContainer { id name } ipAddressRangeContainer { id name } } } service { standard { id name } custom { port portRangeCustomService: portRange { from to } protocol } } connectionOrigin } } properties } sections { audit { updatedTime updatedBy } section { id name } properties } audit { publishedTime publishedBy } revision { id name description changes createdTime updatedTime } } } } }",
68
- "variables": {
69
- "accountId": ACCOUNT_ID
70
- },
71
- "operationName": "policy"
72
- }
73
- print(f"Retrieving all IFW rules and sections for account {ACCOUNT_ID}...")
74
-
75
- # Create API client instance
76
- instance = CallApi(ApiClient(configuration))
77
-
78
- # Create params object for the API call
79
- params = {
80
- 'v': False, # verbose mode
81
- 'f': 'json', # format
82
- 'p': False, # pretty print
83
- 't': False # test mode
84
- }
85
-
86
- try:
87
- # Call the API directly
88
- response = instance.call_api(policyQuery, params)
89
- allIfwRules = response[0] if response else {}
90
-
91
- if not allIfwRules or 'data' not in allIfwRules:
92
- print("ERROR: Failed to retrieve data from API")
93
- sys.exit(1)
94
-
95
- except ApiException as e:
96
- print(f"ERROR: API call failed - {e}")
97
- sys.exit(1)
98
- except Exception as e:
99
- print(f"ERROR: Unexpected error - {e}")
100
- sys.exit(1)
101
-
102
- # First, preserve section IDs before stripping them
103
- section_id_map = {}
104
- sections_with_ids = allIfwRules['data']['policy']['internetFirewall']['policy']['sections']
105
- for section_data in sections_with_ids:
106
- section_id_map[section_data['section']['name']] = section_data['section']['id']
107
-
108
- # Get the first section ID for section_to_start_after_id
109
- section_to_start_after_id = None
110
- if len(sections_with_ids) > 0:
111
- section_to_start_after_id = sections_with_ids[0]['section']['id']
112
- print(f"Excluding first section with system rule: {sections_with_ids[0]['section']['name']}")
113
-
114
- ## Processing data to strip id attributes
115
- processed_data = strip_ids_recursive(allIfwRules)
116
-
117
- ## Filter out rules with properties[0]=="SYSTEM"
118
- filtered_rules = []
119
- for rule_data in processed_data['data']['policy']['internetFirewall']['policy']['rules']:
120
- rule_properties = rule_data.get('properties', [])
121
- # Skip rules where the first property is "SYSTEM"
122
- if rule_properties and rule_properties[0] == "SYSTEM":
123
- print(f"Excluding SYSTEM rule: {rule_data['rule']['name']}")
124
- else:
125
- filtered_rules.append(rule_data)
126
- processed_data['data']['policy']['internetFirewall']['policy']['rules'] = filtered_rules
127
- # rules = filtered_rules
128
-
129
- # Add index_in_section to each rule
130
- # Group rules by section and add index_in_section
131
- section_counters = {}
132
- for rule_data in processed_data['data']['policy']['internetFirewall']['policy']['rules']:
133
- section_name = rule_data['rule']['section']['name']
134
- if section_name not in section_counters:
135
- section_counters[section_name] = 0
136
- section_counters[section_name] += 1
137
- rule_data['rule']['index_in_section'] = section_counters[section_name]
138
-
139
- # Create rules_in_sections array
140
- rules_in_sections = []
141
- for rule_data in processed_data['data']['policy']['internetFirewall']['policy']['rules']:
142
- rule_info = rule_data['rule']
143
- rules_in_sections.append({
144
- "index_in_section": rule_info['index_in_section'],
145
- "section_name": rule_info['section']['name'],
146
- "rule_name": rule_info['name']
147
- })
148
- rule_info.pop("index_in_section", None)
149
- rule_info.pop("index", None)
150
- rule_info["enabled"] = True
151
-
152
- # Add rules_in_sections to the policy structure
153
- processed_data['data']['policy']['internetFirewall']['policy']['rules_in_sections'] = rules_in_sections
154
-
155
- # Reformat sections array to have index, section_id and section_name structure
156
- # Exclude the first section from export
157
- processed_sections = []
158
- # Add first section containing reserved SYSTEM rules as section_to_start_after_id
159
- for index, section_data in enumerate(processed_data['data']['policy']['internetFirewall']['policy']['sections']):
160
- if index == 0:
161
- # Skip the first section which contains reserved SYSTEM rules
162
- continue
163
- else:
164
- processed_sections.append({
165
- "section_index": index,
166
- "section_name": section_data['section']['name']
167
- })
168
-
169
- # Add preserved section IDs and section_to_start_after_id
170
- processed_data['data']['policy']['internetFirewall']['policy']['section_ids'] = section_id_map
171
- if section_to_start_after_id:
172
- processed_data['data']['policy']['internetFirewall']['policy']['section_to_start_after_id'] = section_to_start_after_id
173
-
174
- if len(sections_with_ids) > 0:
175
- print(f"Excluded first section: {sections_with_ids[0]['section']['name']} containing reserved SYSTEM rules")
176
-
177
- # Replace the original sections array with the reformatted one
178
- processed_data['data']['policy']['internetFirewall']['policy']['sections'] = processed_sections
179
-
180
- # Write the processed data to the new file
181
- with open(output_file, 'w', encoding='utf-8') as f:
182
- json.dump(processed_data, f, indent=4, ensure_ascii=False)
183
- print(f"Successfully created {output_file}")
184
-
185
-
186
- if __name__ == "__main__":
187
- main()
188
-
@@ -1,111 +0,0 @@
1
- import sys
2
- import csv
3
- import subprocess
4
- import json
5
- import os
6
- import argparse
7
- import catolib
8
-
9
- # Configuration variables
10
- DESTINATION_DIR = "config_data"
11
-
12
- def main():
13
- # Parse command line arguments
14
- parser = argparse.ArgumentParser(description='Export WAN rules to JSON')
15
- parser.add_argument('account_id', help='Account ID to export rules from')
16
- args = parser.parse_args()
17
-
18
- ACCOUNT_ID = args.account_id
19
- JSON_OUTPUT_FILE = f"all_wf_rules_and_sections_{ACCOUNT_ID}.json"
20
- # Create destination directory if it doesn't exist
21
- if not os.path.exists(DESTINATION_DIR):
22
- print(f"Creating directory: {DESTINATION_DIR}")
23
- os.makedirs(DESTINATION_DIR)
24
-
25
- # Set full file paths
26
- output_file = os.path.join(DESTINATION_DIR, JSON_OUTPUT_FILE)
27
- policyQuery = {
28
- "query": "query policy ( $accountId:ID! ) { policy ( accountId:$accountId ) { wanFirewall { policy { enabled rules { audit { updatedTime updatedBy } rule { id name description index section { id name } enabled source { host { id name } site { id name } subnet ip ipRange { from to } globalIpRange { id name } networkInterface { id name } siteNetworkSubnet { id name } floatingSubnet { id name } user { id name } usersGroup { id name } group { id name } systemGroup { id name } } connectionOrigin country { id name } device { id name } deviceOS deviceAttributes { category type model manufacturer os osVersion } destination { host { id name } site { id name } subnet ip ipRange { from to } globalIpRange { id name } networkInterface { id name } siteNetworkSubnet { id name } floatingSubnet { id name } user { id name } usersGroup { id name } group { id name } systemGroup { id name } } application { application { id name } appCategory { id name } customApp { id name } customCategory { id name } sanctionedAppsCategory { id name } domain fqdn ip subnet ipRange { from to } globalIpRange { id name } } service { standard { id name } custom { port portRange { from to } protocol } } action tracking { event { enabled } alert { enabled frequency subscriptionGroup { id name } webhook { id name } mailingList { id name } } } schedule { activeOn customTimeframePolicySchedule: customTimeframe { from to } customRecurringPolicySchedule: customRecurring { from to days } } direction exceptions { name source { host { id name } site { id name } subnet ip ipRange { from to } globalIpRange { id name } networkInterface { id name } siteNetworkSubnet { id name } floatingSubnet { id name } user { id name } usersGroup { id name } group { id name } systemGroup { id name } } deviceOS destination { host { id name } site { id name } subnet ip ipRange { from to } globalIpRange { id name } networkInterface { id name } siteNetworkSubnet { id name } floatingSubnet { id name } user { id name } usersGroup { id name } group { id name } systemGroup { id name } } country { id name } device { id name } deviceAttributes { category type model manufacturer os osVersion } application { application { id name } appCategory { id name } customApp { id name } customCategory { id name } sanctionedAppsCategory { id name } domain fqdn ip subnet ipRange { from to } globalIpRange { id name } } service { standard { id name } custom { port portRangeCustomService: portRange { from to } protocol } } connectionOrigin direction } } properties } sections { audit { updatedTime updatedBy } section { id name } properties } audit { publishedTime publishedBy } revision { id name description changes createdTime updatedTime } } } } }",
29
- "variables": {
30
- "accountId": ACCOUNT_ID
31
- },
32
- "operationName": "policy"
33
- }
34
- catocliCommand = "catocli raw '"+json.dumps(policyQuery)+"'"
35
- print(f"Retrieving all WAN rules and sections for account {ACCOUNT_ID}...")
36
- allWanRules = catolib.exec_cli(catocliCommand)
37
-
38
- ## Processing data to strip id attributes
39
- processed_data = catolib.strip_ids_recursive(allWanRules)
40
-
41
- ## Preserving section IDs index by section name
42
- section_id_map = {}
43
- if 'data' in allWanRules and 'policy' in allWanRules['data'] and 'wanFirewall' in allWanRules['data']['policy']:
44
- for section_data in allWanRules['data']['policy']['wanFirewall']['policy']['sections']:
45
- section_id_map[section_data['section']['name']] = section_data['section']['id']
46
- processed_data['data']['policy']['wanFirewall']['policy']['section_ids'] = section_id_map
47
-
48
- ## Filter out rules with properties[0]=="SYSTEM"
49
- filtered_rules = []
50
- for rule_data in processed_data['data']['policy']['wanFirewall']['policy']['rules']:
51
- rule_properties = rule_data.get('properties', [])
52
- # Skip rules where the first property is "SYSTEM"
53
- if rule_properties and rule_properties[0] == "SYSTEM":
54
- print(f"Excluding SYSTEM rule: {rule_data['rule']['name']}")
55
- else:
56
- filtered_rules.append(rule_data)
57
- processed_data['data']['policy']['wanFirewall']['policy']['rules'] = filtered_rules
58
- # rules = filtered_rules
59
-
60
- # Add index_in_section to each rule
61
- # Group rules by section and add index_in_section
62
- section_counters = {}
63
- for rule_data in processed_data['data']['policy']['wanFirewall']['policy']['rules']:
64
- section_name = rule_data['rule']['section']['name']
65
- if section_name not in section_counters:
66
- section_counters[section_name] = 0
67
- section_counters[section_name] += 1
68
- rule_data['rule']['index_in_section'] = section_counters[section_name]
69
-
70
- # Create rules_in_sections array
71
- rules_in_sections = []
72
- for rule_data in processed_data['data']['policy']['wanFirewall']['policy']['rules']:
73
- rule_info = rule_data['rule']
74
- rules_in_sections.append({
75
- "index_in_section": rule_info['index_in_section'],
76
- "section_name": rule_info['section']['name'],
77
- "rule_name": rule_info['name']
78
- })
79
- rule_info.pop("index_in_section", None)
80
- rule_info.pop("index", None)
81
- rule_info["enabled"] = True
82
-
83
- # Add rules_in_sections to the policy structure
84
- processed_data['data']['policy']['wanFirewall']['policy']['rules_in_sections'] = rules_in_sections
85
-
86
- # Reformat sections array to have index, section_id and section_name structure
87
- # Exclude the first section from export, excluding first section
88
-
89
- processed_sections = []
90
- # Add first section containing reserved SYSTEM rules as section_to_start_after_id
91
- for index, section_data in enumerate(processed_data['data']['policy']['wanFirewall']['policy']['sections']):
92
- processed_sections.append({
93
- "section_index": index,
94
- "section_name": section_data['section']['name']
95
- })
96
-
97
- if len(processed_data['data']['policy']['wanFirewall']['policy']['sections']) > 0:
98
- print(f"Excluded first section: {processed_data['data']['policy']['wanFirewall']['policy']['sections'][0]['section']['name']} containing reserved SYSTEM rules")
99
-
100
- # Replace the original sections array with the reformatted one
101
- processed_data['data']['policy']['wanFirewall']['policy']['sections'] = processed_sections
102
-
103
- # Write the processed data to the new file
104
- with open(output_file, 'w', encoding='utf-8') as f:
105
- json.dump(processed_data, f, indent=4, ensure_ascii=False)
106
- print(f"Successfully created {output_file}")
107
-
108
-
109
- if __name__ == "__main__":
110
- main()
111
-