-
Notifications
You must be signed in to change notification settings - Fork 160
/
go.py
executable file
·315 lines (291 loc) · 11.7 KB
/
go.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# Copyright 2014 ETH Zurich
# Copyright 2018 ETH Zurich, Anapaya Systems
#
# 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.
"""
:mod:`go` --- SCION topology go generator
=============================================
"""
# Stdlib
import os
import toml
# SCION
from lib.app.sciond import get_default_sciond_path
from lib.defines import SCIOND_API_SOCKDIR
from lib.util import write_file
from topology.common import (
ArgsTopoDicts,
BR_CONFIG_NAME,
BS_CONFIG_NAME,
COMMON_DIR,
CS_CONFIG_NAME,
DISP_CONFIG_NAME,
prom_addr_br,
prom_addr_infra,
prom_addr_sciond,
prom_addr_dispatcher,
PS_CONFIG_NAME,
sciond_name,
SD_CONFIG_NAME,
trust_db_conf_entry,
)
from topology.prometheus import (
BS_PROM_PORT,
CS_PROM_PORT,
DEFAULT_BR_PROM_PORT,
PS_PROM_PORT,
SCIOND_PROM_PORT,
DISP_PROM_PORT,
)
BS_QUIC_PORT = 30352
PS_QUIC_PORT = 30353
CS_QUIC_PORT = 30354
class GoGenArgs(ArgsTopoDicts):
def __init__(self, args, topo_dicts, networks, port_gen=None):
super().__init__(args, topo_dicts, port_gen)
self.networks = networks
class GoGenerator(object):
def __init__(self, args):
"""
:param GoGenArgs args: Contains the passed command line arguments and topo dicts.
"""
self.args = args
self.log_dir = '/share/logs' if args.docker else 'logs'
self.db_dir = '/share/cache' if args.docker else 'gen-cache'
self.log_level = 'trace' if args.trace else 'debug'
def generate_br(self):
for topo_id, topo in self.args.topo_dicts.items():
for k, v in topo.get("BorderRouters", {}).items():
base = topo_id.base_dir(self.args.output_dir)
br_conf = self._build_br_conf(topo_id, topo["ISD_AS"], base, k, v)
write_file(os.path.join(base, k, BR_CONFIG_NAME), toml.dumps(br_conf))
def _build_br_conf(self, topo_id, ia, base, name, v):
config_dir = '/share/conf' if self.args.docker else os.path.join(base, name)
raw_entry = {
'general': {
'ID': name,
'ConfigDir': config_dir,
},
'logging': self._log_entry(name),
'metrics': {
'Prometheus': prom_addr_br(name, v, DEFAULT_BR_PROM_PORT),
},
'discovery': self._discovery_entry(),
'br': {
'Profile': False,
},
}
return raw_entry
def generate_bs(self):
for topo_id, topo in self.args.topo_dicts.items():
for elem_id, elem in topo.get("BeaconService", {}).items():
# only a single Go-BS per AS is currently supported
if elem_id.endswith("-1"):
base = topo_id.base_dir(self.args.output_dir)
bs_conf = self._build_bs_conf(topo_id, topo["ISD_AS"], base, elem_id, elem)
write_file(os.path.join(base, elem_id, BS_CONFIG_NAME), toml.dumps(bs_conf))
def _build_bs_conf(self, topo_id, ia, base, name, infra_elem):
config_dir = '/share/conf' if self.args.docker else os.path.join(base, name)
raw_entry = {
'general': {
'ID': name,
'ConfigDir': config_dir,
'ReconnectToDispatcher': True,
},
'logging': self._log_entry(name),
'trustDB': trust_db_conf_entry(self.args, name),
'beaconDB': beacon_db_conf_entry(self.args, name),
'discovery': self._discovery_entry(),
'metrics': self._metrics_entry(name, infra_elem, BS_PROM_PORT),
'server': {
'QUICListen': prom_addr_infra(self.args.docker, name, infra_elem, BS_QUIC_PORT),
'QUICCertFile': 'gen-certs/tls.pem',
'QUICKeyFile': 'gen-certs/tls.key',
},
'client': {
'EnableQUICTest': self.args.qtest,
'ResolutionFraction': self.args.svcfrac,
},
}
return raw_entry
def generate_ps(self):
for topo_id, topo in self.args.topo_dicts.items():
for elem_id, elem in topo.get("PathService", {}).items():
# only a single Go-PS per AS is currently supported
if elem_id.endswith("-1"):
base = topo_id.base_dir(self.args.output_dir)
ps_conf = self._build_ps_conf(topo_id, topo["ISD_AS"], base, elem_id, elem)
write_file(os.path.join(base, elem_id, PS_CONFIG_NAME), toml.dumps(ps_conf))
def _build_ps_conf(self, topo_id, ia, base, name, infra_elem):
config_dir = '/share/conf' if self.args.docker else os.path.join(base, name)
raw_entry = {
'general': {
'ID': name,
'ConfigDir': config_dir,
'ReconnectToDispatcher': True,
},
'logging': self._log_entry(name),
'trustDB': trust_db_conf_entry(self.args, name),
'discovery': self._discovery_entry(),
'ps': {
'pathDB': {
'Backend': 'sqlite',
'Connection': os.path.join(self.db_dir, '%s.path.db' % name),
},
'SegSync': True,
},
'metrics': self._metrics_entry(name, infra_elem, PS_PROM_PORT),
'server': {
'QUICListen': prom_addr_infra(self.args.docker, name, infra_elem, PS_QUIC_PORT),
'QUICCertFile': 'gen-certs/tls.pem',
'QUICKeyFile': 'gen-certs/tls.key',
},
'client': {
'EnableQUICTest': self.args.qtest,
'ResolutionFraction': self.args.svcfrac,
},
}
return raw_entry
def generate_sciond(self):
for topo_id, topo in self.args.topo_dicts.items():
base = topo_id.base_dir(self.args.output_dir)
sciond_conf = self._build_sciond_conf(topo_id, topo["ISD_AS"], base)
write_file(os.path.join(base, COMMON_DIR, SD_CONFIG_NAME), toml.dumps(sciond_conf))
def _build_sciond_conf(self, topo_id, ia, base):
name = sciond_name(topo_id)
config_dir = '/share/conf' if self.args.docker else os.path.join(base, COMMON_DIR)
raw_entry = {
'general': {
'ID': name,
'ConfigDir': config_dir,
'ReconnectToDispatcher': True,
},
'logging': self._log_entry(name),
'trustDB': trust_db_conf_entry(self.args, name),
'discovery': self._discovery_entry(),
'sd': {
'Reliable': os.path.join(SCIOND_API_SOCKDIR, "%s.sock" % name),
'Unix': os.path.join(SCIOND_API_SOCKDIR, "%s.unix" % name),
'Public': '%s,[127.0.0.1]:0' % ia,
'pathDB': {
'Connection': os.path.join(self.db_dir, '%s.path.db' % name),
},
},
'metrics': {
'Prometheus': prom_addr_sciond(self.args.docker, topo_id,
self.args.networks, SCIOND_PROM_PORT)
},
'client': {
'EnableQUICTest': self.args.qtest,
'ResolutionFraction': self.args.svcfrac,
},
}
return raw_entry
def generate_cs(self):
for topo_id, topo in self.args.topo_dicts.items():
for elem_id, elem in topo.get("CertificateService", {}).items():
# only a single Go-CS per AS is currently supported
if elem_id.endswith("-1"):
base = topo_id.base_dir(self.args.output_dir)
cs_conf = self._build_cs_conf(topo_id, topo["ISD_AS"], base, elem_id, elem)
write_file(os.path.join(base, elem_id, CS_CONFIG_NAME), toml.dumps(cs_conf))
def _build_cs_conf(self, topo_id, ia, base, name, infra_elem):
config_dir = '/share/conf' if self.args.docker else os.path.join(base, name)
raw_entry = {
'general': {
'ID': name,
'ConfigDir': config_dir,
},
'sd_client': {
'Path': get_default_sciond_path(topo_id),
},
'logging': self._log_entry(name),
'trustDB': trust_db_conf_entry(self.args, name),
'discovery': self._discovery_entry(),
'cs': {
'LeafReissueLeadTime': "6h",
'IssuerReissueLeadTime': "3d",
'ReissueRate': "10s",
'ReissueTimeout': "5s",
},
'metrics': self._metrics_entry(name, infra_elem, CS_PROM_PORT),
'server': {
'QUICListen': prom_addr_infra(self.args.docker, name, infra_elem, CS_QUIC_PORT),
'QUICCertFile': 'gen-certs/tls.pem',
'QUICKeyFile': 'gen-certs/tls.key',
},
'client': {
'EnableQUICTest': self.args.qtest,
'ResolutionFraction': self.args.svcfrac,
},
}
return raw_entry
def generate_disp(self):
if self.args.docker:
self._gen_disp_docker()
else:
elem_dir = os.path.join(self.args.output_dir, "dispatcher")
config_file_path = os.path.join(elem_dir, DISP_CONFIG_NAME)
write_file(config_file_path, toml.dumps(self._build_disp_conf("dispatcher")))
def _gen_disp_docker(self):
for topo_id, _ in self.args.topo_dicts.items():
for elem in ["disp", "disp_br"]:
elem = "%s_%s" % (elem, topo_id.file_fmt())
elem_dir = os.path.join(topo_id.base_dir(self.args.output_dir), elem)
disp_conf = self._build_disp_conf(elem, topo_id)
write_file(os.path.join(elem_dir, DISP_CONFIG_NAME), toml.dumps(disp_conf))
def _build_disp_conf(self, name, topo_id=None):
disp_type = "br" if name.startswith("disp_br") else ""
prometheus_addr = prom_addr_dispatcher(self.args.docker, topo_id,
self.args.networks, DISP_PROM_PORT, disp_type)
return {
'dispatcher': {
'ID': name,
},
'logging': self._log_entry("dispatcher"),
'metrics': {
'Prometheus': prometheus_addr,
},
}
def _discovery_entry(self):
entry = {
'static': {
'Enable': self.args.discovery,
},
'dynamic': {
'Enable': self.args.discovery,
}
}
return entry
def _log_entry(self, name):
entry = {
'file': {
'Path': os.path.join(self.log_dir, "%s.log" % name),
'Level': self.log_level,
},
'console': {
'Level': 'crit',
},
}
return entry
def _metrics_entry(self, name, infra_elem, base_port):
prom_addr = prom_addr_infra(self.args.docker, name, infra_elem, base_port)
return {
'Prometheus': prom_addr
}
def beacon_db_conf_entry(args, name):
db_dir = '/share/cache' if args.docker else 'gen-cache'
return {
'Backend': 'sqlite',
'Connection': os.path.join(db_dir, '%s.beacon.db' % name),
}