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

Add usb rsa key #13509

Merged
merged 6 commits into from
Sep 12, 2023
Merged
Changes from 3 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
53 changes: 53 additions & 0 deletions server-utils/server_utils/ssh_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Module to add ssh public keys from usb thumbdrive."""
import os
import hashlib
import subprocess
from pathlib import Path
from typing import Dict, Optional


AUTHORIZED_KEYS = os.path.expanduser("~/.ssh/authorized_keys")


def add_ssh_keys_from_usb(path: Optional[Path] = None) -> None:
"""Find ssh keys on the given path and add them to the authorized_keys."""

path = path or Path("/media")

print(f"Searching for public keys in: {path}")
pub_keys = subprocess.check_output(
['find', path, '-type', 'f', '-name', '*.pub']
).decode().strip().split()
if not pub_keys:
print("No public keys found")
return

# Load the current keys and hash them if we have any
current_keys = dict()
if os.path.exists(AUTHORIZED_KEYS):
with open(AUTHORIZED_KEYS, "r") as fh:
current_keys = {
hashlib.new("md5", line.encode()).hexdigest(): line
for line in fh.read().split("\n")
if line.strip()
}

# Update the existing keys if the ssh public key is valid
with open(AUTHORIZED_KEYS, "a") as fh:
for key in pub_keys:
with open(key, "r") as gh:
ssh_key = gh.read()
if "ssh-rsa" not in ssh_key:
print(f"Invalid ssh public key: {key}")
continue
key_hash = hashlib.new("md5", ssh_key.encode()).hexdigest()
if not current_keys.get(key_hash):
fh.write(ssh_key)
print(f"Added new rsa key: {key}")


def clear_ssh_keys() -> None:
"""Delete all the ssh keys on the robot."""
with open(AUTHORIZED_KEYS, "w") as fh:
fh.write("\n")
print(f"Cleared ssh keys: {AUTHORIZED_KEYS}")