From 7fc1aa2e24296921dd145f8328e3085b9450bded Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 15 May 2020 22:06:06 +0200 Subject: [PATCH] [gopass] Refactor configuration options Configuration values can now be set through either environment variables, or a configuration file. Environment variables have their own special syntax (being prefixed with RGP_), and will take precedence over configuration files. The configuration file is also parsed, not sourced, so that no stray code would be executed on loading them. --- gopass/.local/bin/rofi-gopass | 132 +++++++++++++++------------------- gopass/README.md | 3 +- 2 files changed, 60 insertions(+), 75 deletions(-) diff --git a/gopass/.local/bin/rofi-gopass b/gopass/.local/bin/rofi-gopass index 013ff4b..b641915 100755 --- a/gopass/.local/bin/rofi-gopass +++ b/gopass/.local/bin/rofi-gopass @@ -2,80 +2,64 @@ # # Original inspiration from https://github.com/carnager/rofi-pass/ -# DEFAULT OPTIONS - -# typing tool used, xdotool compatible syntax should work (e.g. ydotool) -# BACKEND=xdotool - -# the complete typing chain autoentry uses -# possible fields: -# :tab | :space | :return | username | password -# -# Note that spaces have to be passed explicitly with :space, -# putting a literal ' ' in the chain will not work -# -# By default it enters the username, tabs down, and enters the password -# It does not press return at the end for safety, though you can add -# :return to make it do so. -# AUTOENTRY_CHAIN="username :tab password" - -# wait time before entering each input -# in milliseconds -# AUTOENTRY_DELAY=20 - -# default key bindings -# Automatically enter username and password -# KEY_AUTOFILL="Return" -# Open entry -# KEY_OPEN_ENTRY -# Automatically enter username only -# KEY_FILL_USER="Alt+u" -# Add username to clipboard -# KEY_CLIP_USER="Ctrl+Alt+u" -# Automatically enter password only -# KEY_FILL_PASS="Alt+p" -# Add password to clipboard -# KEY_CLIP_PASS="Ctrl+Alt+p" -# For the individual gopass entry: -# Automatically fill selected field -# KEY_ENTRYMENU_FILL="Return" -# Add field to clipboard -# KEY_ENTRYMENU_CLIP="Alt+Return" -# Return to main menu -# KEY_ENTRYMENU_QUIT="Alt+BackSpace" - -# The field name containing your gopass username -# can take multiple names, separated by space -# Will go through names in descending precedence -# GOPASS_USERNAME_FIELD="username user login" - -# The location of the rofi-gopass config file -# ROFI_PASS_CONFIGURATION_FILE="~/.config/rofi-gopass" - # rofi wrapper. Add custom settings here. _rofi() { # TODO add check for dmenu - exist rofi critical "rofi-pass" || exit 0 + exist rofi critical "rofi-gopass" || exit 0 rofi -dmenu -no-auto-select -i "$@" -p "Entry" } +# parse, see https://unix.stackexchange.com/a/331965/8541 +_parse_config() { + (grep -E "^$2=" -m 1 "$1" 2>/dev/null || printf "VAR=__UNDEFINED__\n") | head -n1 | cut -d '=' -f 2- +} + # read config file -get_config_file() { - local locations=("$ROFI_PASS_CONFIGURATION_FILE" +get_config() { + local locations=( + "$RGP_CONFIGURATION_FILE" "${XDG_CONFIG_HOME:-$HOME/.config}/rofi-gopass/rofi-gopass.conf" "$HOME/.rofi-gopass.conf" - "/etc/rofi-gopass.conf") + "/etc/rofi-gopass.conf" + ) # return the first config file with a valid path for config in "${locations[@]}"; do if [[ -n "$config" && -f "$config" ]]; then - # we only source config values, don't complain - # shellcheck disable=SC1090 - source "$config" - echo "sourced config" - return + # see if the config has been given a value + local val + val="$(_parse_config "$config" "$1")" + break fi done + + # if there was a config file but no value + # or there was no config file at all + if [ "$val" = "__UNDEFINED__" ] || [ -z "$val" ]; then + val="$2" + fi + printf -- "%s" "$val" +} + +set_defaults() { + # The location of the rofi-gopass config file + # ROFI_PASS_CONFIGURATION_FILE="~/.config/rofi-gopass" + # set options, leaving already set environment variables intact + # try to read any settings from config files + KEY_AUTOFILL="${RGP_KEY_AUTOFILL:-$(get_config KEY_AUTOFILL Return)}" + KEY_ENTRY_OPEN="${RGP_KEY_ENTRY_OPEN:-$(get_config KEY_ENTRY_OPEN Alt+Return)}" + KEY_FILL_USER="${RGP_KEY_FILL_USER:-$(get_config KEY_FILL_USER Alt+u)}" + KEY_CLIP_USER="${RGP_KEY_CLIP_USER:-$(get_config KEY_CLIP_USER Ctrl+Alt+u)}" + KEY_FILL_PASS="${RGP_KEY_FILL_PASS:-$(get_config KEY_FILL_PASS Alt+p)}" + KEY_CLIP_PASS="${RGP_KEY_CLIP_PASS:-$(get_config KEY_CLIP_PASS Ctrl+Alt+p)}" + KEY_ENTRYMENU_FILL="${RGP_KEY_ENTRYMENU_FILL:-$(get_config KEY_ENTRYMENU_FILL Return)}" + KEY_ENTRYMENU_CLIP="${RGP_KEY_ENTRYMENU_CLIP:-$(get_config KEY_ENTRYMENU_CLIP Alt+Return)}" + KEY_ENTRYMENU_QUIT="${RGP_KEY_ENTRYMENU_QUIT:-$(get_config KEY_ENTRYMENU_QUIT Alt+BackSpace)}" + + BACKEND="${RGP_BACKEND:-$(get_config BACKEND xdotool)}" + GOPASS_USERNAME_FIELD="${GOPASS_USERNAME_FIELD:-$(get_config GOPASS_USERNAME_FIELD 'username user login')}" + AUTOENTRY_CHAIN="${RGP_AUTOENTRY_CHAIN:-$(get_config AUTOENTRY_CHAIN 'username :tab password')}" + AUTOENTRY_DELAY="${RGP_AUTOENTRY_DELAY:-$(get_config AUTOENTRY_DELAY 30)}" } # exit on escape pressed @@ -121,11 +105,11 @@ _gp_get_field() { # return username for argument passed show_username() { - _gp_get_field "$1" "${GOPASS_USERNAME_FIELD:-"username user login"}" + _gp_get_field "$1" "${GOPASS_USERNAME_FIELD}" } clip_username() { - _gp_get_field "$1" "${GOPASS_USERNAME_FIELD:-"username user login"}" "-c" + _gp_get_field "$1" "${GOPASS_USERNAME_FIELD}" "-c" } show_field() { @@ -142,11 +126,11 @@ list_fields() { # invoke the dotool to type inputs _type() { - local tool="${BACKEND:-xdotool}" + local tool="${BACKEND}" local toolmode="$1" local key="$2" - "$tool" "$toolmode" --delay "${AUTOENTRY_DELAY:-30}" "$key" + "$tool" "$toolmode" --delay "${AUTOENTRY_DELAY}" "$key" } # automatically enter entry chain, set via AUTOENTRY_CHAIN @@ -171,9 +155,9 @@ autofill() { # opens a menu for the specified gopass entry, containing its individual fields entrymenu() { local entry="$1" - local k_entrymenu_fill="${KEY_ENTRYMENU_FILL:-Return}" - local k_entrymenu_clip="${KEY_ENTRYMENU_CLIP:-Alt+Return}" - local k_entrymenu_quit="${KEY_ENTRYMENU_QUIT:-Alt+BackSpace}" + local k_entrymenu_fill="${KEY_ENTRYMENU_FILL}" + local k_entrymenu_clip="${KEY_ENTRYMENU_CLIP}" + local k_entrymenu_quit="${KEY_ENTRYMENU_QUIT}" local pass_obfuscation="(hidden)" @@ -216,13 +200,13 @@ entrymenu() { } main() { - local autoentry_chain="${AUTOENTRY_CHAIN:-username :tab password}" - local k_autofill="${KEY_AUTOFILL:-Return}" - local k_fill_user="${KEY_FILL_USER:-Alt+u}" - local k_clip_user="${KEY_CLIP_USER:-Ctrl+Alt+u}" - local k_fill_pass="${KEY_FILL_PASS:-Alt+p}" - local k_clip_pass="${KEY_CLIP_PASS:-Ctrl+Alt+p}" - local k_submenu="${KEY_OPEN_ENTRY:-Alt+Return}" + local autoentry_chain="${AUTOENTRY_CHAIN}" + local k_autofill="${KEY_AUTOFILL}" + local k_fill_user="${KEY_FILL_USER}" + local k_clip_user="${KEY_CLIP_USER}" + local k_fill_pass="${KEY_FILL_PASS}" + local k_clip_pass="${KEY_CLIP_PASS}" + local k_submenu="${KEY_ENTRY_OPEN}" entry="$( list_passwords | @@ -266,5 +250,5 @@ main() { esac } -get_config_file +set_defaults main diff --git a/gopass/README.md b/gopass/README.md index 0b7248c..babf52a 100644 --- a/gopass/README.md +++ b/gopass/README.md @@ -33,8 +33,9 @@ These keys, as well as the additional configuration can be changed by setting th * `~/.rofi-gopass.conf` * `/etc/rofi-gopass.conf` -or, alternatively, a custom directory if the `ROFI_PASS_CONFIGURATION_FILE` variable points to a configuration file. +or, alternatively, a custom directory if the `RGP_CONFIGURATION_FILE` variable points to a configuration file. +To use environment variables to configure any of these options or keys, prefix them with `RGP_`, so that e.g. `KEY_AUTOFILL` becomes `RGP_KEY_AUTOFILL`. Environment variables take precedence over configuration file settings. Additional configuration options: