catocli 3.0.18__py3-none-any.whl → 3.0.22__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.
- catocli/Utils/clidriver.py +16 -8
- catocli/Utils/formatter_account_metrics.py +544 -0
- catocli/Utils/formatter_app_stats.py +184 -0
- catocli/Utils/formatter_app_stats_timeseries.py +377 -0
- catocli/Utils/formatter_events_timeseries.py +459 -0
- catocli/Utils/formatter_socket_port_metrics.py +189 -0
- catocli/Utils/formatter_socket_port_metrics_timeseries.py +339 -0
- catocli/Utils/formatter_utils.py +251 -0
- catocli/__init__.py +1 -1
- catocli/clisettings.json +37 -5
- catocli/parsers/customParserApiClient.py +206 -65
- catocli/parsers/mutation_policy/__init__.py +405 -405
- catocli/parsers/mutation_site/__init__.py +15 -15
- catocli/parsers/mutation_sites/__init__.py +15 -15
- catocli/parsers/query_accountMetrics/README.md +90 -0
- catocli/parsers/query_accountMetrics/__init__.py +6 -0
- catocli/parsers/query_appStats/README.md +2 -2
- catocli/parsers/query_appStats/__init__.py +4 -2
- catocli/parsers/query_appStatsTimeSeries/__init__.py +4 -2
- catocli/parsers/query_eventsTimeSeries/README.md +280 -0
- catocli/parsers/query_eventsTimeSeries/__init__.py +6 -0
- catocli/parsers/query_policy/__init__.py +42 -42
- catocli/parsers/query_socketPortMetrics/README.md +44 -0
- catocli/parsers/query_socketPortMetrics/__init__.py +6 -0
- catocli/parsers/query_socketPortMetricsTimeSeries/README.md +83 -0
- catocli/parsers/query_socketPortMetricsTimeSeries/__init__.py +4 -2
- {catocli-3.0.18.dist-info → catocli-3.0.22.dist-info}/METADATA +1 -1
- {catocli-3.0.18.dist-info → catocli-3.0.22.dist-info}/RECORD +179 -177
- {catocli-3.0.18.dist-info → catocli-3.0.22.dist-info}/top_level.txt +0 -1
- models/mutation.policy.antiMalwareFileHash.addRule.json +20 -0
- models/mutation.policy.antiMalwareFileHash.addSection.json +103 -0
- models/mutation.policy.antiMalwareFileHash.createPolicyRevision.json +123 -0
- models/mutation.policy.antiMalwareFileHash.discardPolicyRevision.json +123 -0
- models/mutation.policy.antiMalwareFileHash.moveRule.json +20 -0
- models/mutation.policy.antiMalwareFileHash.moveSection.json +103 -0
- models/mutation.policy.antiMalwareFileHash.publishPolicyRevision.json +123 -0
- models/mutation.policy.antiMalwareFileHash.removeRule.json +20 -0
- models/mutation.policy.antiMalwareFileHash.removeSection.json +103 -0
- models/mutation.policy.antiMalwareFileHash.updatePolicy.json +123 -0
- models/mutation.policy.antiMalwareFileHash.updateRule.json +20 -0
- models/mutation.policy.antiMalwareFileHash.updateSection.json +103 -0
- models/mutation.policy.appTenantRestriction.addRule.json +20 -0
- models/mutation.policy.appTenantRestriction.addSection.json +103 -0
- models/mutation.policy.appTenantRestriction.createPolicyRevision.json +123 -0
- models/mutation.policy.appTenantRestriction.discardPolicyRevision.json +123 -0
- models/mutation.policy.appTenantRestriction.moveRule.json +20 -0
- models/mutation.policy.appTenantRestriction.moveSection.json +103 -0
- models/mutation.policy.appTenantRestriction.publishPolicyRevision.json +123 -0
- models/mutation.policy.appTenantRestriction.removeRule.json +20 -0
- models/mutation.policy.appTenantRestriction.removeSection.json +103 -0
- models/mutation.policy.appTenantRestriction.updatePolicy.json +123 -0
- models/mutation.policy.appTenantRestriction.updateRule.json +20 -0
- models/mutation.policy.appTenantRestriction.updateSection.json +103 -0
- models/mutation.policy.applicationControl.addRule.json +20 -0
- models/mutation.policy.applicationControl.addSection.json +103 -0
- models/mutation.policy.applicationControl.createPolicyRevision.json +123 -0
- models/mutation.policy.applicationControl.discardPolicyRevision.json +123 -0
- models/mutation.policy.applicationControl.moveRule.json +20 -0
- models/mutation.policy.applicationControl.moveSection.json +103 -0
- models/mutation.policy.applicationControl.publishPolicyRevision.json +123 -0
- models/mutation.policy.applicationControl.removeRule.json +20 -0
- models/mutation.policy.applicationControl.removeSection.json +103 -0
- models/mutation.policy.applicationControl.updatePolicy.json +123 -0
- models/mutation.policy.applicationControl.updateRule.json +20 -0
- models/mutation.policy.applicationControl.updateSection.json +103 -0
- models/mutation.policy.dynamicIpAllocation.addRule.json +20 -0
- models/mutation.policy.dynamicIpAllocation.addSection.json +103 -0
- models/mutation.policy.dynamicIpAllocation.createPolicyRevision.json +123 -0
- models/mutation.policy.dynamicIpAllocation.discardPolicyRevision.json +123 -0
- models/mutation.policy.dynamicIpAllocation.moveRule.json +20 -0
- models/mutation.policy.dynamicIpAllocation.moveSection.json +103 -0
- models/mutation.policy.dynamicIpAllocation.publishPolicyRevision.json +123 -0
- models/mutation.policy.dynamicIpAllocation.removeRule.json +20 -0
- models/mutation.policy.dynamicIpAllocation.removeSection.json +103 -0
- models/mutation.policy.dynamicIpAllocation.updatePolicy.json +123 -0
- models/mutation.policy.dynamicIpAllocation.updateRule.json +20 -0
- models/mutation.policy.dynamicIpAllocation.updateSection.json +103 -0
- models/mutation.policy.internetFirewall.addRule.json +20 -0
- models/mutation.policy.internetFirewall.addSection.json +103 -0
- models/mutation.policy.internetFirewall.createPolicyRevision.json +123 -0
- models/mutation.policy.internetFirewall.discardPolicyRevision.json +123 -0
- models/mutation.policy.internetFirewall.moveRule.json +20 -0
- models/mutation.policy.internetFirewall.moveSection.json +103 -0
- models/mutation.policy.internetFirewall.publishPolicyRevision.json +123 -0
- models/mutation.policy.internetFirewall.removeRule.json +20 -0
- models/mutation.policy.internetFirewall.removeSection.json +103 -0
- models/mutation.policy.internetFirewall.updatePolicy.json +123 -0
- models/mutation.policy.internetFirewall.updateRule.json +20 -0
- models/mutation.policy.internetFirewall.updateSection.json +103 -0
- models/mutation.policy.remotePortFwd.addRule.json +20 -0
- models/mutation.policy.remotePortFwd.addSection.json +103 -0
- models/mutation.policy.remotePortFwd.createPolicyRevision.json +123 -0
- models/mutation.policy.remotePortFwd.discardPolicyRevision.json +123 -0
- models/mutation.policy.remotePortFwd.moveRule.json +20 -0
- models/mutation.policy.remotePortFwd.moveSection.json +103 -0
- models/mutation.policy.remotePortFwd.publishPolicyRevision.json +123 -0
- models/mutation.policy.remotePortFwd.removeRule.json +20 -0
- models/mutation.policy.remotePortFwd.removeSection.json +103 -0
- models/mutation.policy.remotePortFwd.updatePolicy.json +123 -0
- models/mutation.policy.remotePortFwd.updateRule.json +20 -0
- models/mutation.policy.remotePortFwd.updateSection.json +103 -0
- models/mutation.policy.socketLan.addRule.json +40 -0
- models/mutation.policy.socketLan.addSection.json +103 -0
- models/mutation.policy.socketLan.createPolicyRevision.json +143 -0
- models/mutation.policy.socketLan.discardPolicyRevision.json +143 -0
- models/mutation.policy.socketLan.moveRule.json +40 -0
- models/mutation.policy.socketLan.moveSection.json +103 -0
- models/mutation.policy.socketLan.publishPolicyRevision.json +143 -0
- models/mutation.policy.socketLan.removeRule.json +40 -0
- models/mutation.policy.socketLan.removeSection.json +103 -0
- models/mutation.policy.socketLan.updatePolicy.json +143 -0
- models/mutation.policy.socketLan.updateRule.json +40 -0
- models/mutation.policy.socketLan.updateSection.json +103 -0
- models/mutation.policy.terminalServer.addRule.json +20 -0
- models/mutation.policy.terminalServer.addSection.json +103 -0
- models/mutation.policy.terminalServer.createPolicyRevision.json +123 -0
- models/mutation.policy.terminalServer.discardPolicyRevision.json +123 -0
- models/mutation.policy.terminalServer.moveRule.json +20 -0
- models/mutation.policy.terminalServer.moveSection.json +103 -0
- models/mutation.policy.terminalServer.publishPolicyRevision.json +123 -0
- models/mutation.policy.terminalServer.removeRule.json +20 -0
- models/mutation.policy.terminalServer.removeSection.json +103 -0
- models/mutation.policy.terminalServer.updatePolicy.json +123 -0
- models/mutation.policy.terminalServer.updateRule.json +20 -0
- models/mutation.policy.terminalServer.updateSection.json +103 -0
- models/mutation.policy.tlsInspect.addRule.json +20 -0
- models/mutation.policy.tlsInspect.addSection.json +103 -0
- models/mutation.policy.tlsInspect.createPolicyRevision.json +123 -0
- models/mutation.policy.tlsInspect.discardPolicyRevision.json +123 -0
- models/mutation.policy.tlsInspect.moveRule.json +20 -0
- models/mutation.policy.tlsInspect.moveSection.json +103 -0
- models/mutation.policy.tlsInspect.publishPolicyRevision.json +123 -0
- models/mutation.policy.tlsInspect.removeRule.json +20 -0
- models/mutation.policy.tlsInspect.removeSection.json +103 -0
- models/mutation.policy.tlsInspect.updatePolicy.json +123 -0
- models/mutation.policy.tlsInspect.updateRule.json +20 -0
- models/mutation.policy.tlsInspect.updateSection.json +103 -0
- models/mutation.policy.wanFirewall.addRule.json +20 -0
- models/mutation.policy.wanFirewall.addSection.json +103 -0
- models/mutation.policy.wanFirewall.createPolicyRevision.json +123 -0
- models/mutation.policy.wanFirewall.discardPolicyRevision.json +123 -0
- models/mutation.policy.wanFirewall.moveRule.json +20 -0
- models/mutation.policy.wanFirewall.moveSection.json +103 -0
- models/mutation.policy.wanFirewall.publishPolicyRevision.json +123 -0
- models/mutation.policy.wanFirewall.removeRule.json +20 -0
- models/mutation.policy.wanFirewall.removeSection.json +103 -0
- models/mutation.policy.wanFirewall.updatePolicy.json +123 -0
- models/mutation.policy.wanFirewall.updateRule.json +20 -0
- models/mutation.policy.wanFirewall.updateSection.json +103 -0
- models/mutation.policy.wanNetwork.addRule.json +20 -0
- models/mutation.policy.wanNetwork.addSection.json +103 -0
- models/mutation.policy.wanNetwork.createPolicyRevision.json +123 -0
- models/mutation.policy.wanNetwork.discardPolicyRevision.json +123 -0
- models/mutation.policy.wanNetwork.moveRule.json +20 -0
- models/mutation.policy.wanNetwork.moveSection.json +103 -0
- models/mutation.policy.wanNetwork.publishPolicyRevision.json +123 -0
- models/mutation.policy.wanNetwork.removeRule.json +20 -0
- models/mutation.policy.wanNetwork.removeSection.json +103 -0
- models/mutation.policy.wanNetwork.updatePolicy.json +123 -0
- models/mutation.policy.wanNetwork.updateRule.json +20 -0
- models/mutation.policy.wanNetwork.updateSection.json +103 -0
- models/mutation.xdr.analystFeedback.json +822 -87
- models/query.policy.antiMalwareFileHash.policy.json +123 -0
- models/query.policy.appTenantRestriction.policy.json +123 -0
- models/query.policy.applicationControl.policy.json +123 -0
- models/query.policy.dynamicIpAllocation.policy.json +123 -0
- models/query.policy.internetFirewall.policy.json +123 -0
- models/query.policy.remotePortFwd.policy.json +123 -0
- models/query.policy.socketLan.policy.json +143 -0
- models/query.policy.terminalServer.policy.json +123 -0
- models/query.policy.tlsInspect.policy.json +123 -0
- models/query.policy.wanFirewall.policy.json +123 -0
- models/query.policy.wanNetwork.policy.json +123 -0
- models/query.xdr.stories.json +822 -87
- models/query.xdr.story.json +822 -87
- schema/catolib.py +25 -8
- catocli/Utils/csv_formatter.py +0 -663
- scripts/catolib.py +0 -62
- scripts/export_if_rules_to_json.py +0 -188
- scripts/export_wf_rules_to_json.py +0 -111
- scripts/import_wf_rules_to_tfstate.py +0 -331
- {catocli-3.0.18.dist-info → catocli-3.0.22.dist-info}/WHEEL +0 -0
- {catocli-3.0.18.dist-info → catocli-3.0.22.dist-info}/entry_points.txt +0 -0
- {catocli-3.0.18.dist-info → catocli-3.0.22.dist-info}/licenses/LICENSE +0 -0
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
|
-
|