Skip to content

Commit

Permalink
Update app for create-keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinlyons committed Dec 21, 2023
1 parent 4c65f1f commit 8446fed
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
37 changes: 28 additions & 9 deletions apps/aarch64-darwin/create-keys
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,35 @@ setup_ssh_directory() {
mkdir -p ${SSH_DIR}
}

generate_keys() {
ssh-keygen -t ed25519 -f "${SSH_DIR}/id_ed25519" -N ""
ssh-keygen -t ed25519 -f "${SSH_DIR}/id_ed25519_agenix" -N ""
chown ${username}:staff ${SSH_DIR}/id_ed25519{,_agenix}{,.pub}
prompt_for_key_generation() {
local key_name=$1
if [[ -f "${SSH_DIR}/${key_name}" ]]; then
echo -e "${RED}Existing SSH key found for ${key_name}.${NC}"
cat "${SSH_DIR}/${key_name}.pub"
read -p "Do you want to replace it? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0 # Indicate key should be replaced
else
return 1 # Indicate key should be kept
fi
fi
return 0 # Indicate no key exists, so it should be created
}

generate_key() {
local key_name=$1
if prompt_for_key_generation "$key_name"; then
ssh-keygen -t ed25519 -f "${SSH_DIR}/${key_name}" -N ""
chown ${username}:staff "${SSH_DIR}/${key_name}"{,.pub}
else
echo -e "${GREEN}Kept existing ${key_name}.${NC}"
fi
}

setup_ssh_directory
generate_keys
generate_key "id_ed25519"
generate_key "id_ed25519_agenix"

echo -e "${GREEN}New SSH keys have been generated.${NC}"
echo -e "${GREEN}1) Add the id_ed25519 key to Github.${NC}"
cat "${SSH_DIR}/id_ed25519.pub"
echo -e "${GREEN}2) Create a private nix-secrets repo in Github, even if it's empty.${NC}"
echo -e "${GREEN}SSH key setup complete.${NC}"
echo -e "${GREEN}Remember to add the necessary keys to Github or other services as required.${NC}"
45 changes: 21 additions & 24 deletions templates/starter-with-secrets/apps/aarch64-darwin/create-keys
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,35 @@ setup_ssh_directory() {
mkdir -p ${SSH_DIR}
}

check_existing_keys() {
if [[ -f "${SSH_DIR}/id_ed25519" || -f "${SSH_DIR}/id_ed25519_agenix" ]]; then
echo -e "${RED}Existing SSH keys found.${NC}"
echo -e "${RED}1) id_ed25519${NC}"
[[ -f "${SSH_DIR}/id_ed25519" ]] && cat "${SSH_DIR}/id_ed25519.pub"
echo -e "${RED}2) id_ed25519_agenix${NC}"
[[ -f "${SSH_DIR}/id_ed25519_agenix" ]] && cat "${SSH_DIR}/id_ed25519_agenix.pub"
read -p "Do you want to replace them? (y/n) " -n 1 -r
prompt_for_key_generation() {
local key_name=$1
if [[ -f "${SSH_DIR}/${key_name}" ]]; then
echo -e "${RED}Existing SSH key found for ${key_name}.${NC}"
cat "${SSH_DIR}/${key_name}.pub"
read -p "Do you want to replace it? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0
return 0 # Indicate key should be replaced
else
return 1
return 1 # Indicate key should be kept
fi
fi
return 0
return 0 # Indicate no key exists, so it should be created
}

generate_keys() {
ssh-keygen -t ed25519 -f "${SSH_DIR}/id_ed25519" -N ""
ssh-keygen -t ed25519 -f "${SSH_DIR}/id_ed25519_agenix" -N ""
chown ${username}:staff ${SSH_DIR}/id_ed25519{,_agenix}{,.pub}
generate_key() {
local key_name=$1
if prompt_for_key_generation "$key_name"; then
ssh-keygen -t ed25519 -f "${SSH_DIR}/${key_name}" -N ""
chown ${username}:staff "${SSH_DIR}/${key_name}"{,.pub}
else
echo -e "${GREEN}Kept existing ${key_name}.${NC}"
fi
}

setup_ssh_directory
if check_existing_keys; then
generate_keys
echo -e "${GREEN}New SSH keys have been generated.${NC}"
else
echo -e "${GREEN}Existing SSH keys kept.${NC}"
fi
generate_key "id_ed25519"
generate_key "id_ed25519_agenix"

echo -e "${GREEN}1) Add the id_ed25519 key to Github.${NC}"
cat "${SSH_DIR}/id_ed25519.pub"
echo -e "${GREEN}2) Create a private nix-secrets repo in Github, even if it's empty.${NC}"
echo -e "${GREEN}SSH key setup complete.${NC}"
echo -e "${GREEN}Remember to add the necessary keys to Github or other services as required.${NC}"

0 comments on commit 8446fed

Please sign in to comment.