From e432cbe473d68fa449995d4fed94fc83f3c9b8ec Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 15 May 2024 15:39:30 +0100 Subject: [PATCH] changes keys to gitea --- flake.lock | 8 +++---- home/common/optional/sops.nix | 8 +++---- hosts/common/users/keys/gitea.pub | 1 + scripts/bootstrap.sh | 39 +++++++++++++++++++++++++++---- 4 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 hosts/common/users/keys/gitea.pub diff --git a/flake.lock b/flake.lock index ce312c8..3d98107 100644 --- a/flake.lock +++ b/flake.lock @@ -43,11 +43,11 @@ "nix-secrets": { "flake": false, "locked": { - "lastModified": 1715644238, - "narHash": "sha256-QIQlx1bJV3E/z3hGS0hFVmXy1841uCPPP80o1c4VGDM=", + "lastModified": 1715721657, + "narHash": "sha256-QZg1Oyfn9DOOQ7Znryvtc0Qe3TOEF3fEeDZtDwTMul4=", "ref": "refs/heads/master", - "rev": "c8cc6d01c075b8fe802b3fd70bd961b1d13f747f", - "revCount": 2, + "rev": "1c2eac9c8be044e897b80ea2ff7a5184e20aa7ee", + "revCount": 4, "type": "git", "url": "ssh://git@git.bitlab21.com/sam/nix-secrets.git" }, diff --git a/home/common/optional/sops.nix b/home/common/optional/sops.nix index 2e6ed20..9164eb3 100644 --- a/home/common/optional/sops.nix +++ b/home/common/optional/sops.nix @@ -16,11 +16,11 @@ in validateSopsFiles = false; secrets = { - "ssh_keys/sam/private" = { - path = "${homeDirectory}/.ssh/openssh_key"; + "ssh_keys/sam/gitea" = { + path = "${homeDirectory}/.ssh/gitea"; }; - "ssh_keys/sam/public" = { - path = "${homeDirectory}/.ssh/openssh_key.pub"; + "ssh_keys/sam/gitea.pub" = { + path = "${homeDirectory}/.ssh/gitea.pub"; }; }; }; diff --git a/hosts/common/users/keys/gitea.pub b/hosts/common/users/keys/gitea.pub new file mode 100644 index 0000000..f5a1a2a --- /dev/null +++ b/hosts/common/users/keys/gitea.pub @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILywv6g5k5cOVplxgbbStrGf3qYVukgCcd80TJSBM8e8 sam@nixos diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh index 88b213a..aed46dd 100755 --- a/scripts/bootstrap.sh +++ b/scripts/bootstrap.sh @@ -19,6 +19,7 @@ read -p "Enter ip address: " ip KEY_DIR="$HOME/keys/$hostname/" mkdir -p $KEY_DIR +ssh-copy-id -i "$(readlink -n "$HOME/.ssh/openssh_key.pub" )" "root@$ip" echo "Creating ssh keys for new host. Will create in current directory: "$KEY_DIR KEY_NAME="ssh_ed25519_key_$hostname" @@ -29,8 +30,10 @@ confirm_delete="" [ "$KNOWN_HOSTS" != "" ] && echo -e "Host found in: ~/.ssh/known_hosts\n\n$KNOWN_HOSTS\n" && read -p "Delete existing hosts from ~/.ssh/known_hosts? (yes|no) " confirm_delete [ "$confirm_delete" = "yes" ] && sed -i "/$ip/d" ~/.ssh/known_hosts -ssh-keygen -t ed25519 -f "$KEY_DIR/$KEY_NAME" -C "$hostname@$ip" -N "" -[ $? != 0 ] && echo "Error generating ssh keys" && exit 1 +overwrite="" +[ -f "$KEY_DIR/$KEY_NAME" ] && read -p "Key exists, would you like to overwrite it? (yes|no) " overwrite + +[ "$overwrite" == "yes" ] && ssh-keygen -t ed25519 -f "$KEY_DIR/$KEY_NAME" -C "$hostname@$ip" -N "" echo "Copying ssh key to target host:" # Rsync currently not available in Nixos. Use scp instead. @@ -39,7 +42,7 @@ scp -i "$KEY_DIR/ssh_ed25519_key_$hostname"* "root@$ip:/etc/ssh/" [ $? != 0 ] && echo "Error copying keys to target host" && exit 1 echo "Generating age key from ssh key" -nix-shell -p ssh-to-age --run "cat $KEY_NAME.pub | ssh-to-age > $KEY_DIR/age_key_$hostname" +nix-shell -p ssh-to-age --run "cat $KEY_DIR/$KEY_NAME.pub | ssh-to-age > $KEY_DIR/age_key_$hostname" [ $? != 0 ] && echo "Error generating age key" && exit 1 AGE_KEY=$(cat "$KEY_DIR/age_key_$hostname") @@ -55,10 +58,23 @@ SHELL=/bin/sh nix run github:nix-community/nixos-anywhere -- --flake .#bootstrap echo "Deleting host from known_hosts" sed -i "/$ip/d" ~/.ssh/known_hosts +ssh-copy-id -i "$HOME/.ssh/openssh_key.pub" "$username@$ip" + +echo "Configuring ssh keys on target host to enable connection to gitea:" + +read -r -d '' config << EOM +Host git.bitlab21.com + IdentitiesOnly yes + IdentityFile ~/.ssh/gitea +EOM + +# Append the string to file on target machine +echo "$config" | ssh "$username@$ip" 'cat > .ssh/config' + echo -e " Complete! -Remember to add the age key to .sops.yaml, like this: +Now add the age key to .sops.yaml, like this: keys: - &hosts: - &$hostname $AGE_KEY @@ -71,10 +87,25 @@ creation_rules: Then to update the keys for the secrets.yaml file, run: 'sops --config .sops.yaml updatekeys secrets.yaml' +or with nix-shell: +'nix-shell -p sops --run 'sops --config .sops.yaml updatekeys secrets.yaml'' +then run 'nix flake lock --update-input nix-secrets $HOME/nixos' +Finally, commit and push these changes to remote so they can be accessed on +the new host. " +while true; +do + read -p "Confirm keys have been added to .sops.yaml using the above steps...(yes|no): " confirm + [ "$confirm" = "yes" ] && break +done + echo "Copying gitea private ssh key to host" +echo "New password is 'nixos'" + +ssh "$username@$ip" "mkdir /home/$username/.ssh" rsync -av "$(readlink -n "$HOME/.ssh/openssh_key" )" "$username@$ip":~/.ssh/gitea ssh "$username@$ip" "nix-shell -p git --run \"GIT_SSH_COMMAND='ssh -i ~/.ssh/gitea -o IdentitiesOnly=yes' git clone git@git.bitlab21.com:sam/nixos.git\"" ssh "$username@$ip" "nix-shell -p git --run \"GIT_SSH_COMMAND='ssh -i ~/.ssh/gitea -o IdentitiesOnly=yes' git clone git@git.bitlab21.com:sam/nix-secrets.git\"" +echo "Successfully installed Nixos on the target host!"