Added setting project to GCPHandler

This commit is contained in:
Shay Nehmad 2019-08-28 14:56:35 +03:00
parent 45fa7570ef
commit 11a1578893
3 changed files with 34 additions and 12 deletions

View File

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

View File

@ -4,13 +4,19 @@ import subprocess
class GCPHandler(object):
AUTHENTICATION_COMMAND = "gcloud auth activate-service-account --key-file=%s"
SET_PROPERTY_PROJECT = "gcloud config set project %s"
MACHINE_STARTING_COMMAND = "gcloud compute instances start %s --zone=%s"
MACHINE_STOPPING_COMMAND = "gcloud compute instances stop %s --zone=%s"
def __init__(self, key_path="../gcp_keys/gcp_key.json", zone="europe-west3-a"):
def __init__(self, key_path="../gcp_keys/gcp_key.json", zone="europe-west3-a", project_id="guardicore-22050661"):
self.zone = zone
try:
# pass the key file to gcp
subprocess.call(GCPHandler.get_auth_command(key_path), shell=True)
print("GCP Handler passed key")
# set project
subprocess.call(GCPHandler.get_set_project_command(project_id), shell=True)
print("GCP Handler set project")
print("GCP Handler initialized successfully")
except Exception as e:
print("GCP Handler failed to initialize: %s." % e)
@ -32,3 +38,7 @@ class GCPHandler(object):
@staticmethod
def get_auth_command(key_path):
return GCPHandler.AUTHENTICATION_COMMAND % key_path
@staticmethod
def get_set_project_command(project):
return GCPHandler.SET_PROPERTY_PROJECT % project

View File

@ -1,3 +1,4 @@
import os
import unittest
from .gcp_machine_handlers import GCPHandler
@ -6,6 +7,13 @@ import requests
from config import *
def generic_blackbox_test_case(config_file_path, analyzers):
load_config_into_server(config_file_path)
run_local_monkey_on_island()
for analyzer in analyzers:
assert analyzer.analyze_test_results()
class TestMonkeyBlackbox(unittest.TestCase):
@classmethod
@ -18,15 +26,19 @@ class TestMonkeyBlackbox(unittest.TestCase):
GCPHandler().stop_machines("elastic-4")
print("Killing all GCP machines...")
def generic_blackbox_test_case(self, config_file_path, analyzers):
self.load_config_into_server(config_file_path)
self.run_local_monkey_on_island()
for analyzer in analyzers:
assert analyzer.analyze_test_results()
def test_ssh_exec(self):
conf_file_name = "ssh.conf"
generic_blackbox_test_case(get_conf_file_path(conf_file_name), [])
def load_config_into_server(self, config_file_path):
def run_local_monkey_on_island():
print("Trying to run local monkey on {}".format(ISLAND_SERVER_ADDRESS))
print(ISLAND_SERVER_URL + "api")
def load_config_into_server(config_file_path):
print("uploading {} to {}".format(config_file_path, ISLAND_SERVER_ADDRESS))
def run_local_monkey_on_island(self):
print("Trying to run local monkey on {}".format(ISLAND_SERVER_ADDRESS))
print(requests.get(ISLAND_SERVER_URL_FORMAT.format(resource="api"), verify=False).text)
def get_conf_file_path(conf_file_name):
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "island_configs", conf_file_name)