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. -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
Environment variables will override the default settings and any configuration file
settings.
SSH_KEY_LOCATIONS SSH_KEY_LOCATIONS
List of folders containing ssh keys. List of folders containing ssh keys.
@ -33,10 +55,14 @@
Unless otherwise defined, this variable defaults to ~/.ssh/ Unless otherwise defined, this variable defaults to ~/.ssh/
This is the same as setting `key_locations' in the configuration file.
LASTPASS_USER LASTPASS_USER
Login username to Lastpass. This is used to initiate a login if you are not Login username to Lastpass. This is used to initiate a login if you are not
already logged in when initiating lssh already logged in when initiating lssh
This is the same as setting `lastpass_user' in the configuration file.
EXAMPLES EXAMPLES
If you have an ssh key located at ~/.ssh/work/id_rsa, it can be loaded by entering: If you have an ssh key located at ~/.ssh/work/id_rsa, it can be loaded by entering:
@ -50,4 +76,4 @@
AUTHOR AUTHOR
James Tomasino 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 #!/bin/sh
version="2020.01.17"
version="2019.12.25" arg_options="hvp:t:"
arg_options="hv"
key="" key=""
pattern_prefix="SSH:"
key_locations="${HOME}/.ssh/"
key_types="id_ed25519 id_dsa id_rsa"
# Required: lpass (lastpass cli) # Required: lpass (lastpass cli)
if ! command -v lpass > /dev/null; then if ! command -v lpass > /dev/null; then
@ -23,6 +25,8 @@ lssh [options] [ssh-key]
OPTIONAL FLAGS: OPTIONAL FLAGS:
-h Show this help -h Show this help
-v Show current version info -v Show current version info
-p [pattern prefix] Set LastPass prefix pattern (default "SSH:")
-t [type] Set key type
END END
} }
@ -45,6 +49,16 @@ parse_input () {
printf "%s\\n" "$version" printf "%s\\n" "$version"
exit 0 exit 0
;; ;;
-p)
shift
pattern_prefix="$1"
shift
;;
-t)
shift
key_types="$1"
shift
;;
--) --)
shift shift
break break
@ -62,8 +76,23 @@ parse_input () {
} }
main () { 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 "$@" parse_input "$@"
key_locations="${SSH_KEY_LOCATIONS:-$HOME/.ssh/}"
if [ -z "${key}" ]; then if [ -z "${key}" ]; then
printf "You need to specify a key name.\n" printf "You need to specify a key name.\n"
@ -81,20 +110,13 @@ main () {
fi fi
if [ -d "${path}${key}" ]; then if [ -d "${path}${key}" ]; then
# check keys in order of crypto awesomeness # check keys in order of crypto awesomeness
# TODO: replace this with some listing of types or generic regex for type in $key_types; do
if [ -f "${path}${key}/id_ed25519" ]; then if [ -f "${path}${key}/${type}" ]; then
printf "Found key at: %s\\n" "${path}${key}/id_ed25519" printf "Found key at: %s\\n" "${path}${key}/${type}"
KEY_ID="${path}${key}/id_ed25519" KEY_ID="${path}${key}/${type}"
break; break;
elif [ -f "${path}${key}/id_dsa" ]; then fi
printf "Found key at: %s\\n" "${path}${key}/id_dsa" done
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
fi fi
done done
@ -106,18 +128,18 @@ main () {
# If not logged into lastpass, do so now # If not logged into lastpass, do so now
while ! lpass status -q; do while ! lpass status -q; do
if [ -z "${LASTPASS_USER}" ]; then if [ -z "${lastpass_user}" ]; then
printf "Lastpass Username: " printf "Lastpass Username: "
read -r lpass_user read -r lpass_user
lpass login --trust "${lpass_user}" lpass login --trust "${lpass_user}"
else else
lpass login --trust "${LASTPASS_USER}" lpass login --trust "${lastpass_user}"
fi fi
done done
# Retrieve key from LastPass. If logged in but not recently authenticated # Retrieve key from LastPass. If logged in but not recently authenticated
# lastpass will prompt with pinentry. If no entry found, suppress error. # 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 the "SSH: xxx" pattern failed, try the key directly
if [ -z "$password" ]; then 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 .SH NAME
lssh \- a wrapper for Lastpass CLI and ssh-agent lssh \- a wrapper for Lastpass CLI and ssh-agent
.SH SYNOPSIS .SH SYNOPSIS
lssh [ -hv ] [ssh key name] lssh [ -hv ] [ssh key name]
.SH DESRIPTION .SH DESRIPTION
lssh quickly activates ssh keys by name, filling passwords via Lastpass CLI, and 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. 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 will be used to look up both the key itself and the password for the key. If
that key has a password, that key has a password,
.B lssh .B lssh
will search Lastpass for an entry named `SSH: [ssh key name]' and autofill the password will search Lastpass for an entry named `SSH: [ssh key name]' and autofill the
with the results. If that entry is not found, `[ssh key name]' will also be attempted password with the results. If that entry is not found, `[ssh key name]' will
before giving up on a password search. The key will be passed to the ssh-agent also be attempted before giving up on a password search. The key will be passed
regardless. to the ssh-agent regardless.
.SH OPTIONS .SH OPTIONS
.TP .TP
.B -h .B -h
@ -23,7 +27,31 @@ Shows simple help.
.TP .TP
.B -v .B -v
Shows current version number. 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 .SH ENVIRONMENT VARIABLES
Environment variables will override the default settings and any configuration
file settings.
.TP .TP
.B SSH_KEY_LOCATIONS .B SSH_KEY_LOCATIONS
List of folders containing ssh keys. 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. searched in order and the search stops at the first successful match.
Unless otherwise defined, this variable defaults to ~/.ssh/ Unless otherwise defined, this variable defaults to ~/.ssh/
This is the same as setting `key_locations' in the configuration file.
.TP .TP
.B LASTPASS_USER .B LASTPASS_USER
Login username to Lastpass. This is used to initiate a login if you are not Login username to Lastpass. This is used to initiate a login if you are not
already logged in when initiating lssh already logged in when initiating lssh
This is the same as setting `lastpass_user' in the configuration file.
.SH EXAMPLES .SH EXAMPLES
If you have an ssh key located at ~/.ssh/work/id_rsa, it can be loaded by If you have an ssh key located at ~/.ssh/work/id_rsa, it can be loaded by
entering: entering:
$ lssh work $ lssh work
.SH DEPENDENCIES .SH DEPENDENCIES
.TP .TP
.B lpass .B lpass
@ -49,5 +83,6 @@ The Lastpass CLI client
.TP .TP
.B expect .B expect
programmed dialogue with interactive programs programmed dialogue with interactive programs
.SH AUTHOR .SH AUTHOR
James Tomasino James Tomasino