From 11a157889341d9d7be1595cf4f40f3f75c073eea Mon Sep 17 00:00:00 2001
From: Shay Nehmad <shay.nehmad@guardicore.com>
Date: Wed, 28 Aug 2019 14:56:35 +0300
Subject: [PATCH] Added setting project to GCPHandler

---
 envs/monkey_zoo/blackbox/config.py            |  2 +-
 .../blackbox/gcp_machine_handlers.py          | 12 ++++++-
 envs/monkey_zoo/blackbox/test_blackbox.py     | 32 +++++++++++++------
 3 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/envs/monkey_zoo/blackbox/config.py b/envs/monkey_zoo/blackbox/config.py
index 95efd9835..2a38498fd 100644
--- a/envs/monkey_zoo/blackbox/config.py
+++ b/envs/monkey_zoo/blackbox/config.py
@@ -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)
diff --git a/envs/monkey_zoo/blackbox/gcp_machine_handlers.py b/envs/monkey_zoo/blackbox/gcp_machine_handlers.py
index aae0c34b4..4937191ed 100644
--- a/envs/monkey_zoo/blackbox/gcp_machine_handlers.py
+++ b/envs/monkey_zoo/blackbox/gcp_machine_handlers.py
@@ -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
diff --git a/envs/monkey_zoo/blackbox/test_blackbox.py b/envs/monkey_zoo/blackbox/test_blackbox.py
index 0ed1e2a37..a9afd5244 100644
--- a/envs/monkey_zoo/blackbox/test_blackbox.py
+++ b/envs/monkey_zoo/blackbox/test_blackbox.py
@@ -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):
-        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 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 get_conf_file_path(conf_file_name):
+    return os.path.join(os.path.dirname(os.path.abspath(__file__)), "island_configs", conf_file_name)