def _generate_cert(name: str, org: str, duration: int, size: int, show: bool, get: bool): """Generate SSL certificate keypair.""" from hyperglass_agent.cli.actions import write_cert, find_app_path if get: app_path = find_app_path() cert_path = app_path / "agent_cert.pem" if not cert_path.exists(): warning("Certificate & key files have not been generated.") do_gen = confirm( style("Would you like to generate them now?", **WARNING)) if not do_gen: error("Certificate & key files do not yet exist.") else: write_cert(name=name, org=org, duration=duration, starttime=CERT_START, size=size, show=show) else: with cert_path.open("r") as f: cert = f.read() label(f"Public Key:\n\n{cert}") else: write_cert(name=name, org=org, duration=duration, starttime=CERT_START, size=size, show=show)
def gen_cert(name, org, duration, size, show, get): """Generate SSL certificate keypair. Arguments: name {str} -- Common Name org {str} -- Organization duration -- Validity in years size {int} -- Key Size show {bool} -- Show private key in CLI """ from hyperglass_agent.cli.actions import write_cert, find_app_path if get: app_path = find_app_path() cert_path = app_path / "agent_cert.pem" if not cert_path.exists(): warning("Certificate & key files have not been generated.") do_gen = confirm( style("Would you like to generate them now?", **WARNING)) if not do_gen: error("Certificate & key files do not yet exist.") else: write_cert(name=name, org=org, duration=duration, size=size, show=show) else: with cert_path.open("r") as f: cert = f.read() label(f"Public Key:\n\n{cert}") else: write_cert(name=name, org=org, duration=duration, size=size, show=show)
def send_cert(): """Send this device's public key to hyperglass.""" import platform from hyperglass_agent.config import params from hyperglass_agent.util import send_public_key from pydantic import AnyHttpUrl, create_model, ValidationError app_path = find_app_path() cert_file = app_path / "agent_cert.pem" if params.ssl is not None and not params.ssl.enable: confirm( "SSL is disabled. Proceed with sending certificate to hyperglass?", default=False, abort=True, ) if not cert_file.exists(): error("File {f} does not exist", f=cert_file) with cert_file.open("r") as f: cert = f.read().strip() _hg_url = prompt("Enter hyperglass URL (e.g. https://lg.example.com)", type=str) url_model = create_model("UrlModel", url=(AnyHttpUrl, ...)) try: hg_url = url_model(url=_hg_url) except ValidationError as ve: msg = ve.errors()[0]["msg"] warning("URL {u} is invalid: {e}", u=_hg_url, e=msg) _hg_url = prompt("Enter hyperglass URL (e.g. https://lg.example.com)", type=str) try: hg_url = url_model(url=_hg_url) except ValidationError as ve: msg = ve.errors()[0]["msg"] error("URL {u} is invalid: {e}", u=_hg_url, e=msg) device_name = platform.node() keep_discovered = confirm( f"This device's hostname appears to be '{device_name}'. " + "Does this match this device's definition in hyperglass?") if not keep_discovered: device_name = prompt( "Enter the device's name as configured in hyperglass") try: status = send_public_key(str(hg_url.url), device_name=device_name, certificate=cert, params=params) success(status) except RuntimeError as re: error(str(re))
def make_systemd(): """Generate a systemd file based on the local system. Arguments: user {str} -- User hyperglass-agent needs to be run as Returns: {str} -- Generated systemd template """ from shutil import which from getpass import getuser template = """ [Unit] Description=hyperglass-agent After=network.target [Service] User={user} Group={group} ExecStart={bin_path} start [Install] WantedBy=multi-user.target """ app_path = find_app_path() service_path = app_path / "hyperglass-agent.service" cmd_path = which("hyperglass-agent") if not cmd_path: bin_path = "python3 -m hyperglass_agent.console" warning("hyperglass executable not found, using {h}", h=bin_path) else: bin_path = cmd_path if app_path == Path.home(): user = getuser() else: user = "******" systemd = template.format(user=user, group=user, bin_path=bin_path) info(f"Generated systemd service:\n{systemd}") if service_path.exists(): service_path.unlink() with service_path.open("w") as f: f.write(systemd) if not service_path.exists(): error("Error writing systemd file to {f}", f=service_path) install_systemd(service_path) return True
def find_app_path(): """Try to find the app_path, prompt user to set one if it is not found.""" import os import inquirer from hyperglass_agent.util import set_app_path from hyperglass_agent.constants import APP_PATHS try: set_app_path(required=True) app_path = Path(os.environ["hyperglass_agent_directory"]) except RuntimeError: warning( "None of the supported paths for hyperglass-agent were found.\n" + "Checked:\n{one}\n{two}", one=APP_PATHS[0], two=APP_PATHS[1], ) create = confirm(style("Would you like to create one?", **WARNING)) if not create: error("hyperglass-agent requires an application path, " + "but you've chosen not to create one.") elif create: available_paths = [ inquirer.List( "selected", message="Choose a directory for hyperglass-agent", choices=APP_PATHS, ) ] answer = inquirer.prompt(available_paths) if answer is None: error("A directory for hyperglass-agent is required") selected = answer["selected"] if not selected.exists(): create_dir(selected) app_path = selected return app_path