返回 CodeWhale
ohos-secrets-store-smoke.sh
根目录 / scripts / ohos / ohos-secrets-store-smoke.sh
1 #!/usr/bin/env sh
2 # Verify the file-backed secret store against a real directory, including mounts
3 # that cannot hold a 0600 mode or an advisory lock.
4 #
5 # The unit tests in crates/secrets cover both branches of the mode check, but no
6 # portable filesystem synthesises mode bits or refuses flock, so a test cannot
7 # build the branch that matters on HarmonyOS hmdfs. This script covers it by
8 # driving the real binary against a real directory instead of stubbing one, and
9 # it says which layer refused when something fails.
10 #
11 # Run it against each filesystem you care about and compare:
12 #
13 # scripts/ohos/ohos-secrets-store-smoke.sh /tmp
14 # scripts/ohos/ohos-secrets-store-smoke.sh "$HOME"
15 #
16 # On a HarmonyOS 2in1, the user directory (/storage/Users/currentUser, hmdfs)
17 # exercises the mode-ignoring and no-advisory-lock fallbacks, and /data (hmfs)
18 # proves the strict path still holds where mode bits are stored.
19 #
20 # Over a device connection, run it inside the device shell:
21 #
22 # hdc file send target/aarch64-unknown-linux-ohos/release/codewhale /data/local/tmp/codewhale
23 # hdc file send scripts/ohos/ohos-secrets-store-smoke.sh /data/local/tmp/smoke.sh
24 # hdc shell "chmod +x /data/local/tmp/codewhale /data/local/tmp/smoke.sh; \
25 # CODEWHALE_BIN=/data/local/tmp/codewhale sh /data/local/tmp/smoke.sh /data/local/tmp"
26 #
27 # Exit status:
28 # 0 every check passed
29 # 1 the secret store failed
30 # 2 bad usage
31 # 3 a different layer refused before the secret store was reached
32 set -eu
33
34 usage() {
35 cat >&2 <<'USAGE'
36 usage: scripts/ohos/ohos-secrets-store-smoke.sh <directory> [binary]
37
38 <directory> an existing, writable directory. The script creates and removes
39 its own subdirectory there; nothing else is touched.
40 [binary] the binary to test. Defaults to $CODEWHALE_BIN, then
41 target/debug/codewhale, then target/release/codewhale.
42 USAGE
43 exit 2
44 }
45
46 [ $# -ge 1 ] || usage
47 dir=${1%/}
48 bin=${2:-${CODEWHALE_BIN:-}}
49
50 if [ -z "$bin" ]; then
51 for candidate in target/debug/codewhale target/release/codewhale; do
52 if [ -x "$candidate" ]; then
53 bin=$candidate
54 break
55 fi
56 done
57 fi
58
59 fail() {
60 echo "FAIL: $*" >&2
61 exit 1
62 }
63
64 mode_of() {
65 stat -c '%a' "$1" 2>/dev/null || stat -f '%Lp' "$1" 2>/dev/null || echo '?'
66 }
67
68 [ -n "$bin" ] || fail "no binary found; pass one or set CODEWHALE_BIN"
69 [ -x "$bin" ] || fail "binary is not executable: $bin"
70 [ -d "$dir" ] || fail "not a directory: $dir"
71
72 home=$dir/codewhale-secrets-smoke.$$
73 log=$dir/codewhale-secrets-smoke.$$.log
74 trap 'rm -rf "$home"; rm -f "$log"' EXIT HUP INT TERM
75
76 mkdir -p "$home" || fail "cannot create $home"
77 echo "binary : $bin"
78 echo "version: $("$bin" --version 2>/dev/null | head -1)"
79 echo "home : $home"
80
81 # What this mount does with a mode it is asked to store. Create a file, try to
82 # narrow it to 0600, and read back what the mount kept. A filesystem that stores
83 # mode bits lands on 0600; a mount that synthesises them stays wider and either
84 # refuses the chmod or ignores it.
85 probe=$home/.mode-probe
86 : >"$probe" || fail "cannot create a file in $home"
87 probe_before=$(mode_of "$probe")
88 chmod 600 "$probe" 2>/dev/null || true
89 probe_after=$(mode_of "$probe")
90 rm -f "$probe"
91 echo "mount : $dir"
92 echo " file created at $probe_before; after chmod 600 it is $probe_after"
93 case $probe_after in
94 600 | 400 | 0)
95 mount_note='stores mode bits'
96 echo ' -> stores mode bits; the strict path is expected'
97 ;;
98 *)
99 mount_note='synthesises mode bits'
100 echo ' -> does not store mode bits; the fallback path is expected'
101 ;;
102 esac
103 echo
104
105 # A key that is obviously not a credential, and never leaves this host.
106 api_key='[redacted]'
107
108 echo '--- 1. auth set writes the key'
109 if ! printf '%s\n' "$api_key" | CODEWHALE_HOME="$home" "$bin" auth set --provider deepseek >"$log" 2>&1; then
110 if grep -q 'config lock' "$log"; then
111 cat "$log" >&2
112 echo 'BLOCKED: the config layer refused before the secret store was reached.' >&2
113 echo ' That is the crates/config write lock, not this store.' >&2
114 exit 3
115 fi
116 cat "$log" >&2
117 fail 'auth set failed; the store could not be written on this mount'
118 fi
119 grep -q 'saved API key' "$log" || {
120 cat "$log" >&2
121 fail 'auth set did not report saving the key'
122 }
123 echo 'ok: key accepted'
124
125 echo '--- 2. the key is readable back through the store'
126 # `auth status` names the layer that answered, so this proves the store supplied
127 # the key rather than the environment or config.toml.
128 if ! CODEWHALE_HOME="$home" "$bin" auth status --provider deepseek >"$log" 2>&1; then
129 cat "$log" >&2
130 fail 'auth status failed'
131 fi
132 grep -q 'active source: secret store' "$log" || {
133 cat "$log" >&2
134 fail 'the stored key is not the active source'
135 }
136 grep -q 'secret store: file-based.*(set,' "$log" || {
137 cat "$log" >&2
138 fail 'the secret store does not report the key as set'
139 }
140 if grep -q 'env var: DEEPSEEK_API_KEY (set' "$log"; then
141 cat "$log" >&2
142 fail 'DEEPSEEK_API_KEY is set in this shell and would mask the store; unset it and retry'
143 fi
144 echo 'ok: auth status resolves the key from the secret store'
145
146 echo '--- 3. the config file holds no plaintext key'
147 config=$home/config.toml
148 [ -f "$config" ] || fail "no config.toml written at $config"
149 if grep -qF -- "$api_key" "$config"; then
150 fail 'the API key was written to config.toml in plaintext'
151 fi
152 echo 'ok: config.toml has no plaintext key'
153
154 echo '--- 4. the secrets file exists, and is owner-only where the mount can express that'
155 secrets=$home/secrets/secrets.json
156 [ -f "$secrets" ] || fail "no secrets.json written at $secrets"
157 secrets_mode=$(mode_of "$secrets")
158 echo " secrets.json mode: $secrets_mode"
159 if [ "$mount_note" = 'stores mode bits' ]; then
160 case $secrets_mode in
161 600 | 400 | 0) echo 'ok: owner-only' ;;
162 *) fail "a mount that stores mode bits left secrets.json at $secrets_mode, not 0600" ;;
163 esac
164 else
165 echo 'ok: the mount reports its own mode, and the store accepted it deliberately'
166 fi
167
168 echo
169 echo "PASS: the secret store works on $dir"
170
170 lines BASH