adds support for a config file, command line params, and env vars

This commit is contained in:
James Tomasino 2020-01-17 22:21:52 +00:00
parent ae3bb3bb4f
commit 531ba0a79f
3 changed files with 110 additions and 27 deletions

View File

@ -23,7 +23,29 @@
-v Shows current version number.
-p [pattern prefix]
Set LastPass prefix pattern for the title search. The default pattern is
`SSH:'. If your key is named `testkey', then the default pattern will search
`SSH: testkey' in LastPass for the key password.
This is the same as setting `pattern_prefix' in the configuration file.
-t [type]
Set the SSH key type to test for. By default lssh will search for
`id_ed25519', `id_dsa`, and `id_rsa` in that order.
This is the same as setting `key_types' in the configuration file.
CONFIGURATION FILE
$XDG_CONFIG_HOME/lssh/config
Configuration settings in this file will override default settings. Each
setting is a string that should be written as `NAME="VALUE"'. Valid settings
are `pattern_prefix', `lastpass_user', `key_locations', and `key_types'
ENVIRONMENT VARIABLES
Environment variables will override the default settings and any configuration file
settings.
SSH_KEY_LOCATIONS
List of folders containing ssh keys.
@ -33,10 +55,14 @@
Unless otherwise defined, this variable defaults to ~/.ssh/
This is the same as setting `key_locations' in the configuration file.
LASTPASS_USER
Login username to Lastpass. This is used to initiate a login if you are not
already logged in when initiating lssh
This is the same as setting `lastpass_user' in the configuration file.
EXAMPLES
If you have an ssh key located at ~/.ssh/work/id_rsa, it can be loaded by entering:
@ -50,4 +76,4 @@
AUTHOR
James Tomasino
version 2019.12.25 25 Dec 2019 LSSH(1)
version 2020.01.17 17 Jan 2020 LSSH(1)

64
lssh
View File

