monkey/envs/monkey_zoo/blackbox/test_blackbox.py

75 lines
2.5 KiB
Python
Raw Normal View History

2019-08-28 19:56:35 +08:00
import os
2019-08-27 20:52:39 +08:00
import unittest
2019-08-27 23:12:14 +08:00
from .gcp_machine_handlers import GCPHandler
2019-08-27 20:52:39 +08:00
2019-08-27 21:23:09 +08:00
import requests
from config import *
2019-08-29 19:57:04 +08:00
# SHA3-512 of '1234567890!@#$%^&*()_nothing_up_my_sleeve_1234567890!@#$%^&*()'
NO_AUTH_CREDS = '55e97c9dcfd22b8079189ddaeea9bce8125887e3237b800c6176c9afa80d2062' \
'8d2c8d0b1538d2208c1444ac66535b764a3d902b35e751df3faec1e477ed3557'
2019-08-27 20:52:39 +08:00
2019-08-29 19:57:04 +08:00
def generic_blackbox_test_case(client, config_file_path, analyzers):
load_config_into_server(client, config_file_path)
2019-08-28 19:56:35 +08:00
run_local_monkey_on_island()
for analyzer in analyzers:
assert analyzer.analyze_test_results()
2019-08-29 19:57:04 +08:00
class MonkeyIslandClient(object):
def __init__(self, server_address):
self.addr = "https://{IP}/".format(IP=server_address)
self.token = self.get_jwt_token_from_server()
def get_jwt_token_from_server(self):
resp = requests.post(self.addr + "api/auth", json={"username": NO_AUTH_CREDS, "password": NO_AUTH_CREDS}, verify=False)
return resp.json()["access_token"]
def get_api_status(self):
return requests.get(self.addr + "api", headers={"Authorization": "JWT " + self.token}, verify=False)
2019-08-27 23:12:14 +08:00
2019-08-29 19:57:04 +08:00
def import_config(self, config_contents):
resp = requests.post(
self.addr + "api/configuration/island",
headers={"Authorization": "JWT " + self.token},
data=config_contents,
verify=False)
print(resp.text)
class TestMonkeyBlackbox(unittest.TestCase):
2019-08-27 20:52:39 +08:00
@classmethod
def setUpClass(cls):
2019-08-29 19:57:04 +08:00
#GCPHandler().start_machines("elastic-4")
2019-08-27 20:52:39 +08:00
print("Setting up all GCP machines...")
@classmethod
def tearDownClass(cls):
2019-08-29 19:57:04 +08:00
#GCPHandler().stop_machines("elastic-4")
2019-08-27 20:52:39 +08:00
print("Killing all GCP machines...")
2019-08-29 19:57:04 +08:00
def test_server_online(self):
client = MonkeyIslandClient(ISLAND_SERVER_ADDRESS)
assert client.get_api_status() is not None
2019-08-28 19:56:35 +08:00
def test_ssh_exec(self):
2019-08-29 19:57:04 +08:00
client = MonkeyIslandClient(ISLAND_SERVER_ADDRESS)
conf_file_name = "SSH.conf"
generic_blackbox_test_case(client, get_conf_file_path(conf_file_name), [])
2019-08-28 19:56:35 +08:00
def run_local_monkey_on_island():
print("Trying to run local monkey on {}".format(ISLAND_SERVER_ADDRESS))
2019-08-29 19:57:04 +08:00
def load_config_into_server(client, config_file_path):
2019-08-28 19:56:35 +08:00
print("uploading {} to {}".format(config_file_path, ISLAND_SERVER_ADDRESS))
2019-08-29 19:57:04 +08:00
with open(config_file_path, "r") as config_file:
client.import_config(config_file.read())
2019-08-27 21:23:09 +08:00
2019-08-28 19:56:35 +08:00
def get_conf_file_path(conf_file_name):
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "island_configs", conf_file_name)