| 1 | #!/usr/bin/env bash |
| 2 | # Store last30days API keys in a pass(1) store. |
| 3 | # |
| 4 | # Keys are stored at pass path `last30days/<KEY>` (the Linux/Unix analog of the |
| 5 | # Keychain `last30days-<KEY>` convention). The lib/env.py loader picks them up |
| 6 | # automatically as a lowest-priority credential source wherever `pass` exists. |
| 7 | # Honors PASSWORD_STORE_DIR; override the path prefix with LAST30DAYS_PASS_PREFIX |
| 8 | # (must match what the loader uses). |
| 9 | # |
| 10 | # Usage: |
| 11 | # ./setup-pass.sh # interactive: prompts for each key |
| 12 | # ./setup-pass.sh KEY [KEY..] # prompt only for the listed keys |
| 13 | # ./setup-pass.sh --list # list which last30days/* entries exist |
| 14 | # ./setup-pass.sh --delete KEY # remove a stored key |
| 15 | # |
| 16 | # Existing values are shown as "(set)" and skipped unless --replace is passed. |
| 17 | # Skip any prompt with empty input. |
| 18 | |
| 19 | set -euo pipefail |
| 20 | |
| 21 | PREFIX="${LAST30DAYS_PASS_PREFIX:-last30days/}" |
| 22 | STORE_DIR="${PASSWORD_STORE_DIR:-${HOME}/.password-store}" |
| 23 | # Mirrors lib/env.py::KEYCHAIN_KEYS — kept in sync via |
| 24 | # tests/test_env_pass.py::test_pass_keys_match_setup_script. |
| 25 | ALL_KEYS=( |
| 26 | OPENAI_API_KEY |
| 27 | XAI_API_KEY |
| 28 | GOOGLE_API_KEY |
| 29 | GEMINI_API_KEY |
| 30 | GOOGLE_GENAI_API_KEY |
| 31 | SCRAPECREATORS_API_KEY |
| 32 | APIFY_API_TOKEN |
| 33 | AUTH_TOKEN |
| 34 | CT0 |
| 35 | BSKY_HANDLE |
| 36 | BSKY_APP_PASSWORD |
| 37 | TRUTHSOCIAL_TOKEN |
| 38 | BRAVE_API_KEY |
| 39 | EXA_API_KEY |
| 40 | SERPER_API_KEY |
| 41 | OPENROUTER_API_KEY |
| 42 | PERPLEXITY_API_KEY |
| 43 | PARALLEL_API_KEY |
| 44 | XQUIK_API_KEY |
| 45 | XIAOHONGSHU_API_BASE |
| 46 | GITHUB_TOKEN |
| 47 | BRIGHTDATA_API_KEY |
| 48 | X_BEARER_TOKEN |
| 49 | DEEPLINE_API_KEY |
| 50 | ) |
| 51 | |
| 52 | REPLACE=0 |
| 53 | ACTION="prompt" |
| 54 | TARGETS=() |
| 55 | |
| 56 | while [[ $# -gt 0 ]]; do |
| 57 | case "$1" in |
| 58 | --list) ACTION="list"; shift ;; |
| 59 | --delete) ACTION="delete"; shift ;; |
| 60 | --replace) REPLACE=1; shift ;; |
| 61 | --help|-h) sed -n '2,/^$/p' "$0" | sed 's/^# //; s/^#//'; exit 0 ;; |
| 62 | --) shift; if [[ $# -gt 0 ]]; then TARGETS+=("$@"); fi; break ;; |
| 63 | -*) echo "unknown flag: $1" >&2; exit 2 ;; |
| 64 | *) TARGETS+=("$1"); shift ;; |
| 65 | esac |
| 66 | done |
| 67 | |
| 68 | matches_regex() { |
| 69 | local LC_ALL=C |
| 70 | local value="$1" |
| 71 | local pattern="$2" |
| 72 | [[ "$value" =~ $pattern ]] |
| 73 | } |
| 74 | |
| 75 | # Validate PREFIX: relative segments only (no leading '/', '-', or '.'; no '..' |
| 76 | # or hidden segments; no control chars or whitespace), used verbatim to match lib/env.py. |
| 77 | # Regexes live in variables because macOS Bash 3.2 rejects inline (...) in `=~`. |
| 78 | prefix_re='^[A-Za-z0-9_][A-Za-z0-9._-]*(/[A-Za-z0-9_][A-Za-z0-9._-]*)*/?$' |
| 79 | if [[ "$PREFIX" == *$'\n'* || "$PREFIX" == *$'\r'* ]] || ! matches_regex "$PREFIX" "$prefix_re"; then |
| 80 | echo "invalid LAST30DAYS_PASS_PREFIX: must be a relative path prefix like 'last30days/'" >&2 |
| 81 | exit 2 |
| 82 | fi |
| 83 | |
| 84 | key_re='^[A-Z0-9_]+$' |
| 85 | is_valid_key() { |
| 86 | local candidate="$1" |
| 87 | local allowed |
| 88 | if ! matches_regex "$candidate" "$key_re"; then |
| 89 | return 1 |
| 90 | fi |
| 91 | for allowed in "${ALL_KEYS[@]}"; do |
| 92 | if [[ "$candidate" == "$allowed" ]]; then |
| 93 | return 0 |
| 94 | fi |
| 95 | done |
| 96 | return 1 |
| 97 | } |
| 98 | |
| 99 | if [[ ${#TARGETS[@]} -gt 0 ]]; then |
| 100 | for key in "${TARGETS[@]}"; do |
| 101 | if ! is_valid_key "$key"; then |
| 102 | echo "unknown or invalid key: $key" >&2 |
| 103 | exit 2 |
| 104 | fi |
| 105 | done |
| 106 | fi |
| 107 | |
| 108 | # Checked after flag parsing so `--help` works on a box without pass installed. |
| 109 | if ! command -v pass >/dev/null 2>&1; then |
| 110 | echo "setup-pass.sh requires the pass(1) password manager (not found on PATH)." >&2 |
| 111 | exit 1 |
| 112 | fi |
| 113 | |
| 114 | case "$ACTION" in |
| 115 | list) |
| 116 | echo "Stored ${PREFIX}* pass entries:" |
| 117 | for key in "${ALL_KEYS[@]}"; do |
| 118 | if pass show -- "${PREFIX}${key}" >/dev/null 2>&1; then |
| 119 | echo " $key" |
| 120 | fi |
| 121 | done |
| 122 | exit 0 |
| 123 | ;; |
| 124 | delete) |
| 125 | if [[ ${#TARGETS[@]} -eq 0 ]]; then |
| 126 | echo "--delete needs at least one KEY name" >&2; exit 2 |
| 127 | fi |
| 128 | for key in "${TARGETS[@]}"; do |
| 129 | if pass rm -f -- "${PREFIX}${key}" >/dev/null 2>&1; then |
| 130 | echo "deleted: $key" |
| 131 | else |
| 132 | echo "not found: $key" |
| 133 | fi |
| 134 | done |
| 135 | exit 0 |
| 136 | ;; |
| 137 | esac |
| 138 | |
| 139 | if [[ ${#TARGETS[@]} -eq 0 ]]; then |
| 140 | TARGETS=("${ALL_KEYS[@]}") |
| 141 | fi |
| 142 | |
| 143 | added=0; skipped=0; replaced=0 |
| 144 | for key in "${TARGETS[@]}"; do |
| 145 | # Existence must not depend on GPG decryption or a successful pinentry prompt. |
| 146 | entry="${STORE_DIR}/${PREFIX}${key}.gpg" |
| 147 | if [[ -e "${entry}" || -L "${entry}" ]]; then |
| 148 | existing=1 |
| 149 | else |
| 150 | existing=0 |
| 151 | fi |
| 152 | if [[ "$existing" -eq 1 && "$REPLACE" -eq 0 ]]; then |
| 153 | printf " %-28s (set, skipping — use --replace to overwrite)\n" "$key" |
| 154 | skipped=$((skipped + 1)) |
| 155 | continue |
| 156 | fi |
| 157 | printf " %-28s " "$key" |
| 158 | value="" |
| 159 | if ! IFS= read -rs value && [[ -z "$value" ]]; then |
| 160 | echo |
| 161 | skipped=$((skipped + 1)) |
| 162 | continue |
| 163 | fi |
| 164 | echo |
| 165 | if [[ -z "$value" ]]; then |
| 166 | skipped=$((skipped + 1)) |
| 167 | continue |
| 168 | fi |
| 169 | # pass treats piped input as overwrite consent, even without --force. |
| 170 | if [[ "${REPLACE}" -eq 0 && (-e "${entry}" || -L "${entry}") ]]; then |
| 171 | printf " %-28s (set while prompting, skipping — use --replace to overwrite)\n" "${key}" |
| 172 | skipped=$((skipped + 1)) |
| 173 | continue |
| 174 | fi |
| 175 | # Don't let one failed insert (gpg misconfig, missing store key, disk) abort |
| 176 | # the whole batch under `set -e`; report it and move on. |
| 177 | insert_args=(-m) |
| 178 | if [[ "${REPLACE}" -eq 1 ]]; then |
| 179 | insert_args+=(-f) |
| 180 | fi |
| 181 | if ! printf '%s\n' "$value" | pass insert "${insert_args[@]}" -- "${PREFIX}${key}" >/dev/null; then |
| 182 | echo " failed: $key (pass insert error)" >&2 |
| 183 | skipped=$((skipped + 1)) |
| 184 | continue |
| 185 | fi |
| 186 | if [[ "$existing" -eq 1 ]]; then |
| 187 | replaced=$((replaced + 1)) |
| 188 | else |
| 189 | added=$((added + 1)) |
| 190 | fi |
| 191 | done |
| 192 | |
| 193 | echo |
| 194 | echo "Done. added=$added replaced=$replaced skipped=$skipped" |
| 195 | echo "Verify with: $0 --list" |
| 196 |