diff --git a/secdep.py b/secdep.py index 217f462..bb197ef 100755 --- a/secdep.py +++ b/secdep.py @@ -104,8 +104,11 @@ if not os.path.exists(ENV_FILE): # 5) SECDEP_AZURE_SUB_ID # 6) SECDEP_AZURE_APP_ID # 7) SECDEP_AZURE_PASSWORD -# 8) SECDEP_AWS_ACCESS_KEY -# 9) SECDEP_AWS_SECRET_KEY +# 8) SECDEP_AZURE_RESOURCE_GROUP +# 9) SECDEP_AZURE_VIRTUAL_NETWORK +# 10 SECDEP_AZURE_STORAGE_ACCOUNT +# 11) SECDEP_AWS_ACCESS_KEY +# 12) SECDEP_AWS_SECRET_KEY # For GCE we need to create a service account (with Owner Role from the IAM section) and download the json file (from # the Service Account's manage keys section) in the same directory as the script @@ -148,6 +151,18 @@ with open(ENV_FILE, 'r') as f: SECDEP_AZURE_PASSWORD = input("Enter your AZURE_PASSWORD: ") with open(ENV_FILE, 'a') as f: f.write('SECDEP_AZURE_PASSWORD={}\n'.format(SECDEP_AZURE_PASSWORD)) + if 'SECDEP_AZURE_RESOURCE_GROUP' not in env_file_content: + SECDEP_AZURE_RESOURCE_GROUP = input("Enter your AZURE_RESOURCE_GROUP: ") + with open(ENV_FILE, 'a') as f: + f.write('SECDEP_AZURE_RESOURCE_GROUP={}\n'.format(SECDEP_AZURE_RESOURCE_GROUP)) + if 'SECDEP_AZURE_VIRTUAL_NETWORK' not in env_file_content: + SECDEP_AZURE_VIRTUAL_NETWORK = input("Enter your AZURE_VIRTUAL_NETWORK: ") + with open(ENV_FILE, 'a') as f: + f.write('SECDEP_AZURE_VIRTUAL_NETWORK={}\n'.format(SECDEP_AZURE_VIRTUAL_NETWORK)) + if 'SECDEP_AZURE_STORAGE_ACCOUNT' not in env_file_content: + SECDEP_AZURE_STORAGE_ACCOUNT = input("Enter your AZURE_STORAGE_ACCOUNT: ") + with open(ENV_FILE, 'a') as f: + f.write('SECDEP_AZURE_STORAGE_ACCOUNT={}\n'.format(SECDEP_AZURE_STORAGE_ACCOUNT)) if 'SECDEP_AWS_ACCESS_KEY' not in env_file_content: SECDEP_AWS_ACCESS_KEY = input("Enter your AWS_ACCESS_KEY: ") with open(ENV_FILE, 'a') as f: @@ -179,6 +194,9 @@ def get_env_vars(): global SECDEP_AZURE_SUB_ID global SECDEP_AZURE_APP_ID global SECDEP_AZURE_PASSWORD + global SECDEP_AZURE_RESOURCE_GROUP + global SECDEP_AZURE_VIRTUAL_NETWORK + global SECDEP_AZURE_STORAGE_ACCOUNT global SECDEP_AWS_ACCESS_KEY global SECDEP_AWS_SECRET_KEY # GCE @@ -194,6 +212,9 @@ def get_env_vars(): SECDEP_AZURE_SUB_ID = os.getenv('SECDEP_AZURE_SUB_ID') SECDEP_AZURE_APP_ID = os.getenv('SECDEP_AZURE_APP_ID') SECDEP_AZURE_PASSWORD = os.getenv('SECDEP_AZURE_PASSWORD') + SECDEP_AZURE_RESOURCE_GROUP = os.getenv('SECDEP_AZURE_RESOURCE_GROUP') + SECDEP_AZURE_VIRTUAL_NETWORK = os.getenv('SECDEP_AZURE_VIRTUAL_NETWORK') + SECDEP_AZURE_STORAGE_ACCOUNT = os.getenv('SECDEP_AZURE_STORAGE_ACCOUNT') # AWS SECDEP_AWS_ACCESS_KEY = os.getenv('SECDEP_AWS_ACCESS_KEY') SECDEP_AWS_SECRET_KEY = os.getenv('SECDEP_AWS_SECRET_KEY') @@ -911,7 +932,9 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi if provider == "gce": image = images[imageName.index(image)] elif provider == "azure": - image = image + driver = get_corresponding_driver(provider) + azlocation = driver.list_locations()[0] + image = driver.get_image(image,location=azlocation) else: # but in the case of aws we still need to get the region first region = getAWSRegionFromAmi(image) @@ -947,7 +970,21 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi if provider == "gce": node = driver.create_node(name=name, image=image, size=size, location=location, ex_service_accounts=sa_scopes, ex_metadata=metadata) elif provider == "azure": - node = driver.create_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group="secdep", ex_storage_account="secdep") + networks = driver.ex_list_networks() + for network in networks: + if network.name == SECDEP_AZURE_VIRTUAL_NETWORK: + ex_network = network + break + else: + print("Could not find the virtual network. Maybe it was not created beforehand?") + assert ex_network is not None, "In azure you need to manually go and create a virtual network before launching a virtual machine" + if location.id != ex_network.location: + print("Because libcloud currently has no method to automatically create resource group which is tied to a location, the given location must be the same as the one specified when you created the virtual network") + print('Current virtual network\'s location: %s' % (ex_network.location)) + exit(0) + subnet = driver.ex_list_subnets(network=ex_network)[0] + network_interface = driver.ex_create_network_interface(name=name, subnet=subnet, resource_group=SECDEP_AZURE_RESOURCE_GROUP, location=location) + node = driver.create_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group=SECDEP_AZURE_RESOURCE_GROUP, ex_storage_account=SECDEP_AZURE_STORAGE_ACCOUNT, ex_nic=network_interface, ex_os_disk_delete=True) else: keys = driver.list_key_pairs() for key in keys: @@ -981,7 +1018,21 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi if provider == "gce": node = driver.create_node(name=name, image=image, size=size, location=location, ex_service_accounts=sa_scopes, ex_metadata=metadata) elif provider == "azure": - node = driver.create_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group="secdep", ex_storage_account="secdep") + networks = driver.ex_list_networks() + for network in networks: + if network.name == SECDEP_AZURE_VIRTUAL_NETWORK: + ex_network = network + break + else: + print("Could not find the virtual network. Maybe it was not created beforehand?") + assert ex_network is not None, "In azure you need to manually go and create a virtual network before launching a virtual machine" + if location.id != ex_network.location: + print("Because libcloud currently has no method to automatically create resource group which is tied to a location, the given location must be the same as the one specified when you created the virtual network") + print('Current virtual network\'s location: %s' % (ex_network.location)) + exit(0) + subnet = driver.ex_list_subnets(network=ex_network)[0] + network_interface = driver.ex_create_network_interface(name=name, subnet=subnet, resource_group=SECDEP_AZURE_RESOURCE_GROUP, location=location) + node = driver.create_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group=SECDEP_AZURE_RESOURCE_GROUP, ex_storage_account=SECDEP_AZURE_STORAGE_ACCOUNT, ex_nic=network_interface, ex_os_disk_delete=True) else: keys = driver.list_key_pairs() for key in keys: