hey, what's that over there?!

This commit is contained in:
2023-03-28 19:00:50 +03:00
parent 857f5a2b05
commit a96829bd80
5 changed files with 198 additions and 152 deletions

View File

@@ -60,7 +60,7 @@ For AWS you firstly need an [account](https://aws.amazon.com/account/) and then
# Dependencies 📦 # Dependencies 📦
We would have 4 external dependecies but due to missing functionality in libcloud when it comes to Azure management we need 7. We would have 3 core external dependecies but due to missing functionality in libcloud when it comes to Azure management we need 6 of them. The rest are just quality of life dependecies 🏖️
1. [libcloud](https://github.com/apache/libcloud) which is the main dependency to connect to the cloud providers 1. [libcloud](https://github.com/apache/libcloud) which is the main dependency to connect to the cloud providers
2. [python-dotenv](https://github.com/theskumar/python-dotenv) which is used to read the `.env` file 2. [python-dotenv](https://github.com/theskumar/python-dotenv) which is used to read the `.env` file
@@ -70,6 +70,7 @@ We would have 4 external dependecies but due to missing functionality in libclou
2. [azure-mgmt-resource](https://pypi.org/project/azure-mgmt-resource/) to manage Azure resources 2. [azure-mgmt-resource](https://pypi.org/project/azure-mgmt-resource/) to manage Azure resources
3. [azure-mgmt-network](https://pypi.org/project/azure-mgmt-network/) to manage Azure networks 3. [azure-mgmt-network](https://pypi.org/project/azure-mgmt-network/) to manage Azure networks
5. [shtab](https://github.com/iterative/shtab) which is used to generate bash and zsh completion scripts 5. [shtab](https://github.com/iterative/shtab) which is used to generate bash and zsh completion scripts
6. [rich](https://github.com/Textualize/rich) which is used to make the output more colorful and easier to read
# Installation 🛠️ # Installation 🛠️

Binary file not shown.

After

Width:  |  Height:  |  Size: 238 KiB

View File

@@ -1,5 +1,11 @@
# Examples and output # Examples and output
### Actual outputs are more polished than the ones shown here (e.g. colors, better formatting, etc.)
One example of the modern output is shown bellow:
![Modern Output](../../images/demo/aws-instance-terminated.png)
## Instance listing 🪧 ## Instance listing 🪧
`python3 secdep.py -l` `python3 secdep.py -l`

View File

@@ -5,3 +5,4 @@ azure-mgmt-resource==22.0.0
paramiko==2.11.1 paramiko==2.11.1
python-dotenv==1.0.0 python-dotenv==1.0.0
shtab==1.5.8 shtab==1.5.8
rich==13.3.3

340
secdep.py
View File

@@ -25,6 +25,12 @@ import shtab
import time import time
import paramiko import paramiko
import socket import socket
from rich import pretty
from rich.console import Console
from rich.prompt import Prompt
from rich.prompt import Confirm
from rich.progress import track
from rich.status import Status
from dotenv import load_dotenv from dotenv import load_dotenv
from libcloud.compute.types import Provider from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver from libcloud.compute.providers import get_driver
@@ -34,6 +40,9 @@ from azure.identity import ClientSecretCredential
from azure.mgmt.resource import ResourceManagementClient from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.network import NetworkManagementClient from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.network.v2022_07_01.models import SecurityRule from azure.mgmt.network.v2022_07_01.models import SecurityRule
console = Console() # Better and more flexible print
prompt = Prompt() # Better and more flexible input
pretty.install()
# Declare the ENV_FILE variable as such to always reside in the same directory as the script # Declare the ENV_FILE variable as such to always reside in the same directory as the script
# We use os.path.join to make sure the path is correct for every OS # We use os.path.join to make sure the path is correct for every OS
@@ -79,7 +88,15 @@ parser.add_argument('-init', '--init', help='Initialize a specific provider\'s v
args = parser.parse_args() args = parser.parse_args()
def show_version(): def show_version():
print("SecDep - Automated secure docker services deployment\nVersion: v1.0.0\nRepo: https://git.konsthol.eu/konsthol/SecDep") console.print('''
____ ____
/ ___| ___ ___| _ \ ___ _ __
\___ \ / _ \/ __| | | |/ _ \ '_ \
___) | __/ (__| |_| | __/ |_) |
|____/ \___|\___|____/ \___| .__/
|_|
''',style="bold cyan")
console.print("[bold cyan]SecDep[/bold cyan] - Automated secure docker services deployment\n[bold cyan]Version[/bold cyan]: v1.0.0\n[bold cyan]Repo[/bold cyan]: [u]https://git.konsthol.eu/konsthol/SecDep[/u]", style="bold blue")
if args.version: if args.version:
show_version() show_version()
@@ -151,12 +168,12 @@ if os.stat(ENV_FILE).st_size == 0:
f.write('SECDEP_AZURE_APP_ID=\n') f.write('SECDEP_AZURE_APP_ID=\n')
f.write('SECDEP_AZURE_PASSWORD=\n') f.write('SECDEP_AZURE_PASSWORD=\n')
case _: case _:
print("Invalid provider") console.print("Invalid provider", style="bold red")
else: else:
print('You will be asked for each needed value\nIf you want to skip a provider press enter on each of their values because they are all needed for authentication\nIf at some point you delete the provider\'s value entry you will once again be asked to enter it\nIf you pressed enter by mistake or inserted an incorrect value just edit the file directly or delete the corresponding line\nThere is also the choice of using the -e option to have that done interactively') console.print('[bold white]You will be asked for each needed value\nIf you want to skip a provider press enter on each of their values because they are all needed for authentication\nIf at some point you delete the provider\'s value entry you will once again be asked to enter it\nIf you pressed enter by mistake or inserted an incorrect value just edit the file directly or delete the corresponding line\nThere is also the choice of using the [u]-e[/u] option to have that done interactively[/bold white]')
if os.stat(ENV_FILE).st_size != 0 and args.init: if os.stat(ENV_FILE).st_size != 0 and args.init:
print("The init flag was only meant to be optionally run once and only in the first run if you knew you were going to be using only one provider. If you need to change or populate a provider\'s needed values use the --edit or -e flag instead") console.print("[bold red]The init flag was only meant to be optionally run once and only in the first run if you knew you were going to be using only one provider.[/bold red] [bold white]If you need to change or populate a provider\'s needed values use the [u]--edit[/u] or [u]-e[/u] flag instead[/bold white]")
exit(0) exit(0)
# We search for these values in the ENV_FILE and for each not found, we prompt the user to enter it # We search for these values in the ENV_FILE and for each not found, we prompt the user to enter it
@@ -166,39 +183,39 @@ if os.stat(ENV_FILE).st_size != 0 and args.init:
with open(ENV_FILE, 'r') as f: with open(ENV_FILE, 'r') as f:
env_file_content = f.read() env_file_content = f.read()
if 'SECDEP_GCE_CLIENT_ID' not in env_file_content: if 'SECDEP_GCE_CLIENT_ID' not in env_file_content:
SECDEP_GCE_CLIENT_ID = input("Enter your GCE_CLIENT_ID: ") SECDEP_GCE_CLIENT_ID = prompt.ask("[bold white]Enter your [u]GCE_CLIENT_ID[/u] [/bold white]")
with open(ENV_FILE, 'a') as f: with open(ENV_FILE, 'a') as f:
f.write('SECDEP_GCE_CLIENT_ID={}\n'.format(SECDEP_GCE_CLIENT_ID)) f.write('SECDEP_GCE_CLIENT_ID={}\n'.format(SECDEP_GCE_CLIENT_ID))
if 'SECDEP_GCE_CLIENT_SECRET' not in env_file_content: if 'SECDEP_GCE_CLIENT_SECRET' not in env_file_content:
SECDEP_GCE_CLIENT_SECRET = input("Enter your GCE_CLIENT_SECRET: ") SECDEP_GCE_CLIENT_SECRET = prompt.ask("[bold white]Enter your [u]GCE_CLIENT_SECRET[/u] [/bold white]")
with open(ENV_FILE, 'a') as f: with open(ENV_FILE, 'a') as f:
f.write('SECDEP_GCE_CLIENT_SECRET={}\n'.format(SECDEP_GCE_CLIENT_SECRET)) f.write('SECDEP_GCE_CLIENT_SECRET={}\n'.format(SECDEP_GCE_CLIENT_SECRET))
if 'SECDEP_GCE_PROJECT_ID' not in env_file_content: if 'SECDEP_GCE_PROJECT_ID' not in env_file_content:
SECDEP_GCE_PROJECT_ID = input("Enter your GCE_PROJECT_ID: ") SECDEP_GCE_PROJECT_ID = prompt.ask("[bold white]Enter your [u]GCE_PROJECT_ID[/u] [/bold white]")
with open(ENV_FILE, 'a') as f: with open(ENV_FILE, 'a') as f:
f.write('SECDEP_GCE_PROJECT_ID={}\n'.format(SECDEP_GCE_PROJECT_ID)) f.write('SECDEP_GCE_PROJECT_ID={}\n'.format(SECDEP_GCE_PROJECT_ID))
if 'SECDEP_AZURE_TENANT_ID' not in env_file_content: if 'SECDEP_AZURE_TENANT_ID' not in env_file_content:
SECDEP_AZURE_TENANT_ID = input("Enter your AZURE_TENANT_ID: ") SECDEP_AZURE_TENANT_ID = prompt.ask("[bold white]Enter your [u]AZURE_TENANT_ID[/u] [/bold white]")
with open(ENV_FILE, 'a') as f: with open(ENV_FILE, 'a') as f:
f.write('SECDEP_AZURE_TENANT_ID={}\n'.format(SECDEP_AZURE_TENANT_ID)) f.write('SECDEP_AZURE_TENANT_ID={}\n'.format(SECDEP_AZURE_TENANT_ID))
if 'SECDEP_AZURE_SUB_ID' not in env_file_content: if 'SECDEP_AZURE_SUB_ID' not in env_file_content:
SECDEP_AZURE_SUB_ID = input("Enter your AZURE_SUB_ID: ") SECDEP_AZURE_SUB_ID = prompt.ask("[bold white]Enter your [u]AZURE_SUB_ID[/u] [/bold white]")
with open(ENV_FILE, 'a') as f: with open(ENV_FILE, 'a') as f:
f.write('SECDEP_AZURE_SUB_ID={}\n'.format(SECDEP_AZURE_SUB_ID)) f.write('SECDEP_AZURE_SUB_ID={}\n'.format(SECDEP_AZURE_SUB_ID))
if 'SECDEP_AZURE_APP_ID' not in env_file_content: if 'SECDEP_AZURE_APP_ID' not in env_file_content:
SECDEP_AZURE_APP_ID = input("Enter your AZURE_APP_ID: ") SECDEP_AZURE_APP_ID = prompt.ask("[bold white]Enter your [u]AZURE_APP_ID[/u] [/bold white]")
with open(ENV_FILE, 'a') as f: with open(ENV_FILE, 'a') as f:
f.write('SECDEP_AZURE_APP_ID={}\n'.format(SECDEP_AZURE_APP_ID)) f.write('SECDEP_AZURE_APP_ID={}\n'.format(SECDEP_AZURE_APP_ID))
if 'SECDEP_AZURE_PASSWORD' not in env_file_content: if 'SECDEP_AZURE_PASSWORD' not in env_file_content:
SECDEP_AZURE_PASSWORD = input("Enter your AZURE_PASSWORD: ") SECDEP_AZURE_PASSWORD = prompt.ask("[bold white]Enter your [u]AZURE_PASSWORD[/u] [/bold white]")
with open(ENV_FILE, 'a') as f: with open(ENV_FILE, 'a') as f:
f.write('SECDEP_AZURE_PASSWORD={}\n'.format(SECDEP_AZURE_PASSWORD)) f.write('SECDEP_AZURE_PASSWORD={}\n'.format(SECDEP_AZURE_PASSWORD))
if 'SECDEP_AWS_ACCESS_KEY' not in env_file_content: if 'SECDEP_AWS_ACCESS_KEY' not in env_file_content:
SECDEP_AWS_ACCESS_KEY = input("Enter your AWS_ACCESS_KEY: ") SECDEP_AWS_ACCESS_KEY = prompt.ask("[bold white]Enter your [u]AWS_ACCESS_KEY[/u] [/bold white]")
with open(ENV_FILE, 'a') as f: with open(ENV_FILE, 'a') as f:
f.write('SECDEP_AWS_ACCESS_KEY={}\n'.format(SECDEP_AWS_ACCESS_KEY)) f.write('SECDEP_AWS_ACCESS_KEY={}\n'.format(SECDEP_AWS_ACCESS_KEY))
if 'SECDEP_AWS_SECRET_KEY' not in env_file_content: if 'SECDEP_AWS_SECRET_KEY' not in env_file_content:
SECDEP_AWS_SECRET_KEY = input("Enter your AWS_SECRET_KEY: ") SECDEP_AWS_SECRET_KEY = prompt.ask("[bold white]Enter your [u]AWS_SECRET_KEY[/u] [/bold white]")
with open(ENV_FILE, 'a') as f: with open(ENV_FILE, 'a') as f:
f.write('SECDEP_AWS_SECRET_KEY={}\n'.format(SECDEP_AWS_SECRET_KEY)) f.write('SECDEP_AWS_SECRET_KEY={}\n'.format(SECDEP_AWS_SECRET_KEY))
@@ -257,9 +274,9 @@ def update_env_file():
for line in file_entries: for line in file_entries:
count += 1 count += 1
print("{}) {}".format(count, line)) print("{}) {}".format(count, line))
print("Choosing 0 will exit the function") console.print("Choosing [u]0[/u] will exit the function", style="bold white")
print("You will be asked to enter the new value until it is valid or you enter 0") console.print("You will be asked to enter the new value [u]until it is valid[/u] or you enter [u]0[/u]", style="bold white")
choice = input("Choose the entry you want to update: ") choice = prompt.ask("[bold white]Choose the entry you want to update [/bold white]")
try: try:
choice = int(choice) choice = int(choice)
if choice > count or choice < 0: if choice > count or choice < 0:
@@ -275,11 +292,11 @@ def update_env_file():
entry_value = entry.split('=')[1] entry_value = entry.split('=')[1]
if entry_value == '': if entry_value == '':
entry_value = 'None' entry_value = 'None'
print("The current value for {} is {}".format(entry_name, entry_value)) console.print("[bold white]The current value for {} is {}[/bold white]".format(entry_name, entry_value))
new_value = input("Enter the new value: ") new_value = prompt.ask("[bold white]Enter the new value [/bold white]")
with open(ENV_FILE, 'w') as f: with open(ENV_FILE, 'w') as f:
f.write(env_file_content.replace(entry, "{}={}".format(entry_name, new_value))) f.write(env_file_content.replace(entry, "{}={}".format(entry_name, new_value)))
print("The value for {} was updated successfully".format(entry_name)) console.print("[bold white]The value for {} was updated successfully[/bold white]".format(entry_name))
update_env_file() update_env_file()
# Reload the environment variables # Reload the environment variables
# That was setup this way because the initial thought was exiting manually but it will stay that way just in case we do end up making it like so # That was setup this way because the initial thought was exiting manually but it will stay that way just in case we do end up making it like so
@@ -574,21 +591,27 @@ global providers_quantity
def get_gce_driver(): def get_gce_driver():
if SECDEP_GCE_CLIENT_SECRET !="" and SECDEP_GCE_PROJECT_ID !="" and SECDEP_GCE_CLIENT_ID !="": if SECDEP_GCE_CLIENT_SECRET !="" and SECDEP_GCE_PROJECT_ID !="" and SECDEP_GCE_CLIENT_ID !="":
driver = get_driver(Provider.GCE) driver = get_driver(Provider.GCE)
print("Trying to authenticate with google...\n") console.print("Trying to authenticate with google...\n", style="bold white")
for step in track(range(1)):
pass
return driver(SECDEP_GCE_CLIENT_ID, SECDEP_GCE_CLIENT_SECRET, project=SECDEP_GCE_PROJECT_ID) return driver(SECDEP_GCE_CLIENT_ID, SECDEP_GCE_CLIENT_SECRET, project=SECDEP_GCE_PROJECT_ID)
# Get Azure driver # Get Azure driver
def get_azure_driver(): def get_azure_driver():
if SECDEP_AZURE_TENANT_ID !="" and SECDEP_AZURE_SUB_ID !="" and SECDEP_AZURE_APP_ID !="" and SECDEP_AZURE_PASSWORD !="": if SECDEP_AZURE_TENANT_ID !="" and SECDEP_AZURE_SUB_ID !="" and SECDEP_AZURE_APP_ID !="" and SECDEP_AZURE_PASSWORD !="":
driver = get_driver(Provider.AZURE_ARM) driver = get_driver(Provider.AZURE_ARM)
print("Trying to authenticate with azure...\n") console.print("Trying to authenticate with azure...\n", style="bold white")
for step in track(range(1)):
pass
return driver(tenant_id=SECDEP_AZURE_TENANT_ID, subscription_id=SECDEP_AZURE_SUB_ID, key=SECDEP_AZURE_APP_ID, secret=SECDEP_AZURE_PASSWORD) return driver(tenant_id=SECDEP_AZURE_TENANT_ID, subscription_id=SECDEP_AZURE_SUB_ID, key=SECDEP_AZURE_APP_ID, secret=SECDEP_AZURE_PASSWORD)
# Get AWS driver # Get AWS driver
def get_aws_driver(): def get_aws_driver():
if SECDEP_AWS_ACCESS_KEY !="" and SECDEP_AWS_SECRET_KEY !="": if SECDEP_AWS_ACCESS_KEY !="" and SECDEP_AWS_SECRET_KEY !="":
driver = get_driver(Provider.EC2) driver = get_driver(Provider.EC2)
print("Trying to authenticate with amazon...\n") console.print("Trying to authenticate with amazon...\n", style="bold white")
for step in track(range(1)):
pass
return driver(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY) return driver(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY)
# We need to know the quantity to print the loading percentage when getting the list of all the nodes # We need to know the quantity to print the loading percentage when getting the list of all the nodes
@@ -619,13 +642,13 @@ def get_corresponding_driver(provider):
case "aws": case "aws":
driver = aws_driver driver = aws_driver
case _: case _:
print("Invalid provider") console.print("Invalid provider", style="bold red")
assert driver is not None, "You need to set all {} environment variables first".format(provider.upper()) assert driver is not None, "You need to set all {} environment variables first".format(provider.upper())
return driver return driver
# This function takes a provider arguement and lists all the available sizes # This function takes a provider arguement and lists all the available sizes
def list_provider_sizes(provider): def list_provider_sizes(provider):
print("Getting "+provider+" sizes...") console.print("Getting "+provider+" sizes...", style="bold white")
driver = get_corresponding_driver(provider) driver = get_corresponding_driver(provider)
if provider == "aws" or provider == "gce": if provider == "aws" or provider == "gce":
sizes = driver.list_sizes(location=None) sizes = driver.list_sizes(location=None)
@@ -635,30 +658,31 @@ def list_provider_sizes(provider):
sizes = driver.list_sizes(location=azlocation) sizes = driver.list_sizes(location=azlocation)
sizes = [size for size in sizes if size.ram < 16384] sizes = [size for size in sizes if size.ram < 16384]
count = 0 count = 0
print("Available "+provider+" sizes") console.print("Available "+provider+" sizes", style="bold white")
if provider == "aws": if provider == "aws":
for size in sizes: for size in sizes:
count += 1 count += 1
print("{}) {}\n\nRam: {}\nDisk: {}\nBandwidth: {}\nPrice: {}\n".format(count, size.name, size.ram, size.disk, size.bandwidth, size.price)) console.print("[bold white]{}) {}\n\n[/bold white][bold blue]Ram: {}[/bold blue]\n[bold cyan]Disk: {}[/bold cyan]\n[bold magenta]Bandwidth: {}[/bold magenta]\n[bold yellow]Price: {}[/bold yellow]\n".format(count, size.name, size.ram, size.disk, size.bandwidth, size.price))
elif provider == "gce": elif provider == "gce":
for size in sizes: for size in sizes:
count += 1 count += 1
print("{}) {}\n\n{}\nPrice: {}\n".format(count, size.name, size.extra['description'], size.price)) console.print("[bold white]{}) {}\n\n[/bold white][italic white]{}[/italic white]\n[bold yellow]Price: {}[/bold yellow]\n".format(count, size.name, size.extra['description'], size.price))
else: else:
for size in sizes: for size in sizes:
count += 1 count += 1
print("{}) {}\n\nRam: {}\nDisk: {}\nPrice: {}\n".format(count, size.name, size.ram, size.disk, size.price)) console.print("[bold white]{}) {}\n\n[/bold white][bold blue]Ram: {}[/bold blue]\n[bold cyan]Disk: {}[/bold cyan]\n[bold yellow]Price: {}[/bold yellow]\n".format(count, size.name, size.ram, size.disk, size.price))
return sizes return sizes
# This function takes a provider arguement and lists all the available locations # This function takes a provider arguement and lists all the available locations
def list_provider_locations(provider): def list_provider_locations(provider):
print("Getting "+provider+" locations...") console.print("Getting "+provider+" locations...", style="bold white")
driver = get_corresponding_driver(provider) driver = get_corresponding_driver(provider)
locations = driver.list_locations() locations = driver.list_locations()
count = 0 count = 0
print("Available "+provider+" locations") console.print("Available "+provider+" locations", style="bold white")
if provider == "aws": if provider == "aws":
print("Still loading...") status = Status("[bold white]Still loading...[/bold white]", spinner="dots")
status.start()
locations = [] locations = []
awsLocations = ["ap-northeast-1", "ap-northeast-2", "ap-northeast-3", "ap-south-1", "ap-southeast-1", "ap-southeast-2", "ca-central-1", "eu-central-1", "eu-north-1", "eu-west-1", "eu-west-2", "eu-west-3", "sa-east-1", "us-east-1", "us-east-2", "us-west-1", "us-west-2"] awsLocations = ["ap-northeast-1", "ap-northeast-2", "ap-northeast-3", "ap-south-1", "ap-southeast-1", "ap-southeast-2", "ca-central-1", "eu-central-1", "eu-north-1", "eu-west-1", "eu-west-2", "eu-west-3", "sa-east-1", "us-east-1", "us-east-2", "us-west-1", "us-west-2"]
for region in awsLocations: for region in awsLocations:
@@ -666,45 +690,49 @@ def list_provider_locations(provider):
specificAwsLocations = awsdr.list_locations() specificAwsLocations = awsdr.list_locations()
for item in specificAwsLocations: for item in specificAwsLocations:
locations.append(item) locations.append(item)
status.stop()
for location in locations: for location in locations:
count += 1 count += 1
print("{}) {}\n\nRegion name: {}\nCountry: {}\n".format(count, location.name, location.availability_zone.region_name, location.country)) console.print("[bold white]{}) {}\n\n[/bold white][bold cyan]Region name: {}[/bold cyan]\n[bold blue]Country: {}[/bold blue]\n".format(count, location.name, location.availability_zone.region_name, location.country))
else: else:
for location in locations: for location in locations:
count += 1 count += 1
print("{}) {}\n\nCountry: {}\n".format(count, location.name, location.country)) console.print("[bold white]{}) {}\n\n[/bold white][bold blue]Country: {}\n[/bold blue]".format(count, location.name, location.country))
return locations return locations
def listAWSregions(list): def listAWSregions(list):
count = 0 count = 0
print("Available aws regions:") console.print("Available aws regions:", style="bold white")
for item in list: for item in list:
count += 1 count += 1
print("{}) {}".format(count, item)) console.print("[bold white]{}) {}[/bold white]".format(count, item))
return list return list
# This function lists all available images from the providers. # This function lists all available images from the providers.
def list_provider_images(provider,images=None): def list_provider_images(provider,images=None):
driver = get_corresponding_driver(provider) driver = get_corresponding_driver(provider)
print("Getting images from " +provider+"...") console.print("Getting images from " +provider+"...", style="bold white")
if provider == "azure": if provider == "azure":
images = AZURE_images images = AZURE_images
elif provider == "aws": elif provider == "aws":
images = AWS_images images = AWS_images
else: else:
status = Status("[bold white]Still loading...[/bold white]", spinner="dots")
status.start()
images = driver.list_images() images = driver.list_images()
status.stop()
# We filter out the images we do not care about # We filter out the images we do not care about
images = list(filter(lambda x: 'windows' not in x.name.lower() and 'cos' not in x.name.lower() and 'arm64' not in x.name.lower() and 'byos' not in x.name.lower() and 'sap' not in x.name.lower(), images)) images = list(filter(lambda x: 'windows' not in x.name.lower() and 'cos' not in x.name.lower() and 'arm64' not in x.name.lower() and 'byos' not in x.name.lower() and 'sap' not in x.name.lower(), images))
count = 0 count = 0
print("Available "+provider+" images") console.print("Available "+provider+" images", style="bold white")
if provider == "azure" or provider == "aws": if provider == "azure" or provider == "aws":
for image in images: for image in images:
count += 1 count += 1
print("{}) {}".format(count, image)) console.print("[bold white]{}) {}[/bold white]".format(count, image))
else: else:
for image in images: for image in images:
count += 1 count += 1
print("{}) {}\n\n{}\n".format(count, image.name, image.extra['description'])) console.print("[bold white]{}) {}\n\n[/bold white][italic white]{}[/italic white]\n".format(count, image.name, image.extra['description']))
return images return images
# This function gets called in every get function to create a menu for selection # This function gets called in every get function to create a menu for selection
@@ -715,33 +743,33 @@ def choose_from_list(listFromlistFunction,listName):
print("No items") print("No items")
exit(0) exit(0)
if listName == "awsLocation": if listName == "awsLocation":
printFormat = "{}) {}\n\nRegion name: {}\nCountry: {}\n" printFormat = "[bold white]{}) {}\n\n[/bold white][bold cyan]Region name: {}[/bold cyan]\n[bold blue]Country: {}[/bold blue]\n"
printstring = "print(printFormat.format(count, item.name, item.availability_zone.region_name, item.country))" printstring = "console.print(printFormat.format(count, item.name, item.availability_zone.region_name, item.country))"
elif listName == "azureLocation" or listName == "gceLocation": elif listName == "azureLocation" or listName == "gceLocation":
printFormat = "{}) {}\n\nCountry: {}\n" printFormat = "[bold white]{}) {}\n\n[/bold white][bold blue]Country: {}\n[/bold blue]"
printstring = "print(printFormat.format(count, item.name, item.country))" printstring = "console.print(printFormat.format(count, item.name, item.country))"
elif listName == "awsSize": elif listName == "awsSize":
printFormat = "{}) {}\n\nRam: {}\nDisk: {}\nBandwidth: {}\nPrice: {}\n" printFormat = "[bold white]{}) {}\n\n[/bold white][bold blue]Ram: {}[/bold blue]\n[bold cyan]Disk: {}[/bold cyan]\n[bold magenta]Bandwidth: {}[/bold magenta]\n[bold yellow]Price: {}[/bold yellow]\n"
printstring = "print(printFormat.format(count, item.name, item.ram, item.disk, item.bandwidth, item.price))" printstring = "console.print(printFormat.format(count, item.name, item.ram, item.disk, item.bandwidth, item.price))"
elif listName == "gceSize": elif listName == "gceSize":
printFormat = "{}) {}\n\n{}\nPrice: {}\n" printFormat = "[bold white]{}) {}\n\n[/bold white][italic white]{}[/italic white]\n[bold yellow]Price: {}[/bold yellow]\n"
printstring = "print(printFormat.format(count, item.name, item.extra['description'], item.price))" printstring = "console.print(printFormat.format(count, item.name, item.extra['description'], item.price))"
elif listName == "azureSize": elif listName == "azureSize":
printFormat = "{}) {}\n\nRam: {}\nDisk: {}\nPrice: {}\n" printFormat = "[bold white]{}) {}\n\n[/bold white][bold blue]Ram: {}[/bold blue]\n[bold cyan]Disk: {}[/bold cyan]\n[bold yellow]Price: {}[/bold yellow]\n"
printstring = "print(printFormat.format(count, item.name, item.ram, item.disk, item.price))" printstring = "console.print(printFormat.format(count, item.name, item.ram, item.disk, item.price))"
elif listName == "awsImage" or listName == "azureImage" or listName == "awsRegion" or listName == "aws_region": elif listName == "awsImage" or listName == "azureImage" or listName == "awsRegion" or listName == "aws_region":
printFormat = "{}) {}" printFormat = "[bold white]{}) {}[/bold white]"
printstring = "print(printFormat.format(count, item))" printstring = "console.print(printFormat.format(count, item))"
elif listName == "gceImage": elif listName == "gceImage":
printFormat = "{}) {}\n\n{}\n" printFormat = "[bold white]{}) {}\n\n[/bold white][italic white]{}[/italic white]\n"
printstring = "print(printFormat.format(count, item.name, item.extra['description']))" printstring = "console.print(printFormat.format(count, item.name, item.extra['description']))"
elif listName == "node": elif listName == "node":
printFormat = "{}) {}\n\nState: {}\nPublic IPs: {}\nPrivate IPs: {}\nDriver: {}\nSize: {}\nImage: {}\nCreation Date: {}\nExtra: {}\n" printFormat = "[bold white]{}) {}[/bold white]\n\n[bold cyan]State: {}[/bold cyan]\n[bold magenta]Public IPs: {}[/bold magenta]\n[bold blue]Private IPs: {}[/bold blue]\n[bold white]Driver: {}[/bold white]\n[bold cyan]Size: {}[/bold cyan]\n[bold magenta]Image: {}[/bold magenta]\n[bold blue]Creation Date: {}[/bold blue]\n[bold white]Extra: [/bold white]{}\n"
printstring = "print(printFormat.format(count, item.name, item.state, item.public_ips, item.private_ips, item.driver, item.size, item.image, item.created_at, item.extra))" printstring = "console.print(printFormat.format(count, item.name, item.state, item.public_ips, item.private_ips, item.driver, item.size, item.image, item.created_at, item.extra))"
compiled_code = compile(printstring,"<string>","exec") compiled_code = compile(printstring,"<string>","exec")
print("Choosing 0 will exit") console.print("Choosing 0 will exit", style="bold white")
choice = input("Choose the "+listName+" you want to use: ") choice = prompt.ask("[bold white]Choose the "+listName+" you want to use [/bold white]")
while True: while True:
try: try:
choice = int(choice) choice = int(choice)
@@ -769,9 +797,9 @@ def choose_from_list(listFromlistFunction,listName):
for item in listFromlistFunction: for item in listFromlistFunction:
count += 1 count += 1
exec(compiled_code) exec(compiled_code)
print("Invalid choice") console.print("[u]Invalid[/u] choice", style="bold red")
print("Choosing 0 will exit") console.print("Choosing 0 will exit", style="bold white")
choice = input("Choose the "+listName+" you want to use: ") choice = prompt.ask("[bold white]Choose the "+listName+" you want to use [/bold white]")
# This function gets a provider location and returns it # This function gets a provider location and returns it
def get_provider_location(provider): def get_provider_location(provider):
@@ -839,7 +867,7 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
auth = NodeAuthSSHKey(pubkey) auth = NodeAuthSSHKey(pubkey)
# Check if name was given and if not prompt the user to give one # Check if name was given and if not prompt the user to give one
if name is None: if name is None:
name = input("Enter the name of the node: ") name = prompt.ask("[bold white]Enter the name of the node [/bold white]")
assert name != "", "Name is empty" assert name != "", "Name is empty"
name = provider+"-"+name name = provider+"-"+name
else: else:
@@ -868,7 +896,7 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
locationName.append(loc.id) locationName.append(loc.id)
# If it was not found prompt the user for selection # If it was not found prompt the user for selection
if location not in locationName: if location not in locationName:
print("Invalid Location") console.print("[u]Invalid Location[/u]", style="bold red")
location = get_provider_location(provider) location = get_provider_location(provider)
assert location is not None, "Location is None" assert location is not None, "Location is None"
else: else:
@@ -886,7 +914,7 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
for siz in sizes: for siz in sizes:
sizeName.append(siz.name) sizeName.append(siz.name)
if size not in sizeName: if size not in sizeName:
print("Invalid Size") console.print("[u]Invalid[/u] Size", style="bold red")
size = get_provider_size(provider) size = get_provider_size(provider)
assert size is not None, "Size is None" assert size is not None, "Size is None"
else: else:
@@ -925,10 +953,11 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
for ami in amis: for ami in amis:
imageName.append(ami) imageName.append(ami)
if image not in imageName: if image not in imageName:
print("Invalid Image") console.print("[u]Invalid[/u] Image", style="bold red")
image = get_provider_image(provider) image = get_provider_image(provider)
# If the image given was not in the list of valid values we must get the actual image and the region from the ami to get the correct driver # If the image given was not in the list of valid values we must get the actual image and the region from the ami to get the correct driver
if provider == "aws": if provider == "aws":
assert image is not None, "Image is None"
ami = image.id ami = image.id
region = getAWSRegionFromAmi(ami) region = getAWSRegionFromAmi(ami)
dr = get_corresponding_driver(provider) dr = get_corresponding_driver(provider)
@@ -963,23 +992,23 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
driver = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY, region=region) driver = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY, region=region)
# If the user did not input the -y or --yes flag then we output the current choices for a second though # If the user did not input the -y or --yes flag then we output the current choices for a second though
if confirm is False: if confirm is False:
print("\nName: %s\n" % (name)) console.print("[bold white]\nName: %s\n[/bold white]" % (name))
# There is a differentiation between aws and the other providers in region and location # There is a differentiation between aws and the other providers in region and location
if provider == "aws": if provider == "aws":
print("\nLocation: %s\n" % (region)) console.print("[bold white]\nLocation: %s\n[/bold white]" % (region))
else: else:
print("\nLocation: %s\n" % (location)) console.print("[bold white]\nLocation: %s\n[/bold white]" % (location))
print("\nSize: %s\n" % (size)) console.print("[bold white]\nSize: [/bold white]%s\n" % (size))
print("\nImage: %s\n" % (image)) console.print("[bold white]\nImage: [/bold white]%s\n" % (image))
print("Type yes if you want to confirm your choices") console.print("Type yes if you want to confirm your choices", style="bold white")
confirm = input("Continue? ") confirm = Confirm.ask("[bold white]Continue? [/bold white]")
# Any input other than yes does not continue the node creation # Any input other than yes does not continue the node creation
assert confirm == "yes", "User did not confirm" assert confirm, "User did not confirm"
if provider == "gce": if provider == "gce":
gceNodes = driver.list_nodes() gceNodes = driver.list_nodes()
for gceNode in gceNodes: for gceNode in gceNodes:
if gceNode.name == name: if gceNode.name == name:
print("A node with that name already exists under this project, please choose another one") console.print("A node with that name already exists under this project, please choose [u]another[/u] one", style="bold red")
exit(0) exit(0)
existIn = False existIn = False
firewalls = driver.ex_list_firewalls() firewalls = driver.ex_list_firewalls()
@@ -992,17 +1021,17 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
if args.deploy: if args.deploy:
actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True) actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True)
node = driver.deploy_node(name=name, image=image, size=size, location=location, ex_service_accounts=sa_scopes, ex_metadata=metadata, deploy=actualDeployScript, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_username="secdep") node = driver.deploy_node(name=name, image=image, size=size, location=location, ex_service_accounts=sa_scopes, ex_metadata=metadata, deploy=actualDeployScript, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_username="secdep")
print('harden stdout: %s' % (actualDeployScript.stdout)) console.print('[bold white]harden stdout: %s[/bold white]' % (actualDeployScript.stdout))
print('harden stderr: %s' % (actualDeployScript.stderr)) console.print('[bold red]harden stderr: %s[/bold red]' % (actualDeployScript.stderr))
print('harden exit_code: %s' % (actualDeployScript.exit_status)) console. print('[bold white]harden exit_code: %s[/bold white]' % (actualDeployScript.exit_status))
else: else:
node = driver.create_node(name=name, image=image, size=size, location=location, ex_service_accounts=sa_scopes, ex_metadata=metadata) node = driver.create_node(name=name, image=image, size=size, location=location, ex_service_accounts=sa_scopes, ex_metadata=metadata)
elif provider == "azure": elif provider == "azure":
print("Keep in mind azure node creation may take a while because we need to create all the needed resources first") console.print("Keep in mind azure node creation may take a while because we need to create all the needed resources first", style="bold white")
res_groups = driver.ex_list_resource_groups() res_groups = driver.ex_list_resource_groups()
for res_group in res_groups: for res_group in res_groups:
if res_group.name == name+"-res_group": if res_group.name == name+"-res_group":
print("A resource group with that name already exists, please try a different virtual machine name to differentiate the resource group name") console.print("A resource group with that name already exists, please try a [u]different[/u] virtual machine name to differentiate the resource group name", style="bold red")
exit(0) exit(0)
credential = ClientSecretCredential(client_id=SECDEP_AZURE_APP_ID, client_secret=SECDEP_AZURE_PASSWORD, tenant_id=SECDEP_AZURE_TENANT_ID) credential = ClientSecretCredential(client_id=SECDEP_AZURE_APP_ID, client_secret=SECDEP_AZURE_PASSWORD, tenant_id=SECDEP_AZURE_TENANT_ID)
subscription_id = SECDEP_AZURE_SUB_ID subscription_id = SECDEP_AZURE_SUB_ID
@@ -1025,7 +1054,7 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
ex_network = network ex_network = network
break break
else: else:
print("Could not find the virtual network. Maybe it was not created correctly?") console.print("Could not find the virtual network. Maybe it was not created correctly?", style="bold red")
# Get Virtual Network's default subnet we created # Get Virtual Network's default subnet we created
subnet = driver.ex_list_subnets(network=ex_network)[0] subnet = driver.ex_list_subnets(network=ex_network)[0]
# Create public ip # Create public ip
@@ -1047,9 +1076,9 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
if args.deploy: if args.deploy:
actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True) actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True)
node = driver.deploy_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group=res_group.name, ex_use_managed_disks=True, ex_nic=newnic, ex_os_disk_delete=True, deploy=actualDeployScript, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_username="secdep") node = driver.deploy_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group=res_group.name, ex_use_managed_disks=True, ex_nic=newnic, ex_os_disk_delete=True, deploy=actualDeployScript, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_username="secdep")
print('harden stdout: %s' % (actualDeployScript.stdout)) console.print('[bold white]harden stdout: %s[/bold white]' % (actualDeployScript.stdout))
print('harden stderr: %s' % (actualDeployScript.stderr)) console.print('[bold red]harden stderr: %s[/bold red]' % (actualDeployScript.stderr))
print('harden exit_code: %s' % (actualDeployScript.exit_status)) console.print('[bold white]harden exit_code: %s[/bold white]' % (actualDeployScript.exit_status))
else: else:
node = driver.create_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group=res_group.name, ex_use_managed_disks=True, ex_nic=newnic, ex_os_disk_delete=True) node = driver.create_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group=res_group.name, ex_use_managed_disks=True, ex_nic=newnic, ex_os_disk_delete=True)
else: else:
@@ -1083,24 +1112,24 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True) actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True)
msd = MultiStepDeployment([deploy, actualDeployScript]) msd = MultiStepDeployment([deploy, actualDeployScript])
node = driver.deploy_node(name=name, image=image, size=size, ex_keyname=keyname, deploy=msd, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_alternate_usernames=["admin", "ec2-user", "centos", "fedora", "ubuntu"]) node = driver.deploy_node(name=name, image=image, size=size, ex_keyname=keyname, deploy=msd, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_alternate_usernames=["admin", "ec2-user", "centos", "fedora", "ubuntu"])
print('deploy stdout: %s' % (deploy.stdout)) console.print('[bold white]deploy stdout: %s[/bold white]' % (deploy.stdout))
print('deploy stderr: %s' % (deploy.stderr)) console.print('[bold red]deploy stderr: %s[/bold red]' % (deploy.stderr))
print('deploy exit_code: %s' % (deploy.exit_status)) console.print('[bold white]deploy exit_code: %s[/bold white]' % (deploy.exit_status))
print('harden stdout: %s' % (actualDeployScript.stdout)) console.print('[bold white]harden stdout: %s[/bold white]' % (actualDeployScript.stdout))
print('harden stderr: %s' % (actualDeployScript.stderr)) console.print('[bold red]harden stderr: %s[/bold red]' % (actualDeployScript.stderr))
print('harden exit_code: %s' % (actualDeployScript.exit_status)) console.print('[bold white]harden exit_code: %s[/bold white]' % (actualDeployScript.exit_status))
else: else:
node = driver.deploy_node(name=name, image=image, size=size, ex_keyname=keyname, deploy=deploy, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_alternate_usernames=["admin", "ec2-user", "centos", "fedora", "ubuntu"]) node = driver.deploy_node(name=name, image=image, size=size, ex_keyname=keyname, deploy=deploy, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_alternate_usernames=["admin", "ec2-user", "centos", "fedora", "ubuntu"])
print('deploy stdout: %s' % (deploy.stdout)) console.print('[bold white]deploy stdout: %s[/bold white]' % (deploy.stdout))
print('deploy stderr: %s' % (deploy.stderr)) console.print('[bold red]deploy stderr: %s[/bold red]' % (deploy.stderr))
print('deploy exit_code: %s' % (deploy.exit_status)) console.print('[bold white]deploy exit_code: %s[/bold white]' % (deploy.exit_status))
else: else:
# When the -y or --yes parameter is passed we go straight to the node creation # When the -y or --yes parameter is passed we go straight to the node creation
if provider == "gce": if provider == "gce":
gceNodes = driver.list_nodes() gceNodes = driver.list_nodes()
for gceNode in gceNodes: for gceNode in gceNodes:
if gceNode.name == name: if gceNode.name == name:
print("A node with that name already exists under this project, please choose another one") cosole.print("A node with that name already exists under this project, please choose [u]another[/u] one", style="bold red")
exit(0) exit(0)
existIn = False existIn = False
firewalls = driver.ex_list_firewalls() firewalls = driver.ex_list_firewalls()
@@ -1113,17 +1142,17 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
if args.deploy: if args.deploy:
actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True) actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True)
node = driver.deploy_node(name=name, image=image, size=size, location=location, ex_service_accounts=sa_scopes, ex_metadata=metadata, deploy=actualDeployScript, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_username="secdep") node = driver.deploy_node(name=name, image=image, size=size, location=location, ex_service_accounts=sa_scopes, ex_metadata=metadata, deploy=actualDeployScript, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_username="secdep")
print('harden stdout: %s' % (actualDeployScript.stdout)) console.print('[bold white]harden stdout: %s[/bold white]' % (actualDeployScript.stdout))
print('harden stderr: %s' % (actualDeployScript.stderr)) console.print('[bold red]harden stderr: %s[/bold red]' % (actualDeployScript.stderr))
print('harden exit_code: %s' % (actualDeployScript.exit_status)) console. print('[bold white]harden exit_code: %s[/bold white]' % (actualDeployScript.exit_status))
else: else:
node = driver.create_node(name=name, image=image, size=size, location=location, ex_service_accounts=sa_scopes, ex_metadata=metadata) node = driver.create_node(name=name, image=image, size=size, location=location, ex_service_accounts=sa_scopes, ex_metadata=metadata)
elif provider == "azure": elif provider == "azure":
print("Keep in mind azure node creation may take a while because we need to create all the needed resources first") console.print("Keep in mind azure node creation may take a while because we need to create all the needed resources first", style="bold white")
res_groups = driver.ex_list_resource_groups() res_groups = driver.ex_list_resource_groups()
for res_group in res_groups: for res_group in res_groups:
if res_group.name == name+"-res_group": if res_group.name == name+"-res_group":
print("A resource group with that name already exists, please try a different virtual machine name to differentiate the resource group name") console.print("A resource group with that name already exists, please try a [u]different[/u] virtual machine name to differentiate the resource group name", style="bold red")
exit(0) exit(0)
credential = ClientSecretCredential(client_id=SECDEP_AZURE_APP_ID, client_secret=SECDEP_AZURE_PASSWORD, tenant_id=SECDEP_AZURE_TENANT_ID) credential = ClientSecretCredential(client_id=SECDEP_AZURE_APP_ID, client_secret=SECDEP_AZURE_PASSWORD, tenant_id=SECDEP_AZURE_TENANT_ID)
subscription_id = SECDEP_AZURE_SUB_ID subscription_id = SECDEP_AZURE_SUB_ID
@@ -1146,7 +1175,7 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
ex_network = network ex_network = network
break break
else: else:
print("Could not find the virtual network. Maybe it was not created correctly?") console.print("Could not find the virtual network. Maybe it was not created correctly?", style="bold red")
# Get Virtual Network's default subnet we created # Get Virtual Network's default subnet we created
subnet = driver.ex_list_subnets(network=ex_network)[0] subnet = driver.ex_list_subnets(network=ex_network)[0]
# Create public ip # Create public ip
@@ -1168,9 +1197,9 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
if args.deploy: if args.deploy:
actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True) actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True)
node = driver.deploy_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group=res_group.name, ex_use_managed_disks=True, ex_nic=newnic, ex_os_disk_delete=True, deploy=actualDeployScript, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_username="secdep") node = driver.deploy_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group=res_group.name, ex_use_managed_disks=True, ex_nic=newnic, ex_os_disk_delete=True, deploy=actualDeployScript, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_username="secdep")
print('harden stdout: %s' % (actualDeployScript.stdout)) console.print('[bold white]harden stdout: %s[/bold white]' % (actualDeployScript.stdout))
print('harden stderr: %s' % (actualDeployScript.stderr)) console.print('[bold red]harden stderr: %s[/bold red]' % (actualDeployScript.stderr))
print('harden exit_code: %s' % (actualDeployScript.exit_status)) console.print('[bold white]harden exit_code: %s[/bold white]' % (actualDeployScript.exit_status))
else: else:
node = driver.create_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group=res_group.name, ex_use_managed_disks=True, ex_nic=newnic, ex_os_disk_delete=True) node = driver.create_node(name=name, size=size, image=image, location=location, auth=auth, ex_user_name="secdep", ex_resource_group=res_group.name, ex_use_managed_disks=True, ex_nic=newnic, ex_os_disk_delete=True)
else: else:
@@ -1204,56 +1233,58 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True) actualDeployScript = ScriptFileDeployment(script_file=SECDEP_DEPLOY_SCRIPT, args=args.deploy, name="harden", delete=True)
msd = MultiStepDeployment([deploy, actualDeployScript]) msd = MultiStepDeployment([deploy, actualDeployScript])
node = driver.deploy_node(name=name, image=image, size=size, ex_keyname=keyname, deploy=msd, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_alternate_usernames=["admin", "ec2-user", "centos", "fedora", "ubuntu"]) node = driver.deploy_node(name=name, image=image, size=size, ex_keyname=keyname, deploy=msd, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_alternate_usernames=["admin", "ec2-user", "centos", "fedora", "ubuntu"])
print('deploy stdout: %s' % (deploy.stdout)) console.print('[bold white]deploy stdout: %s[/bold white]' % (deploy.stdout))
print('deploy stderr: %s' % (deploy.stderr)) console.print('[bold red]deploy stderr: %s[/bold red]' % (deploy.stderr))
print('deploy exit_code: %s' % (deploy.exit_status)) console.print('[bold white]deploy exit_code: %s[/bold white]' % (deploy.exit_status))
print('harden stdout: %s' % (actualDeployScript.stdout)) console.print('[bold white]harden stdout: %s[/bold white]' % (actualDeployScript.stdout))
print('harden stderr: %s' % (actualDeployScript.stderr)) console.print('[bold red]harden stderr: %s[/bold red]' % (actualDeployScript.stderr))
print('harden exit_code: %s' % (actualDeployScript.exit_status)) console.print('[bold white]harden exit_code: %s[/bold white]' % (actualDeployScript.exit_status))
else: else:
node = driver.deploy_node(name=name, image=image, size=size, ex_keyname=keyname, deploy=deploy, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_alternate_usernames=["admin", "ec2-user", "centos", "fedora", "ubuntu"]) node = driver.deploy_node(name=name, image=image, size=size, ex_keyname=keyname, deploy=deploy, ssh_key=SECDEP_SSH_PRIVATE_KEY, ssh_alternate_usernames=["admin", "ec2-user", "centos", "fedora", "ubuntu"])
print('deploy stdout: %s' % (deploy.stdout)) console.print('[bold white]deploy stdout: %s[/bold white]' % (deploy.stdout))
print('deploy stderr: %s' % (deploy.stderr)) console.print('[bold red]deploy stderr: %s[/bold red]' % (deploy.stderr))
print('deploy exit_code: %s' % (deploy.exit_status)) console.print('[bold white]deploy exit_code: %s[/bold white]' % (deploy.exit_status))
print(node.name + " created successfully") console.print(node.name + " created successfully", style="bold white")
print("Node is initializing") console.print("Node is initializing, please wait...", style="bold white")
print("ip to connect to") console.print("ip to connect to", style="bold white")
print("\nIP: %s" % (node.public_ips[0])) console.print("[bold white]\nIP: %s[/bold white]" % (node.public_ips[0]))
print("ssh command:") console.print("[u]ssh command:[/u]", style="bold white")
if args.deploy: if args.deploy:
print("\nssh -p 22100 -i %s secdep@%s\n" % (SECDEP_SSH_PRIVATE_KEY, node.public_ips[0])) console.print("[bold white]\nssh -p 22100 -i %s secdep@%s\n[/bold white]" % (SECDEP_SSH_PRIVATE_KEY, node.public_ips[0]))
else: else:
print("\nssh -i %s secdep@%s\n" % (SECDEP_SSH_PRIVATE_KEY, node.public_ips[0])) console.print("[bold white]\nssh -i %s secdep@%s\n[/bold white]" % (SECDEP_SSH_PRIVATE_KEY, node.public_ips[0]))
return node return node
def list_all_nodes(provider, filterIn=None, awsRegion=None): def list_all_nodes(provider, filterIn=None, awsRegion=None):
print("Getting all nodes...") console.print("Getting all nodes...", style="bold white")
print("Loading 0%...") status = Status("[bold white]Please wait...[/bold white]", spinner="dots")
status.start()
console.print("Loading 0%...", style="bold white")
nodes = [] nodes = []
if provider is None: if provider is None:
if SECDEP_GCE_CLIENT_ID != "": if SECDEP_GCE_CLIENT_ID != "":
print("Getting GCE nodes...") console.print("Getting GCE nodes...", style="bold white")
driver = get_corresponding_driver("gce") driver = get_corresponding_driver("gce")
gceNodes = driver.list_nodes() gceNodes = driver.list_nodes()
if len(gceNodes) > 0: if len(gceNodes) > 0:
for node in gceNodes: for node in gceNodes:
nodes.append(node) nodes.append(node)
print("Loading %s%%..." % (int((1/providers_quantity)*100))) console.print("[bold white]Loading %s%%...[/bold white]" % (int((1/providers_quantity)*100)))
else: else:
print("Skipping gce") console.print("Skipping gce", style="bold red")
if SECDEP_AZURE_APP_ID != "": if SECDEP_AZURE_APP_ID != "":
print("Getting AZURE nodes...") console.print("Getting AZURE nodes...", style="bold white")
driver2 = get_corresponding_driver("azure") driver2 = get_corresponding_driver("azure")
azureNodes = driver2.list_nodes() azureNodes = driver2.list_nodes()
if len(azureNodes) > 0: if len(azureNodes) > 0:
for node in azureNodes: for node in azureNodes:
nodes.append(node) nodes.append(node)
print("Loading %s%%..." % (int((2/providers_quantity)*100))) console.print("[bold white]Loading %s%%...[/bold white]" % (int((2/providers_quantity)*100)))
else: else:
print("Skipping azure") console.print("Skipping azure", style="bold red")
if SECDEP_AWS_ACCESS_KEY != "": if SECDEP_AWS_ACCESS_KEY != "":
driver3 = get_corresponding_driver("aws") driver3 = get_corresponding_driver("aws")
print("Getting AWS nodes...") console.print("Getting AWS nodes...", style="bold white")
awsLocations = ["ap-northeast-1", "ap-northeast-2", "ap-northeast-3", "ap-south-1", "ap-southeast-1", "ap-southeast-2", "ca-central-1", "eu-central-1", "eu-north-1", "eu-west-1", "eu-west-2", "eu-west-3", "sa-east-1", "us-east-1", "us-east-2", "us-west-1", "us-west-2"] awsLocations = ["ap-northeast-1", "ap-northeast-2", "ap-northeast-3", "ap-south-1", "ap-southeast-1", "ap-southeast-2", "ca-central-1", "eu-central-1", "eu-north-1", "eu-west-1", "eu-west-2", "eu-west-3", "sa-east-1", "us-east-1", "us-east-2", "us-west-1", "us-west-2"]
for region in awsLocations: for region in awsLocations:
driver3 = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY, region=region) driver3 = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY, region=region)
@@ -1262,35 +1293,38 @@ def list_all_nodes(provider, filterIn=None, awsRegion=None):
if len(awsNodes) > 0: if len(awsNodes) > 0:
for node in awsNodes: for node in awsNodes:
nodes.append(node) nodes.append(node)
print("Loading %s%%..." % (int((3/providers_quantity)*100))) console.print("[bold white]Loading %s%%...[/bold white]" % (int((3/providers_quantity)*100)))
else: else:
print("Skipping aws") console.print("Skipping aws", style="bold red")
status.stop()
elif provider == "gce": elif provider == "gce":
if SECDEP_GCE_CLIENT_ID != "": if SECDEP_GCE_CLIENT_ID != "":
print("Getting GCE nodes...") console.print("Getting GCE nodes...", style="bold white")
driver = get_corresponding_driver("gce") driver = get_corresponding_driver("gce")
gceNodes = driver.list_nodes() gceNodes = driver.list_nodes()
if len(gceNodes) > 0: if len(gceNodes) > 0:
for node in gceNodes: for node in gceNodes:
nodes.append(node) nodes.append(node)
print("Loading %s%%..." % (int((1/providers_quantity)*100))) console.print("[bold white]Loading %s%%...[/bold white]" % (int((1/providers_quantity)*100)))
else: else:
print("Skipping gce") console.print("Skipping gce", style="bold red")
status.stop()
elif provider == "azure": elif provider == "azure":
if SECDEP_AZURE_APP_ID != "": if SECDEP_AZURE_APP_ID != "":
print("Getting AZURE nodes...") console.print("Getting AZURE nodes...", style="bold white")
driver2 = get_corresponding_driver("azure") driver2 = get_corresponding_driver("azure")
azureNodes = driver2.list_nodes() azureNodes = driver2.list_nodes()
if len(azureNodes) > 0: if len(azureNodes) > 0:
for node in azureNodes: for node in azureNodes:
nodes.append(node) nodes.append(node)
print("Loading %s%%..." % (int((2/providers_quantity)*100))) console.print("[bold white]Loading %s%%...[/bold white]" % (int((2/providers_quantity)*100)))
else: else:
print("Skipping azure") console.print("Skipping azure", style="bold red")
status.stop()
elif provider == "aws": elif provider == "aws":
if SECDEP_AWS_ACCESS_KEY != "": if SECDEP_AWS_ACCESS_KEY != "":
driver3 = get_corresponding_driver("aws") driver3 = get_corresponding_driver("aws")
print("Getting AWS nodes...") console.print("Getting AWS nodes...", style="bold white")
awsLocations = ["ap-northeast-1", "ap-northeast-2", "ap-northeast-3", "ap-south-1", "ap-southeast-1", "ap-southeast-2", "ca-central-1", "eu-central-1", "eu-north-1", "eu-west-1", "eu-west-2", "eu-west-3", "sa-east-1", "us-east-1", "us-east-2", "us-west-1", "us-west-2"] awsLocations = ["ap-northeast-1", "ap-northeast-2", "ap-northeast-3", "ap-south-1", "ap-southeast-1", "ap-southeast-2", "ca-central-1", "eu-central-1", "eu-north-1", "eu-west-1", "eu-west-2", "eu-west-3", "sa-east-1", "us-east-1", "us-east-2", "us-west-1", "us-west-2"]
if awsRegion is None: if awsRegion is None:
for region in awsLocations: for region in awsLocations:
@@ -1310,12 +1344,13 @@ def list_all_nodes(provider, filterIn=None, awsRegion=None):
if len(awsNodes) > 0: if len(awsNodes) > 0:
for node in awsNodes: for node in awsNodes:
nodes.append(node) nodes.append(node)
print("Loading %s%%..." % (int((3/providers_quantity)*100))) console.print("[bold white]Loading %s%%...[/bold white]" % (int((3/providers_quantity)*100)))
else: else:
print("Skipping aws") console.print("Skipping aws", style="bold red")
status.stop()
count = 0 count = 0
if len(nodes) == 0: if len(nodes) == 0:
print("No nodes") console.print("No nodes", style="bold white")
exit(0) exit(0)
# available states: running, rebooting, terminated, pending, stopped, suspended, paused, erro, unknown # available states: running, rebooting, terminated, pending, stopped, suspended, paused, erro, unknown
# for delete # for delete
@@ -1332,7 +1367,7 @@ def list_all_nodes(provider, filterIn=None, awsRegion=None):
nodes = list(filter(lambda x: 'running' in x.state.lower() or 'suspended' in x.state.lower() or 'paused' in x.state.lower(), nodes)) nodes = list(filter(lambda x: 'running' in x.state.lower() or 'suspended' in x.state.lower() or 'paused' in x.state.lower(), nodes))
for node in nodes: for node in nodes:
count += 1 count += 1
print("{}) {}\n\nState: {}\nPublic IPs: {}\nPrivate IPs: {}\nDriver: {}\nSize: {}\nImage: {}\nCreation Date: {}\nExtra: {}\n".format(count, node.name, node.state, node.public_ips, node.private_ips, node.driver, node.size, node.image, node.created_at, node.extra)) console.print("[bold white]{}) {}[/bold white]\n\n[bold cyan]State: {}[/bold cyan]\n[bold magenta]Public IPs: {}[/bold magenta]\n[bold blue]Private IPs: {}[/bold blue]\n[bold white]Driver: {}[/bold white]\n[bold cyan]Size: {}[/bold cyan]\n[bold magenta]Image: {}[/bold magenta]\n[bold blue]Creation Date: {}[/bold blue]\n[bold white]Extra: [/bold white]{}\n".format(count, node.name, node.state, node.public_ips, node.private_ips, node.driver, node.size, node.image, node.created_at, node.extra))
return nodes return nodes
def get_node(provider, awsRegion=None): def get_node(provider, awsRegion=None):
@@ -1342,7 +1377,7 @@ def get_node(provider, awsRegion=None):
def node_action(action, provider, awsRegion=None): def node_action(action, provider, awsRegion=None):
node = choose_from_list(list_all_nodes(provider, action, awsRegion), "node") node = choose_from_list(list_all_nodes(provider, action, awsRegion), "node")
if node is None: if node is None:
print("Nothing was chosen") console.print("Nothing was chosen", style="bold white")
exit(0) exit(0)
providerName = node.name.split("-")[0] providerName = node.name.split("-")[0]
if providerName == "gce": if providerName == "gce":
@@ -1372,11 +1407,11 @@ def node_action(action, provider, awsRegion=None):
print("Invalid action command") print("Invalid action command")
exit(0) exit(0)
if(succeded): if(succeded):
print("%s node %s -> successful" % (providerName.upper(), action)) console.print("[bold white]%s node %s -> successful[/bold white]" % (providerName.upper(), action))
else: else:
print("%s node %s -> failed" % (providerName.upper(), action)) console.print("[bold red]%s node %s -> failed[/bold red]" % (providerName.upper(), action))
if providerName == "azure" and action == "delete": if providerName == "azure" and action == "delete":
print("Deleting the corresponding resource group may take a while") console.print("Deleting the corresponding resource group may take a while", style="bold white")
poller = resource_client.resource_groups.begin_delete(node_name+"-res_group") poller = resource_client.resource_groups.begin_delete(node_name+"-res_group")
result = poller.result() result = poller.result()
@@ -1413,11 +1448,11 @@ def node_action_all(action, provider, awsRegion=None):
print("Invalid action command") print("Invalid action command")
exit(0) exit(0)
if(succeded): if(succeded):
print("%s node %s -> successful" % (node.name, string)) console.print("[bold white]%s node %s -> successful[/bold white]" % (node.name, string))
else: else:
print("%s node %s -> failed" % (node.name, string)) console.print("[bold red]%s node %s -> failed[/bold red]" % (node.name, string))
if providerName == "azure" and action == "delete": if providerName == "azure" and action == "delete":
print("Deleting the corresponding resource group may take a while") console.print("Deleting the corresponding resource group may take a while", style="bold white")
poller = resource_client.resource_groups.begin_delete(node_name+"-res_group") poller = resource_client.resource_groups.begin_delete(node_name+"-res_group")
result = poller.result() result = poller.result()
@@ -1440,7 +1475,7 @@ def ssh(provider, port=None, awsRegion=None):
channel.get_pty() channel.get_pty()
channel.invoke_shell() channel.invoke_shell()
while True: while True:
command = input('$> ') command = prompt.ask('[bold white]$> [/bold white]')
if command == 'exit': break if command == 'exit': break
channel.send((command + "\n").encode()) channel.send((command + "\n").encode())
while True: while True:
@@ -1453,8 +1488,11 @@ def ssh(provider, port=None, awsRegion=None):
break break
ssh.close() ssh.close()
if args.list and args.ssh or args.action and args.ssh:
console.print("No need for [u]--list[/u] when using [u]--ssh[/u] or [u]--action[/u]", style="bold red")
exit(0)
if args.awsregion and args.provider != "aws": if args.awsregion and args.provider != "aws":
print("AWS region flag as the name suggests only goes with the aws provider") console.print("AWS region flag as the name suggests only goes with the aws provider", style="bold red")
exit(0) exit(0)
# If -I -S or -G is passed, provider must be passed as well # If -I -S or -G is passed, provider must be passed as well
if args.listimages or args.listsizes or args.listlocations: if args.listimages or args.listsizes or args.listlocations:
@@ -1502,11 +1540,11 @@ if args.ssh:
ssh(args.provider, args.port, args.awsregion) ssh(args.provider, args.port, args.awsregion)
exit(0) exit(0)
if args.image or args.size or args.name or args.region or args.yes or args.deploy and not args.create: if args.image or args.size or args.name or args.region or args.yes or args.deploy and not args.create:
print("Image, size, name, region, yes and deploy parameters only go along with the create flag") console.print("Image, size, name, region, yes and deploy parameters [u]only[/u] go along with the create flag", style="bold red")
exit(0) exit(0)
if args.print and not args.list or args.listimages or args.listsizes or args.listlocations: if args.print and not args.list or args.listimages or args.listsizes or args.listlocations:
print("The print flag only goes together with the list, list images, list sizes or list locations") console.print("The print flag [u]only[/u] goes together with the list, list images, list sizes or list locations", style="bold red")
exit(0) exit(0)
if args.port and not args.ssh: if args.port and not args.ssh:
print("The port flag only goes with the ssh flag") console.print("The port flag [u]only[/u] goes with the ssh flag", style="bold red")
exit(0) exit(0)