secrets: show the token masked (first/last 10 chars + length)

Makes a truncated or doubled paste visible at a glance without printing
the whole token.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-18 11:57:16 -04:00
co-authored by Claude Opus 5
parent 5bc8a27f60
commit cb7ab17769
+11 -1
View File
@@ -81,6 +81,14 @@ ask() {
printf '%s' "${_ans:-$2}" printf '%s' "${_ans:-$2}"
} }
# mask <secret> — first 10 and last 10 characters plus the length, so a
# truncated or doubled paste is visible without printing the whole token.
mask() {
printf '%s' "$1" | awk '{ n = length($0)
if (n <= 20) printf "%s (%d chars)", "(too short)", n
else printf "%s**%s (%d chars)", substr($0, 1, 10), substr($0, n - 9), n }'
}
if [ -z "$OP_SERVICE_ACCOUNT_TOKEN" ] && [ -f "$SECRETS_ENV" ]; then if [ -z "$OP_SERVICE_ACCOUNT_TOKEN" ] && [ -f "$SECRETS_ENV" ]; then
say "secrets: no service-account token; keeping existing $SECRETS_ENV." say "secrets: no service-account token; keeping existing $SECRETS_ENV."
exit 0 exit 0
@@ -95,8 +103,10 @@ while :; do
op whoami >/dev/null 2>&1 && break op whoami >/dev/null 2>&1 && break
if [ "$_tries" -eq 0 ]; then if [ "$_tries" -eq 0 ]; then
say "secrets: 1Password REJECTED the service-account token $_token_src (bad paste, revoked or expired?)." say "secrets: 1Password REJECTED the service-account token $_token_src (bad paste, revoked or expired?)."
say " token: $(mask "$OP_SERVICE_ACCOUNT_TOKEN")"
else else
say "secrets: 1Password REJECTED that token — most likely a truncated or bad paste." say "secrets: 1Password REJECTED that token — most likely a truncated or bad paste."
say " token: $(mask "$OP_SERVICE_ACCOUNT_TOKEN")"
fi fi
fi fi
if ! have_tty; then if ! have_tty; then
@@ -118,7 +128,7 @@ while :; do
OP_SERVICE_ACCOUNT_TOKEN="$(printf '%s' "$OP_SERVICE_ACCOUNT_TOKEN" | tr -d ' \t\r\n')" OP_SERVICE_ACCOUNT_TOKEN="$(printf '%s' "$OP_SERVICE_ACCOUNT_TOKEN" | tr -d ' \t\r\n')"
[ -n "$OP_SERVICE_ACCOUNT_TOKEN" ] || say "secrets: no token entered." [ -n "$OP_SERVICE_ACCOUNT_TOKEN" ] || say "secrets: no token entered."
done done
say "secrets: service-account token OK." say "secrets: service-account token OK: $(mask "$OP_SERVICE_ACCOUNT_TOKEN")"
# pick <kind> <current> <default> <check-fn> <list-cmd...> # pick <kind> <current> <default> <check-fn> <list-cmd...>
# Resolve a vault or item name: keep <current> if it checks out; otherwise use # Resolve a vault or item name: keep <current> if it checks out; otherwise use