From 2e7b334b9561be72b30ac0fb50db88308ef354d8 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 3 Mar 2025 04:55:18 +0100
Subject: [PATCH] Add secrets setup.

---
 testing/scripts/setup_secrets.sh | 51 ++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100755 testing/scripts/setup_secrets.sh

diff --git a/testing/scripts/setup_secrets.sh b/testing/scripts/setup_secrets.sh
new file mode 100755
index 0000000..0368ad9
--- /dev/null
+++ b/testing/scripts/setup_secrets.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+set -e
+set -x
+
+SECRETS_DEV=sdb
+PATH_SECRETS_DEV=/dev/${SECRETS_DEV}
+PATH_REL_SECRETS=to_usbbb
+PATH_SOURCE=/media/${SECRETS_DEV}/${PATH_REL_SECRETS}
+PATH_HOME=/home/plom
+PATH_TARGET=${PATH_HOME}/${PATH_REL_SECRETS}
+PATH_REL_SSH=.ssh
+PATH_SSH=${PATH_HOME}/${PATH_REL_SSH}
+NAME_KEY=id_rsa
+
+# Ensure we're running as root.
+if [ `whoami` != "root" ]; then
+    echo "Must be run as root."
+    exit 1 
+fi
+
+# Mount secrets device and copy over its content.
+echo "Put secrets drive into slot for ${PATH_SECRETS_DEV}."
+while [ ! -e "${PATH_SECRETS_DEV}" ]; do
+    sleep 0.1
+done
+while [ true ]; do
+    stty -echo
+    printf "Secrets passphrase: "
+    read SECRETS_PASS 
+    stty echo
+    echo "" # newline so user knows their input return was accepted
+    set +e
+    echo "${SECRETS_PASS}" | pmount "${PATH_SECRETS_DEV}"
+    result=$?
+    set -e
+    if [ "${result}" = "0" ]; then
+        break
+    elif [ "${result}" != "100" ]; then
+        echo "Aborting due to pmount error."
+	exit 1
+    fi
+done
+cp -a "${PATH_SOURCE}" "${PATH_TARGET}"
+chown -R plom:plom "${PATH_TARGET}"
+pumount "${SECRETS_DEV}"
+echo "You can remove ${PATH_SECRETS_DEV} now."
+
+# Set up SSH authentication.
+echo "Setting up .ssh"
+su -lc "mkdir -p ${PATH_SSH} && cp ${PATH_TARGET}/${NAME_KEY} ${PATH_SSH}/"
+su -lc "ssh-keygen -y -f ${PATH_SSH}/${NAME_KEY} > ${PATH_SSH}/${NAME_KEY}.pub" plom
-- 
2.30.2