├── load └── README.md /load: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | HOMETIME="18:00:00" 6 | HOURS="$1" 7 | LASTPASS_NAME="Personal/GitHub SSH key" 8 | PRIVATE_KEY="key" 9 | OWNER="Josh" 10 | 11 | main() { 12 | delete_all_ssh_identities 13 | save_private_key_to_file 14 | add_private_key 15 | } 16 | 17 | delete_all_ssh_identities() { 18 | ssh-add -D 19 | } 20 | 21 | save_private_key_to_file() { 22 | delete_private_key_file 23 | lpass show "$LASTPASS_NAME" --notes > "$PRIVATE_KEY" 24 | chmod 400 "$PRIVATE_KEY" 25 | } 26 | 27 | add_private_key() { 28 | if [ -z "$HOURS" ] 29 | then 30 | add_private_key_until_hometime 31 | else 32 | add_private_key_for_x_hours 33 | fi 34 | } 35 | 36 | add_private_key_until_hometime() { 37 | local now="$(date +%s)" 38 | local until="$( date -j -f "%T" $HOMETIME +%s )" 39 | local seconds_remaining="$( expr $until - $now )" 40 | 41 | if [ "$seconds_remaining" -lt 1 ] 42 | then 43 | echo "$HOMETIME is history. Go home!" 44 | exit 1 45 | else 46 | echo "Adding $OWNER's private key until $HOMETIME..." 47 | ssh-add -t "$seconds_remaining" "$PRIVATE_KEY" 48 | fi 49 | } 50 | 51 | add_private_key_for_x_hours() { 52 | echo "Adding $OWNER's private key for $HOURS hours..." 53 | ssh-add -t "${HOURS}h" "$PRIVATE_KEY" 54 | } 55 | 56 | delete_private_key_file() { 57 | rm -f "$PRIVATE_KEY" 58 | } 59 | 60 | trap delete_private_key_file EXIT 61 | main 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Load an SSH key for the day using LastPass CLI 2 | 3 | At [Pivotal](https://pivotal.io) we share Mac workstations, so we typically log in for the day. 4 | 5 | I used to carry my SSH key on a USB stick. Now it's a backup and I load my SSH key using LastPass. 6 | 7 | ## Prerequisites 8 | 9 | 1. [LastPass](https://lastpass.com/) account 10 | 1. LastPass CLI: `brew install lastpass-cli` 11 | 12 | ## Set up 13 | 14 | 1. Create a new SSH key; GitHub has a [decent guide](https://help.github.com/articles/generating-an-ssh-key/) 15 | 16 | 1. Put the private key in the notes field of a LastPass secure note, e.g. "Personal/GitHub SSH key" 17 | 18 | 1. Customise the [load script](https://github.com/jamesjoshuahill/lastpass-ssh-key/blob/master/load), with your own variables 19 | 20 | 1. Put your custom load script in the notes field of another LastPass secure note, e.g. "Personal/Load GitHub SSH Key" 21 | 22 | ## Usage 23 | 24 | ```bash 25 | lpass login $USERNAME 26 | 27 | # Load your SSH key until hometime! 28 | bash <(lpass show 'Personal/Load GitHub SSH Key' --notes) 29 | 30 | # Or, load your SSH key for 2 hours! 31 | bash <(lpass show 'Personal/Load GitHub SSH Key' --notes) 2 32 | ``` 33 | 34 | ## Bash profile 35 | 36 | For convenience we've added a function to our bash profile: 37 | 38 | ```bash 39 | load_ssh_key() { 40 | if lpass status; then 41 | bash <(lpass show 'Personal/Load GitHub SSH Key' --notes) "$1" 42 | fi 43 | } 44 | ``` 45 | --------------------------------------------------------------------------------