Repository: cablelabs/transparent-security

Issue:
Refactor RESTful APIs for only one class per create/delete API
http_server_flask.py currently has 2 classes for each data_drop insert/delete (Gateway & Aggregate). As each is being called via POST while using the same payload, the delete method should be created from the remove classes "post" methods.

Discussion:

Patch:
diff --git a/playbooks/scenarios/full/packet-flood.yml b/playbooks/scenarios/full/packet-flood.yml
index ab87387..29ea56e 100644
--- a/playbooks/scenarios/full/packet-flood.yml
+++ b/playbooks/scenarios/full/packet-flood.yml
@@ -41,8 +41,7 @@
     send_loops: 2
     send_loop_delay: 5
     cleanup_rest_call:
-      url: "http://{{ sdn_ip }}:{{ sdn_port }}/stopAttack"
-      method: POST
+      url: "http://{{ sdn_ip }}:{{ sdn_port }}/gwAttack"
       body:
         src_ip: "{% if scenario_send_ip_version|int == 4 %}{{ sender.ip }}{% else %}{{ sender.ipv6 }}{% endif %}"
         packet_size: 112
@@ -80,8 +79,7 @@
     send_loops: 2
     send_loop_delay: 5
     cleanup_rest_call:
-      url: "http://{{ sdn_ip }}:{{ sdn_port }}/stopAttack"
-      method: POST
+      url: "http://{{ sdn_ip }}:{{ sdn_port }}/gwAttack"
       body:
         src_ip: "{% if scenario_send_ip_version|int == 4 %}{{ sender.ip }}{% else %}{{ sender.ipv6 }}{% endif %}"
         packet_size: 112
@@ -119,8 +117,7 @@
     send_loops: 2
     send_loop_delay: 5
     cleanup_rest_call:
-      url: "http://{{ sdn_ip }}:{{ sdn_port }}/stopAttack"
-      method: POST
+      url: "http://{{ sdn_ip }}:{{ sdn_port }}/gwAttack"
       body:
         src_ip: "{% if scenario_send_ip_version|int == 4 %}{{ sender.ip }}{% else %}{{ sender.ipv6 }}{% endif %}"
         packet_size: 112
diff --git a/playbooks/scenarios/gateway/data-drop.yml b/playbooks/scenarios/gateway/data-drop.yml
index d162a59..81197ac 100644
--- a/playbooks/scenarios/gateway/data-drop.yml
+++ b/playbooks/scenarios/gateway/data-drop.yml
@@ -30,20 +30,13 @@
     send_sw_port: "{{ topo_dict.links[0].south_facing_port }}"
     receiver: "{{ topo_dict.hosts[remote_rec_host] }}"
     rec_sw_port: "{{ topo_dict.links[1].north_facing_port }}"
-    attack_body:
-      src_ip: "{% if scenario_send_ip_version|int == 4 %}{{ sender.ip }}{% else %}{{ sender.ipv6 }}{% endif %}"
-      packet_size: 112
-      attack_type: "UDP Flood"
-      dst_port: "{{ send_port }}"
-      dst_ip: "{% if scenario_send_ip_version|int == 4 %}{{ receiver.ip }}{% else %}{{ receiver.ipv6 }}{% endif %}"
-      src_mac: "{{ sender.mac }}"
-    start_attack:
-      url: "http://{{ sdn_ip }}:{{ sdn_port }}/attack"
-      method: POST
-      body: "{{ attack_body }}"
-      ok_status: 201
-    stop_attack:
-      url: "http://{{ sdn_ip }}:{{ sdn_port }}/stopAttack"
-      method: POST
-      body: "{{ attack_body }}"
+    attack:
+      url: "http://{{ sdn_ip }}:{{ sdn_port }}/gwAttack"
+      body:
+        src_ip: "{% if scenario_send_ip_version|int == 4 %}{{ sender.ip }}{% else %}{{ sender.ipv6 }}{% endif %}"
+        packet_size: 112
+        attack_type: "UDP Flood"
+        dst_port: "{{ send_port }}"
+        dst_ip: "{% if scenario_send_ip_version|int == 4 %}{{ receiver.ip }}{% else %}{{ receiver.ipv6 }}{% endif %}"
+        src_mac: "{{ sender.mac }}"
       ok_status: 201
diff --git a/playbooks/scenarios/single_switch/data_drop_basic.yml b/playbooks/scenarios/single_switch/data_drop_basic.yml
index 7741024..5273707 100644
--- a/playbooks/scenarios/single_switch/data_drop_basic.yml
+++ b/playbooks/scenarios/single_switch/data_drop_basic.yml
@@ -39,13 +39,13 @@
 - hosts: "{{ rec_host }}"
   gather_facts: no
   tasks:
-    - name: Start mitigation with body {{ start_attack.body }} to {{ start_attack.url }}
+    - name: Start mitigation with body {{ attack.body }} to {{ attack.url }}
       uri:
-        url: "{{ start_attack.url }}"
-        method: "{{ start_attack.method }}"
-        body_format: "{{ start_attack.body_format | default('json') }}"
-        body: "{{ start_attack.body }}"
-        status_code: "{{ start_attack.ok_status | default('200') }}"
+        url: "{{ attack.url }}"
+        method: POST
+        body_format: "{{ attack.body_format | default('json') }}"
+        body: "{{ attack.body }}"
+        status_code: "{{ attack.ok_status | default('200') }}"
 
 # Sending UDP packets expect none to be received
 - import_playbook: ../test_cases/send_receive.yml
