Skip to content

Commit

Permalink
🎉 Added a new feature to easily tunnel to specific host using ssm thr…
Browse files Browse the repository at this point in the history
…ough another host in AWS
  • Loading branch information
Alex Lubneuski committed Nov 11, 2024
1 parent 341c43d commit dd4d573
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions d3b_cli_igor/utils/scripts/tunnel-to-host
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

INSTANCE_ID=""

if [ -z $1 ]; then
echo "getting info"
info=$(aws ec2 describe-instances --region us-east-1 --query "Reservations[*].Instances[?Tags[?Key=='Name']|[?Value=='$ORG-infra-bastion-ssm-ec2-$syslevel-0']].[InstanceId,PrivateIpAddress]" --output text | tail -1)
INSTANCE_ID=$(echo "$info" | awk '{ print $1 }')
INSTANCE_IP=$(echo "$info" | awk '{ print $2 }')
if [ -z "$INSTANCE_ID" ]; then
echo "Could not find bastion host. Please contact DevOps"
exit 1
fi
else
INSTANCE_ID="${1}"
fi

aws ssm start-session --target "${INSTANCE_ID}" --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters '{"portNumber":["'$2'"],"localPortNumber":["'$3'"],"host":["'$4'"]}'
12 changes: 12 additions & 0 deletions d3b_cli_igor/utils/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
onboarding_script = "onboarding"
awslogin_script = "awslogin"
dev_env_tunnel_script = "dev-env-tunnel"
tunnel_to_host_script = "tunnel-to-host"

path = os.path.dirname(__file__)

Expand Down Expand Up @@ -52,5 +53,16 @@ def dev_env_tunnel(environment,cidr_block):
except botocore.exceptions.NoCredentialsError:
print("Credentials are NOT valid. You might want to execute : igor awslogin and export AWS_PROFILE=<profile_name> in order to set credentials.")

def tunnel_to_host(instance_id,port,local_port,host):
sts = boto3.client('sts')
try:
sts.get_caller_identity()
print("Credentials are valid.")
os.system(tunnel_to_host_script + " " + instance_id + " " + port + " " + local_port + " " + host)
except botocore.exceptions.ClientError:
print("Credentials are NOT valid. You might want to execute : igor awslogin and export AWS_PROFILE=<profile_name> in order to set credentials.")
except botocore.exceptions.NoCredentialsError:
print("Credentials are NOT valid. You might want to execute : igor awslogin and export AWS_PROFILE=<profile_name> in order to set credentials.")

def awslogin():
os.system(awslogin_script)
10 changes: 10 additions & 0 deletions igor
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ def dev_env_tunnel(environment,cidr_block):
check_creds()
d3b_cli_igor.utils.shortcuts.dev_env_tunnel(environment,cidr_block)

@click.command(name="tunnel-to-host")
@click.option("--instance_id", nargs=1, required=True)
@click.option("--port", nargs=1, default="", required=True)
@click.option("--local_port", nargs=1, default="", required=True)
@click.option("--host", nargs=1, default="", required=True)
def tunnel_to_host(instance_id,port,local_port,host):
check_creds()
d3b_cli_igor.utils.shortcuts.tunnel_to_host(instance_id,port,local_port,host)

@click.command(name="diff")
@click.option(
"--file1",
Expand Down Expand Up @@ -223,6 +232,7 @@ igor_cli.add_command(github_open)
igor_cli.add_command(onboarding)
igor_cli.add_command(awslogin)
igor_cli.add_command(dev_env_tunnel)
igor_cli.add_command(tunnel_to_host)
igor_cli.add_command(diff)
igor_cli.add_command(split_files)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"d3b_cli_igor/utils/scripts/check_build",
"d3b_cli_igor/utils/scripts/github_open",
"d3b_cli_igor/utils/scripts/dev-env-tunnel",
"d3b_cli_igor/utils/scripts/tunnel-to-host",
"d3b_cli_igor/utils/scripts/onboarding_devops_mac",
"d3b_cli_igor/utils/scripts/onboarding_devops_ubuntu",
"d3b_cli_igor/utils/scripts/onboarding_dev_mac",
Expand Down

0 comments on commit dd4d573

Please sign in to comment.