From 497c7a291b2ad541f298962a6a793b461c661705 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 28 Apr 2023 13:08:10 -0600 Subject: [PATCH 01/36] Add network orchestrator repository --- .gitignore | 1 - net_orc/LICENSE | 201 ++++++ net_orc/README.md | 66 ++ net_orc/cmd/install | 9 + net_orc/cmd/start | 45 ++ net_orc/conf/.gitignore | 1 + net_orc/conf/network/radius/ca.crt | 26 + net_orc/conf/system.json.example | 7 + net_orc/docker-compose.yml | 64 ++ .../devices/faux-dev/bin/get_default_gateway | 3 + .../devices/faux-dev/bin/start_dhcp_client | 16 + .../faux-dev/bin/start_network_service | 39 ++ .../devices/faux-dev/conf/module_config.json | 11 + .../devices/faux-dev/faux-dev.Dockerfile | 20 + .../devices/faux-dev/python/src/dhcp_check.py | 85 +++ .../devices/faux-dev/python/src/dns_check.py | 109 ++++ .../faux-dev/python/src/gateway_check.py | 40 ++ .../devices/faux-dev/python/src/logger.py | 43 ++ .../devices/faux-dev/python/src/ntp_check.py | 79 +++ .../devices/faux-dev/python/src/run.py | 114 ++++ .../devices/faux-dev/python/src/util.py | 28 + net_orc/network/modules/base/base.Dockerfile | 23 + net_orc/network/modules/base/bin/capture | 30 + .../network/modules/base/bin/setup_binaries | 10 + net_orc/network/modules/base/bin/start_grpc | 17 + net_orc/network/modules/base/bin/start_module | 79 +++ .../modules/base/bin/start_network_service | 10 + .../modules/base/bin/wait_for_interface | 21 + .../modules/base/conf/module_config.json | 12 + .../modules/base/python/requirements.txt | 2 + .../base/python/src/grpc/start_server.py | 34 + .../network/modules/base/python/src/logger.py | 47 ++ .../modules/dhcp-1/bin/start_network_service | 77 +++ .../network/modules/dhcp-1/conf/dhcpd.conf | 26 + .../modules/dhcp-1/conf/module_config.json | 25 + .../network/modules/dhcp-1/conf/radvd.conf | 12 + .../network/modules/dhcp-1/dhcp-1.Dockerfile | 14 + .../dhcp-1/python/src/grpc/__init__.py | 0 .../dhcp-1/python/src/grpc/dhcp_config.py | 267 ++++++++ .../dhcp-1/python/src/grpc/network_service.py | 44 ++ .../dhcp-1/python/src/grpc/proto/grpc.proto | 36 ++ .../network/modules/dhcp-1/python/src/run.py | 40 ++ .../modules/dhcp-2/bin/start_network_service | 77 +++ .../network/modules/dhcp-2/conf/dhcpd.conf | 24 + .../modules/dhcp-2/conf/module_config.json | 25 + .../network/modules/dhcp-2/conf/radvd.conf | 12 + .../network/modules/dhcp-2/dhcp-2.Dockerfile | 14 + .../dhcp-2/python/src/grpc/__init__.py | 0 .../dhcp-2/python/src/grpc/dhcp_config.py | 267 ++++++++ .../dhcp-2/python/src/grpc/network_service.py | 44 ++ .../dhcp-2/python/src/grpc/proto/grpc.proto | 36 ++ .../network/modules/dhcp-2/python/src/run.py | 40 ++ .../modules/dns/bin/start_network_service | 48 ++ net_orc/network/modules/dns/conf/dnsmasq.conf | 5 + .../modules/dns/conf/module_config.json | 22 + net_orc/network/modules/dns/dns.Dockerfile | 14 + .../modules/gateway/bin/start_network_service | 30 + .../modules/gateway/conf/module_config.json | 22 + .../modules/gateway/gateway.Dockerfile | 11 + .../modules/ntp/bin/start_network_service | 13 + .../modules/ntp/conf/module_config.json | 22 + net_orc/network/modules/ntp/ntp-server.py | 315 +++++++++ net_orc/network/modules/ntp/ntp.Dockerfile | 13 + .../modules/ntp/python/src/ntp_server.py | 315 +++++++++ .../modules/ovs/bin/start_network_service | 22 + .../modules/ovs/conf/module_config.json | 23 + net_orc/network/modules/ovs/ovs.Dockerfile | 20 + .../modules/ovs/python/requirements.txt | 0 .../network/modules/ovs/python/src/logger.py | 17 + .../modules/ovs/python/src/ovs_control.py | 107 ++++ net_orc/network/modules/ovs/python/src/run.py | 53 ++ .../network/modules/ovs/python/src/util.py | 19 + .../modules/radius/bin/start_network_service | 20 + net_orc/network/modules/radius/conf/ca.crt | 26 + net_orc/network/modules/radius/conf/eap | 602 ++++++++++++++++++ .../modules/radius/conf/module_config.json | 22 + .../modules/radius/python/requirements.txt | 3 + .../radius/python/src/authenticator.py | 31 + .../network/modules/radius/radius.Dockerfile | 26 + .../template/bin/start_network_service | 13 + .../modules/template/conf/module_config.json | 26 + .../template/python/src/template_main.py | 4 + .../modules/template/template.Dockerfile | 11 + net_orc/orchestrator.Dockerfile | 22 + net_orc/python/requirements.txt | 4 + net_orc/python/src/listener.py | 68 ++ net_orc/python/src/logger.py | 27 + net_orc/python/src/network_event.py | 10 + net_orc/python/src/network_orchestrator.py | 573 +++++++++++++++++ net_orc/python/src/network_runner.py | 68 ++ net_orc/python/src/network_validator.py | 274 ++++++++ net_orc/python/src/run_validator.py | 52 ++ net_orc/python/src/util.py | 30 + 93 files changed, 5374 insertions(+), 1 deletion(-) create mode 100644 net_orc/LICENSE create mode 100644 net_orc/README.md create mode 100644 net_orc/cmd/install create mode 100644 net_orc/cmd/start create mode 100644 net_orc/conf/.gitignore create mode 100644 net_orc/conf/network/radius/ca.crt create mode 100644 net_orc/conf/system.json.example create mode 100644 net_orc/docker-compose.yml create mode 100644 net_orc/network/devices/faux-dev/bin/get_default_gateway create mode 100644 net_orc/network/devices/faux-dev/bin/start_dhcp_client create mode 100644 net_orc/network/devices/faux-dev/bin/start_network_service create mode 100644 net_orc/network/devices/faux-dev/conf/module_config.json create mode 100644 net_orc/network/devices/faux-dev/faux-dev.Dockerfile create mode 100644 net_orc/network/devices/faux-dev/python/src/dhcp_check.py create mode 100644 net_orc/network/devices/faux-dev/python/src/dns_check.py create mode 100644 net_orc/network/devices/faux-dev/python/src/gateway_check.py create mode 100644 net_orc/network/devices/faux-dev/python/src/logger.py create mode 100644 net_orc/network/devices/faux-dev/python/src/ntp_check.py create mode 100644 net_orc/network/devices/faux-dev/python/src/run.py create mode 100644 net_orc/network/devices/faux-dev/python/src/util.py create mode 100644 net_orc/network/modules/base/base.Dockerfile create mode 100644 net_orc/network/modules/base/bin/capture create mode 100644 net_orc/network/modules/base/bin/setup_binaries create mode 100644 net_orc/network/modules/base/bin/start_grpc create mode 100644 net_orc/network/modules/base/bin/start_module create mode 100644 net_orc/network/modules/base/bin/start_network_service create mode 100644 net_orc/network/modules/base/bin/wait_for_interface create mode 100644 net_orc/network/modules/base/conf/module_config.json create mode 100644 net_orc/network/modules/base/python/requirements.txt create mode 100644 net_orc/network/modules/base/python/src/grpc/start_server.py create mode 100644 net_orc/network/modules/base/python/src/logger.py create mode 100644 net_orc/network/modules/dhcp-1/bin/start_network_service create mode 100644 net_orc/network/modules/dhcp-1/conf/dhcpd.conf create mode 100644 net_orc/network/modules/dhcp-1/conf/module_config.json create mode 100644 net_orc/network/modules/dhcp-1/conf/radvd.conf create mode 100644 net_orc/network/modules/dhcp-1/dhcp-1.Dockerfile create mode 100644 net_orc/network/modules/dhcp-1/python/src/grpc/__init__.py create mode 100644 net_orc/network/modules/dhcp-1/python/src/grpc/dhcp_config.py create mode 100644 net_orc/network/modules/dhcp-1/python/src/grpc/network_service.py create mode 100644 net_orc/network/modules/dhcp-1/python/src/grpc/proto/grpc.proto create mode 100644 net_orc/network/modules/dhcp-1/python/src/run.py create mode 100644 net_orc/network/modules/dhcp-2/bin/start_network_service create mode 100644 net_orc/network/modules/dhcp-2/conf/dhcpd.conf create mode 100644 net_orc/network/modules/dhcp-2/conf/module_config.json create mode 100644 net_orc/network/modules/dhcp-2/conf/radvd.conf create mode 100644 net_orc/network/modules/dhcp-2/dhcp-2.Dockerfile create mode 100644 net_orc/network/modules/dhcp-2/python/src/grpc/__init__.py create mode 100644 net_orc/network/modules/dhcp-2/python/src/grpc/dhcp_config.py create mode 100644 net_orc/network/modules/dhcp-2/python/src/grpc/network_service.py create mode 100644 net_orc/network/modules/dhcp-2/python/src/grpc/proto/grpc.proto create mode 100644 net_orc/network/modules/dhcp-2/python/src/run.py create mode 100644 net_orc/network/modules/dns/bin/start_network_service create mode 100644 net_orc/network/modules/dns/conf/dnsmasq.conf create mode 100644 net_orc/network/modules/dns/conf/module_config.json create mode 100644 net_orc/network/modules/dns/dns.Dockerfile create mode 100644 net_orc/network/modules/gateway/bin/start_network_service create mode 100644 net_orc/network/modules/gateway/conf/module_config.json create mode 100644 net_orc/network/modules/gateway/gateway.Dockerfile create mode 100644 net_orc/network/modules/ntp/bin/start_network_service create mode 100644 net_orc/network/modules/ntp/conf/module_config.json create mode 100644 net_orc/network/modules/ntp/ntp-server.py create mode 100644 net_orc/network/modules/ntp/ntp.Dockerfile create mode 100644 net_orc/network/modules/ntp/python/src/ntp_server.py create mode 100644 net_orc/network/modules/ovs/bin/start_network_service create mode 100644 net_orc/network/modules/ovs/conf/module_config.json create mode 100644 net_orc/network/modules/ovs/ovs.Dockerfile create mode 100644 net_orc/network/modules/ovs/python/requirements.txt create mode 100644 net_orc/network/modules/ovs/python/src/logger.py create mode 100644 net_orc/network/modules/ovs/python/src/ovs_control.py create mode 100644 net_orc/network/modules/ovs/python/src/run.py create mode 100644 net_orc/network/modules/ovs/python/src/util.py create mode 100644 net_orc/network/modules/radius/bin/start_network_service create mode 100644 net_orc/network/modules/radius/conf/ca.crt create mode 100644 net_orc/network/modules/radius/conf/eap create mode 100644 net_orc/network/modules/radius/conf/module_config.json create mode 100644 net_orc/network/modules/radius/python/requirements.txt create mode 100644 net_orc/network/modules/radius/python/src/authenticator.py create mode 100644 net_orc/network/modules/radius/radius.Dockerfile create mode 100644 net_orc/network/modules/template/bin/start_network_service create mode 100644 net_orc/network/modules/template/conf/module_config.json create mode 100644 net_orc/network/modules/template/python/src/template_main.py create mode 100644 net_orc/network/modules/template/template.Dockerfile create mode 100644 net_orc/orchestrator.Dockerfile create mode 100644 net_orc/python/requirements.txt create mode 100644 net_orc/python/src/listener.py create mode 100644 net_orc/python/src/logger.py create mode 100644 net_orc/python/src/network_event.py create mode 100644 net_orc/python/src/network_orchestrator.py create mode 100644 net_orc/python/src/network_runner.py create mode 100644 net_orc/python/src/network_validator.py create mode 100644 net_orc/python/src/run_validator.py create mode 100644 net_orc/python/src/util.py diff --git a/.gitignore b/.gitignore index f79a6efcb..15aae1278 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ # Runtime folder runtime/ venv/ -net_orc/ .vscode/ # Byte-compiled / optimized / DLL files diff --git a/net_orc/LICENSE b/net_orc/LICENSE new file mode 100644 index 000000000..261eeb9e9 --- /dev/null +++ b/net_orc/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/net_orc/README.md b/net_orc/README.md new file mode 100644 index 000000000..9cb1eec1a --- /dev/null +++ b/net_orc/README.md @@ -0,0 +1,66 @@ +Testrun logo + +## Introduction :wave: +The network orchestrator is a tool to automate the management of a test lab network and provide essential services to begin device testing in just a few minutes. + +## Motivation :bulb: +Test labs may be maintaining a large and complex network using equipment such as: A managed layer 3 switch, an enterprise-grade network router, virtualized or physical servers to provide DNS, NTP, 802.1x etc. With this amount of moving parts, all with dynamic configuration files and constant software updates, more time is likely to be spent on preparation and clean up of functinality or penetration testing - not forgetting the number of software tools required to perform the testing. + +## How it works :triangular_ruler: +The network orchestrator creates an isolated and controlled network environment to fully simulate enterprise network deployments in your device testing lab. +This removes the necessity for complex hardware, advanced knowledge and networking experience whilst enabling semi-technical engineers to validate device +behaviour against industry cyber standards. + +The network orchestrator will provide the network and some tools to assist an engineer performing the additional testing. At the same time, packet captures of the device behaviour will be recorded, alongside logs for each network service, for further debugging. + +## Minimum Requirements :computer: +### Hardware + - PC running Ubuntu LTS (laptop or desktop) + - 2x USB ethernet adapter (One may be built in ethernet) + - Connect one adapter to your router (for internet access) + - Connect one adapter to your device under test + - Internet connection +### Software + - Python 3 with pip3 (Already available on Ubuntu LTS) + - Docker - [Install guide](https://docs.docker.com/engine/install/ubuntu/) + - Open vSwitch ``sudo apt-get install openvswitch-common openvswitch-switch`` + +An additional network interface (even wifi) with internet access can be used to maintain internet connection during use of the network orchestrator. + +## How to use :arrow_forward: +1) Ensure you have a device with the minimum hardware and software requirements setup +2) Clone the project using ```git clone https://github.com/auto-iot/network-orchestrator``` +3) Navigate into the project using ```cd network-orchestrator``` +4) Copy conf/system.json.example to conf/system.json (after setting the correct interfaces in the file) +5) Start the tool using ```sudo cmd/start``` + +## Issue reporting :triangular_flag_on_post: +If the application has come across a problem at any point during setup or use, please raise an issue under the [issues tab](https://github.com/auto-iot/network-orchestrator/issues). Issue templates exist for both bug reports and feature requests. If neither of these are appropriate for your issue, raise a blank issue instead. + +## Roadmap :chart_with_upwards_trend: + - Ability to modify configuration files of each network service during use (via GRPC) + - IPv6 internet routing + +## Contributing :keyboard: +The contributing requirements can be found in [CONTRIBUTING.md](CONTRIBUTING.md). In short, checkout the [Google CLA](https://cla.developers.google.com/) site to get started. + +## FAQ :raising_hand: +1) What services are provided on the virtual network? + + The following are network services that are containerized and accessible to the device under test though are likely to change over time: + - DHCP in failover configuration with internet connectivity + - IPv6 router advertisements + - DNS (and DNS over HTTPS) + - NTPv4 + - 802.1x Port Based Authentication + +2) Can I run the network orchestrator on a virtual machine? + + Probably. Provided that the required 2x USB ethernet adapters are passed to the virtual machine as USB devices rather than network adapters, the tool should + still work. We will look to test and approve the use of virtualisation in the future. + +3) Can I connect multiple devices to the Network Orchestrator? + + In short, Yes you can. The way in which multiple devices could be tested simultaneously is yet to be decided. However, if you simply want to add field/peer devices during runtime (even another laptop performing manual testing) then you may connect the USB ethernet adapter to an unmanaged switch. + +4) Raise an issue with the label 'question' if your question has not been answered in this readme. \ No newline at end of file diff --git a/net_orc/cmd/install b/net_orc/cmd/install new file mode 100644 index 000000000..fe9781cb2 --- /dev/null +++ b/net_orc/cmd/install @@ -0,0 +1,9 @@ +#!/bin/bash -e + +python3 -m venv venv + +source venv/bin/activate + +pip3 install -r python/requirements.txt + +deactivate diff --git a/net_orc/cmd/start b/net_orc/cmd/start new file mode 100644 index 000000000..8369d2c8b --- /dev/null +++ b/net_orc/cmd/start @@ -0,0 +1,45 @@ +#!/bin/bash -e + +if [[ "$EUID" -ne 0 ]]; then + echo "Must run as root. Use sudo cmd/start" + exit 1 +fi + +# Check if python modules exist. Install if not +[ ! -d "venv" ] && cmd/install + +# Ensure that /var/run/netns folder exists +mkdir -p /var/run/netns + +# Clear up existing runtime files +rm -rf runtime + +# Activate Python virtual environment +source venv/bin/activate + +# -u flag allows python print statements +# to be logged by docker by running unbuffered +python3 -u python/src/network_runner.py $@ + +# TODO: Work in progress code for containerization of OVS module +# asyncRun() { +# "$@" & +# pid="$!" +# echo "PID Running: " $pid +# trap "echo 'Stopping PID $pid'; kill -SIGTERM $pid" SIGINT SIGTERM + +# sleep 10 + +# # A signal emitted while waiting will make the wait command return code > 128 +# # Let's wrap it in a loop that doesn't end before the process is indeed stopped +# while kill -0 $pid > /dev/null 2>&1; do +# #while $(kill -0 $pid 2>/dev/null); do +# wait +# done +# } + +# # -u flag allows python print statements +# # to be logged by docker by running unbuffered +# asyncRun python3 -u python/src/run.py $@ + +deactivate \ No newline at end of file diff --git a/net_orc/conf/.gitignore b/net_orc/conf/.gitignore new file mode 100644 index 000000000..41b89ceb1 --- /dev/null +++ b/net_orc/conf/.gitignore @@ -0,0 +1 @@ +system.json \ No newline at end of file diff --git a/net_orc/conf/network/radius/ca.crt b/net_orc/conf/network/radius/ca.crt new file mode 100644 index 000000000..d009cb1ab --- /dev/null +++ b/net_orc/conf/network/radius/ca.crt @@ -0,0 +1,26 @@ +-----BEGIN CERTIFICATE----- +MIIEYTCCA0mgAwIBAgIUQJ4F8hBCnCp7ASPZqG/tNQgoUR4wDQYJKoZIhvcNAQEL +BQAwgb8xCzAJBgNVBAYTAkdCMRswGQYDVQQIDBIbWzN+TGVpY2VzdGVyc2hpcmUx +FTATBgNVBAcMDExvdWdoYm9yb3VnaDEUMBIGA1UECgwLRm9yZXN0IFJvY2sxDjAM +BgNVBAsMBUN5YmVyMR8wHQYDVQQDDBZjeWJlci5mb3Jlc3Ryb2NrLmNvLnVrMTUw +MwYJKoZIhvcNAQkBFiZjeWJlcnNlY3VyaXR5LnRlc3RpbmdAZm9yZXN0cm9jay5j +by51azAeFw0yMjAzMDQxMjEzMTBaFw0yNzAzMDMxMjEzMTBaMIG/MQswCQYDVQQG +EwJHQjEbMBkGA1UECAwSG1szfkxlaWNlc3RlcnNoaXJlMRUwEwYDVQQHDAxMb3Vn +aGJvcm91Z2gxFDASBgNVBAoMC0ZvcmVzdCBSb2NrMQ4wDAYDVQQLDAVDeWJlcjEf +MB0GA1UEAwwWY3liZXIuZm9yZXN0cm9jay5jby51azE1MDMGCSqGSIb3DQEJARYm +Y3liZXJzZWN1cml0eS50ZXN0aW5nQGZvcmVzdHJvY2suY28udWswggEiMA0GCSqG +SIb3DQEBAQUAA4IBDwAwggEKAoIBAQDDNz3vJiZ5nX8lohEhqXvxEme3srip8qF7 +r5ScIeQzsTKuPNAmoefx9TcU3SyA2BnREuDX+OCYMN62xxWG2PndOl0LNezAY22C +PJwHbaBntLKY/ZhxYSTyratM7zxKSVLtClamA/bJXBhdfZZKYOP3xlZQEQTygtzK +j5hZwDrpDARtjRZIMWPLqVcoaW9ow2urJVsdD4lYAhpQU2UIgiWo7BG3hJsUfcYX +EQyyrMKJ7xaCwzIU7Sem1PETrzeiWg4KhDijc7A0RMPWlU5ljf0CnY/IZwiDsMRl +hGmGBPvR+ddiWPZPtSKj6TPWpsaMUR9UwncLmSSrhf1otX4Mw0vbAgMBAAGjUzBR +MB0GA1UdDgQWBBR0Qxx2mDTPIfpnzO5YtycGs6t8ijAfBgNVHSMEGDAWgBR0Qxx2 +mDTPIfpnzO5YtycGs6t8ijAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUA +A4IBAQCpTMBMZGXF74WCxrIk23MUsu0OKzMs8B16Wy8BHz+7hInLZwbkx71Z0TP5 +rsMITetSANtM/k4jH7Vmr1xmzU7oSz5zKU1+7rIjKjGtih48WZdJay0uqfKe0K2s +vsRS0LVLY6IiTFWK9YrLC0QFSK7z5GDl1oc/D5yIZAkbsL6PRQJ5RQsYf5BhHfyB +PRV/KcF7c9iKVYW2vILJzbyYLHTDADTHbtfCe5+pAGxagswDjSMVkQu5iJNjbtUO +5iv7PRkgzUFru9Kk6q+LrXbzyPPCwlc3Xbh1q5jSkJLkcV3K26E7+uX5HI+Hxpeh +a8kOsdnw+N8wX6bc7eXIaGBDMine +-----END CERTIFICATE----- diff --git a/net_orc/conf/system.json.example b/net_orc/conf/system.json.example new file mode 100644 index 000000000..77c981394 --- /dev/null +++ b/net_orc/conf/system.json.example @@ -0,0 +1,7 @@ +{ + "network": { + "device_intf": "enx207bd2620617", + "internet_intf": "enx207bd26205e9" + }, + "log_level": "INFO" +} \ No newline at end of file diff --git a/net_orc/docker-compose.yml b/net_orc/docker-compose.yml new file mode 100644 index 000000000..8c50d766a --- /dev/null +++ b/net_orc/docker-compose.yml @@ -0,0 +1,64 @@ +version: "3.7" + +services: + + base: + build: + context: network/modules/base + dockerfile: base.Dockerfile + image: test-run/base + container_name: tr-ct-base + + ovs: + depends_on: + - base + build: + context: network/modules/ovs + dockerfile: ovs.Dockerfile + image: test-run/ovs + network_mode: host + container_name: tr-ct-ovs + stdin_open: true + privileged: true + volumes: + - $PWD/network/modules/ovs/python:/ovs/python + # Mount host open vswitch socket to allow container + # access to control open vswitch on the host + - /var/run/openvswitch/db.sock:/var/run/openvswitch/db.sock + # Mount host network namespace to allow container + # access to assign proper namespaces to containers + - /var/run/netns:/var/run/netns + + netorch: + depends_on: + - base + build: + context: . + dockerfile: orchestrator.Dockerfile + image: test-run/orchestrator + network_mode: host + privileged: true + volumes: + - $PWD/cmd:/orchestrator/cmd + - $PWD/network:/orchestrator/network + - $PWD/python:/orchestrator/python + # Mount host docker socket to allow container access + # control docker containers on the host + - /var/run/docker.sock:/var/run/docker.sock + # Mount host open vswitch socket to allow container + # access to control open vswitch on the host + - /var/run/openvswitch/db.sock:/var/run/openvswitch/db.sock + # Mount host network namespace to allow container + # access to assign proper namespaces to containers + - /var/run/netns:/var/run/netns + # Mount the host process information to allow container + # access to configure docker containers and namespaces properly + - /proc:/proc + container_name: network_orchestrator + stdin_open: true + working_dir: /orchestrator + #entrypoint: ["cmd/start"] + # Give more time for stopping so when we stop the container it has + # time to stop all network services gracefuly + stop_grace_period: 60s + entrypoint: ["python3","-u","python/src/run.py"] diff --git a/net_orc/network/devices/faux-dev/bin/get_default_gateway b/net_orc/network/devices/faux-dev/bin/get_default_gateway new file mode 100644 index 000000000..f6f1e2a0d --- /dev/null +++ b/net_orc/network/devices/faux-dev/bin/get_default_gateway @@ -0,0 +1,3 @@ +#!/bin/bash -e + +route | grep default | awk '{print $2}' \ No newline at end of file diff --git a/net_orc/network/devices/faux-dev/bin/start_dhcp_client b/net_orc/network/devices/faux-dev/bin/start_dhcp_client new file mode 100644 index 000000000..de9270c82 --- /dev/null +++ b/net_orc/network/devices/faux-dev/bin/start_dhcp_client @@ -0,0 +1,16 @@ +#!/bin/bash -e + +# Fetch the interface +INTF=$1 + +PID_FILE=/var/run/dhclient.pid + +echo "Starting DHCP Client on interface $INTF" + +#Kill any existing running dhclient process +if [ -f $PID_FILE ]; then + kill -9 $(cat $PID_FILE) || true + rm -f $PID_FILE +fi + +dhclient $INTF \ No newline at end of file diff --git a/net_orc/network/devices/faux-dev/bin/start_network_service b/net_orc/network/devices/faux-dev/bin/start_network_service new file mode 100644 index 000000000..b727d2091 --- /dev/null +++ b/net_orc/network/devices/faux-dev/bin/start_network_service @@ -0,0 +1,39 @@ +#!/bin/bash -e + +# Directory where all binaries will be loaded +BIN_DIR="/testrun/bin" + +# Fetch module name +MODULE_NAME=$1 + +# Default interface should be veth0 for all containers +DEFAULT_IFACE=veth0 + +# Allow a user to define an interface by passing it into this script +DEFINED_IFACE=$2 + +# Select which interace to use +if [[ -z $DEFINED_IFACE || "$DEFINED_IFACE" == "null" ]] +then + echo "No interface defined, defaulting to veth0" + INTF=$DEFAULT_IFACE +else + INTF=$DEFINED_IFACE +fi + +#Create and set permissions on the output files +LOG_FILE=/runtime/validation/$MODULE_NAME.log +RESULT_FILE=/runtime/validation/result.json +touch $LOG_FILE +touch $RESULT_FILE +chown $HOST_USER:$HOST_USER $LOG_FILE +chown $HOST_USER:$HOST_USER $RESULT_FILE + +# Start dhclient +$BIN_DIR/start_dhcp_client $INTF + +# -u flag allows python print statements +# to be logged by docker by running unbuffered +exec python3 -u /testrun/python/src/run.py "-m $MODULE_NAME" + +echo Network validator complete \ No newline at end of file diff --git a/net_orc/network/devices/faux-dev/conf/module_config.json b/net_orc/network/devices/faux-dev/conf/module_config.json new file mode 100644 index 000000000..afde8c629 --- /dev/null +++ b/net_orc/network/devices/faux-dev/conf/module_config.json @@ -0,0 +1,11 @@ +{ + "config": { + "meta": { + "name": "faux-dev", + "description": "Faux device to test network modules are functioning properly" + }, + "docker": { + "timeout": 60 + } + } +} \ No newline at end of file diff --git a/net_orc/network/devices/faux-dev/faux-dev.Dockerfile b/net_orc/network/devices/faux-dev/faux-dev.Dockerfile new file mode 100644 index 000000000..1686341b5 --- /dev/null +++ b/net_orc/network/devices/faux-dev/faux-dev.Dockerfile @@ -0,0 +1,20 @@ +# Image name: test-run/faux-dev +FROM test-run/base:latest + +#Update and get all additional requirements not contained in the base image +RUN apt-get update --fix-missing + +# NTP requireds interactive installation so we're going to turn that off +ARG DEBIAN_FRONTEND=noninteractive + +# Install dhcp client and ntp client +RUN apt-get install -y isc-dhcp-client ntp ntpdate + +# Copy over all configuration files +COPY network/devices/faux-dev/conf /testrun/conf + +# Load device binary files +COPY network/devices/faux-dev/bin /testrun/bin + +# Copy over all python files +COPY network/devices/faux-dev/python /testrun/python \ No newline at end of file diff --git a/net_orc/network/devices/faux-dev/python/src/dhcp_check.py b/net_orc/network/devices/faux-dev/python/src/dhcp_check.py new file mode 100644 index 000000000..ab7defc39 --- /dev/null +++ b/net_orc/network/devices/faux-dev/python/src/dhcp_check.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 + +import time +import logger + +LOGGER = None +LOG_NAME = "dhcp_validator" +DHCP_LEASE_FILE = "/var/lib/dhcp/dhclient.leases" +IP_ADDRESS_KEY = "fixed-address" +DNS_OPTION_KEY = "option domain-name-servers" +GATEWAY_OPTION_KEY = "option routers" +NTP_OPTION_KEY = "option ntp-servers" + + +class DHCPValidator: + def __init__(self, module): + self._dhcp_lease = None + self.dhcp_lease_test = False + self.add_logger(module) + + def add_logger(self, module): + global LOGGER + LOGGER = logger.get_logger(LOG_NAME, module) + + def print_test_results(self): + self.print_test_result("DHCP lease test", self.dhcp_lease_test) + + def print_test_result(self, test_name, result): + LOGGER.info(test_name + ": Pass" if result else test_name + ": Fail") + + def get_dhcp_lease(self): + """Returns the current DHCP lease.""" + return self._dhcp_lease + + def validate(self): + self._resolve_dhcp_lease() + LOGGER.info("IP Addr: " + self._dhcp_lease.ip_addr) + LOGGER.info("Gateway: " + self._dhcp_lease.gateway) + LOGGER.info("DNS Server: " + self._dhcp_lease.dns_server) + LOGGER.info("NTP Server: " + self._dhcp_lease.ntp_server) + + def _resolve_dhcp_lease(self): + LOGGER.info("Resolving DHCP lease...") + while self._dhcp_lease is None: + time.sleep(5) + try: + lease_file = open(DHCP_LEASE_FILE) + lines = lease_file.read() + LOGGER.debug("Lease file:\n" + lines) + leases = lines.split("lease ") + # Last lease is the current lease + cur_lease = leases[-1] + if cur_lease is not None: + LOGGER.debug("Current lease: " + cur_lease) + self._dhcp_lease = DHCPLease() + self.dhcp_lease_test = True + # Iterate over entire lease and pick the parts we care about + lease_parts = cur_lease.split("\n") + for part in lease_parts: + part_clean = part.strip() + if part_clean.startswith(IP_ADDRESS_KEY): + self._dhcp_lease.ip_addr = part_clean[len( + IP_ADDRESS_KEY):-1].strip() + elif part_clean.startswith(DNS_OPTION_KEY): + self._dhcp_lease.dns_server = part_clean[len( + DNS_OPTION_KEY):-1].strip() + elif part_clean.startswith(GATEWAY_OPTION_KEY): + self._dhcp_lease.gateway = part_clean[len( + GATEWAY_OPTION_KEY):-1].strip() + elif part_clean.startswith(NTP_OPTION_KEY): + self._dhcp_lease.ntp_server = part_clean[len( + NTP_OPTION_KEY):-1].strip() + except Exception: + LOGGER.error("DHCP Resolved Error") + LOGGER.info("DHCP lease resolved") + + +class DHCPLease: + """Stores information about a device's DHCP lease.""" + + def __init__(self): + self.ip_addr = None + self.gateway = None + self.dns_server = None + self.ntp_server = None diff --git a/net_orc/network/devices/faux-dev/python/src/dns_check.py b/net_orc/network/devices/faux-dev/python/src/dns_check.py new file mode 100644 index 000000000..d3d709d6e --- /dev/null +++ b/net_orc/network/devices/faux-dev/python/src/dns_check.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 + +import logger +import time +import util +import subprocess + +from dhcp_check import DHCPLease + +LOGGER = None +LOG_NAME = "dns_validator" +HOST_PING = "google.com" +CAPTURE_FILE = "/runtime/network/faux-dev.pcap" +DNS_CONFIG_FILE = "/etc/resolv.conf" + + +class DNSValidator: + + def __init__(self, module): + self._dns_server = None + self._dns_resolution_test = False + self._dns_dhcp_server_test = False + self.add_logger(module) + + def add_logger(self, module): + global LOGGER + LOGGER = logger.get_logger(LOG_NAME, module) + + def print_test_results(self): + self.print_test_result( + "DNS resolution test", self._dns_resolution_test) + self.print_test_result( + "DNS DHCP server test", self._dns_dhcp_server_test) + + def print_test_result(self, test_name, result): + LOGGER.info(test_name + ": Pass" if result else test_name + ": Fail") + + def validate(self, dhcp_lease): + self._dns_server = dhcp_lease.dns_server + self._set_dns_server() + self._check_dns_traffic() + + def _check_dns_traffic(self): + LOGGER.info("Checking DNS traffic for DNS server: " + self._dns_server) + + # Ping a host to generate DNS traffic + if self._ping(HOST_PING)[0]: + LOGGER.info("Ping success") + self._dns_resolution_test = True + else: + LOGGER.info("Ping failed") + + # Some delay between pings and DNS traffic in the capture file + # so give some delay before we try to query again + time.sleep(5) + + # Check if the device has sent any DNS requests + filter_to_dns = 'dst port 53 and dst host {}'.format( + self._dns_server) + to_dns = self._exec_tcpdump(filter_to_dns) + num_query_dns = len(to_dns) + LOGGER.info("DNS queries found: " + str(num_query_dns)) + dns_traffic_detected = len(to_dns) > 0 + if dns_traffic_detected: + LOGGER.info("DNS traffic detected to configured DHCP DNS server") + self._dns_dhcp_server_test = True + else: + LOGGER.error("No DNS traffic detected") + + # Docker containeres resolve DNS servers from the host + # and do not play nice with normal networking methods + # so we need to set our DNS servers manually + def _set_dns_server(self): + f = open(DNS_CONFIG_FILE, "w", encoding="utf-8") + f.write("nameserver " + self._dns_server) + f.close() + + # Generate DNS traffic by doing a simple ping by hostname + def _ping(self, host): + cmd = "ping -c 5 " + host + success = util.run_command(cmd, LOGGER) + return success + + def _exec_tcpdump(self, tcpdump_filter): + """ + Args + tcpdump_filter: Filter to pass onto tcpdump file + capture_file: Optional capture file to look + Returns + List of packets matching the filter + """ + command = 'tcpdump -tttt -n -r {} {}'.format( + CAPTURE_FILE, tcpdump_filter) + + LOGGER.debug("tcpdump command: " + command) + + process = subprocess.Popen(command, + universal_newlines=True, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + text = str(process.stdout.read()).rstrip() + + LOGGER.debug("tcpdump response: " + text) + + if text: + return text.split("\n") + + return [] \ No newline at end of file diff --git a/net_orc/network/devices/faux-dev/python/src/gateway_check.py b/net_orc/network/devices/faux-dev/python/src/gateway_check.py new file mode 100644 index 000000000..17457874a --- /dev/null +++ b/net_orc/network/devices/faux-dev/python/src/gateway_check.py @@ -0,0 +1,40 @@ +import logger +import util + +from dhcp_check import DHCPLease + +LOGGER = None +LOG_NAME = "gateway_validator" + + +class GatewayValidator: + + def __init__(self, module): + self._gateway = None + self._default_gateway_test = False + self.add_logger(module) + + def add_logger(self, module): + global LOGGER + LOGGER = logger.get_logger(LOG_NAME, module) + + def print_test_results(self): + self.print_test_result("Default gateway test", + self._default_gateway_test) + + def print_test_result(self, test_name, result): + LOGGER.info(test_name + ": Pass" if result else test_name + ": Fail") + + + def validate(self, dhcp_lease): + self._gateway = dhcp_lease.gateway + self.check_default_gateway() + + def check_default_gateway(self): + LOGGER.info( + "Checking default gateway matches DHCP gateway: " + self._gateway) + cmd = "/testrun/bin/get_default_gateway" + success, default_gateway, stderr = util.run_command(cmd, LOGGER) + LOGGER.info("Default gateway resolved: " + default_gateway) + if default_gateway == self._gateway: + self._default_gateway_test = True \ No newline at end of file diff --git a/net_orc/network/devices/faux-dev/python/src/logger.py b/net_orc/network/devices/faux-dev/python/src/logger.py new file mode 100644 index 000000000..bf692c85e --- /dev/null +++ b/net_orc/network/devices/faux-dev/python/src/logger.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +import json +import logging +import os + +LOGGERS = {} +_LOG_FORMAT = "%(asctime)s %(name)-8s %(levelname)-7s %(message)s" +_DATE_FORMAT = '%b %02d %H:%M:%S' +_CONF_DIR = "conf" +_CONF_FILE_NAME = "system.json" +_LOG_DIR = "/runtime/validation" + +# Set log level +with open(os.path.join(_CONF_DIR, _CONF_FILE_NAME), encoding='utf-8') as conf_file: + system_conf_json = json.load(conf_file) + +log_level_str = system_conf_json['log_level'] +log_level = logging.getLevelName(log_level_str) + +log_format = logging.Formatter(fmt=_LOG_FORMAT, datefmt=_DATE_FORMAT) + +def add_file_handler(log, log_file): + """Add file handler to existing log.""" + handler = logging.FileHandler(os.path.join(_LOG_DIR, log_file + ".log")) + handler.setFormatter(log_format) + log.addHandler(handler) + +def add_stream_handler(log): + """Add stream handler to existing log.""" + handler = logging.StreamHandler() + handler.setFormatter(log_format) + log.addHandler(handler) + +def get_logger(name, log_file=None): + """Return logger for requesting class.""" + if name not in LOGGERS: + LOGGERS[name] = logging.getLogger(name) + LOGGERS[name].setLevel(log_level) + add_stream_handler(LOGGERS[name]) + if log_file is not None: + add_file_handler(LOGGERS[name], log_file) + return LOGGERS[name] diff --git a/net_orc/network/devices/faux-dev/python/src/ntp_check.py b/net_orc/network/devices/faux-dev/python/src/ntp_check.py new file mode 100644 index 000000000..a50bf337e --- /dev/null +++ b/net_orc/network/devices/faux-dev/python/src/ntp_check.py @@ -0,0 +1,79 @@ +import time +import logger +import util + +LOGGER = None +LOG_NAME = "ntp_validator" +ATTEMPTS = 3 + + +class NTPValidator: + """Perform testing of the NTP server.""" + + def __init__(self, module): + self._ntp_server = None + self._ntp_sync_test = False + self.add_logger(module) + + def add_logger(self, module): + global LOGGER + LOGGER = logger.get_logger(LOG_NAME, module) + + def print_test_results(self): + """Print all test results to log.""" + self.print_test_result("NTP sync test", + self._ntp_sync_test) + + def print_test_result(self, test_name, result): + """Output test result to log.""" + LOGGER.info(test_name + ": Pass" if result else test_name + ": Fail") + + def validate(self, dhcp_lease): + """Call NTP sync test.""" + self._ntp_server = dhcp_lease.ntp_server + self.check_ntp() + + def check_ntp(self): + """Perform NTP sync test.""" + if self._ntp_server is not None: + attempt = 0 + LOGGER.info(f"Attempting to sync to NTP server: {self._ntp_server}") + LOGGER.info("Attempts allowed: " + str(ATTEMPTS)) + + # If we don't ping before syncing, this will fail. + while attempt < ATTEMPTS and not self._ntp_sync_test: + attempt += 1 + if self.ping_ntp_server(): + self.sync_ntp() + if not self._ntp_sync_test: + LOGGER.info("Waiting 5 seconds before next attempt") + time.sleep(5) + else: + LOGGER.info("No NTP server available from DHCP lease") + + def sync_ntp(self): + """Send NTP request to server.""" + LOGGER.info("Sending NTP Sync Request to: " + self._ntp_server) + cmd = "ntpdate " + self._ntp_server + ntp_response = util.run_command(cmd, LOGGER)[1] + LOGGER.info("NTP sync response: " + ntp_response) + if "adjust time server " + self._ntp_server in ntp_response: + LOGGER.info("NTP sync succesful") + self._ntp_sync_test = True + else: + LOGGER.info("NTP client failed to sync to server") + + def ping_ntp_server(self): + """Ping NTP server before sending a time request.""" + LOGGER.info("Pinging NTP server before syncing...") + if self.ping(self._ntp_server): + LOGGER.info("NTP server successfully pinged") + return True + LOGGER.info("NTP server did not respond to ping") + return False + + def ping(self, host): + """Send ping request to host.""" + cmd = "ping -c 1 " + host + success = util.run_command(cmd, LOGGER) + return success diff --git a/net_orc/network/devices/faux-dev/python/src/run.py b/net_orc/network/devices/faux-dev/python/src/run.py new file mode 100644 index 000000000..5891b8c4b --- /dev/null +++ b/net_orc/network/devices/faux-dev/python/src/run.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python3 + +import argparse +import json +import os +import signal +import sys + +import logger +from dns_check import DNSValidator +from dhcp_check import DHCPValidator +from gateway_check import GatewayValidator +from ntp_check import NTPValidator + +RESULTS_DIR = '/runtime/validation/' +LOGGER = logger.get_logger('validator') + +class FauxDevice: + """Represents a virtual testing device.""" + + def __init__(self, module): + + signal.signal(signal.SIGINT, self._handler) + signal.signal(signal.SIGTERM, self._handler) + signal.signal(signal.SIGABRT, self._handler) + signal.signal(signal.SIGQUIT, self._handler) + + self.dhcp_validator = DHCPValidator(module) + self.dns_validator = DNSValidator(module) + self.gateway_validator = GatewayValidator(module) + self.ntp_validator = NTPValidator(module) + + self._module = module + self.run_tests() + results = self.generate_results() + self.write_results(results) + + def run_tests(self): + """Execute configured network tests.""" + + # Run DHCP tests first since everything hinges on basic DHCP compliance first + self.dhcp_validator.validate() + + dhcp_lease = self.dhcp_validator.get_dhcp_lease() + + # Use current lease from dhcp tests to validate DNS behaviors + self.dns_validator.validate(dhcp_lease) + + # Use current lease from dhcp tests to validate default gateway + self.gateway_validator.validate(dhcp_lease) + + # Use current lease from dhcp tests to validate ntp server + self.ntp_validator.validate(dhcp_lease) + + def print_test_results(self): + """Print test results to log.""" + self.dhcp_validator.print_test_results() + self.dns_validator.print_test_results() + self.gateway_validator.print_test_results() + self.ntp_validator.print_test_results() + + def generate_results(self): + """Transform test results into JSON format.""" + + results = [] + results.append(self.generate_result("dhcp_lease", self.dhcp_validator.dhcp_lease_test)) + results.append(self.generate_result("dns_from_dhcp", self.dns_validator._dns_dhcp_server_test)) + results.append(self.generate_result("dns_resolution", self.dns_validator._dns_resolution_test)) + results.append(self.generate_result("gateway_default", self.gateway_validator._default_gateway_test)) + results.append(self.generate_result("ntp_sync", self.ntp_validator._ntp_sync_test)) + json_results = json.dumps({"results":results}, indent=2) + + return json_results + + def write_results(self, results): + """Write test results to file.""" + results_file = os.path.join(RESULTS_DIR, "result.json") + LOGGER.info("Writing results to " + results_file) + f = open(results_file, "w", encoding="utf-8") + f.write(results) + f.close() + + def generate_result(self, test_name, test_result): + """Return JSON object for test result.""" + if test_result is not None: + result = "compliant" if test_result else "non-compliant" + else: + result = "skipped" + LOGGER.info(test_name + ": " + result) + res_dict = { + "name": test_name, + "result": result + } + return res_dict + + def _handler(self, signum, frame): # pylint: disable=unused-argument + if signum in (2, signal.SIGTERM): + sys.exit(1) + +def run(argv): # pylint: disable=unused-argument + """Run the network validator.""" + parser = argparse.ArgumentParser(description="Faux Device _validator", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument("-m","--module", + help="Define the module name to be used to create the log file") + + args = parser.parse_args() + + # For some reason passing in the args from bash adds an extra + # space before the argument so we'll just strip out extra space + FauxDevice(args.module.strip()) + +if __name__ == "__main__": + run(sys.argv) diff --git a/net_orc/network/devices/faux-dev/python/src/util.py b/net_orc/network/devices/faux-dev/python/src/util.py new file mode 100644 index 000000000..605af1132 --- /dev/null +++ b/net_orc/network/devices/faux-dev/python/src/util.py @@ -0,0 +1,28 @@ +import subprocess +import shlex + +# Runs a process at the os level +# By default, returns the standard output and error output +# If the caller sets optional output parameter to False, +# will only return a boolean result indicating if it was +# succesful in running the command. Failure is indicated +# by any return code from the process other than zero. + + +def run_command(cmd, logger, output=True): + success = False + process = subprocess.Popen(shlex.split( + cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + + if process.returncode != 0: + err_msg = "%s. Code: %s" % (stderr.strip(), process.returncode) + logger.error("Command Failed: " + cmd) + logger.error("Error: " + err_msg) + else: + success = True + + if output: + return success, stdout.strip().decode('utf-8'), stderr + else: + return success, None, stderr diff --git a/net_orc/network/modules/base/base.Dockerfile b/net_orc/network/modules/base/base.Dockerfile new file mode 100644 index 000000000..2400fd1c6 --- /dev/null +++ b/net_orc/network/modules/base/base.Dockerfile @@ -0,0 +1,23 @@ +# Image name: test-run/base +FROM ubuntu:jammy + +# Install common software +RUN apt-get update && apt-get install -y net-tools iputils-ping tcpdump iproute2 jq python3 python3-pip dos2unix + +#Setup the base python requirements +COPY network/modules/base/python /testrun/python + +# Install all python requirements for the module +RUN pip3 install -r /testrun/python/requirements.txt + +# Add the bin files +COPY network/modules/base/bin /testrun/bin + +# Remove incorrect line endings +RUN dos2unix /testrun/bin/* + +# Make sure all the bin files are executable +RUN chmod u+x /testrun/bin/* + +#Start the network module +ENTRYPOINT [ "/testrun/bin/start_module" ] \ No newline at end of file diff --git a/net_orc/network/modules/base/bin/capture b/net_orc/network/modules/base/bin/capture new file mode 100644 index 000000000..8a8430feb --- /dev/null +++ b/net_orc/network/modules/base/bin/capture @@ -0,0 +1,30 @@ +#!/bin/bash -e + +# Fetch module name +MODULE_NAME=$1 + +# Define the local file location for the capture to be saved +PCAP_DIR="/runtime/network/" +PCAP_FILE=$MODULE_NAME.pcap + +# Default interface should be veth0 for all containers +DEFAULT_IFACE=veth0 + +# Allow a user to define an interface by passing it into this script +DEFINED_IFACE=$2 + +# Select which interace to use +if [[ -z $DEFINED_IFACE ]] +then + INTERFACE=$DEFAULT_IFACE +else + INTERFACE=$DEFINED_IFACE +fi + +# Create the output directory and start the capture +mkdir -p $PCAP_DIR +chown $HOST_USER:$HOST_USER $PCAP_DIR +tcpdump -i $INTERFACE -w $PCAP_DIR/$PCAP_FILE -Z $HOST_USER & + +#Small pause to let the capture to start +sleep 1 \ No newline at end of file diff --git a/net_orc/network/modules/base/bin/setup_binaries b/net_orc/network/modules/base/bin/setup_binaries new file mode 100644 index 000000000..3535ead3c --- /dev/null +++ b/net_orc/network/modules/base/bin/setup_binaries @@ -0,0 +1,10 @@ +#!/bin/bash -e + +# Directory where all binaries will be loaded +BIN_DIR=$1 + +# Remove incorrect line endings +dos2unix $BIN_DIR/* + +# Make sure all the bin files are executable +chmod u+x $BIN_DIR/* \ No newline at end of file diff --git a/net_orc/network/modules/base/bin/start_grpc b/net_orc/network/modules/base/bin/start_grpc new file mode 100644 index 000000000..9792b4bd4 --- /dev/null +++ b/net_orc/network/modules/base/bin/start_grpc @@ -0,0 +1,17 @@ +#!/bin/bash -e + +GRPC_DIR="/testrun/python/src/grpc" +GRPC_PROTO_DIR="proto" +GRPC_PROTO_FILE="grpc.proto" + +#Move into the grpc directory +pushd $GRPC_DIR >/dev/null 2>&1 + +#Build the grpc proto file every time before starting server +python3 -m grpc_tools.protoc --proto_path=. ./$GRPC_PROTO_DIR/$GRPC_PROTO_FILE --python_out=. --grpc_python_out=. + +popd >/dev/null 2>&1 + +#Start the grpc server +python3 -u $GRPC_DIR/start_server.py $@ + diff --git a/net_orc/network/modules/base/bin/start_module b/net_orc/network/modules/base/bin/start_module new file mode 100644 index 000000000..7fdcbc404 --- /dev/null +++ b/net_orc/network/modules/base/bin/start_module @@ -0,0 +1,79 @@ +#!/bin/bash + +# Directory where all binaries will be loaded +BIN_DIR="/testrun/bin" + +# Default interface should be veth0 for all containers +DEFAULT_IFACE=veth0 + +# Create a local user that matches the same as the host +# to be used for correct file ownership for various logs +# HOST_USER mapped in via docker container environemnt variables +useradd $HOST_USER + +# Enable IPv6 for all containers +sysctl net.ipv6.conf.all.disable_ipv6=0 +sysctl -p + +#Read in the config file +CONF_FILE="/testrun/conf/module_config.json" +CONF=`cat $CONF_FILE` + +if [[ -z $CONF ]] +then + echo "No config file present at $CONF_FILE. Exiting startup." + exit 1 +fi + +# Extract the necessary config parameters +MODULE_NAME=$(echo "$CONF" | jq -r '.config.meta.name') +DEFINED_IFACE=$(echo "$CONF" | jq -r '.config.network.interface') +GRPC=$(echo "$CONF" | jq -r '.config.grpc') + +# Validate the module name is present +if [[ -z "$MODULE_NAME" || "$MODULE_NAME" == "null" ]] +then + echo "No module name present in $CONF_FILE. Exiting startup." + exit 1 +fi + +# Select which interace to use +if [[ -z $DEFINED_IFACE || "$DEFINED_IFACE" == "null" ]] +then + echo "No Interface Defined, defaulting to veth0" + INTF=$DEFAULT_IFACE +else + INTF=$DEFINED_IFACE +fi + +echo "Starting module $MODULE_NAME on local interface $INTF..." + +$BIN_DIR/setup_binaries $BIN_DIR + +# Wait for interface to become ready +$BIN_DIR/wait_for_interface $INTF + +# Small pause to let the interface stabalize before starting the capture +#sleep 1 + +# Start network capture +$BIN_DIR/capture $MODULE_NAME $INTF + +# Start the grpc server +if [[ ! -z $GRPC && ! $GRPC == "null" ]] +then + GRPC_PORT=$(echo "$GRPC" | jq -r '.port') + if [[ ! -z $GRPC_PORT && ! $GRPC_PORT == "null" ]] + then + echo "gRPC port resolved from config: $GRPC_PORT" + $BIN_DIR/start_grpc "-p $GRPC_PORT" & + else + $BIN_DIR/start_grpc & + fi +fi + +#Small pause to let all core services stabalize +sleep 3 + +#Start the networking service +$BIN_DIR/start_network_service $MODULE_NAME $INTF \ No newline at end of file diff --git a/net_orc/network/modules/base/bin/start_network_service b/net_orc/network/modules/base/bin/start_network_service new file mode 100644 index 000000000..7d13750b8 --- /dev/null +++ b/net_orc/network/modules/base/bin/start_network_service @@ -0,0 +1,10 @@ +#!/bin/bash + +# Place holder function for testing and validation +# Each network module should include a start_networkig_service +# file that overwrites this one to boot all of the its specific +# requirements to run. + +echo "Starting network service..." +echo "This is not a real network service, just a test" +echo "Network service started" \ No newline at end of file diff --git a/net_orc/network/modules/base/bin/wait_for_interface b/net_orc/network/modules/base/bin/wait_for_interface new file mode 100644 index 000000000..1377705d8 --- /dev/null +++ b/net_orc/network/modules/base/bin/wait_for_interface @@ -0,0 +1,21 @@ +#!/bin/bash + +# Default interface should be veth0 for all containers +DEFAULT_IFACE=veth0 + +# Allow a user to define an interface by passing it into this script +DEFINED_IFACE=$1 + +# Select which interace to use +if [[ -z $DEFINED_IFACE ]] +then + INTF=$DEFAULT_IFACE +else + INTF=$DEFINED_IFACE +fi + +# Wait for local interface to be ready +while ! ip link show $INTF; do + echo $INTF is not yet ready. Waiting 3 seconds + sleep 3 +done \ No newline at end of file diff --git a/net_orc/network/modules/base/conf/module_config.json b/net_orc/network/modules/base/conf/module_config.json new file mode 100644 index 000000000..1f3a47ba2 --- /dev/null +++ b/net_orc/network/modules/base/conf/module_config.json @@ -0,0 +1,12 @@ +{ + "config": { + "meta": { + "name": "base", + "display_name": "Base", + "description": "Base image" + }, + "docker": { + "enable_container": false + } + } +} \ No newline at end of file diff --git a/net_orc/network/modules/base/python/requirements.txt b/net_orc/network/modules/base/python/requirements.txt new file mode 100644 index 000000000..9c4e2b056 --- /dev/null +++ b/net_orc/network/modules/base/python/requirements.txt @@ -0,0 +1,2 @@ +grpcio +grpcio-tools \ No newline at end of file diff --git a/net_orc/network/modules/base/python/src/grpc/start_server.py b/net_orc/network/modules/base/python/src/grpc/start_server.py new file mode 100644 index 000000000..9ed31ffcf --- /dev/null +++ b/net_orc/network/modules/base/python/src/grpc/start_server.py @@ -0,0 +1,34 @@ +from concurrent import futures +import grpc +import proto.grpc_pb2_grpc as pb2_grpc +import proto.grpc_pb2 as pb2 +from network_service import NetworkService +import logging +import sys +import argparse + +DEFAULT_PORT = '5001' + +def serve(PORT): + server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) + pb2_grpc.add_NetworkModuleServicer_to_server(NetworkService(), server) + server.add_insecure_port('[::]:' + PORT) + server.start() + server.wait_for_termination() + +def run(argv): + parser = argparse.ArgumentParser(description="GRPC Server for Network Module", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument("-p", "--port", default=DEFAULT_PORT, + help="Define the default port to run the server on.") + + args = parser.parse_args() + + PORT = args.port + + print("gRPC server starting on port " + PORT) + serve(PORT) + + +if __name__ == "__main__": + run(sys.argv) \ No newline at end of file diff --git a/net_orc/network/modules/base/python/src/logger.py b/net_orc/network/modules/base/python/src/logger.py new file mode 100644 index 000000000..4924512c6 --- /dev/null +++ b/net_orc/network/modules/base/python/src/logger.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +import json +import logging +import os + +LOGGERS = {} +_LOG_FORMAT = "%(asctime)s %(name)-8s %(levelname)-7s %(message)s" +_DATE_FORMAT = '%b %02d %H:%M:%S' +_DEFAULT_LEVEL = logging.INFO +_CONF_DIR = "conf" +_CONF_FILE_NAME = "system.json" +_LOG_DIR = "/runtime/network/" + +# Set log level +try: + system_conf_json = json.load( + open(os.path.join(_CONF_DIR, _CONF_FILE_NAME))) + log_level_str = system_conf_json['log_level'] + log_level = logging.getLevelName(log_level_str) +except: + # TODO: Print out warning that log level is incorrect or missing + log_level = _DEFAULT_LEVEL + +log_format = logging.Formatter(fmt=_LOG_FORMAT, datefmt=_DATE_FORMAT) + + +def add_file_handler(log, logFile): + handler = logging.FileHandler(_LOG_DIR+logFile+".log") + handler.setFormatter(log_format) + log.addHandler(handler) + + +def add_stream_handler(log): + handler = logging.StreamHandler() + handler.setFormatter(log_format) + log.addHandler(handler) + + +def get_logger(name, logFile=None): + if name not in LOGGERS: + LOGGERS[name] = logging.getLogger(name) + LOGGERS[name].setLevel(log_level) + add_stream_handler(LOGGERS[name]) + if logFile is not None: + add_file_handler(LOGGERS[name], logFile) + return LOGGERS[name] diff --git a/net_orc/network/modules/dhcp-1/bin/start_network_service b/net_orc/network/modules/dhcp-1/bin/start_network_service new file mode 100644 index 000000000..e8e0ad06c --- /dev/null +++ b/net_orc/network/modules/dhcp-1/bin/start_network_service @@ -0,0 +1,77 @@ +#!/bin/bash + +CONFIG_FILE=/etc/dhcp/dhcpd.conf +DHCP_PID_FILE=/var/run/dhcpd.pid +DHCP_LOG_FILE=/runtime/network/dhcp1-dhcpd.log +RA_PID_FILE=/var/run/radvd/radvd.pid +RA_LOG_FILE=/runtime/network/dhcp1-radvd.log + +echo "Starrting Network Service..." + +#Enable IPv6 Forwarding +sysctl net.ipv6.conf.all.forwarding=1 +sysctl -p + +# Create leases file if needed +touch /var/lib/dhcp/dhcpd.leases + +#Create directory for radvd +mkdir /var/run/radvd + +#Create and set permissions on the log files +touch $DHCP_LOG_FILE +touch $RA_LOG_FILE +chown $HOST_USER:$HOST_USER $DHCP_LOG_FILE +chown $HOST_USER:$HOST_USER $RA_LOG_FILE + + +#Move the config files to the correct location +cp /testrun/conf/dhcpd.conf /etc/dhcp/dhcpd.conf +cp /testrun/conf/radvd.conf /etc/radvd.conf + +# Restart dhcp server when config changes +while true; do + + new_checksum=$(md5sum $CONFIG_FILE) + + if [ "$checksum" == "$new_checksum" ]; then + sleep 2 + continue + fi + + echo Config changed. Restarting dhcp server at $(date).. + + if [ -f $DHCP_PID_FILE ]; then + kill -9 $(cat $DHCP_PID_FILE) || true + rm -f $DHCP_PID_FILE + fi + + if [ -f $RA_PID_FILE ]; then + kill -9 $(cat $RA_PID_FILE) || true + rm -f $RA_PID_FILE + fi + + checksum=$new_checksum + + echo Starting isc-dhcp-server at $(date) + + radvd -m logfile -l $RA_LOG_FILE -p $RA_PID_FILE + dhcpd -d &> $DHCP_LOG_FILE & + + while [ ! -f $DHCP_PID_FILE ]; do + echo Waiting for $DHCP_PID_FILE... + sleep 2 + done + + echo $DHCP_PID_FILE now available + + while [ ! -f $RA_PID_FILE ]; do + echo Waiting for $RA_PID_FILE... + sleep 2 + done + + echo $RA_PID_FILE now available + + echo Server now stable + +done \ No newline at end of file diff --git a/net_orc/network/modules/dhcp-1/conf/dhcpd.conf b/net_orc/network/modules/dhcp-1/conf/dhcpd.conf new file mode 100644 index 000000000..9f4fe1c28 --- /dev/null +++ b/net_orc/network/modules/dhcp-1/conf/dhcpd.conf @@ -0,0 +1,26 @@ +default-lease-time 300; + +failover peer "failover-peer" { + primary; + address 10.10.10.2; + port 847; + peer address 10.10.10.3; + peer port 647; + max-response-delay 60; + max-unacked-updates 10; + mclt 3600; + split 128; + load balance max seconds 3; +} + +subnet 10.10.10.0 netmask 255.255.255.0 { + option ntp-servers 10.10.10.5; + option subnet-mask 255.255.255.0; + option broadcast-address 10.10.10.255; + option routers 10.10.10.1; + option domain-name-servers 10.10.10.4; + pool { + failover peer "failover-peer"; + range 10.10.10.10 10.10.10.20; + } +} diff --git a/net_orc/network/modules/dhcp-1/conf/module_config.json b/net_orc/network/modules/dhcp-1/conf/module_config.json new file mode 100644 index 000000000..56d9aa271 --- /dev/null +++ b/net_orc/network/modules/dhcp-1/conf/module_config.json @@ -0,0 +1,25 @@ +{ + "config": { + "meta": { + "name": "dhcp-1", + "display_name": "DHCP Primary", + "description": "Primary DHCP server with IPv6 SLAAC" + }, + "network": { + "interface": "veth0", + "enable_wan": false, + "ip_index": 2 + }, + "grpc":{ + "port": 5001 + }, + "docker": { + "mounts": [ + { + "source": "runtime/network", + "target": "/runtime/network" + } + ] + } + } +} \ No newline at end of file diff --git a/net_orc/network/modules/dhcp-1/conf/radvd.conf b/net_orc/network/modules/dhcp-1/conf/radvd.conf new file mode 100644 index 000000000..f6d6f30d9 --- /dev/null +++ b/net_orc/network/modules/dhcp-1/conf/radvd.conf @@ -0,0 +1,12 @@ +interface veth0 +{ + AdvSendAdvert on; + AdvManagedFlag off; + MinRtrAdvInterval 30; + MaxRtrAdvInterval 60; + prefix fd10:77be:4186::/64 { + AdvOnLink on; + AdvAutonomous on; + AdvRouterAddr on; + }; +}; \ No newline at end of file diff --git a/net_orc/network/modules/dhcp-1/dhcp-1.Dockerfile b/net_orc/network/modules/dhcp-1/dhcp-1.Dockerfile new file mode 100644 index 000000000..99804e0e3 --- /dev/null +++ b/net_orc/network/modules/dhcp-1/dhcp-1.Dockerfile @@ -0,0 +1,14 @@ +# Image name: test-run/dhcp-primary +FROM test-run/base:latest + +# Install dhcp server +RUN apt-get install -y isc-dhcp-server radvd + +# Copy over all configuration files +COPY network/modules/dhcp-1/conf /testrun/conf + +# Copy over all binary files +COPY network/modules/dhcp-1/bin /testrun/bin + +# Copy over all python files +COPY network/modules/dhcp-1/python /testrun/python diff --git a/net_orc/network/modules/dhcp-1/python/src/grpc/__init__.py b/net_orc/network/modules/dhcp-1/python/src/grpc/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/net_orc/network/modules/dhcp-1/python/src/grpc/dhcp_config.py b/net_orc/network/modules/dhcp-1/python/src/grpc/dhcp_config.py new file mode 100644 index 000000000..f5445ca44 --- /dev/null +++ b/net_orc/network/modules/dhcp-1/python/src/grpc/dhcp_config.py @@ -0,0 +1,267 @@ +import re + +CONFIG_FILE = "/etc/dhcp/dhcpd.conf" +CONFIG_FILE_TEST = "network/modules/dhcp-1/conf/dhcpd.conf" + +DEFAULT_LEASE_TIME_KEY = "default-lease-time" + + +class DHCPConfig: + + def __init__(self): + self._default_lease_time = 300 + self._subnets = [] + self._peer = None + + def write_config(self): + conf = str(self) + print("Writing config: \n" + conf) + f = open(CONFIG_FILE, "w") + f.write(conf) + + def resolve_config(self): + with open(CONFIG_FILE) as f: + conf = f.read() + self.resolve_subnets(conf) + self.peer = DHCPFailoverPeer(conf) + + def resolve_subnets(self, conf): + self._subnets = [] + regex = r"(subnet.*)" + subnets = re.findall(regex, conf, re.MULTILINE | re.DOTALL) + for subnet in subnets: + dhcp_subnet = DHCPSubnet(subnet) + self._subnets.append(dhcp_subnet) + + def set_range(self, start, end, subnet=0, pool=0): + print("Setting Range for pool ") + print(self._subnets[subnet]._pools[pool]) + self._subnets[subnet]._pools[pool]._range_start = start + self._subnets[subnet]._pools[pool]._range_end = end + + def resolve_settings(self, conf): + lines = conf.split("\n") + for line in lines: + if DEFAULT_LEASE_TIME_KEY in line: + self._default_lease_time = line.strip().split(DEFAULT_LEASE_TIME_KEY)[ + 1].strip().split(";")[0] + + self.peer = peer + + def __str__(self): + + config = """\r{DEFAULT_LEASE_TIME_KEY} {DEFAULT_LEASE_TIME};""" + + config = config.format(length='multi-line', + DEFAULT_LEASE_TIME_KEY=DEFAULT_LEASE_TIME_KEY, DEFAULT_LEASE_TIME=self._default_lease_time + ) + + config += "\n\n"+str(self.peer) + for subnet in self._subnets: + config += "\n\n"+str(subnet) + return str(config) + + +FAILOVER_PEER_KEY = "failover peer" +PRIMARY_KEY = "primary" +ADDRESS_KEY = "address" +PORT_KEY = "port" +PEER_ADDRESS_KEY = "peer address" +PEER_PORT_KEY = "peer port" +MAX_RESPONSE_DELAY_KEY = "max-response-delay" +MAX_UNACKED_UPDATES_KEY = "max-unacked-updates" +MCLT_KEY = "mclt" +SPLIT_KEY = "split" +LOAD_BALANCE_MAX_SECONDS_KEY = "load balance max seconds" + + +class DHCPFailoverPeer: + def __init__(self, config): + self.name = None + self.primary = False + self.address = None + self.port = None + self.peer_address = None + self.peer_port = None + self.max_response_delay = None + self.max_unacked_updates = None + self.mclt = None + self.split = None + self.load_balance_max_seconds = None + self.peer = None + + self.resolve_peer(config) + + def __str__(self): + config = "{FAILOVER_PEER_KEY} \"{FAILOVER_PEER}\" {{\n" + config += "\tprimary;" if self.primary else "secondary;" + config += """\n\t{ADDRESS_KEY} {ADDRESS}; + {PORT_KEY} {PORT}; + {PEER_ADDRESS_KEY} {PEER_ADDRESS}; + {PEER_PORT_KEY} {PEER_PORT}; + {MAX_RESPONSE_DELAY_KEY} {MAX_RESPONSE_DELAY}; + {MAX_UNACKED_UPDATES_KEY} {MAX_UNACKED_UPDATES}; + {MCLT_KEY} {MCLT}; + {SPLIT_KEY} {SPLIT}; + {LOAD_BALANCE_MAX_SECONDS_KEY} {LOAD_BALANCE_MAX_SECONDS}; + \r}}""" + + return config.format(length='multi-line', + FAILOVER_PEER_KEY=FAILOVER_PEER_KEY, FAILOVER_PEER=self.name, + ADDRESS_KEY=ADDRESS_KEY, ADDRESS=self.address, + PORT_KEY=PORT_KEY, PORT=self.port, + PEER_ADDRESS_KEY=PEER_ADDRESS_KEY, PEER_ADDRESS=self.peer_address, + PEER_PORT_KEY=PEER_PORT_KEY, PEER_PORT=self.peer_port, + MAX_RESPONSE_DELAY_KEY=MAX_RESPONSE_DELAY_KEY, MAX_RESPONSE_DELAY=self.max_response_delay, + MAX_UNACKED_UPDATES_KEY=MAX_UNACKED_UPDATES_KEY, MAX_UNACKED_UPDATES=self.max_unacked_updates, + MCLT_KEY=MCLT_KEY, MCLT=self.mclt, + SPLIT_KEY=SPLIT_KEY, SPLIT=self.split, + LOAD_BALANCE_MAX_SECONDS_KEY=LOAD_BALANCE_MAX_SECONDS_KEY, LOAD_BALANCE_MAX_SECONDS=self.load_balance_max_seconds + ) + + def resolve_peer(self, conf): + peer = "" + lines = conf.split("\n") + for line in lines: + if line.startswith(FAILOVER_PEER_KEY) or len(peer) > 0: + if(len(peer) <= 0): + self.name = line.strip().split(FAILOVER_PEER_KEY)[ + 1].strip().split("{")[0].split("\"")[1] + peer += line+"\n" + if PRIMARY_KEY in line: + self.primary = True + elif ADDRESS_KEY in line and PEER_ADDRESS_KEY not in line: + self.address = line.strip().split(ADDRESS_KEY)[ + 1].strip().split(";")[0] + elif PORT_KEY in line and PEER_PORT_KEY not in line: + self.port = line.strip().split(PORT_KEY)[ + 1].strip().split(";")[0] + elif PEER_ADDRESS_KEY in line: + self.peer_address = line.strip().split(PEER_ADDRESS_KEY)[ + 1].strip().split(";")[0] + elif PEER_PORT_KEY in line: + self.peer_port = line.strip().split(PEER_PORT_KEY)[ + 1].strip().split(";")[0] + elif MAX_RESPONSE_DELAY_KEY in line: + self.max_response_delay = line.strip().split(MAX_RESPONSE_DELAY_KEY)[ + 1].strip().split(";")[0] + elif MAX_UNACKED_UPDATES_KEY in line: + self.max_unacked_updates = line.strip().split(MAX_UNACKED_UPDATES_KEY)[ + 1].strip().split(";")[0] + elif MCLT_KEY in line: + self.mclt = line.strip().split(MCLT_KEY)[ + 1].strip().split(";")[0] + elif SPLIT_KEY in line: + self.split = line.strip().split(SPLIT_KEY)[ + 1].strip().split(";")[0] + elif LOAD_BALANCE_MAX_SECONDS_KEY in line: + self.load_balance_max_seconds = line.strip().split(LOAD_BALANCE_MAX_SECONDS_KEY)[ + 1].strip().split(";")[0] + if line.endswith("}") and len(peer) > 0: + break + self.peer = peer + + +NTP_OPTION_KEY = "option ntp-servers" +SUBNET_MASK_OPTION_KEY = "option subnet-mask" +BROADCAST_OPTION_KEY = "option broadcast-address" +ROUTER_OPTION_KEY = "option routers" +DNS_OPTION_KEY = "option domain-name-servers" + + +class DHCPSubnet: + def __init__(self, subnet): + self._ntp_servers = None + self._subnet_mask = None + self._broadcast = None + self._routers = None + self._dns_servers = None + self._pools = [] + + self.resolve_subnet(subnet) + self.resolve_pools(subnet) + + def __str__(self): + config = """subnet 10.10.10.0 netmask {SUBNET_MASK_OPTION} {{ + \r\t{NTP_OPTION_KEY} {NTP_OPTION}; + \r\t{SUBNET_MASK_OPTION_KEY} {SUBNET_MASK_OPTION}; + \r\t{BROADCAST_OPTION_KEY} {BROADCAST_OPTION}; + \r\t{ROUTER_OPTION_KEY} {ROUTER_OPTION}; + \r\t{DNS_OPTION_KEY} {DNS_OPTION};""" + + config = config.format(length='multi-line', + NTP_OPTION_KEY=NTP_OPTION_KEY, NTP_OPTION=self._ntp_servers, + SUBNET_MASK_OPTION_KEY=SUBNET_MASK_OPTION_KEY, SUBNET_MASK_OPTION=self._subnet_mask, + BROADCAST_OPTION_KEY=BROADCAST_OPTION_KEY, BROADCAST_OPTION=self._broadcast, + ROUTER_OPTION_KEY=ROUTER_OPTION_KEY, ROUTER_OPTION=self._routers, + DNS_OPTION_KEY=DNS_OPTION_KEY, DNS_OPTION=self._dns_servers + ) + for pool in self._pools: + config += "\n\t"+str(pool) + + config += "\n\r}" + return config + + def resolve_subnet(self, subnet): + subnet_parts = subnet.split("\n") + for part in subnet_parts: + if NTP_OPTION_KEY in part: + self._ntp_servers = part.strip().split(NTP_OPTION_KEY)[ + 1].strip().split(";")[0] + elif SUBNET_MASK_OPTION_KEY in part: + self._subnet_mask = part.strip().split(SUBNET_MASK_OPTION_KEY)[ + 1].strip().split(";")[0] + elif BROADCAST_OPTION_KEY in part: + self._broadcast = part.strip().split(BROADCAST_OPTION_KEY)[ + 1].strip().split(";")[0] + elif ROUTER_OPTION_KEY in part: + self._routers = part.strip().split(ROUTER_OPTION_KEY)[ + 1].strip().split(";")[0] + elif DNS_OPTION_KEY in part: + self._dns_servers = part.strip().split(DNS_OPTION_KEY)[ + 1].strip().split(";")[0] + + def resolve_pools(self, subnet): + regex = r"(pool.*)\}" + pools = re.findall(regex, subnet, re.MULTILINE | re.DOTALL) + for pool in pools: + dhcp_pool = DHCPPool(pool) + self._pools.append(dhcp_pool) + + +FAILOVER_KEY = "failover peer" +RANGE_KEY = "range" + + +class DHCPPool: + + def __init__(self, pool): + self._failover_peer = None + self._range_start = None + self._range_end = None + self.resolve_pool(pool) + + def __str__(self): + + config = """pool {{ + \r\t\t{FAILOVER_KEY} "{FAILOVER}"; + \r\t\t{RANGE_KEY} {RANGE_START} {RANGE_END}; + \r\t}}""" + + return config.format(length='multi-line', + FAILOVER_KEY=FAILOVER_KEY, FAILOVER=self._failover_peer, + RANGE_KEY=RANGE_KEY, RANGE_START=self._range_start, RANGE_END=self._range_end, + ) + + def resolve_pool(self, pool): + pool_parts = pool.split("\n") + # pool_parts = pool.split("\n") + for part in pool_parts: + if FAILOVER_KEY in part: + self._failover_peer = part.strip().split( + FAILOVER_KEY)[1].strip().split(";")[0].replace("\"", "") + if RANGE_KEY in part: + range = part.strip().split(RANGE_KEY)[ + 1].strip().split(";")[0] + self._range_start = range.split(" ")[0].strip() + self._range_end = range.split(" ")[1].strip() diff --git a/net_orc/network/modules/dhcp-1/python/src/grpc/network_service.py b/net_orc/network/modules/dhcp-1/python/src/grpc/network_service.py new file mode 100644 index 000000000..f90cb6b51 --- /dev/null +++ b/net_orc/network/modules/dhcp-1/python/src/grpc/network_service.py @@ -0,0 +1,44 @@ +import proto.grpc_pb2_grpc as pb2_grpc +import proto.grpc_pb2 as pb2 + +from dhcp_config import DHCPConfig + + +class NetworkService(pb2_grpc.NetworkModule): + + def __init__(self): + self._dhcp_config = DHCPConfig() + + """ + Resolve the current DHCP configuration and return + the first range from the first subnet in the file + """ + + def GetDHCPRange(self, request, context): + self._dhcp_config.resolve_config() + pool = self._dhcp_config._subnets[0]._pools[0] + return pb2.DHCPRange(code=200, start=pool._range_start, end=pool._range_end) + + """ + Change DHCP configuration and set the + the first range from the first subnet in the configuration + """ + + def SetDHCPRange(self, request, context): + print("Setting DHCPRange") + print("Start: " + request.start) + print("End: " + request.end) + self._dhcp_config.resolve_config() + self._dhcp_config.set_range(request.start, request.end, 0, 0) + self._dhcp_config.write_config() + return pb2.Response(code=200, message="DHCP Range Set") + + """ + Return the current status of the network module + """ + + def GetStatus(self, request, context): + # ToDo: Figure out how to resolve the current DHCP status + dhcpStatus = True + message = str({"dhcpStatus":dhcpStatus}) + return pb2.Response(code=200, message=message) diff --git a/net_orc/network/modules/dhcp-1/python/src/grpc/proto/grpc.proto b/net_orc/network/modules/dhcp-1/python/src/grpc/proto/grpc.proto new file mode 100644 index 000000000..8e2732620 --- /dev/null +++ b/net_orc/network/modules/dhcp-1/python/src/grpc/proto/grpc.proto @@ -0,0 +1,36 @@ +syntax = "proto3"; + +service NetworkModule { + + rpc GetDHCPRange(GetDHCPRangeRequest) returns (DHCPRange) {}; + + rpc SetDHCPRange(DHCPRange) returns (Response) {}; + + rpc GetStatus(GetStatusRequest) returns (Response) {}; + + rpc GetIPAddress(GetIPAddressRequest) returns (Response) {}; + + rpc SetLeaseAddress(SetLeaseAddressRequest) returns (Response) {}; + +} + +message Response { + int32 code = 1; + string message = 2; +} + +message DHCPRange { + int32 code = 1; + string start = 2; + string end = 3; +} + +message GetDHCPRangeRequest {} + +message GetIPAddressRequest {} + +message GetStatusRequest {} + +message SetLeaseAddressRequest { + string ipAddress = 1; +} \ No newline at end of file diff --git a/net_orc/network/modules/dhcp-1/python/src/run.py b/net_orc/network/modules/dhcp-1/python/src/run.py new file mode 100644 index 000000000..830f048cf --- /dev/null +++ b/net_orc/network/modules/dhcp-1/python/src/run.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +import signal +import sys +import argparse + +from grpc.dhcp_config import DHCPConfig + + +class DHCPServer: + + def __init__(self, module): + + signal.signal(signal.SIGINT, self.handler) + signal.signal(signal.SIGTERM, self.handler) + signal.signal(signal.SIGABRT, self.handler) + signal.signal(signal.SIGQUIT, self.handler) + + config = DHCPConfig() + config.resolve_config() + config.write_config() + + def handler(self, signum, frame): + if (signum == 2 or signal == signal.SIGTERM): + exit(1) + + +def run(argv): + parser = argparse.ArgumentParser(description="Faux Device Validator", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument( + "-m", "--module", help="Define the module name to be used to create the log file") + + args = parser.parse_args() + + server = DHCPServer(args.module) + + +if __name__ == "__main__": + run(sys.argv) diff --git a/net_orc/network/modules/dhcp-2/bin/start_network_service b/net_orc/network/modules/dhcp-2/bin/start_network_service new file mode 100644 index 000000000..d58174695 --- /dev/null +++ b/net_orc/network/modules/dhcp-2/bin/start_network_service @@ -0,0 +1,77 @@ +#!/bin/bash + +CONFIG_FILE=/etc/dhcp/dhcpd.conf +DHCP_PID_FILE=/var/run/dhcpd.pid +DHCP_LOG_FILE=/runtime/network/dhcp2-dhcpd.log +RA_PID_FILE=/var/run/radvd/radvd.pid +RA_LOG_FILE=/runtime/network/dhcp2-radvd.log + +echo "Starrting Network Service..." + +#Enable IPv6 Forwarding +sysctl net.ipv6.conf.all.forwarding=1 +sysctl -p + +# Create leases file if needed +touch /var/lib/dhcp/dhcpd.leases + +#Create directory for radvd +mkdir /var/run/radvd + +#Create and set permissions on the log files +touch $DHCP_LOG_FILE +touch $RA_LOG_FILE +chown $HOST_USER:$HOST_USER $DHCP_LOG_FILE +chown $HOST_USER:$HOST_USER $RA_LOG_FILE + + +#Move the config files to the correct location +cp /testrun/conf/dhcpd.conf /etc/dhcp/dhcpd.conf +cp /testrun/conf/radvd.conf /etc/radvd.conf + +# Restart dhcp server when config changes +while true; do + + new_checksum=$(md5sum $CONFIG_FILE) + + if [ "$checksum" == "$new_checksum" ]; then + sleep 2 + continue + fi + + echo Config changed. Restarting dhcp server at $(date).. + + if [ -f $DHCP_PID_FILE ]; then + kill -9 $(cat $DHCP_PID_FILE) || true + rm -f $DHCP_PID_FILE + fi + + if [ -f $RA_PID_FILE ]; then + kill -9 $(cat $RA_PID_FILE) || true + rm -f $RA_PID_FILE + fi + + checksum=$new_checksum + + echo Starting isc-dhcp-server at $(date) + + radvd -m logfile -l $RA_LOG_FILE -p $RA_PID_FILE + dhcpd -d &> $DHCP_LOG_FILE & + + while [ ! -f $DHCP_PID_FILE ]; do + echo Waiting for $DHCP_PID_FILE... + sleep 2 + done + + echo $DHCP_PID_FILE now available + + while [ ! -f $RA_PID_FILE ]; do + echo Waiting for $RA_PID_FILE... + sleep 2 + done + + echo $RA_PID_FILE now available + + echo Server now stable + +done \ No newline at end of file diff --git a/net_orc/network/modules/dhcp-2/conf/dhcpd.conf b/net_orc/network/modules/dhcp-2/conf/dhcpd.conf new file mode 100644 index 000000000..e73a81441 --- /dev/null +++ b/net_orc/network/modules/dhcp-2/conf/dhcpd.conf @@ -0,0 +1,24 @@ +default-lease-time 300; + +failover peer "failover-peer" { + secondary; + address 10.10.10.3; + port 647; + peer address 10.10.10.2; + peer port 847; + max-response-delay 60; + max-unacked-updates 10; + load balance max seconds 3; +} + +subnet 10.10.10.0 netmask 255.255.255.0 { + option ntp-servers 10.10.10.5; + option subnet-mask 255.255.255.0; + option broadcast-address 10.10.10.255; + option routers 10.10.10.1; + option domain-name-servers 10.10.10.4; + pool { + failover peer "failover-peer"; + range 10.10.10.10 10.10.10.20; + } +} diff --git a/net_orc/network/modules/dhcp-2/conf/module_config.json b/net_orc/network/modules/dhcp-2/conf/module_config.json new file mode 100644 index 000000000..2a978ca8c --- /dev/null +++ b/net_orc/network/modules/dhcp-2/conf/module_config.json @@ -0,0 +1,25 @@ +{ + "config": { + "meta": { + "name": "dhcp-2", + "display_name": "DHCP Secondary", + "description": "Secondary DHCP server with IPv6 SLAAC" + }, + "network": { + "interface": "veth0", + "enable_wan": false, + "ip_index": 3 + }, + "grpc":{ + "port": 5001 + }, + "docker": { + "mounts": [ + { + "source": "runtime/network", + "target": "/runtime/network" + } + ] + } + } +} \ No newline at end of file diff --git a/net_orc/network/modules/dhcp-2/conf/radvd.conf b/net_orc/network/modules/dhcp-2/conf/radvd.conf new file mode 100644 index 000000000..f6d6f30d9 --- /dev/null +++ b/net_orc/network/modules/dhcp-2/conf/radvd.conf @@ -0,0 +1,12 @@ +interface veth0 +{ + AdvSendAdvert on; + AdvManagedFlag off; + MinRtrAdvInterval 30; + MaxRtrAdvInterval 60; + prefix fd10:77be:4186::/64 { + AdvOnLink on; + AdvAutonomous on; + AdvRouterAddr on; + }; +}; \ No newline at end of file diff --git a/net_orc/network/modules/dhcp-2/dhcp-2.Dockerfile b/net_orc/network/modules/dhcp-2/dhcp-2.Dockerfile new file mode 100644 index 000000000..989992570 --- /dev/null +++ b/net_orc/network/modules/dhcp-2/dhcp-2.Dockerfile @@ -0,0 +1,14 @@ +# Image name: test-run/dhcp-primary +FROM test-run/base:latest + +# Install dhcp server +RUN apt-get install -y isc-dhcp-server radvd + +# Copy over all configuration files +COPY network/modules/dhcp-2/conf /testrun/conf + +# Copy over all binary files +COPY network/modules/dhcp-2/bin /testrun/bin + +# Copy over all python files +COPY network/modules/dhcp-2/python /testrun/python diff --git a/net_orc/network/modules/dhcp-2/python/src/grpc/__init__.py b/net_orc/network/modules/dhcp-2/python/src/grpc/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/net_orc/network/modules/dhcp-2/python/src/grpc/dhcp_config.py b/net_orc/network/modules/dhcp-2/python/src/grpc/dhcp_config.py new file mode 100644 index 000000000..f5445ca44 --- /dev/null +++ b/net_orc/network/modules/dhcp-2/python/src/grpc/dhcp_config.py @@ -0,0 +1,267 @@ +import re + +CONFIG_FILE = "/etc/dhcp/dhcpd.conf" +CONFIG_FILE_TEST = "network/modules/dhcp-1/conf/dhcpd.conf" + +DEFAULT_LEASE_TIME_KEY = "default-lease-time" + + +class DHCPConfig: + + def __init__(self): + self._default_lease_time = 300 + self._subnets = [] + self._peer = None + + def write_config(self): + conf = str(self) + print("Writing config: \n" + conf) + f = open(CONFIG_FILE, "w") + f.write(conf) + + def resolve_config(self): + with open(CONFIG_FILE) as f: + conf = f.read() + self.resolve_subnets(conf) + self.peer = DHCPFailoverPeer(conf) + + def resolve_subnets(self, conf): + self._subnets = [] + regex = r"(subnet.*)" + subnets = re.findall(regex, conf, re.MULTILINE | re.DOTALL) + for subnet in subnets: + dhcp_subnet = DHCPSubnet(subnet) + self._subnets.append(dhcp_subnet) + + def set_range(self, start, end, subnet=0, pool=0): + print("Setting Range for pool ") + print(self._subnets[subnet]._pools[pool]) + self._subnets[subnet]._pools[pool]._range_start = start + self._subnets[subnet]._pools[pool]._range_end = end + + def resolve_settings(self, conf): + lines = conf.split("\n") + for line in lines: + if DEFAULT_LEASE_TIME_KEY in line: + self._default_lease_time = line.strip().split(DEFAULT_LEASE_TIME_KEY)[ + 1].strip().split(";")[0] + + self.peer = peer + + def __str__(self): + + config = """\r{DEFAULT_LEASE_TIME_KEY} {DEFAULT_LEASE_TIME};""" + + config = config.format(length='multi-line', + DEFAULT_LEASE_TIME_KEY=DEFAULT_LEASE_TIME_KEY, DEFAULT_LEASE_TIME=self._default_lease_time + ) + + config += "\n\n"+str(self.peer) + for subnet in self._subnets: + config += "\n\n"+str(subnet) + return str(config) + + +FAILOVER_PEER_KEY = "failover peer" +PRIMARY_KEY = "primary" +ADDRESS_KEY = "address" +PORT_KEY = "port" +PEER_ADDRESS_KEY = "peer address" +PEER_PORT_KEY = "peer port" +MAX_RESPONSE_DELAY_KEY = "max-response-delay" +MAX_UNACKED_UPDATES_KEY = "max-unacked-updates" +MCLT_KEY = "mclt" +SPLIT_KEY = "split" +LOAD_BALANCE_MAX_SECONDS_KEY = "load balance max seconds" + + +class DHCPFailoverPeer: + def __init__(self, config): + self.name = None + self.primary = False + self.address = None + self.port = None + self.peer_address = None + self.peer_port = None + self.max_response_delay = None + self.max_unacked_updates = None + self.mclt = None + self.split = None + self.load_balance_max_seconds = None + self.peer = None + + self.resolve_peer(config) + + def __str__(self): + config = "{FAILOVER_PEER_KEY} \"{FAILOVER_PEER}\" {{\n" + config += "\tprimary;" if self.primary else "secondary;" + config += """\n\t{ADDRESS_KEY} {ADDRESS}; + {PORT_KEY} {PORT}; + {PEER_ADDRESS_KEY} {PEER_ADDRESS}; + {PEER_PORT_KEY} {PEER_PORT}; + {MAX_RESPONSE_DELAY_KEY} {MAX_RESPONSE_DELAY}; + {MAX_UNACKED_UPDATES_KEY} {MAX_UNACKED_UPDATES}; + {MCLT_KEY} {MCLT}; + {SPLIT_KEY} {SPLIT}; + {LOAD_BALANCE_MAX_SECONDS_KEY} {LOAD_BALANCE_MAX_SECONDS}; + \r}}""" + + return config.format(length='multi-line', + FAILOVER_PEER_KEY=FAILOVER_PEER_KEY, FAILOVER_PEER=self.name, + ADDRESS_KEY=ADDRESS_KEY, ADDRESS=self.address, + PORT_KEY=PORT_KEY, PORT=self.port, + PEER_ADDRESS_KEY=PEER_ADDRESS_KEY, PEER_ADDRESS=self.peer_address, + PEER_PORT_KEY=PEER_PORT_KEY, PEER_PORT=self.peer_port, + MAX_RESPONSE_DELAY_KEY=MAX_RESPONSE_DELAY_KEY, MAX_RESPONSE_DELAY=self.max_response_delay, + MAX_UNACKED_UPDATES_KEY=MAX_UNACKED_UPDATES_KEY, MAX_UNACKED_UPDATES=self.max_unacked_updates, + MCLT_KEY=MCLT_KEY, MCLT=self.mclt, + SPLIT_KEY=SPLIT_KEY, SPLIT=self.split, + LOAD_BALANCE_MAX_SECONDS_KEY=LOAD_BALANCE_MAX_SECONDS_KEY, LOAD_BALANCE_MAX_SECONDS=self.load_balance_max_seconds + ) + + def resolve_peer(self, conf): + peer = "" + lines = conf.split("\n") + for line in lines: + if line.startswith(FAILOVER_PEER_KEY) or len(peer) > 0: + if(len(peer) <= 0): + self.name = line.strip().split(FAILOVER_PEER_KEY)[ + 1].strip().split("{")[0].split("\"")[1] + peer += line+"\n" + if PRIMARY_KEY in line: + self.primary = True + elif ADDRESS_KEY in line and PEER_ADDRESS_KEY not in line: + self.address = line.strip().split(ADDRESS_KEY)[ + 1].strip().split(";")[0] + elif PORT_KEY in line and PEER_PORT_KEY not in line: + self.port = line.strip().split(PORT_KEY)[ + 1].strip().split(";")[0] + elif PEER_ADDRESS_KEY in line: + self.peer_address = line.strip().split(PEER_ADDRESS_KEY)[ + 1].strip().split(";")[0] + elif PEER_PORT_KEY in line: + self.peer_port = line.strip().split(PEER_PORT_KEY)[ + 1].strip().split(";")[0] + elif MAX_RESPONSE_DELAY_KEY in line: + self.max_response_delay = line.strip().split(MAX_RESPONSE_DELAY_KEY)[ + 1].strip().split(";")[0] + elif MAX_UNACKED_UPDATES_KEY in line: + self.max_unacked_updates = line.strip().split(MAX_UNACKED_UPDATES_KEY)[ + 1].strip().split(";")[0] + elif MCLT_KEY in line: + self.mclt = line.strip().split(MCLT_KEY)[ + 1].strip().split(";")[0] + elif SPLIT_KEY in line: + self.split = line.strip().split(SPLIT_KEY)[ + 1].strip().split(";")[0] + elif LOAD_BALANCE_MAX_SECONDS_KEY in line: + self.load_balance_max_seconds = line.strip().split(LOAD_BALANCE_MAX_SECONDS_KEY)[ + 1].strip().split(";")[0] + if line.endswith("}") and len(peer) > 0: + break + self.peer = peer + + +NTP_OPTION_KEY = "option ntp-servers" +SUBNET_MASK_OPTION_KEY = "option subnet-mask" +BROADCAST_OPTION_KEY = "option broadcast-address" +ROUTER_OPTION_KEY = "option routers" +DNS_OPTION_KEY = "option domain-name-servers" + + +class DHCPSubnet: + def __init__(self, subnet): + self._ntp_servers = None + self._subnet_mask = None + self._broadcast = None + self._routers = None + self._dns_servers = None + self._pools = [] + + self.resolve_subnet(subnet) + self.resolve_pools(subnet) + + def __str__(self): + config = """subnet 10.10.10.0 netmask {SUBNET_MASK_OPTION} {{ + \r\t{NTP_OPTION_KEY} {NTP_OPTION}; + \r\t{SUBNET_MASK_OPTION_KEY} {SUBNET_MASK_OPTION}; + \r\t{BROADCAST_OPTION_KEY} {BROADCAST_OPTION}; + \r\t{ROUTER_OPTION_KEY} {ROUTER_OPTION}; + \r\t{DNS_OPTION_KEY} {DNS_OPTION};""" + + config = config.format(length='multi-line', + NTP_OPTION_KEY=NTP_OPTION_KEY, NTP_OPTION=self._ntp_servers, + SUBNET_MASK_OPTION_KEY=SUBNET_MASK_OPTION_KEY, SUBNET_MASK_OPTION=self._subnet_mask, + BROADCAST_OPTION_KEY=BROADCAST_OPTION_KEY, BROADCAST_OPTION=self._broadcast, + ROUTER_OPTION_KEY=ROUTER_OPTION_KEY, ROUTER_OPTION=self._routers, + DNS_OPTION_KEY=DNS_OPTION_KEY, DNS_OPTION=self._dns_servers + ) + for pool in self._pools: + config += "\n\t"+str(pool) + + config += "\n\r}" + return config + + def resolve_subnet(self, subnet): + subnet_parts = subnet.split("\n") + for part in subnet_parts: + if NTP_OPTION_KEY in part: + self._ntp_servers = part.strip().split(NTP_OPTION_KEY)[ + 1].strip().split(";")[0] + elif SUBNET_MASK_OPTION_KEY in part: + self._subnet_mask = part.strip().split(SUBNET_MASK_OPTION_KEY)[ + 1].strip().split(";")[0] + elif BROADCAST_OPTION_KEY in part: + self._broadcast = part.strip().split(BROADCAST_OPTION_KEY)[ + 1].strip().split(";")[0] + elif ROUTER_OPTION_KEY in part: + self._routers = part.strip().split(ROUTER_OPTION_KEY)[ + 1].strip().split(";")[0] + elif DNS_OPTION_KEY in part: + self._dns_servers = part.strip().split(DNS_OPTION_KEY)[ + 1].strip().split(";")[0] + + def resolve_pools(self, subnet): + regex = r"(pool.*)\}" + pools = re.findall(regex, subnet, re.MULTILINE | re.DOTALL) + for pool in pools: + dhcp_pool = DHCPPool(pool) + self._pools.append(dhcp_pool) + + +FAILOVER_KEY = "failover peer" +RANGE_KEY = "range" + + +class DHCPPool: + + def __init__(self, pool): + self._failover_peer = None + self._range_start = None + self._range_end = None + self.resolve_pool(pool) + + def __str__(self): + + config = """pool {{ + \r\t\t{FAILOVER_KEY} "{FAILOVER}"; + \r\t\t{RANGE_KEY} {RANGE_START} {RANGE_END}; + \r\t}}""" + + return config.format(length='multi-line', + FAILOVER_KEY=FAILOVER_KEY, FAILOVER=self._failover_peer, + RANGE_KEY=RANGE_KEY, RANGE_START=self._range_start, RANGE_END=self._range_end, + ) + + def resolve_pool(self, pool): + pool_parts = pool.split("\n") + # pool_parts = pool.split("\n") + for part in pool_parts: + if FAILOVER_KEY in part: + self._failover_peer = part.strip().split( + FAILOVER_KEY)[1].strip().split(";")[0].replace("\"", "") + if RANGE_KEY in part: + range = part.strip().split(RANGE_KEY)[ + 1].strip().split(";")[0] + self._range_start = range.split(" ")[0].strip() + self._range_end = range.split(" ")[1].strip() diff --git a/net_orc/network/modules/dhcp-2/python/src/grpc/network_service.py b/net_orc/network/modules/dhcp-2/python/src/grpc/network_service.py new file mode 100644 index 000000000..f90cb6b51 --- /dev/null +++ b/net_orc/network/modules/dhcp-2/python/src/grpc/network_service.py @@ -0,0 +1,44 @@ +import proto.grpc_pb2_grpc as pb2_grpc +import proto.grpc_pb2 as pb2 + +from dhcp_config import DHCPConfig + + +class NetworkService(pb2_grpc.NetworkModule): + + def __init__(self): + self._dhcp_config = DHCPConfig() + + """ + Resolve the current DHCP configuration and return + the first range from the first subnet in the file + """ + + def GetDHCPRange(self, request, context): + self._dhcp_config.resolve_config() + pool = self._dhcp_config._subnets[0]._pools[0] + return pb2.DHCPRange(code=200, start=pool._range_start, end=pool._range_end) + + """ + Change DHCP configuration and set the + the first range from the first subnet in the configuration + """ + + def SetDHCPRange(self, request, context): + print("Setting DHCPRange") + print("Start: " + request.start) + print("End: " + request.end) + self._dhcp_config.resolve_config() + self._dhcp_config.set_range(request.start, request.end, 0, 0) + self._dhcp_config.write_config() + return pb2.Response(code=200, message="DHCP Range Set") + + """ + Return the current status of the network module + """ + + def GetStatus(self, request, context): + # ToDo: Figure out how to resolve the current DHCP status + dhcpStatus = True + message = str({"dhcpStatus":dhcpStatus}) + return pb2.Response(code=200, message=message) diff --git a/net_orc/network/modules/dhcp-2/python/src/grpc/proto/grpc.proto b/net_orc/network/modules/dhcp-2/python/src/grpc/proto/grpc.proto new file mode 100644 index 000000000..8e2732620 --- /dev/null +++ b/net_orc/network/modules/dhcp-2/python/src/grpc/proto/grpc.proto @@ -0,0 +1,36 @@ +syntax = "proto3"; + +service NetworkModule { + + rpc GetDHCPRange(GetDHCPRangeRequest) returns (DHCPRange) {}; + + rpc SetDHCPRange(DHCPRange) returns (Response) {}; + + rpc GetStatus(GetStatusRequest) returns (Response) {}; + + rpc GetIPAddress(GetIPAddressRequest) returns (Response) {}; + + rpc SetLeaseAddress(SetLeaseAddressRequest) returns (Response) {}; + +} + +message Response { + int32 code = 1; + string message = 2; +} + +message DHCPRange { + int32 code = 1; + string start = 2; + string end = 3; +} + +message GetDHCPRangeRequest {} + +message GetIPAddressRequest {} + +message GetStatusRequest {} + +message SetLeaseAddressRequest { + string ipAddress = 1; +} \ No newline at end of file diff --git a/net_orc/network/modules/dhcp-2/python/src/run.py b/net_orc/network/modules/dhcp-2/python/src/run.py new file mode 100644 index 000000000..830f048cf --- /dev/null +++ b/net_orc/network/modules/dhcp-2/python/src/run.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +import signal +import sys +import argparse + +from grpc.dhcp_config import DHCPConfig + + +class DHCPServer: + + def __init__(self, module): + + signal.signal(signal.SIGINT, self.handler) + signal.signal(signal.SIGTERM, self.handler) + signal.signal(signal.SIGABRT, self.handler) + signal.signal(signal.SIGQUIT, self.handler) + + config = DHCPConfig() + config.resolve_config() + config.write_config() + + def handler(self, signum, frame): + if (signum == 2 or signal == signal.SIGTERM): + exit(1) + + +def run(argv): + parser = argparse.ArgumentParser(description="Faux Device Validator", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument( + "-m", "--module", help="Define the module name to be used to create the log file") + + args = parser.parse_args() + + server = DHCPServer(args.module) + + +if __name__ == "__main__": + run(sys.argv) diff --git a/net_orc/network/modules/dns/bin/start_network_service b/net_orc/network/modules/dns/bin/start_network_service new file mode 100644 index 000000000..4537033c0 --- /dev/null +++ b/net_orc/network/modules/dns/bin/start_network_service @@ -0,0 +1,48 @@ +#!/bin/bash + +CONFIG_FILE=/etc/dnsmasq.conf +PID_FILE=/var/run/dnsmasq.pid +LOG_FILE=/runtime/network/dns.log + +echo Starting dns + +cp /testrun/conf/dnsmasq.conf /etc/dnsmasq.conf + +# Route internet traffic through gateway +ip route add default via 10.10.10.1 dev veth0 + +# Restart dnsmasq when config changes +while true; do + + new_checksum=$(md5sum $CONFIG_FILE) + + if [ "$checksum" == "$new_checksum" ]; then + sleep 2 + continue + fi + + echo Config changed. Restarting dnsmasq at $(date).. + + if [ -f $PID_FILE ]; then + kill -9 $(cat $PID_FILE) || true + rm -f $PID_FILE + fi + + checksum=$new_checksum + + echo Starting dnsmasq at $(date) + + dnsmasq --log-facility=$LOG_FILE -u $HOST_USER & + + while [ ! -f $PID_FILE ]; do + echo Waiting for $PID_FILE... + sleep 2 + done + + # Group flag doesn't work for some reason on dnsmasq + # so we'll manually change the group to the log file + chgrp $HOST_USER $LOG_FILE + + echo $PID_FILE now available + +done \ No newline at end of file diff --git a/net_orc/network/modules/dns/conf/dnsmasq.conf b/net_orc/network/modules/dns/conf/dnsmasq.conf new file mode 100644 index 000000000..5513a9220 --- /dev/null +++ b/net_orc/network/modules/dns/conf/dnsmasq.conf @@ -0,0 +1,5 @@ +server=8.8.8.8 + +interface=veth0 + +log-queries \ No newline at end of file diff --git a/net_orc/network/modules/dns/conf/module_config.json b/net_orc/network/modules/dns/conf/module_config.json new file mode 100644 index 000000000..73f890d28 --- /dev/null +++ b/net_orc/network/modules/dns/conf/module_config.json @@ -0,0 +1,22 @@ +{ + "config": { + "meta": { + "name": "dns", + "display_name": "DNS", + "description": "A DNS server" + }, + "network": { + "interface": "veth0", + "enable_wan": false, + "ip_index": 4 + }, + "docker": { + "mounts": [ + { + "source": "runtime/network", + "target": "/runtime/network" + } + ] + } + } +} \ No newline at end of file diff --git a/net_orc/network/modules/dns/dns.Dockerfile b/net_orc/network/modules/dns/dns.Dockerfile new file mode 100644 index 000000000..84c1c7eb1 --- /dev/null +++ b/net_orc/network/modules/dns/dns.Dockerfile @@ -0,0 +1,14 @@ +# Image name: test-run/dns +FROM test-run/base:latest + +#Update and get all additional requirements not contained in the base image +RUN apt-get update --fix-missing + +#Install dnsmasq +RUN apt-get install -y dnsmasq + +# Copy over all configuration files +COPY network/modules/dns/conf /testrun/conf + +# Copy over all binary files +COPY network/modules/dns/bin /testrun/bin diff --git a/net_orc/network/modules/gateway/bin/start_network_service b/net_orc/network/modules/gateway/bin/start_network_service new file mode 100644 index 000000000..b1b31d335 --- /dev/null +++ b/net_orc/network/modules/gateway/bin/start_network_service @@ -0,0 +1,30 @@ +#!/bin/bash + +LOCAL_INTF=veth0 +EXT_INTF=eth1 + +echo Starting gateway + +/testrun/bin/wait_for_interface $EXT_INT + +# Enable IPv6 forwarding +sysctl net.ipv6.conf.eth1.accept_ra=1 +sysctl net.ipv6.conf.default.forwarding=1 +sysctl -p + +# Start dhclient if external interface does not have IP +if ! ip addr show $EXT_INTF | fgrep 'inet '; then + echo No inet address for $EXT_INTF, initiating dhcp client... + dhclient $EXT_INTF +fi + +# Enable NAT to the outside world +echo 1 > /proc/sys/net/ipv4/ip_forward +iptables -t nat -A POSTROUTING -o $EXT_INTF -j MASQUERADE +iptables -A FORWARD -i $EXT_INTF -o $LOCAL_INTF -m state --state RELATED,ESTABLISHED -j ACCEPT +iptables -A FORWARD -i $LOCAL_INTF -o $EXT_INTF -j ACCEPT + +# Keep gateway running until killed by framework +while true; do + sleep 10 +done diff --git a/net_orc/network/modules/gateway/conf/module_config.json b/net_orc/network/modules/gateway/conf/module_config.json new file mode 100644 index 000000000..35bd34392 --- /dev/null +++ b/net_orc/network/modules/gateway/conf/module_config.json @@ -0,0 +1,22 @@ +{ + "config": { + "meta": { + "name": "gateway", + "display_name": "Gateway", + "description": "Enable internet connectivity on device bridge" + }, + "network": { + "interface": "veth0", + "enable_wan": true, + "ip_index": 1 + }, + "docker": { + "mounts": [ + { + "source": "runtime/network", + "target": "/runtime/network" + } + ] + } + } +} \ No newline at end of file diff --git a/net_orc/network/modules/gateway/gateway.Dockerfile b/net_orc/network/modules/gateway/gateway.Dockerfile new file mode 100644 index 000000000..b7085ebac --- /dev/null +++ b/net_orc/network/modules/gateway/gateway.Dockerfile @@ -0,0 +1,11 @@ +# Image name: test-run/gateway +FROM test-run/base:latest + +# Install required packages +RUN apt-get install -y iptables isc-dhcp-client + +# Copy over all configuration files +COPY network/modules/gateway/conf /testrun/conf + +# Copy over all binary files +COPY network/modules/gateway/bin /testrun/bin diff --git a/net_orc/network/modules/ntp/bin/start_network_service b/net_orc/network/modules/ntp/bin/start_network_service new file mode 100644 index 000000000..4c0c5dc74 --- /dev/null +++ b/net_orc/network/modules/ntp/bin/start_network_service @@ -0,0 +1,13 @@ +#!/bin/bash + +PYTHON_SRC_DIR=/testrun/python/src +LOG_FILE="/runtime/network/ntp.log" + +echo Starting ntp + +#Create and set permissions on the log file +touch $LOG_FILE +chown $HOST_USER:$HOST_USER $LOG_FILE + +#Start the NTP server +python3 -u $PYTHON_SRC_DIR/ntp_server.py > $LOG_FILE diff --git a/net_orc/network/modules/ntp/conf/module_config.json b/net_orc/network/modules/ntp/conf/module_config.json new file mode 100644 index 000000000..781521263 --- /dev/null +++ b/net_orc/network/modules/ntp/conf/module_config.json @@ -0,0 +1,22 @@ +{ + "config": { + "meta": { + "name": "ntp", + "display_name": "NTP", + "description": "An NTP server" + }, + "network": { + "interface": "veth0", + "enable_wan": false, + "ip_index": 5 + }, + "docker": { + "mounts": [ + { + "source": "runtime/network", + "target": "/runtime/network" + } + ] + } + } +} \ No newline at end of file diff --git a/net_orc/network/modules/ntp/ntp-server.py b/net_orc/network/modules/ntp/ntp-server.py new file mode 100644 index 000000000..ace3099b0 --- /dev/null +++ b/net_orc/network/modules/ntp/ntp-server.py @@ -0,0 +1,315 @@ +import datetime +import socket +import struct +import time +import queue + +import threading +import select + +taskQueue = queue.Queue() +stopFlag = False + +def system_to_ntp_time(timestamp): + """Convert a system time to a NTP time. + + Parameters: + timestamp -- timestamp in system time + + Returns: + corresponding NTP time + """ + return timestamp + NTP.NTP_DELTA + +def _to_int(timestamp): + """Return the integral part of a timestamp. + + Parameters: + timestamp -- NTP timestamp + + Retuns: + integral part + """ + return int(timestamp) + +def _to_frac(timestamp, n=32): + """Return the fractional part of a timestamp. + + Parameters: + timestamp -- NTP timestamp + n -- number of bits of the fractional part + + Retuns: + fractional part + """ + return int(abs(timestamp - _to_int(timestamp)) * 2**n) + +def _to_time(integ, frac, n=32): + """Return a timestamp from an integral and fractional part. + + Parameters: + integ -- integral part + frac -- fractional part + n -- number of bits of the fractional part + + Retuns: + timestamp + """ + return integ + float(frac)/2**n + + + +class NTPException(Exception): + """Exception raised by this module.""" + pass + + +class NTP: + """Helper class defining constants.""" + + _SYSTEM_EPOCH = datetime.date(*time.gmtime(0)[0:3]) + """system epoch""" + _NTP_EPOCH = datetime.date(1900, 1, 1) + """NTP epoch""" + NTP_DELTA = (_SYSTEM_EPOCH - _NTP_EPOCH).days * 24 * 3600 + """delta between system and NTP time""" + + REF_ID_TABLE = { + 'DNC': "DNC routing protocol", + 'NIST': "NIST public modem", + 'TSP': "TSP time protocol", + 'DTS': "Digital Time Service", + 'ATOM': "Atomic clock (calibrated)", + 'VLF': "VLF radio (OMEGA, etc)", + 'callsign': "Generic radio", + 'LORC': "LORAN-C radionavidation", + 'GOES': "GOES UHF environment satellite", + 'GPS': "GPS UHF satellite positioning", + } + """reference identifier table""" + + STRATUM_TABLE = { + 0: "unspecified", + 1: "primary reference", + } + """stratum table""" + + MODE_TABLE = { + 0: "unspecified", + 1: "symmetric active", + 2: "symmetric passive", + 3: "client", + 4: "server", + 5: "broadcast", + 6: "reserved for NTP control messages", + 7: "reserved for private use", + } + """mode table""" + + LEAP_TABLE = { + 0: "no warning", + 1: "last minute has 61 seconds", + 2: "last minute has 59 seconds", + 3: "alarm condition (clock not synchronized)", + } + """leap indicator table""" + +class NTPPacket: + """NTP packet class. + + This represents an NTP packet. + """ + + _PACKET_FORMAT = "!B B B b 11I" + """packet format to pack/unpack""" + + def __init__(self, version=4, mode=3, tx_timestamp=0): + """Constructor. + + Parameters: + version -- NTP version + mode -- packet mode (client, server) + tx_timestamp -- packet transmit timestamp + """ + self.leap = 0 + """leap second indicator""" + self.version = version + """version""" + self.mode = mode + """mode""" + self.stratum = 0 + """stratum""" + self.poll = 0 + """poll interval""" + self.precision = 0 + """precision""" + self.root_delay = 0 + """root delay""" + self.root_dispersion = 0 + """root dispersion""" + self.ref_id = 0 + """reference clock identifier""" + self.ref_timestamp = 0 + """reference timestamp""" + self.orig_timestamp = 0 + self.orig_timestamp_high = 0 + self.orig_timestamp_low = 0 + """originate timestamp""" + self.recv_timestamp = 0 + """receive timestamp""" + self.tx_timestamp = tx_timestamp + self.tx_timestamp_high = 0 + self.tx_timestamp_low = 0 + """tansmit timestamp""" + + def to_data(self): + """Convert this NTPPacket to a buffer that can be sent over a socket. + + Returns: + buffer representing this packet + + Raises: + NTPException -- in case of invalid field + """ + try: + packed = struct.pack(NTPPacket._PACKET_FORMAT, + (self.leap << 6 | self.version << 3 | self.mode), + self.stratum, + self.poll, + self.precision, + _to_int(self.root_delay) << 16 | _to_frac(self.root_delay, 16), + _to_int(self.root_dispersion) << 16 | + _to_frac(self.root_dispersion, 16), + self.ref_id, + _to_int(self.ref_timestamp), + _to_frac(self.ref_timestamp), + #Change by lichen, avoid loss of precision + self.orig_timestamp_high, + self.orig_timestamp_low, + _to_int(self.recv_timestamp), + _to_frac(self.recv_timestamp), + _to_int(self.tx_timestamp), + _to_frac(self.tx_timestamp)) + except struct.error: + raise NTPException("Invalid NTP packet fields.") + return packed + + def from_data(self, data): + """Populate this instance from a NTP packet payload received from + the network. + + Parameters: + data -- buffer payload + + Raises: + NTPException -- in case of invalid packet format + """ + try: + unpacked = struct.unpack(NTPPacket._PACKET_FORMAT, + data[0:struct.calcsize(NTPPacket._PACKET_FORMAT)]) + except struct.error: + raise NTPException("Invalid NTP packet.") + + self.leap = unpacked[0] >> 6 & 0x3 + self.version = unpacked[0] >> 3 & 0x7 + self.mode = unpacked[0] & 0x7 + self.stratum = unpacked[1] + self.poll = unpacked[2] + self.precision = unpacked[3] + self.root_delay = float(unpacked[4])/2**16 + self.root_dispersion = float(unpacked[5])/2**16 + self.ref_id = unpacked[6] + self.ref_timestamp = _to_time(unpacked[7], unpacked[8]) + self.orig_timestamp = _to_time(unpacked[9], unpacked[10]) + self.orig_timestamp_high = unpacked[9] + self.orig_timestamp_low = unpacked[10] + self.recv_timestamp = _to_time(unpacked[11], unpacked[12]) + self.tx_timestamp = _to_time(unpacked[13], unpacked[14]) + self.tx_timestamp_high = unpacked[13] + self.tx_timestamp_low = unpacked[14] + + def GetTxTimeStamp(self): + return (self.tx_timestamp_high,self.tx_timestamp_low) + + def SetOriginTimeStamp(self,high,low): + self.orig_timestamp_high = high + self.orig_timestamp_low = low + + +class RecvThread(threading.Thread): + def __init__(self,socket): + threading.Thread.__init__(self) + self.socket = socket + def run(self): + global t,stopFlag + while True: + if stopFlag == True: + print("RecvThread Ended") + break + rlist,wlist,elist = select.select([self.socket],[],[],1); + if len(rlist) != 0: + print("Received %d packets" % len(rlist)) + for tempSocket in rlist: + try: + data,addr = tempSocket.recvfrom(1024) + recvTimestamp = recvTimestamp = system_to_ntp_time(time.time()) + taskQueue.put((data,addr,recvTimestamp)) + except socket.error as msg: + print(msg) + +class WorkThread(threading.Thread): + def __init__(self,socket): + threading.Thread.__init__(self) + self.socket = socket + def run(self): + global taskQueue,stopFlag + while True: + if stopFlag == True: + print("WorkThread Ended") + break + try: + data,addr,recvTimestamp = taskQueue.get(timeout=1) + recvPacket = NTPPacket() + recvPacket.from_data(data) + timeStamp_high,timeStamp_low = recvPacket.GetTxTimeStamp() + sendPacket = NTPPacket(version=4,mode=4) + sendPacket.stratum = 2 + sendPacket.poll = 10 + ''' + sendPacket.precision = 0xfa + sendPacket.root_delay = 0x0bfa + sendPacket.root_dispersion = 0x0aa7 + sendPacket.ref_id = 0x808a8c2c + ''' + sendPacket.ref_timestamp = recvTimestamp-5 + sendPacket.SetOriginTimeStamp(timeStamp_high,timeStamp_low) + sendPacket.recv_timestamp = recvTimestamp + sendPacket.tx_timestamp = system_to_ntp_time(time.time()) + socket.sendto(sendPacket.to_data(),addr) + print("Sent to %s:%d" % (addr[0],addr[1])) + except queue.Empty: + continue + + +listenIp = "0.0.0.0" +listenPort = 123 +socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) +socket.bind((listenIp,listenPort)) +print("local socket: ", socket.getsockname()); +recvThread = RecvThread(socket) +recvThread.start() +workThread = WorkThread(socket) +workThread.start() + +while True: + try: + time.sleep(0.5) + except KeyboardInterrupt: + print("Exiting...") + stopFlag = True + recvThread.join() + workThread.join() + #socket.close() + print("Exited") + break + diff --git a/net_orc/network/modules/ntp/ntp.Dockerfile b/net_orc/network/modules/ntp/ntp.Dockerfile new file mode 100644 index 000000000..3474a504e --- /dev/null +++ b/net_orc/network/modules/ntp/ntp.Dockerfile @@ -0,0 +1,13 @@ +# Image name: test-run/ntp +FROM test-run/base:latest + +# Copy over all configuration files +COPY network/modules/ntp/conf /testrun/conf + +# Copy over all binary files +COPY network/modules/ntp/bin /testrun/bin + +# Copy over all python files +COPY network/modules/ntp/python /testrun/python + +EXPOSE 123/udp diff --git a/net_orc/network/modules/ntp/python/src/ntp_server.py b/net_orc/network/modules/ntp/python/src/ntp_server.py new file mode 100644 index 000000000..a53134fe7 --- /dev/null +++ b/net_orc/network/modules/ntp/python/src/ntp_server.py @@ -0,0 +1,315 @@ +import datetime +import socket +import struct +import time +import queue + +import threading +import select + +taskQueue = queue.Queue() +stopFlag = False + +def system_to_ntp_time(timestamp): + """Convert a system time to a NTP time. + + Parameters: + timestamp -- timestamp in system time + + Returns: + corresponding NTP time + """ + return timestamp + NTP.NTP_DELTA + +def _to_int(timestamp): + """Return the integral part of a timestamp. + + Parameters: + timestamp -- NTP timestamp + + Retuns: + integral part + """ + return int(timestamp) + +def _to_frac(timestamp, n=32): + """Return the fractional part of a timestamp. + + Parameters: + timestamp -- NTP timestamp + n -- number of bits of the fractional part + + Retuns: + fractional part + """ + return int(abs(timestamp - _to_int(timestamp)) * 2**n) + +def _to_time(integ, frac, n=32): + """Return a timestamp from an integral and fractional part. + + Parameters: + integ -- integral part + frac -- fractional part + n -- number of bits of the fractional part + + Retuns: + timestamp + """ + return integ + float(frac)/2**n + + + +class NTPException(Exception): + """Exception raised by this module.""" + pass + + +class NTP: + """Helper class defining constants.""" + + _SYSTEM_EPOCH = datetime.date(*time.gmtime(0)[0:3]) + """system epoch""" + _NTP_EPOCH = datetime.date(1900, 1, 1) + """NTP epoch""" + NTP_DELTA = (_SYSTEM_EPOCH - _NTP_EPOCH).days * 24 * 3600 + """delta between system and NTP time""" + + REF_ID_TABLE = { + 'DNC': "DNC routing protocol", + 'NIST': "NIST public modem", + 'TSP': "TSP time protocol", + 'DTS': "Digital Time Service", + 'ATOM': "Atomic clock (calibrated)", + 'VLF': "VLF radio (OMEGA, etc)", + 'callsign': "Generic radio", + 'LORC': "LORAN-C radionavidation", + 'GOES': "GOES UHF environment satellite", + 'GPS': "GPS UHF satellite positioning", + } + """reference identifier table""" + + STRATUM_TABLE = { + 0: "unspecified", + 1: "primary reference", + } + """stratum table""" + + MODE_TABLE = { + 0: "unspecified", + 1: "symmetric active", + 2: "symmetric passive", + 3: "client", + 4: "server", + 5: "broadcast", + 6: "reserved for NTP control messages", + 7: "reserved for private use", + } + """mode table""" + + LEAP_TABLE = { + 0: "no warning", + 1: "last minute has 61 seconds", + 2: "last minute has 59 seconds", + 3: "alarm condition (clock not synchronized)", + } + """leap indicator table""" + +class NTPPacket: + """NTP packet class. + + This represents an NTP packet. + """ + + _PACKET_FORMAT = "!B B B b 11I" + """packet format to pack/unpack""" + + def __init__(self, version=4, mode=3, tx_timestamp=0): + """Constructor. + + Parameters: + version -- NTP version + mode -- packet mode (client, server) + tx_timestamp -- packet transmit timestamp + """ + self.leap = 0 + """leap second indicator""" + self.version = version + """version""" + self.mode = mode + """mode""" + self.stratum = 0 + """stratum""" + self.poll = 0 + """poll interval""" + self.precision = 0 + """precision""" + self.root_delay = 0 + """root delay""" + self.root_dispersion = 0 + """root dispersion""" + self.ref_id = 0 + """reference clock identifier""" + self.ref_timestamp = 0 + """reference timestamp""" + self.orig_timestamp = 0 + self.orig_timestamp_high = 0 + self.orig_timestamp_low = 0 + """originate timestamp""" + self.recv_timestamp = 0 + """receive timestamp""" + self.tx_timestamp = tx_timestamp + self.tx_timestamp_high = 0 + self.tx_timestamp_low = 0 + """tansmit timestamp""" + + def to_data(self): + """Convert this NTPPacket to a buffer that can be sent over a socket. + + Returns: + buffer representing this packet + + Raises: + NTPException -- in case of invalid field + """ + try: + packed = struct.pack(NTPPacket._PACKET_FORMAT, + (self.leap << 6 | self.version << 3 | self.mode), + self.stratum, + self.poll, + self.precision, + _to_int(self.root_delay) << 16 | _to_frac(self.root_delay, 16), + _to_int(self.root_dispersion) << 16 | + _to_frac(self.root_dispersion, 16), + self.ref_id, + _to_int(self.ref_timestamp), + _to_frac(self.ref_timestamp), + #Change by lichen, avoid loss of precision + self.orig_timestamp_high, + self.orig_timestamp_low, + _to_int(self.recv_timestamp), + _to_frac(self.recv_timestamp), + _to_int(self.tx_timestamp), + _to_frac(self.tx_timestamp)) + except struct.error: + raise NTPException("Invalid NTP packet fields.") + return packed + + def from_data(self, data): + """Populate this instance from a NTP packet payload received from + the network. + + Parameters: + data -- buffer payload + + Raises: + NTPException -- in case of invalid packet format + """ + try: + unpacked = struct.unpack(NTPPacket._PACKET_FORMAT, + data[0:struct.calcsize(NTPPacket._PACKET_FORMAT)]) + except struct.error: + raise NTPException("Invalid NTP packet.") + + self.leap = unpacked[0] >> 6 & 0x3 + self.version = unpacked[0] >> 3 & 0x7 + self.mode = unpacked[0] & 0x7 + self.stratum = unpacked[1] + self.poll = unpacked[2] + self.precision = unpacked[3] + self.root_delay = float(unpacked[4])/2**16 + self.root_dispersion = float(unpacked[5])/2**16 + self.ref_id = unpacked[6] + self.ref_timestamp = _to_time(unpacked[7], unpacked[8]) + self.orig_timestamp = _to_time(unpacked[9], unpacked[10]) + self.orig_timestamp_high = unpacked[9] + self.orig_timestamp_low = unpacked[10] + self.recv_timestamp = _to_time(unpacked[11], unpacked[12]) + self.tx_timestamp = _to_time(unpacked[13], unpacked[14]) + self.tx_timestamp_high = unpacked[13] + self.tx_timestamp_low = unpacked[14] + + def GetTxTimeStamp(self): + return (self.tx_timestamp_high,self.tx_timestamp_low) + + def SetOriginTimeStamp(self,high,low): + self.orig_timestamp_high = high + self.orig_timestamp_low = low + + +class RecvThread(threading.Thread): + def __init__(self,socket): + threading.Thread.__init__(self) + self.socket = socket + def run(self): + global t,stopFlag + while True: + if stopFlag == True: + print("RecvThread Ended") + break + rlist,wlist,elist = select.select([self.socket],[],[],1); + if len(rlist) != 0: + print("Received %d packets" % len(rlist)) + for tempSocket in rlist: + try: + data,addr = tempSocket.recvfrom(1024) + recvTimestamp = recvTimestamp = system_to_ntp_time(time.time()) + taskQueue.put((data,addr,recvTimestamp)) + except socket.error as msg: + print(msg) + +class WorkThread(threading.Thread): + def __init__(self,socket): + threading.Thread.__init__(self) + self.socket = socket + def run(self): + global taskQueue,stopFlag + while True: + if stopFlag == True: + print("WorkThread Ended") + break + try: + data,addr,recvTimestamp = taskQueue.get(timeout=1) + recvPacket = NTPPacket() + recvPacket.from_data(data) + timeStamp_high,timeStamp_low = recvPacket.GetTxTimeStamp() + sendPacket = NTPPacket(version=4,mode=4) + sendPacket.stratum = 2 + sendPacket.poll = 10 + ''' + sendPacket.precision = 0xfa + sendPacket.root_delay = 0x0bfa + sendPacket.root_dispersion = 0x0aa7 + sendPacket.ref_id = 0x808a8c2c + ''' + sendPacket.ref_timestamp = recvTimestamp-5 + sendPacket.SetOriginTimeStamp(timeStamp_high,timeStamp_low) + sendPacket.recv_timestamp = recvTimestamp + sendPacket.tx_timestamp = system_to_ntp_time(time.time()) + socket.sendto(sendPacket.to_data(),addr) + print("Sent to %s:%d" % (addr[0],addr[1])) + except queue.Empty: + continue + + +listenIp = "0.0.0.0" +listenPort = 123 +socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) +socket.bind((listenIp,listenPort)) +print("local socket: ", socket.getsockname()); +recvThread = RecvThread(socket) +recvThread.start() +workThread = WorkThread(socket) +workThread.start() + +while True: + try: + time.sleep(0.5) + except KeyboardInterrupt: + print("Exiting...") + stopFlag = True + recvThread.join() + workThread.join() + #socket.close() + print("Exited") + break + diff --git a/net_orc/network/modules/ovs/bin/start_network_service b/net_orc/network/modules/ovs/bin/start_network_service new file mode 100644 index 000000000..7c38f484a --- /dev/null +++ b/net_orc/network/modules/ovs/bin/start_network_service @@ -0,0 +1,22 @@ +#!/bin/bash -e + +if [[ "$EUID" -ne 0 ]]; then + echo "Must run as root." + exit 1 +fi + +asyncRun() { + "$@" & + pid="$!" + trap "echo 'Stopping PID $pid'; kill -SIGTERM $pid" SIGINT SIGTERM + + # A signal emitted while waiting will make the wait command return code > 128 + # Let's wrap it in a loop that doesn't end before the process is indeed stopped + while kill -0 $pid > /dev/null 2>&1; do + wait + done +} + +# -u flag allows python print statements +# to be logged by docker by running unbuffered +asyncRun exec python3 -u /ovs/python/src/run.py \ No newline at end of file diff --git a/net_orc/network/modules/ovs/conf/module_config.json b/net_orc/network/modules/ovs/conf/module_config.json new file mode 100644 index 000000000..f6a1eff50 --- /dev/null +++ b/net_orc/network/modules/ovs/conf/module_config.json @@ -0,0 +1,23 @@ +{ + "config": { + "meta": { + "name": "ovs", + "display_name": "OVS", + "description": "Setup and configure Open vSwitch" + }, + "network": { + "interface": "veth0", + "enable_wan": false, + "ip_index": 6, + "host": true + }, + "docker": { + "mounts": [ + { + "source": "runtime/network", + "target": "/runtime/network" + } + ] + } + } +} \ No newline at end of file diff --git a/net_orc/network/modules/ovs/ovs.Dockerfile b/net_orc/network/modules/ovs/ovs.Dockerfile new file mode 100644 index 000000000..cd4710e66 --- /dev/null +++ b/net_orc/network/modules/ovs/ovs.Dockerfile @@ -0,0 +1,20 @@ +# Image name: test-run/orchestrator +FROM test-run/base:latest + +#Update and get all additional requirements not contained in the base image +RUN apt-get update --fix-missing + +#Install openvswitch +RUN apt-get install -y openvswitch-switch + +# Copy over all configuration files +COPY network/modules/ovs/conf /testrun/conf + +# Copy over all binary files +COPY network/modules/ovs/bin /testrun/bin + +# Copy over all python files +COPY network/modules/ovs/python /testrun/python + +#Install all python requirements for the module +RUN pip3 install -r /testrun/python/requirements.txt \ No newline at end of file diff --git a/net_orc/network/modules/ovs/python/requirements.txt b/net_orc/network/modules/ovs/python/requirements.txt new file mode 100644 index 000000000..e69de29bb diff --git a/net_orc/network/modules/ovs/python/src/logger.py b/net_orc/network/modules/ovs/python/src/logger.py new file mode 100644 index 000000000..50dfb4f50 --- /dev/null +++ b/net_orc/network/modules/ovs/python/src/logger.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 + +import logging +import os +import sys + +LOGGERS = {} +_LOG_FORMAT = "%(asctime)s %(name)-8s %(levelname)-7s %(message)s" +_DATE_FORMAT = '%b %02d %H:%M:%S' + +# Set level to debug if set as runtime flag +logging.basicConfig(format=_LOG_FORMAT, datefmt=_DATE_FORMAT, level=logging.INFO) + +def get_logger(name): + if name not in LOGGERS: + LOGGERS[name] = logging.getLogger(name) + return LOGGERS[name] \ No newline at end of file diff --git a/net_orc/network/modules/ovs/python/src/ovs_control.py b/net_orc/network/modules/ovs/python/src/ovs_control.py new file mode 100644 index 000000000..6647dc89e --- /dev/null +++ b/net_orc/network/modules/ovs/python/src/ovs_control.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 + +#import ipaddress +import json +import logger +#import os +import util + +CONFIG_FILE = "/ovs/conf/system.json" +DEVICE_BRIDGE = "tr-d" +INTERNET_BRIDGE = "tr-c" +LOGGER = logger.get_logger('ovs_ctrl') + +class OVSControl: + + def __init__(self): + self._int_intf = None + self._dev_intf = None + self._load_config() + + def add_bridge(self,bridgeName): + LOGGER.info("Adding OVS Bridge: " + bridgeName) + # Create the bridge using ovs-vsctl commands + # Uses the --may-exist option to prevent failures + # if this bridge already exists by this name it won't fail + # and will not modify the existing bridge + success=util.run_command("ovs-vsctl --may-exist add-br " + bridgeName) + return success + + def add_port(self,port, bridgeName): + LOGGER.info("Adding Port " + port + " to OVS Bridge: " + bridgeName) + # Add a port to the bridge using ovs-vsctl commands + # Uses the --may-exist option to prevent failures + # if this port already exists on the bridge and will not + # modify the existing bridge + success=util.run_command("ovs-vsctl --may-exist add-port " + bridgeName + " " + port) + return success + + def create_net(self): + LOGGER.info("Creating baseline network") + + # Create data plane + self.add_bridge(DEVICE_BRIDGE) + + # Create control plane + self.add_bridge(INTERNET_BRIDGE) + + # Remove IP from internet adapter + self.set_interface_ip(self._int_intf,"0.0.0.0") + + # Add external interfaces to data and control plane + self.add_port(self._dev_intf,DEVICE_BRIDGE) + self.add_port(self._int_intf,INTERNET_BRIDGE) + + # # Set ports up + self.set_bridge_up(DEVICE_BRIDGE) + self.set_bridge_up(INTERNET_BRIDGE) + + def delete_bridge(self,bridgeName): + LOGGER.info("Deleting OVS Bridge: " + bridgeName) + # Delete the bridge using ovs-vsctl commands + # Uses the --if-exists option to prevent failures + # if this bridge does not exists + success=util.run_command("ovs-vsctl --if-exists del-br " + bridgeName) + return success + + def _load_config(self): + LOGGER.info("Loading Configuration: " + CONFIG_FILE) + config_json = json.load(open(CONFIG_FILE, 'r')) + self._int_intf = config_json['internet_intf'] + self._dev_intf = config_json['device_intf'] + LOGGER.info("Configuration Loaded") + LOGGER.info("Internet Interface: " + self._int_intf) + LOGGER.info("Device Interface: " + self._dev_intf) + + def restore_net(self): + LOGGER.info("Restoring Network...") + # Delete data plane + self.delete_bridge(DEVICE_BRIDGE) + + # Delete control plane + self.delete_bridge(INTERNET_BRIDGE) + + LOGGER.info("Network is restored") + + def show_config(self): + LOGGER.info("Show current config of OVS") + success=util.run_command("ovs-vsctl show") + return success + + def set_bridge_up(self,bridgeName): + LOGGER.info("Setting Bridge device to up state: " + bridgeName) + success=util.run_command("ip link set dev " + bridgeName + " up") + return success + + def set_interface_ip(self,interface, ipAddr): + LOGGER.info("Setting interface " + interface + " to " + ipAddr) + # Remove IP from internet adapter + util.run_command("ifconfig " + interface + " 0.0.0.0") + +if __name__ == '__main__': + ovs = OVSControl() + ovs.create_net() + ovs.show_config() + ovs.restore_net() + ovs.show_config() + diff --git a/net_orc/network/modules/ovs/python/src/run.py b/net_orc/network/modules/ovs/python/src/run.py new file mode 100644 index 000000000..4c1474e74 --- /dev/null +++ b/net_orc/network/modules/ovs/python/src/run.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import logger +import signal +import time + +from ovs_control import OVSControl + +LOGGER = logger.get_logger('ovs_control_run') + +class OVSControlRun: + + def __init__(self): + + signal.signal(signal.SIGINT, self.handler) + signal.signal(signal.SIGTERM, self.handler) + signal.signal(signal.SIGABRT, self.handler) + signal.signal(signal.SIGQUIT, self.handler) + + LOGGER.info("Starting OVS Control") + + # Get all components ready + self._ovs_control = OVSControl() + + self._ovs_control.restore_net() + + self._ovs_control.create_net() + + self._ovs_control.show_config() + + # Get network ready (via Network orchestrator) + LOGGER.info("Network is ready. Waiting for device information...") + + #Loop forever until process is stopped + while True: + LOGGER.info("OVS Running") + time.sleep(1000) + + # TODO: This time should be configurable (How long to hold before exiting, this could be infinite too) + #time.sleep(300) + + # Tear down network + #self._ovs_control.shutdown() + + def handler(self, signum, frame): + LOGGER.info("SigtermEnum: " + str(signal.SIGTERM)) + LOGGER.info("Exit signal received: " + str(signum)) + if (signum == 2 or signal == signal.SIGTERM): + LOGGER.info("Exit signal received. Restoring network...") + self._ovs_control.shutdown() + exit(1) + +ovs = OVSControlRun() diff --git a/net_orc/network/modules/ovs/python/src/util.py b/net_orc/network/modules/ovs/python/src/util.py new file mode 100644 index 000000000..8bb0439bc --- /dev/null +++ b/net_orc/network/modules/ovs/python/src/util.py @@ -0,0 +1,19 @@ +import subprocess +import logger + + +def run_command(cmd): + success = False + LOGGER = logger.get_logger('util') + process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + if process.returncode !=0: + err_msg = "%s. Code: %s" % (stderr.strip(), process.returncode) + LOGGER.error("Command Failed: " + cmd) + LOGGER.error("Error: " + err_msg) + else: + succ_msg = "%s. Code: %s" % (stdout.strip().decode('utf-8'), process.returncode) + LOGGER.info("Command Success: " + cmd) + LOGGER.info("Success: " + succ_msg) + success = True + return success \ No newline at end of file diff --git a/net_orc/network/modules/radius/bin/start_network_service b/net_orc/network/modules/radius/bin/start_network_service new file mode 100644 index 000000000..e27a828dd --- /dev/null +++ b/net_orc/network/modules/radius/bin/start_network_service @@ -0,0 +1,20 @@ +#!/bin/bash + +PYTHON_SRC_DIR=/testrun/python/src +CONF_DIR="/testrun/conf" +LOG_FILE="/runtime/network/radius.log" + +echo Starting authenticator.py + +cp $CONF_DIR/eap /etc/freeradius/3.0/mods-available/eap + +# Do we want to mount resources/network/{module} to the network module to avoid file copying during build? +cp $CONF_DIR/ca.crt /etc/ssl/certs/ca-certificates.crt + +python3 -u $PYTHON_SRC_DIR/authenticator.py & + +#Create and set permissions on the log file +touch $LOG_FILE +chown $HOST_USER:$HOST_USER $LOG_FILE + +freeradius -f -X &> $LOG_FILE \ No newline at end of file diff --git a/net_orc/network/modules/radius/conf/ca.crt b/net_orc/network/modules/radius/conf/ca.crt new file mode 100644 index 000000000..d009cb1ab --- /dev/null +++ b/net_orc/network/modules/radius/conf/ca.crt @@ -0,0 +1,26 @@ +-----BEGIN CERTIFICATE----- +MIIEYTCCA0mgAwIBAgIUQJ4F8hBCnCp7ASPZqG/tNQgoUR4wDQYJKoZIhvcNAQEL +BQAwgb8xCzAJBgNVBAYTAkdCMRswGQYDVQQIDBIbWzN+TGVpY2VzdGVyc2hpcmUx +FTATBgNVBAcMDExvdWdoYm9yb3VnaDEUMBIGA1UECgwLRm9yZXN0IFJvY2sxDjAM +BgNVBAsMBUN5YmVyMR8wHQYDVQQDDBZjeWJlci5mb3Jlc3Ryb2NrLmNvLnVrMTUw +MwYJKoZIhvcNAQkBFiZjeWJlcnNlY3VyaXR5LnRlc3RpbmdAZm9yZXN0cm9jay5j +by51azAeFw0yMjAzMDQxMjEzMTBaFw0yNzAzMDMxMjEzMTBaMIG/MQswCQYDVQQG +EwJHQjEbMBkGA1UECAwSG1szfkxlaWNlc3RlcnNoaXJlMRUwEwYDVQQHDAxMb3Vn +aGJvcm91Z2gxFDASBgNVBAoMC0ZvcmVzdCBSb2NrMQ4wDAYDVQQLDAVDeWJlcjEf +MB0GA1UEAwwWY3liZXIuZm9yZXN0cm9jay5jby51azE1MDMGCSqGSIb3DQEJARYm +Y3liZXJzZWN1cml0eS50ZXN0aW5nQGZvcmVzdHJvY2suY28udWswggEiMA0GCSqG +SIb3DQEBAQUAA4IBDwAwggEKAoIBAQDDNz3vJiZ5nX8lohEhqXvxEme3srip8qF7 +r5ScIeQzsTKuPNAmoefx9TcU3SyA2BnREuDX+OCYMN62xxWG2PndOl0LNezAY22C +PJwHbaBntLKY/ZhxYSTyratM7zxKSVLtClamA/bJXBhdfZZKYOP3xlZQEQTygtzK +j5hZwDrpDARtjRZIMWPLqVcoaW9ow2urJVsdD4lYAhpQU2UIgiWo7BG3hJsUfcYX +EQyyrMKJ7xaCwzIU7Sem1PETrzeiWg4KhDijc7A0RMPWlU5ljf0CnY/IZwiDsMRl +hGmGBPvR+ddiWPZPtSKj6TPWpsaMUR9UwncLmSSrhf1otX4Mw0vbAgMBAAGjUzBR +MB0GA1UdDgQWBBR0Qxx2mDTPIfpnzO5YtycGs6t8ijAfBgNVHSMEGDAWgBR0Qxx2 +mDTPIfpnzO5YtycGs6t8ijAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUA +A4IBAQCpTMBMZGXF74WCxrIk23MUsu0OKzMs8B16Wy8BHz+7hInLZwbkx71Z0TP5 +rsMITetSANtM/k4jH7Vmr1xmzU7oSz5zKU1+7rIjKjGtih48WZdJay0uqfKe0K2s +vsRS0LVLY6IiTFWK9YrLC0QFSK7z5GDl1oc/D5yIZAkbsL6PRQJ5RQsYf5BhHfyB +PRV/KcF7c9iKVYW2vILJzbyYLHTDADTHbtfCe5+pAGxagswDjSMVkQu5iJNjbtUO +5iv7PRkgzUFru9Kk6q+LrXbzyPPCwlc3Xbh1q5jSkJLkcV3K26E7+uX5HI+Hxpeh +a8kOsdnw+N8wX6bc7eXIaGBDMine +-----END CERTIFICATE----- diff --git a/net_orc/network/modules/radius/conf/eap b/net_orc/network/modules/radius/conf/eap new file mode 100644 index 000000000..a868f16cd --- /dev/null +++ b/net_orc/network/modules/radius/conf/eap @@ -0,0 +1,602 @@ +eap { + + default_eap_type = tls + + # A list is maintained to correlate EAP-Response + # packets with EAP-Request packets. After a + # configurable length of time, entries in the list + # expire, and are deleted. + # + timer_expire = 60 + + # There are many EAP types, but the server has support + # for only a limited subset. If the server receives + # a request for an EAP type it does not support, then + # it normally rejects the request. By setting this + # configuration to "yes", you can tell the server to + # instead keep processing the request. Another module + # MUST then be configured to proxy the request to + # another RADIUS server which supports that EAP type. + # + # If another module is NOT configured to handle the + # request, then the request will still end up being + # rejected. + # + ignore_unknown_eap_types = no + + # Cisco AP1230B firmware 12.2(13)JA1 has a bug. When given + # a User-Name attribute in an Access-Accept, it copies one + # more byte than it should. + # + # We can work around it by configurably adding an extra + # zero byte. + # + cisco_accounting_username_bug = no + + # Help prevent DoS attacks by limiting the number of + # sessions that the server is tracking. For simplicity, + # this is taken from the "max_requests" directive in + # radiusd.conf. + # + max_sessions = ${max_requests} + + # Common TLS configuration for TLS-based EAP types + # ------------------------------------------------ + # + # See raddb/certs/README.md for additional comments + # on certificates. + # + # If OpenSSL was not found at the time the server was + # built, the "tls", "ttls", and "peap" sections will + # be ignored. + # + # If you do not currently have certificates signed by + # a trusted CA you may use the 'snakeoil' certificates. + # Included with the server in raddb/certs. + # + # If these certificates have not been auto-generated: + # cd raddb/certs + # make + # + # These test certificates SHOULD NOT be used in a normal + # deployment. They are created only to make it easier + # to install the server, and to perform some simple + # tests with EAP-TLS, TTLS, or PEAP. + # + # Note that you should NOT use a globally known CA here! + # e.g. using a Verisign cert as a "known CA" means that + # ANYONE who has a certificate signed by them can + # authenticate via EAP-TLS! This is likely not what you want. + # + tls-config tls-common { + private_key_password = whatever + private_key_file = /etc/ssl/private/ssl-cert-snakeoil.key + + # If Private key & Certificate are located in + # the same file, then private_key_file & + # certificate_file must contain the same file + # name. + # + # If ca_file (below) is not used, then the + # certificate_file below SHOULD also include all of + # the intermediate CA certificates used to sign the + # server certificate, but NOT the root CA. + # + # Including the ROOT CA certificate is not useful and + # merely inflates the exchanged data volume during + # the TLS negotiation. + # + # This file should contain the server certificate, + # followed by intermediate certificates, in order. + # i.e. If we have a server certificate signed by CA1, + # which is signed by CA2, which is signed by a root + # CA, then the "certificate_file" should contain + # server.pem, followed by CA1.pem, followed by + # CA2.pem. + # + # When using "ca_file" or "ca_dir", the + # "certificate_file" should contain only + # "server.pem". And then you may (or may not) need + # to set "auto_chain", depending on your version of + # OpenSSL. + # + # In short, SSL / TLS certificates are complex. + # There are many versions of software, each of which + # behave slightly differently. It is impossible to + # give advice which will work everywhere. Instead, + # we give general guidelines. + # + certificate_file = /etc/ssl/certs/ssl-cert-snakeoil.pem + + # Trusted Root CA list + # + # This file can contain multiple CA certificates. + # ALL of the CA's in this list will be trusted to + # issue client certificates for authentication. + # + # In general, you should use self-signed + # certificates for 802.1x (EAP) authentication. + # In that case, this CA file should contain + # *one* CA certificate. + # + ca_file = /etc/ssl/certs/ca-certificates.crt + + # Check the Certificate Revocation List + # + # 1) Copy CA certificates and CRLs to same directory. + # 2) Execute 'c_rehash '. + # 'c_rehash' is OpenSSL's command. + # 3) uncomment the lines below. + # 5) Restart radiusd + # check_crl = yes + + # Check if intermediate CAs have been revoked. + # check_all_crl = yes + + ca_path = ${cadir} + + # OpenSSL does not reload contents of ca_path dir over time. + # That means that if check_crl is enabled and CRLs are loaded + # from ca_path dir, at some point CRLs will expire and + # RADIUSd will stop authenticating users. + # If ca_path_reload_interval is non-zero, it will force OpenSSL + # to reload all data from ca_path periodically + # + # Flush ca_path each hour + # ca_path_reload_interval = 3600 + + + # Accept an expired Certificate Revocation List + # + # allow_expired_crl = no + + # If check_cert_issuer is set, the value will + # be checked against the DN of the issuer in + # the client certificate. If the values do not + # match, the certificate verification will fail, + # rejecting the user. + # + # This check can be done more generally by checking + # the value of the TLS-Client-Cert-Issuer attribute. + # This check can be done via any mechanism you + # choose. + # + # check_cert_issuer = "/C=GB/ST=Berkshire/L=Newbury/O=My Company Ltd" + + # If check_cert_cn is set, the value will + # be xlat'ed and checked against the CN + # in the client certificate. If the values + # do not match, the certificate verification + # will fail rejecting the user. + # + # This check is done only if the previous + # "check_cert_issuer" is not set, or if + # the check succeeds. + # + # This check can be done more generally by writing + # "unlang" statements to examine the value of the + # TLS-Client-Cert-Common-Name attribute. + # + # check_cert_cn = %{User-Name} + + # + # This configuration item only applies when there is + # an intermediate CA between the "root" CA, and the + # client certificate. If we trust the root CA, then + # by definition we also trust ANY intermediate CA + # which is signed by that root. This means ANOTHER + # intermediate CA can issue client certificates, and + # have them accepted by the EAP module. + # + # The solution is to list ONLY the trusted CAs in the + # FreeRADIUS configuration, and then set this + # configuration item to "yes". + # + # Then, when the server receives a client certificate + # from an untrusted CA, that authentication request + # can be rejected. + # + # It is possible to do these checks in "unlang", by + # checking for unknown names in the + # TLS-Cert-Common-Name attribute, but that is + # more complex. So we add a configuration option + # which can be set once, and which works for all + # possible intermediate CAs, no matter what their + # value. + # + # reject_unknown_intermediate_ca = no + + # Set this option to specify the allowed + # TLS cipher suites. The format is listed + # in "man 1 ciphers". + # + cipher_list = "DEFAULT" + + # If enabled, OpenSSL will use server cipher list + # (possibly defined by cipher_list option above) + # for choosing right cipher suite rather than + # using client-specified list which is OpenSSl default + # behavior. Setting this to "yes" means that OpenSSL + # will choose the servers ciphers, even if they do not + # best match what the client sends. + # + # TLS negotiation is usually good, but can be imperfect. + # This setting allows administrators to "fine tune" it + # if necessary. + # + cipher_server_preference = no + + # You can selectively disable TLS versions for + # compatability with old client devices. + # + # If your system has OpenSSL 1.1.0 or greater, do NOT + # use these. Instead, set tls_min_version and + # tls_max_version. + # +# disable_tlsv1_2 = yes +# disable_tlsv1_1 = yes +# disable_tlsv1 = yes + + + # Set min / max TLS version. + # + # Generally speaking you should NOT use TLS 1.0 or + # TLS 1.1. They are old, possibly insecure, and + # deprecated. However, it is sometimes necessary to + # enable it for compatibility with legact systems. + # We recommend replacing those legacy systems, and + # using at least TLS 1.2. + # + # Some Debian versions disable older versions of TLS, + # and requires the application to manually enable + # them. + # + # If you are running such a distribution, you should + # set these options, otherwise older clients will not + # be able to connect. + # + # Allowed values are "1.0", "1.1", "1.2", and "1.3". + # + # As of 2021, it is STRONGLY RECOMMENDED to set + # + # tls_min_version = "1.2" + # + # Older TLS versions are insecure and deprecated. + # + # In order to enable TLS 1.0 and TLS 1.1, you may + # also need to update cipher_list below to: + # + # * OpenSSL >= 3.x + # + # cipher_list = "DEFAULT@SECLEVEL=0" + # + # * OpenSSL < 3.x + # + # cipher_list = "DEFAULT@SECLEVEL=1" + # + # The values must be in quotes. + # + # We also STRONGLY RECOMMEND to set + # + # tls_max_version = "1.2" + # + # While the server will accept "1.3" as a value, + # most EAP supplicants WILL NOT DO TLS 1.3 PROPERLY. + # + # i.e. they WILL NOT WORK, SO DO NOT ASK QUESTIONS ON + # THE LIST ABOUT WHY IT DOES NOT WORK. + # + # The TLS 1.3 support is here for future + # compatibility, as clients get upgraded, and people + # don't upgrade their copies of FreeRADIUS. + # + # Also note that we only support TLS 1.3 for EAP-TLS. + # Other versions of EAP (PEAP, TTLS, FAST) DO NOT + # SUPPORT TLS 1.3. + # + tls_min_version = "1.2" + tls_max_version = "1.2" + + # Elliptical cryptography configuration + # + # This configuration should be one of the following: + # + # * a name of the curve to use, e.g. "prime256v1". + # + # * a colon separated list of curve NIDs or names. + # + # * an empty string, in which case OpenSSL will choose + # the "best" curve for the situation. + # + # For supported curve names, please run + # + # openssl ecparam -list_curves + # + ecdh_curve = "" + + # Session resumption / fast reauthentication + # cache. + # + # The cache contains the following information: + # + # session Id - unique identifier, managed by SSL + # User-Name - from the Access-Accept + # Stripped-User-Name - from the Access-Request + # Cached-Session-Policy - from the Access-Accept + # + # See also the "store" subsection below for + # additional attributes which can be cached. + # + # The "Cached-Session-Policy" is the name of a + # policy which should be applied to the cached + # session. This policy can be used to assign + # VLANs, IP addresses, etc. It serves as a useful + # way to re-apply the policy from the original + # Access-Accept to the subsequent Access-Accept + # for the cached session. + # + # On session resumption, these attributes are + # copied from the cache, and placed into the + # reply list. + # + # You probably also want "use_tunneled_reply = yes" + # when using fast session resumption. + # + # You can check if a session has been resumed by + # looking for the existence of the EAP-Session-Resumed + # attribute. Note that this attribute will *only* + # exist in the "post-auth" section. + # + # CAVEATS: The cache is stored and reloaded BEFORE + # the "post-auth" section is run. This limitation + # makes caching more difficult than it should be. In + # practice, it means that the first authentication + # session must set the reply attributes before the + # post-auth section is run. + # + # When the session is resumed, the attributes are + # restored and placed into the session-state list. + # + cache { + # Enable it. The default is "no". Deleting the entire "cache" + # subsection also disables caching. + # + # The session cache requires the use of the + # "name" and "persist_dir" configuration + # items, below. + # + # The internal OpenSSL session cache has been permanently + # disabled. + # + # You can disallow resumption for a particular user by adding the + # following attribute to the control item list: + # + # Allow-Session-Resumption = No + # + # If "enable = no" below, you CANNOT enable resumption for just one + # user by setting the above attribute to "yes". + # + enable = no + + # Lifetime of the cached entries, in hours. The sessions will be + # deleted/invalidated after this time. + # + lifetime = 24 # hours + + # Internal "name" of the session cache. Used to + # distinguish which TLS context sessions belong to. + # + # The server will generate a random value if unset. + # This will change across server restart so you MUST + # set the "name" if you want to persist sessions (see + # below). + # + # name = "EAP module" + + # Simple directory-based storage of sessions. + # Two files per session will be written, the SSL + # state and the cached VPs. This will persist session + # across server restarts. + # + # The default directory is ${logdir}, for historical + # reasons. You should ${db_dir} instead. And check + # the value of db_dir in the main radiusd.conf file. + # It should not point to ${raddb} + # + # The server will need write perms, and the directory + # should be secured from anyone else. You might want + # a script to remove old files from here periodically: + # + # find ${logdir}/tlscache -mtime +2 -exec rm -f {} \; + # + # This feature REQUIRES "name" option be set above. + # + # persist_dir = "${logdir}/tlscache" + + # + # As of 3.0.20, it is possible to partially + # control which attributes exist in the + # session cache. This subsection lists + # attributes which are taken from the reply, + # and saved to the on-disk cache. When the + # session is resumed, these attributes are + # added to the "session-state" list. The + # default configuration will then take care + # of copying them to the reply. + # + store { + Tunnel-Private-Group-Id + } + } + + # Client certificates can be validated via an + # external command. This allows dynamic CRLs or OCSP + # to be used. + # + # This configuration is commented out in the + # default configuration. Uncomment it, and configure + # the correct paths below to enable it. + # + # If OCSP checking is enabled, and the OCSP checks fail, + # the verify section is not run. + # + # If OCSP checking is disabled, the verify section is + # run on successful certificate validation. + # + verify { + # If the OCSP checks succeed, the verify section + # is run to allow additional checks. + # + # If you want to skip verify on OCSP success, + # uncomment this configuration item, and set it + # to "yes". + # + # skip_if_ocsp_ok = no + + # A temporary directory where the client + # certificates are stored. This directory + # MUST be owned by the UID of the server, + # and MUST not be accessible by any other + # users. When the server starts, it will do + # "chmod go-rwx" on the directory, for + # security reasons. The directory MUST + # exist when the server starts. + # + # You should also delete all of the files + # in the directory when the server starts. + # + # tmpdir = /tmp/radiusd + + # The command used to verify the client cert. + # We recommend using the OpenSSL command-line + # tool. + # + # The ${..ca_path} text is a reference to + # the ca_path variable defined above. + # + # The %{TLS-Client-Cert-Filename} is the name + # of the temporary file containing the cert + # in PEM format. This file is automatically + # deleted by the server when the command + # returns. + # + # client = "/path/to/openssl verify -CApath ${..ca_path} %{TLS-Client-Cert-Filename}" + } + + # OCSP Configuration + # + # Certificates can be verified against an OCSP + # Responder. This makes it possible to immediately + # revoke certificates without the distribution of + # new Certificate Revocation Lists (CRLs). + # + ocsp { + # Enable it. The default is "no". + # Deleting the entire "ocsp" subsection + # also disables ocsp checking + # + enable = no + + # The OCSP Responder URL can be automatically + # extracted from the certificate in question. + # To override the OCSP Responder URL set + # "override_cert_url = yes". + # + override_cert_url = yes + + # If the OCSP Responder address is not extracted from + # the certificate, the URL can be defined here. + # + url = "http://127.0.0.1/ocsp/" + + # If the OCSP Responder can not cope with nonce + # in the request, then it can be disabled here. + # + # For security reasons, disabling this option + # is not recommended as nonce protects against + # replay attacks. + # + # Note that Microsoft AD Certificate Services OCSP + # Responder does not enable nonce by default. It is + # more secure to enable nonce on the responder than + # to disable it in the query here. + # See http://technet.microsoft.com/en-us/library/cc770413%28WS.10%29.aspx + # + # use_nonce = yes + + # Number of seconds before giving up waiting + # for OCSP response. 0 uses system default. + # + # timeout = 0 + + # Normally an error in querying the OCSP + # responder (no response from server, server did + # not understand the request, etc) will result in + # a validation failure. + # + # To treat these errors as 'soft' failures and + # still accept the certificate, enable this + # option. + # + # Warning: this may enable clients with revoked + # certificates to connect if the OCSP responder + # is not available. Use with caution. + # + # softfail = no + } + + # + # The server can present different certificates based + # on the realm presented in EAP. See + # raddb/certs/realms/README.md for examples of how to + # configure this. + # + # Note that the default is to use the same set of + # realm certificates for both EAP and RadSec! If + # this is not what you want, you should use different + # subdirectories or each, e.g. ${certdir}/realms/radsec/, + # and ${certdir}/realms/eap/ + # + # realm_dir = ${certdir}/realms/ + } + + # EAP-TLS + # + # The TLS configuration for TLS-based EAP types is held in + # the "tls-config" section, above. + # + tls { + # Point to the common TLS configuration + # + tls = tls-common + + # As part of checking a client certificate, the EAP-TLS + # sets some attributes such as TLS-Client-Cert-Common-Name. This + # virtual server has access to these attributes, and can + # be used to accept or reject the request. + # + # virtual_server = check-eap-tls + + # You can control whether or not EAP-TLS requires a + # client certificate by setting + # + # configurable_client_cert = yes + # + # Once that setting has been changed, you can then set + # + # EAP-TLS-Require-Client-Cert = No + # + # in the control items for a request, and the EAP-TLS + # module will not require a client certificate from + # the supplicant. + # + # WARNING: This configuration should only be used + # when the users are placed into a "captive portal" + # or "walled garden", where they have limited network + # access. Otherwise the configuraton will allow + # anyone on the network, without authenticating them! + # +# configurable_client_cert = no + } + +} diff --git a/net_orc/network/modules/radius/conf/module_config.json b/net_orc/network/modules/radius/conf/module_config.json new file mode 100644 index 000000000..153d951df --- /dev/null +++ b/net_orc/network/modules/radius/conf/module_config.json @@ -0,0 +1,22 @@ +{ + "config": { + "meta": { + "name": "radius", + "display_name": "Radius", + "description": "Enable port based authentication" + }, + "network": { + "interface": "veth0", + "enable_wan": false, + "ip_index": 7 + }, + "docker": { + "mounts": [ + { + "source": "runtime/network", + "target": "/runtime/network" + } + ] + } + } +} \ No newline at end of file diff --git a/net_orc/network/modules/radius/python/requirements.txt b/net_orc/network/modules/radius/python/requirements.txt new file mode 100644 index 000000000..37d126cb1 --- /dev/null +++ b/net_orc/network/modules/radius/python/requirements.txt @@ -0,0 +1,3 @@ +eventlet +pbr +transitions \ No newline at end of file diff --git a/net_orc/network/modules/radius/python/src/authenticator.py b/net_orc/network/modules/radius/python/src/authenticator.py new file mode 100644 index 000000000..55fa51d87 --- /dev/null +++ b/net_orc/network/modules/radius/python/src/authenticator.py @@ -0,0 +1,31 @@ +from chewie.chewie import Chewie +import logging + +_LOG_FORMAT = "%(asctime)s %(name)-8s %(levelname)-7s %(message)s" +_DATE_FORMAT = '%b %02d %H:%M:%S' +INTERFACE_NAME="veth0" +RADIUS_SERVER_IP="127.0.0.1" +RADIUS_SERVER_PORT=1812 +RADIUS_SERVER_SECRET="testing123" + +class Authenticator(): + + def __init__(self): + self.chewie = Chewie(INTERFACE_NAME, self._get_logger(), self._auth_handler, self._failure_handler, self._logoff_handler, radius_server_ip=RADIUS_SERVER_IP, radius_server_port=RADIUS_SERVER_PORT, radius_server_secret=RADIUS_SERVER_SECRET) + self.chewie.run() + + def _get_logger(self): + logging.basicConfig(format=_LOG_FORMAT, datefmt=_DATE_FORMAT, level=logging.INFO) + logger = logging.getLogger("chewie") + return logger + + def _auth_handler(self, address, group_address, *args, **kwargs): + print("Successful auth for " + str(address) + " on port " + str(group_address)) + + def _failure_handler(self, address, group_address): + print("Failed auth for " + str(address) + " on port " + str(group_address)) + + def _logoff_handler(self, address, group_address): + print("Log off reported for " + str(address) + " on port " + str(group_address)) + +authenticator = Authenticator() \ No newline at end of file diff --git a/net_orc/network/modules/radius/radius.Dockerfile b/net_orc/network/modules/radius/radius.Dockerfile new file mode 100644 index 000000000..a72313826 --- /dev/null +++ b/net_orc/network/modules/radius/radius.Dockerfile @@ -0,0 +1,26 @@ +# Image name: test-run/radius +FROM test-run/base:latest + +# Install radius and git +RUN apt-get update && apt-get install -y openssl freeradius git + +# Clone chewie from source. +RUN git clone --branch 0.0.25 https://github.com/faucetsdn/chewie + +# Install chewie as Python module +RUN pip3 install chewie/ + +EXPOSE 1812/udp +EXPOSE 1813/udp + +# Copy over all configuration files +COPY network/modules/radius/conf /testrun/conf + +# Copy over all binary files +COPY network/modules/radius/bin /testrun/bin + +# Copy over all python files +COPY network/modules/radius/python /testrun/python + +# Install all python requirements for the module +RUN pip3 install -r /testrun/python/requirements.txt \ No newline at end of file diff --git a/net_orc/network/modules/template/bin/start_network_service b/net_orc/network/modules/template/bin/start_network_service new file mode 100644 index 000000000..94ae0def9 --- /dev/null +++ b/net_orc/network/modules/template/bin/start_network_service @@ -0,0 +1,13 @@ +#!/bin/bash + +# Place holder function for testing and validation +# Each network module should include a start_networkig_service +# file that overwrites this one to boot all of the its specific +# requirements to run. + +echo "Starting network service..." +echo "This is not a real network service, just a test" +echo "Network service started" + +# Do Nothing, just keep the module alive +while true; do sleep 1; done \ No newline at end of file diff --git a/net_orc/network/modules/template/conf/module_config.json b/net_orc/network/modules/template/conf/module_config.json new file mode 100644 index 000000000..bcea3808e --- /dev/null +++ b/net_orc/network/modules/template/conf/module_config.json @@ -0,0 +1,26 @@ +{ + "config": { + "meta": { + "name": "template", + "display_name": "Template", + "description": "Template for building network service modules" + }, + "network": { + "interface": "veth0", + "enable_wan": false, + "ip_index": 9 + }, + "grpc": { + "port": 50001 + }, + "docker": { + "enable_container": false, + "mounts": [ + { + "source": "runtime/network", + "target": "/runtime/network" + } + ] + } + } +} \ No newline at end of file diff --git a/net_orc/network/modules/template/python/src/template_main.py b/net_orc/network/modules/template/python/src/template_main.py new file mode 100644 index 000000000..50c425c23 --- /dev/null +++ b/net_orc/network/modules/template/python/src/template_main.py @@ -0,0 +1,4 @@ +"""Python code for the template module.""" + +if __name__ == "__main__": + print ("Template main") diff --git a/net_orc/network/modules/template/template.Dockerfile b/net_orc/network/modules/template/template.Dockerfile new file mode 100644 index 000000000..54bfb9628 --- /dev/null +++ b/net_orc/network/modules/template/template.Dockerfile @@ -0,0 +1,11 @@ +# Image name: test-run/dhcp-primary +FROM test-run/base:latest + +# Copy over all configuration files +COPY network/modules/template/conf /testrun/conf + +# Load device binary files +COPY network/modules/template/bin /testrun/bin + +# Copy over all python files +COPY network/modules/template/python /testrun/python \ No newline at end of file diff --git a/net_orc/orchestrator.Dockerfile b/net_orc/orchestrator.Dockerfile new file mode 100644 index 000000000..f062a33d4 --- /dev/null +++ b/net_orc/orchestrator.Dockerfile @@ -0,0 +1,22 @@ +# Image name: test-run/orchestrator +FROM test-run/base:latest + +#Update and get all additional requirements not contained in the base image +RUN apt-get update + +RUN apt-get install -y python3-pip curl openvswitch-switch + +#Download and install docker client +ENV DOCKERVERSION=20.10.2 +RUN curl -fsSLO https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKERVERSION}.tgz \ + && tar xzvf docker-${DOCKERVERSION}.tgz --strip 1 -C /usr/local/bin docker/docker \ + && rm docker-${DOCKERVERSION}.tgz + +#Create a directory to load all the app files into +RUN mkdir /python + +#Load the requirements file +COPY python/requirements.txt /python + +#Install all python requirements for the module +RUN pip3 install -r python/requirements.txt diff --git a/net_orc/python/requirements.txt b/net_orc/python/requirements.txt new file mode 100644 index 000000000..5d8f29214 --- /dev/null +++ b/net_orc/python/requirements.txt @@ -0,0 +1,4 @@ +docker +ipaddress +netifaces +scapy \ No newline at end of file diff --git a/net_orc/python/src/listener.py b/net_orc/python/src/listener.py new file mode 100644 index 000000000..d07de4686 --- /dev/null +++ b/net_orc/python/src/listener.py @@ -0,0 +1,68 @@ +"""Intercepts network traffic between network services and the device +under test.""" +from scapy.all import AsyncSniffer, DHCP, get_if_hwaddr +import logger +from network_event import NetworkEvent + +LOGGER = logger.get_logger('listener') + +DHCP_DISCOVER = 1 +DHCP_OFFER = 2 +DHCP_REQUEST = 3 +DHCP_ACK = 5 +CONTAINER_MAC_PREFIX = '9a:02:57:1e:8f' + + +class Listener: + """Methods to start and stop the network listener.""" + + def __init__(self, device_intf): + self._device_intf = device_intf + self._device_intf_mac = get_if_hwaddr(self._device_intf) + + self._sniffer = AsyncSniffer( + iface=self._device_intf, prn=self._packet_callback) + + self._callbacks = [] + self._discovered_devices = [] + + def start_listener(self): + """Start sniffing packets on the device interface.""" + self._sniffer.start() + + def stop_listener(self): + """Stop sniffing packets on the device interface.""" + self._sniffer.stop() + + def is_running(self): + """Determine whether the sniffer is running.""" + return self._sniffer.running + + def register_callback(self, callback, events=[]): # pylint: disable=dangerous-default-value + """Register a callback for specified events.""" + self._callbacks.append( + { + 'callback': callback, + 'events': events + } + ) + + def _packet_callback(self, packet): + + # Ignore packets originating from our containers + if packet.src.startswith(CONTAINER_MAC_PREFIX) or packet.src == self._device_intf_mac: + return + + if not packet.src is None and packet.src not in self._discovered_devices: + self._device_discovered(packet.src) + + def _get_dhcp_type(self, packet): + return packet[DHCP].options[0][1] + + def _device_discovered(self, mac_addr): + LOGGER.debug(f'Discovered device with address {mac_addr}') + self._discovered_devices.append(mac_addr) + + for callback in self._callbacks: + if NetworkEvent.DEVICE_DISCOVERED in callback['events']: + callback['callback'](mac_addr) diff --git a/net_orc/python/src/logger.py b/net_orc/python/src/logger.py new file mode 100644 index 000000000..e930f1953 --- /dev/null +++ b/net_orc/python/src/logger.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import json +import logging +import os + +LOGGERS = {} +_LOG_FORMAT = "%(asctime)s %(name)-8s %(levelname)-7s %(message)s" +_DATE_FORMAT = '%b %02d %H:%M:%S' +_DEFAULT_LEVEL = logging.INFO +_CONF_DIR="conf" +_CONF_FILE_NAME="system.json" + +# Set log level +try: + system_conf_json = json.load(open(os.path.join(_CONF_DIR, _CONF_FILE_NAME), encoding='UTF-8')) + log_level_str = system_conf_json['log_level'] + LOG_LEVEL = logging.getLevelName(log_level_str) +except OSError: + LOG_LEVEL = _DEFAULT_LEVEL + +logging.basicConfig(format=_LOG_FORMAT, datefmt=_DATE_FORMAT, level=LOG_LEVEL) + +def get_logger(name): + if name not in LOGGERS: + LOGGERS[name] = logging.getLogger(name) + return LOGGERS[name] diff --git a/net_orc/python/src/network_event.py b/net_orc/python/src/network_event.py new file mode 100644 index 000000000..c77dfa706 --- /dev/null +++ b/net_orc/python/src/network_event.py @@ -0,0 +1,10 @@ +"""Specify the various types of network events to be reported.""" +from enum import Enum + +class NetworkEvent(Enum): + """All possible network events.""" + + ALL = 0 + DEVICE_DISCOVERED = 1 + DHCP_LEASE_NEW = 2 + DHCP_LEASE_RENEWED = 3 diff --git a/net_orc/python/src/network_orchestrator.py b/net_orc/python/src/network_orchestrator.py new file mode 100644 index 000000000..828ad58a7 --- /dev/null +++ b/net_orc/python/src/network_orchestrator.py @@ -0,0 +1,573 @@ +#!/usr/bin/env python3 + +import ipaddress +import json +import os +import shutil +import sys +import time +import threading + +import docker +from docker.types import Mount + +import logger +import util +from listener import Listener +from network_validator import NetworkValidator + +LOGGER = logger.get_logger("net_orc") +CONFIG_FILE = "conf/system.json" +EXAMPLE_CONFIG_FILE = "conf/system.json.example" +RUNTIME_DIR = "runtime/network" +NETWORK_MODULES_DIR = "network/modules" +NETWORK_MODULE_METADATA = "conf/module_config.json" +DEVICE_BRIDGE = "tr-d" +INTERNET_BRIDGE = "tr-c" +PRIVATE_DOCKER_NET = "tr-private-net" +CONTAINER_NAME = "network_orchestrator" +RUNTIME = 300 + + +class NetworkOrchestrator: + """Manage and controls a virtual testing network.""" + + def __init__(self, config_file=CONFIG_FILE, validate=True, async_monitor=False): + self._int_intf = None + self._dev_intf = None + + self.listener = None + + self._net_modules = [] + + self.validate = validate + + self.async_monitor = async_monitor + + self._path = os.path.dirname(os.path.dirname( + os.path.dirname(os.path.realpath(__file__)))) + + self.validator = NetworkValidator() + + shutil.rmtree(os.path.join(os.getcwd(), RUNTIME_DIR), ignore_errors=True) + + self.network_config = NetworkConfig() + + self.load_config(config_file) + + def start(self): + """Start the network orchestrator.""" + + LOGGER.info("Starting Network Orchestrator") + # Get all components ready + self.load_network_modules() + + # Restore the network first if required + self.stop(kill=True) + + self.start_network() + + if self.async_monitor: + # Run the monitor method asynchronously to keep this method non-blocking + self._monitor_thread = threading.Thread( + target=self.monitor_network) + self._monitor_thread.daemon = True + self._monitor_thread.start() + else: + self.monitor_network() + + def start_network(self): + """Start the virtual testing network.""" + LOGGER.info("Starting network") + + self.build_network_modules() + self.create_net() + self.start_network_services() + + if self.validate: + # Start the validator after network is ready + self.validator.start() + + # Get network ready (via Network orchestrator) + LOGGER.info("Network is ready.") + + def stop(self, kill=False): + """Stop the network orchestrator.""" + self.stop_validator(kill=kill) + self.stop_network(kill=kill) + + def stop_validator(self, kill=False): + """Stop the network validator.""" + # Shutdown the validator + self.validator.stop(kill=kill) + + def stop_network(self, kill=False): + """Stop the virtual testing network.""" + # Shutdown network + self.stop_networking_services(kill=kill) + self.restore_net() + + def monitor_network(self): + # TODO: This time should be configurable (How long to hold before exiting, this could be infinite too) + time.sleep(RUNTIME) + + self.stop() + + def load_config(self,config_file=None): + if config_file is None: + # If not defined, use relative pathing to local file + self._config_file=os.path.join(self._path, CONFIG_FILE) + else: + # If defined, use as provided + self._config_file=config_file + + if not os.path.isfile(self._config_file): + LOGGER.error("Configuration file is not present at " + config_file) + LOGGER.info("An example is present in " + EXAMPLE_CONFIG_FILE) + sys.exit(1) + + LOGGER.info("Loading config file: " + os.path.abspath(self._config_file)) + with open(self._config_file, encoding='UTF-8') as config_json_file: + config_json = json.load(config_json_file) + self.import_config(config_json) + + def import_config(self, json_config): + self._int_intf = json_config['network']['internet_intf'] + self._dev_intf = json_config['network']['device_intf'] + + def _check_network_services(self): + LOGGER.debug("Checking network modules...") + for net_module in self._net_modules: + if net_module.enable_container: + LOGGER.debug("Checking network module: " + + net_module.display_name) + success = self._ping(net_module) + if success: + LOGGER.debug(net_module.display_name + + " responded succesfully: " + str(success)) + else: + LOGGER.error(net_module.display_name + + " failed to respond to ping") + + def _ping(self, net_module): + host = net_module.net_config.ipv4_address + namespace = "tr-ctns-" + net_module.dir_name + cmd = "ip netns exec " + namespace + " ping -c 1 " + str(host) + success = util.run_command(cmd, output=False) + return success + + def _create_private_net(self): + client = docker.from_env() + try: + network = client.networks.get(PRIVATE_DOCKER_NET) + network.remove() + except docker.errors.NotFound: + pass + + # TODO: These should be made into variables + ipam_pool = docker.types.IPAMPool( + subnet='100.100.0.0/16', + iprange='100.100.100.0/24' + ) + + ipam_config = docker.types.IPAMConfig( + pool_configs=[ipam_pool] + ) + + client.networks.create( + PRIVATE_DOCKER_NET, + ipam=ipam_config, + internal=True, + check_duplicate=True, + driver="macvlan" + ) + + def create_net(self): + LOGGER.info("Creating baseline network") + + if not util.interface_exists(self._int_intf) or not util.interface_exists(self._dev_intf): + LOGGER.error("Configured interfaces are not ready for use. " + + "Ensure both interfaces are connected.") + sys.exit(1) + + # Create data plane + util.run_command("ovs-vsctl add-br " + DEVICE_BRIDGE) + + # Create control plane + util.run_command("ovs-vsctl add-br " + INTERNET_BRIDGE) + + # Add external interfaces to data and control plane + util.run_command("ovs-vsctl add-port " + + DEVICE_BRIDGE + " " + self._dev_intf) + util.run_command("ovs-vsctl add-port " + + INTERNET_BRIDGE + " " + self._int_intf) + + # Enable forwarding of eapol packets + util.run_command("ovs-ofctl add-flow " + DEVICE_BRIDGE + + " 'table=0, dl_dst=01:80:c2:00:00:03, actions=flood'") + + # Remove IP from internet adapter + util.run_command("ifconfig " + self._int_intf + " 0.0.0.0") + + # Set ports up + util.run_command("ip link set dev " + DEVICE_BRIDGE + " up") + util.run_command("ip link set dev " + INTERNET_BRIDGE + " up") + + self._create_private_net() + + self.listener = Listener(self._dev_intf) + self.listener.start_listener() + + def load_network_modules(self): + """Load network modules from module_config.json.""" + LOGGER.debug("Loading network modules from /" + NETWORK_MODULES_DIR) + + loaded_modules = "Loaded the following network modules: " + net_modules_dir = os.path.join(self._path, NETWORK_MODULES_DIR) + + for module_dir in os.listdir(net_modules_dir): + + net_module = NetworkModule() + + # Load basic module information + + net_module_json = json.load(open(os.path.join( + self._path, net_modules_dir, module_dir, NETWORK_MODULE_METADATA), encoding='UTF-8')) + + net_module.name = net_module_json['config']['meta']['name'] + net_module.display_name = net_module_json['config']['meta']['display_name'] + net_module.description = net_module_json['config']['meta']['description'] + net_module.dir = os.path.join( + self._path, net_modules_dir, module_dir) + net_module.dir_name = module_dir + net_module.build_file = module_dir + ".Dockerfile" + net_module.container_name = "tr-ct-" + net_module.dir_name + net_module.image_name = "test-run/" + net_module.dir_name + + # Attach folder mounts to network module + if "docker" in net_module_json['config']: + if "mounts" in net_module_json['config']['docker']: + for mount_point in net_module_json['config']['docker']['mounts']: + net_module.mounts.append(Mount( + target=mount_point['target'], + source=os.path.join( + os.getcwd(), mount_point['source']), + type='bind' + )) + + # Determine if this is a container or just an image/template + if "enable_container" in net_module_json['config']['docker']: + net_module.enable_container = net_module_json['config']['docker']['enable_container'] + + # Load network service networking configuration + if net_module.enable_container: + + net_module.net_config.enable_wan = net_module_json['config']['network']['enable_wan'] + net_module.net_config.ip_index = net_module_json['config']['network']['ip_index'] + + net_module.net_config.host = False if not "host" in net_module_json[ + 'config']['network'] else net_module_json['config']['network']['host'] + + net_module.net_config.ipv4_address = self.network_config.ipv4_network[ + net_module.net_config.ip_index] + net_module.net_config.ipv4_network = self.network_config.ipv4_network + + net_module.net_config.ipv6_address = self.network_config.ipv6_network[ + net_module.net_config.ip_index] + net_module.net_config.ipv6_network = self.network_config.ipv6_network + + loaded_modules += net_module.dir_name + " " + + self._net_modules.append(net_module) + + LOGGER.info(loaded_modules) + + def build_network_modules(self): + LOGGER.info("Building network modules...") + for net_module in self._net_modules: + self._build_module(net_module) + + def _build_module(self, net_module): + LOGGER.debug("Building network module " + net_module.dir_name) + client = docker.from_env() + client.images.build( + dockerfile=os.path.join(net_module.dir, net_module.build_file), + path=self._path, + forcerm=True, + tag="test-run/" + net_module.dir_name + ) + + def _get_network_module(self, name): + for net_module in self._net_modules: + if name == net_module.display_name: + return net_module + return None + + # Start the OVS network module + # This should always be called before loading all + # other modules to allow for a properly setup base + # network + def _start_ovs_module(self): + self._start_network_service(self._get_network_module("OVS")) + + def _start_network_service(self, net_module): + + LOGGER.debug("Starting net service " + net_module.display_name) + network = "host" if net_module.net_config.host else PRIVATE_DOCKER_NET + LOGGER.debug(f"""Network: {network}, image name: {net_module.image_name}, + container name: {net_module.container_name}""") + try: + client = docker.from_env() + net_module.container = client.containers.run( + net_module.image_name, + auto_remove=True, + cap_add=["NET_ADMIN"], + name=net_module.container_name, + hostname=net_module.container_name, + network=PRIVATE_DOCKER_NET, + privileged=True, + detach=True, + mounts=net_module.mounts, + environment={"HOST_USER": os.getlogin()} + ) + except docker.errors.ContainerError as error: + LOGGER.error("Container run error") + LOGGER.error(error) + + if network != "host": + self._attach_service_to_network(net_module) + + def _stop_service_module(self, net_module, kill=False): + LOGGER.debug("Stopping Service container " + net_module.container_name) + try: + container = self._get_service_container(net_module) + if container is not None: + if kill: + LOGGER.debug("Killing container:" + + net_module.container_name) + container.kill() + else: + LOGGER.debug("Stopping container:" + + net_module.container_name) + container.stop() + LOGGER.debug("Container stopped:" + net_module.container_name) + except Exception as error: + LOGGER.error("Container stop error") + LOGGER.error(error) + + def _get_service_container(self, net_module): + LOGGER.debug("Resolving service container: " + + net_module.container_name) + container = None + try: + client = docker.from_env() + container = client.containers.get(net_module.container_name) + except docker.errors.NotFound: + LOGGER.debug("Container " + + net_module.container_name + " not found") + except Exception as e: + LOGGER.error("Failed to resolve container") + LOGGER.error(e) + return container + + def stop_networking_services(self, kill=False): + LOGGER.info("Stopping network services") + for net_module in self._net_modules: + # Network modules may just be Docker images, so we do not want to stop them + if not net_module.enable_container: + continue + self._stop_service_module(net_module, kill) + + def start_network_services(self): + LOGGER.info("Starting network services") + + os.makedirs(os.path.join(os.getcwd(), RUNTIME_DIR), exist_ok=True) + + for net_module in self._net_modules: + + # TODO: There should be a better way of doing this + # Do not try starting OVS module again, as it should already be running + if "OVS" != net_module.display_name: + + # Network modules may just be Docker images, so we do not want to start them as containers + if not net_module.enable_container: + continue + + self._start_network_service(net_module) + + LOGGER.info("All network services are running") + self._check_network_services() + + # TODO: Let's move this into a separate script? It does not look great + def _attach_service_to_network(self, net_module): + LOGGER.debug("Attaching net service " + + net_module.display_name + " to device bridge") + + # Device bridge interface example: tr-di-dhcp (Test Run Device Interface for DHCP container) + bridge_intf = DEVICE_BRIDGE + "i-" + net_module.dir_name + + # Container interface example: tr-cti-dhcp (Test Run Container Interface for DHCP container) + container_intf = "tr-cti-" + net_module.dir_name + + # Container network namespace name + container_net_ns = "tr-ctns-" + net_module.dir_name + + # Create interface pair + util.run_command("ip link add " + bridge_intf + + " type veth peer name " + container_intf) + + # Add bridge interface to device bridge + util.run_command("ovs-vsctl add-port " + + DEVICE_BRIDGE + " " + bridge_intf) + + # Get PID for running container + # TODO: Some error checking around missing PIDs might be required + container_pid = util.run_command( + "docker inspect -f {{.State.Pid}} " + net_module.container_name)[0] + + # Create symlink for container network namespace + util.run_command("ln -sf /proc/" + container_pid + + "/ns/net /var/run/netns/" + container_net_ns) + + # Attach container interface to container network namespace + util.run_command("ip link set " + container_intf + + " netns " + container_net_ns) + + # Rename container interface name to veth0 + util.run_command("ip netns exec " + container_net_ns + + " ip link set dev " + container_intf + " name veth0") + + # Set MAC address of container interface + util.run_command("ip netns exec " + container_net_ns + " ip link set dev veth0 address 9a:02:57:1e:8f:" + str(net_module.net_config.ip_index)) + + # Set IP address of container interface + util.run_command("ip netns exec " + container_net_ns + " ip addr add " + + net_module.net_config.get_ipv4_addr_with_prefix() + " dev veth0") + + util.run_command("ip netns exec " + container_net_ns + " ip addr add " + + net_module.net_config.get_ipv6_addr_with_prefix() + " dev veth0") + + # Set interfaces up + util.run_command("ip link set dev " + bridge_intf + " up") + util.run_command("ip netns exec " + container_net_ns + + " ip link set dev veth0 up") + + if net_module.net_config.enable_wan: + LOGGER.debug("Attaching net service " + + net_module.display_name + " to internet bridge") + + # Internet bridge interface example: tr-ci-dhcp (Test Run Control (Internet) Interface for DHCP container) + bridge_intf = INTERNET_BRIDGE + "i-" + net_module.dir_name + + # Container interface example: tr-cti-dhcp (Test Run Container Interface for DHCP container) + container_intf = "tr-cti-" + net_module.dir_name + + # Create interface pair + util.run_command("ip link add " + bridge_intf + + " type veth peer name " + container_intf) + + # Attach bridge interface to internet bridge + util.run_command("ovs-vsctl add-port " + + INTERNET_BRIDGE + " " + bridge_intf) + + # Attach container interface to container network namespace + util.run_command("ip link set " + container_intf + + " netns " + container_net_ns) + + # Rename container interface name to eth1 + util.run_command("ip netns exec " + container_net_ns + + " ip link set dev " + container_intf + " name eth1") + + # Set MAC address of container interface + util.run_command("ip netns exec " + container_net_ns + + " ip link set dev eth1 address 9a:02:57:1e:8f:0" + str(net_module.net_config.ip_index)) + + # Set interfaces up + util.run_command("ip link set dev " + bridge_intf + " up") + util.run_command("ip netns exec " + + container_net_ns + " ip link set dev eth1 up") + + def restore_net(self): + + LOGGER.info("Clearing baseline network") + + if hasattr(self, 'listener') and self.listener is not None and self.listener.is_running(): + self.listener.stop_listener() + + client = docker.from_env() + + # Stop all network containers if still running + for net_module in self._net_modules: + try: + container = client.containers.get( + "tr-ct-" + net_module.dir_name) + container.kill() + except Exception: + continue + + # Delete data plane + util.run_command("ovs-vsctl --if-exists del-br tr-d") + + # Delete control plane + util.run_command("ovs-vsctl --if-exists del-br tr-c") + + # Restart internet interface + if util.interface_exists(self._int_intf): + util.run_command("ip link set " + self._int_intf + " down") + util.run_command("ip link set " + self._int_intf + " up") + + LOGGER.info("Network is restored") + + +class NetworkModule: + + def __init__(self): + self.name = None + self.display_name = None + self.description = None + + self.container = None + self.container_name = None + self.image_name = None + + # Absolute path + self.dir = None + self.dir_name = None + self.build_file = None + self.mounts = [] + + self.enable_container = True + + self.net_config = NetworkModuleNetConfig() + +# The networking configuration for a network module + + +class NetworkModuleNetConfig: + + def __init__(self): + + self.enable_wan = False + + self.ip_index = 0 + self.ipv4_address = None + self.ipv4_network = None + self.ipv6_address = None + self.ipv6_network = None + + self.host = False + + def get_ipv4_addr_with_prefix(self): + return format(self.ipv4_address) + "/" + str(self.ipv4_network.prefixlen) + + def get_ipv6_addr_with_prefix(self): + return format(self.ipv6_address) + "/" + str(self.ipv6_network.prefixlen) + +# Represents the current configuration of the network for the device bridge + +class NetworkConfig: + + # TODO: Let's get this from a configuration file + def __init__(self): + self.ipv4_network = ipaddress.ip_network('10.10.10.0/24') + self.ipv6_network = ipaddress.ip_network('fd10:77be:4186::/64') diff --git a/net_orc/python/src/network_runner.py b/net_orc/python/src/network_runner.py new file mode 100644 index 000000000..3fe9e8a41 --- /dev/null +++ b/net_orc/python/src/network_runner.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 + +"""Wrapper for the NetworkOrchestrator that simplifies +virtual network start process by allowing direct calling +from the command line. + +Run using the provided command scripts in the cmd folder. +E.g sudo cmd/start +""" + +import argparse +import signal +import sys +import time + +import logger + +from network_orchestrator import NetworkOrchestrator + +LOGGER = logger.get_logger('net_runner') + +class NetworkRunner: + def __init__(self, config_file=None, validate=True, async_monitor=False): + self._monitor_thread = None + self._register_exits() + self.net_orc = NetworkOrchestrator(config_file=config_file,validate=validate,async_monitor=async_monitor) + + def _register_exits(self): + signal.signal(signal.SIGINT, self._exit_handler) + signal.signal(signal.SIGTERM, self._exit_handler) + signal.signal(signal.SIGABRT, self._exit_handler) + signal.signal(signal.SIGQUIT, self._exit_handler) + + def _exit_handler(self, signum, arg): # pylint: disable=unused-argument + LOGGER.debug("Exit signal received: " + str(signum)) + if signum in (2, signal.SIGTERM): + LOGGER.info("Exit signal received.") + # Kill all container services quickly + # If we're here, we want everything to stop immediately + # and don't care about a gracefully shutdown + self.stop(True) + sys.exit(1) + + def stop(self, kill=False): + self.net_orc.stop(kill) + + def start(self): + self.net_orc.start() + +def parse_args(argv): + parser = argparse.ArgumentParser(description="Test Run Help", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument("--no-validate", action="store_true", + help="Turn off the validation of the network after network boot") + parser.add_argument("-f", "--config-file", default=None, + help="Define the configuration file for the Network Orchestrator") + parser.add_argument("-d", "--daemon", action="store_true", + help="Run the network monitor process in the background as a daemon thread") + + args, unknown = parser.parse_known_args() + return args + +if __name__ == "__main__": + args=parse_args(sys.argv) + runner = NetworkRunner(config_file=args.config_file, + validate=not args.no_validate, + async_monitor=args.daemon) + runner.start() \ No newline at end of file diff --git a/net_orc/python/src/network_validator.py b/net_orc/python/src/network_validator.py new file mode 100644 index 000000000..53fbcdbd0 --- /dev/null +++ b/net_orc/python/src/network_validator.py @@ -0,0 +1,274 @@ +"""Holds logic for validation of network services prior to runtime.""" +import json +import os +import shutil +import time +import docker +from docker.types import Mount +import logger +import util + +LOGGER = logger.get_logger("validator") +OUTPUT_DIR = "runtime/validation" +DEVICES_DIR = "network/devices" +DEVICE_METADATA = "conf/module_config.json" +DEVICE_BRIDGE = "tr-d" +CONF_DIR = "conf" +CONF_FILE = "system.json" + +class NetworkValidator: + """Perform validation of network services.""" + + def __init__(self): + self._net_devices = [] + + self._path = os.path.dirname(os.path.dirname( + os.path.dirname(os.path.realpath(__file__)))) + + self._device_dir = os.path.join(self._path, DEVICES_DIR) + + shutil.rmtree(os.path.join(self._path, OUTPUT_DIR), ignore_errors=True) + + def start(self): + """Start the network validator.""" + LOGGER.info("Starting validator") + self._load_devices() + self._build_network_devices() + self._start_network_devices() + + def stop(self, kill=False): + """Stop the network validator.""" + LOGGER.info("Stopping validator") + self._stop_network_devices(kill) + LOGGER.info("Validator stopped") + + def _build_network_devices(self): + LOGGER.debug("Building network validators...") + for net_device in self._net_devices: + self._build_device(net_device) + + def _build_device(self, net_device): + LOGGER.debug("Building network validator " + net_device.dir_name) + try: + client = docker.from_env() + client.images.build( + dockerfile=os.path.join(net_device.dir, net_device.build_file), + path=self._path, + forcerm=True, + tag="test-run/" + net_device.dir_name + ) + LOGGER.debug("Validator device built: " + net_device.dir_name) + except docker.errors.BuildError as error: + LOGGER.error("Container build error") + LOGGER.error(error) + + def _load_devices(self): + + LOGGER.info(f"Loading validators from {DEVICES_DIR}") + + loaded_devices = "Loaded the following validators: " + + for module_dir in os.listdir(self._device_dir): + + device = FauxDevice() + + # Load basic module information + with open(os.path.join(self._device_dir, module_dir, DEVICE_METADATA), + encoding='utf-8') as device_config_file: + device_json = json.load(device_config_file) + + device.name = device_json['config']['meta']['name'] + device.description = device_json['config']['meta']['description'] + + device.dir = os.path.join(self._path, self._device_dir, module_dir) + device.dir_name = module_dir + device.build_file = module_dir + ".Dockerfile" + device.container_name = "tr-ct-" + device.dir_name + device.image_name = "test-run/" + device.dir_name + + runtime_source = os.path.join(os.getcwd(), OUTPUT_DIR, device.name) + conf_source = os.path.join(os.getcwd(), CONF_DIR) + os.makedirs(runtime_source, exist_ok=True) + + device.mounts = [ + Mount( + target='/runtime/validation', + source=runtime_source, + type = 'bind' + ), + Mount( + target='/conf', + source=conf_source, + type='bind', + read_only=True + ), + Mount( + target='/runtime/network', + source=runtime_source, + type='bind' + ) + ] + + if 'timeout' in device_json['config']['docker']: + device.timeout = device_json['config']['docker']['timeout'] + + # Determine if this is a container or just an image/template + if "enable_container" in device_json['config']['docker']: + device.enable_container = device_json['config']['docker']['enable_container'] + + self._net_devices.append(device) + + loaded_devices += device.dir_name + " " + + LOGGER.info(loaded_devices) + + def _start_network_devices(self): + LOGGER.debug("Starting network devices") + for net_device in self._net_devices: + self._start_network_device(net_device) + + def _start_network_device(self, device): + LOGGER.info("Starting device " + device.name) + LOGGER.debug("Image name: " + device.image_name) + LOGGER.debug("Container name: " + device.container_name) + + try: + client = docker.from_env() + device.container = client.containers.run( + device.image_name, + auto_remove=True, + cap_add=["NET_ADMIN"], + name=device.container_name, + hostname=device.container_name, + network="none", + privileged=True, + detach=True, + mounts=device.mounts, + environment={"HOST_USER": os.getlogin()} + ) + except docker.errors.ContainerError as error: + LOGGER.error("Container run error") + LOGGER.error(error) + + self._attach_device_to_network(device) + + # Determine the module timeout time + test_module_timeout = time.time() + device.timeout + status = self._get_device_status(device) + + while time.time() < test_module_timeout and status == 'running': + time.sleep(1) + status = self._get_device_status(device) + + LOGGER.info("Validation device " + device.name + " has finished") + + def _get_device_status(self,module): + container = self._get_device_container(module) + if container is not None: + return container.status + return None + + def _attach_device_to_network(self, device): + LOGGER.debug("Attaching device " + device.name + " to device bridge") + + # Device bridge interface example: tr-di-dhcp + # (Test Run Device Interface for DHCP container) + bridge_intf = DEVICE_BRIDGE + "i-" + device.dir_name + + # Container interface example: tr-cti-dhcp (Test Run Container Interface for DHCP container) + container_intf = "tr-cti-" + device.dir_name + + # Container network namespace name + container_net_ns = "tr-ctns-" + device.dir_name + + # Create interface pair + util.run_command("ip link add " + bridge_intf + + " type veth peer name " + container_intf) + + # Add bridge interface to device bridge + util.run_command("ovs-vsctl add-port " + + DEVICE_BRIDGE + " " + bridge_intf) + + # Get PID for running container + # TODO: Some error checking around missing PIDs might be required + container_pid = util.run_command( + "docker inspect -f {{.State.Pid}} " + device.container_name)[0] + + # Create symlink for container network namespace + util.run_command("ln -sf /proc/" + container_pid + + "/ns/net /var/run/netns/" + container_net_ns) + + # Attach container interface to container network namespace + util.run_command("ip link set " + container_intf + + " netns " + container_net_ns) + + # Rename container interface name to veth0 + util.run_command("ip netns exec " + container_net_ns + + " ip link set dev " + container_intf + " name veth0") + + # Set interfaces up + util.run_command("ip link set dev " + bridge_intf + " up") + util.run_command("ip netns exec " + container_net_ns + + " ip link set dev veth0 up") + + def _stop_network_device(self, net_device, kill=False): + LOGGER.debug("Stopping device container " + net_device.container_name) + try: + container = self._get_device_container(net_device) + if container is not None: + if kill: + LOGGER.debug("Killing container:" + + net_device.container_name) + container.kill() + else: + LOGGER.debug("Stopping container:" + + net_device.container_name) + container.stop() + LOGGER.debug("Container stopped:" + net_device.container_name) + except Exception as e: + LOGGER.error("Container stop error") + LOGGER.error(e) + + def _get_device_container(self, net_device): + LOGGER.debug("Resolving device container: " + + net_device.container_name) + container = None + try: + client = docker.from_env() + container = client.containers.get(net_device.container_name) + except docker.errors.NotFound: + LOGGER.debug("Container " + + net_device.container_name + " not found") + except Exception as e: + LOGGER.error("Failed to resolve container") + LOGGER.error(e) + return container + + def _stop_network_devices(self, kill=False): + LOGGER.debug("Stopping devices") + for net_device in self._net_devices: + # Devices may just be Docker images, so we do not want to stop them + if not net_device.enable_container: + continue + self._stop_network_device(net_device, kill) + +class FauxDevice: # pylint: disable=too-few-public-methods,too-many-instance-attributes + """Represent a faux device.""" + + def __init__(self): + self.name = "Unknown device" + self.description = "Unknown description" + + self.container = None + self.container_name = None + self.image_name = None + + # Absolute path + self.dir = None + + self.dir_name = None + self.build_file = None + self.mounts = [] + + self.enable_container = True + self.timeout = 60 diff --git a/net_orc/python/src/run_validator.py b/net_orc/python/src/run_validator.py new file mode 100644 index 000000000..318456083 --- /dev/null +++ b/net_orc/python/src/run_validator.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +import os +import logger +import signal +import time +import os + +from network_orchestrator import NetworkOrchestrator +from network_orchestrator_validator import NetworkOrchestratorValidator + +LOGGER = logger.get_logger('test_run') +RUNTIME_FOLDER = "runtime/network" + +class ValidatorRun: + + def __init__(self): + + signal.signal(signal.SIGINT, self.handler) + signal.signal(signal.SIGTERM, self.handler) + signal.signal(signal.SIGABRT, self.handler) + signal.signal(signal.SIGQUIT, self.handler) + + LOGGER.info("Starting Network Orchestrator") + #os.makedirs(RUNTIME_FOLDER) + + # Cleanup any old validator components + self._validator = NetworkOrchestratorValidator() + self._validator._stop_validator(True); + + # Start the validator after network is ready + self._validator._start_validator() + + # TODO: Kill validator once all faux devices are no longer running + time.sleep(2000) + + # Gracefully shutdown network + self._validator._stop_validator(); + + def handler(self, signum, frame): + LOGGER.debug("SigtermEnum: " + str(signal.SIGTERM)) + LOGGER.debug("Exit signal received: " + str(signum)) + if (signum == 2 or signum == signal.SIGTERM): + LOGGER.info("Exit signal received. Stopping validator...") + # Kill all container services quickly + # If we're here, we want everything to stop immediately + # and don't care about a gracefully shutdown. + self._validator._stop_validator(True); + LOGGER.info("Validator stopped") + exit(1) + +test_run = ValidatorRun() diff --git a/net_orc/python/src/util.py b/net_orc/python/src/util.py new file mode 100644 index 000000000..a5cfe205f --- /dev/null +++ b/net_orc/python/src/util.py @@ -0,0 +1,30 @@ +import subprocess +import shlex +import logger +import netifaces + + +# Runs a process at the os level +# By default, returns the standard output and error output +# If the caller sets optional output parameter to False, +# will only return a boolean result indicating if it was +# succesful in running the command. Failure is indicated +# by any return code from the process other than zero. +def run_command(cmd, output=True): + success = False + LOGGER = logger.get_logger('util') + process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + if process.returncode !=0 and output: + err_msg = "%s. Code: %s" % (stderr.strip(), process.returncode) + LOGGER.error("Command Failed: " + cmd) + LOGGER.error("Error: " + err_msg) + else: + success = True + if output: + return stdout.strip().decode('utf-8'), stderr + else: + return success + +def interface_exists(interface): + return interface in netifaces.interfaces() \ No newline at end of file From b114adcc6c9571e6ef4f08e3c6e82285e48cf314 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 28 Apr 2023 13:16:26 -0600 Subject: [PATCH 02/36] cleanup duplicate start and install scripts --- cmd/install | 14 +++------- cmd/start | 21 +++++++++++++++ net_orc/cmd/install | 9 ------- net_orc/cmd/start | 45 -------------------------------- test_orc/python/requirements.txt | 2 ++ 5 files changed, 26 insertions(+), 65 deletions(-) delete mode 100644 net_orc/cmd/install delete mode 100644 net_orc/cmd/start diff --git a/cmd/install b/cmd/install index 6dee1c635..cfda9b322 100755 --- a/cmd/install +++ b/cmd/install @@ -1,19 +1,11 @@ #!/bin/bash -e -GIT_URL=https://github.com/auto-iot -NET_ORC_DIR=net_orc -NET_ORC_VERSION="main" - python3 -m venv venv source venv/bin/activate -pip3 install -r etc/requirements.txt - -rm -rf $NET_ORC_DIR -git clone -b $NET_ORC_VERSION $GIT_URL/network-orchestrator $NET_ORC_DIR -chown -R $USER $NET_ORC_DIR +pip3 install -r net_orc/python/requirements.txt -pip3 install -r $NET_ORC_DIR/python/requirements.txt +pip3 install -r test_orc/python/requirements.txt -deactivate \ No newline at end of file +deactivate diff --git a/cmd/start b/cmd/start index 113f14b3e..d146f413d 100755 --- a/cmd/start +++ b/cmd/start @@ -20,4 +20,25 @@ source venv/bin/activate # TODO: Execute python code python -u framework/test_runner.py $@ +# TODO: Work in progress code for containerization of OVS module +# asyncRun() { +# "$@" & +# pid="$!" +# echo "PID Running: " $pid +# trap "echo 'Stopping PID $pid'; kill -SIGTERM $pid" SIGINT SIGTERM + +# sleep 10 + +# # A signal emitted while waiting will make the wait command return code > 128 +# # Let's wrap it in a loop that doesn't end before the process is indeed stopped +# while kill -0 $pid > /dev/null 2>&1; do +# #while $(kill -0 $pid 2>/dev/null); do +# wait +# done +# } + +# # -u flag allows python print statements +# # to be logged by docker by running unbuffered +# asyncRun python3 -u python/src/run.py $@ + deactivate \ No newline at end of file diff --git a/net_orc/cmd/install b/net_orc/cmd/install deleted file mode 100644 index fe9781cb2..000000000 --- a/net_orc/cmd/install +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -e - -python3 -m venv venv - -source venv/bin/activate - -pip3 install -r python/requirements.txt - -deactivate diff --git a/net_orc/cmd/start b/net_orc/cmd/start deleted file mode 100644 index 8369d2c8b..000000000 --- a/net_orc/cmd/start +++ /dev/null @@ -1,45 +0,0 @@ -#!/bin/bash -e - -if [[ "$EUID" -ne 0 ]]; then - echo "Must run as root. Use sudo cmd/start" - exit 1 -fi - -# Check if python modules exist. Install if not -[ ! -d "venv" ] && cmd/install - -# Ensure that /var/run/netns folder exists -mkdir -p /var/run/netns - -# Clear up existing runtime files -rm -rf runtime - -# Activate Python virtual environment -source venv/bin/activate - -# -u flag allows python print statements -# to be logged by docker by running unbuffered -python3 -u python/src/network_runner.py $@ - -# TODO: Work in progress code for containerization of OVS module -# asyncRun() { -# "$@" & -# pid="$!" -# echo "PID Running: " $pid -# trap "echo 'Stopping PID $pid'; kill -SIGTERM $pid" SIGINT SIGTERM - -# sleep 10 - -# # A signal emitted while waiting will make the wait command return code > 128 -# # Let's wrap it in a loop that doesn't end before the process is indeed stopped -# while kill -0 $pid > /dev/null 2>&1; do -# #while $(kill -0 $pid 2>/dev/null); do -# wait -# done -# } - -# # -u flag allows python print statements -# # to be logged by docker by running unbuffered -# asyncRun python3 -u python/src/run.py $@ - -deactivate \ No newline at end of file diff --git a/test_orc/python/requirements.txt b/test_orc/python/requirements.txt index e69de29bb..ba5865db1 100644 --- a/test_orc/python/requirements.txt +++ b/test_orc/python/requirements.txt @@ -0,0 +1,2 @@ +netifaces +scapy \ No newline at end of file From d1804466aa2409e4dc3bf1c5ff4af63ebb065634 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 28 Apr 2023 13:45:29 -0600 Subject: [PATCH 03/36] Temporary fix for python dependencies --- cmd/install | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/install b/cmd/install index cfda9b322..539234006 100755 --- a/cmd/install +++ b/cmd/install @@ -4,6 +4,8 @@ python3 -m venv venv source venv/bin/activate +pip3 install --upgrade requests + pip3 install -r net_orc/python/requirements.txt pip3 install -r test_orc/python/requirements.txt From f5b7a640f88a15d2f167b6f69cf1395c374d86d0 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 28 Apr 2023 14:57:02 -0600 Subject: [PATCH 04/36] Remove duplicate python requirements --- etc/requirements.txt | 2 -- test_orc/python/requirements.txt | 2 -- 2 files changed, 4 deletions(-) delete mode 100644 etc/requirements.txt diff --git a/etc/requirements.txt b/etc/requirements.txt deleted file mode 100644 index 979b408bd..000000000 --- a/etc/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -netifaces -scapy \ No newline at end of file diff --git a/test_orc/python/requirements.txt b/test_orc/python/requirements.txt index ba5865db1..e69de29bb 100644 --- a/test_orc/python/requirements.txt +++ b/test_orc/python/requirements.txt @@ -1,2 +0,0 @@ -netifaces -scapy \ No newline at end of file From eaba9bb32bfe02227ed38b3d6cb2d5b54f677d5c Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 28 Apr 2023 15:04:45 -0600 Subject: [PATCH 05/36] remove duplicate conf files --- net_orc/conf/.gitignore | 1 - net_orc/conf/network/radius/ca.crt | 26 -------------------------- net_orc/conf/system.json.example | 7 ------- 3 files changed, 34 deletions(-) delete mode 100644 net_orc/conf/.gitignore delete mode 100644 net_orc/conf/network/radius/ca.crt delete mode 100644 net_orc/conf/system.json.example diff --git a/net_orc/conf/.gitignore b/net_orc/conf/.gitignore deleted file mode 100644 index 41b89ceb1..000000000 --- a/net_orc/conf/.gitignore +++ /dev/null @@ -1 +0,0 @@ -system.json \ No newline at end of file diff --git a/net_orc/conf/network/radius/ca.crt b/net_orc/conf/network/radius/ca.crt deleted file mode 100644 index d009cb1ab..000000000 --- a/net_orc/conf/network/radius/ca.crt +++ /dev/null @@ -1,26 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIEYTCCA0mgAwIBAgIUQJ4F8hBCnCp7ASPZqG/tNQgoUR4wDQYJKoZIhvcNAQEL -BQAwgb8xCzAJBgNVBAYTAkdCMRswGQYDVQQIDBIbWzN+TGVpY2VzdGVyc2hpcmUx -FTATBgNVBAcMDExvdWdoYm9yb3VnaDEUMBIGA1UECgwLRm9yZXN0IFJvY2sxDjAM -BgNVBAsMBUN5YmVyMR8wHQYDVQQDDBZjeWJlci5mb3Jlc3Ryb2NrLmNvLnVrMTUw -MwYJKoZIhvcNAQkBFiZjeWJlcnNlY3VyaXR5LnRlc3RpbmdAZm9yZXN0cm9jay5j -by51azAeFw0yMjAzMDQxMjEzMTBaFw0yNzAzMDMxMjEzMTBaMIG/MQswCQYDVQQG -EwJHQjEbMBkGA1UECAwSG1szfkxlaWNlc3RlcnNoaXJlMRUwEwYDVQQHDAxMb3Vn -aGJvcm91Z2gxFDASBgNVBAoMC0ZvcmVzdCBSb2NrMQ4wDAYDVQQLDAVDeWJlcjEf -MB0GA1UEAwwWY3liZXIuZm9yZXN0cm9jay5jby51azE1MDMGCSqGSIb3DQEJARYm -Y3liZXJzZWN1cml0eS50ZXN0aW5nQGZvcmVzdHJvY2suY28udWswggEiMA0GCSqG -SIb3DQEBAQUAA4IBDwAwggEKAoIBAQDDNz3vJiZ5nX8lohEhqXvxEme3srip8qF7 -r5ScIeQzsTKuPNAmoefx9TcU3SyA2BnREuDX+OCYMN62xxWG2PndOl0LNezAY22C -PJwHbaBntLKY/ZhxYSTyratM7zxKSVLtClamA/bJXBhdfZZKYOP3xlZQEQTygtzK -j5hZwDrpDARtjRZIMWPLqVcoaW9ow2urJVsdD4lYAhpQU2UIgiWo7BG3hJsUfcYX -EQyyrMKJ7xaCwzIU7Sem1PETrzeiWg4KhDijc7A0RMPWlU5ljf0CnY/IZwiDsMRl -hGmGBPvR+ddiWPZPtSKj6TPWpsaMUR9UwncLmSSrhf1otX4Mw0vbAgMBAAGjUzBR -MB0GA1UdDgQWBBR0Qxx2mDTPIfpnzO5YtycGs6t8ijAfBgNVHSMEGDAWgBR0Qxx2 -mDTPIfpnzO5YtycGs6t8ijAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUA -A4IBAQCpTMBMZGXF74WCxrIk23MUsu0OKzMs8B16Wy8BHz+7hInLZwbkx71Z0TP5 -rsMITetSANtM/k4jH7Vmr1xmzU7oSz5zKU1+7rIjKjGtih48WZdJay0uqfKe0K2s -vsRS0LVLY6IiTFWK9YrLC0QFSK7z5GDl1oc/D5yIZAkbsL6PRQJ5RQsYf5BhHfyB -PRV/KcF7c9iKVYW2vILJzbyYLHTDADTHbtfCe5+pAGxagswDjSMVkQu5iJNjbtUO -5iv7PRkgzUFru9Kk6q+LrXbzyPPCwlc3Xbh1q5jSkJLkcV3K26E7+uX5HI+Hxpeh -a8kOsdnw+N8wX6bc7eXIaGBDMine ------END CERTIFICATE----- diff --git a/net_orc/conf/system.json.example b/net_orc/conf/system.json.example deleted file mode 100644 index 77c981394..000000000 --- a/net_orc/conf/system.json.example +++ /dev/null @@ -1,7 +0,0 @@ -{ - "network": { - "device_intf": "enx207bd2620617", - "internet_intf": "enx207bd26205e9" - }, - "log_level": "INFO" -} \ No newline at end of file From 80f7ca6ba906b73d35cec561486b0032641faed1 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 28 Apr 2023 15:17:45 -0600 Subject: [PATCH 06/36] remove remote-net option --- framework/test_runner.py | 10 +++------- framework/testrun.py | 16 ++++------------ 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/framework/test_runner.py b/framework/test_runner.py index 91ff4cb1a..14cadf3e1 100644 --- a/framework/test_runner.py +++ b/framework/test_runner.py @@ -19,9 +19,9 @@ class TestRunner: - def __init__(self, local_net=True, config_file=None, validate=True, net_only=False): + def __init__(self, config_file=None, validate=True, net_only=False): self._register_exits() - self.test_run = TestRun(local_net=local_net, config_file=config_file, + self.test_run = TestRun(config_file=config_file, validate=validate, net_only=net_only) def _register_exits(self): @@ -51,9 +51,6 @@ def start(self): def parse_args(argv): parser = argparse.ArgumentParser(description="Test Run", formatter_class=argparse.ArgumentDefaultsHelpFormatter) - parser.add_argument("-r", "--remote-net", action="store_false", - help='''Use the network orchestrator from the parent directory instead - of the one downloaded locally from the install script.''') parser.add_argument("-f", "--config-file", default=None, help="Define the configuration file for Test Run and Network Orchestrator") parser.add_argument("--no-validate", action="store_true", @@ -66,8 +63,7 @@ def parse_args(argv): if __name__ == "__main__": args = parse_args(sys.argv) - runner = TestRunner(local_net=args.remote_net, - config_file=args.config_file, + runner = TestRunner(config_file=args.config_file, validate=not args.no_validate, net_only=args.net_only) runner.start() diff --git a/framework/testrun.py b/framework/testrun.py index 42534265a..0561163ac 100644 --- a/framework/testrun.py +++ b/framework/testrun.py @@ -38,7 +38,7 @@ class TestRun: # pylint: disable=too-few-public-methods orchestrator and user interface. """ - def __init__(self, local_net=True, config_file=CONFIG_FILE,validate=True, net_only=False): + def __init__(self, config_file=CONFIG_FILE,validate=True, net_only=False): self._devices = [] self._net_only = net_only @@ -46,7 +46,7 @@ def __init__(self, local_net=True, config_file=CONFIG_FILE,validate=True, net_on self._register_exits() # Import the correct net orchestrator - self.import_dependencies(local_net) + self.import_dependencies() # Expand the config file to absolute pathing config_file_abs=self._get_config_abs(config_file=config_file) @@ -78,17 +78,9 @@ def stop(self,kill=False): self._stop_tests() self._stop_network(kill=kill) - def import_dependencies(self, local_net=True): - if local_net: - # Add local net_orc to Python path - net_orc_dir = os.path.join(parent_dir, 'net_orc', 'python', 'src') - else: - # Resolve the path to the test-run parent folder - root_dir = os.path.abspath(os.path.join(parent_dir, os.pardir)) - # Add manually cloned network orchestrator from parent folder - net_orc_dir = os.path.join( - root_dir, 'network-orchestrator', 'python', 'src') + def import_dependencies(self): # Add net_orc to Python path + net_orc_dir = os.path.join(parent_dir, 'net_orc', 'python', 'src') sys.path.append(net_orc_dir) # Import the network orchestrator global net_orc From 1ba883c5e3d8857ae09602728d87eb5e7d80aa15 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 28 Apr 2023 15:31:06 -0600 Subject: [PATCH 07/36] cleanp unecessary files --- framework/.gitignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 framework/.gitignore diff --git a/framework/.gitignore b/framework/.gitignore deleted file mode 100644 index ba0430d26..000000000 --- a/framework/.gitignore +++ /dev/null @@ -1 +0,0 @@ -__pycache__/ \ No newline at end of file From 74c7e663535c7952b2417e3dfb789b962fb6a0ca Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Mon, 1 May 2023 12:21:10 -0600 Subject: [PATCH 08/36] Add dns test module Fix test module build process --- framework/testrun.py | 8 +- local/devices/MyLaptop/device_config.json | 5 + .../Teltonika TRB140/device_config.json | 4 +- test_orc/modules/base/bin/capture | 3 +- test_orc/modules/dns/bin/start_test_module | 42 ++++++++ test_orc/modules/dns/conf/module_config.json | 18 ++++ test_orc/modules/dns/dns.Dockerfile | 11 ++ test_orc/modules/dns/python/src/dns_module.py | 102 ++++++++++++++++++ test_orc/modules/dns/python/src/logger.py | 46 ++++++++ test_orc/modules/dns/python/src/run.py | 53 +++++++++ test_orc/python/src/test_orchestrator.py | 27 +++-- 11 files changed, 303 insertions(+), 16 deletions(-) create mode 100644 local/devices/MyLaptop/device_config.json create mode 100644 test_orc/modules/dns/bin/start_test_module create mode 100644 test_orc/modules/dns/conf/module_config.json create mode 100644 test_orc/modules/dns/dns.Dockerfile create mode 100644 test_orc/modules/dns/python/src/dns_module.py create mode 100644 test_orc/modules/dns/python/src/logger.py create mode 100644 test_orc/modules/dns/python/src/run.py diff --git a/framework/testrun.py b/framework/testrun.py index 0561163ac..56c82d7b6 100644 --- a/framework/testrun.py +++ b/framework/testrun.py @@ -63,6 +63,7 @@ def start(self): self._start_network() else: self._start_network() + self._test_orc.start() self._net_orc.listener.register_callback( self._device_discovered, [NetworkEvent.DEVICE_DISCOVERED]) @@ -119,9 +120,10 @@ def _get_config_abs(self,config_file=None): def _start_network(self): self._net_orc.start() - def _run_tests(self): + def _run_tests(self,device): """Iterate through and start all test modules.""" - self._test_orc.start() + time.sleep(60) # Let device bootup + self._test_orc._run_test_modules(device) def _stop_network(self,kill=False): self._net_orc.stop(kill=kill) @@ -165,4 +167,4 @@ def _device_discovered(self, mac_addr): f'A new device has been discovered with mac address {mac_addr}') # TODO: Pass device information to test orchestrator/runner - self._run_tests() + self._run_tests(device) diff --git a/local/devices/MyLaptop/device_config.json b/local/devices/MyLaptop/device_config.json new file mode 100644 index 000000000..c0bb15cc7 --- /dev/null +++ b/local/devices/MyLaptop/device_config.json @@ -0,0 +1,5 @@ +{ + "make": "Microsoft", + "model": "Surface Laptop 3", + "mac_addr": "3c:8c:f8:ea:4a:cf" +} \ No newline at end of file diff --git a/local/devices/Teltonika TRB140/device_config.json b/local/devices/Teltonika TRB140/device_config.json index 759c1e9b4..3ac414a5a 100644 --- a/local/devices/Teltonika TRB140/device_config.json +++ b/local/devices/Teltonika TRB140/device_config.json @@ -1,5 +1,5 @@ { - "make": "Teltonika", - "model": "TRB140", + "make": "Microsoft", + "model": "Surface Laptop 3", "mac_addr": "00:1e:42:35:73:c4" } \ No newline at end of file diff --git a/test_orc/modules/base/bin/capture b/test_orc/modules/base/bin/capture index dccafb0c5..facb6acf7 100644 --- a/test_orc/modules/base/bin/capture +++ b/test_orc/modules/base/bin/capture @@ -4,7 +4,7 @@ MODULE_NAME=$1 # Define the local file location for the capture to be saved -PCAP_DIR="/runtime/output/" +PCAP_DIR="/runtime/output" PCAP_FILE=$MODULE_NAME.pcap # Allow a user to define an interface by passing it into this script @@ -13,7 +13,6 @@ INTERFACE=$2 # Create the output directory and start the capture mkdir -p $PCAP_DIR chown $HOST_USER:$HOST_USER $PCAP_DIR -echo "PCAP Dir: $PCAP_DIR/$PCAP_FILE" tcpdump -i $INTERFACE -w $PCAP_DIR/$PCAP_FILE -Z $HOST_USER & # Small pause to let the capture to start diff --git a/test_orc/modules/dns/bin/start_test_module b/test_orc/modules/dns/bin/start_test_module new file mode 100644 index 000000000..2938eb0f8 --- /dev/null +++ b/test_orc/modules/dns/bin/start_test_module @@ -0,0 +1,42 @@ +#!/bin/bash + +# An example startup script that does the bare minimum to start +# a test module via a pyhon script. Each test module should include a +# start_test_module file that overwrites this one to boot all of its +# specific requirements to run. + +# Define where the python source files are located +PYTHON_SRC_DIR=/testrun/python/src + +# Fetch module name +MODULE_NAME=$1 + +# Default interface should be veth0 for all containers +DEFAULT_IFACE=veth0 + +# Allow a user to define an interface by passing it into this script +DEFINED_IFACE=$2 + +# Select which interace to use +if [[ -z $DEFINED_IFACE || "$DEFINED_IFACE" == "null" ]] +then + echo "No interface defined, defaulting to veth0" + INTF=$DEFAULT_IFACE +else + INTF=$DEFINED_IFACE +fi + +# Create and set permissions on the log files +LOG_FILE=/runtime/output/$MODULE_NAME.log +RESULT_FILE=/runtime/output/$MODULE_NAME-result.json +touch $LOG_FILE +touch $RESULT_FILE +chown $HOST_USER:$HOST_USER $LOG_FILE +chown $HOST_USER:$HOST_USER $RESULT_FILE + +# Run the python scrip that will execute the tests for this module +# -u flag allows python print statements +# to be logged by docker by running unbuffered +python3 -u $PYTHON_SRC_DIR/run.py "-m $MODULE_NAME" + +echo Module has finished \ No newline at end of file diff --git a/test_orc/modules/dns/conf/module_config.json b/test_orc/modules/dns/conf/module_config.json new file mode 100644 index 000000000..d5b7c54d9 --- /dev/null +++ b/test_orc/modules/dns/conf/module_config.json @@ -0,0 +1,18 @@ +{ + "config": { + "meta": { + "name": "dns", + "display_name": "DNS", + "description": "DNS test" + }, + "network": { + "interface": "eth0", + "enable_wan": false, + "ip_index": 9 + }, + "docker": { + "enable_container": true, + "timeout": 30 + } + } +} \ No newline at end of file diff --git a/test_orc/modules/dns/dns.Dockerfile b/test_orc/modules/dns/dns.Dockerfile new file mode 100644 index 000000000..7c3497bc3 --- /dev/null +++ b/test_orc/modules/dns/dns.Dockerfile @@ -0,0 +1,11 @@ +# Image name: test-run/baseline-test +FROM test-run/base-test:latest + +# Copy over all configuration files +COPY modules/dns/conf /testrun/conf + +# Load device binary files +COPY modules/dns/bin /testrun/bin + +# Copy over all python files +COPY modules/dns/python /testrun/python \ No newline at end of file diff --git a/test_orc/modules/dns/python/src/dns_module.py b/test_orc/modules/dns/python/src/dns_module.py new file mode 100644 index 000000000..3eac35bb8 --- /dev/null +++ b/test_orc/modules/dns/python/src/dns_module.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 + +import json +import time +import logger +import subprocess + +LOG_NAME = "test_dns" +RESULTS_DIR = "/runtime/output/" +LOGGER = logger.get_logger(LOG_NAME) +CAPTURE_FILE = "/runtime/network/dns.pcap" + +class DNSModule: + + def __init__(self, module): + self._dns_server = "10.10.10.4" + self._dns_resolution_test = False + self._dns_dhcp_server_test = False + self.module = module + self.add_logger(module) + + def add_logger(self, module): + global LOGGER + LOGGER = logger.get_logger(LOG_NAME, module) + + # Run all the DNS Tests + def run_tests(self): + LOGGER.info("Checking for DNS traffic from DHCP provided server") + self._check_dns_traffic() + + def generate_results(self): + LOGGER.info("Generating test results") + results = [] + results.append(self.generate_result( + "DNS resolution test", self._dns_resolution_test)) + results.append(self.generate_result( + "DNS DHCP server test", self._dns_dhcp_server_test)) + json_results = json.dumps({"results": results}, indent=2) + self.write_results(json_results) + + def write_results(self, results): + results_file = RESULTS_DIR + self.module + "-result.json" + LOGGER.info("Writing results to " + results_file) + f = open(results_file, "w", encoding="utf-8") + f.write(results) + f.close() + + def generate_result(self, test_name, test_result): + if test_result is not None: + result = "compliant" if test_result else "non-compliant" + else: + result = "skipped" + LOGGER.info(test_name + ": " + result) + res_dict = { + "name": test_name, + "result": result, + "description": "The device is " + result + } + return res_dict + + def _check_dns_traffic(self): + LOGGER.info("Checking DNS traffic for DNS server: " + self._dns_server) + + # Check if the device has sent any DNS requests + filter_to_dns = 'dst port 53 and dst host {}'.format( + self._dns_server) + to_dns = self._exec_tcpdump(filter_to_dns) + num_query_dns = len(to_dns) + LOGGER.info("DNS queries found: " + str(num_query_dns)) + dns_traffic_detected = len(to_dns) > 0 + if dns_traffic_detected: + LOGGER.info("DNS traffic detected to configured DHCP DNS server") + self._dns_dhcp_server_test = True + else: + LOGGER.error("No DNS traffic detected") + + def _exec_tcpdump(self, tcpdump_filter): + """ + Args + tcpdump_filter: Filter to pass onto tcpdump file + capture_file: Optional capture file to look + Returns + List of packets matching the filter + """ + command = 'tcpdump -tttt -n -r {} {}'.format( + CAPTURE_FILE, tcpdump_filter) + + LOGGER.debug("tcpdump command: " + command) + + process = subprocess.Popen(command, + universal_newlines=True, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + text = str(process.stdout.read()).rstrip() + + LOGGER.debug("tcpdump response: " + text) + + if text: + return text.split("\n") + + return [] diff --git a/test_orc/modules/dns/python/src/logger.py b/test_orc/modules/dns/python/src/logger.py new file mode 100644 index 000000000..641aa16b4 --- /dev/null +++ b/test_orc/modules/dns/python/src/logger.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +import json +import logging +import os + +LOGGERS = {} +_LOG_FORMAT = "%(asctime)s %(name)-8s %(levelname)-7s %(message)s" +_DATE_FORMAT = '%b %02d %H:%M:%S' +_DEFAULT_LEVEL = logging.INFO +_CONF_DIR = "conf" +_CONF_FILE_NAME = "system.json" +_LOG_DIR = "/runtime/output/" + +# Set log level +try: + system_conf_json = json.load( + open(os.path.join(_CONF_DIR, _CONF_FILE_NAME))) + log_level_str = system_conf_json['log_level'] + log_level = logging.getLevelName(log_level_str) +except: + # TODO: Print out warning that log level is incorrect or missing + log_level = _DEFAULT_LEVEL + +log_format = logging.Formatter(fmt=_LOG_FORMAT, datefmt=_DATE_FORMAT) + +def add_file_handler(log, logFile): + handler = logging.FileHandler(_LOG_DIR+logFile+".log") + handler.setFormatter(log_format) + log.addHandler(handler) + + +def add_stream_handler(log): + handler = logging.StreamHandler() + handler.setFormatter(log_format) + log.addHandler(handler) + + +def get_logger(name, logFile=None): + if name not in LOGGERS: + LOGGERS[name] = logging.getLogger(name) + LOGGERS[name].setLevel(log_level) + add_stream_handler(LOGGERS[name]) + if logFile is not None: + add_file_handler(LOGGERS[name], logFile) + return LOGGERS[name] diff --git a/test_orc/modules/dns/python/src/run.py b/test_orc/modules/dns/python/src/run.py new file mode 100644 index 000000000..3eae610ed --- /dev/null +++ b/test_orc/modules/dns/python/src/run.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import argparse +import signal +import sys +import logger +import time + +from dns_module import DNSModule + +LOGGER = logger.get_logger('dns_module') +RUNTIME = 300 + +class DNSModuleRunner: + + def __init__(self,module): + + signal.signal(signal.SIGINT, self._handler) + signal.signal(signal.SIGTERM, self._handler) + signal.signal(signal.SIGABRT, self._handler) + signal.signal(signal.SIGQUIT, self._handler) + + LOGGER.info("Starting DNS Test Module") + + self._test_module = DNSModule(module) + self._test_module.run_tests() + self._test_module.generate_results() + + time.sleep(60) + + def _handler(self, signum, *other): + LOGGER.debug("SigtermEnum: " + str(signal.SIGTERM)) + LOGGER.debug("Exit signal received: " + str(signum)) + if signum in (2, signal.SIGTERM): + LOGGER.info("Exit signal received. Stopping test module...") + LOGGER.info("Test module stopped") + sys.exit(1) + +def run(argv): + parser = argparse.ArgumentParser(description="Test Module DNS", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + parser.add_argument( + "-m", "--module", help="Define the module name to be used to create the log file") + + args = parser.parse_args() + + # For some reason passing in the args from bash adds an extra + # space before the argument so we'll just strip out extra space + DNSModuleRunner(args.module.strip()) + +if __name__ == "__main__": + run(sys.argv) diff --git a/test_orc/python/src/test_orchestrator.py b/test_orc/python/src/test_orchestrator.py index f68a13579..337d5fc47 100644 --- a/test_orc/python/src/test_orchestrator.py +++ b/test_orc/python/src/test_orchestrator.py @@ -33,20 +33,20 @@ def __init__(self): def start(self): LOGGER.info("Starting Test Orchestrator") self._load_test_modules() - self._run_test_modules() + self.build_test_modules() def stop(self): """Stop any running tests""" self._stop_modules() - def _run_test_modules(self): + def _run_test_modules(self,device): """Iterates through each test module and starts the container.""" LOGGER.info("Running test modules...") for module in self._test_modules: - self._run_test_module(module) + self._run_test_module(module,device) LOGGER.info("All tests complete") - def _run_test_module(self, module): + def _run_test_module(self, module,device): """Start the test container and extract the results.""" if module is None or not module.enable_container: @@ -56,6 +56,7 @@ def _run_test_module(self, module): try: container_runtime_dir = os.path.join(self._root_path, "runtime/test/" + module.name) + network_runtime_dir = os.path.join(self._root_path, "runtime/network") os.makedirs(container_runtime_dir) client = docker.from_env() @@ -68,11 +69,19 @@ def _run_test_module(self, module): hostname=module.container_name, privileged=True, detach=True, - mounts=[Mount( - target="/runtime/output", - source=container_runtime_dir, - type='bind' - )], + mounts= [ + Mount( + target="/runtime/output", + source=container_runtime_dir, + type='bind' + ), + Mount( + target="/runtime/network", + source=network_runtime_dir, + type='bind', + read_only=True + ), + ], environment={"HOST_USER": os.getlogin()} ) except (docker.errors.APIError, docker.errors.ContainerError) as container_error: From 79c488c7a7fc990afd44c2e1379676c04ed04d04 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Mon, 1 May 2023 13:19:40 -0600 Subject: [PATCH 09/36] Add mac address of device under test to test container Update dns test to use mac address filter --- framework/testrun.py | 3 ++ test_orc/modules/dns/python/src/dns_module.py | 8 +++- test_orc/modules/dns/python/src/run.py | 2 +- test_orc/python/src/test_orchestrator.py | 45 ++++++++++--------- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/framework/testrun.py b/framework/testrun.py index 56c82d7b6..7c75aeadb 100644 --- a/framework/testrun.py +++ b/framework/testrun.py @@ -122,7 +122,10 @@ def _start_network(self): def _run_tests(self,device): """Iterate through and start all test modules.""" + + # To Do: Make this configurable time.sleep(60) # Let device bootup + self._test_orc._run_test_modules(device) def _stop_network(self,kill=False): diff --git a/test_orc/modules/dns/python/src/dns_module.py b/test_orc/modules/dns/python/src/dns_module.py index 3eac35bb8..cfc221d17 100644 --- a/test_orc/modules/dns/python/src/dns_module.py +++ b/test_orc/modules/dns/python/src/dns_module.py @@ -4,6 +4,7 @@ import time import logger import subprocess +import os LOG_NAME = "test_dns" RESULTS_DIR = "/runtime/output/" @@ -14,6 +15,7 @@ class DNSModule: def __init__(self, module): self._dns_server = "10.10.10.4" + self._device_mac = os.environ['DEVICE_MAC'] self._dns_resolution_test = False self._dns_dhcp_server_test = False self.module = module @@ -60,10 +62,12 @@ def generate_result(self, test_name, test_result): def _check_dns_traffic(self): LOGGER.info("Checking DNS traffic for DNS server: " + self._dns_server) + LOGGER.info("Inspecting DNS traffic from device: " + self._device_mac) # Check if the device has sent any DNS requests - filter_to_dns = 'dst port 53 and dst host {}'.format( - self._dns_server) + filter_to_dns = 'dst port 53 and dst host {} and ether src {}'.format( + self._dns_server, self._device_mac) + LOGGER.info("Packet Capture Filter: " + filter_to_dns) to_dns = self._exec_tcpdump(filter_to_dns) num_query_dns = len(to_dns) LOGGER.info("DNS queries found: " + str(num_query_dns)) diff --git a/test_orc/modules/dns/python/src/run.py b/test_orc/modules/dns/python/src/run.py index 3eae610ed..ac4759309 100644 --- a/test_orc/modules/dns/python/src/run.py +++ b/test_orc/modules/dns/python/src/run.py @@ -26,7 +26,7 @@ def __init__(self,module): self._test_module.run_tests() self._test_module.generate_results() - time.sleep(60) + LOGGER.info("DNS Test Module Finished") def _handler(self, signum, *other): LOGGER.debug("SigtermEnum: " + str(signal.SIGTERM)) diff --git a/test_orc/python/src/test_orchestrator.py b/test_orc/python/src/test_orchestrator.py index 337d5fc47..18de8d11a 100644 --- a/test_orc/python/src/test_orchestrator.py +++ b/test_orc/python/src/test_orchestrator.py @@ -14,6 +14,7 @@ TEST_MODULES_DIR = "modules" MODULE_CONFIG = "conf/module_config.json" + class TestOrchestrator: """Manages and controls the test modules.""" @@ -27,7 +28,8 @@ def __init__(self): # Resolve the path to the test-run folder self._root_path = os.path.abspath(os.path.join(self._path, os.pardir)) - shutil.rmtree(os.path.join(self._root_path, RUNTIME_DIR), ignore_errors=True) + shutil.rmtree(os.path.join(self._root_path, + RUNTIME_DIR), ignore_errors=True) os.makedirs(os.path.join(self._root_path, RUNTIME_DIR), exist_ok=True) def start(self): @@ -39,14 +41,14 @@ def stop(self): """Stop any running tests""" self._stop_modules() - def _run_test_modules(self,device): + def _run_test_modules(self, device): """Iterates through each test module and starts the container.""" LOGGER.info("Running test modules...") for module in self._test_modules: - self._run_test_module(module,device) + self._run_test_module(module, device) LOGGER.info("All tests complete") - def _run_test_module(self, module,device): + def _run_test_module(self, module, device): """Start the test container and extract the results.""" if module is None or not module.enable_container: @@ -55,8 +57,10 @@ def _run_test_module(self, module,device): LOGGER.info("Running test module " + module.name) try: - container_runtime_dir = os.path.join(self._root_path, "runtime/test/" + module.name) - network_runtime_dir = os.path.join(self._root_path, "runtime/network") + container_runtime_dir = os.path.join( + self._root_path, "runtime/test/" + device.mac_addr.replace(":","") + "/" + module.name) + network_runtime_dir = os.path.join( + self._root_path, "runtime/network") os.makedirs(container_runtime_dir) client = docker.from_env() @@ -69,20 +73,21 @@ def _run_test_module(self, module,device): hostname=module.container_name, privileged=True, detach=True, - mounts= [ + mounts=[ Mount( target="/runtime/output", source=container_runtime_dir, type='bind' - ), + ), Mount( target="/runtime/network", source=network_runtime_dir, type='bind', read_only=True - ), - ], - environment={"HOST_USER": os.getlogin()} + ), + ], + environment={"HOST_USER": os.getlogin( + ), "DEVICE_MAC": device.mac_addr} ) except (docker.errors.APIError, docker.errors.ContainerError) as container_error: LOGGER.error("Test module " + module.name + " has failed to start") @@ -99,7 +104,7 @@ def _run_test_module(self, module,device): LOGGER.info("Test module " + module.name + " has finished") - def _get_module_status(self,module): + def _get_module_status(self, module): container = self._get_module_container(module) if container is not None: return container.status @@ -133,11 +138,11 @@ def _load_test_modules(self): # Load basic module information module = TestModule() with open(os.path.join( - self._path, - modules_dir, - module_dir, - MODULE_CONFIG), - encoding='UTF-8') as module_config_file: + self._path, + modules_dir, + module_dir, + MODULE_CONFIG), + encoding='UTF-8') as module_config_file: module_json = json.load(module_config_file) module.name = module_json['config']['meta']['name'] @@ -159,7 +164,7 @@ def _load_test_modules(self): self._test_modules.append(module) if module.enable_container: - loaded_modules += module.dir_name + " " + loaded_modules += module.dir_name + " " LOGGER.info(loaded_modules) @@ -176,7 +181,7 @@ def _build_test_module(self, module): client.images.build( dockerfile=os.path.join(module.dir, module.build_file), path=self._path, - forcerm=True, # Cleans up intermediate containers during build + forcerm=True, # Cleans up intermediate containers during build tag=module.image_name ) except docker.errors.BuildError as error: @@ -206,4 +211,4 @@ def _stop_module(self, module, kill=False): container.stop() LOGGER.debug("Container stopped:" + module.container_name) except docker.errors.NotFound: - pass \ No newline at end of file + pass From 793ce112fb7dd98025f9fc1a540e887b3c1d4c33 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Mon, 1 May 2023 14:50:38 -0600 Subject: [PATCH 10/36] Update dns module tests --- test_orc/modules/dns/python/src/dns_module.py | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/test_orc/modules/dns/python/src/dns_module.py b/test_orc/modules/dns/python/src/dns_module.py index cfc221d17..3b19fe3b7 100644 --- a/test_orc/modules/dns/python/src/dns_module.py +++ b/test_orc/modules/dns/python/src/dns_module.py @@ -11,6 +11,7 @@ LOGGER = logger.get_logger(LOG_NAME) CAPTURE_FILE = "/runtime/network/dns.pcap" + class DNSModule: def __init__(self, module): @@ -28,7 +29,8 @@ def add_logger(self, module): # Run all the DNS Tests def run_tests(self): LOGGER.info("Checking for DNS traffic from DHCP provided server") - self._check_dns_traffic() + self._check_dns_traffic_from_device() + self._check_dns_traffic_to_server() def generate_results(self): LOGGER.info("Generating test results") @@ -60,23 +62,40 @@ def generate_result(self, test_name, test_result): } return res_dict - def _check_dns_traffic(self): - LOGGER.info("Checking DNS traffic for DNS server: " + self._dns_server) - LOGGER.info("Inspecting DNS traffic from device: " + self._device_mac) - - # Check if the device has sent any DNS requests - filter_to_dns = 'dst port 53 and dst host {} and ether src {}'.format( - self._dns_server, self._device_mac) - LOGGER.info("Packet Capture Filter: " + filter_to_dns) - to_dns = self._exec_tcpdump(filter_to_dns) + def _check_dns_traffic(self, tcpdump_filter): + to_dns = self._exec_tcpdump(tcpdump_filter) num_query_dns = len(to_dns) LOGGER.info("DNS queries found: " + str(num_query_dns)) dns_traffic_detected = len(to_dns) > 0 - if dns_traffic_detected: + LOGGER.info("DNS traffic detected: " + str(dns_traffic_detected)) + return dns_traffic_detected + + def _check_dns_traffic_to_server(self): + LOGGER.info( + "Checking DNS traffic for configured DHCP DNS server: " + self._dns_server) + + # Check if the device DNS traffic is to appropriate server + tcpdump_filter = 'dst port 53 and dst host {} and ether src {}'.format( + self._dns_server, self._device_mac) + + self._dns_dhcp_server_test = self._check_dns_traffic( + tcpdump_filter=tcpdump_filter) + + if self._dns_dhcp_server_test: LOGGER.info("DNS traffic detected to configured DHCP DNS server") - self._dns_dhcp_server_test = True - else: - LOGGER.error("No DNS traffic detected") + + def _check_dns_traffic_from_device(self): + LOGGER.info("Checking DNS traffic from device: " + self._device_mac) + + # Check if the device DNS traffic is to appropriate server + tcpdump_filter = 'dst port 53 and ether src {}'.format( + self._device_mac) + + self._dns_resolution_test = self._check_dns_traffic( + tcpdump_filter=tcpdump_filter) + + if self._dns_resolution_test: + LOGGER.info("DNS traffic detected from device") def _exec_tcpdump(self, tcpdump_filter): """ From 2f7538261be507ba3f89ad0f3779353fdc27b534 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Mon, 1 May 2023 15:49:34 -0600 Subject: [PATCH 11/36] Change result output --- test_orc/modules/dns/python/src/dns_module.py | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/test_orc/modules/dns/python/src/dns_module.py b/test_orc/modules/dns/python/src/dns_module.py index 3b19fe3b7..470cc8c45 100644 --- a/test_orc/modules/dns/python/src/dns_module.py +++ b/test_orc/modules/dns/python/src/dns_module.py @@ -17,8 +17,8 @@ class DNSModule: def __init__(self, module): self._dns_server = "10.10.10.4" self._device_mac = os.environ['DEVICE_MAC'] - self._dns_resolution_test = False - self._dns_dhcp_server_test = False + self._dns_network_from_device = None + self._dns_network_from_dhcp = None self.module = module self.add_logger(module) @@ -29,16 +29,22 @@ def add_logger(self, module): # Run all the DNS Tests def run_tests(self): LOGGER.info("Checking for DNS traffic from DHCP provided server") - self._check_dns_traffic_from_device() - self._check_dns_traffic_to_server() + self._test_dns_network_from_device() + self._test_dns_network_from_dhcp() def generate_results(self): LOGGER.info("Generating test results") results = [] results.append(self.generate_result( - "DNS resolution test", self._dns_resolution_test)) + test_name="dns.network.from_device", + test_result=self._dns_network_from_device, + description="Verify the device sends DNS requests", + expected_behavior="The device sends DNS requests.")) results.append(self.generate_result( - "DNS DHCP server test", self._dns_dhcp_server_test)) + test_name="dns.network.from_dhcp", + test_result=self._dns_network_from_dhcp, + description="Verify the device allows for a DNS server to be entered automatically", + expected_behavior="The device sends DNS requests to the DNS server provided by the DHCP server")) json_results = json.dumps({"results": results}, indent=2) self.write_results(json_results) @@ -49,7 +55,7 @@ def write_results(self, results): f.write(results) f.close() - def generate_result(self, test_name, test_result): + def generate_result(self, test_name, test_result, description, expected_behavior): if test_result is not None: result = "compliant" if test_result else "non-compliant" else: @@ -58,7 +64,8 @@ def generate_result(self, test_name, test_result): res_dict = { "name": test_name, "result": result, - "description": "The device is " + result + "description": description, + "expected_behavior":expected_behavior } return res_dict @@ -70,7 +77,7 @@ def _check_dns_traffic(self, tcpdump_filter): LOGGER.info("DNS traffic detected: " + str(dns_traffic_detected)) return dns_traffic_detected - def _check_dns_traffic_to_server(self): + def _test_dns_network_from_dhcp(self): LOGGER.info( "Checking DNS traffic for configured DHCP DNS server: " + self._dns_server) @@ -78,23 +85,23 @@ def _check_dns_traffic_to_server(self): tcpdump_filter = 'dst port 53 and dst host {} and ether src {}'.format( self._dns_server, self._device_mac) - self._dns_dhcp_server_test = self._check_dns_traffic( + self._dns_network_from_dhcp = self._check_dns_traffic( tcpdump_filter=tcpdump_filter) - if self._dns_dhcp_server_test: + if self._dns_network_from_dhcp: LOGGER.info("DNS traffic detected to configured DHCP DNS server") - def _check_dns_traffic_from_device(self): + def _test_dns_network_from_device(self): LOGGER.info("Checking DNS traffic from device: " + self._device_mac) # Check if the device DNS traffic is to appropriate server tcpdump_filter = 'dst port 53 and ether src {}'.format( self._device_mac) - self._dns_resolution_test = self._check_dns_traffic( + self._dns_network_from_device = self._check_dns_traffic( tcpdump_filter=tcpdump_filter) - if self._dns_resolution_test: + if self._dns_network_from_device: LOGGER.info("DNS traffic detected from device") def _exec_tcpdump(self, tcpdump_filter): From cdcbf7c404f58d43b6ce7e2cb824309de11421c4 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Mon, 1 May 2023 16:06:43 -0600 Subject: [PATCH 12/36] logging update --- test_orc/modules/dns/python/src/run.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test_orc/modules/dns/python/src/run.py b/test_orc/modules/dns/python/src/run.py index ac4759309..0639c0d75 100644 --- a/test_orc/modules/dns/python/src/run.py +++ b/test_orc/modules/dns/python/src/run.py @@ -8,7 +8,8 @@ from dns_module import DNSModule -LOGGER = logger.get_logger('dns_module') +LOG_NAME = "dns_module" +LOGGER = logger.get_logger(LOG_NAME) RUNTIME = 300 class DNSModuleRunner: @@ -19,6 +20,7 @@ def __init__(self,module): signal.signal(signal.SIGTERM, self._handler) signal.signal(signal.SIGABRT, self._handler) signal.signal(signal.SIGQUIT, self._handler) + self.add_logger(module) LOGGER.info("Starting DNS Test Module") @@ -28,6 +30,10 @@ def __init__(self,module): LOGGER.info("DNS Test Module Finished") + def add_logger(self, module): + global LOGGER + LOGGER = logger.get_logger(LOG_NAME, module) + def _handler(self, signum, *other): LOGGER.debug("SigtermEnum: " + str(signal.SIGTERM)) LOGGER.debug("Exit signal received: " + str(signum)) From a9496a4e77e896d6983b455fd604c29a4418bec3 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Tue, 2 May 2023 13:07:36 -0600 Subject: [PATCH 13/36] Update test module for better reusability --- .../modules/base/python/src/test_module.py | 54 +++++++++++++ test_orc/modules/dns/conf/module_config.json | 14 +++- test_orc/modules/dns/python/src/dns_module.py | 81 ++++--------------- test_orc/modules/dns/python/src/run.py | 2 +- 4 files changed, 83 insertions(+), 68 deletions(-) create mode 100644 test_orc/modules/base/python/src/test_module.py diff --git a/test_orc/modules/base/python/src/test_module.py b/test_orc/modules/base/python/src/test_module.py new file mode 100644 index 000000000..f1d3650d6 --- /dev/null +++ b/test_orc/modules/base/python/src/test_module.py @@ -0,0 +1,54 @@ +import json +import logger +import os + +LOGGER = None +RESULTS_DIR = "/runtime/output/" +CONF_DIR = "/testrun/conf" + +class TestModule: + + def __init__(self,module_name, log_name): + self._module_name=module_name + self._device_mac = os.environ['DEVICE_MAC'] + self._add_logger(log_name=log_name,module_name=module_name) + + def _add_logger(self, log_name, module_name): + global LOGGER + LOGGER = logger.get_logger(log_name, module_name) + + def _get_logger(self): + return LOGGER + + def run_tests(self): + tests = [] + tests.append({"name": "dns.network.from_device", + "description": "", "expected_behavior": ""}) + tests.append({"name": "dns.network.from_dhcp", + "description": "", "expected_behavior": ""}) + for test in tests: + test_method_name = "_" + test["name"].replace(".", "_") + LOGGER.info("Attempting to run test: " + test_method_name) + + # Resolve the correct python method by test name and run test + if hasattr(self, test_method_name): + result = getattr(self, test_method_name)() + else: + LOGGER.info("Test method " + test_method_name + + " not resolved. Skipping") + result = None + + if result is not None: + test["result"] = "compliant" if result else "non-compliant" + else: + test["result"] = "skipped" + LOGGER.info(test["name"] + ": " + str(result)) + json_results = json.dumps({"results": tests}, indent=2) + self._write_results(json_results) + + def _write_results(self, results): + results_file = RESULTS_DIR + self._module_name + "-result.json" + LOGGER.info("Writing results to " + results_file) + f = open(results_file, "w", encoding="utf-8") + f.write(results) + f.close() diff --git a/test_orc/modules/dns/conf/module_config.json b/test_orc/modules/dns/conf/module_config.json index d5b7c54d9..5bf20fc84 100644 --- a/test_orc/modules/dns/conf/module_config.json +++ b/test_orc/modules/dns/conf/module_config.json @@ -13,6 +13,18 @@ "docker": { "enable_container": true, "timeout": 30 - } + }, + "tests":[ + { + "name": "dns.network.from_device", + "description": "Verify the device sends DNS requests", + "expected_behavior": "The device sends DNS requests." + }, + { + "name": "dns.network.from_dhcp", + "description": "Verify the device allows for a DNS server to be entered automatically", + "expected_behavior": "The device sends DNS requests to the DNS server provided by the DHCP server" + } + ] } } \ No newline at end of file diff --git a/test_orc/modules/dns/python/src/dns_module.py b/test_orc/modules/dns/python/src/dns_module.py index 470cc8c45..22f82f93a 100644 --- a/test_orc/modules/dns/python/src/dns_module.py +++ b/test_orc/modules/dns/python/src/dns_module.py @@ -1,73 +1,23 @@ #!/usr/bin/env python3 import json -import time -import logger import subprocess import os +from test_module import TestModule + LOG_NAME = "test_dns" RESULTS_DIR = "/runtime/output/" -LOGGER = logger.get_logger(LOG_NAME) CAPTURE_FILE = "/runtime/network/dns.pcap" +LOGGER = None - -class DNSModule: +class DNSModule(TestModule): def __init__(self, module): + super().__init__(module_name=module, log_name=LOG_NAME) self._dns_server = "10.10.10.4" - self._device_mac = os.environ['DEVICE_MAC'] - self._dns_network_from_device = None - self._dns_network_from_dhcp = None - self.module = module - self.add_logger(module) - - def add_logger(self, module): global LOGGER - LOGGER = logger.get_logger(LOG_NAME, module) - - # Run all the DNS Tests - def run_tests(self): - LOGGER.info("Checking for DNS traffic from DHCP provided server") - self._test_dns_network_from_device() - self._test_dns_network_from_dhcp() - - def generate_results(self): - LOGGER.info("Generating test results") - results = [] - results.append(self.generate_result( - test_name="dns.network.from_device", - test_result=self._dns_network_from_device, - description="Verify the device sends DNS requests", - expected_behavior="The device sends DNS requests.")) - results.append(self.generate_result( - test_name="dns.network.from_dhcp", - test_result=self._dns_network_from_dhcp, - description="Verify the device allows for a DNS server to be entered automatically", - expected_behavior="The device sends DNS requests to the DNS server provided by the DHCP server")) - json_results = json.dumps({"results": results}, indent=2) - self.write_results(json_results) - - def write_results(self, results): - results_file = RESULTS_DIR + self.module + "-result.json" - LOGGER.info("Writing results to " + results_file) - f = open(results_file, "w", encoding="utf-8") - f.write(results) - f.close() - - def generate_result(self, test_name, test_result, description, expected_behavior): - if test_result is not None: - result = "compliant" if test_result else "non-compliant" - else: - result = "skipped" - LOGGER.info(test_name + ": " + result) - res_dict = { - "name": test_name, - "result": result, - "description": description, - "expected_behavior":expected_behavior - } - return res_dict + LOGGER = self._get_logger() def _check_dns_traffic(self, tcpdump_filter): to_dns = self._exec_tcpdump(tcpdump_filter) @@ -77,7 +27,7 @@ def _check_dns_traffic(self, tcpdump_filter): LOGGER.info("DNS traffic detected: " + str(dns_traffic_detected)) return dns_traffic_detected - def _test_dns_network_from_dhcp(self): + def _dns_network_from_dhcp(self): LOGGER.info( "Checking DNS traffic for configured DHCP DNS server: " + self._dns_server) @@ -85,24 +35,23 @@ def _test_dns_network_from_dhcp(self): tcpdump_filter = 'dst port 53 and dst host {} and ether src {}'.format( self._dns_server, self._device_mac) - self._dns_network_from_dhcp = self._check_dns_traffic( - tcpdump_filter=tcpdump_filter) + result = self._check_dns_traffic(tcpdump_filter=tcpdump_filter) - if self._dns_network_from_dhcp: - LOGGER.info("DNS traffic detected to configured DHCP DNS server") + LOGGER.info( + "DNS traffic detected to configured DHCP DNS server: " + str(result)) + return result - def _test_dns_network_from_device(self): + def _dns_network_from_device(self): LOGGER.info("Checking DNS traffic from device: " + self._device_mac) # Check if the device DNS traffic is to appropriate server tcpdump_filter = 'dst port 53 and ether src {}'.format( self._device_mac) - self._dns_network_from_device = self._check_dns_traffic( - tcpdump_filter=tcpdump_filter) + result = self._check_dns_traffic(tcpdump_filter=tcpdump_filter) - if self._dns_network_from_device: - LOGGER.info("DNS traffic detected from device") + LOGGER.info("DNS traffic detected from device: " + str(result)) + return result def _exec_tcpdump(self, tcpdump_filter): """ diff --git a/test_orc/modules/dns/python/src/run.py b/test_orc/modules/dns/python/src/run.py index 0639c0d75..c2c77b81c 100644 --- a/test_orc/modules/dns/python/src/run.py +++ b/test_orc/modules/dns/python/src/run.py @@ -26,7 +26,7 @@ def __init__(self,module): self._test_module = DNSModule(module) self._test_module.run_tests() - self._test_module.generate_results() + #self._test_module.generate_results() LOGGER.info("DNS Test Module Finished") From 39c3340ef48712dfb9608c9fba25c00e0b4fa015 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Tue, 2 May 2023 13:40:40 -0600 Subject: [PATCH 14/36] Load in module config to test module --- test_orc/modules/base/python/src/test_module.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/test_orc/modules/base/python/src/test_module.py b/test_orc/modules/base/python/src/test_module.py index f1d3650d6..3bfb6ea8a 100644 --- a/test_orc/modules/base/python/src/test_module.py +++ b/test_orc/modules/base/python/src/test_module.py @@ -4,7 +4,7 @@ LOGGER = None RESULTS_DIR = "/runtime/output/" -CONF_DIR = "/testrun/conf" +CONF_FILE = "/testrun/conf/module_config.json" class TestModule: @@ -12,6 +12,8 @@ def __init__(self,module_name, log_name): self._module_name=module_name self._device_mac = os.environ['DEVICE_MAC'] self._add_logger(log_name=log_name,module_name=module_name) + self._config = self._read_config() + LOGGER.info("Config:\n" + str(self._config)) def _add_logger(self, log_name, module_name): global LOGGER @@ -21,11 +23,7 @@ def _get_logger(self): return LOGGER def run_tests(self): - tests = [] - tests.append({"name": "dns.network.from_device", - "description": "", "expected_behavior": ""}) - tests.append({"name": "dns.network.from_dhcp", - "description": "", "expected_behavior": ""}) + tests = self._config["config"]["tests"] for test in tests: test_method_name = "_" + test["name"].replace(".", "_") LOGGER.info("Attempting to run test: " + test_method_name) @@ -46,6 +44,12 @@ def run_tests(self): json_results = json.dumps({"results": tests}, indent=2) self._write_results(json_results) + def _read_config(self): + f = open(CONF_FILE, encoding="utf-8") + config = json.load(f) + f.close() + return config + def _write_results(self, results): results_file = RESULTS_DIR + self._module_name + "-result.json" LOGGER.info("Writing results to " + results_file) From b37ef3527b9ff55cfa5a5883427552719a59b9c8 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Tue, 2 May 2023 13:54:04 -0600 Subject: [PATCH 15/36] logging cleanup --- test_orc/modules/base/python/src/logger.py | 17 +++---- .../modules/base/python/src/test_module.py | 1 - .../modules/baseline/python/src/logger.py | 46 ------------------- test_orc/modules/dns/python/src/logger.py | 46 ------------------- 4 files changed, 9 insertions(+), 101 deletions(-) delete mode 100644 test_orc/modules/baseline/python/src/logger.py delete mode 100644 test_orc/modules/dns/python/src/logger.py diff --git a/test_orc/modules/base/python/src/logger.py b/test_orc/modules/base/python/src/logger.py index 0eb7b9ccf..641aa16b4 100644 --- a/test_orc/modules/base/python/src/logger.py +++ b/test_orc/modules/base/python/src/logger.py @@ -10,12 +10,12 @@ _DEFAULT_LEVEL = logging.INFO _CONF_DIR = "conf" _CONF_FILE_NAME = "system.json" -_LOG_DIR = "/runtime/network/" +_LOG_DIR = "/runtime/output/" # Set log level try: system_conf_json = json.load( - open(os.path.join(_CONF_DIR, _CONF_FILE_NAME), encoding='utf-8')) + open(os.path.join(_CONF_DIR, _CONF_FILE_NAME))) log_level_str = system_conf_json['log_level'] log_level = logging.getLevelName(log_level_str) except: @@ -24,22 +24,23 @@ log_format = logging.Formatter(fmt=_LOG_FORMAT, datefmt=_DATE_FORMAT) - -def add_file_handler(log, log_file): - handler = logging.FileHandler(_LOG_DIR+log_file+".log") +def add_file_handler(log, logFile): + handler = logging.FileHandler(_LOG_DIR+logFile+".log") handler.setFormatter(log_format) log.addHandler(handler) + def add_stream_handler(log): handler = logging.StreamHandler() handler.setFormatter(log_format) log.addHandler(handler) -def get_logger(name, log_file=None): + +def get_logger(name, logFile=None): if name not in LOGGERS: LOGGERS[name] = logging.getLogger(name) LOGGERS[name].setLevel(log_level) add_stream_handler(LOGGERS[name]) - if log_file is not None: - add_file_handler(LOGGERS[name], log_file) + if logFile is not None: + add_file_handler(LOGGERS[name], logFile) return LOGGERS[name] diff --git a/test_orc/modules/base/python/src/test_module.py b/test_orc/modules/base/python/src/test_module.py index 3bfb6ea8a..389103ed9 100644 --- a/test_orc/modules/base/python/src/test_module.py +++ b/test_orc/modules/base/python/src/test_module.py @@ -13,7 +13,6 @@ def __init__(self,module_name, log_name): self._device_mac = os.environ['DEVICE_MAC'] self._add_logger(log_name=log_name,module_name=module_name) self._config = self._read_config() - LOGGER.info("Config:\n" + str(self._config)) def _add_logger(self, log_name, module_name): global LOGGER diff --git a/test_orc/modules/baseline/python/src/logger.py b/test_orc/modules/baseline/python/src/logger.py deleted file mode 100644 index 641aa16b4..000000000 --- a/test_orc/modules/baseline/python/src/logger.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 - -import json -import logging -import os - -LOGGERS = {} -_LOG_FORMAT = "%(asctime)s %(name)-8s %(levelname)-7s %(message)s" -_DATE_FORMAT = '%b %02d %H:%M:%S' -_DEFAULT_LEVEL = logging.INFO -_CONF_DIR = "conf" -_CONF_FILE_NAME = "system.json" -_LOG_DIR = "/runtime/output/" - -# Set log level -try: - system_conf_json = json.load( - open(os.path.join(_CONF_DIR, _CONF_FILE_NAME))) - log_level_str = system_conf_json['log_level'] - log_level = logging.getLevelName(log_level_str) -except: - # TODO: Print out warning that log level is incorrect or missing - log_level = _DEFAULT_LEVEL - -log_format = logging.Formatter(fmt=_LOG_FORMAT, datefmt=_DATE_FORMAT) - -def add_file_handler(log, logFile): - handler = logging.FileHandler(_LOG_DIR+logFile+".log") - handler.setFormatter(log_format) - log.addHandler(handler) - - -def add_stream_handler(log): - handler = logging.StreamHandler() - handler.setFormatter(log_format) - log.addHandler(handler) - - -def get_logger(name, logFile=None): - if name not in LOGGERS: - LOGGERS[name] = logging.getLogger(name) - LOGGERS[name].setLevel(log_level) - add_stream_handler(LOGGERS[name]) - if logFile is not None: - add_file_handler(LOGGERS[name], logFile) - return LOGGERS[name] diff --git a/test_orc/modules/dns/python/src/logger.py b/test_orc/modules/dns/python/src/logger.py deleted file mode 100644 index 641aa16b4..000000000 --- a/test_orc/modules/dns/python/src/logger.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 - -import json -import logging -import os - -LOGGERS = {} -_LOG_FORMAT = "%(asctime)s %(name)-8s %(levelname)-7s %(message)s" -_DATE_FORMAT = '%b %02d %H:%M:%S' -_DEFAULT_LEVEL = logging.INFO -_CONF_DIR = "conf" -_CONF_FILE_NAME = "system.json" -_LOG_DIR = "/runtime/output/" - -# Set log level -try: - system_conf_json = json.load( - open(os.path.join(_CONF_DIR, _CONF_FILE_NAME))) - log_level_str = system_conf_json['log_level'] - log_level = logging.getLevelName(log_level_str) -except: - # TODO: Print out warning that log level is incorrect or missing - log_level = _DEFAULT_LEVEL - -log_format = logging.Formatter(fmt=_LOG_FORMAT, datefmt=_DATE_FORMAT) - -def add_file_handler(log, logFile): - handler = logging.FileHandler(_LOG_DIR+logFile+".log") - handler.setFormatter(log_format) - log.addHandler(handler) - - -def add_stream_handler(log): - handler = logging.StreamHandler() - handler.setFormatter(log_format) - log.addHandler(handler) - - -def get_logger(name, logFile=None): - if name not in LOGGERS: - LOGGERS[name] = logging.getLogger(name) - LOGGERS[name].setLevel(log_level) - add_stream_handler(LOGGERS[name]) - if logFile is not None: - add_file_handler(LOGGERS[name], logFile) - return LOGGERS[name] From 4a6b50cf42e649529a46edf4b74ef8e2ddc6b391 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Tue, 2 May 2023 14:43:35 -0600 Subject: [PATCH 16/36] Update baseline module to new template Misc cleanup --- .../modules/baseline/conf/module_config.json | 22 +++++-- .../baseline/python/src/baseline_module.py | 31 ++++++++++ test_orc/modules/baseline/python/src/run.py | 13 ++-- .../baseline/python/src/test_module.py | 61 ------------------- test_orc/modules/dns/python/src/dns_module.py | 4 -- test_orc/modules/dns/python/src/run.py | 1 - 6 files changed, 55 insertions(+), 77 deletions(-) create mode 100644 test_orc/modules/baseline/python/src/baseline_module.py delete mode 100644 test_orc/modules/baseline/python/src/test_module.py diff --git a/test_orc/modules/baseline/conf/module_config.json b/test_orc/modules/baseline/conf/module_config.json index 1b8b7b9ba..e06fcb203 100644 --- a/test_orc/modules/baseline/conf/module_config.json +++ b/test_orc/modules/baseline/conf/module_config.json @@ -10,12 +10,26 @@ "enable_wan": false, "ip_index": 9 }, - "grpc": { - "port": 50001 - }, "docker": { "enable_container": true, "timeout": 30 - } + }, + "tests":[ + { + "name": "baseline.pass", + "description": "Simulate a compliant test", + "expected_behavior": "A compliant test result is generated" + }, + { + "name": "baseline.fail", + "description": "Simulate a non-compliant test", + "expected_behavior": "A non-compliant test result is generated" + }, + { + "name": "baseline.skip", + "description": "Simulate a skipped test", + "expected_behavior": "A skipped test result is generated" + } + ] } } \ No newline at end of file diff --git a/test_orc/modules/baseline/python/src/baseline_module.py b/test_orc/modules/baseline/python/src/baseline_module.py new file mode 100644 index 000000000..80c04ef48 --- /dev/null +++ b/test_orc/modules/baseline/python/src/baseline_module.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +from test_module import TestModule + +LOG_NAME = "test_baseline" +LOGGER = None + +class BaselineModule(TestModule): + + def __init__(self, module): + super().__init__(module_name=module, log_name=LOG_NAME) + global LOGGER + LOGGER = self._get_logger() + + def _baseline_pass(self): + LOGGER.info( + "Running baseline pass test") + LOGGER.info("Baseline pass test finished") + return True + + def _baseline_fail(self): + LOGGER.info( + "Running baseline pass test") + LOGGER.info("Baseline pass test finished") + return False + + def _baseline_skip(self): + LOGGER.info( + "Running baseline pass test") + LOGGER.info("Baseline pass test finished") + return None \ No newline at end of file diff --git a/test_orc/modules/baseline/python/src/run.py b/test_orc/modules/baseline/python/src/run.py index 7ff11559f..ffa171e17 100644 --- a/test_orc/modules/baseline/python/src/run.py +++ b/test_orc/modules/baseline/python/src/run.py @@ -5,12 +5,12 @@ import sys import logger -from test_module import TestModule +from baseline_module import BaselineModule LOGGER = logger.get_logger('test_module') RUNTIME = 300 -class TestModuleRunner: +class BaselineModuleRunner: def __init__(self,module): @@ -19,11 +19,10 @@ def __init__(self,module): signal.signal(signal.SIGABRT, self._handler) signal.signal(signal.SIGQUIT, self._handler) - LOGGER.info("Starting Test Module Template") + LOGGER.info("Starting Baseline Module") - self._test_module = TestModule(module) + self._test_module = BaselineModule(module) self._test_module.run_tests() - self._test_module.generate_results() def _handler(self, signum, *other): LOGGER.debug("SigtermEnum: " + str(signal.SIGTERM)) @@ -34,7 +33,7 @@ def _handler(self, signum, *other): sys.exit(1) def run(argv): - parser = argparse.ArgumentParser(description="Test Module Template", + parser = argparse.ArgumentParser(description="Baseline Module Help", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument( @@ -44,7 +43,7 @@ def run(argv): # For some reason passing in the args from bash adds an extra # space before the argument so we'll just strip out extra space - TestModuleRunner(args.module.strip()) + BaselineModuleRunner(args.module.strip()) if __name__ == "__main__": run(sys.argv) diff --git a/test_orc/modules/baseline/python/src/test_module.py b/test_orc/modules/baseline/python/src/test_module.py deleted file mode 100644 index d4065cde3..000000000 --- a/test_orc/modules/baseline/python/src/test_module.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env python3 - -import json -import time -import logger - -LOG_NAME = "test_baseline" -RESULTS_DIR = "/runtime/output/" -LOGGER = logger.get_logger(LOG_NAME) - -class TestModule: - - def __init__(self, module): - - self.module_test1 = None - self.module_test2 = None - self.module_test3 = None - self.module = module - self.add_logger(module) - - def add_logger(self, module): - global LOGGER - LOGGER = logger.get_logger(LOG_NAME, module) - - # Make up some fake test results - def run_tests(self): - LOGGER.info("Running test 1...") - self.module_test1 = True - LOGGER.info("Test 1 complete.") - - LOGGER.info("Running test 2...") - self.module_test2 = False - LOGGER.info("Test 2 complete.") - - def generate_results(self): - results = [] - results.append(self.generate_result("Test 1", self.module_test1)) - results.append(self.generate_result("Test 2", self.module_test2)) - results.append(self.generate_result("Test 3", self.module_test3)) - json_results = json.dumps({"results":results}, indent=2) - self.write_results(json_results) - - def write_results(self,results): - results_file=RESULTS_DIR+self.module+"-result.json" - LOGGER.info("Writing results to " + results_file) - f = open(results_file, "w", encoding="utf-8") - f.write(results) - f.close() - - def generate_result(self, test_name, test_result): - if test_result is not None: - result = "compliant" if test_result else "non-compliant" - else: - result = "skipped" - LOGGER.info(test_name + ": " + result) - res_dict = { - "name": test_name, - "result": result, - "description": "The device is " + result - } - return res_dict diff --git a/test_orc/modules/dns/python/src/dns_module.py b/test_orc/modules/dns/python/src/dns_module.py index 22f82f93a..f1333ce14 100644 --- a/test_orc/modules/dns/python/src/dns_module.py +++ b/test_orc/modules/dns/python/src/dns_module.py @@ -1,13 +1,9 @@ #!/usr/bin/env python3 -import json import subprocess -import os - from test_module import TestModule LOG_NAME = "test_dns" -RESULTS_DIR = "/runtime/output/" CAPTURE_FILE = "/runtime/network/dns.pcap" LOGGER = None diff --git a/test_orc/modules/dns/python/src/run.py b/test_orc/modules/dns/python/src/run.py index c2c77b81c..7ee5e7833 100644 --- a/test_orc/modules/dns/python/src/run.py +++ b/test_orc/modules/dns/python/src/run.py @@ -26,7 +26,6 @@ def __init__(self,module): self._test_module = DNSModule(module) self._test_module.run_tests() - #self._test_module.generate_results() LOGGER.info("DNS Test Module Finished") From ee6892410364a646ca6148959a2815b4f624ab22 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Tue, 2 May 2023 16:40:22 -0600 Subject: [PATCH 17/36] Add ability to disable individual tests --- framework/device.py | 1 + framework/testrun.py | 6 +- local/devices/MyLaptop/device_config.json | 18 +++++- .../modules/base/python/src/test_module.py | 56 ++++++++++++++----- test_orc/python/src/test_orchestrator.py | 7 ++- 5 files changed, 68 insertions(+), 20 deletions(-) diff --git a/framework/device.py b/framework/device.py index 08014c127..9fbf0d2d6 100644 --- a/framework/device.py +++ b/framework/device.py @@ -8,3 +8,4 @@ class Device: make: str model: str mac_addr: str + test_modules: str diff --git a/framework/testrun.py b/framework/testrun.py index 7c75aeadb..fcf8f58df 100644 --- a/framework/testrun.py +++ b/framework/testrun.py @@ -29,6 +29,7 @@ DEVICE_MAKE = 'make' DEVICE_MODEL = 'model' DEVICE_MAC_ADDR = 'mac_addr' +DEVICE_TEST_MODULES = 'test_modules' class TestRun: # pylint: disable=too-few-public-methods @@ -145,9 +146,10 @@ def _load_devices(self): device_make = device_config_json.get(DEVICE_MAKE) device_model = device_config_json.get(DEVICE_MODEL) mac_addr = device_config_json.get(DEVICE_MAC_ADDR) + test_modules = device_config_json.get(DEVICE_TEST_MODULES) - device = Device(device_make, device_model, - mac_addr=mac_addr) + device = Device(make=device_make, model=device_model, + mac_addr=mac_addr,test_modules=json.dumps(test_modules)) self._devices.append(device) LOGGER.info('Loaded ' + str(len(self._devices)) + ' devices') diff --git a/local/devices/MyLaptop/device_config.json b/local/devices/MyLaptop/device_config.json index c0bb15cc7..0f5e03d6d 100644 --- a/local/devices/MyLaptop/device_config.json +++ b/local/devices/MyLaptop/device_config.json @@ -1,5 +1,17 @@ { - "make": "Microsoft", - "model": "Surface Laptop 3", - "mac_addr": "3c:8c:f8:ea:4a:cf" + "make": "Microsoft", + "model": "Surface Laptop 3", + "mac_addr": "3c:8c:f8:ea:4a:cf", + "test_modules": [ + { + "name": "dns", + "enabled": true, + "tests": [ + { + "name": "dns.network.from_device", + "enabled": false + } + ] + } + ] } \ No newline at end of file diff --git a/test_orc/modules/base/python/src/test_module.py b/test_orc/modules/base/python/src/test_module.py index 389103ed9..0db4cc602 100644 --- a/test_orc/modules/base/python/src/test_module.py +++ b/test_orc/modules/base/python/src/test_module.py @@ -6,12 +6,13 @@ RESULTS_DIR = "/runtime/output/" CONF_FILE = "/testrun/conf/module_config.json" + class TestModule: - def __init__(self,module_name, log_name): - self._module_name=module_name + def __init__(self, module_name, log_name): + self._module_name = module_name self._device_mac = os.environ['DEVICE_MAC'] - self._add_logger(log_name=log_name,module_name=module_name) + self._add_logger(log_name=log_name, module_name=module_name) self._config = self._read_config() def _add_logger(self, log_name, module_name): @@ -21,20 +22,49 @@ def _add_logger(self, log_name, module_name): def _get_logger(self): return LOGGER + def _get_tests(self): + device_test_module = self._get_device_test_module() + return self._get_device_tests(device_test_module) + + def _get_device_tests(self, device_test_module): + module_tests = self._config["config"]["tests"] + if device_test_module is None: + return module_tests + elif not device_test_module["enabled"]: + return [] + else: + for test in module_tests: + for dev_test in device_test_module["tests"]: + if test["name"] == dev_test["name"]: + test["enabled"] = dev_test["enabled"] + return module_tests + + def _get_device_test_module(self): + device_modules = json.loads(os.environ['DEVICE_TEST_MODULES']) + for module in device_modules: + if self._module_name == module["name"]: + return module + return None + def run_tests(self): - tests = self._config["config"]["tests"] + tests = self._get_tests() + device_modules = os.environ['DEVICE_TEST_MODULES'] for test in tests: - test_method_name = "_" + test["name"].replace(".", "_") - LOGGER.info("Attempting to run test: " + test_method_name) + test_method_name = "_" + test["name"].replace(".", "_") + result = None + if ("enabled" in test and test["enabled"]) or "enabled" not in test: + LOGGER.info("Attempting to run test: " + test["name"]) - # Resolve the correct python method by test name and run test - if hasattr(self, test_method_name): - result = getattr(self, test_method_name)() + # Resolve the correct python method by test name and run test + if hasattr(self, test_method_name): + result = getattr(self, test_method_name)() + else: + LOGGER.info("Test " + test["name"] + + " not resolved. Skipping") + result = None else: - LOGGER.info("Test method " + test_method_name + - " not resolved. Skipping") - result = None - + LOGGER.info("Test " + test["name"] + + " disabled. Skipping") if result is not None: test["result"] = "compliant" if result else "non-compliant" else: diff --git a/test_orc/python/src/test_orchestrator.py b/test_orc/python/src/test_orchestrator.py index 18de8d11a..c4a8a823a 100644 --- a/test_orc/python/src/test_orchestrator.py +++ b/test_orc/python/src/test_orchestrator.py @@ -86,8 +86,11 @@ def _run_test_module(self, module, device): read_only=True ), ], - environment={"HOST_USER": os.getlogin( - ), "DEVICE_MAC": device.mac_addr} + environment={ + "HOST_USER": os.getlogin(), + "DEVICE_MAC": device.mac_addr, + "DEVICE_TEST_MODULES": device.test_modules + } ) except (docker.errors.APIError, docker.errors.ContainerError) as container_error: LOGGER.error("Test module " + module.name + " has failed to start") From 4ecb1f57e71352e506bbbc1cecf229195bde986f Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Wed, 3 May 2023 07:55:11 -0600 Subject: [PATCH 18/36] remove duplicate readme --- net_orc/README.md | 66 ----------------------------------------------- 1 file changed, 66 deletions(-) delete mode 100644 net_orc/README.md diff --git a/net_orc/README.md b/net_orc/README.md deleted file mode 100644 index 9cb1eec1a..000000000 --- a/net_orc/README.md +++ /dev/null @@ -1,66 +0,0 @@ -Testrun logo - -## Introduction :wave: -The network orchestrator is a tool to automate the management of a test lab network and provide essential services to begin device testing in just a few minutes. - -## Motivation :bulb: -Test labs may be maintaining a large and complex network using equipment such as: A managed layer 3 switch, an enterprise-grade network router, virtualized or physical servers to provide DNS, NTP, 802.1x etc. With this amount of moving parts, all with dynamic configuration files and constant software updates, more time is likely to be spent on preparation and clean up of functinality or penetration testing - not forgetting the number of software tools required to perform the testing. - -## How it works :triangular_ruler: -The network orchestrator creates an isolated and controlled network environment to fully simulate enterprise network deployments in your device testing lab. -This removes the necessity for complex hardware, advanced knowledge and networking experience whilst enabling semi-technical engineers to validate device -behaviour against industry cyber standards. - -The network orchestrator will provide the network and some tools to assist an engineer performing the additional testing. At the same time, packet captures of the device behaviour will be recorded, alongside logs for each network service, for further debugging. - -## Minimum Requirements :computer: -### Hardware - - PC running Ubuntu LTS (laptop or desktop) - - 2x USB ethernet adapter (One may be built in ethernet) - - Connect one adapter to your router (for internet access) - - Connect one adapter to your device under test - - Internet connection -### Software - - Python 3 with pip3 (Already available on Ubuntu LTS) - - Docker - [Install guide](https://docs.docker.com/engine/install/ubuntu/) - - Open vSwitch ``sudo apt-get install openvswitch-common openvswitch-switch`` - -An additional network interface (even wifi) with internet access can be used to maintain internet connection during use of the network orchestrator. - -## How to use :arrow_forward: -1) Ensure you have a device with the minimum hardware and software requirements setup -2) Clone the project using ```git clone https://github.com/auto-iot/network-orchestrator``` -3) Navigate into the project using ```cd network-orchestrator``` -4) Copy conf/system.json.example to conf/system.json (after setting the correct interfaces in the file) -5) Start the tool using ```sudo cmd/start``` - -## Issue reporting :triangular_flag_on_post: -If the application has come across a problem at any point during setup or use, please raise an issue under the [issues tab](https://github.com/auto-iot/network-orchestrator/issues). Issue templates exist for both bug reports and feature requests. If neither of these are appropriate for your issue, raise a blank issue instead. - -## Roadmap :chart_with_upwards_trend: - - Ability to modify configuration files of each network service during use (via GRPC) - - IPv6 internet routing - -## Contributing :keyboard: -The contributing requirements can be found in [CONTRIBUTING.md](CONTRIBUTING.md). In short, checkout the [Google CLA](https://cla.developers.google.com/) site to get started. - -## FAQ :raising_hand: -1) What services are provided on the virtual network? - - The following are network services that are containerized and accessible to the device under test though are likely to change over time: - - DHCP in failover configuration with internet connectivity - - IPv6 router advertisements - - DNS (and DNS over HTTPS) - - NTPv4 - - 802.1x Port Based Authentication - -2) Can I run the network orchestrator on a virtual machine? - - Probably. Provided that the required 2x USB ethernet adapters are passed to the virtual machine as USB devices rather than network adapters, the tool should - still work. We will look to test and approve the use of virtualisation in the future. - -3) Can I connect multiple devices to the Network Orchestrator? - - In short, Yes you can. The way in which multiple devices could be tested simultaneously is yet to be decided. However, if you simply want to add field/peer devices during runtime (even another laptop performing manual testing) then you may connect the USB ethernet adapter to an unmanaged switch. - -4) Raise an issue with the label 'question' if your question has not been answered in this readme. \ No newline at end of file From 84934167cbf2e0669624936b51d7b21e27eef182 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Wed, 3 May 2023 09:07:33 -0600 Subject: [PATCH 19/36] Update device directories --- .gitignore | 2 ++ framework/testrun.py | 17 +++++++++++------ .../devices/Teltonika TRB140/device_config.json | 5 ----- .../devices/Teltonika TRB140/device_config.json | 5 +++++ 4 files changed, 18 insertions(+), 11 deletions(-) delete mode 100644 local/devices/Teltonika TRB140/device_config.json create mode 100644 resources/devices/Teltonika TRB140/device_config.json diff --git a/.gitignore b/.gitignore index 15aae1278..db1580ffb 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ runtime/ venv/ .vscode/ +local/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/framework/testrun.py b/framework/testrun.py index fcf8f58df..07a217921 100644 --- a/framework/testrun.py +++ b/framework/testrun.py @@ -24,7 +24,8 @@ EXAMPLE_CONFIG_FILE = "conf/system.json.example" RUNTIME = 300 -DEVICES_DIR = 'local/devices' +LOCAL_DEVICES_DIR = 'local/devices' +RESOURCE_DEVICES_DIR = 'resources/devices' DEVICE_CONFIG = 'device_config.json' DEVICE_MAKE = 'make' DEVICE_MODEL = 'model' @@ -57,7 +58,7 @@ def __init__(self, config_file=CONFIG_FILE,validate=True, net_only=False): def start(self): - self._load_devices() + self._load_all_devices() if self._net_only: LOGGER.info("Network only option configured, no tests will be run") @@ -135,11 +136,15 @@ def _stop_network(self,kill=False): def _stop_tests(self): self._test_orc.stop() - def _load_devices(self): - LOGGER.debug('Loading devices from ' + DEVICES_DIR) + def _load_all_devices(self): + self._load_devices(device_dir=LOCAL_DEVICES_DIR) + self._load_devices(device_dir=RESOURCE_DEVICES_DIR) - for device_folder in os.listdir(DEVICES_DIR): - with open(os.path.join(DEVICES_DIR, device_folder, DEVICE_CONFIG), + def _load_devices(self,device_dir): + LOGGER.debug('Loading devices from ' + device_dir) + + for device_folder in os.listdir(device_dir): + with open(os.path.join(device_dir, device_folder, DEVICE_CONFIG), encoding='utf-8') as device_config_file: device_config_json = json.load(device_config_file) diff --git a/local/devices/Teltonika TRB140/device_config.json b/local/devices/Teltonika TRB140/device_config.json deleted file mode 100644 index 3ac414a5a..000000000 --- a/local/devices/Teltonika TRB140/device_config.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "make": "Microsoft", - "model": "Surface Laptop 3", - "mac_addr": "00:1e:42:35:73:c4" -} \ No newline at end of file diff --git a/resources/devices/Teltonika TRB140/device_config.json b/resources/devices/Teltonika TRB140/device_config.json new file mode 100644 index 000000000..759c1e9b4 --- /dev/null +++ b/resources/devices/Teltonika TRB140/device_config.json @@ -0,0 +1,5 @@ +{ + "make": "Teltonika", + "model": "TRB140", + "mac_addr": "00:1e:42:35:73:c4" +} \ No newline at end of file From 8c4b3a9c69007352e9544ff519ee2e58d842eeea Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Wed, 3 May 2023 09:11:01 -0600 Subject: [PATCH 20/36] Remove local folder --- local/devices/MyLaptop/device_config.json | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 local/devices/MyLaptop/device_config.json diff --git a/local/devices/MyLaptop/device_config.json b/local/devices/MyLaptop/device_config.json deleted file mode 100644 index 0f5e03d6d..000000000 --- a/local/devices/MyLaptop/device_config.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "make": "Microsoft", - "model": "Surface Laptop 3", - "mac_addr": "3c:8c:f8:ea:4a:cf", - "test_modules": [ - { - "name": "dns", - "enabled": true, - "tests": [ - { - "name": "dns.network.from_device", - "enabled": false - } - ] - } - ] -} \ No newline at end of file From 1f4dd9b5b5e6977fde0651fb1f540722a6d369a0 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Wed, 3 May 2023 09:59:15 -0600 Subject: [PATCH 21/36] Update device template Update test module to work with new device config file format --- .../Teltonika TRB140/device_config.json | 5 --- resources/devices/Template/device_config.json | 32 +++++++++++++++++++ .../modules/base/python/src/test_module.py | 19 +++++------ 3 files changed, 40 insertions(+), 16 deletions(-) delete mode 100644 resources/devices/Teltonika TRB140/device_config.json create mode 100644 resources/devices/Template/device_config.json diff --git a/resources/devices/Teltonika TRB140/device_config.json b/resources/devices/Teltonika TRB140/device_config.json deleted file mode 100644 index 759c1e9b4..000000000 --- a/resources/devices/Teltonika TRB140/device_config.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "make": "Teltonika", - "model": "TRB140", - "mac_addr": "00:1e:42:35:73:c4" -} \ No newline at end of file diff --git a/resources/devices/Template/device_config.json b/resources/devices/Template/device_config.json new file mode 100644 index 000000000..f8b56b7a3 --- /dev/null +++ b/resources/devices/Template/device_config.json @@ -0,0 +1,32 @@ +{ + "make": "Manufacturer X", + "model": "Device X", + "mac_addr": "aa:bb:cc:dd:ee:ff", + "test_modules": { + "dns": { + "enabled": true, + "tests": { + "dns.network.from_device": { + "enabled": true + }, + "dns.network.from_dhcp": { + "enabled": true + } + } + }, + "baseline": { + "enabled": true, + "tests": { + "baseline.passe": { + "enabled": true + }, + "baseline.pass": { + "enabled": true + }, + "baseline.skip": { + "enabled": true + } + } + } + } +} \ No newline at end of file diff --git a/test_orc/modules/base/python/src/test_module.py b/test_orc/modules/base/python/src/test_module.py index 0db4cc602..6f7f48c3a 100644 --- a/test_orc/modules/base/python/src/test_module.py +++ b/test_orc/modules/base/python/src/test_module.py @@ -34,26 +34,24 @@ def _get_device_tests(self, device_test_module): return [] else: for test in module_tests: - for dev_test in device_test_module["tests"]: - if test["name"] == dev_test["name"]: - test["enabled"] = dev_test["enabled"] + if test["name"] in device_test_module["tests"]: + test["enabled"] = device_test_module["tests"][test["name"]]["enabled"] return module_tests def _get_device_test_module(self): - device_modules = json.loads(os.environ['DEVICE_TEST_MODULES']) - for module in device_modules: - if self._module_name == module["name"]: - return module + test_modules = json.loads(os.environ['DEVICE_TEST_MODULES']) + if self._module_name in test_modules: + return test_modules[self._module_name] return None def run_tests(self): tests = self._get_tests() device_modules = os.environ['DEVICE_TEST_MODULES'] for test in tests: - test_method_name = "_" + test["name"].replace(".", "_") + test_method_name = "_" + test["name"].replace(".", "_") result = None if ("enabled" in test and test["enabled"]) or "enabled" not in test: - LOGGER.info("Attempting to run test: " + test["name"]) + LOGGER.info("Attempting to run test: " + test["name"]) # Resolve the correct python method by test name and run test if hasattr(self, test_method_name): @@ -64,12 +62,11 @@ def run_tests(self): result = None else: LOGGER.info("Test " + test["name"] + - " disabled. Skipping") + " disabled. Skipping") if result is not None: test["result"] = "compliant" if result else "non-compliant" else: test["result"] = "skipped" - LOGGER.info(test["name"] + ": " + str(result)) json_results = json.dumps({"results": tests}, indent=2) self._write_results(json_results) From 2dffb48435d28e521f064cd95a0893715badf8db Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Wed, 3 May 2023 10:16:21 -0600 Subject: [PATCH 22/36] Change test module network config options Do not start network services for modules not configured for network --- test_orc/modules/base/bin/start_module | 27 ++++++++----------- test_orc/modules/base/conf/module_config.json | 1 + .../modules/baseline/conf/module_config.json | 6 +---- test_orc/modules/dns/conf/module_config.json | 6 +---- 4 files changed, 14 insertions(+), 26 deletions(-) diff --git a/test_orc/modules/base/bin/start_module b/test_orc/modules/base/bin/start_module index a9f5402f4..6adc53f58 100644 --- a/test_orc/modules/base/bin/start_module +++ b/test_orc/modules/base/bin/start_module @@ -4,7 +4,7 @@ BIN_DIR="/testrun/bin" # Default interface should be veth0 for all containers -DEFAULT_IFACE=veth0 +IFACE=veth0 # Create a local user that matches the same as the host # to be used for correct file ownership for various logs @@ -27,7 +27,7 @@ fi # Extract the necessary config parameters MODULE_NAME=$(echo "$CONF" | jq -r '.config.meta.name') -DEFINED_IFACE=$(echo "$CONF" | jq -r '.config.network.interface') +NETWORK_REQUIRED=$(echo "$CONF" | jq -r '.config.network') GRPC=$(echo "$CONF" | jq -r '.config.grpc') # Validate the module name is present @@ -37,24 +37,19 @@ then exit 1 fi -# Select which interace to use -if [[ -z $DEFINED_IFACE || "$DEFINED_IFACE" == "null" ]] -then - echo "No Interface Defined, defaulting to veth0" - INTF=$DEFAULT_IFACE -else - INTF=$DEFINED_IFACE -fi - echo "Starting module $MODULE_NAME..." $BIN_DIR/setup_binaries $BIN_DIR -# Wait for interface to become ready -$BIN_DIR/wait_for_interface $INTF +# Only start network services if the test container needs +# a network connection to run its tests +if [ $NETWORK_REQUIRED == "true" ];then + # Wait for interface to become ready + $BIN_DIR/wait_for_interface $IFACE -# Start network capture -$BIN_DIR/capture $MODULE_NAME $INTF + # Start network capture + $BIN_DIR/capture $MODULE_NAME $IFACE +fi # Start the grpc server if [[ ! -z $GRPC && ! $GRPC == "null" ]] @@ -73,4 +68,4 @@ fi sleep 3 # Start the networking service -$BIN_DIR/start_test_module $MODULE_NAME $INTF \ No newline at end of file +$BIN_DIR/start_test_module $MODULE_NAME $IFACE \ No newline at end of file diff --git a/test_orc/modules/base/conf/module_config.json b/test_orc/modules/base/conf/module_config.json index 1f3a47ba2..7288dacfd 100644 --- a/test_orc/modules/base/conf/module_config.json +++ b/test_orc/modules/base/conf/module_config.json @@ -5,6 +5,7 @@ "display_name": "Base", "description": "Base image" }, + "network": false, "docker": { "enable_container": false } diff --git a/test_orc/modules/baseline/conf/module_config.json b/test_orc/modules/baseline/conf/module_config.json index e06fcb203..ba337267a 100644 --- a/test_orc/modules/baseline/conf/module_config.json +++ b/test_orc/modules/baseline/conf/module_config.json @@ -5,11 +5,7 @@ "display_name": "Baseline", "description": "Baseline test" }, - "network": { - "interface": "eth0", - "enable_wan": false, - "ip_index": 9 - }, + "network": false, "docker": { "enable_container": true, "timeout": 30 diff --git a/test_orc/modules/dns/conf/module_config.json b/test_orc/modules/dns/conf/module_config.json index 5bf20fc84..d21f6bca6 100644 --- a/test_orc/modules/dns/conf/module_config.json +++ b/test_orc/modules/dns/conf/module_config.json @@ -5,11 +5,7 @@ "display_name": "DNS", "description": "DNS test" }, - "network": { - "interface": "eth0", - "enable_wan": false, - "ip_index": 9 - }, + "network": false, "docker": { "enable_container": true, "timeout": 30 From 590c247565c5bdd11f6ef07d7ebbab2db1c28da5 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Wed, 3 May 2023 14:49:39 -0600 Subject: [PATCH 23/36] Initial nmap test module add Add device ip resolving to base module Add network mounting for test modules --- framework/testrun.py | 2 +- net_orc/python/src/network_orchestrator.py | 60 +++++++++++++++++++ test_orc/modules/base/base.Dockerfile | 2 +- test_orc/modules/base/bin/get_ip_from_mac | 8 +++ .../modules/base/python/src/test_module.py | 25 ++++++++ test_orc/modules/nmap/bin/start_test_module | 42 +++++++++++++ test_orc/modules/nmap/conf/module_config.json | 21 +++++++ test_orc/modules/nmap/nmap.Dockerfile | 11 ++++ .../modules/nmap/python/src/nmap_module.py | 21 +++++++ test_orc/modules/nmap/python/src/run.py | 49 +++++++++++++++ test_orc/python/src/module.py | 4 ++ test_orc/python/src/test_orchestrator.py | 9 ++- 12 files changed, 251 insertions(+), 3 deletions(-) create mode 100644 test_orc/modules/base/bin/get_ip_from_mac create mode 100644 test_orc/modules/nmap/bin/start_test_module create mode 100644 test_orc/modules/nmap/conf/module_config.json create mode 100644 test_orc/modules/nmap/nmap.Dockerfile create mode 100644 test_orc/modules/nmap/python/src/nmap_module.py create mode 100644 test_orc/modules/nmap/python/src/run.py diff --git a/framework/testrun.py b/framework/testrun.py index 07a217921..9c0dd6b51 100644 --- a/framework/testrun.py +++ b/framework/testrun.py @@ -54,7 +54,7 @@ def __init__(self, config_file=CONFIG_FILE,validate=True, net_only=False): config_file_abs=self._get_config_abs(config_file=config_file) self._net_orc = net_orc.NetworkOrchestrator(config_file=config_file_abs,validate=validate,async_monitor=not self._net_only) - self._test_orc = test_orc.TestOrchestrator() + self._test_orc = test_orc.TestOrchestrator(self._net_orc) def start(self): diff --git a/net_orc/python/src/network_orchestrator.py b/net_orc/python/src/network_orchestrator.py index 828ad58a7..1bbd90ab2 100644 --- a/net_orc/python/src/network_orchestrator.py +++ b/net_orc/python/src/network_orchestrator.py @@ -398,6 +398,66 @@ def start_network_services(self): LOGGER.info("All network services are running") self._check_network_services() + def _attach_test_module_to_network(self, test_module): + LOGGER.debug("Attaching test module " + + test_module.display_name + " to device bridge") + + # Device bridge interface example: tr-di-baseline-test (Test Run Device Interface for baseline test container) + bridge_intf = DEVICE_BRIDGE + "i-" + test_module.dir_name + "-test" + + # Container interface example: tr-cti-baseline-test (Test Run Container Interface for baseline test container) + container_intf = "tr-test-" + test_module.dir_name + + # Container network namespace name + container_net_ns = "tr-test-" + test_module.dir_name + + # Create interface pair + util.run_command("ip link add " + bridge_intf + + " type veth peer name " + container_intf) + + # Add bridge interface to device bridge + util.run_command("ovs-vsctl add-port " + + DEVICE_BRIDGE + " " + bridge_intf) + + # Get PID for running container + # TODO: Some error checking around missing PIDs might be required + container_pid = util.run_command( + "docker inspect -f {{.State.Pid}} " + test_module.container_name)[0] + + # Create symlink for container network namespace + util.run_command("ln -sf /proc/" + container_pid + + "/ns/net /var/run/netns/" + container_net_ns) + + # Attach container interface to container network namespace + util.run_command("ip link set " + container_intf + + " netns " + container_net_ns) + + # Rename container interface name to veth0 + util.run_command("ip netns exec " + container_net_ns + + " ip link set dev " + container_intf + " name veth0") + + # Set MAC address of container interface + util.run_command("ip netns exec " + container_net_ns + " ip link set dev veth0 address 9a:02:57:1e:8f:" + str(test_module.ip_index)) + + # Set IP address of container interface + ipv4_address = self.network_config.ipv4_network[test_module.ip_index] + ipv6_address = self.network_config.ipv6_network[test_module.ip_index] + + ipv4_address_with_prefix=str(ipv4_address) + "/" + str(self.network_config.ipv4_network.prefixlen) + ipv6_address_with_prefix=str(ipv6_address) + "/" + str(self.network_config.ipv6_network.prefixlen) + + util.run_command("ip netns exec " + container_net_ns + " ip addr add " + + ipv4_address_with_prefix + " dev veth0") + + util.run_command("ip netns exec " + container_net_ns + " ip addr add " + + ipv6_address_with_prefix + " dev veth0") + + + # Set interfaces up + util.run_command("ip link set dev " + bridge_intf + " up") + util.run_command("ip netns exec " + container_net_ns + + " ip link set dev veth0 up") + # TODO: Let's move this into a separate script? It does not look great def _attach_service_to_network(self, net_module): LOGGER.debug("Attaching net service " + diff --git a/test_orc/modules/base/base.Dockerfile b/test_orc/modules/base/base.Dockerfile index b5f35326a..8354f05be 100644 --- a/test_orc/modules/base/base.Dockerfile +++ b/test_orc/modules/base/base.Dockerfile @@ -2,7 +2,7 @@ FROM ubuntu:jammy # Install common software -RUN apt-get update && apt-get install -y net-tools iputils-ping tcpdump iproute2 jq python3 python3-pip dos2unix +RUN apt-get update && apt-get install -y net-tools iputils-ping tcpdump iproute2 jq python3 python3-pip dos2unix nmap # Setup the base python requirements COPY modules/base/python /testrun/python diff --git a/test_orc/modules/base/bin/get_ip_from_mac b/test_orc/modules/base/bin/get_ip_from_mac new file mode 100644 index 000000000..09a19bc13 --- /dev/null +++ b/test_orc/modules/base/bin/get_ip_from_mac @@ -0,0 +1,8 @@ +#!/bin/bash + +NET=$1 +MAC=$2 + +IP_ADDR=$(nmap -sP $NET | grep -B 2 $MAC | head -n 1 | cut -d " " -f 5) + +echo $IP_ADDR \ No newline at end of file diff --git a/test_orc/modules/base/python/src/test_module.py b/test_orc/modules/base/python/src/test_module.py index 6f7f48c3a..899aaaa87 100644 --- a/test_orc/modules/base/python/src/test_module.py +++ b/test_orc/modules/base/python/src/test_module.py @@ -1,6 +1,7 @@ import json import logger import os +import subprocess LOGGER = None RESULTS_DIR = "/runtime/output/" @@ -14,6 +15,7 @@ def __init__(self, module_name, log_name): self._device_mac = os.environ['DEVICE_MAC'] self._add_logger(log_name=log_name, module_name=module_name) self._config = self._read_config() + self._device_ip = None def _add_logger(self, log_name, module_name): global LOGGER @@ -45,6 +47,9 @@ def _get_device_test_module(self): return None def run_tests(self): + if self._config["config"]["network"]: + self._device_ip = self._get_device_ip() + LOGGER.info("Device IP Resolved for testing: " + str(self._device_ip)) tests = self._get_tests() device_modules = os.environ['DEVICE_TEST_MODULES'] for test in tests: @@ -82,3 +87,23 @@ def _write_results(self, results): f = open(results_file, "w", encoding="utf-8") f.write(results) f.close() + + def _get_device_ip(self): + command = '/testrun/bin/get_ip_from_mac {} {}'.format( + "10.10.10.0/24", self._device_mac.upper()) + + LOGGER.info("exec command: " + command) + + process = subprocess.Popen(command, + universal_newlines=True, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + text = str(process.stdout.read()).rstrip() + + LOGGER.debug("nmap response: " + text) + + if text: + return text.split("\n")[0] + + return None diff --git a/test_orc/modules/nmap/bin/start_test_module b/test_orc/modules/nmap/bin/start_test_module new file mode 100644 index 000000000..4bb7e9f96 --- /dev/null +++ b/test_orc/modules/nmap/bin/start_test_module @@ -0,0 +1,42 @@ +#!/bin/bash + +# An example startup script that does the bare minimum to start +# a test module via a pyhon script. Each test module should include a +# start_test_module file that overwrites this one to boot all of its +# specific requirements to run. + +# Define where the python source files are located +PYTHON_SRC_DIR=/testrun/python/src + +# Fetch module name +MODULE_NAME=$1 + +# Default interface should be veth0 for all containers +DEFAULT_IFACE=veth0 + +# Allow a user to define an interface by passing it into this script +DEFINED_IFACE=$2 + +# Select which interace to use +if [[ -z $DEFINED_IFACE || "$DEFINED_IFACE" == "null" ]] +then + echo "No interface defined, defaulting to veth0" + INTF=$DEFAULT_IFACE +else + INTF=$DEFINED_IFACE +fi + +# Create and set permissions on the log files +LOG_FILE=/runtime/output/$MODULE_NAME.log +RESULT_FILE=/runtime/output/$MODULE_NAME-result.json +touch $LOG_FILE +touch $RESULT_FILE +chown $HOST_USER:$HOST_USER $LOG_FILE +chown $HOST_USER:$HOST_USER $RESULT_FILE + +# Run the python scrip that will execute the tests for this module +# -u flag allows python print statements +# to be logged by docker by running unbuffered +python3 -u $PYTHON_SRC_DIR/run.py "-m $MODULE_NAME" + +echo Module has finished \ No newline at end of file diff --git a/test_orc/modules/nmap/conf/module_config.json b/test_orc/modules/nmap/conf/module_config.json new file mode 100644 index 000000000..c8777c2d5 --- /dev/null +++ b/test_orc/modules/nmap/conf/module_config.json @@ -0,0 +1,21 @@ +{ + "config": { + "meta": { + "name": "nmap", + "display_name": "nmap", + "description": "Scan for open ports using nmap" + }, + "network": true, + "docker": { + "enable_container": true, + "timeout": 30 + }, + "tests":[ + { + "name": "nmap.scan", + "description": "Simple scan of open ports", + "expected_behavior": "Report all open ports" + } + ] + } +} \ No newline at end of file diff --git a/test_orc/modules/nmap/nmap.Dockerfile b/test_orc/modules/nmap/nmap.Dockerfile new file mode 100644 index 000000000..12f23dde7 --- /dev/null +++ b/test_orc/modules/nmap/nmap.Dockerfile @@ -0,0 +1,11 @@ +# Image name: test-run/baseline-test +FROM test-run/base-test:latest + +# Copy over all configuration files +COPY modules/nmap/conf /testrun/conf + +# Load device binary files +COPY modules/nmap/bin /testrun/bin + +# Copy over all python files +COPY modules/nmap/python /testrun/python \ No newline at end of file diff --git a/test_orc/modules/nmap/python/src/nmap_module.py b/test_orc/modules/nmap/python/src/nmap_module.py new file mode 100644 index 000000000..f42439b07 --- /dev/null +++ b/test_orc/modules/nmap/python/src/nmap_module.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +import time +from test_module import TestModule + +LOG_NAME = "test_nmap" +LOGGER = None + +class NmapModule(TestModule): + + def __init__(self, module): + super().__init__(module_name=module, log_name=LOG_NAME) + global LOGGER + LOGGER = self._get_logger() + + def _nmap_scan(self): + LOGGER.info( + "Running nmap scan test") + time.sleep(30) + LOGGER.info("nmap scan test finished") + return True diff --git a/test_orc/modules/nmap/python/src/run.py b/test_orc/modules/nmap/python/src/run.py new file mode 100644 index 000000000..48bb92dd2 --- /dev/null +++ b/test_orc/modules/nmap/python/src/run.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import argparse +import signal +import sys +import logger + +from nmap_module import NmapModule + +LOGGER = logger.get_logger('test_module') +RUNTIME = 300 + +class NmapModuleRunner: + + def __init__(self,module): + + signal.signal(signal.SIGINT, self._handler) + signal.signal(signal.SIGTERM, self._handler) + signal.signal(signal.SIGABRT, self._handler) + signal.signal(signal.SIGQUIT, self._handler) + + LOGGER.info("Starting nmap Module") + + self._test_module = NmapModule(module) + self._test_module.run_tests() + + def _handler(self, signum, *other): + LOGGER.debug("SigtermEnum: " + str(signal.SIGTERM)) + LOGGER.debug("Exit signal received: " + str(signum)) + if signum in (2, signal.SIGTERM): + LOGGER.info("Exit signal received. Stopping test module...") + LOGGER.info("Test module stopped") + sys.exit(1) + +def run(argv): + parser = argparse.ArgumentParser(description="Nmap Module Help", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + parser.add_argument( + "-m", "--module", help="Define the module name to be used to create the log file") + + args = parser.parse_args() + + # For some reason passing in the args from bash adds an extra + # space before the argument so we'll just strip out extra space + NmapModuleRunner(args.module.strip()) + +if __name__ == "__main__": + run(sys.argv) diff --git a/test_orc/python/src/module.py b/test_orc/python/src/module.py index 8121c34db..6b2f14f9d 100644 --- a/test_orc/python/src/module.py +++ b/test_orc/python/src/module.py @@ -15,9 +15,13 @@ class TestModule: # pylint: disable=too-few-public-methods,too-many-instance-att container_name: str = None image_name :str = None enable_container: bool = True + network: bool = True timeout: int = 60 # Absolute path dir: str = None dir_name: str = None + + #Set IP Index for all test modules + ip_index: str = 9 diff --git a/test_orc/python/src/test_orchestrator.py b/test_orc/python/src/test_orchestrator.py index c4a8a823a..17dc5a19a 100644 --- a/test_orc/python/src/test_orchestrator.py +++ b/test_orc/python/src/test_orchestrator.py @@ -18,9 +18,10 @@ class TestOrchestrator: """Manages and controls the test modules.""" - def __init__(self): + def __init__(self,net_orc): self._test_modules = [] self._module_config = None + self._net_orc = net_orc self._path = os.path.dirname(os.path.dirname( os.path.dirname(os.path.realpath(__file__)))) @@ -97,6 +98,11 @@ def _run_test_module(self, module, device): LOGGER.debug(container_error) return + # Mount the test container to the virtual network if requried + if module.network: + LOGGER.info("Mounting test module to the network") + self._net_orc._attach_test_module_to_network(module) + # Determine the module timeout time test_module_timeout = time.time() + module.timeout status = self._get_module_status(module) @@ -151,6 +157,7 @@ def _load_test_modules(self): module.name = module_json['config']['meta']['name'] module.display_name = module_json['config']['meta']['display_name'] module.description = module_json['config']['meta']['description'] + module.network = module_json['config']['network'] module.dir = os.path.join(self._path, modules_dir, module_dir) module.dir_name = module_dir module.build_file = module_dir + ".Dockerfile" From 726d7729cacfccf34a2102b0d9186e266d072f41 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Wed, 3 May 2023 15:32:09 -0600 Subject: [PATCH 24/36] Update ipv4 device resolving in test modules --- .../bin/{get_ip_from_mac => get_ipv4_addr} | 0 .../modules/base/python/src/test_module.py | 27 ++++++------------- test_orc/modules/base/python/src/util.py | 25 +++++++++++++++++ 3 files changed, 33 insertions(+), 19 deletions(-) rename test_orc/modules/base/bin/{get_ip_from_mac => get_ipv4_addr} (100%) create mode 100644 test_orc/modules/base/python/src/util.py diff --git a/test_orc/modules/base/bin/get_ip_from_mac b/test_orc/modules/base/bin/get_ipv4_addr similarity index 100% rename from test_orc/modules/base/bin/get_ip_from_mac rename to test_orc/modules/base/bin/get_ipv4_addr diff --git a/test_orc/modules/base/python/src/test_module.py b/test_orc/modules/base/python/src/test_module.py index 899aaaa87..9e62e484a 100644 --- a/test_orc/modules/base/python/src/test_module.py +++ b/test_orc/modules/base/python/src/test_module.py @@ -1,7 +1,7 @@ import json import logger import os -import subprocess +import util LOGGER = None RESULTS_DIR = "/runtime/output/" @@ -15,7 +15,8 @@ def __init__(self, module_name, log_name): self._device_mac = os.environ['DEVICE_MAC'] self._add_logger(log_name=log_name, module_name=module_name) self._config = self._read_config() - self._device_ip = None + self._device_ipv4_addr = None + self._device_ipv6_addr = None def _add_logger(self, log_name, module_name): global LOGGER @@ -48,8 +49,8 @@ def _get_device_test_module(self): def run_tests(self): if self._config["config"]["network"]: - self._device_ip = self._get_device_ip() - LOGGER.info("Device IP Resolved for testing: " + str(self._device_ip)) + self._device_ipv4_addr = self._get_device_ipv4() + LOGGER.info("Device IP Resolved: " + str(self._device_ipv4_addr)) tests = self._get_tests() device_modules = os.environ['DEVICE_TEST_MODULES'] for test in tests: @@ -88,22 +89,10 @@ def _write_results(self, results): f.write(results) f.close() - def _get_device_ip(self): - command = '/testrun/bin/get_ip_from_mac {} {}'.format( + def _get_device_ipv4(self): + command = '/testrun/bin/get_ipv4_addr {} {}'.format( "10.10.10.0/24", self._device_mac.upper()) - - LOGGER.info("exec command: " + command) - - process = subprocess.Popen(command, - universal_newlines=True, - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - text = str(process.stdout.read()).rstrip() - - LOGGER.debug("nmap response: " + text) - + text, err = util.run_command(command) if text: return text.split("\n")[0] - return None diff --git a/test_orc/modules/base/python/src/util.py b/test_orc/modules/base/python/src/util.py new file mode 100644 index 000000000..a2dcfbdb1 --- /dev/null +++ b/test_orc/modules/base/python/src/util.py @@ -0,0 +1,25 @@ +import subprocess +import shlex +import logger + +# Runs a process at the os level +# By default, returns the standard output and error output +# If the caller sets optional output parameter to False, +# will only return a boolean result indicating if it was +# succesful in running the command. Failure is indicated +# by any return code from the process other than zero. +def run_command(cmd, output=True): + success = False + LOGGER = logger.get_logger('util') + process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + if process.returncode !=0 and output: + err_msg = "%s. Code: %s" % (stderr.strip(), process.returncode) + LOGGER.error("Command Failed: " + cmd) + LOGGER.error("Error: " + err_msg) + else: + success = True + if output: + return stdout.strip().decode('utf-8'), stderr + else: + return success From d6bfbf298aa141b93b7d1208012cd9eb3c0ee1d5 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Wed, 3 May 2023 15:40:07 -0600 Subject: [PATCH 25/36] Map in ip subnets and remove hard coded references --- framework/testrun.py | 2 +- test_orc/modules/base/python/src/test_module.py | 4 +++- test_orc/python/src/test_orchestrator.py | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/framework/testrun.py b/framework/testrun.py index 9c0dd6b51..a13bc2655 100644 --- a/framework/testrun.py +++ b/framework/testrun.py @@ -126,7 +126,7 @@ def _run_tests(self,device): """Iterate through and start all test modules.""" # To Do: Make this configurable - time.sleep(60) # Let device bootup + time.sleep(5) # Let device bootup self._test_orc._run_test_modules(device) diff --git a/test_orc/modules/base/python/src/test_module.py b/test_orc/modules/base/python/src/test_module.py index 9e62e484a..63a78a482 100644 --- a/test_orc/modules/base/python/src/test_module.py +++ b/test_orc/modules/base/python/src/test_module.py @@ -13,6 +13,8 @@ class TestModule: def __init__(self, module_name, log_name): self._module_name = module_name self._device_mac = os.environ['DEVICE_MAC'] + self._ipv4_subnet = os.environ['IPV4_SUBNET'] + self._ipv6_subnet = os.environ['IPV6_SUBNET'] self._add_logger(log_name=log_name, module_name=module_name) self._config = self._read_config() self._device_ipv4_addr = None @@ -91,7 +93,7 @@ def _write_results(self, results): def _get_device_ipv4(self): command = '/testrun/bin/get_ipv4_addr {} {}'.format( - "10.10.10.0/24", self._device_mac.upper()) + self._ipv4_subnet, self._device_mac.upper()) text, err = util.run_command(command) if text: return text.split("\n")[0] diff --git a/test_orc/python/src/test_orchestrator.py b/test_orc/python/src/test_orchestrator.py index 17dc5a19a..7b60ad0a4 100644 --- a/test_orc/python/src/test_orchestrator.py +++ b/test_orc/python/src/test_orchestrator.py @@ -90,7 +90,9 @@ def _run_test_module(self, module, device): environment={ "HOST_USER": os.getlogin(), "DEVICE_MAC": device.mac_addr, - "DEVICE_TEST_MODULES": device.test_modules + "DEVICE_TEST_MODULES": device.test_modules, + "IPV4_SUBNET": self._net_orc.network_config.ipv4_network, + "IPV6_SUBNET": self._net_orc.network_config.ipv6_network } ) except (docker.errors.APIError, docker.errors.ContainerError) as container_error: From 95e27b837a033164e6057bd0ccc080e969599990 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Wed, 3 May 2023 16:22:42 -0600 Subject: [PATCH 26/36] Add ftp port test --- test_orc/modules/nmap/conf/module_config.json | 2 +- .../modules/nmap/python/src/nmap_module.py | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/test_orc/modules/nmap/conf/module_config.json b/test_orc/modules/nmap/conf/module_config.json index c8777c2d5..5eb49dab9 100644 --- a/test_orc/modules/nmap/conf/module_config.json +++ b/test_orc/modules/nmap/conf/module_config.json @@ -12,7 +12,7 @@ }, "tests":[ { - "name": "nmap.scan", + "name": "security.services.ftp", "description": "Simple scan of open ports", "expected_behavior": "Report all open ports" } diff --git a/test_orc/modules/nmap/python/src/nmap_module.py b/test_orc/modules/nmap/python/src/nmap_module.py index f42439b07..aec84a949 100644 --- a/test_orc/modules/nmap/python/src/nmap_module.py +++ b/test_orc/modules/nmap/python/src/nmap_module.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import time +import util from test_module import TestModule LOG_NAME = "test_nmap" @@ -13,9 +14,25 @@ def __init__(self, module): global LOGGER LOGGER = self._get_logger() - def _nmap_scan(self): + def _security_services_ftp(self): LOGGER.info( "Running nmap scan test") - time.sleep(30) + port_scan_results = self._scan_for_ports("20-21") + LOGGER.info("Port Scan Results: " + str(port_scan_results)) LOGGER.info("nmap scan test finished") return True + + def _scan_for_ports(self,ports): + results = [] + text, err = util.run_command("nmap -p" + ports + " " + self._device_ipv4_addr) + LOGGER.info("nmap result: " + str(text)) + if text: + rows = text.split("PORT")[1].split("MAC Address")[0].split("\n") + for result in rows[1:-1]: #Iterate skipping the header and tail rows + LOGGER.info("Result: " + str(result)) + cols = result.split(" ") + LOGGER.info("Column: " + str(cols)) + port_result = {"port":cols[0],"state":cols[1],"service":cols[2]} + results.append(port_result) + return results + From 2c48725b1ed4a6d93de061cef651ff48dd47edf4 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Thu, 4 May 2023 15:53:03 -0600 Subject: [PATCH 27/36] Add ability to pass config for individual tests within a module Update nmap module scan to run tests based on config --- framework/testrun.py | 2 +- .../modules/base/python/src/test_module.py | 6 +- test_orc/modules/nmap/conf/module_config.json | 54 +++++++++++++- .../modules/nmap/python/src/nmap_module.py | 74 +++++++++++++++---- 4 files changed, 114 insertions(+), 22 deletions(-) diff --git a/framework/testrun.py b/framework/testrun.py index a13bc2655..171622271 100644 --- a/framework/testrun.py +++ b/framework/testrun.py @@ -172,9 +172,9 @@ def _device_discovered(self, mac_addr): LOGGER.info( f'Discovered {device.make} {device.model} on the network') else: - device = Device(make=None, model=None, mac_addr=mac_addr) LOGGER.info( f'A new device has been discovered with mac address {mac_addr}') + device = Device(make=None, model=None, mac_addr=mac_addr,test_modules=json.dumps("{}")) # TODO: Pass device information to test orchestrator/runner self._run_tests(device) diff --git a/test_orc/modules/base/python/src/test_module.py b/test_orc/modules/base/python/src/test_module.py index 63a78a482..93c6847ee 100644 --- a/test_orc/modules/base/python/src/test_module.py +++ b/test_orc/modules/base/python/src/test_module.py @@ -33,6 +33,7 @@ def _get_tests(self): def _get_device_tests(self, device_test_module): module_tests = self._config["config"]["tests"] + LOGGER.info("Device Config: " + str(device_test_module)) if device_test_module is None: return module_tests elif not device_test_module["enabled"]: @@ -63,7 +64,10 @@ def run_tests(self): # Resolve the correct python method by test name and run test if hasattr(self, test_method_name): - result = getattr(self, test_method_name)() + if "config" in test: + result = getattr(self,test_method_name)(config=test["config"]) + else: + result = getattr(self, test_method_name)() else: LOGGER.info("Test " + test["name"] + " not resolved. Skipping") diff --git a/test_orc/modules/nmap/conf/module_config.json b/test_orc/modules/nmap/conf/module_config.json index 5eb49dab9..873155770 100644 --- a/test_orc/modules/nmap/conf/module_config.json +++ b/test_orc/modules/nmap/conf/module_config.json @@ -10,11 +10,57 @@ "enable_container": true, "timeout": 30 }, - "tests":[ + "tests": [ { - "name": "security.services.ftp", - "description": "Simple scan of open ports", - "expected_behavior": "Report all open ports" + "name": "security.nmap.ports", + "description": "Run an nmap scan of open ports", + "expected_behavior": "Report all open ports", + "config": { + "security.services.ftp": { + "tcp_ports": { + "20": { + "allowed": false, + "description": "File Transfer Protocol (FTP) Server Data Transfer" + }, + "21": { + "allowed": false, + "description": "File Transfer Protocol (FTP) Server Data Transfer" + } + }, + "description": "Check FTP port 20/21 is disabled and FTP is not running on any port", + "expected_behavior": "There is no FTP service running on any port" + }, + "security.services.ssh": { + "tcp_ports": { + "22": { + "allowed": true, + "description": "Secure Shell (SSH) server" + } + }, + "description": "Check TELNET port 23 is disabled and TELNET is not running on any port", + "expected_behavior": "There is no FTP service running on any port" + }, + "security.services.telnet": { + "tcp_ports": { + "23": { + "allowed": false, + "description": "Telnet Serverr" + } + }, + "description": "Check TELNET port 23 is disabled and TELNET is not running on any port", + "expected_behavior": "There is no FTP service running on any port" + }, + "security.services.wsdapi": { + "tcp_ports": { + "5357": { + "allowed": false, + "description": "Some sort of open api" + } + }, + "description": "Check TELNET port 23 is disabled and TELNET is not running on any port", + "expected_behavior": "There is no FTP service running on any port" + } + } } ] } diff --git a/test_orc/modules/nmap/python/src/nmap_module.py b/test_orc/modules/nmap/python/src/nmap_module.py index aec84a949..ebf87359d 100644 --- a/test_orc/modules/nmap/python/src/nmap_module.py +++ b/test_orc/modules/nmap/python/src/nmap_module.py @@ -2,11 +2,13 @@ import time import util +import json from test_module import TestModule LOG_NAME = "test_nmap" LOGGER = None + class NmapModule(TestModule): def __init__(self, module): @@ -14,25 +16,65 @@ def __init__(self, module): global LOGGER LOGGER = self._get_logger() - def _security_services_ftp(self): + def _security_nmap_ports(self, config): LOGGER.info( "Running nmap scan test") - port_scan_results = self._scan_for_ports("20-21") - LOGGER.info("Port Scan Results: " + str(port_scan_results)) - LOGGER.info("nmap scan test finished") - return True - - def _scan_for_ports(self,ports): - results = [] - text, err = util.run_command("nmap -p" + ports + " " + self._device_ipv4_addr) - LOGGER.info("nmap result: " + str(text)) + if self._device_ipv4_addr is not None: + self._scan_tcp_results = self._scan_for_tcp_ports() + LOGGER.info("Port Scan Results: " + str(self._scan_tcp_results)) + LOGGER.info("nmap scan test finished") + self._process_port_results( + tests=config) + return True, config + else: + LOGGER.info("Device ip address not resolved, skipping") + return None + + def _process_port_results(self, tests): + for test in tests: + LOGGER.info("Running test: " + str(test)) + self._check_scan_results(test_config=tests[test]) + + def _check_scan_results(self, test_config): + if "tcp_ports" in test_config: + tcp_port_config = test_config["tcp_ports"] + + if tcp_port_config is not None: + for port in tcp_port_config: + result = None + LOGGER.info("Checking Port: " + str(port)) + LOGGER.debug("Port Config: " + str(tcp_port_config[port])) + if port in self._scan_tcp_results: + if self._scan_tcp_results[port]["state"] == "open": + if not tcp_port_config[port]["allowed"]: + LOGGER.info("Unallowed port open") + result = False + else: + LOGGER.info("Allowed port open") + result = True + else: + LOGGER.info("Port is closed") + result = True + else: + LOGGER.info("Port not detected, closed") + result = True + + if result is not None: + tcp_port_config[port]["result"] = "compliant" if result else "non-compliant" + else: + tcp_port_config[port]["result"] = "skipped" + + LOGGER.info("Results:\n" + json.dumps(tcp_port_config)) + + def _scan_for_tcp_ports(self): + results = {} + text, err = util.run_command("nmap " + self._device_ipv4_addr) + LOGGER.info("nmap results\n" + str(text)) if text: rows = text.split("PORT")[1].split("MAC Address")[0].split("\n") - for result in rows[1:-1]: #Iterate skipping the header and tail rows - LOGGER.info("Result: " + str(result)) + for result in rows[1:-1]: # Iterate skipping the header and tail rows cols = result.split(" ") - LOGGER.info("Column: " + str(cols)) - port_result = {"port":cols[0],"state":cols[1],"service":cols[2]} - results.append(port_result) + port_result = {cols[0].split( + "/")[0]: {"state": cols[1], "service": cols[2]}} + results.update(port_result) return results - From 60de8b364bfb17b356de68eeda829854743454e7 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 5 May 2023 08:20:08 -0600 Subject: [PATCH 28/36] Add full module check for compliance --- test_orc/modules/nmap/python/src/nmap_module.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test_orc/modules/nmap/python/src/nmap_module.py b/test_orc/modules/nmap/python/src/nmap_module.py index ebf87359d..6b209c732 100644 --- a/test_orc/modules/nmap/python/src/nmap_module.py +++ b/test_orc/modules/nmap/python/src/nmap_module.py @@ -13,10 +13,12 @@ class NmapModule(TestModule): def __init__(self, module): super().__init__(module_name=module, log_name=LOG_NAME) + self._unallowed_ports = [] global LOGGER LOGGER = self._get_logger() def _security_nmap_ports(self, config): + LOGGER.info( "Running nmap scan test") if self._device_ipv4_addr is not None: @@ -25,7 +27,8 @@ def _security_nmap_ports(self, config): LOGGER.info("nmap scan test finished") self._process_port_results( tests=config) - return True, config + LOGGER.info("Unallowed Ports: " + str(self._unallowed_ports)) + return len(self._unallowed_ports)==0 else: LOGGER.info("Device ip address not resolved, skipping") return None @@ -49,6 +52,7 @@ def _check_scan_results(self, test_config): if not tcp_port_config[port]["allowed"]: LOGGER.info("Unallowed port open") result = False + self._unallowed_ports.append(str(port)) else: LOGGER.info("Allowed port open") result = True From 0810cdbb9985de24fb47a47717bd92ef75f4443d Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 5 May 2023 09:32:42 -0600 Subject: [PATCH 29/36] Add all tcp port scans to config --- test_orc/modules/nmap/conf/module_config.json | 88 ++++++++++++++++++- .../modules/nmap/python/src/nmap_module.py | 2 - 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/test_orc/modules/nmap/conf/module_config.json b/test_orc/modules/nmap/conf/module_config.json index 873155770..da11f1a06 100644 --- a/test_orc/modules/nmap/conf/module_config.json +++ b/test_orc/modules/nmap/conf/module_config.json @@ -44,12 +44,98 @@ "tcp_ports": { "23": { "allowed": false, - "description": "Telnet Serverr" + "description": "Telnet Server" } }, "description": "Check TELNET port 23 is disabled and TELNET is not running on any port", "expected_behavior": "There is no FTP service running on any port" }, + "security.services.smtp": { + "tcp_ports": { + "25": { + "allowed": false, + "description": "Simple Mail Transfer Protocol (SMTP) Server" + }, + "465": { + "allowed": false, + "description": "Simple Mail Transfer Protocol over SSL (SMTPS) Server" + }, + "587": { + "allowed": false, + "description": "Simple Mail Transfer Protocol via TLS (SMTPS) Server" + } + }, + "description": "Check SMTP port 25 is disabled and ports 465 or 587 with SSL encryption are (not?) enabled and SMTP is not running on any port.", + "expected_behavior": "There is no smtp service running on any port" + }, + "security.services.http": { + "tcp_ports": { + "80": { + "allowed": false, + "description": "Administrative Insecure Web-Server" + } + }, + "description": "Check that there is no HTTP server running on any port", + "expected_behavior": "Device is unreachable on port 80 (or any other port) and only responds to HTTPS requests on port 443 (or any other port if HTTP is used at all)" + }, + "security.services.pop": { + "tcp_ports": { + "110": { + "allowed": false, + "description": "Post Office Protocol v3 (POP3) Server" + } + }, + "description": "Check POP port 110 is disalbed and POP is not running on any port", + "expected_behavior": "There is no pop service running on any port" + }, + "security.services.imap": { + "tcp_ports": { + "143": { + "allowed": false, + "description": "Internet Message Access Protocol (IMAP) Server" + } + }, + "description": "Check IMAP port 143 is disabled and IMAP is not running on any port", + "expected_behavior": "There is no imap service running on any port" + }, + "security.services.snmpv3": { + "tcp_ports": { + "161": { + "allowed": false, + "description": "Simple Network Management Protocol (SNMP)" + }, + "162": { + "allowed": false, + "description": "Simple Network Management Protocol (SNMP) Trap" + } + }, + "description": "Check SNMP port 161/162 is disabled. If SNMP is an essential service, check it supports version 3", + "expected_behavior": "Device is unreachable on port 161 (or any other port) and device is unreachable on port 162 (or any other port) unless SNMP is essential in which case it is SNMPv3 is used." + }, + "security.services.https": { + "tcp_ports": { + "80": { + "allowed": false, + "description": "Administrative Secure Web-Server" + } + }, + "description": "Check that if there is a web server running it is running on a secure port.", + "expected_behavior": "Device only responds to HTTPS requests on port 443 (or any other port if HTTP is used at all)" + }, + "security.services.vnc": { + "tcp_ports": { + "5800": { + "allowed": false, + "description": "Virtual Network Computing (VNC) Remote Frame Buffer Protocol Over HTTP" + }, + "5500": { + "allowed": false, + "description": "Virtual Network Computing (VNC) Remote Frame Buffer Protocol" + } + }, + "description": "Check VNC is disabled on any port", + "expected_behavior": "Device cannot be accessed /connected to via VNc on any port" + }, "security.services.wsdapi": { "tcp_ports": { "5357": { diff --git a/test_orc/modules/nmap/python/src/nmap_module.py b/test_orc/modules/nmap/python/src/nmap_module.py index 6b209c732..623e1a6a2 100644 --- a/test_orc/modules/nmap/python/src/nmap_module.py +++ b/test_orc/modules/nmap/python/src/nmap_module.py @@ -68,8 +68,6 @@ def _check_scan_results(self, test_config): else: tcp_port_config[port]["result"] = "skipped" - LOGGER.info("Results:\n" + json.dumps(tcp_port_config)) - def _scan_for_tcp_ports(self): results = {} text, err = util.run_command("nmap " + self._device_ipv4_addr) From 82175424d0cbc53678d223a6ce67b3b39966fdc5 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 5 May 2023 15:29:53 -0600 Subject: [PATCH 30/36] Update nmap commands to match existing DAQ tests Add udp scanning and tests --- test_orc/modules/nmap/conf/module_config.json | 32 ++++++++- .../modules/nmap/python/src/nmap_module.py | 70 ++++++++++++++----- 2 files changed, 83 insertions(+), 19 deletions(-) diff --git a/test_orc/modules/nmap/conf/module_config.json b/test_orc/modules/nmap/conf/module_config.json index da11f1a06..20eb6eb8a 100644 --- a/test_orc/modules/nmap/conf/module_config.json +++ b/test_orc/modules/nmap/conf/module_config.json @@ -8,7 +8,7 @@ "network": true, "docker": { "enable_container": true, - "timeout": 30 + "timeout": 300 }, "tests": [ { @@ -109,6 +109,16 @@ "description": "Simple Network Management Protocol (SNMP) Trap" } }, + "udp_ports": { + "161": { + "allowed": false, + "description": "Simple Network Management Protocol (SNMP)" + }, + "162": { + "allowed": false, + "description": "Simple Network Management Protocol (SNMP) Trap" + } + }, "description": "Check SNMP port 161/162 is disabled. If SNMP is an essential service, check it supports version 3", "expected_behavior": "Device is unreachable on port 161 (or any other port) and device is unreachable on port 162 (or any other port) unless SNMP is essential in which case it is SNMPv3 is used." }, @@ -136,6 +146,26 @@ "description": "Check VNC is disabled on any port", "expected_behavior": "Device cannot be accessed /connected to via VNc on any port" }, + "security.services.tftp": { + "udp_ports": { + "69": { + "allowed": false, + "description": "Trivial File Transfer Protocol (TFTP) Server" + } + }, + "description": "Check TFTP port 69 is disabled (UDP)", + "expected_behavior": "There is no tftp service running on any port" + }, + "security.services.ntp": { + "udp_ports": { + "123": { + "allowed": false, + "description": "Network Time Protocol (NTP) Server" + } + }, + "description": "Check NTP port 123 is disabled and the device is not operating as an NTP server", + "expected_behavior": "The device dos not respond to NTP requests when it's IP is set as the NTP server on another device" + }, "security.services.wsdapi": { "tcp_ports": { "5357": { diff --git a/test_orc/modules/nmap/python/src/nmap_module.py b/test_orc/modules/nmap/python/src/nmap_module.py index 623e1a6a2..82230befe 100644 --- a/test_orc/modules/nmap/python/src/nmap_module.py +++ b/test_orc/modules/nmap/python/src/nmap_module.py @@ -22,8 +22,10 @@ def _security_nmap_ports(self, config): LOGGER.info( "Running nmap scan test") if self._device_ipv4_addr is not None: - self._scan_tcp_results = self._scan_for_tcp_ports() - LOGGER.info("Port Scan Results: " + str(self._scan_tcp_results)) + self._scan_tcp_results = self._scan_tcp_ports() + self._scan_udp_results = self._scan_udp_ports(config) + LOGGER.info("TCP Scan Results: " + str(self._scan_tcp_results)) + LOGGER.info("UDP Scan Results: " + str(self._scan_udp_results)) LOGGER.info("nmap scan test finished") self._process_port_results( tests=config) @@ -35,21 +37,31 @@ def _security_nmap_ports(self, config): def _process_port_results(self, tests): for test in tests: - LOGGER.info("Running test: " + str(test)) + LOGGER.info("Checking test: " + str(test)) self._check_scan_results(test_config=tests[test]) def _check_scan_results(self, test_config): + port_config = {} if "tcp_ports" in test_config: - tcp_port_config = test_config["tcp_ports"] + port_config.update(test_config["tcp_ports"]) + elif "udp_ports" in test_config: + port_config.update(test_config["udp_ports"]) - if tcp_port_config is not None: - for port in tcp_port_config: + LOGGER.info("Ports Config: " + str(port_config)) + scan_results = {} + if self._scan_tcp_results is not None: + scan_results.update(self._scan_tcp_results) + if self._scan_udp_results is not None: + scan_results.update(self._scan_udp_results) + LOGGER.info("Scan Results: " + str(scan_results)) + if port_config is not None: + for port in port_config: result = None LOGGER.info("Checking Port: " + str(port)) - LOGGER.debug("Port Config: " + str(tcp_port_config[port])) - if port in self._scan_tcp_results: - if self._scan_tcp_results[port]["state"] == "open": - if not tcp_port_config[port]["allowed"]: + LOGGER.debug("Port Config: " + str(port_config[port])) + if port in scan_results: + if scan_results[port]["state"] == "open": + if not port_config[port]["allowed"]: LOGGER.info("Unallowed port open") result = False self._unallowed_ports.append(str(port)) @@ -64,19 +76,41 @@ def _check_scan_results(self, test_config): result = True if result is not None: - tcp_port_config[port]["result"] = "compliant" if result else "non-compliant" + port_config[port]["result"] = "compliant" if result else "non-compliant" else: - tcp_port_config[port]["result"] = "skipped" + port_config[port]["result"] = "skipped" - def _scan_for_tcp_ports(self): + def _scan_tcp_ports(self): + LOGGER.info( "Running nmap TCP port scans") + nmap_results, err = util.run_command("nmap -sT -sV -Pn -v -p 1-100,5357 --allports --version-intensity 7 -T4 " + self._device_ipv4_addr) + return self._process_nmap_results(nmap_results=nmap_results) + + def _scan_udp_ports(self, tests): + ports = [] + for test in tests: + test_config = tests[test] + if "udp_ports" in test_config: + for port in test_config["udp_ports"]: + ports.append(port) + if len(ports) > 0: + port_list = ','.join(ports) + LOGGER.info("UDP Ports: " + str(port_list)) + LOGGER.info( "Running nmap UDP port scans") + nmap_results, err = util.run_command("nmap -sU -sV -p " + port_list + " " + self._device_ipv4_addr) + return self._process_nmap_results(nmap_results=nmap_results) + return None + + def _process_nmap_results(self,nmap_results): results = {} - text, err = util.run_command("nmap " + self._device_ipv4_addr) - LOGGER.info("nmap results\n" + str(text)) - if text: - rows = text.split("PORT")[1].split("MAC Address")[0].split("\n") + LOGGER.info("nmap results\n" + str(nmap_results)) + if nmap_results: + if "Service Info" in nmap_results: + rows = nmap_results.split("PORT")[1].split("Service Info")[0].split("\n") + else: + rows = nmap_results.split("PORT")[1].split("MAC Address")[0].split("\n") for result in rows[1:-1]: # Iterate skipping the header and tail rows cols = result.split(" ") port_result = {cols[0].split( "/")[0]: {"state": cols[1], "service": cols[2]}} results.update(port_result) - return results + return results \ No newline at end of file From 1c443157ccbd877c6387f6ba8776319e6ca5e3d9 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 5 May 2023 15:39:59 -0600 Subject: [PATCH 31/36] logging cleanup --- test_orc/modules/nmap/python/src/nmap_module.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test_orc/modules/nmap/python/src/nmap_module.py b/test_orc/modules/nmap/python/src/nmap_module.py index 82230befe..347916726 100644 --- a/test_orc/modules/nmap/python/src/nmap_module.py +++ b/test_orc/modules/nmap/python/src/nmap_module.py @@ -18,9 +18,8 @@ def __init__(self, module): LOGGER = self._get_logger() def _security_nmap_ports(self, config): - LOGGER.info( - "Running nmap scan test") + "Running security.nmap.ports test") if self._device_ipv4_addr is not None: self._scan_tcp_results = self._scan_tcp_ports() self._scan_udp_results = self._scan_udp_ports(config) @@ -46,14 +45,11 @@ def _check_scan_results(self, test_config): port_config.update(test_config["tcp_ports"]) elif "udp_ports" in test_config: port_config.update(test_config["udp_ports"]) - - LOGGER.info("Ports Config: " + str(port_config)) scan_results = {} if self._scan_tcp_results is not None: scan_results.update(self._scan_tcp_results) if self._scan_udp_results is not None: scan_results.update(self._scan_udp_results) - LOGGER.info("Scan Results: " + str(scan_results)) if port_config is not None: for port in port_config: result = None From 4f2c43532d0ef2e49af24138e8ea50305d0143bc Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Fri, 5 May 2023 16:24:56 -0600 Subject: [PATCH 32/36] Update TCP port scanning range Update logging --- .../modules/nmap/python/src/nmap_module.py | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/test_orc/modules/nmap/python/src/nmap_module.py b/test_orc/modules/nmap/python/src/nmap_module.py index 347916726..14e5120c2 100644 --- a/test_orc/modules/nmap/python/src/nmap_module.py +++ b/test_orc/modules/nmap/python/src/nmap_module.py @@ -21,11 +21,10 @@ def _security_nmap_ports(self, config): LOGGER.info( "Running security.nmap.ports test") if self._device_ipv4_addr is not None: - self._scan_tcp_results = self._scan_tcp_ports() + self._scan_tcp_results = self._scan_tcp_ports(config) self._scan_udp_results = self._scan_udp_ports(config) - LOGGER.info("TCP Scan Results: " + str(self._scan_tcp_results)) - LOGGER.info("UDP Scan Results: " + str(self._scan_udp_results)) - LOGGER.info("nmap scan test finished") + LOGGER.debug("TCP scan results: " + str(self._scan_tcp_results)) + LOGGER.debug("UDP scan results: " + str(self._scan_udp_results)) self._process_port_results( tests=config) LOGGER.info("Unallowed Ports: " + str(self._unallowed_ports)) @@ -36,7 +35,7 @@ def _security_nmap_ports(self, config): def _process_port_results(self, tests): for test in tests: - LOGGER.info("Checking test: " + str(test)) + LOGGER.info("Checking results for test: " + str(test)) self._check_scan_results(test_config=tests[test]) def _check_scan_results(self, test_config): @@ -45,6 +44,7 @@ def _check_scan_results(self, test_config): port_config.update(test_config["tcp_ports"]) elif "udp_ports" in test_config: port_config.update(test_config["udp_ports"]) + scan_results = {} if self._scan_tcp_results is not None: scan_results.update(self._scan_tcp_results) @@ -53,14 +53,14 @@ def _check_scan_results(self, test_config): if port_config is not None: for port in port_config: result = None - LOGGER.info("Checking Port: " + str(port)) - LOGGER.debug("Port Config: " + str(port_config[port])) + LOGGER.info("Checking port: " + str(port)) + LOGGER.debug("Port config: " + str(port_config[port])) if port in scan_results: if scan_results[port]["state"] == "open": if not port_config[port]["allowed"]: LOGGER.info("Unallowed port open") - result = False self._unallowed_ports.append(str(port)) + result = False else: LOGGER.info("Allowed port open") result = True @@ -76,9 +76,22 @@ def _check_scan_results(self, test_config): else: port_config[port]["result"] = "skipped" - def _scan_tcp_ports(self): - LOGGER.info( "Running nmap TCP port scans") - nmap_results, err = util.run_command("nmap -sT -sV -Pn -v -p 1-100,5357 --allports --version-intensity 7 -T4 " + self._device_ipv4_addr) + def _scan_tcp_ports(self, tests): + max_port = 1000 + ports = [] + for test in tests: + test_config = tests[test] + if "tcp_ports" in test_config: + for port in test_config["tcp_ports"]: + if int(port) > max_port: + ports.append(port) + ports_to_scan="1-" + str(max_port) + if len(ports) > 0: + ports_to_scan += "," + ','.join(ports) + LOGGER.info("Running nmap TCP port scan") + LOGGER.info("TCP ports: " + str(ports_to_scan)) + nmap_results, err = util.run_command("nmap -sT -sV -Pn -v -p " + ports_to_scan + " --allports --version-intensity 7 -T4 " + self._device_ipv4_addr) + LOGGER.info("TCP port scan complete") return self._process_nmap_results(nmap_results=nmap_results) def _scan_udp_ports(self, tests): @@ -90,15 +103,16 @@ def _scan_udp_ports(self, tests): ports.append(port) if len(ports) > 0: port_list = ','.join(ports) - LOGGER.info("UDP Ports: " + str(port_list)) - LOGGER.info( "Running nmap UDP port scans") + LOGGER.info("Running nmap UDP port scan") + LOGGER.info("UDP ports: " + str(port_list)) nmap_results, err = util.run_command("nmap -sU -sV -p " + port_list + " " + self._device_ipv4_addr) + LOGGER.info("UDP port scan complete") return self._process_nmap_results(nmap_results=nmap_results) return None def _process_nmap_results(self,nmap_results): results = {} - LOGGER.info("nmap results\n" + str(nmap_results)) + LOGGER.debug("nmap results\n" + str(nmap_results)) if nmap_results: if "Service Info" in nmap_results: rows = nmap_results.split("PORT")[1].split("Service Info")[0].split("\n") @@ -107,6 +121,8 @@ def _process_nmap_results(self,nmap_results): for result in rows[1:-1]: # Iterate skipping the header and tail rows cols = result.split(" ") port_result = {cols[0].split( - "/")[0]: {"state": cols[1], "service": cols[2]}} + "/")[0]: {"state": cols[1], "service": cols[2], "version":""}} + if len(cols) > 3: + port_result["version"]=cols[3] results.update(port_result) return results \ No newline at end of file From 28114c65da86001992f47e428fde0f0927d76d9b Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Mon, 8 May 2023 11:27:20 -0600 Subject: [PATCH 33/36] Merge device config into module config Update device template --- resources/devices/Template/device_config.json | 115 ++++++++++++++++++ .../modules/base/python/src/test_module.py | 10 +- test_orc/modules/nmap/conf/module_config.json | 10 -- .../modules/nmap/python/src/nmap_module.py | 6 + 4 files changed, 127 insertions(+), 14 deletions(-) diff --git a/resources/devices/Template/device_config.json b/resources/devices/Template/device_config.json index f8b56b7a3..7a3d4441c 100644 --- a/resources/devices/Template/device_config.json +++ b/resources/devices/Template/device_config.json @@ -27,6 +27,121 @@ "enabled": true } } + }, + "nmap": { + "enabled": true, + "tests": { + "security.nmap.ports": { + "enabled": true, + "security.services.ftp": { + "tcp_ports": { + "20": { + "allowed": false + }, + "21": { + "allowed": false + } + } + }, + "security.services.ssh": { + "tcp_ports": { + "22": { + "allowed": true + } + } + }, + "security.services.telnet": { + "tcp_ports": { + "23": { + "allowed": false + } + } + }, + "security.services.smtp": { + "tcp_ports": { + "25": { + "allowed": false + }, + "465": { + "allowed": false + }, + "587": { + "allowed": false + } + } + }, + "security.services.http": { + "tcp_ports": { + "80": { + "allowed": false + } + } + }, + "security.services.pop": { + "tcp_ports": { + "110": { + "allowed": false + } + } + }, + "security.services.imap": { + "tcp_ports": { + "143": { + "allowed": false + } + } + }, + "security.services.snmpv3": { + "tcp_ports": { + "161": { + "allowed": false + }, + "162": { + "allowed": false + } + }, + "udp_ports": { + "161": { + "allowed": false + }, + "162": { + "allowed": false + } + } + }, + "security.services.https": { + "tcp_ports": { + "80": { + "allowed": false + } + } + }, + "security.services.vnc": { + "tcp_ports": { + "5500": { + "allowed": false + }, + "5800": { + "allowed": false + } + } + }, + "security.services.tftp": { + "udp_ports": { + "69": { + "allowed": false + } + } + }, + "security.services.ntp": { + "udp_ports": { + "123": { + "allowed": false + } + } + } + } + } } } } \ No newline at end of file diff --git a/test_orc/modules/base/python/src/test_module.py b/test_orc/modules/base/python/src/test_module.py index 93c6847ee..9a348faa7 100644 --- a/test_orc/modules/base/python/src/test_module.py +++ b/test_orc/modules/base/python/src/test_module.py @@ -33,15 +33,17 @@ def _get_tests(self): def _get_device_tests(self, device_test_module): module_tests = self._config["config"]["tests"] - LOGGER.info("Device Config: " + str(device_test_module)) if device_test_module is None: return module_tests elif not device_test_module["enabled"]: return [] else: for test in module_tests: + # Resolve device specific configurations for the test if it exists + # and update module test config with device config options if test["name"] in device_test_module["tests"]: - test["enabled"] = device_test_module["tests"][test["name"]]["enabled"] + dev_test_config = device_test_module["tests"][test["name"]] + test["config"].update(dev_test_config) return module_tests def _get_device_test_module(self): @@ -55,7 +57,6 @@ def run_tests(self): self._device_ipv4_addr = self._get_device_ipv4() LOGGER.info("Device IP Resolved: " + str(self._device_ipv4_addr)) tests = self._get_tests() - device_modules = os.environ['DEVICE_TEST_MODULES'] for test in tests: test_method_name = "_" + test["name"].replace(".", "_") result = None @@ -65,7 +66,8 @@ def run_tests(self): # Resolve the correct python method by test name and run test if hasattr(self, test_method_name): if "config" in test: - result = getattr(self,test_method_name)(config=test["config"]) + result = getattr(self, test_method_name)( + config=test["config"]) else: result = getattr(self, test_method_name)() else: diff --git a/test_orc/modules/nmap/conf/module_config.json b/test_orc/modules/nmap/conf/module_config.json index 20eb6eb8a..e8e21c29b 100644 --- a/test_orc/modules/nmap/conf/module_config.json +++ b/test_orc/modules/nmap/conf/module_config.json @@ -165,16 +165,6 @@ }, "description": "Check NTP port 123 is disabled and the device is not operating as an NTP server", "expected_behavior": "The device dos not respond to NTP requests when it's IP is set as the NTP server on another device" - }, - "security.services.wsdapi": { - "tcp_ports": { - "5357": { - "allowed": false, - "description": "Some sort of open api" - } - }, - "description": "Check TELNET port 23 is disabled and TELNET is not running on any port", - "expected_behavior": "There is no FTP service running on any port" } } } diff --git a/test_orc/modules/nmap/python/src/nmap_module.py b/test_orc/modules/nmap/python/src/nmap_module.py index 14e5120c2..9b3bae3b3 100644 --- a/test_orc/modules/nmap/python/src/nmap_module.py +++ b/test_orc/modules/nmap/python/src/nmap_module.py @@ -20,6 +20,12 @@ def __init__(self, module): def _security_nmap_ports(self, config): LOGGER.info( "Running security.nmap.ports test") + + # Delete the enabled key from the config if it exists + # to prevent it being treated as a test key + if "enabled" in config: + del config["enabled"] + if self._device_ipv4_addr is not None: self._scan_tcp_results = self._scan_tcp_ports(config) self._scan_udp_results = self._scan_udp_ports(config) From 09a92392a05d27ba4ac53fadecbb2f8257b821b0 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Mon, 8 May 2023 12:05:13 -0600 Subject: [PATCH 34/36] fix merge issues --- framework/testrun.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/framework/testrun.py b/framework/testrun.py index 80733d6c6..d1235f595 100644 --- a/framework/testrun.py +++ b/framework/testrun.py @@ -6,9 +6,6 @@ Run using the provided command scripts in the cmd folder. E.g sudo cmd/start """ -import network_orchestrator as net_orc # pylint: disable=wrong-import-position,import-outside-toplevel -import test_orchestrator as test_orc # pylint: disable=wrong-import-position,import-outside-toplevel -from listener import NetworkEvent # pylint: disable=wrong-import-position,import-outside-toplevel import os import sys import json From 8b289981b6af4e6a775bdf8d2d5ec0d540488902 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Tue, 16 May 2023 09:40:01 -0600 Subject: [PATCH 35/36] Update timeouts Add multi-threading for multiple scanns to run simultaneously Add option to use scan scripts for services --- framework/testrun.py | 2 +- net_orc/python/src/network_orchestrator.py | 2 +- test_orc/modules/base/base.Dockerfile | 2 +- test_orc/modules/nmap/conf/module_config.json | 5 +- .../modules/nmap/python/src/nmap_module.py | 159 ++++++++++++++---- 5 files changed, 133 insertions(+), 37 deletions(-) diff --git a/framework/testrun.py b/framework/testrun.py index d1235f595..3a7554d7a 100644 --- a/framework/testrun.py +++ b/framework/testrun.py @@ -30,7 +30,7 @@ LOGGER = logger.get_logger('test_run') CONFIG_FILE = 'conf/system.json' EXAMPLE_CONFIG_FILE = 'conf/system.json.example' -RUNTIME = 300 +RUNTIME = 600 LOCAL_DEVICES_DIR = 'local/devices' RESOURCE_DEVICES_DIR = 'resources/devices' diff --git a/net_orc/python/src/network_orchestrator.py b/net_orc/python/src/network_orchestrator.py index 1bbd90ab2..d556e30f5 100644 --- a/net_orc/python/src/network_orchestrator.py +++ b/net_orc/python/src/network_orchestrator.py @@ -26,7 +26,7 @@ INTERNET_BRIDGE = "tr-c" PRIVATE_DOCKER_NET = "tr-private-net" CONTAINER_NAME = "network_orchestrator" -RUNTIME = 300 +RUNTIME = 600 class NetworkOrchestrator: diff --git a/test_orc/modules/base/base.Dockerfile b/test_orc/modules/base/base.Dockerfile index 8354f05be..a508caef7 100644 --- a/test_orc/modules/base/base.Dockerfile +++ b/test_orc/modules/base/base.Dockerfile @@ -2,7 +2,7 @@ FROM ubuntu:jammy # Install common software -RUN apt-get update && apt-get install -y net-tools iputils-ping tcpdump iproute2 jq python3 python3-pip dos2unix nmap +RUN apt-get update && apt-get install -y net-tools iputils-ping tcpdump iproute2 jq python3 python3-pip dos2unix nmap --fix-missing # Setup the base python requirements COPY modules/base/python /testrun/python diff --git a/test_orc/modules/nmap/conf/module_config.json b/test_orc/modules/nmap/conf/module_config.json index e8e21c29b..5449327a1 100644 --- a/test_orc/modules/nmap/conf/module_config.json +++ b/test_orc/modules/nmap/conf/module_config.json @@ -8,7 +8,7 @@ "network": true, "docker": { "enable_container": true, - "timeout": 300 + "timeout": 600 }, "tests": [ { @@ -71,6 +71,9 @@ "security.services.http": { "tcp_ports": { "80": { + "service_scan": { + "script": "http-methods" + }, "allowed": false, "description": "Administrative Insecure Web-Server" } diff --git a/test_orc/modules/nmap/python/src/nmap_module.py b/test_orc/modules/nmap/python/src/nmap_module.py index 9b3bae3b3..4f9ae144d 100644 --- a/test_orc/modules/nmap/python/src/nmap_module.py +++ b/test_orc/modules/nmap/python/src/nmap_module.py @@ -3,6 +3,7 @@ import time import util import json +import threading from test_module import TestModule LOG_NAME = "test_nmap" @@ -14,6 +15,9 @@ class NmapModule(TestModule): def __init__(self, module): super().__init__(module_name=module, log_name=LOG_NAME) self._unallowed_ports = [] + self._scan_tcp_results = None + self._udp_tcp_results = None + self._script_scan_results = None global LOGGER LOGGER = self._get_logger() @@ -27,14 +31,35 @@ def _security_nmap_ports(self, config): del config["enabled"] if self._device_ipv4_addr is not None: - self._scan_tcp_results = self._scan_tcp_ports(config) - self._scan_udp_results = self._scan_udp_ports(config) + # Run the monitor method asynchronously to keep this method non-blocking + self._tcp_scan_thread = threading.Thread( + target=self._scan_tcp_ports, args=(config,)) + self._udp_scan_thread = threading.Thread( + target=self._scan_udp_ports, args=(config,)) + self._script_scan_thread = threading.Thread( + target=self._scan_scripts, args=(config,)) + + self._tcp_scan_thread.daemon = True + self._udp_scan_thread.daemon = True + self._script_scan_thread.daemon = True + + self._tcp_scan_thread.start() + self._udp_scan_thread.start() + self._script_scan_thread.start() + + while self._tcp_scan_thread.is_alive() or self._udp_scan_thread.is_alive() or self._script_scan_thread.is_alive(): + time.sleep(1) + LOGGER.debug("TCP scan results: " + str(self._scan_tcp_results)) LOGGER.debug("UDP scan results: " + str(self._scan_udp_results)) + LOGGER.debug("Service scan results: " + + str(self._script_scan_results)) self._process_port_results( tests=config) LOGGER.info("Unallowed Ports: " + str(self._unallowed_ports)) - return len(self._unallowed_ports)==0 + LOGGER.info("Script scan results:\n" + + json.dumps(self._script_scan_results)) + return len(self._unallowed_ports) == 0 else: LOGGER.info("Device ip address not resolved, skipping") return None @@ -50,12 +75,14 @@ def _check_scan_results(self, test_config): port_config.update(test_config["tcp_ports"]) elif "udp_ports" in test_config: port_config.update(test_config["udp_ports"]) - + scan_results = {} if self._scan_tcp_results is not None: scan_results.update(self._scan_tcp_results) if self._scan_udp_results is not None: scan_results.update(self._scan_udp_results) + if self._script_scan_results is not None: + scan_results.update(self._script_scan_results) if port_config is not None: for port in port_config: result = None @@ -82,53 +109,119 @@ def _check_scan_results(self, test_config): else: port_config[port]["result"] = "skipped" + def _scan_scripts(self, tests): + scan_results = {} + LOGGER.info("Checing for scan scripts") + for test in tests: + test_config = tests[test] + if "tcp_ports" in test_config: + for port in test_config["tcp_ports"]: + port_config = test_config["tcp_ports"][port] + if "service_scan" in port_config: + LOGGER.info("Service Scan Detected for: " + str(port)) + svc = port_config["service_scan"] + scan_results.update( + self._scan_tcp_with_script(svc["script"])) + if "udp_ports" in test_config: + for port in test_config["udp_ports"]: + if "service_scan" in port: + LOGGER.info("Service Scan Detected for: " + str(port)) + svc = port["service_scan"] + self._scan_udp_with_script(svc["script"], port) + scan_results.update( + self._scan_tcp_with_script(svc["script"])) + self._script_scan_results = scan_results + + def _scan_tcp_with_script(self, script_name, ports=None): + LOGGER.info("Running TCP nmap scan with script " + script_name) + scan_options = " -v -n T3 --host-timeout=6m -A --script " + script_name + port_options = " --open " + if ports is None: + port_options += " -p- " + else: + port_options += " -p" + ports + " " + results_file = "/runtime/output/" + self._module_name + "-"+script_name+".log" + nmap_options = scan_options + port_options + " -oG " + results_file + nmap_results, err = util.run_command( + "nmap " + nmap_options + " " + self._device_ipv4_addr) + LOGGER.info("Nmap TCP script scan complete") + LOGGER.info("nmap script results\n" + str(nmap_results)) + return self._process_nmap_results(nmap_results=nmap_results) + + def _scan_udp_with_script(self, script_name, ports=None): + LOGGER.info("Running UDP nmap scan with script " + script_name) + scan_options = " --sU -Pn -n --script " + script_name + port_options = " --open " + if ports is None: + port_options += " -p- " + else: + port_options += " -p" + ports + " " + nmap_options = scan_options + port_options + nmap_results, err = util.run_command( + "nmap " + nmap_options + self._device_ipv4_addr) + LOGGER.info("Nmap UDP script scan complete") + LOGGER.info("nmap script results\n" + str(nmap_results)) + return self._process_nmap_results(nmap_results=nmap_results) + def _scan_tcp_ports(self, tests): max_port = 1000 ports = [] for test in tests: test_config = tests[test] if "tcp_ports" in test_config: - for port in test_config["tcp_ports"]: - if int(port) > max_port: - ports.append(port) - ports_to_scan="1-" + str(max_port) - if len(ports) > 0: + for port in test_config["tcp_ports"]: + if int(port) > max_port: + ports.append(port) + ports_to_scan = "1-" + str(max_port) + if len(ports) > 0: ports_to_scan += "," + ','.join(ports) LOGGER.info("Running nmap TCP port scan") LOGGER.info("TCP ports: " + str(ports_to_scan)) - nmap_results, err = util.run_command("nmap -sT -sV -Pn -v -p " + ports_to_scan + " --allports --version-intensity 7 -T4 " + self._device_ipv4_addr) + nmap_results, err = util.run_command( + "nmap -sT -sV -Pn -v -p " + ports_to_scan + " --allports --version-intensity 7 -T4 " + self._device_ipv4_addr) LOGGER.info("TCP port scan complete") - return self._process_nmap_results(nmap_results=nmap_results) + self._scan_tcp_results = self._process_nmap_results( + nmap_results=nmap_results) def _scan_udp_ports(self, tests): ports = [] for test in tests: test_config = tests[test] if "udp_ports" in test_config: - for port in test_config["udp_ports"]: - ports.append(port) + for port in test_config["udp_ports"]: + ports.append(port) if len(ports) > 0: port_list = ','.join(ports) - LOGGER.info("Running nmap UDP port scan") - LOGGER.info("UDP ports: " + str(port_list)) - nmap_results, err = util.run_command("nmap -sU -sV -p " + port_list + " " + self._device_ipv4_addr) - LOGGER.info("UDP port scan complete") - return self._process_nmap_results(nmap_results=nmap_results) - return None - - def _process_nmap_results(self,nmap_results): + LOGGER.info("Running nmap UDP port scan") + LOGGER.info("UDP ports: " + str(port_list)) + nmap_results, err = util.run_command( + "nmap -sU -sV -p " + port_list + " " + self._device_ipv4_addr) + LOGGER.info("UDP port scan complete") + self._scan_udp_results = self._process_nmap_results( + nmap_results=nmap_results) + + def _process_nmap_results(self, nmap_results): results = {} - LOGGER.debug("nmap results\n" + str(nmap_results)) + LOGGER.info("nmap results\n" + str(nmap_results)) if nmap_results: if "Service Info" in nmap_results: - rows = nmap_results.split("PORT")[1].split("Service Info")[0].split("\n") - else: - rows = nmap_results.split("PORT")[1].split("MAC Address")[0].split("\n") - for result in rows[1:-1]: # Iterate skipping the header and tail rows - cols = result.split(" ") - port_result = {cols[0].split( - "/")[0]: {"state": cols[1], "service": cols[2], "version":""}} - if len(cols) > 3: - port_result["version"]=cols[3] - results.update(port_result) - return results \ No newline at end of file + rows = nmap_results.split("PORT")[1].split( + "Service Info")[0].split("\n") + elif "PORT" in nmap_results: + rows = nmap_results.split("PORT")[1].split( + "MAC Address")[0].split("\n") + if rows: + for result in rows[1:-1]: # Iterate skipping the header and tail rows + cols = result.split() + port = cols[0].split("/")[0] + # If results don't start with a a port number, it's likely a bleed over + # from previous result so we need to ignore it + if port.isdigit(): + version = "" + if len(cols) > 3: + # recombine full version information that may contain spaces + version = ' '.join(cols[3:]) + port_result = {cols[0].split( + "/")[0]: {"state": cols[1], "service": cols[2], "version": version}} + results.update(port_result) + return results From 1090cb8e44ee9e43f565070f0017e480faee8475 Mon Sep 17 00:00:00 2001 From: jhughesbiot Date: Tue, 16 May 2023 16:13:26 -0600 Subject: [PATCH 36/36] Fix merge issues --- framework/testrun.py | 31 +++-------- net_orc/python/src/network_orchestrator.py | 52 +++++++------------ .../modules/nmap/python/src/nmap_module.py | 2 +- test_orc/modules/nmap/python/src/run.py | 1 - test_orc/python/src/test_orchestrator.py | 2 +- 5 files changed, 28 insertions(+), 60 deletions(-) diff --git a/framework/testrun.py b/framework/testrun.py index 2167ac232..44c3bca6d 100644 --- a/framework/testrun.py +++ b/framework/testrun.py @@ -25,6 +25,9 @@ test_orc_dir = os.path.join(parent_dir, 'test_orc', 'python', 'src') sys.path.append(test_orc_dir) +from listener import NetworkEvent # pylint: disable=wrong-import-position,import-outside-toplevel +import test_orchestrator as test_orc # pylint: disable=wrong-import-position,import-outside-toplevel +import network_orchestrator as net_orc # pylint: disable=wrong-import-position,import-outside-toplevel from device import Device # pylint: disable=wrong-import-position,import-outside-toplevel @@ -57,9 +60,6 @@ def __init__(self, config_file=CONFIG_FILE, validate=True, net_only=False, singl # Catch any exit signals self._register_exits() - # Import the correct net orchestrator - self.import_dependencies() - # Expand the config file to absolute pathing config_file_abs = self._get_config_abs(config_file=config_file) @@ -68,7 +68,7 @@ def __init__(self, config_file=CONFIG_FILE, validate=True, net_only=False, singl validate=validate, async_monitor=not self._net_only, single_intf = self._single_intf) - self._test_orc = test_orc.TestOrchestrator() + self._test_orc = test_orc.TestOrchestrator(self._net_orc) def start(self): @@ -81,9 +81,11 @@ def start(self): else: self._start_network() self._test_orc.start() + self._net_orc.listener.register_callback( - self._device_discovered, - [NetworkEvent.DEVICE_DISCOVERED]) + self._device_stable, + [NetworkEvent.DEVICE_STABLE] + ) LOGGER.info("Waiting for devices on the network...") @@ -96,23 +98,6 @@ def stop(self, kill=False): self._stop_tests() self._stop_network(kill=kill) - def import_dependencies(self): - # Add net_orc to Python path - net_orc_dir = os.path.join(parent_dir, 'net_orc', 'python', 'src') - sys.path.append(net_orc_dir) - # Import the network orchestrator - global net_orc - import network_orchestrator as net_orc # pylint: disable=wrong-import-position,import-outside-toplevel - - # Add test_orc to Python path - test_orc_dir = os.path.join(parent_dir, 'test_orc', 'python', 'src') - sys.path.append(test_orc_dir) - global test_orc - import test_orchestrator as test_orc # pylint: disable=wrong-import-position,import-outside-toplevel - - global NetworkEvent - from listener import NetworkEvent # pylint: disable=wrong-import-position,import-outside-toplevel - def _register_exits(self): signal.signal(signal.SIGINT, self._exit_handler) signal.signal(signal.SIGTERM, self._exit_handler) diff --git a/net_orc/python/src/network_orchestrator.py b/net_orc/python/src/network_orchestrator.py index 75fdc6128..5399896ba 100644 --- a/net_orc/python/src/network_orchestrator.py +++ b/net_orc/python/src/network_orchestrator.py @@ -111,10 +111,6 @@ def start_network(self): # Get network ready (via Network orchestrator) LOGGER.info("Network is ready.") - # Start the listener - self.listener = Listener(self._dev_intf) - self.listener.start_listener() - def stop(self, kill=False): """Stop the network orchestrator.""" self.stop_validator(kill=kill) @@ -137,22 +133,26 @@ def monitor_network(self): self.stop() - def load_config(self,config_file=None): + def load_config(self, config_file=None): + if config_file is None: # If not defined, use relative pathing to local file - self._config_file=os.path.join(self._path, CONFIG_FILE) + self._config_file = os.path.join(self._path, CONFIG_FILE) else: # If defined, use as provided - self._config_file=config_file + self._config_file = config_file - self.listener.register_callback(self._device_discovered, [ - NetworkEvent.DEVICE_DISCOVERED]) - self.listener.register_callback( - self._dhcp_lease_ack, [NetworkEvent.DHCP_LEASE_ACK]) - # TODO: This time should be configurable (How long to hold before exiting, this could be infinite too) - time.sleep(self._runtime) + if not os.path.isfile(self._config_file): + LOGGER.error("Configuration file is not present at " + config_file) + LOGGER.info("An example is present in " + EXAMPLE_CONFIG_FILE) + sys.exit(1) + + LOGGER.info("Loading config file: " + + os.path.abspath(self._config_file)) + with open(self._config_file, encoding='UTF-8') as config_json_file: + config_json = json.load(config_json_file) + self.import_config(config_json) - self.stop() def _device_discovered(self, mac_addr): @@ -197,26 +197,6 @@ def _get_device(self, mac_addr): self._devices.append(device) return device - def load_config(self, config_file=None): - if config_file is None: - # If not defined, use relative pathing to local file - self._config_file = os.path.join(self._path, CONFIG_FILE) - else: - # If defined, use as provided - self._config_file = config_file - - if not os.path.isfile(self._config_file): - LOGGER.error("Configuration file is not present at " + config_file) - LOGGER.info("An example is present in " + EXAMPLE_CONFIG_FILE) - sys.exit(1) - - LOGGER.info("Loading config file: " + - os.path.abspath(self._config_file)) - - with open(self._config_file, encoding='UTF-8') as config_json_file: - config_json = json.load(config_json_file) - self.import_config(config_json) - def import_config(self, json_config): self._int_intf = json_config['network']['internet_intf'] self._dev_intf = json_config['network']['device_intf'] @@ -347,6 +327,10 @@ def create_net(self): self._create_private_net() self.listener = Listener(self._dev_intf) + self.listener.register_callback(self._device_discovered, [ + NetworkEvent.DEVICE_DISCOVERED]) + self.listener.register_callback( + self._dhcp_lease_ack, [NetworkEvent.DHCP_LEASE_ACK]) self.listener.start_listener() def load_network_modules(self): diff --git a/test_orc/modules/nmap/python/src/nmap_module.py b/test_orc/modules/nmap/python/src/nmap_module.py index 4f9ae144d..7d5bd3604 100644 --- a/test_orc/modules/nmap/python/src/nmap_module.py +++ b/test_orc/modules/nmap/python/src/nmap_module.py @@ -178,7 +178,7 @@ def _scan_tcp_ports(self, tests): LOGGER.info("Running nmap TCP port scan") LOGGER.info("TCP ports: " + str(ports_to_scan)) nmap_results, err = util.run_command( - "nmap -sT -sV -Pn -v -p " + ports_to_scan + " --allports --version-intensity 7 -T4 " + self._device_ipv4_addr) + "nmap -sT -sV -Pn -v -p " + ports_to_scan + " --version-intensity 7 -T4 " + self._device_ipv4_addr) LOGGER.info("TCP port scan complete") self._scan_tcp_results = self._process_nmap_results( nmap_results=nmap_results) diff --git a/test_orc/modules/nmap/python/src/run.py b/test_orc/modules/nmap/python/src/run.py index 48bb92dd2..4c8294769 100644 --- a/test_orc/modules/nmap/python/src/run.py +++ b/test_orc/modules/nmap/python/src/run.py @@ -8,7 +8,6 @@ from nmap_module import NmapModule LOGGER = logger.get_logger('test_module') -RUNTIME = 300 class NmapModuleRunner: diff --git a/test_orc/python/src/test_orchestrator.py b/test_orc/python/src/test_orchestrator.py index db59ca1fd..28fd0af11 100644 --- a/test_orc/python/src/test_orchestrator.py +++ b/test_orc/python/src/test_orchestrator.py @@ -43,7 +43,7 @@ def stop(self): """Stop any running tests""" self._stop_modules() - def _run_test_modules(self, device): + def run_test_modules(self, device): """Iterates through each test module and starts the container.""" LOGGER.info(f"Running test modules on device with mac addr {device.mac_addr}") for module in self._test_modules: