it is hump day _^_

This commit is contained in:
2023-03-23 23:08:48 +02:00
parent 92a3bb437d
commit 1af330e862
3 changed files with 51 additions and 25 deletions

View File

@@ -43,3 +43,5 @@
`python3 secdep.py -P gce --ssh`
![Deletion 2](../../videos/demo/gce-instance-ssh.gif)
You can also specify a port with the `--port` flag.

View File

@@ -40,3 +40,12 @@ We can now use the tool by running `secdep` in the terminal.
For example `secdep -h` will show the help menu.
## Specific aws use case
When using aws as provider value, you can use the `--awsregion` flag to specify the region on which we operate. That decreases the speed of some actions because it no longer needs to go through all of the regions to check if there are any nodes there.
Example usage:
`secdep -P aws -l --awsregion us-east-2`
`secdep -P aws -a deleteall --awsregion us-east-2`

View File

@@ -70,6 +70,7 @@ parser.add_argument('-g', '--region', help='Region to use')
parser.add_argument('-y', '--yes', help='Do not ask for confirmation during creation', action='store_true')
parser.add_argument('-p', '--print', help='Also print node, image, location or size', action='store_true')
parser.add_argument('-port', '--port', help='Port to connect to when using ssh')
parser.add_argument('-awsregion', '--awsregion', help='Specify aws region to not have to go through all of them')
parser.add_argument('-ssh', '--ssh', help='Connect to an instance using ssh with the option to use -P PROVIDER to choose node from a specific provider', action='store_true')
args = parser.parse_args()
@@ -611,7 +612,7 @@ def list_provider_locations(provider):
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"]
for region in awsLocations:
awsdr = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY,region=region)
awsdr = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY, region=region)
specificAwsLocations = awsdr.list_locations()
for item in specificAwsLocations:
locations.append(item)
@@ -849,7 +850,7 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
region = getAWSRegionFromAmi(ami)
dr = get_corresponding_driver(provider)
assert dr is not None, "Driver is not set up correctly"
image = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY,region=region).get_image(ami)
image = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY, region=region).get_image(ami)
else:
blockPrint()
# If provider was aws we must get the list of amis
@@ -882,7 +883,7 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
region = getAWSRegionFromAmi(ami)
dr = get_corresponding_driver(provider)
assert dr is not None, "Driver is not set up correctly"
image = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY,region=region).get_image(ami)
image = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY, region=region).get_image(ami)
assert image is not None, "Image is None"
else:
# If image was indeed in the list we choose it
@@ -897,7 +898,7 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
region = getAWSRegionFromAmi(image)
dr = get_corresponding_driver(provider)
assert dr is not None, "Driver is not set up correctly"
image = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY,region=region).get_image(image)
image = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY, region=region).get_image(image)
# In the case of gce we need to give the sa_scopes and the ex_metadata parameters
if provider == "gce":
sa_scopes = [{"email": "default","scopes": ["cloud-platform"]}]
@@ -1172,7 +1173,7 @@ def create_node(provider, name=None, location=None, size=None, image=None, confi
print("If you ever change the port adjust the command accordingly")
return node
def list_all_nodes(provider, filterIn=None):
def list_all_nodes(provider, filterIn=None, awsRegion=None):
print("Getting all nodes...")
print("Loading 0%...")
nodes = []
@@ -1197,7 +1198,7 @@ def list_all_nodes(provider, filterIn=None):
print("Getting AWS nodes...")
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:
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)
# make it so it tries all drivers
awsNodes = driver3.list_nodes()
if len(awsNodes) > 0:
@@ -1225,10 +1226,17 @@ def list_all_nodes(provider, filterIn=None):
elif provider == "aws":
if SECDEP_AWS_ACCESS_KEY != "":
print("Getting AWS nodes...")
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:
driver3 = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY,region=region)
# make it so it tries all drivers
if awsRegion is None:
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:
driver3 = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY, region=region)
# make it so it tries all drivers
awsNodes = driver3.list_nodes()
if len(awsNodes) > 0:
for node in awsNodes:
nodes.append(node)
else:
driver3 = get_driver(Provider.EC2)(SECDEP_AWS_ACCESS_KEY, SECDEP_AWS_SECRET_KEY, region=awsRegion)
awsNodes = driver3.list_nodes()
if len(awsNodes) > 0:
for node in awsNodes:
@@ -1256,12 +1264,12 @@ def list_all_nodes(provider, filterIn=None):
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))
return nodes
def get_node(provider):
node = choose_from_list(list_all_nodes(provider), "node")
def get_node(provider, awsRegion=None):
node = choose_from_list(list_all_nodes(provider, None, awsRegion), "node")
return node
def node_action(action, provider):
node = choose_from_list(list_all_nodes(provider, action), "node")
def node_action(action, provider, awsRegion=None):
node = choose_from_list(list_all_nodes(provider, action, awsRegion), "node")
if node is None:
print("Nothing was chosen")
exit(0)
@@ -1279,7 +1287,7 @@ def node_action(action, provider):
driver = get_corresponding_driver("aws")
assert driver is not None, "Driver is not set up correctly"
region = getAWSRegionFromAmi(node.extra['image_id'])
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)
match action:
case "reboot":
succeded = driver.reboot_node(node)
@@ -1301,9 +1309,9 @@ def node_action(action, provider):
poller = resource_client.resource_groups.begin_delete(node_name+"-res_group")
result = poller.result()
def node_action_all(action, provider):
def node_action_all(action, provider, awsRegion=None):
string = action[:-3]
nodes = list_all_nodes(provider, string)
nodes = list_all_nodes(provider, string, awsRegion)
node_name = ""
for node in nodes:
providerName = node.name.split("-")[0]
@@ -1320,7 +1328,7 @@ def node_action_all(action, provider):
driver = get_corresponding_driver("aws")
assert driver is not None, "Driver is not set up correctly"
region = getAWSRegionFromAmi(node.extra['image_id'])
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)
match action:
case "rebootall":
succeded = driver.reboot_node(node)
@@ -1342,8 +1350,8 @@ def node_action_all(action, provider):
poller = resource_client.resource_groups.begin_delete(node_name+"-res_group")
result = poller.result()
def ssh(provider, port=None):
node = choose_from_list(list_all_nodes(provider,"stop"), "node")
def ssh(provider, port=None, awsRegion=None):
node = choose_from_list(list_all_nodes(provider,"stop",awsRegion), "node")
ip = node.public_ips[0]
if port is None:
port = 22
@@ -1374,6 +1382,9 @@ def ssh(provider, port=None):
break
ssh.close()
if args.awsregion and args.provider != "aws":
print("AWS region flag as the name suggests only goes with the aws provider")
exit(0)
# If -I -S or -G is passed, provider must be passed as well
if args.listimages or args.listsizes or args.listlocations:
assert args.provider is not None, "Provider must be passed if listing images, sizes or locations"
@@ -1408,22 +1419,26 @@ if args.create:
exit(0)
if args.list:
if args.print:
print(get_node(args.provider))
print(get_node(args.provider, args.awsregion))
else:
list_all_nodes(args.provider)
list_all_nodes(args.provider, None, args.awsregion)
exit(0)
# If args.action contains the word all execute the node_action_all function, otherwise the node_action function
if args.action:
if(args.action.endswith("all")):
node_action_all(args.action, args.provider)
node_action_all(args.action, args.provider, args.awsregion)
else:
node_action(args.action, args.provider)
node_action(args.action, args.provider, args.awsregion)
exit(0)
if args.ssh:
ssh(args.provider, args.port)
ssh(args.provider, args.port, args.awsregion)
exit(0)
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")
exit(0)
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")
exit(0)
if args.port and not args.ssh:
print("The port flag only goes with the ssh flag")
exit(0)