Skip to content
pi-c Artifact [e653679b2c]
Login

Artifact [e653679b2c]

Artifact e653679b2cdf1458cc7151f27a94030f797393934556569d5e31f2293427ba11:


#!/bin/sh
# What is left on a key and how old that is, when it expires, and keys from a command, against a fake
# server with a private HOME: an expiry recorded (pic auth expires, --expires, RPC) or read from the
# provider's free check, shown with its source, warned of on stderr within keyExpiryWarnDays,
# pic auth status --expiring for a cron job, gone with a new value; a key whose value comes from a
# command that runs when a request needs it and again when the key is refused, whose new value is
# noted as rotated, whose failure stops the request with one line; the offline setting sending no
# check. No key text is stored or printed. --no-faults (the release build): skipped, since the
# passphrase comes from PIC_TEST_PASSPHRASE, which only the dev build reads.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
SQLITE3=${SQLITE3:-sqlite3}
if [ "${1:-}" = --no-faults ]; then
    printf 'SKIP: keyexpiry needs the dev build (PIC_TEST_PASSPHRASE)\n'
    exit 0
fi
S=$(cd "$(dirname "$0")/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-keyexpiry.XXXXXX")
T=$(cd "$T" && pwd -P)
spid=
cleanup() { [ -n "$spid" ] && kill "$spid" 2>/dev/null || :; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done
mkdir "$T/home" "$T/ws" "$T/out"
export HOME="$T/home" PIC_PI_EXPORT=0 PIC_MODEL_CATALOG=offline PIC_MODELS_FILE="$T/models.json"
export PIC_TEST_PASSPHRASE="pic-ke-passphrase-$$"
K1="pic-ke-one-$$-a1"
K2="pic-ke-two-$$-b2"
round=0
serve() { # SPEC...: a fresh server, its log in $T/log
    [ -n "$spid" ] && kill "$spid" 2>/dev/null || :
    [ -n "$spid" ] && wait "$spid" 2>/dev/null || :
    rm -rf "$T/log" "$T/port"
    mkdir "$T/log"
    "$SERVER" "$T/port" "$T/log" 60 "$@" &
    spid=$!
    i=0
    while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
    port=$(cat "$T/port")
    cat > "$T/models.json" <<EOM
{"providers":{"fake":{"baseUrl":"http://127.0.0.1:$port","api":"anthropic-messages","apiKey":"\$FAKE_API_KEY","models":[
  {"id":"m","contextWindow":200000}]},
 "openrouter":{"baseUrl":"http://127.0.0.1:$port/api/v1"}}}
EOM
}
pic() { # NAME ARGS...: stdout and stderr in $T/out/NAME.{out,err}
    name=$1
    shift
    "$PIC" --db "$T/j" --workspace "$T/ws" "$@" > "$T/out/$name.out" 2> "$T/out/$name.err" < /dev/null
}
requests() { ls "$T/log" | wc -l | tr -d ' '; }
sent_key() { sed -n 's/^x-api-key: //p' "$T/log/$1.req" | tr -d '\r'; }

serve "sse:$S/text.sse"
printf '%s\n' "$K1" | "$PIC" --db "$T/j" --workspace "$T/ws" login fake > /dev/null 2> "$T/out/l1.err" || fail "login: $(cat "$T/out/l1.err")"
printf '%s\n' "$K2" | "$PIC" --db "$T/j" --workspace "$T/ws" auth login fake --api-key --as second --expires 2027-01-31 --json \
    > "$T/out/l2.out" 2> "$T/out/l2.err" || fail "login --expires: $(cat "$T/out/l2.err")"

# Recorded, shown with its source, and changed or removed by pic auth expires.
pic st1 auth status --json fake || fail "status: $(cat "$T/out/st1.err")"
grep -qF '"label":"second","kind":"api_key"' "$T/out/st1.out" || fail "status: $(cat "$T/out/st1.out")"
grep -qF '"expires_at":"2027-01-31T23:59:59Z"' "$T/out/st1.out" && grep -qF '"expiry_source":"recorded"' "$T/out/st1.out" ||
    fail "--expires: $(cat "$T/out/st1.out")"
pic ex1 auth expires fake 3d --json || fail "expires: $(cat "$T/out/ex1.err")"
grep -qF '"provider":"fake","label":"default","expires_at":"' "$T/out/ex1.out" || fail "expires: $(cat "$T/out/ex1.out")"
pic st2 auth status fake || fail "status: $(cat "$T/out/st2.err")"
grep -q 'default\*.*expires in 3 days (.*, recorded)' "$T/out/st2.out" || fail "status table: $(cat "$T/out/st2.out")"
rc=0
pic bad auth expires fake someday || rc=$?
[ "$rc" = 1 ] || fail "a bad date was taken"
printf 'PASS: an expiry is recorded with --expires and pic auth expires, and shown with its source\n'

# Warned of within keyExpiryWarnDays, on stderr, once; the request still goes.
pic w1 --model fake/m -p 'soon' || fail "soon: $(cat "$T/out/w1.err")"
grep -qF 'pic: the key "default" for fake expires in 3 days' "$T/out/w1.err" || fail "warning: $(cat "$T/out/w1.err")"
[ "$(grep -c 'expires in' "$T/out/w1.err")" = 1 ] || fail "warned more than once: $(cat "$T/out/w1.err")"
pic set1 settings keyExpiryWarnDays 2 || fail "setting: $(cat "$T/out/set1.err")"
pic w2 --model fake/m -p 'later' || fail "later: $(cat "$T/out/w2.err")"
! grep -qF 'expires in' "$T/out/w2.err" || fail "warned outside keyExpiryWarnDays: $(cat "$T/out/w2.err")"
pic ex2 auth expires fake 2020-01-01 || fail "expires past: $(cat "$T/out/ex2.err")"
pic w3 --model fake/m -p 'expired' || fail "expired: $(cat "$T/out/w3.err")"
grep -qF 'pic: the key "default" for fake expired 01 Jan (recorded)' "$T/out/w3.err" || fail "expired warning: $(cat "$T/out/w3.err")"
printf 'PASS: an expiry near or past is said on stderr, within keyExpiryWarnDays only, and the request still goes\n'

# --expiring DAYS: 1 when a key in use expires within it, for a cron job.
rc=0
pic soon auth status --expiring 7 || rc=$?
[ "$rc" = 1 ] || fail "--expiring with an expired key: exit $rc"
pic ex3 auth expires fake never || fail "never: $(cat "$T/out/ex3.err")"
rc=0
pic soon2 auth status --expiring 7 --json || rc=$?
[ "$rc" = 0 ] || fail "--expiring with no expiry near: exit $rc $(cat "$T/out/soon2.err")"
printf 'PASS: pic auth status --expiring exits 1 only when a key in use expires within it\n'

# A new value forgets the old expiry; the same key again keeps it. RPC records one too.
pic ex4 auth expires fake:second 30d || fail "expires second: $(cat "$T/out/ex4.err")"
printf '%s\n' '{"id":"a","type":"add_credential","provider":"fake","label":"second","key":"'"$K2"'"}' \
    '{"id":"b","type":"get_credentials","provider":"fake"}' |
    "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m --mode rpc > "$T/out/r1.out" 2> "$T/out/r1.err" || fail "rpc: $(cat "$T/out/r1.err")"
grep '"id":"b"' "$T/out/r1.out" | grep -q '"label":"second"[^}]*"expiry_source":"recorded"' || fail "same key: $(cat "$T/out/r1.out")"
printf '%s\n' '{"id":"a","type":"add_credential","provider":"fake","label":"second","key":"'"$K2"'-new"}' \
    '{"id":"b","type":"get_credentials","provider":"fake"}' \
    '{"id":"c","type":"set_credential_expiry","provider":"fake","label":"second","expires_at":"2028-02-29"}' \
    '{"id":"d","type":"add_credential","provider":"fake","label":"third","key":"'"$K1"'-3","expires_at":"10d"}' \
    '{"id":"e","type":"get_credentials","provider":"fake"}' |
    "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m --mode rpc > "$T/out/r2.out" 2> "$T/out/r2.err" || fail "rpc: $(cat "$T/out/r2.err")"
! grep '"id":"b"' "$T/out/r2.out" | grep -q '"label":"second"[^}]*"expires_at"' || fail "new value kept the expiry: $(cat "$T/out/r2.out")"
grep '"id":"c"' "$T/out/r2.out" | grep -qF '"expires_at":"2028-02-29T23:59:59Z","expiry_source":"recorded"' ||
    fail "set_credential_expiry: $(cat "$T/out/r2.out")"
grep '"id":"e"' "$T/out/r2.out" | grep -q '"label":"third"[^}]*"days_left":10' || fail "add_credential expires_at: $(cat "$T/out/r2.out")"
printf 'PASS: a new value forgets its expiry, the same one keeps it; RPC records one\n'

# The provider's own word: OpenRouter's free check gives its expiry, and what is left with its age.
printf '{"data":{"label":"k","limit":10,"limit_remaining":4.12,"expires_at":"2026-12-31T00:00:00Z"}}' > "$T/or.json"
serve "at:/api/v1/key=status:200:$T/or.json" "sse:$S/text.sse"
printf '%s\n' "pic-ke-or-$$" | "$PIC" --db "$T/j" --workspace "$T/ws" login openrouter > /dev/null 2> "$T/out/lo.err" ||
    fail "login openrouter: $(cat "$T/out/lo.err")"
pic chk auth status --check --json openrouter || fail "check: $(cat "$T/out/chk.err")"
grep -qF '"expires_at":"2026-12-31T00:00:00Z"' "$T/out/chk.out" && grep -qF '"expiry_source":"provider"' "$T/out/chk.out" ||
    fail "provider expiry: $(cat "$T/out/chk.out")"
grep -qF '"left":{"kind":"money","amount":4.12,"currency":"$","source":"check","as_of":"' "$T/out/chk.out" || fail "left: $(cat "$T/out/chk.out")"
grep -qF '"stale":false}' "$T/out/chk.out" || fail "left age: $(cat "$T/out/chk.out")"
printf 'PASS: a free check reads the provider'"'"'s expiry, and what is left carries its source and age\n'

# offline: no check goes out.
pic off settings offline true || fail "offline: $(cat "$T/out/off.err")"
before=$(requests)
pic chk2 auth status --refresh --json openrouter || :
[ "$(requests)" = "$before" ] || fail "offline: a check went out"
pic on settings offline false || fail "online: $(cat "$T/out/on.err")"
printf 'PASS: offline sends no check\n'

# A key from a command: run when needed, the command stored and never its output.
serve "sse:$S/text.sse"
printf 'pic-ke-cmd-%s\n' "$$" > "$T/keyfile"
pic c1 auth login fake --as vault --command "cat '$T/keyfile'" --json || fail "--command: $(cat "$T/out/c1.err")"
grep -qxF '{"provider":"fake","label":"vault","kind":"command","changed":true}' "$T/out/c1.out" || fail "--command: $(cat "$T/out/c1.out")"
pic c2 --model fake/m --key vault -p 'from a command' || fail "command key: $(cat "$T/out/c2.err")"
[ "$(sent_key "$(requests)")" = "pic-ke-cmd-$$" ] || fail "command key: sent $(sent_key "$(requests)")"
pic c3 auth status --json fake || fail "status: $(cat "$T/out/c3.err")"
grep -qF '"label":"vault","kind":"command"' "$T/out/c3.out" || fail "command kind: $(cat "$T/out/c3.out")"
! grep -aqF "pic-ke-cmd-$$" "$HOME/.pic/pic.sqlite" || fail "the command's output was stored"
printf 'PASS: a key from a command is run when a request needs it, and its output is never stored\n'

# Refused: the command runs again, and a new value is sent once more and noted as rotated.
printf '{"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"}}' > "$T/auth.json"
serve "status:401:$T/auth.json" "sse:$S/text.sse"
cat > "$T/rot.sh" <<EOS
n=\$(cat "$T/rot.n" 2>/dev/null || echo 0); n=\$((n + 1)); echo \$n > "$T/rot.n"; echo "pic-ke-rot-$$-\$n"
EOS
pic c4 auth login fake --as rot --command "sh '$T/rot.sh'" || fail "--command rot: $(cat "$T/out/c4.err")"
pic c5 --model fake/m --key rot -p 'rotated' || fail "rotated: $(cat "$T/out/c5.err")"
[ "$(requests)" = 2 ] || fail "rotated: $(requests) requests, wanted the refused one and one again"
[ "$(sent_key 1)" = "pic-ke-rot-$$-1" ] && [ "$(sent_key 2)" = "pic-ke-rot-$$-2" ] || fail "rotated: sent $(sent_key 1) then $(sent_key 2)"
pic c6 auth status --json fake || fail "status: $(cat "$T/out/c6.err")"
grep -q '"label":"rot"[^}]*"rotated_at":"' "$T/out/c6.out" || fail "rotated_at: $(cat "$T/out/c6.out")"
printf 'PASS: a refused command key runs again, a new value goes once more and is noted as rotated\n'

# A command that fails stops the request with one line, and nothing is sent.
pic c7 auth login fake --as broken --command 'echo vault locked >&2; exit 3' || fail "--command broken: $(cat "$T/out/c7.err")"
before=$(requests)
rc=0
pic c8 --model fake/m --key broken -p 'broken' || rc=$?
[ "$rc" != 0 ] || fail "broken: exit 0"
[ "$(requests)" = "$before" ] || fail "broken: a request was sent"
grep -qF 'the key command failed (exit 3): vault locked' "$T/out/c8.err" || fail "broken: $(cat "$T/out/c8.err")"
printf 'PASS: a failing key command stops the request with its own words, and nothing is sent\n'

# A command key under a login agent's bare environment (launchd gives PATH=/usr/bin:/bin:/usr/sbin:/sbin):
# the command still finds what Homebrew and /usr/local install, as a terminal's would.
serve "sse:$S/text.sse"
pic c9 auth login fake --as bare --command 'p=":$PATH:"; case "$p" in *:/opt/homebrew/bin:*) ;; *) exit 4 ;; esac; case "$p" in *:/usr/local/bin:*) echo pic-ke-bare-key ;; *) exit 5 ;; esac' ||
    fail "--command bare: $(cat "$T/out/c9.err")"
env -i HOME="$HOME" PATH=/usr/bin:/bin:/usr/sbin:/sbin PIC_TEST_PASSPHRASE="$PIC_TEST_PASSPHRASE" PIC_PI_EXPORT=0 \
    PIC_MODEL_CATALOG=offline PIC_MODELS_FILE="$PIC_MODELS_FILE" "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m --key bare \
    -p 'bare' > "$T/out/c10.out" 2> "$T/out/c10.err" < /dev/null || fail "bare environment: $(cat "$T/out/c10.err")"
[ "$(sent_key "$(requests)")" = pic-ke-bare-key ] || fail "bare environment: sent $(sent_key "$(requests)")"
printf 'PASS: a key command run with a bare PATH still finds /opt/homebrew/bin and /usr/local/bin\n'

if grep -rlF -e "$K1" -e "$K2" "$T/out" 2>/dev/null | grep -q .; then
    fail "a key was printed: $(grep -rlF -e "$K1" -e "$K2" "$T/out")"
fi
printf 'PASS: no output carries a key\n'