| 1 | #!/bin/bash |
| 2 | # Store last30days API keys in the macOS Keychain. |
| 3 | # |
| 4 | # Keys are stored as generic passwords with service name `last30days-<KEY>` |
| 5 | # for the current user. The lib/env.py loader picks them up automatically as |
| 6 | # the lowest-priority credential source on Darwin. |
| 7 | # |
| 8 | # Usage: |
| 9 | # ./setup-keychain.sh # interactive: prompts for each key |
| 10 | # ./setup-keychain.sh KEY [KEY..] # prompt only for the listed keys |
| 11 | # ./setup-keychain.sh --list # list which last30days-* items exist |
| 12 | # ./setup-keychain.sh --delete KEY # remove a stored key |
| 13 | # |
| 14 | # Existing values are shown as "(set)" and skipped unless --replace is passed. |
| 15 | # Skip any prompt with empty input. |
| 16 | |
| 17 | set -euo pipefail |
| 18 | |
| 19 | PREFIX="last30days-" |
| 20 | # Mirrors lib/env.py::KEYCHAIN_KEYS — kept in sync via |
| 21 | # tests/test_env_keychain.py::test_keychain_keys_match_setup_script. |
| 22 | ALL_KEYS=( |
| 23 | OPENAI_API_KEY |
| 24 | XAI_API_KEY |
| 25 | GOOGLE_API_KEY |
| 26 | GEMINI_API_KEY |
| 27 | GOOGLE_GENAI_API_KEY |
| 28 | SCRAPECREATORS_API_KEY |
| 29 | APIFY_API_TOKEN |
| 30 | AUTH_TOKEN |
| 31 | CT0 |
| 32 | BSKY_HANDLE |
| 33 | BSKY_APP_PASSWORD |
| 34 | TRUTHSOCIAL_TOKEN |
| 35 | BRAVE_API_KEY |
| 36 | EXA_API_KEY |
| 37 | SERPER_API_KEY |
| 38 | OPENROUTER_API_KEY |
| 39 | PERPLEXITY_API_KEY |
| 40 | PARALLEL_API_KEY |
| 41 | XQUIK_API_KEY |
| 42 | XIAOHONGSHU_API_BASE |
| 43 | GITHUB_TOKEN |
| 44 | BRIGHTDATA_API_KEY |
| 45 | X_BEARER_TOKEN |
| 46 | DEEPLINE_API_KEY |
| 47 | ) |
| 48 | |
| 49 | if [[ "${OSTYPE:-}" != darwin* ]]; then |
| 50 | echo "setup-keychain.sh requires macOS (security command). Got: ${OSTYPE:-}" >&2 |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | if [[ -x /usr/bin/security ]]; then |
| 55 | SECURITY_BIN="/usr/bin/security" |
| 56 | else |
| 57 | SECURITY_BIN="${LAST30DAYS_TEST_SECURITY_BIN:-/usr/bin/security}" |
| 58 | fi |
| 59 | if [[ "$SECURITY_BIN" != /* || ! -x "$SECURITY_BIN" ]]; then |
| 60 | echo "security binary not found at ${SECURITY_BIN}" >&2 |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
| 64 | matches_regex() { |
| 65 | local LC_ALL=C |
| 66 | local value="$1" |
| 67 | local pattern="$2" |
| 68 | [[ "$value" =~ $pattern ]] |
| 69 | } |
| 70 | |
| 71 | USER_NAME="${USER:-}" |
| 72 | if [[ -z "$USER_NAME" && -x /usr/bin/id ]]; then |
| 73 | USER_NAME="$(/usr/bin/id -un 2>/dev/null || true)" |
| 74 | fi |
| 75 | if [[ -z "$USER_NAME" ]]; then |
| 76 | echo "Unable to determine current username (set USER)." >&2 |
| 77 | exit 1 |
| 78 | fi |
| 79 | cntrl_re='[[:cntrl:]]' |
| 80 | if [[ "$USER_NAME" == *$'\n'* || "$USER_NAME" == *$'\r'* ]] || matches_regex "$USER_NAME" "$cntrl_re"; then |
| 81 | echo "Invalid USER value." >&2 |
| 82 | exit 1 |
| 83 | fi |
| 84 | |
| 85 | is_valid_key() { |
| 86 | local candidate="$1" |
| 87 | local allowed |
| 88 | for allowed in "${ALL_KEYS[@]}"; do |
| 89 | if [[ "$candidate" == "$allowed" ]]; then |
| 90 | return 0 |
| 91 | fi |
| 92 | done |
| 93 | return 1 |
| 94 | } |
| 95 | |
| 96 | hex_encode() { |
| 97 | local LC_ALL=C |
| 98 | local s="$1" |
| 99 | local i ch n h out="" |
| 100 | for (( i = 0; i < ${#s}; i++ )); do |
| 101 | ch="${s:i:1}" |
| 102 | printf -v n "%d" "'${ch}" |
| 103 | printf -v h "%02x" $(( n & 0xff )) |
| 104 | out+="${h}" |
| 105 | done |
| 106 | HEX_VALUE="${out}" |
| 107 | } |
| 108 | |
| 109 | REPLACE=0 |
| 110 | ACTION="prompt" |
| 111 | TARGETS=() |
| 112 | |
| 113 | while [[ $# -gt 0 ]]; do |
| 114 | case "$1" in |
| 115 | --list) ACTION="list"; shift ;; |
| 116 | --delete) ACTION="delete"; shift ;; |
| 117 | --replace) REPLACE=1; shift ;; |
| 118 | --help|-h) sed -n '2,/^$/p' "$0" | sed 's/^# //; s/^#//'; exit 0 ;; |
| 119 | --) shift; if [[ $# -gt 0 ]]; then TARGETS+=("$@"); fi; break ;; |
| 120 | -*) echo "unknown flag: $1" >&2; exit 2 ;; |
| 121 | *) TARGETS+=("$1"); shift ;; |
| 122 | esac |
| 123 | done |
| 124 | |
| 125 | if [[ ${#TARGETS[@]} -gt 0 ]]; then |
| 126 | for key in "${TARGETS[@]}"; do |
| 127 | if ! is_valid_key "$key"; then |
| 128 | echo "unknown or invalid key: $key" >&2 |
| 129 | exit 2 |
| 130 | fi |
| 131 | done |
| 132 | fi |
| 133 | |
| 134 | case "$ACTION" in |
| 135 | list) |
| 136 | echo "Stored last30days-* keychain items:" |
| 137 | for key in "${ALL_KEYS[@]}"; do |
| 138 | # Existence checks do not need to decrypt or return the password. |
| 139 | if "$SECURITY_BIN" find-generic-password -a "$USER_NAME" -s "${PREFIX}${key}" >/dev/null 2>&1; then |
| 140 | echo " $key" |
| 141 | fi |
| 142 | done |
| 143 | exit 0 |
| 144 | ;; |
| 145 | delete) |
| 146 | if [[ ${#TARGETS[@]} -eq 0 ]]; then |
| 147 | echo "--delete needs at least one KEY name" >&2; exit 2 |
| 148 | fi |
| 149 | for key in "${TARGETS[@]}"; do |
| 150 | if "$SECURITY_BIN" delete-generic-password -a "$USER_NAME" -s "${PREFIX}${key}" >/dev/null 2>&1; then |
| 151 | echo "deleted: $key" |
| 152 | else |
| 153 | echo "not found: $key" |
| 154 | fi |
| 155 | done |
| 156 | exit 0 |
| 157 | ;; |
| 158 | esac |
| 159 | |
| 160 | if [[ ${#TARGETS[@]} -eq 0 ]]; then |
| 161 | TARGETS=("${ALL_KEYS[@]}") |
| 162 | fi |
| 163 | |
| 164 | escaped_user="${USER_NAME//\\/\\\\}" |
| 165 | escaped_user="${escaped_user//\"/\\\"}" |
| 166 | |
| 167 | added=0; skipped=0; replaced=0 |
| 168 | for key in "${TARGETS[@]}"; do |
| 169 | if "$SECURITY_BIN" find-generic-password -a "$USER_NAME" -s "${PREFIX}${key}" >/dev/null 2>&1; then |
| 170 | existed=1 |
| 171 | else |
| 172 | existed=0 |
| 173 | fi |
| 174 | if [[ "$existed" -eq 1 && "$REPLACE" -eq 0 ]]; then |
| 175 | printf " %-28s (set, skipping — use --replace to overwrite)\n" "$key" |
| 176 | skipped=$((skipped + 1)) |
| 177 | continue |
| 178 | fi |
| 179 | printf " %-28s " "$key" |
| 180 | value="" |
| 181 | if ! IFS= read -rs value && [[ -z "$value" ]]; then |
| 182 | echo |
| 183 | skipped=$((skipped + 1)) |
| 184 | continue |
| 185 | fi |
| 186 | echo |
| 187 | if [[ -z "$value" ]]; then |
| 188 | skipped=$((skipped + 1)) |
| 189 | continue |
| 190 | fi |
| 191 | # Hex-encode in pure bash builtins and pipe via stdin to `security -i` so the |
| 192 | # secret never enters argv or a bash-3.2 heredoc temp file in /tmp. |
| 193 | HEX_VALUE="" |
| 194 | hex_encode "$value" |
| 195 | printf 'add-generic-password -U -a "%s" -s "%s" -X %s\n' \ |
| 196 | "$escaped_user" "${PREFIX}${key}" "$HEX_VALUE" | "$SECURITY_BIN" -i |
| 197 | value="" |
| 198 | HEX_VALUE="" |
| 199 | if [[ "$existed" -eq 1 ]]; then |
| 200 | replaced=$((replaced + 1)) |
| 201 | else |
| 202 | added=$((added + 1)) |
| 203 | fi |
| 204 | done |
| 205 | |
| 206 | echo |
| 207 | echo "Done. added=$added replaced=$replaced skipped=$skipped" |
| 208 | echo "Verify with: $0 --list" |
| 209 |