1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
#!/bin/bash
# Tests for buckets: get, has, put, mv, rekey, list, check and enrol, beside
# the old store that common.sh sets up.
this_dir="$(cd "$(dirname "$0")" && pwd)"
source "${this_dir}/common.sh"
export SZECRET_BUCKETS="${TEST_DIR}/buckets"
export SZECRET_IDENTITY="${TEST_DIR}/me/identity"
mkdir -p "${SZECRET_BUCKETS}"
# This machine, another host, and the admin, each a key of its own.
szecret enrol >/dev/null 2>&1
me="$(cat "${SZECRET_IDENTITY}.pub")"
age-keygen -o "${TEST_DIR}/other" 2>/dev/null
other="$(age-keygen -y "${TEST_DIR}/other")"
age-keygen -o "${TEST_DIR}/admin" 2>/dev/null
admin="$(age-keygen -y "${TEST_DIR}/admin")"
echo "${admin}" > "${TEST_DIR}/admins"
export SZECRET_ADMINS_COMMAND="cat '${TEST_DIR}/admins'"
# bucket <name> <"name key" lines...>: a recipients file as szecret-buckets writes it.
bucket() {
local b="$1" row
shift
mkdir -p "${SZECRET_BUCKETS}/${b}"
{
echo "# Written by szecret-buckets"
for row in "$@"; do
echo "# ${row%% *}"
echo "${row#* }"
done
} > "${SZECRET_BUCKETS}/${b}/recipients"
}
bucket mine "me ${me}" "other ${other}"
bucket theirs "other ${other}"
bucket admin-only
test_enrol_twice_same_key() {
local again
again="$(szecret enrol 2>/dev/null)"
assert_equals "${me}" "${again}" "second enrol"
assert_success test "$(stat -c %a "${SZECRET_IDENTITY}" 2>/dev/null || stat -f %Lp "${SZECRET_IDENTITY}")" = 600
pass "enrol: makes one identity, mode 600, and prints the same key again"
}
test_enrol_missing_hardware() {
local err
err="$(SZECRET_IDENTITY="${TEST_DIR}/hw/identity" PATH="/usr/bin:/bin:$(dirname "$(command -v age)")" szecret enrol --tpm 2>&1)" \
&& fail "enrol --tpm should fail without the plugin"
assert_contains "${err}" "no key made"
[[ ! -e "${TEST_DIR}/hw/identity" ]] || fail "enrol --tpm left a key"
pass "enrol: refuses missing hardware and makes no file key instead"
}
test_put_refuses_before_rekey() {
local err
err="$(echo v | szecret put mine/x 2>&1)" && fail "put before the first rekey should fail"
assert_contains "${err}" "never been keyed"
pass "put: refuses a bucket never keyed"
}
test_rekey_first_time() {
local out
out="$(szecret rekey mine)"
assert_contains "${out}" "+ me"
assert_contains "${out}" "+ admin"
assert_contains "$(cat "${SZECRET_BUCKETS}/mine/.szecret")" "kind=bucket"
szecret rekey theirs >/dev/null
szecret rekey admin-only >/dev/null
assert_equals "${admin}" "$(sed -n 's/^encrypted-to=//p' "${SZECRET_BUCKETS}/admin-only/.szecret")" "admin-only's set"
pass "rekey: keys empty buckets without asking, the admins always in"
}
test_put_get_bucket() {
echo "v1" | szecret put mine/x
assert_equals "v1" "$(szecret get mine/x)" "get mine/x"
assert_equals "v1" "$(szecret get x)" "get x"
assert_equals "v1" "$(age -d -i "${TEST_DIR}/admin" "${SZECRET_BUCKETS}/mine/x.age")" "the admin reads it"
assert_equals "v1" "$(age -d -i "${TEST_DIR}/other" "${SZECRET_BUCKETS}/mine/x.age")" "the other reader reads it"
echo "v2" | szecret put x 2>/dev/null && fail "plain put of a name in a bucket should fail"
pass "put/get: a bucket's readers and the admins read it"
}
test_not_a_reader() {
echo "t" | szecret put theirs/t
local err
err="$(szecret get t 2>&1)" && fail "get from a bucket not read here should fail"
assert_contains "${err}" "t is in bucket theirs; this machine isn't one of its readers"
szecret has t && fail "has t should fail"
szecret has x || fail "has x should succeed"
szecret has nope && fail "has nope should fail"
pass "get/has: a bucket this machine doesn't read"
}
test_plain_put_admin_only() {
local err
err="$(echo "a" | szecret put a 2>&1)"
assert_contains "${err}" "admin-only"
assert_file_exists "${SZECRET_BUCKETS}/admin-only/a.age"
assert_equals "a" "$(age -d -i "${TEST_DIR}/admin" "${SZECRET_BUCKETS}/admin-only/a.age")" "the admin reads it"
szecret get a 2>/dev/null && fail "this machine should not read admin-only"
pass "put: a plain name goes to admin-only"
}
test_old_store_side_by_side() {
echo "old" | SZECRET_BUCKETS="" szecret put oldone
assert_file_exists "${SZECRET_STORE}/oldone.age"
assert_equals "old" "$(szecret get oldone)" "get from the old store"
echo "old2" | szecret put oldone 2>/dev/null
assert_equals "old2" "$(szecret get oldone)" "rewritten in the old store"
szecret has oldone || fail "has oldone"
pass "the old store: read and rewritten in place beside the buckets"
}
test_two_places() {
cp "${SZECRET_BUCKETS}/mine/x.age" "${SZECRET_BUCKETS}/theirs/x.age"
local err
err="$(szecret get x 2>&1)" && fail "a name in two places should fail"
assert_contains "${err}" "mine/x"
assert_contains "${err}" "theirs/x"
assert_equals "v1" "$(szecret get mine/x)" "bucket/name still works"
rm "${SZECRET_BUCKETS}/theirs/x.age"
pass "get: a name in two places names both"
}
test_mv() {
szecret mv oldone mine 2>/dev/null
[[ ! -e "${SZECRET_STORE}/oldone.age" ]] || fail "the old file should be gone"
assert_equals "old2" "$(szecret get mine/oldone)" "moved"
assert_equals "old2" "$(age -d -i "${TEST_DIR}/admin" "${SZECRET_BUCKETS}/mine/oldone.age")" "the admin reads it"
szecret mv a mine 2>/dev/null && fail "mv of a secret this machine can't read should fail"
assert_file_exists "${SZECRET_BUCKETS}/admin-only/a.age"
cp "${SZECRET_BUCKETS}/mine/oldone.age" "${SZECRET_BUCKETS}/theirs/oldone.age"
szecret mv theirs/oldone mine 2>/dev/null && fail "mv onto an existing name should fail"
assert_equals "old2" "$(szecret get mine/oldone)" "the existing file is untouched"
rm "${SZECRET_BUCKETS}/theirs/oldone.age"
pass "mv: moves out of the old store; one it can't read stays put; never overwrites"
}
# mv --phone: what szecret itself checks of the header that comes back. The
# phone's own work (cloud-setup's phone/szmv.py) is tested there.
test_mv_phone_refusals() {
local err f="${SZECRET_BUCKETS}/admin-only/a.age" before
before="$(shasum "${f}")"
untouched() {
assert_equals "${before}" "$(shasum "${f}")" "$1: admin-only/a is as it was"
[[ ! -e "${SZECRET_BUCKETS}/mine/a.age" ]] || fail "$1: left mine/a"
[[ -z "$(ls -A "${SZECRET_BUCKETS}/mine" | grep '^\.put\.')" ]] || fail "$1: left a temporary file"
}
err="$(SZECRET_PHONE_COMMAND="" szecret mv --phone a mine 2>&1)" && fail "no command should fail"
assert_contains "${err}" "SZECRET_PHONE_COMMAND"
untouched "no command"
err="$(SZECRET_PHONE_COMMAND="cat >/dev/null; exit 1" szecret mv --phone a mine 2>&1)" && fail "a failed command should fail"
untouched "a failed command"
err="$(SZECRET_PHONE_COMMAND="cat >/dev/null; echo not-a-header" szecret mv --phone a mine 2>&1)" && fail "garbage should fail"
assert_contains "${err}" "not a whole age header"
untouched "garbage"
# The readers it is told of, and a header of the right shape for them that
# is not for this file: this machine is one of the readers, and cannot open it.
cat > "${TEST_DIR}/fake-phone" <<'STUB'
#!/bin/bash
desc="$(cat)"
args=()
while read -r k v _; do [[ "${k}" == reader ]] && args+=(-r "${v}"); done <<<"${desc}"
echo "${desc}" > "${TEST_DIR}/fake-phone.desc"
echo x | age "${args[@]}" | LC_ALL=C awk '{ print } /^--- / { exit }'
STUB
chmod +x "${TEST_DIR}/fake-phone"
err="$(TEST_DIR="${TEST_DIR}" SZECRET_PHONE_COMMAND="${TEST_DIR}/fake-phone" szecret mv --phone a mine 2>&1)" \
&& fail "a header for another file key should fail where this machine reads it"
assert_contains "${err}" "cannot open the new file"
untouched "a wrong header"
assert_contains "$(cat "${TEST_DIR}/fake-phone.desc")" "name a"
assert_contains "$(cat "${TEST_DIR}/fake-phone.desc")" "from admin-only"
assert_contains "$(cat "${TEST_DIR}/fake-phone.desc")" "reader ${me} me"
assert_contains "$(cat "${TEST_DIR}/fake-phone.desc")" "reader ${admin} admin"
# The readers change while the phone answers.
local saved
saved="$(cat "${SZECRET_BUCKETS}/mine/recipients")"
err="$(TEST_DIR="${TEST_DIR}" SZECRET_PHONE_COMMAND="${TEST_DIR}/fake-phone; echo '${other}' >> '${SZECRET_BUCKETS}/mine/recipients'" \
szecret mv --phone a mine 2>&1)" && fail "a reader change while waiting should fail"
echo "${saved}" > "${SZECRET_BUCKETS}/mine/recipients"
untouched "a reader change"
# The file changes while the phone answers (a rekey arriving), into a
# bucket this machine does not read, so it cannot open the result.
cp "${f}" "${TEST_DIR}/a.saved"
err="$(TEST_DIR="${TEST_DIR}" SZECRET_PHONE_COMMAND="${TEST_DIR}/fake-phone; printf x >> '${f}'" \
szecret mv --phone a theirs 2>&1)" && fail "a file changed while waiting should fail"
assert_contains "${err}" "changed while the phone answered"
[[ ! -e "${SZECRET_BUCKETS}/theirs/a.age" ]] || fail "a changed file: left theirs/a"
cp "${TEST_DIR}/a.saved" "${f}"
untouched "a changed file"
pass "mv --phone: no command, a failure, garbage, a header that is not this file's, readers or the file changing: nothing written"
}
test_put_refuses_stale_readers() {
bucket mine "me ${me}"
local err
err="$(echo v | szecret put mine/x 2>&1)" && fail "put after a reader change should fail"
assert_contains "${err}" "rekey"
out="$(szecret check 2>&1)" && fail "check should report the change"
assert_contains "${out}" "readers changed since its last rekey (-other)"
pass "put and check: refuse until a rekey after the readers change"
}
test_rekey_removal() {
local out
out="$(szecret rekey mine < /dev/null)"
assert_contains "${out}" "- other"
age -d -i "${TEST_DIR}/other" "${SZECRET_BUCKETS}/mine/x.age" >/dev/null 2>&1 && fail "the removed reader still reads x"
assert_equals "v1" "$(szecret get mine/x)" "still readable here"
pass "rekey: a removed reader no longer reads the files"
}
test_rekey_addition_needs_yes() {
bucket mine "me ${me}" "other ${other}"
local err
err="$(szecret rekey mine < /dev/null 2>&1)" && fail "an addition without a terminal should fail"
assert_contains "${err}" "--add-ok"
age -d -i "${TEST_DIR}/other" "${SZECRET_BUCKETS}/mine/x.age" >/dev/null 2>&1 && fail "nothing should have changed"
szecret rekey --add-ok mine >/dev/null
assert_equals "v1" "$(age -d -i "${TEST_DIR}/other" "${SZECRET_BUCKETS}/mine/x.age")" "added"
pass "rekey: an addition needs a terminal or --add-ok"
}
test_rekey_unreadable_changes_nothing() {
# theirs holds t, which this machine can't open: a rekey here must stop whole.
bucket theirs "other ${other}" "me ${me}"
local before err
before="$(cat "${SZECRET_BUCKETS}/theirs/.szecret")"
err="$(szecret rekey --add-ok theirs 2>&1)" && fail "rekey of a file it can't read should fail"
assert_contains "${err}" "nothing changed"
assert_equals "${before}" "$(cat "${SZECRET_BUCKETS}/theirs/.szecret")" ".szecret unchanged"
[[ -z "$(ls -a "${SZECRET_BUCKETS}" | grep rekey)" ]] || fail "work directory left behind"
bucket theirs "other ${other}"
pass "rekey: a file it can't decrypt stops it, nothing changed"
}
test_no_admins() {
local err
err="$(echo v | SZECRET_ADMINS_COMMAND="true" szecret put mine/y 2>&1)" && fail "put without admins should fail"
assert_contains "${err}" "no admins"
err="$(echo v | env -u SZECRET_ADMINS_COMMAND "${SECRETS_DIR}/main.sh" put mine/y 2>&1)" && fail "put without the command should fail"
assert_contains "${err}" "no admins"
[[ ! -e "${SZECRET_BUCKETS}/mine/y.age" ]] || fail "y written without admins"
pass "put: refuses with no admins"
}
test_admin_change_needs_rekey() {
age-keygen -o "${TEST_DIR}/admin2" 2>/dev/null
age-keygen -y "${TEST_DIR}/admin2" >> "${TEST_DIR}/admins"
echo v | szecret put mine/y 2>/dev/null && fail "put after the admins changed should fail"
szecret rekey --add-ok mine >/dev/null
echo v | szecret put mine/y
assert_equals "v" "$(age -d -i "${TEST_DIR}/admin2" "${SZECRET_BUCKETS}/mine/y.age")" "the new admin reads it"
pass "put: refuses after the admins change, until a rekey"
}
test_list() {
local out
out="$(szecret list)"
assert_contains "${out}" "oldone"
assert_contains "${out}" "x"
[[ "$(echo "${out}" | grep -cx t)" -eq 0 ]] || fail "list shows a secret not read here: ${out}"
out="$(szecret list --names)"
assert_contains "${out}" "mine/x"
out="$(szecret list --names --all)"
assert_contains "${out}" "theirs/t"
assert_contains "${out}" "admin-only/a"
pass "list: bare names this machine reads; --names and --all"
}
test_check_ok() {
local out
out="$(szecret check)" || fail "check should pass: ${out}"
assert_contains "${out}" "no problems"
out="$(szecret check --all 2>&1)" && fail "check --all here should fail: this machine can't open theirs or admin-only"
assert_contains "${out}" "theirs/t: does not decrypt here"
pass "check: passes on what this machine reads; --all finds what it can't open"
}
test_check_stanza_types() {
local d="${SZECRET_BUCKETS}/mine"
# A passphrase file's header (age -p wants a terminal).
printf 'age-encryption.org/v1\n-> scrypt abc 18\nxyz\n--- mac\n' > "${d}/pw.age"
local out
out="$(szecret check 2>&1)" && fail "check should flag scrypt"
assert_contains "${out}" "mine/pw: unexpected stanza type scrypt"
rm "${d}/pw.age"
pass "check: flags stanza types it doesn't expect"
}
test_check_missing_plugin() {
bucket mine "me ${me}" "other ${other}" "castle age1se1qgg9a5ep2jkuz2v9d0ek0dvqr3ml7mjpz7nqwfvymk0d4nqx9uxs5vqz0xr"
local out
out="$(szecret check 2>&1)" && fail "check should flag a key it can't encrypt to"
assert_contains "${out}" "can't encrypt to castle here: age-plugin-se is not installed"
bucket mine "me ${me}" "other ${other}"
pass "check: flags a key type this machine can't encrypt to"
}
test_local_store_key_in_bucket() {
local d="${TEST_DIR}/repo-store"
mkdir -p "${d}"
SZECRET_BUCKETS="" szecret create-local inrepo "${d}" >/dev/null 2>&1 || fail "create-local"
echo "local1" | szecret put-local "${d}/v.age" 2>/dev/null || fail "put-local before the move"
szecret mv local-key-inrepo mine 2>/dev/null || fail "mv local-key-inrepo"
assert_equals "local1" "$(szecret get-local "${d}/v.age")" "get-local with its key in a bucket"
echo "local2" | szecret put-local "${d}/v.age" 2>/dev/null || fail "put-local after the move"
assert_equals "local2" "$(szecret get-local "${d}/v.age")" "put-local with its key in a bucket"
pass "get-local/put-local: an old-style repository store whose key moved to a bucket"
}
test_ready() {
szecret ready || fail "ready with an identity"
SZECRET_STORE="" SZECRET_PRIVATE_KEY="${TEST_DIR}/none" szecret ready || fail "ready with only buckets and an identity"
pass "ready: an identity and a store"
}
test_enrol_twice_same_key
test_enrol_missing_hardware
test_put_refuses_before_rekey
test_rekey_first_time
test_put_get_bucket
test_not_a_reader
test_plain_put_admin_only
test_old_store_side_by_side
test_two_places
test_mv
test_mv_phone_refusals
test_put_refuses_stale_readers
test_rekey_removal
test_rekey_addition_needs_yes
test_rekey_unreadable_changes_nothing
test_no_admins
test_admin_change_needs_rekey
test_list
test_check_ok
test_local_store_key_in_bucket
test_check_stanza_types
test_check_missing_plugin
test_ready
|