chore: use flock for ssh bootstrap repair
This commit is contained in:
committed by
GitHub
parent
31abf40722
commit
7f975bc4f1
@@ -39,20 +39,20 @@ lib.mkIf userExists {
|
|||||||
Type = "oneshot";
|
Type = "oneshot";
|
||||||
RemainAfterExit = true;
|
RemainAfterExit = true;
|
||||||
};
|
};
|
||||||
path = [ pkgs.openssh pkgs.coreutils ];
|
path = [ pkgs.openssh pkgs.coreutils pkgs.util-linux ];
|
||||||
script = ''
|
script = ''
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
PASSPHRASE=$(cat /var/lib/secrets/ssh-passphrase)
|
PASSPHRASE=$(cat /var/lib/secrets/ssh-passphrase)
|
||||||
lock_dir="${keyPath}.lock"
|
lock_file="${keyPath}.lock"
|
||||||
|
|
||||||
if ! mkdir "$lock_dir"; then
|
exec 9>"$lock_file"
|
||||||
|
|
||||||
|
if ! flock -n 9; then
|
||||||
echo "Factory SSH key setup is already running." >&2
|
echo "Factory SSH key setup is already running." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
trap 'rmdir "$lock_dir"' EXIT
|
|
||||||
|
|
||||||
generate_factory_key() {
|
generate_factory_key() {
|
||||||
ssh-keygen -q -N "$PASSPHRASE" -t ed25519 -f "${keyPath}"
|
ssh-keygen -q -N "$PASSPHRASE" -t ed25519 -f "${keyPath}"
|
||||||
chown ${userName}:users "${keyPath}" "${keyPath}.pub"
|
chown ${userName}:users "${keyPath}" "${keyPath}.pub"
|
||||||
@@ -63,8 +63,7 @@ lib.mkIf userExists {
|
|||||||
if [ ! -f "${keyPath}" ]; then
|
if [ ! -f "${keyPath}" ]; then
|
||||||
generate_factory_key
|
generate_factory_key
|
||||||
elif ! ssh-keygen -y -P "$PASSPHRASE" -f "${keyPath}" >/dev/null 2>&1; then
|
elif ! ssh-keygen -y -P "$PASSPHRASE" -f "${keyPath}" >/dev/null 2>&1; then
|
||||||
echo "Existing factory SSH key does not match current passphrase; backing it up and generating a replacement."
|
backup_suffix="$(date -u +%Y%m%d_%H%M%S)-$$"
|
||||||
backup_suffix=$(date -u +%Y%m%d_%H%M%S)
|
|
||||||
backup_path="${keyPath}.bak-$backup_suffix"
|
backup_path="${keyPath}.bak-$backup_suffix"
|
||||||
backup_index=0
|
backup_index=0
|
||||||
|
|
||||||
@@ -73,6 +72,7 @@ lib.mkIf userExists {
|
|||||||
backup_path="${keyPath}.bak-$backup_suffix-$backup_index"
|
backup_path="${keyPath}.bak-$backup_suffix-$backup_index"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
echo "Existing factory SSH key does not match current passphrase; backing it up to $backup_path and generating a replacement."
|
||||||
mv "${keyPath}" "$backup_path"
|
mv "${keyPath}" "$backup_path"
|
||||||
|
|
||||||
if [ -f "${keyPath}.pub" ]; then
|
if [ -f "${keyPath}.pub" ]; then
|
||||||
|
|||||||
Reference in New Issue
Block a user