Now pytest gets --island 1.2.3.4:5000 as command line param

This commit is contained in:
Shay Nehmad 2019-08-29 15:14:07 +03:00
parent 98a60a37df
commit ebd2628516
6 changed files with 46 additions and 53 deletions

0
envs/__init__.py Normal file
View File

View File

View File

@ -1,2 +0,0 @@
ISLAND_SERVER_ADDRESS = "1.2.3.4"
ISLAND_SERVER_URL = "https://{IP}/".format(IP=ISLAND_SERVER_ADDRESS)

View File

@ -1,10 +1,11 @@
import pytest
def pytest_addoption(parser): def pytest_addoption(parser):
parser.addoption("--island", action="store", default="default name") parser.addoption("--island", action="store", default="",
help="Specify the Monkey Island address (host+port).")
def pytest_generate_tests(metafunc): @pytest.fixture
# This is called for every test. Only get/set command line arguments def island(request):
# if the argument is specified in the list of test "fixturenames". request.cls.island = request.config.getoption("--island")
option_value = metafunc.config.option.name
if 'name' in metafunc.fixturenames and option_value is not None:
metafunc.parametrize("island", [option_value])

View File

@ -0,0 +1,27 @@
import requests
# SHA3-512 of '1234567890!@#$%^&*()_nothing_up_my_sleeve_1234567890!@#$%^&*()'
NO_AUTH_CREDS = '55e97c9dcfd22b8079189ddaeea9bce8125887e3237b800c6176c9afa80d2062' \
'8d2c8d0b1538d2208c1444ac66535b764a3d902b35e751df3faec1e477ed3557'
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)
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)

View File

@ -1,74 +1,41 @@
import os import os
import unittest import unittest
from .gcp_machine_handlers import GCPHandler
import requests import pytest
from config import * from envs.monkey_zoo.blackbox.monkey_island_client import MonkeyIslandClient
# SHA3-512 of '1234567890!@#$%^&*()_nothing_up_my_sleeve_1234567890!@#$%^&*()'
NO_AUTH_CREDS = '55e97c9dcfd22b8079189ddaeea9bce8125887e3237b800c6176c9afa80d2062' \
'8d2c8d0b1538d2208c1444ac66535b764a3d902b35e751df3faec1e477ed3557'
def generic_blackbox_test_case(client, config_file_path, analyzers): def generic_blackbox_test_case(client, config_file_path, analyzers):
load_config_into_server(client, config_file_path) with open(config_file_path, "r") as config_file:
run_local_monkey_on_island() client.import_config(config_file.read())
# run_local_monkey_on_island()
for analyzer in analyzers: for analyzer in analyzers:
assert analyzer.analyze_test_results() assert analyzer.analyze_test_results()
class MonkeyIslandClient(object): @pytest.mark.usefixtures("island")
def __init__(self, server_address): # noinspection PyUnresolvedReferences
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)
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): class TestMonkeyBlackbox(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
#GCPHandler().start_machines("elastic-4") # GCPHandler().start_machines("elastic-4")
print("Setting up all GCP machines...") print("Setting up all GCP machines...")
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
#GCPHandler().stop_machines("elastic-4") # GCPHandler().stop_machines("elastic-4")
print("Killing all GCP machines...") print("Killing all GCP machines...")
def test_server_online(self): def test_server_online(self):
client = MonkeyIslandClient(ISLAND_SERVER_ADDRESS) client = MonkeyIslandClient(self.island)
assert client.get_api_status() is not None assert client.get_api_status() is not None
def test_ssh_exec(self): def test_ssh_exec(self):
client = MonkeyIslandClient(ISLAND_SERVER_ADDRESS) client = MonkeyIslandClient(self.island)
conf_file_name = "SSH.conf" conf_file_name = "SSH.conf"
generic_blackbox_test_case(client, get_conf_file_path(conf_file_name), []) generic_blackbox_test_case(client, get_conf_file_path(conf_file_name), [])
def run_local_monkey_on_island():
print("Trying to run local monkey on {}".format(ISLAND_SERVER_ADDRESS))
def load_config_into_server(client, config_file_path):
print("uploading {} to {}".format(config_file_path, ISLAND_SERVER_ADDRESS))
with open(config_file_path, "r") as config_file:
client.import_config(config_file.read())
def get_conf_file_path(conf_file_name): def get_conf_file_path(conf_file_name):
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "island_configs", conf_file_name) return os.path.join(os.path.dirname(os.path.abspath(__file__)), "island_configs", conf_file_name)