Skip to content

Commit

Permalink
Core: allow getting ssh user and password from environment variables
Browse files Browse the repository at this point in the history
Co-authored-by: João Antônio Cardoso <[email protected]>
  • Loading branch information
Williangalvani and joaoantoniocardoso committed Nov 18, 2024
1 parent f776c91 commit ae544c8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
13 changes: 7 additions & 6 deletions core/libs/commonwealth/commonwealth/utils/commands.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import subprocess
from pathlib import Path
from typing import List, Optional
Expand All @@ -12,8 +13,8 @@ class KeyNotFound(Exception):
def run_command_with_password(command: str, check: bool = True) -> "subprocess.CompletedProcess['str']":
# attempt to run the command with sshpass
# used as a fallback if the ssh key is not found
user = "pi"
password = "raspberry"
user = os.environ.get("SSH_USER", "pi")
password = os.environ.get("SSH_PASSWORD", "raspberry")

return subprocess.run(
[
Expand All @@ -35,7 +36,7 @@ def run_command_with_password(command: str, check: bool = True) -> "subprocess.C

def run_command_with_ssh_key(command: str, check: bool = True) -> "subprocess.CompletedProcess['str']":
# attempt to run the command with the ssh key
user = "pi"
user = os.environ.get("SSH_USER", "pi")
id_file = "/root/.config/.ssh/id_rsa"
if not Path(id_file).exists():
raise KeyNotFound
Expand Down Expand Up @@ -84,8 +85,8 @@ def upload_file_with_password(
) -> "subprocess.CompletedProcess['str']":
# attempt to upload the file with sshpass
# used as a fallback if the ssh key is not found
user = "pi"
password = "raspberry"
user = os.environ.get("SSH_USER", "pi")
password = os.environ.get("SSH_PASSWORD", "raspberry")

return subprocess.run(
[
Expand All @@ -105,7 +106,7 @@ def upload_file_with_password(

def upload_file_with_ssh_key(source: str, destination: str, check: bool = True) -> "subprocess.CompletedProcess['str']":
# attempt to upload the file with the ssh key
user = "pi"
user = os.environ.get("SSH_USER", "pi")
id_file = "/root/.config/.ssh/id_rsa"
if not Path(id_file).exists():
raise KeyNotFound
Expand Down
7 changes: 5 additions & 2 deletions core/services/commander/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ def setup_ssh() -> None:
key_path = Path("/root/.config/.ssh")
private_key = key_path / "id_rsa"
public_key = private_key.with_suffix(".pub")
authorized_keys = Path("/home/pi/.ssh/authorized_keys")
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)
Expand All @@ -247,7 +250,7 @@ def setup_ssh() -> None:
authorized_keys_text += public_key_text
authorized_keys.write_text(authorized_keys_text, "utf-8")

shutil.chown(authorized_keys, "pi", "pi")
os.chown(authorized_keys, uid, gid)
authorized_keys.chmod(0o600)
except Exception as error:
logger.error(f"Error setting up ssh: {error}")
Expand Down
4 changes: 3 additions & 1 deletion core/start-blueos-core
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ function create_service {
tmux send-keys -t $SESSION_NAME "run-service '$SERVICE_NAME' '$command' $memory_limit_mb " C-m
}

SSH_USER=${SSH_USER:-pi}

ssh_command() {
ssh -i /root/.config/.ssh/id_rsa -o StrictHostKeyChecking=no pi@localhost "$1"
ssh -i /root/.config/.ssh/id_rsa -o StrictHostKeyChecking=no $SSH_USER@localhost "$1"
}

prepare_cgroups() {
Expand Down
2 changes: 1 addition & 1 deletion core/tools/scripts/red-pill
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ usage() {
}

# Default values
user="pi"
user=${SSH_USER:-pi}

while getopts ":hu:" opt; do
case ${opt} in
Expand Down

0 comments on commit ae544c8

Please sign in to comment.