-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathDocker.py
1376 lines (1097 loc) · 49.1 KB
/
Docker.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
from seedemu.core.Emulator import Emulator
from seedemu.core import Node, Network, Compiler, BaseSystem, BaseOption, NodeScope, NodeScope, NodeScopeType, NodeScopeTier, OptionHandling, BaseVolume, OptionMode
from seedemu.core.enums import NodeRole, NetworkType
from .DockerImage import DockerImage
from .DockerImageConstant import *
from typing import Dict, Generator, List, Set, Tuple
from hashlib import md5
from functools import cmp_to_key
from os import mkdir, chdir
from re import sub
from ipaddress import IPv4Network, IPv4Address
from shutil import copyfile
import json
from yaml import dump
SEEDEMU_INTERNET_MAP_IMAGE='handsonsecurity/seedemu-multiarch-map:buildx-latest'
SEEDEMU_ETHER_VIEW_IMAGE='handsonsecurity/seedemu-multiarch-etherview:buildx-latest'
DockerCompilerFileTemplates: Dict[str, str] = {}
DockerCompilerFileTemplates['dockerfile'] = """\
ARG DEBIAN_FRONTEND=noninteractive
"""
#RUN echo 'exec zsh' > /root/.bashrc
DockerCompilerFileTemplates['start_script'] = """\
#!/bin/bash
{startCommands}
echo "ready! run 'docker exec -it $HOSTNAME /bin/zsh' to attach to this node" >&2
{buildtime_sysctl}
tail -f /dev/null
"""
DockerCompilerFileTemplates['seedemu_sniffer'] = """\
#!/bin/bash
last_pid=0
while read -sr expr; do {
[ "$last_pid" != 0 ] && kill $last_pid 2> /dev/null
[ -z "$expr" ] && continue
tcpdump -e -i any -nn -p -q "$expr" &
last_pid=$!
}; done
[ "$last_pid" != 0 ] && kill $last_pid
"""
DockerCompilerFileTemplates['seedemu_worker'] = """\
#!/bin/bash
net() {
[ "$1" = "status" ] && {
ip -j link | jq -cr '.[] .operstate' | grep -q UP && echo "up" || echo "down"
return
}
ip -j li | jq -cr '.[] .ifname' | while read -r ifname; do ip link set "$ifname" "$1"; done
}
bgp() {
cmd="$1"
peer="$2"
[ "$cmd" = "bird_peer_down" ] && birdc dis "$2"
[ "$cmd" = "bird_peer_up" ] && birdc en "$2"
}
while read -sr line; do {
id="`cut -d ';' -f1 <<< "$line"`"
cmd="`cut -d ';' -f2 <<< "$line"`"
output="no such command."
[ "$cmd" = "net_down" ] && output="`net down 2>&1`"
[ "$cmd" = "net_up" ] && output="`net up 2>&1`"
[ "$cmd" = "net_status" ] && output="`net status 2>&1`"
[ "$cmd" = "bird_list_peer" ] && output="`birdc s p | grep --color=never BGP 2>&1`"
[[ "$cmd" == "bird_peer_"* ]] && output="`bgp $cmd 2>&1`"
printf '_BEGIN_RESULT_'
jq -Mcr --arg id "$id" --arg return_value "$?" --arg output "$output" -n '{id: $id | tonumber, return_value: $return_value | tonumber, output: $output }'
printf '_END_RESULT_'
}; done
"""
DockerCompilerFileTemplates['replace_address_script'] = '''\
#!/bin/bash
ip -j addr | jq -cr '.[]' | while read -r iface; do {
ifname="`jq -cr '.ifname' <<< "$iface"`"
jq -cr '.addr_info[]' <<< "$iface" | while read -r iaddr; do {
addr="`jq -cr '"\(.local)/\(.prefixlen)"' <<< "$iaddr"`"
line="`grep "$addr" < /dummy_addr_map.txt`"
[ -z "$line" ] && continue
new_addr="`cut -d, -f2 <<< "$line"`"
ip addr del "$addr" dev "$ifname"
ip addr add "$new_addr" dev "$ifname"
}; done
}; done
'''
DockerCompilerFileTemplates['compose'] = """\
version: "3.4"
services:
{dummies}
{services}
networks:
{networks}
{volumes}
"""
DockerCompilerFileTemplates['compose_dummy'] = """\
{imageDigest}:
build:
context: .
dockerfile: dummies/{imageDigest}
image: {imageDigest}
{dependsOn}
"""
DockerCompilerFileTemplates['depends_on'] = """\
depends_on:
- {dependsOn}
"""
DockerCompilerFileTemplates['compose_service'] = """\
{nodeId}:
build: ./{nodeId}
container_name: {nodeName}
depends_on:
- {dependsOn}
cap_add:
- ALL
{sysctls}
privileged: true
networks:
{networks}{ports}{volumes}
labels:
{labelList}
environment:
{environment}
"""
DockerCompilerFileTemplates['compose_sysctl'] = """
sysctls:
"""
DockerCompilerFileTemplates['compose_label_meta'] = """\
org.seedsecuritylabs.seedemu.meta.{key}: "{value}"
"""
DockerCompilerFileTemplates['compose_ports'] = """\
ports:
{portList}
"""
DockerCompilerFileTemplates['compose_port'] = """\
- {hostPort}:{nodePort}/{proto}
"""
DockerCompilerFileTemplates['compose_volumes'] = """\
volumes:
{volumeList}
"""
DockerCompilerFileTemplates['compose_service_network'] = """\
{netId}:
{address}
"""
DockerCompilerFileTemplates['compose_service_network_address'] = """\
ipv4_address: {address}
"""
DockerCompilerFileTemplates['compose_network'] = """\
{netId}:
driver_opts:
com.docker.network.driver.mtu: {mtu}
ipam:
config:
- subnet: {prefix}
labels:
{labelList}
"""
DockerCompilerFileTemplates['seedemu_internet_map'] = """\
seedemu-internet-client:
image: {clientImage}
container_name: seedemu_internet_map
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- {clientPort}:8080/tcp
"""
DockerCompilerFileTemplates['seedemu_ether_view'] = """\
seedemu-ether-client:
image: {clientImage}
container_name: seedemu_ether_view
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- {clientPort}:5000/tcp
"""
DockerCompilerFileTemplates['zshrc_pre'] = """\
export NOPRECMD=1
alias st=set_title
"""
DockerCompilerFileTemplates['local_image'] = """\
{imageName}:
build:
context: {dirName}
image: {imageName}
"""
# class DockerImage(object):
# """!
# @brief The DockerImage class.
# This class represents a candidate image for docker compiler.
# """
# __software: Set[str]
# __name: str
# __local: bool
# __dirName: str
# def __init__(self, name: str, software: List[str], local: bool = False, dirName: str = None) -> None:
# """!
# @brief create a new docker image.
# @param name name of the image. Can be name of a local image, image on
# dockerhub, or image in private repo.
# @param software set of software pre-installed in the image, so the
# docker compiler can skip them when compiling.
# @param local (optional) set this image as a local image. A local image
# is built locally instead of pulled from the docker hub. Default to False.
# @param dirName (optional) directory name of the local image (when local
# is True). Default to None. None means use the name of the image.
# """
# super().__init__()
# self.__name = name
# self.__software = set()
# self.__local = local
# self.__dirName = dirName if dirName != None else name
# for soft in software:
# self.__software.add(soft)
# def getName(self) -> str:
# """!
# @brief get the name of this image.
# @returns name.
# """
# return self.__name
# def getSoftware(self) -> Set[str]:
# """!
# @brief get set of software installed on this image.
# @return set.
# """
# return self.__software
# def getDirName(self) -> str:
# """!
# @brief returns the directory name of this image.
# @return directory name.
# """
# return self.__dirName
# def isLocal(self) -> bool:
# """!
# @brief returns True if this image is local.
# @return True if this image is local.
# """
# return self.__local
# def addSoftwares(self, software) -> DockerImage:
# """!
# @brief add softwares to this image.
# @return self, for chaining api calls.
# """
# for soft in software:
# self.__software.add(soft)
class Docker(Compiler):
"""!
@brief The Docker compiler class.
Docker is one of the compiler driver. It compiles the lab to docker
containers.
"""
__services: str
__networks: str
__naming_scheme: str
__self_managed_network: bool
__dummy_network_pool: Generator[IPv4Network, None, None]
__internet_map_enabled: bool
__internet_map_port: int
__ether_view_enabled: bool
__ether_view_port: int
__client_hide_svcnet: bool
__images: Dict[str, Tuple[DockerImage, int]]
__forced_image: str
__disable_images: bool
__image_per_node_list: Dict[Tuple[str, str], DockerImage]
_used_images: Set[str]
__config: List[ Tuple[BaseOption , NodeScope] ] # all encountered Options for .env file
__option_handling: OptionHandling # strategy how to deal with Options
__basesystem_dockerimage_mapping: dict
def __init__(
self,
platform:Platform = Platform.AMD64,
namingScheme: str = "as{asn}{role}-{displayName}-{primaryIp}",
selfManagedNetwork: bool = False,
dummyNetworksPool: str = '10.128.0.0/9',
dummyNetworksMask: int = 24,
internetMapEnabled: bool = True,
internetMapPort: int = 8080,
etherViewEnabled: bool = False,
etherViewPort: int = 5000,
clientHideServiceNet: bool = True,
option_handling: OptionHandling = OptionHandling.CREATE_SEPARATE_ENV_FILE
):
"""!
@brief Docker compiler constructor.
@param platform (optional) node cpu architecture Default to Platform.AMD64
@param namingScheme (optional) node naming scheme. Available variables
are: {asn}, {role} (r - router, h - host, rs - route server), {name},
{primaryIp} and {displayName}. {displayName} will automatically fall
back to {name} if
Default to as{asn}{role}-{displayName}-{primaryIp}.
@param selfManagedNetwork (optional) use self-managed network. Enable
this to manage the network inside containers instead of using docker's
network management. This works by first assigning "dummy" prefix and
address to containers, then replace those address with "real" address
when the containers start. This will allow the use of overlapping
networks in the emulation and will allow the use of the ".1" address on
nodes. Note this will break port forwarding (except for service nodes
like real-world access node and remote access node.) Default to False.
@param dummyNetworksPool (optional) dummy networks pool. This should not
overlap with any "real" networks used in the emulation, including
loopback IP addresses. Default to 10.128.0.0/9.
@param dummyNetworksMask (optional) mask of dummy networks. Default to
24.
@param internetMapEnabled (optional) set if seedemu internetMap should be enabled.
Default to False. Note that the seedemu internetMap allows unauthenticated
access to all nodes, which can potentially allow root access to the
emulator host. Only enable seedemu in a trusted network.
@param internetMapPort (optional) set seedemu internetMap port. Default to 8080.
@param etherViewEnabled (optional) set if seedemu EtherView should be enabled.
Default to False.
@param etherViewPort (optional) set seedemu EtherView port. Default to 5000.
@param clientHideServiceNet (optional) hide service network for the
client map by not adding metadata on the net. Default to True.
"""
self.__option_handling = option_handling
self.__networks = ""
self.__services = ""
self.__naming_scheme = namingScheme
self.__self_managed_network = selfManagedNetwork
self.__dummy_network_pool = IPv4Network(dummyNetworksPool).subnets(new_prefix = dummyNetworksMask)
self.__internet_map_enabled = internetMapEnabled
self.__internet_map_port = internetMapPort
self.__ether_view_enabled = etherViewEnabled
self.__ether_view_port = etherViewPort
self.__client_hide_svcnet = clientHideServiceNet
self.__images = {}
self.__forced_image = None
self.__disable_images = False
self._used_images = set()
self.__image_per_node_list = {}
self.__config = [] # variables for '.env' file alongside 'docker-compose.yml'
self.__volumes_dedup = (
[]
) # unforunately set(()) failed to automatically deduplicate
self.__vol_names = []
super().__init__()
self.__platform = platform
self.__basesystem_dockerimage_mapping = BASESYSTEM_DOCKERIMAGE_MAPPING_PER_PLATFORM[self.__platform]
for name, image in self.__basesystem_dockerimage_mapping.items():
priority = 0
if name == BaseSystem.DEFAULT:
priority = 1
self.addImage(image, priority=priority)
def _addVolume(self, vol: BaseVolume):
"""! @brief add a docker volume/bind mount/or tmpfs
Remember them for later, to generate the top lvl 'volumes:' section of docker-compose.yml
"""
# if vol.type() == 'volume': # then it is a named-volume
key = vol.asDict()["source"]
if key not in self.__vol_names:
self.__volumes_dedup.append(vol)
self.__vol_names.append(key)
return self
def _getVolumes(self) -> List[BaseVolume]:
"""! @brief get all docker volumes/mounts that must appear
in docker-compose.yml top-level 'volumes:' section
"""
return self.__volumes_dedup
def optionHandlingCapabilities(self) -> OptionHandling:
return OptionHandling.DIRECT_DOCKER_COMPOSE | OptionHandling.CREATE_SEPARATE_ENV_FILE
def getName(self) -> str:
return "Docker"
def addImage(self, image: DockerImage, priority: int = -1) -> Docker:
"""!
@brief add an candidate image to the compiler.
@param image image to add.
@param priority (optional) priority of this image. Used when one or more
images with same number of missing software exist. The one with highest
priority wins. If two or more images with same priority and same number
of missing software exist, the one added the last will be used. All
built-in images has priority of 0. Default to -1. All built-in images are
prior to the added candidate image. To set a candidate image to a node,
use setImageOverride() method.
@returns self, for chaining api calls.
"""
assert image.getName() not in self.__images, 'image with name {} already exists.'.format(image.getName())
self.__images[image.getName()] = (image, priority)
return self
def getImages(self) -> List[Tuple[DockerImage, int]]:
"""!
@brief get list of images configured.
@returns list of tuple of images and priority.
"""
return list(self.__images.values())
def forceImage(self, imageName: str) -> Docker:
"""!
@brief forces the docker compiler to use a image, identified by the
imageName. Image with such name must be added to the docker compiler
with the addImage method, or the docker compiler will fail at compile
time. Set to None to disable the force behavior.
@param imageName name of the image.
@returns self, for chaining api calls.
"""
self.__forced_image = imageName
return self
def disableImages(self, disabled: bool = True) -> Docker:
"""!
@brief forces the docker compiler to not use any images and build
everything for starch. Set to False to disable the behavior.
@param disabled (option) disabled image if True. Default to True.
@returns self, for chaining api calls.
"""
self.__disable_images = disabled
return self
def setImageOverride(self, node:Node, imageName:str) -> Docker:
"""!
@brief set the docker compiler to use a image on the specified Node.
@param node target node to override image.
@param imageName name of the image to use.
@returns self, for chaining api calls.
"""
asn = node.getAsn()
name = node.getName()
self.__image_per_node_list[(asn, name)]=imageName
def _groupSoftware(self, emulator: Emulator):
"""!
@brief Group apt-get install calls to maximize docker cache.
@param emulator emulator to load nodes from.
"""
registry = emulator.getRegistry()
# { [imageName]: { [softName]: [nodeRef] } }
softGroups: Dict[str, Dict[str, List[Node]]] = {}
# { [imageName]: useCount }
groupIter: Dict[str, int] = {}
for ((scope, type, name), obj) in registry.getAll().items():
if type not in ['rnode', 'csnode', 'hnode', 'snode', 'rs', 'snode']:
continue
node: Node = obj
(img, _) = self._selectImageFor(node)
imgName = img.getName()
if not imgName in groupIter:
groupIter[imgName] = 0
groupIter[imgName] += 1
if not imgName in softGroups:
softGroups[imgName] = {}
group = softGroups[imgName]
for soft in node.getSoftware():
if soft not in group:
group[soft] = []
group[soft].append(node)
for (key, val) in softGroups.items():
maxIter = groupIter[key]
self._log('grouping software for image "{}" - {} references.'.format(key, maxIter))
step = 1
for commRequired in range(maxIter, 0, -1):
currentTier: Set[str] = set()
currentTierNodes: Set[Node] = set()
for (soft, nodes) in val.items():
if len(nodes) == commRequired:
currentTier.add(soft)
for node in nodes: currentTierNodes.add(node)
for node in currentTierNodes:
if not node.hasAttribute('__soft_install_tiers'):
node.setAttribute('__soft_install_tiers', [])
node.getAttribute('__soft_install_tiers').append(currentTier)
if len(currentTier) > 0:
self._log('the following software has been grouped together in step {}: {} since they are referenced by {} nodes.'.format(step, currentTier, len(currentTierNodes)))
step += 1
def _selectImageFor(self, node: Node) -> Tuple[DockerImage, Set[str]]:
"""!
@brief select image for the given node.
@param node node.
@returns tuple of selected image and set of missing software.
"""
nodeSoft = node.getSoftware()
nodeKey = (node.getAsn(), node.getName())
# #1 Highest Priority (User Custom Image)
if nodeKey in self.__image_per_node_list:
image_name = self.__image_per_node_list[nodeKey]
assert image_name in self.__images, 'image-per-node configured, but image {} does not exist.'.format(image_name)
(image, _) = self.__images[image_name]
self._log('image-per-node configured, using {}'.format(image.getName()))
return (image, nodeSoft - image.getSoftware())
# Should we keep this feature?
if self.__disable_images:
self._log('disable-imaged configured, using base image.')
(image, _) = self.__images['ubuntu:20.04']
return (image, nodeSoft - image.getSoftware())
# Set Default Image for All Nodes
if self.__forced_image != None:
assert self.__forced_image in self.__images, 'forced-image configured, but image {} does not exist.'.format(self.__forced_image)
(image, _) = self.__images[self.__forced_image]
self._log('force-image configured, using image: {}'.format(image.getName()))
return (image, nodeSoft - image.getSoftware())
#Maintain a table : Virtual Image Name - Actual Image Name
image = self.__basesystem_dockerimage_mapping[node.getBaseSystem()]
return (image, nodeSoft - image.getSoftware())
# candidates: List[Tuple[DockerImage, int]] = []
# minMissing = len(nodeSoft)
# for (image, prio) in self.__images.values():
# missing = len(nodeSoft - image.getSoftware())
# if missing < minMissing:
# candidates = []
# minMissing = missing
# if missing <= minMissing:
# candidates.append((image, prio))
# assert len(candidates) > 0, '_electImageFor ended w/ no images?'
# (selected, maxPrio) = candidates[0]
# for (candidate, prio) in candidates:
# if prio >= maxPrio:
# maxPrio = prio
# selected = candidate
# return (selected, nodeSoft - selected.getSoftware())
def _getNetMeta(self, net: Network) -> str:
"""!
@brief get net metadata labels.
@param net net object.
@returns metadata labels string.
"""
(scope, type, name) = net.getRegistryInfo()
labels = ''
if self.__client_hide_svcnet and scope == 'seedemu' and name == '000_svc':
return DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'dummy',
value = 'dummy label for hidden node/net'
)
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'type',
value = 'global' if scope == 'ix' else 'local'
)
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'scope',
value = scope
)
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'name',
value = name
)
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'prefix',
value = net.getPrefix()
)
if net.getDisplayName() != None:
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'displayname',
value = net.getDisplayName()
)
if net.getDescription() != None:
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'description',
value = net.getDescription()
)
return labels
def _getNodeMeta(self, node: Node) -> str:
"""!
@brief get node metadata labels.
@param node node object.
@returns metadata labels string.
"""
(scope, type, name) = node.getRegistryInfo()
labels = ''
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'asn',
value = node.getAsn()
)
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'nodename',
value = name
)
if type == 'hnode':
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'role',
value = 'Host'
)
if type == 'rnode':
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'role',
value = 'Router'
)
if type == 'brdnode':
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'role',
value = 'BorderRouter'
)
if type == 'csnode':
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'role',
value = 'SCION Control Service'
)
if type == 'snode':
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'role',
value = 'Emulator Service Worker'
)
if type == 'rs':
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'role',
value = 'Route Server'
)
if node.getDisplayName() != None:
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'displayname',
value = node.getDisplayName()
)
if node.getDescription() != None:
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'description',
value = node.getDescription()
)
if len(node.getClasses()) > 0:
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'class',
value = json.dumps(node.getClasses()).replace("\"", "\\\"")
)
for key, value in node.getLabel().items():
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = key,
value = value
)
n = 0
for iface in node.getInterfaces():
net = iface.getNet()
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'net.{}.name'.format(n),
value = net.getName()
)
labels += DockerCompilerFileTemplates['compose_label_meta'].format(
key = 'net.{}.address'.format(n),
value = '{}/{}'.format(iface.getAddress(), net.getPrefix().prefixlen)
)
n += 1
return labels
def _nodeRoleToString(self, role: NodeRole):
"""!
@brief convert node role to prefix string
@param role node role
@returns prefix string
"""
if role == NodeRole.Host: return 'h'
if role == NodeRole.Router: return 'r'
if role == NodeRole.ControlService: return 'cs'
if role == NodeRole.RouteServer: return 'rs'
if role == NodeRole.BorderRouter: return 'brd'
assert False, 'unknown node role {}'.format(role)
def _contextToPrefix(self, scope: str, type: str) -> str:
"""!
@brief Convert context to prefix.
@param scope scope.
@param type type.
@returns prefix string.
"""
return '{}_{}_'.format(type, scope)
def _addFile(self, path: str, content: str) -> str:
"""!
@brief Stage file to local folder and return Dockerfile command.
@param path path to file. (in container)
@param content content of the file.
@returns COPY expression for dockerfile.
"""
staged_path = md5(path.encode('utf-8')).hexdigest()
print(content, file=open(staged_path, 'w'))
return 'COPY {} {}\n'.format(staged_path, path)
def _importFile(self, path: str, hostpath: str) -> str:
"""!
@brief Stage file to local folder and return Dockerfile command.
@param path path to file. (in container)
@param hostpath path to file. (on host)
@returns COPY expression for dockerfile.
"""
staged_path = md5(path.encode('utf-8')).hexdigest()
copyfile(hostpath, staged_path)
return 'COPY {} {}\n'.format(staged_path, path)
def _getComposeNodeName(self, node: Node) -> str:
"""!
@brief Given a node, compute its final container_name, as it will be
known in the docker-compose file.
"""
name = self.__naming_scheme.format(
asn = node.getAsn(),
role = self._nodeRoleToString(node.getRole()),
name = node.getName(),
displayName = node.getDisplayName() if node.getDisplayName() != None else node.getName(),
primaryIp = node.getInterfaces()[0].getAddress()
)
return sub(r'[^a-zA-Z0-9_.-]', '_', name)
def _getRealNodeName(self, node: Node) -> str:
"""!
@brief Computes the sub directory names inside the output folder.
"""
(scope, type, _) = node.getRegistryInfo()
prefix = self._contextToPrefix(scope, type)
return '{}{}'.format(prefix, node.getName())
def _getRealNetName(self, net: Network):
"""!
@brief Computes name of a network as it will be known in the docker-compose file.
"""
(netscope, _, _) = net.getRegistryInfo()
net_prefix = self._contextToPrefix(netscope, 'net')
if net.getType() == NetworkType.Bridge: net_prefix = ''
return '{}{}'.format(net_prefix, net.getName())
def _getComposeServicePortList(self, node: Node) -> str:
"""!
@brief Computes the 'ports:' section of the service in docker-compose.yml.
"""
_ports = node.getPorts()
ports = ''
if len(_ports) > 0:
lst = ''
for (h, n, p) in _ports:
lst += DockerCompilerFileTemplates['compose_port'].format(
hostPort = h,
nodePort = n,
proto = p
)
ports = DockerCompilerFileTemplates['compose_ports'].format(
portList = lst
)
return ports
def _getComposeNodeNets(self, node: Node) -> str:
node_nets = ''
dummy_addr_map = ''
for iface in node.getInterfaces():
net = iface.getNet()
real_netname = self._getRealNetName(net)
address = iface.getAddress()
if self.__self_managed_network and net.getType() != NetworkType.Bridge:
d_index: int = net.getAttribute('dummy_prefix_index')
d_prefix: IPv4Network = net.getAttribute('dummy_prefix')
d_address: IPv4Address = d_prefix[d_index]
net.setAttribute('dummy_prefix_index', d_index + 1)
dummy_addr_map += '{}/{},{}/{}\n'.format(
d_address, d_prefix.prefixlen,
iface.getAddress(), iface.getNet().getPrefix().prefixlen
)
address = d_address
self._log('using self-managed network: using dummy address {}/{} for {}/{} on as{}/{}'.format(
d_address, d_prefix.prefixlen, iface.getAddress(), iface.getNet().getPrefix().prefixlen,
node.getAsn(), node.getName()
))
if address == None:
address = ""
else:
address = DockerCompilerFileTemplates['compose_service_network_address'].format(address = address)
node_nets += DockerCompilerFileTemplates['compose_service_network'].format(
netId = real_netname,
address = address
)
return node_nets, dummy_addr_map
def _getComposeNodeVolumes(self, node: Node) -> str:
""" compute the docker-compose 'volumes:' section for this service(emulation node)"""
volumes = ''
# svcvols = map( lambda vol: ServiceLvlVolume(vol), node.getCustomVolumes() )
svcvols = list (set(node.getDockerVolumes() ))
for v in svcvols:
v.mode = 'service'
yamlvols = '\n'.join(map( lambda line: ' ' + line ,dump( svcvols ).split('\n') ) )
volumes +=' volumes:\n' + yamlvols if len(node.getDockerVolumes()) > 0 else ''
# the top-level docker-compose volumes section is rendered at a later stage ..
# Remember encountered volumes until then
for v in node.getDockerVolumes():
self._addVolume(v)
return volumes
def _computeDockerfile(self, node: Node) -> str:
"""!
@brief Returns dockerfile contents for node.
"""
dockerfile = DockerCompilerFileTemplates['dockerfile']
(image, soft) = self._selectImageFor(node)
if not node.hasAttribute('__soft_install_tiers') and len(soft) > 0:
dockerfile += 'RUN apt-get update && apt-get install -y --no-install-recommends {}\n'.format(' '.join(sorted(soft)))
if node.hasAttribute('__soft_install_tiers'):
softLists: List[List[str]] = node.getAttribute('__soft_install_tiers')
for softList in softLists:
softList = set(softList) & soft
if len(softList) == 0: continue
dockerfile += 'RUN apt-get update && apt-get install -y --no-install-recommends {}\n'.format(' '.join(sorted(softList)))
#included in the seedemu-base dockerImage.
#dockerfile += 'RUN curl -L https://grml.org/zsh/zshrc > /root/.zshrc\n'
dockerfile = 'FROM {}\n'.format(md5(image.getName().encode('utf-8')).hexdigest()) + dockerfile
self._used_images.add(image.getName())
for cmd in node.getDockerCommands(): dockerfile += '{}\n'.format(cmd)
for cmd in node.getBuildCommands(): dockerfile += 'RUN {}\n'.format(cmd)
start_commands = ''
if self.__self_managed_network:
start_commands += 'chmod +x /replace_address.sh\n'
start_commands += '/replace_address.sh\n'
dockerfile += self._addFile('/replace_address.sh', DockerCompilerFileTemplates['replace_address_script'])
dockerfile += self._addFile('/root/.zshrc.pre', DockerCompilerFileTemplates['zshrc_pre'])
for (cmd, fork) in node.getStartCommands():
start_commands += '{}{}\n'.format(cmd, ' &' if fork else '')
for (cmd, fork) in node.getPostConfigCommands():
start_commands += '{}{}\n'.format(cmd, ' &' if fork else '')
dockerfile += self._addFile('/start.sh', DockerCompilerFileTemplates['start_script'].format(
startCommands = start_commands,
buildtime_sysctl=self._getNodeBuildtimeSysctl(node)
))
dockerfile += self._addFile('/seedemu_sniffer', DockerCompilerFileTemplates['seedemu_sniffer'])
dockerfile += self._addFile('/seedemu_worker', DockerCompilerFileTemplates['seedemu_worker'])
dockerfile += 'RUN chmod +x /start.sh\n'
dockerfile += 'RUN chmod +x /seedemu_sniffer\n'
dockerfile += 'RUN chmod +x /seedemu_worker\n'