|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +dirname="$(dirname "$(realpath $0)")" |
| 4 | +source "$dirname/common.sh" |
| 5 | + |
| 6 | +# prints the script's usage options |
| 7 | +function print_help { |
| 8 | + echo "Usage:" |
| 9 | + echo " $0 -H <hostname> [options] [arguments to 'op signin']" |
| 10 | + echo " $0 -h" |
| 11 | + echo |
| 12 | + echo "Options:" |
| 13 | + echo " -h Print this help and exit" |
| 14 | + echo " -H <hostname>" |
| 15 | + echo " Remote hostname" |
| 16 | + echo " -i <identity>" |
| 17 | + echo " Use an existing identity instead of generating a new one (e.g." |
| 18 | + echo " ('-i id_rsa' will use private key 'id_rsa' and public key" |
| 19 | + echo " 'id_rsa.pub')" |
| 20 | + echo " -t <title>" |
| 21 | + echo " Title for the new 1Password item (default is user@host)" |
| 22 | + echo " -u <username>" |
| 23 | + echo " Remote username (default is current username)" |
| 24 | + echo " -y Perform all actions automatically" |
| 25 | + echo |
| 26 | + echo "See 'op signin --help' for additional arguments to 'op signin'." |
| 27 | + echo "(If you run 'op signin' at least once before running this script" |
| 28 | + echo "you shouldn't need any additional arguments.)" |
| 29 | +} |
| 30 | + |
| 31 | +# prompts the user to continue (returns 0), skip (returns 1), or abort (exit the |
| 32 | +# script immediately), using the arguments as a prompt message |
| 33 | +function confirm { |
| 34 | + if [ ! -z $skip_confirms ]; then |
| 35 | + echo "$* (-y specified, continuing)" |
| 36 | + return 0 |
| 37 | + fi |
| 38 | + |
| 39 | + while true; do |
| 40 | + read -p "$* [(continue)/skip/abort]: " answer |
| 41 | + |
| 42 | + case $answer in |
| 43 | + [Cc]|continue|"") |
| 44 | + return 0 |
| 45 | + ;; |
| 46 | + [Ss]|skip) |
| 47 | + return 1 |
| 48 | + ;; |
| 49 | + [Aa]|abort) |
| 50 | + echo "Aborted" |
| 51 | + exit 0 |
| 52 | + ;; |
| 53 | + esac |
| 54 | + done |
| 55 | +} |
| 56 | + |
| 57 | +OPTIND=1 |
| 58 | +ssh_host="" |
| 59 | +ssh_user="$(id -u -n)" |
| 60 | +title="" |
| 61 | +key_file="" |
| 62 | +skip_confirms="" |
| 63 | +while getopts "hH:i:t:u:y" opt; do |
| 64 | + case "$opt" in |
| 65 | + h) |
| 66 | + print_help |
| 67 | + exit 0 |
| 68 | + ;; |
| 69 | + H) |
| 70 | + ssh_host="$OPTARG" |
| 71 | + ;; |
| 72 | + i) |
| 73 | + key_file="$OPTARG" |
| 74 | + ;; |
| 75 | + t) |
| 76 | + title="$OPTARG" |
| 77 | + ;; |
| 78 | + u) |
| 79 | + ssh_user="$OPTARG" |
| 80 | + ;; |
| 81 | + y) |
| 82 | + skip_confirms="1" |
| 83 | + ;; |
| 84 | + esac |
| 85 | +done |
| 86 | +shift $((OPTIND-1)) |
| 87 | +[ "${1:-}" = "--" ] && shift |
| 88 | + |
| 89 | +# TODO: allow overriding this with an option |
| 90 | +temp_storage_root="$default_temp_storage_root" |
| 91 | + |
| 92 | +if [ -z $ssh_host ]; then |
| 93 | + echo "Option -H is required" >&2 |
| 94 | + print_help |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +if [ -z "$key_file" ]; then |
| 99 | + cleanup_key_pair="1" |
| 100 | + key_file="$dirname/temp_id_rsa" |
| 101 | + # TODO: generate this somewhere it won't be written to disk (use temp dir?) |
| 102 | + # TODO: can we read the new keys into variables here and immediately delete |
| 103 | + # the file to avoid having to set another variable to remember to |
| 104 | + # delete it later? |
| 105 | + echo "Generating new keypair..." |
| 106 | + # TODO: option for customizing key comment |
| 107 | + ssh-keygen -f "$key_file" -N "" -C "$(id -un)@$(hostname) -> $ssh_user@$ssh_host" -q |
| 108 | +else |
| 109 | + if [ -f "$key_file" ] && [ -f "$key_file.pub" ]; then |
| 110 | + echo "Using existing keypair $key_file and $key_file.pub (specified by -f)" |
| 111 | + else |
| 112 | + echo "One of $key_file and $key_file.pub does not exist (specified by -f)" >&2 |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | +fi |
| 116 | + |
| 117 | +op_signin $@ |
| 118 | + |
| 119 | +template="$(cat $dirname/itemtemplate.json)" |
| 120 | +item_data="$(echo "$template" | jq \ |
| 121 | + --arg ssh_host "$ssh_host" \ |
| 122 | + --arg ssh_user "$ssh_user" \ |
| 123 | + --rawfile ssh_private_key "$key_file" \ |
| 124 | + --rawfile ssh_public_key "$key_file.pub" \ |
| 125 | + ' |
| 126 | + (.sections[0].fields[] | select(.n == "url")) |= ( |
| 127 | + . | .v |= $ssh_host |
| 128 | + ) | |
| 129 | + (.sections[0].fields[] | select(.n == "username")) |= ( |
| 130 | + . | .v |= $ssh_user |
| 131 | + ) | |
| 132 | + (.sections[0].fields[] | select(.n == "ssh_private_key")) |= ( |
| 133 | + . | .v = $ssh_private_key |
| 134 | + ) | |
| 135 | + (.sections[0].fields[] | select(.n == "ssh_public_key")) |= ( |
| 136 | + . | .v = $ssh_public_key |
| 137 | + ) |
| 138 | + ' |
| 139 | +)" |
| 140 | + |
| 141 | +if confirm "Saving new item in 1password"; then |
| 142 | + encoded_item_data="$(echo "$item_data" | op encode)" |
| 143 | + new_uuid="$(op create item Server --title "${title:-$ssh_host}" "$encoded_item_data" | jq '.uuid' --raw-output)" |
| 144 | +else |
| 145 | + # you can skip uploading the key to 1Password, but we still need a fake |
| 146 | + # UUID so the key can have a name in the filesystem if stored |
| 147 | + new_uuid="_local_only_$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 14 | head -n 1)" |
| 148 | +fi |
| 149 | + |
| 150 | +if confirm "Copying public key to host: ssh-copy-id -i '$key_file' '$ssh_host'"; then |
| 151 | + ssh-copy-id -i "$key_file" "$ssh_host" |
| 152 | +fi |
| 153 | + |
| 154 | +if confirm "Adding key for local use"; then |
| 155 | + # cat "$key_file" | ssh-add - |
| 156 | + configure_keys "$temp_storage_root" "$new_uuid" "$private_key" "$public_key" "$ssh_host" "$ssh_user" |
| 157 | +fi |
| 158 | + |
| 159 | +if [ ! -z $cleanup_key_pair ]; then |
| 160 | + rm "$key_file" "$key_file.pub" |
| 161 | +fi |
| 162 | + |
| 163 | +op signout |
| 164 | + |
| 165 | +echo "Done." |
0 commit comments