From 543a28318d081ab7cd1feba0979db08f657a42ea Mon Sep 17 00:00:00 2001
From: Attila Farkas <394097@mail.muni.cz>
Date: Thu, 2 Jul 2020 12:32:18 +0000
Subject: [PATCH] Resolve "Change base_box to be compatible with the latest
 KYPO topology definition"

---
 big_broker.yml                             | 15 ++++++++-----
 conf/box_mapping.yml                       |  1 +
 conf/router_attributes.yml                 |  3 ++-
 conf/vagrant_mapping.yml                   |  4 ++--
 modules/vagrant_generator.py               | 25 ++++++++++++++++++++++
 topologies/0-routers-1-network-1-host.yml  |  3 ++-
 topologies/0-routers-1-network-5-hosts.yml | 15 ++++++++-----
 topologies/1-router-1-network-1-host.yml   |  3 ++-
 topologies/1-router-2-networks-2-hosts.yml |  6 ++++--
 topology.yml                               |  6 ++++--
 10 files changed, 62 insertions(+), 19 deletions(-)
 create mode 100644 conf/box_mapping.yml

diff --git a/big_broker.yml b/big_broker.yml
index 11921f9..9bd863b 100644
--- a/big_broker.yml
+++ b/big_broker.yml
@@ -2,23 +2,28 @@
 name: big-broker
 hosts:
   - name: web
-    base_box: generic/debian10
+    base_box:
+      image: generic/debian10
     memory: 2048
 
   - name: workstation
-    base_box: generic/debian10
+    base_box:
+      image: generic/debian10
     memory: 2048
 
   - name: database
-    base_box: generic/debian10
+    base_box:
+      image: generic/debian10
     memory: 2048
 
   - name: attacker
-    base_box: mu/kali-2019.4
+    base_box:
+      image: kali-2019.4
     memory: 2048
 
   - name: client
-    base_box: generic/debian10
+    base_box:
+      image: generic/debian10
     memory: 2048
 
 routers:
diff --git a/conf/box_mapping.yml b/conf/box_mapping.yml
new file mode 100644
index 0000000..f4c7448
--- /dev/null
+++ b/conf/box_mapping.yml
@@ -0,0 +1 @@
+kali-2019.4: muni-kypo/kali-2019.4
diff --git a/conf/router_attributes.yml b/conf/router_attributes.yml
index a62efd3..3d811b0 100644
--- a/conf/router_attributes.yml
+++ b/conf/router_attributes.yml
@@ -1,3 +1,4 @@
-base_box: debian/stretch64
+base_box:
+  image: debian/stretch64
 memory: 256
 cpus: 1
diff --git a/conf/vagrant_mapping.yml b/conf/vagrant_mapping.yml
index 3d7abf6..45ce921 100644
--- a/conf/vagrant_mapping.yml
+++ b/conf/vagrant_mapping.yml
@@ -1,7 +1,6 @@
 string:
     base_mac: vm.base_mac
     base_address: vm.base_address
-    base_box: vm.box
     box_download_checksum: vm.box_download_checksum
     box_download_checksum_type: vm.box_download_checksum_type
     box_download_client_cert: vm.box_download_client_cert
@@ -19,7 +18,6 @@ string:
     ssh_remote_user: ssh.remote_user
     ssh_shell: ssh.shell 
     ssh_sudo_command: ssh.sudo_command
-    ssh_username: ssh.username
     winrm_username: winrm.username
     winrm_password: winrm.password
     winrm_host: winrm.host
@@ -57,6 +55,8 @@ boolean:
     winrm_ssl_peer_verification: winrm.ssl_peer_verification
     winssh_forward_agent: winssh.forward_agent
     winssh_keep_alive: winssh.keep_alive
+special:
+    base_box: vm.box
 other:
     box_url: vm.box_url
     guest: vm.guest
diff --git a/modules/vagrant_generator.py b/modules/vagrant_generator.py
index 44dc52d..044160a 100644
--- a/modules/vagrant_generator.py
+++ b/modules/vagrant_generator.py
@@ -4,6 +4,7 @@ from modules.file_manager import generate_file, open_yaml
 
 VAGRANT_MAPPING = open_yaml("conf/vagrant_mapping.yml")
 VIRTUALBOX_MAPPING = open_yaml("conf/virtualbox_mapping.yml")
+BOX_MAPPING = open_yaml("conf/box_mapping.yml")
 BASE_PLAYBOOK = "base_provisioning/device_configuration.yml"
 USER_PLAYBOOK = "provisioning/playbook.yml"
 
@@ -31,6 +32,28 @@ def _create_complex_attribute(key, value):
     return attribute
 
 
