- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrender.py
More file actions
Latest commit
137 lines (129 loc) · 5.53 KB
/
Copy pathrender.py
File metadata and controls
137 lines (129 loc) · 5.53 KB
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
"""
This module processes the rendering of templates.
"""
importinitialize
importos
importre
fromjinja2importEnvironment, FileSystemLoader
fromget_propertyimportget_home_directory
fromget_propertyimportget_policy_directory
fromget_propertyimportget_template_directory
fromget_propertyimportget_standards_directory
fromget_propertyimportget_updated_list
fromparse_cmdimportparse_commands
with_remediate=None
defrender(template_list,node_object,auditcreeper,output,with_remediation):
"""
Uncomment the below function and replace with the above define function to include secrets if hashicorp is used.
"""
#def render(template_list,node_object,auditcreeper,output,with_remediation,secrets):
globalwith_remediate
with_remediate=with_remediation
template_list_copy=template_list
ifauditcreeper:
template_list=template_list_copy[0]
forindexininitialize.element:
get_hardware_vendor_template_directory=get_template_directory(node_object[index]['hardware_vendor'],node_object[index]['opersys'],node_object[index]['type'])
print ("{}".format(node_object[index]['name']))
fortemplateintemplate_list:
config=process_jinja2_template(node_object,index,template,with_remediation)
print("{}{}".format(get_hardware_vendor_template_directory,template))
if(output):
print("{}".format(config))
withopen('{}/rendered-configs/{}.{}'.format(get_home_directory(),node_object[index]['name'],template.replace('jinja2','')) +'conf','r') asfile:
init_config=file.readlines()
"""
The below parse_commands() function will only get executed if
it needs to store commands in the global variable initialize.configuration
for push. push_cfgs(output = True) vs push_render(output = False) functions.
"""
ifoutput!=True:
parse_commands(node_object[index],init_config,set_notation=False)
print()
ifauditcreeper:
template_list=get_updated_list(template_list_copy)
returnNone
defprocess_jinja2_template(node_object,index,template,with_remediation):
hardware_vendor_template_directory=get_template_directory(node_object[index]['hardware_vendor'],node_object[index]['opersys'],node_object[index]['type'])
standards_directory=get_standards_directory(node_object[index]['name'],node_object[index]['hardware_vendor'],node_object[index]['type'])
env=Environment(
loader=FileSystemLoader([hardware_vendor_template_directory,standards_directory]),
lstrip_blocks=True,
trim_blocks=True
)
env.filters['remediate'] =remediate
baseline=env.get_template(template)
os.makedirs('{}/rendered-configs/'.format(get_home_directory()),exist_ok=True)
withopen('{}/rendered-configs/{}.{}'.format(get_home_directory(),node_object[index]['name'],template.replace('jinja2','')) +'conf', 'w') asfile:
config=baseline.render(
node=node_object[index],
with_remediation=with_remediation
#set = 'set ' ### for ZPE
#add = 'add ' ### for LTM
#delete = 'delete ' for LTM
)
file.write(config)
file.close
returnconfig
defprocess_json_template(policy_list,node_object,policy_list_copy,output,auditcreeper):
commands= []
redirect= []
policy_list_copy=policy_list
ifauditcreeper:
policy_list=policy_list_copy[0]
all_policies=policy_list_copy[0]
"""
:param redirect: A list of which method superloop will access. This variable is sent to the multithread_engine. Each element is a redirect per node.
:type alt_key_file: list
:param commands: Referenced to global variable commands which keeps track of all commands per node.
:type commands: list
"""
forindexininitialize.element:
ifnode_object[index]['type'] =='vfirewall'ornode_object[index]['type'] =='firewall':
get_hardware_vendor_policy_directory=get_policy_directory(node_object[index]['hardware_vendor'],node_object[index]['opersys'],node_object[index]['type'])
print ("{}".format(node_object[index]['name']))
redirect.append('push_acl')
"""
The below iteration takes care of the configs for each policy file per firewall node(s).
"""
# if auditcreeper:
# for policy in all_policies:
# print('{}{}'.format(get_hardware_vendor_policy_directory,policy))
# print('')
# for policy in policy_list:
# commands = parse_firewall_acl(node_object[index],policy)
# policy_list = get_updated_list(policy_list_copy)
# os.makedirs('{}/rendered-configs/'.format(get_home_directory()),exist_ok=True)
# with open('{}/rendered-configs/{}.{}'.format(get_home_directory(),node_object[index]['name'],policy_list[0].replace('json','')) + 'conf', 'w') as file:
# for config in commands:
# if output:
# print(config)
# file.write(config)
# file.write('\n')
# file.close
# initialize.configuration.append(commands)
# else:
forpolicyinpolicy_list:
print('{}{}'.format(get_hardware_vendor_policy_directory,policy))
commands=parse_firewall_acl(node_object[index],policy)
os.makedirs('{}/rendered-configs/'.format(get_home_directory()),exist_ok=True)
withopen('{}/rendered-configs/{}.{}'.format(get_home_directory(),node_object[index]['name'],policy_list[0].replace('json','')) +'conf', 'w') asfile:
forconfigincommands:
ifoutput:
print(config)
file.write(config)
file.write('\n')
file.close
initialize.configuration.append(commands)
else:
print('+ Node {} is not of type; firewall.'.format(node_object[index]['name']))
exit()
returncommands
defremediate(input):
"""
Custom filter to process boolean.
"""
ifwith_remediate==True:
returninput
else:
return''