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
|
#!/usr/bin/python
##############################################################################
# Copyright (c) 2017 Politecnico di Torino and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import json
from pprint import pprint
import sys, getopt
import commands
import os
from config import *
from utility import *
import subprocess
#global variables
chains = {}
chains["chains"] = []
routing = {}
routing["routing_table"] = []
configuration = {}
configuration["nodes"] = []
#end of global variables
#generates json file describing the chains (default chains.json)
def generate_chains(curr_dir, multiplier, flowspace):
filename = "chains.json"
multiplier = int(multiplier)
number_of_chains = multiplier*multiplier
for i in range(0, int(number_of_chains)):
chains["chains"].insert(i, {})
chains["chains"][i]["id"] = i+1
chains["chains"][i]["flowspace"] = flowspace
chain_nodes = multiplier
chains["chains"][i]["nodes"] = []
#set attributes for nth client
chains["chains"][i]["nodes"].insert(0, {})
node_name = "client_" + str((i%multiplier)+1)
chains["chains"][i]["nodes"][0]["name"] = node_name
node_type = "web_client"
chains["chains"][i]["nodes"][0]["functional_type"] = node_type
node_address = "ip_web_client_" + str((i%multiplier)+1)
chains["chains"][i]["nodes"][0]["address"] = node_address
#set attributes for chain of firewalls
for j in range(1, chain_nodes+1):
chains["chains"][i]["nodes"].insert(j, {})
node_name = "firewall_" + str(j)
chains["chains"][i]["nodes"][j]["name"] = node_name
node_type = "firewall"
chains["chains"][i]["nodes"][j]["functional_type"] = node_type
node_address = "ip_firewall_" + str(j)
chains["chains"][i]["nodes"][j]["address"] = node_address
#set attributes for nth web server
chains["chains"][i]["nodes"].insert(chain_nodes+1, {})
node_name = "server_" + str((i%multiplier)+1)
chains["chains"][i]["nodes"][chain_nodes+1]["name"] = node_name
node_type = "web_server"
chains["chains"][i]["nodes"][chain_nodes+1]["functional_type"] = node_type
node_address = "ip_web_server_" + str((i%multiplier)+1)
chains["chains"][i]["nodes"][chain_nodes+1]["address"] = node_address
#pprint(chains)
with smart_open(curr_dir + "/" + filename) as f:
print >>f, json.dumps(chains)
return filename
#generates json file describing the node configurations (default config.json)
def generate_config(curr_dir):
chains_file = "chains.json"
chains = parse_chains(curr_dir + "/" + chains_file)
print "Chains read from file:"
pprint(chains)
chains_id = []
for chain_id, chain in chains.items():
chains_id.append(chain_id)
print "Chain #" + str(chain_id) + " has " + str(len(chain)) + " elements"
for node_name in chain.keys():
print node_name + " ",
print ""
filename = "config.json"
config_names = []
i = -1
for number_of_chain in chains_id:
number_of_nodes = len(chains[number_of_chain].keys())
# for i in range(0, number_of_nodes):
for node_name, node_map in chains[number_of_chain].items():
if node_name in config_names:
continue
config_names.append(node_name)
i += 1
configuration["nodes"].insert(i, {})
# node_id = raw_input("Node #" + str(i+1) + " id? -->")
# configuration["nodes"][i]["id"] = node_id
configuration["nodes"][i]["id"] = node_name
name_split = node_name.split("_")
#init = raw_input("Any parameter for inizialization of node " + node_name + "? (N/Y)-->")
init_list = devices_initialization[node_map["functional_type"]]
if init_list != []:
for init_item in init_list:
init_param = "ip_" + init_item + "_" + name_split[1]
configuration["nodes"][i][init_item] = init_param
node_description = name_split[0] + " denies any traffic from web_client #" + name_split[1] + " to web_server #" + name_split[1]
configuration["nodes"][i]["description"] = node_description
while(True):
#node_configuration_type = raw_input("Node " + node_id +"'s configuration type (list, maps)? (L/M) -->")
#n = search_node_in_chains(node_id)
node_configuration_type = devices_configuration_methods[node_map["functional_type"]]
if node_configuration_type == "list":
#list
configuration["nodes"][i]["configuration"] = []
break
if node_configuration_type == "maps":
#maps
configuration["nodes"][i]["configuration"] = []
n_entries = 1
for m in range(0, n_entries):
configuration["nodes"][i]["configuration"].insert(m, {})
map_elements = 1
for n in range(0, map_elements):
key = "ip_web_server_" + name_split[1]
value = "ip_web_client_" + name_split[1]
configuration["nodes"][i]["configuration"][m][key] = value
break
else:
print "Invalid config, please edit the config file"
#pprint(configuration)
with smart_open(curr_dir + "/" + filename) as f:
print >>f, json.dumps(configuration)
return filename
def main(argv):
#exit if any command line argument is missing
if len(argv) < 4:
print 'batch_generator.py -m <multiplier> -o <output_directory>'
sys.exit(2)
#initialize json file names
chains_file = ''
configuration_file = ''
output_dir = ''
multiplier = ''
#parse command line arguments and exit if there is an error
try:
opts, args = getopt.getopt(argv,"hm:o:",["mutliplier=","help","odir="])
except getopt.GetoptError as err:
print str(err)
print 'batch_generator.py -m <multiplier> -o <output_directory>'
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print 'batch_generator.py -m <multiplier> -o <output_directory>'
sys.exit()
elif opt in ("-o", "--output"):
output_dir = arg
elif opt in ("-m", "--multiplier"):
multiplier = arg
generate_chains(output_dir, multiplier, "tcp=80")
generate_config(output_dir)
if __name__ == "__main__":
main(sys.argv[1:])
|