@ -1,8 +1,10 @@
#!/bin/sh
version="2019.12.25"
arg_options="hv"
version="2020.01.17"
arg_options="hvp:t:"
key=""
pattern_prefix="SSH:"
key_locations="${HOME}/.ssh/"
key_types="id_ed25519 id_dsa id_rsa"
# Required: lpass (lastpass cli)
if ! command -v lpass > /dev/null; then
@ -23,6 +25,8 @@ lssh [options] [ssh-key]
OPTIONAL FLAGS:
-h Show this help
-v Show current version info
-p [pattern prefix] Set LastPass prefix pattern (default "SSH:")
-t [type] Set key type
END
}
@ -45,6 +49,16 @@ parse_input () {
printf "%s\\n" "$version"
exit 0
;;
-p)
shift
pattern_prefix="$1"
shift
;;
-t)
shift
key_types="$1"
shift
;;
--)
shift
break
@ -62,8 +76,23 @@ parse_input () {
}
main () {
# Load config, overwrites hardcoded defaults
if [ -n "$XDG_CONFIG_HOME" ]; then
config="${XDG_CONFIG_HOME}/lssh/config"
else
config="${HOME}/.config/lssh/config"
fi
if [ -f "$config" ]; then
# shellcheck disable=SC1090
. "$config"
fi
# env vars override config file
key_locations="${SSH_KEY_LOCATIONS:-${key_locations}}"
lastpass_user="${LASTPASS_USER:-${lastpass_user}}"
# command line switches override everything
parse_input "$@"
key_locations="${SSH_KEY_LOCATIONS:-$HOME/.ssh/}"
if [ -z "${key}" ]; then
printf "You need to specify a key name.\n"
@ -81,20 +110,13 @@ main () {
fi
if [ -d "${path}${key}" ]; then
# check keys in order of crypto awesomeness
# TODO: replace this with some listing of types or generic regex
if [ -f "${path}${key}/id_ed25519" ]; then
printf "Found key at: %s\\n" "${path}${key}/id_ed25519"
KEY_ID="${path}${key}/id_ed25519"
break;
elif [ -f "${path}${key}/id_dsa" ]; then
printf "Found key at: %s\\n" "${path}${key}/id_dsa"
KEY_ID="${path}${key}/id_dsa"
break;
elif [ -f "${path}${key}/id_rsa" ]; then
printf "Found key at: %s\\n" "${path}${key}/id_rsa"
KEY_ID="${path}${key}/id_rsa"
break;
fi
for type in $key_types; do
if [ -f "${path}${key}/${type}" ]; then
printf "Found key at: %s\\n" "${path}${key}/${type}"
KEY_ID="${path}${key}/${type}"
break;
fi
done
fi
done
@ -106,18 +128,18 @@ main () {
# If not logged into lastpass, do so now
while ! lpass status -q; do
if [ -z "${LASTPASS_USER}" ]; then
if [ -z "${lastpass_user}" ]; then
printf "Lastpass Username: "
read -r lpass_user
lpass login --trust "${lpass_user}"
else
lpass login --trust "${LASTPASS_USER}"
lpass login --trust "${lastpass_user}"
fi
done
# Retrieve key from LastPass. If logged in but not recently authenticated
# lastpass will prompt with pinentry. If no entry found, suppress error.
password=$(lpass show --password "SSH: ${key}" 2> /dev/null)
password=$(lpass show --password "${pattern_prefix} ${key}" 2> /dev/null)
# If the "SSH: xxx" pattern failed, try the key directly
if [ -z "$password" ]; then

45
lssh.1
View File

@ -1,8 +1,11 @@
.TH LSSH 1 "25 Dec 2019" "version 2019.12.25"
.TH LSSH 1 "17 Jan 2020" "version 2020.01.17"
.SH NAME
lssh \- a wrapper for Lastpass CLI and ssh-agent
.SH SYNOPSIS
lssh [ -hv ] [ssh key name]
.SH DESRIPTION
lssh quickly activates ssh keys by name, filling passwords via Lastpass CLI, and
adding them to ssh-agent. Keys are activated for 1-hour at a time.
@ -12,10 +15,11 @@ The ssh key name provided to
will be used to look up both the key itself and the password for the key. If
that key has a password,
.B lssh
will search Lastpass for an entry named `SSH: [ssh key name]' and autofill the password
with the results. If that entry is not found, `[ssh key name]' will also be attempted
before giving up on a password search. The key will be passed to the ssh-agent
regardless.
will search Lastpass for an entry named `SSH: [ssh key name]' and autofill the
password with the results. If that entry is not found, `[ssh key name]' will
also be attempted before giving up on a password search. The key will be passed
to the ssh-agent regardless.
.SH OPTIONS
.TP
.B -h
@ -23,7 +27,31 @@ Shows simple help.
.TP
.B -v
Shows current version number.
.TP
.B -p [pattern prefix]
Set LastPass prefix pattern for the title search. The default pattern is
`SSH:'. If your key is named `testkey', then the default pattern will search
`SSH: testkey' in LastPass for the key password.
This is the same as setting `pattern_prefix' in the configuration file.
.TP
.B -t [type]
Set the SSH key type to test for. By default
.B lssh
will search for `id_ed25519', `id_dsa`, and `id_rsa` in that order.
This is the same as setting `key_types' in the configuration file.
.SH CONFIGURATION FILE
.TP
.I $XDG_CONFIG_HOME/lssh/config
Configuration settings in this file will override default settings. Each
setting is a string that should be written as `NAME="VALUE"'. Valid settings
are `pattern_prefix', `lastpass_user', `key_locations', and `key_types'
.SH ENVIRONMENT VARIABLES
Environment variables will override the default settings and any configuration
file settings.
.TP
.B SSH_KEY_LOCATIONS
List of folders containing ssh keys.
@ -33,15 +61,21 @@ separated, which will be used to search for the ssh keys. The folders are
searched in order and the search stops at the first successful match.
Unless otherwise defined, this variable defaults to ~/.ssh/
This is the same as setting `key_locations' in the configuration file.
.TP
.B LASTPASS_USER
Login username to Lastpass. This is used to initiate a login if you are not
already logged in when initiating lssh
This is the same as setting `lastpass_user' in the configuration file.
.SH EXAMPLES
If you have an ssh key located at ~/.ssh/work/id_rsa, it can be loaded by
entering:
$ lssh work
.SH DEPENDENCIES
.TP
.B lpass
@ -49,5 +83,6 @@ The Lastpass CLI client
.TP
.B expect
programmed dialogue with interactive programs
.SH AUTHOR
James Tomasino