forked from google/capirca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aclgen.py
230 lines (189 loc) · 8 KB
/
aclgen.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
#
# Copyright 2011 Google 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.
#
# This is an sample tool which will render policy
# files into usable iptables tables, cisco access lists or
# juniper firewall filters.
# system imports
import copy
import dircache
import datetime
from optparse import OptionParser
import os
import logging
import sys
# compiler imports
from lib import naming
from lib import policy
# renderers
from lib import arista
from lib import aruba
from lib import brocade
from lib import cisco
from lib import ciscoasa
from lib import ciscoxr
from lib import gce
from lib import iptables
from lib import ipset
from lib import speedway
from lib import juniper
from lib import junipersrx
from lib import packetfilter
from lib import demo
from lib import nsxv
# pylint: disable=bad-indentation
def parse_args(command_line_args):
"""Populate flags from the command-line arguments."""
_parser = OptionParser()
_parser.add_option('-d', '--def', dest='definitions',
help='definitions directory', default='./def')
_parser.add_option('-o', dest='output_directory', help='output directory',
default='./filters')
_parser.add_option('', '--poldir', dest='policy_directory',
help='policy directory (incompatible with -p)',
default='./policies')
_parser.add_option('-p', '--pol',
help='policy file (incompatible with poldir)',
dest='policy')
_parser.add_option('--debug', help='enable debug-level logging', dest='debug')
_parser.add_option('-s', '--shade_checking', help='Enable shade checking',
action="store_true", dest="shade_check", default=False)
_parser.add_option('-e', '--exp_info', type='int', action='store',
dest='exp_info', default=2,
help='Weeks in advance to notify that a term will expire')
flags, unused_args = _parser.parse_args(command_line_args)
return flags
def load_and_render(base_dir, defs, shade_check, exp_info, output_dir):
rendered = 0
for dirfile in dircache.listdir(base_dir):
fname = os.path.join(base_dir, dirfile)
#logging.debug('load_and_render working with fname %s', fname)
if os.path.isdir(fname):
rendered += load_and_render(fname, defs, shade_check, exp_info, output_dir)
elif fname.endswith('.pol'):
#logging.debug('attempting to render_filters on fname %s', fname)
rendered += render_filters(fname, defs, shade_check, exp_info, output_dir)
return rendered
def filter_name(source, suffix, output_directory):
source = source.lstrip('./')
o_dir = '/'.join([output_directory] + source.split('/')[1:-1])
fname = '%s%s' % (os.path.basename(source).split('.')[0], suffix)
return os.path.join(o_dir, fname)
def do_output_filter(filter_text, filter_file):
if not os.path.isdir(os.path.dirname(filter_file)):
os.makedirs(os.path.dirname(filter_file))
output = open(filter_file, 'w')
if output:
filter_text = revision_tag_handler(filter_file, filter_text)
print 'writing %s' % filter_file
output.write(filter_text)
def revision_tag_handler(fname, text):
# replace $Id:$ and $Date:$ tags with filename and date
timestamp = datetime.datetime.now().strftime('%Y/%m/%d')
new_text = []
for line in text.split('\n'):
if '$Id:$' in line:
line = line.replace('$Id:$', '$Id: %s $' % fname)
if '$Date:$' in line:
line = line.replace('$Date:$', '$Date: %s $' % timestamp)
new_text.append(line)
return '\n'.join(new_text)
def get_policy_obj(source_file, definitions_obj, optimize, shade_check):
"""Memoized call to parse policy by file name.
Returns parsed policy object.
"""
return policy.CacheParseFile(source_file, definitions_obj, optimize,
shade_check=shade_check)
def render_filters(source_file, definitions_obj, shade_check, exp_info, output_dir):
"""Render platform specfic filters for each target platform.
For each target specified in each header of the policy, use that
platforms renderer to render its platform specific filter, using its
own separate copy of the policy object and with optional, target
specific attributes such as optimization and expiration attributes.
Output the rendered filters for each target platform.
Return the rendered filter count.
"""
supported_targets = {
'arista': {'optimized': True, 'renderer': arista.Arista},
'aruba': {'optimized': True, 'renderer': aruba.Aruba},
'brocade': {'optimized': True, 'renderer': brocade.Brocade},
'cisco': {'optimized': True, 'renderer': cisco.Cisco},
'ciscoasa': {'optimized': True, 'renderer': ciscoasa.CiscoASA},
'ciscoxr': {'optimized': True, 'renderer': ciscoxr.CiscoXR},
'demo': {'optimized': True, 'renderer': demo.Demo},
'gce': {'optimized': True, 'renderer': gce.GCE},
'ipset': {'optimized': True, 'renderer': ipset.Ipset},
'iptables': {'optimized': True, 'renderer': iptables.Iptables},
'juniper': {'optimized': True, 'renderer': juniper.Juniper},
'junipersrx': {'optimized': False, 'renderer': junipersrx.JuniperSRX},
'nsxv': {'optimized': True, 'renderer': nsxv.Nsxv},
'packetfilter': {'optimized': True, 'renderer': packetfilter.PacketFilter},
'speedway': {'optimized': True, 'renderer': speedway.Speedway},
'srx': {'optimized': False, 'renderer': junipersrx.JuniperSRX},
}
# Get a policy object from cache to determine headers within the policy file.
pol = get_policy_obj(source_file, definitions_obj, True, shade_check)
# Keep track of how many filters get rendered.
count = 0
for header in pol.headers:
for target_platform in header.platforms:
this_platform = supported_targets.get(target_platform)
# If header specifies an unsupported platform target then skip.
if not this_platform:
continue
optimized = this_platform['optimized']
# Copy Policy Obj.
pol = copy.deepcopy(get_policy_obj(source_file, definitions_obj,
optimized, shade_check))
renderer = this_platform['renderer']
# Render.
fw = renderer(pol, exp_info)
# Output.
do_output_filter(str(fw), filter_name(source_file, fw._SUFFIX, output_dir))
# Count.
count += 1
return count
def main(args):
FLAGS = parse_args(args)
# Do some sanity checking.
if FLAGS.policy_directory and FLAGS.policy:
# When parsing a single file, ignore default path of policy_directory.
FLAGS.policy_directory = False
if not (FLAGS.policy_directory or FLAGS.policy):
raise ValueError('must provide policy or policy_directive')
# Set log level to DEBUG if debug option is specified.
if FLAGS.debug:
logging.basicConfig(level=logging.DEBUG)
if not FLAGS.definitions:
_parser.error('no definitions supplied')
defs = naming.Naming(FLAGS.definitions)
if not defs:
print 'problem loading definitions'
return
count = 0
if FLAGS.policy_directory:
count = load_and_render(FLAGS.policy_directory, defs, FLAGS.shade_check,
FLAGS.exp_info, FLAGS.output_directory)
elif FLAGS.policy:
count = render_filters(FLAGS.policy, defs, FLAGS.shade_check,
FLAGS.exp_info, FLAGS.output_directory)
print '%d filters rendered' % count
if __name__ == '__main__':
# Start main program.
# Pass in command-line args (except for first entry, which is the script name).
# Note that OptionParser slices sys.argv in this way as well,
# ref https://docs.python.org/2/library/optparse.html.
main(sys.argv[1:])