Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: move setup_ssh() over to startup_blueos_update #3050

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions core/services/commander/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging
import os
import shutil
import subprocess
import time
from enum import Enum
from pathlib import Path
Expand Down Expand Up @@ -221,44 +220,7 @@ async def root() -> Any:
return HTMLResponse(content=html_content, status_code=200)


def setup_ssh() -> None:
# store the key in the docker .config volume
key_path = Path("/root/.config/.ssh")
private_key = key_path / "id_rsa"
public_key = private_key.with_suffix(".pub")
user = os.environ.get("SSH_USER", "pi")
gid = int(os.environ.get("USER_GID", 1000))
uid = int(os.environ.get("USER_UID", 1000))
authorized_keys = Path(f"/home/{user}/.ssh/authorized_keys")

try:
key_path.mkdir(parents=True, exist_ok=True)
# check if id_rsa.pub exists, creates a new one if it doesnt
if not public_key.is_file():
subprocess.run(["ssh-keygen", "-t", "rsa", "-f", private_key, "-q", "-N", ""], check=True)
public_key_text = public_key.read_text("utf-8")
# add id_rsa.pub to authorized_keys if not there already
try:
authorized_keys_text = authorized_keys.read_text("utf-8")
except FileNotFoundError:
logger.info(f"File does not exist: {authorized_keys}")
authorized_keys_text = ""

if public_key_text not in authorized_keys_text:
if not authorized_keys_text.endswith("\n"):
authorized_keys_text += "\n"
authorized_keys_text += public_key_text
authorized_keys.write_text(authorized_keys_text, "utf-8")

os.chown(authorized_keys, uid, gid)
authorized_keys.chmod(0o600)
except Exception as error:
logger.error(f"Error setting up ssh: {error}")
logger.info("SSH setup done")


if __name__ == "__main__":
setup_ssh()
# Register ssh client and remove message from the following commands
run_command("ls")

Expand Down
47 changes: 47 additions & 0 deletions core/tools/blueos_startup_update/blueos_startup_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import os
import re
import time
import subprocess
from pathlib import Path
from typing import List, Tuple
import configparser

Expand Down Expand Up @@ -504,6 +506,47 @@ def configure_network_manager() -> bool:
return True


def setup_ssh() -> bool:
logger.info("Setting up SSH...")
# store the key in the docker .config volume
key_path = Path("/root/.config/.ssh")
private_key = key_path / "id_rsa"
public_key = private_key.with_suffix(".pub")
user = os.environ.get("SSH_USER", "pi")
gid = int(os.environ.get("USER_GID", 1000))
uid = int(os.environ.get("USER_UID", 1000))
authorized_keys = Path(f"/home/{user}/.ssh/authorized_keys")

try:
key_path.mkdir(parents=True, exist_ok=True)
# check if id_rsa.pub exists, creates a new one if it doesnt
if not public_key.is_file():
subprocess.run(["ssh-keygen", "-t", "rsa", "-f", private_key, "-q", "-N", ""], check=True)
logger.info("Generated new SSH key pair")

public_key_text = public_key.read_text("utf-8")
# add id_rsa.pub to authorized_keys if not there already
try:
authorized_keys_text = authorized_keys.read_text("utf-8")
except FileNotFoundError:
logger.info(f"File does not exist: {authorized_keys}")
authorized_keys_text = ""

if public_key_text not in authorized_keys_text:
if not authorized_keys_text.endswith("\n"):
authorized_keys_text += "\n"
authorized_keys_text += public_key_text
authorized_keys.write_text(authorized_keys_text, "utf-8")
logger.info("Added public key to authorized_keys")

os.chown(authorized_keys, uid, gid)
authorized_keys.chmod(0o600)
return True
except Exception as sshError:
logger.error(f"Error setting up ssh: {sshError}")
return False


def main() -> int:
start = time.time()
# check if boot_loop_detector exists
Expand Down Expand Up @@ -578,6 +621,10 @@ def main() -> int:


if __name__ == "__main__":
try:
setup_ssh()
except Exception as error:
logger.error(f"An error occurred while setting up ssh: {error}")
try:
main()
if os.path.exists(BOOT_LOOP_DETECTOR):
Expand Down
Loading