changes keys to gitea

This commit is contained in:
Sam 2024-05-15 15:39:30 +01:00
parent af75086a7e
commit e432cbe473
4 changed files with 44 additions and 12 deletions

View File

@ -43,11 +43,11 @@
"nix-secrets": { "nix-secrets": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1715644238, "lastModified": 1715721657,
"narHash": "sha256-QIQlx1bJV3E/z3hGS0hFVmXy1841uCPPP80o1c4VGDM=", "narHash": "sha256-QZg1Oyfn9DOOQ7Znryvtc0Qe3TOEF3fEeDZtDwTMul4=",
"ref": "refs/heads/master", "ref": "refs/heads/master",
"rev": "c8cc6d01c075b8fe802b3fd70bd961b1d13f747f", "rev": "1c2eac9c8be044e897b80ea2ff7a5184e20aa7ee",
"revCount": 2, "revCount": 4,
"type": "git", "type": "git",
"url": "ssh://git@git.bitlab21.com/sam/nix-secrets.git" "url": "ssh://git@git.bitlab21.com/sam/nix-secrets.git"
}, },

View File

@ -16,11 +16,11 @@ in
validateSopsFiles = false; validateSopsFiles = false;
secrets = { secrets = {
"ssh_keys/sam/private" = { "ssh_keys/sam/gitea" = {
path = "${homeDirectory}/.ssh/openssh_key"; path = "${homeDirectory}/.ssh/gitea";
}; };
"ssh_keys/sam/public" = { "ssh_keys/sam/gitea.pub" = {
path = "${homeDirectory}/.ssh/openssh_key.pub"; path = "${homeDirectory}/.ssh/gitea.pub";
}; };
}; };
}; };

View File

@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILywv6g5k5cOVplxgbbStrGf3qYVukgCcd80TJSBM8e8 sam@nixos

View File

@ -19,6 +19,7 @@ read -p "Enter ip address: " ip
KEY_DIR="$HOME/keys/$hostname/" KEY_DIR="$HOME/keys/$hostname/"
mkdir -p $KEY_DIR 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 echo "Creating ssh keys for new host. Will create in current directory: "$KEY_DIR
KEY_NAME="ssh_ed25519_key_$hostname" 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 [ "$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 [ "$confirm_delete" = "yes" ] && sed -i "/$ip/d" ~/.ssh/known_hosts
ssh-keygen -t ed25519 -f "$KEY_DIR/$KEY_NAME" -C "$hostname@$ip" -N "" overwrite=""
[ $? != 0 ] && echo "Error generating ssh keys" && exit 1 [ -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:" echo "Copying ssh key to target host:"
# Rsync currently not available in Nixos. Use scp instead. # 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 [ $? != 0 ] && echo "Error copying keys to target host" && exit 1
echo "Generating age key from ssh key" 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 [ $? != 0 ] && echo "Error generating age key" && exit 1
AGE_KEY=$(cat "$KEY_DIR/age_key_$hostname") 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" echo "Deleting host from known_hosts"
sed -i "/$ip/d" ~/.ssh/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 " echo -e "
Complete! Complete!
Remember to add the age key to .sops.yaml, like this: Now add the age key to .sops.yaml, like this:
keys: keys:
- &hosts: - &hosts:
- &$hostname $AGE_KEY - &$hostname $AGE_KEY
@ -71,10 +87,25 @@ creation_rules:
Then to update the keys for the secrets.yaml file, run: Then to update the keys for the secrets.yaml file, run:
'sops --config .sops.yaml updatekeys secrets.yaml' '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 "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 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/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\"" 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!"