Skip to content

Commit ae544c8

Browse files
Core: allow getting ssh user and password from environment variables
Co-authored-by: João Antônio Cardoso <joao.maker@gmail.com>
1 parent f776c91 commit ae544c8

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

core/libs/commonwealth/commonwealth/utils/commands.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import subprocess
23
from pathlib import Path
34
from typing import List, Optional
@@ -12,8 +13,8 @@ class KeyNotFound(Exception):
1213
def run_command_with_password(command: str, check: bool = True) -> "subprocess.CompletedProcess['str']":
1314
# attempt to run the command with sshpass
1415
# used as a fallback if the ssh key is not found
15-
user = "pi"
16-
password = "raspberry"
16+
user = os.environ.get("SSH_USER", "pi")
17+
password = os.environ.get("SSH_PASSWORD", "raspberry")
1718

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

3637
def run_command_with_ssh_key(command: str, check: bool = True) -> "subprocess.CompletedProcess['str']":
3738
# attempt to run the command with the ssh key
38-
user = "pi"
39+
user = os.environ.get("SSH_USER", "pi")
3940
id_file = "/root/.config/.ssh/id_rsa"
4041
if not Path(id_file).exists():
4142
raise KeyNotFound
@@ -84,8 +85,8 @@ def upload_file_with_password(
8485
) -> "subprocess.CompletedProcess['str']":
8586
# attempt to upload the file with sshpass
8687
# used as a fallback if the ssh key is not found
87-
user = "pi"
88-
password = "raspberry"
88+
user = os.environ.get("SSH_USER", "pi")
89+
password = os.environ.get("SSH_PASSWORD", "raspberry")
8990

9091
return subprocess.run(
9192
[
@@ -105,7 +106,7 @@ def upload_file_with_password(
105106

106107
def upload_file_with_ssh_key(source: str, destination: str, check: bool = True) -> "subprocess.CompletedProcess['str']":
107108
# attempt to upload the file with the ssh key
108-
user = "pi"
109+
user = os.environ.get("SSH_USER", "pi")
109110
id_file = "/root/.config/.ssh/id_rsa"
110111
if not Path(id_file).exists():
111112
raise KeyNotFound

core/services/commander/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ def setup_ssh() -> None:
226226
key_path = Path("/root/.config/.ssh")
227227
private_key = key_path / "id_rsa"
228228
public_key = private_key.with_suffix(".pub")
229-
authorized_keys = Path("/home/pi/.ssh/authorized_keys")
229+
user = os.environ.get("SSH_USER", "pi")
230+
gid = int(os.environ.get("USER_GID", 1000))
231+
uid = int(os.environ.get("USER_UID", 1000))
232+
authorized_keys = Path(f"/home/{user}/.ssh/authorized_keys")
230233

231234
try:
232235
key_path.mkdir(parents=True, exist_ok=True)
@@ -247,7 +250,7 @@ def setup_ssh() -> None:
247250
authorized_keys_text += public_key_text
248251
authorized_keys.write_text(authorized_keys_text, "utf-8")
249252

250-
shutil.chown(authorized_keys, "pi", "pi")
253+
os.chown(authorized_keys, uid, gid)
251254
authorized_keys.chmod(0o600)
252255
except Exception as error:
253256
logger.error(f"Error setting up ssh: {error}")

core/start-blueos-core

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,10 @@ function create_service {
159159
tmux send-keys -t $SESSION_NAME "run-service '$SERVICE_NAME' '$command' $memory_limit_mb " C-m
160160
}
161161

162+
SSH_USER=${SSH_USER:-pi}
163+
162164
ssh_command() {
163-
ssh -i /root/.config/.ssh/id_rsa -o StrictHostKeyChecking=no pi@localhost "$1"
165+
ssh -i /root/.config/.ssh/id_rsa -o StrictHostKeyChecking=no $SSH_USER@localhost "$1"
164166
}
165167

166168
prepare_cgroups() {

core/tools/scripts/red-pill

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ usage() {
99
}
1010

1111
# Default values
12-
user="pi"
12+
user=${SSH_USER:-pi}
1313

1414
while getopts ":hu:" opt; do
1515
case ${opt} in

0 commit comments

Comments
 (0)