@@ -64,13 +64,13 @@
 - hosts: "{{ rec_host }}"
   gather_facts: no
   tasks:
-    - name: Stop mitigation with body {{ stop_attack.body }} to {{ stop_attack.url }}
+    - name: Stop mitigation with body {{ attack.body }} to {{ attack.url }}
       uri:
-        url: "{{ stop_attack.url }}"
-        method: "{{ stop_attack.method }}"
-        body_format: "{{ stop_attack.body_format | default('json') }}"
-        body: "{{ stop_attack.body }}"
-        status_code: "{{ stop_attack.ok_status | default('200') }}"
+        url: "{{ attack.url }}"
+        method: DELETE
+        body_format: "{{ attack.body_format | default('json') }}"
+        body: "{{ attack.body }}"
+        status_code: "{{ attack.ok_status | default('200') }}"
 
 # Sending UDP packets expect all to be received
 - import_playbook: ../test_cases/send_receive.yml
diff --git a/trans_sec/analytics/oinc.py b/trans_sec/analytics/oinc.py
index 0e54264..337cfb7 100644
--- a/trans_sec/analytics/oinc.py
+++ b/trans_sec/analytics/oinc.py
@@ -84,7 +84,7 @@ class PacketAnalytics(object):
         :raises Exception: due to the remote HTTP POST
         """
         logger.info('Start attack - %s', attack_dict)
-        self.sdn_interface.post('attack', attack_dict)
+        self.sdn_interface.post('gwAttack', attack_dict)
 
     @abc.abstractmethod
     def process_packet(self, packet, udp_dport=UDP_INT_DST_PORT):
diff --git a/trans_sec/controller/http_server_flask.py b/trans_sec/controller/http_server_flask.py
index a0fe7de..9e0acf0 100644
--- a/trans_sec/controller/http_server_flask.py
+++ b/trans_sec/controller/http_server_flask.py
@@ -36,17 +36,11 @@ class SDNControllerServer:
     def start(self):
         self.thread.start()
         self.api.add_resource(
-            GwAttack, '/attack',
-            resource_class_kwargs={'sdn_controller': self.sdn_controller})
-        self.api.add_resource(
-            StopGwAttack, '/stopAttack',
+            GwAttack, '/gwAttack',
             resource_class_kwargs={'sdn_controller': self.sdn_controller})
         self.api.add_resource(
             AggAttack, '/aggAttack',
             resource_class_kwargs={'sdn_controller': self.sdn_controller})
-        self.api.add_resource(
-            StopAggAttack, '/stopAggAttack',
-            resource_class_kwargs={'sdn_controller': self.sdn_controller})
         self.api.add_resource(Shutdown, '/shutdown')
 
     def stop(self):
@@ -83,15 +77,7 @@ class GwAttack(Resource):
         self.sdn_controller.add_attacker(args)
         return json.dumps({"success": True}), 201
 
-
-class StopGwAttack(Resource):
-    """
-    Class for exposing web service to issue a stop attack call to gateway.p4
-    """
-    def __init__(self, **kwargs):
-        self.sdn_controller = kwargs['sdn_controller']
-
-    def post(self):
+    def delete(self):
         logger.info('Attacker to remove')
         parser = reqparse.RequestParser()
         parser.add_argument('src_mac', type=str)
@@ -126,15 +112,7 @@ class AggAttack(Resource):
         self.sdn_controller.add_agg_attacker(args)
         return json.dumps({"success": True}), 201
 
-
-class StopAggAttack(Resource):
-    """
-    Class for exposing web service to issue a stop attack acll to aggregate.p4
-    """
-    def __init__(self, **kwargs):
-        self.sdn_controller = kwargs['sdn_controller']
-
-    def post(self):
+    def delete(self):
         logger.info('Attacker to remove')
         parser = reqparse.RequestParser()
         parser.add_argument('src_mac', type=str)


Test patch:
diff --git a/playbooks/scenarios/test_cases/send_receive.yml b/playbooks/scenarios/test_cases/send_receive.yml
index 6787853..3baa889 100644
--- a/playbooks/scenarios/test_cases/send_receive.yml
+++ b/playbooks/scenarios/test_cases/send_receive.yml
@@ -204,7 +204,7 @@
     - name: Call web service
       uri:
         url: "{{ cleanup_rest_call.url }}"
-        method: "{{ cleanup_rest_call.method }}"
+        method: DELETE
         body_format: json
         body: "{{ cleanup_rest_call.body }}"
         status_code: "{{ cleanup_rest_call.ok_status }}"
diff --git a/tests/trans_sec/controller/http_server_flask_tests.py b/tests/trans_sec/controller/http_server_flask_tests.py
index bb75e74..552ca1a 100644
--- a/tests/trans_sec/controller/http_server_flask_tests.py
+++ b/tests/trans_sec/controller/http_server_flask_tests.py
@@ -54,7 +54,7 @@ class HttpSessionTests(unittest.TestCase):
             'packet_size': '12',
             'attack_type': 'test',
         }
-        ret_val = requests.post(url='http://127.0.0.1:9998/attack',
+        ret_val = requests.post(url='http://127.0.0.1:9998/gwAttack',
                                 params=attack)
         self.assertEquals(201, ret_val.status_code)