Skip to content

Commit

Permalink
notifier version and force mode added
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiri Otoupal committed Nov 2, 2023
1 parent df0bb58 commit 023ca1e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion abst/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"CLI Command making OCI Bastion and kubernetes usage simple and fast"
)

__version__ = "2.2.3"
__version__ = "2.2.4"
__author__ = "Jiri Otoupal"
__author_email__ = "jiri-otoupal@ips-database.eu"
__license__ = "MIT"
Expand Down
10 changes: 5 additions & 5 deletions abst/bastion_support/bastion_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from click import clear
from rich.align import Align

from abst.bastion_support.oci_bastion import Bastion
from abst.config import default_stack_location, default_stack_contents, \
default_contexts_location
from abst.bastion_support.oci_bastion import Bastion
from abst.wrappers import load_stack_decorator


Expand Down Expand Up @@ -99,18 +99,18 @@ def __display_loop(cls):
sleep(3)

@classmethod
def _run_indefinitely(cls, func):
def _run_indefinitely(cls, func, force: bool = False):
while True:
if cls.stopped:
return
try:
func()
func(force=force)
finally:
sleep(1)

@classmethod
@load_stack_decorator
def run(cls):
def run(cls, force=False):
signal.signal(signal.SIGINT, BastionScheduler.kill_all)
signal.signal(signal.SIGTERM, BastionScheduler.kill_all)
rich.print("Will run all Bastions in parallel")
Expand All @@ -123,7 +123,7 @@ def run(cls):
Bastion.load_json(Bastion.get_creds_path_resolve(context_name)).get("region", None))
cls.__live_stack.add(bastion)
t = Thread(name=context_name, target=cls._run_indefinitely,
args=[bastion.create_forward_loop], daemon=True)
args=[bastion.create_forward_loop, force], daemon=True)
thread_list.append(t)
t.start()
rich.print(f"Started {context_name}")
Expand Down
12 changes: 8 additions & 4 deletions abst/bastion_support/oci_bastion.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Bastion:
session_list = []
session_desc = dict()
custom_ssh_options: str = "-o ServerAliveInterval=20"
force_ssh_options: str = "-o StrictHostKeyChecking=no -o ServerAliveInterval=20 -o UserKnownHostsFile=/dev/null"

def __init__(self, context_name=None, region=None):
self.context_name = context_name
Expand Down Expand Up @@ -162,7 +163,7 @@ def create_managed_loop(self, shell: bool = False):
self.kill()

@mark_on_exit
def create_forward_loop(self, shell: bool = False):
def create_forward_loop(self, shell: bool = False, force: bool = False):

from abst.bastion_support.bastion_scheduler import BastionScheduler
if BastionScheduler.stopped:
Expand Down Expand Up @@ -203,7 +204,7 @@ def create_forward_loop(self, shell: bool = False):

ssh_tunnel_arg_str = self.run_ssh_tunnel_port_forward(bid, host, ip, port,
shell,
creds.get("local-port", 22), ssh_pub_key_path)
creds.get("local-port", 22), ssh_pub_key_path, force)

while status := (sdata := self.get_bastion_state())[
"lifecycle_state"] == "ACTIVE" and \
Expand Down Expand Up @@ -231,10 +232,13 @@ def run_ssh_tunnel_managed_session(self, bid, host, private_key_path, username,
exit_code = self.__run_ssh_tunnel_call(ssh_tunnel_args, shell, already_split=True)
return ssh_tunnel_args, exit_code

def run_ssh_tunnel_port_forward(self, bid, host, ip, port, shell, local_port, ssh_pub_key_path):
def run_ssh_tunnel_port_forward(self, bid, host, ip, port, shell, local_port, ssh_pub_key_path, force=False):
print(f"Bastion {self.get_print_name()} initialized")
print(f"Initializing SSH Tunnel for {self.get_print_name()}")
ssh_tunnel_arg_str = f"ssh {self.custom_ssh_options} -N -L {local_port}:{ip}:{port} -p 22 {bid}@{host} -vvv -i {ssh_pub_key_path.strip('.pub')}"
additional_args = "" if not force else self.force_ssh_options

ssh_tunnel_arg_str = (f"ssh {self.custom_ssh_options} -N -L {local_port}:{ip}:{port} -p 22 {bid}@{host} "
f"-vvv -i {ssh_pub_key_path.strip('.pub')} {additional_args}")
self.__run_ssh_tunnel(ssh_tunnel_arg_str, shell)
return ssh_tunnel_arg_str

Expand Down
12 changes: 8 additions & 4 deletions abst/cli_commands/parallel/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from InquirerPy import inquirer

from abst.bastion_support.bastion_scheduler import BastionScheduler
from abst.utils.misc_funcs import setup_calls
from abst.tools import display_scheduled
from abst.utils.misc_funcs import setup_calls


@click.group(help="Parallel Bastion Control group")
Expand Down Expand Up @@ -47,9 +47,13 @@ def remove(debug, context_name):

@parallel.command("run", help="Run All Bastions in fullauto")
@click.option("--debug", is_flag=True, default=False)
@click.option("-y", is_flag=True, default=False)
def run(debug, y):
@click.option("-y", is_flag=True, default=False, help="Automatically confirm")
@click.option("-f", "--force", is_flag=True, default=False, help="Will force connections ignoring security policies")
def run(debug, y, force):
setup_calls(debug)
if force:
rich.print("[red]Running in force mode[/red][gray] this mode is less secure as it is ignoring key checking and"
" security policies involving known_hosts[/gray]")
display_scheduled()
if not y:
try:
Expand All @@ -62,7 +66,7 @@ def run(debug, y):
if not confirm:
rich.print("[green]Cancelling, nothing started[/green]")
exit(0)
BastionScheduler.run()
BastionScheduler.run(force)


@parallel.command("display", help="Display current Bastions is stack")
Expand Down
6 changes: 6 additions & 0 deletions abst/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import click
import rich
from InquirerPy import inquirer
from requests import ConnectTimeout

from abst.__version__ import __version_name__, __version__
from abst.bastion_support.bastion_scheduler import BastionScheduler
Expand All @@ -16,6 +17,7 @@
from abst.cli_commands.kubectl_cli.commands import ssh_pod, log_pod
from abst.cli_commands.parallel.commands import parallel
from abst.config import default_creds_path, default_contexts_location, default_conf_path
from abst.notifier.version_notifier import Notifier
from abst.utils.misc_funcs import setup_calls


Expand Down Expand Up @@ -83,6 +85,10 @@ def clean():


def main():
try:
Notifier.notify()
except (ConnectionError, ConnectTimeout):
return False
Bastion.create_default_location()
cli()

Expand Down
6 changes: 0 additions & 6 deletions abst/utils/misc_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@
from typing import Optional

import rich
from requests import ConnectTimeout
from rich.logging import RichHandler

from abst.bastion_support.oci_bastion import Bastion
from abst.config import default_contexts_location
from abst.notifier.version_notifier import Notifier


def setup_calls(debug):
setup_debug(debug)
try:
Notifier.notify()
except (ConnectionError, ConnectTimeout):
return False


def setup_debug(debug):
Expand Down

0 comments on commit 023ca1e

Please sign in to comment.