+def _create_special_commands(attribute, value):
+    """Create special commands that cannot be mapped to a single command."""
+    special_attributes = []
+    if attribute == "base_box":
+        if "image" in value:
+            if value["image"] in BOX_MAPPING:
+                value["image"] = BOX_MAPPING[value["image"]]
+            box_name = dict()
+            box_name["type"] = "string"
+            box_name["command"] = "vm.box"
+            box_name["value"] = value["image"]
+            special_attributes.append(box_name)
+        if "man_user" in value:
+            user = dict()
+            user["type"] = "string"
+            user["command"] = "ssh.username"
+            user["value"] = value["man_user"]
+            special_attributes.append(user)
+
+    return special_attributes
+
+
 def _create_commands(device_attributes):
     """Create basic vagrant definition commands for a device."""
     commands = []
@@ -53,6 +76,8 @@ def _create_commands(device_attributes):
             vagrant_attribute = VAGRANT_MAPPING["other"][attribute]
             commands.append(_create_complex_attribute(vagrant_attribute,
                                                       value))
+        elif attribute in VAGRANT_MAPPING["special"]:
+            commands.extend(_create_special_commands(attribute, value))
         elif attribute in VIRTUALBOX_MAPPING["integer"]:
             vagrant_attribute = VIRTUALBOX_MAPPING["integer"][attribute]
             vb_commands.append(_create_simple_attribute(vagrant_attribute,
diff --git a/topologies/0-routers-1-network-1-host.yml b/topologies/0-routers-1-network-1-host.yml
index 7d2afb3..080591c 100644
--- a/topologies/0-routers-1-network-1-host.yml
+++ b/topologies/0-routers-1-network-1-host.yml
@@ -2,7 +2,8 @@
 name: 0-routers-1-networks-1-hosts
 hosts:
   - name: debian10
-    base_box: generic/debian10
+    base_box:
+      image: generic/debian10
     flavor: csirtmu.tiny1x4
     cpus: 2
 
diff --git a/topologies/0-routers-1-network-5-hosts.yml b/topologies/0-routers-1-network-5-hosts.yml
index b389367..722e384 100644
--- a/topologies/0-routers-1-network-5-hosts.yml
+++ b/topologies/0-routers-1-network-5-hosts.yml
@@ -2,23 +2,28 @@
 name: 1-network-various-boxes
 hosts:
   - name: debian10
-    base_box: generic/debian10
+    base_box:
+      image: generic/debian10
     memory: 512
 
   - name: debian9
-    base_box: generic/debian9
+    base_box:
+      image: generic/debian9
     memory: 512
 
   - name: debian-stretch
-    base_box: debian/contrib-stretch64
+    base_box:
+      image: debian/contrib-stretch64
     memory: 512
 
   - name: ubuntu-xenial
-    base_box: ubuntu/xenial64
+    base_box:
+      image: ubuntu/xenial64
     memory: 1024
 
   - name: mu-kali
-    base_box: mu/kali-2019.4
+    base_box:
+      image: kali-2019.4
     memory: 2048
 
 networks:
diff --git a/topologies/1-router-1-network-1-host.yml b/topologies/1-router-1-network-1-host.yml
index 1001782..88a8e35 100644
--- a/topologies/1-router-1-network-1-host.yml
+++ b/topologies/1-router-1-network-1-host.yml
@@ -3,7 +3,8 @@
 name: 1-router-1-host
 hosts:
   - name: debian10
-    base_box: generic/debian10
+    base_box:
+      image: generic/debian10
     memory: 512
 
 routers:
diff --git a/topologies/1-router-2-networks-2-hosts.yml b/topologies/1-router-2-networks-2-hosts.yml
index 4fba3a2..df921b9 100644
--- a/topologies/1-router-2-networks-2-hosts.yml
+++ b/topologies/1-router-2-networks-2-hosts.yml
@@ -2,11 +2,13 @@
 name: 2-networks
 hosts:
   - name: server
-    base_box: generic/debian10
+    base_box:
+      image: generic/debian10
     memory: 512
 
   - name: home
-    base_box: generic/debian10
+    base_box:
+      image: generic/debian10
     memory: 512
 
 routers:
diff --git a/topology.yml b/topology.yml
index 9a68624..02978c4 100644
--- a/topology.yml
+++ b/topology.yml
@@ -1,12 +1,14 @@
 name: small-sandbox
 hosts:
   - name: server
-    base_box: ubuntu/xenial64
+    base_box:
+      image: ubuntu/xenial64
     flavor: csirtmu.tiny1x4
     cpus: 2
 
   - name: home
-    base_box: ubuntu/xenial64
+    base_box:
+      image: ubuntu/xenial64
     boot_timeout: 1000
     box_check_update: false
 
-- 
GitLab