]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/units/testsuite-54.sh
creds: Add ImportCredential=
[thirdparty/systemd.git] / test / units / testsuite-54.sh
CommitLineData
30dd9f73 1#!/usr/bin/env bash
7b3cec95 2# SPDX-License-Identifier: LGPL-2.1-or-later
084575ff
FS
3# shellcheck disable=SC2016
4set -eux
30dd9f73
LP
5
6systemd-analyze log-level debug
7
ad2875af
FS
8run_with_cred_compare() {
9 local cred="${1:?}"
10 local exp="${2?}"
11 shift 2
12
13 diff <(systemd-run -p SetCredential="$cred" --wait --pipe -- systemd-creds "$@") <(echo -ne "$exp")
14}
15
16# Sanity checks
17#
18# Create a dummy "full" disk (similar to /dev/full) to check out-of-space
19# scenarios
20mkdir /tmp/full
21mount -t tmpfs -o size=1,nr_inodes=1 tmpfs /tmp/full
22
23# verb: setup
24# Run this first, otherwise any encrypted credentials wouldn't be decryptable
25# as we regnerate the host key
26rm -fv /var/lib/systemd/credential.secret
27systemd-creds setup
28test -e /var/lib/systemd/credential.secret
29rm -fv /var/lib/systemd/credential.secret
30
31# Prepare a couple of dummy credentials for the cat/list verbs
32CRED_DIR="$(mktemp -d)"
33ENC_CRED_DIR="$(mktemp -d)"
34echo foo >"$CRED_DIR/secure-or-weak"
35echo foo >"$CRED_DIR/insecure"
36echo foo | systemd-creds --name="encrypted" encrypt - - | base64 -d >"$ENC_CRED_DIR/encrypted"
37echo foo | systemd-creds encrypt - - | base64 -d >"$ENC_CRED_DIR/encrypted-unnamed"
38chmod -R 0400 "$CRED_DIR" "$ENC_CRED_DIR"
39chmod -R 0444 "$CRED_DIR/insecure"
40mkdir /tmp/empty/
41
42systemd-creds --system
43systemd-creds --no-pager --help
44systemd-creds --version
45systemd-creds has-tpm2 || :
46systemd-creds has-tpm2 -q || :
47
48# verb: list
49systemd-creds list --system
50ENCRYPTED_CREDENTIALS_DIRECTORY="$ENC_CRED_DIR" CREDENTIALS_DIRECTORY="$CRED_DIR" systemd-creds list --no-legend
51ENCRYPTED_CREDENTIALS_DIRECTORY="$ENC_CRED_DIR" CREDENTIALS_DIRECTORY="$CRED_DIR" systemd-creds list --json=pretty | jq
52ENCRYPTED_CREDENTIALS_DIRECTORY="$ENC_CRED_DIR" CREDENTIALS_DIRECTORY="$CRED_DIR" systemd-creds list --json=short | jq
53ENCRYPTED_CREDENTIALS_DIRECTORY="$ENC_CRED_DIR" CREDENTIALS_DIRECTORY="$CRED_DIR" systemd-creds list --json=off
54ENCRYPTED_CREDENTIALS_DIRECTORY="/tmp/empty/" CREDENTIALS_DIRECTORY="/tmp/empty/" systemd-creds list
55
56# verb: cat
57for cred in secure-or-weak insecure encrypted encrypted-unnamed; do
58 ENCRYPTED_CREDENTIALS_DIRECTORY="$ENC_CRED_DIR" CREDENTIALS_DIRECTORY="$CRED_DIR" systemd-creds cat "$cred"
59done
60run_with_cred_compare "mycred:" "" cat mycred
61run_with_cred_compare "mycred:\n" "\n" cat mycred
62run_with_cred_compare "mycred:foo" "foo" cat mycred
63run_with_cred_compare "mycred:foo" "foofoofoo" cat mycred mycred mycred
64# Note: --newline= does nothing when stdout is not a tty, which is the case here
65run_with_cred_compare "mycred:foo" "foo" --newline=yes cat mycred
66run_with_cred_compare "mycred:foo" "foo" --newline=no cat mycred
67run_with_cred_compare "mycred:foo" "foo" --newline=auto cat mycred
68run_with_cred_compare "mycred:foo" "foo" --transcode=no cat mycred
69run_with_cred_compare "mycred:foo" "foo" --transcode=0 cat mycred
70run_with_cred_compare "mycred:foo" "foo" --transcode=false cat mycred
71run_with_cred_compare "mycred:foo" "Zm9v" --transcode=base64 cat mycred
72run_with_cred_compare "mycred:Zm9v" "foo" --transcode=unbase64 cat mycred
73run_with_cred_compare "mycred:Zm9v" "foofoofoo" --transcode=unbase64 cat mycred mycred mycred
74run_with_cred_compare "mycred:Zm9vCg==" "foo\n" --transcode=unbase64 cat mycred
75run_with_cred_compare "mycred:hello world" "68656c6c6f20776f726c64" --transcode=hex cat mycred
76run_with_cred_compare "mycred:68656c6c6f20776f726c64" "hello world" --transcode=unhex cat mycred
77run_with_cred_compare "mycred:68656c6c6f20776f726c64" "hello worldhello world" --transcode=unhex cat mycred mycred
78run_with_cred_compare "mycred:68656c6c6f0a776f726c64" "hello\nworld" --transcode=unhex cat mycred
79run_with_cred_compare 'mycred:{ "foo" : "bar", "baz" : [ 3, 4 ] }' '{"foo":"bar","baz":[3,4]}\n' --json=short cat mycred
80systemd-run -p SetCredential='mycred:{ "foo" : "bar", "baz" : [ 3, 4 ] }' --wait --pipe -- systemd-creds --json=pretty cat mycred | jq
81
82# verb: encrypt/decrypt
83echo "According to all known laws of aviation..." >/tmp/cred.orig
84systemd-creds --with-key=host encrypt /tmp/cred.orig /tmp/cred.enc
85systemd-creds decrypt /tmp/cred.enc /tmp/cred.dec
86diff /tmp/cred.orig /tmp/cred.dec
87rm -f /tmp/cred.{enc,dec}
88# --pretty
89cred_name="fo'''o''bar"
90cred_option="$(systemd-creds --pretty --name="$cred_name" encrypt /tmp/cred.orig -)"
91mkdir -p /run/systemd/system
92cat >/run/systemd/system/test-54-pretty-cred.service <<EOF
93[Service]
94Type=oneshot
95${cred_option:?}
96ExecStart=bash -c "diff <(systemd-creds cat \"$cred_name\") /tmp/cred.orig"
97EOF
98systemctl daemon-reload
99systemctl start test-54-pretty-cred
100rm /run/systemd/system/test-54-pretty-cred.service
101# Credential validation: name
102systemd-creds --name="foo" -H encrypt /tmp/cred.orig /tmp/cred.enc
103(! systemd-creds decrypt /tmp/cred.enc /tmp/cred.dec)
104(! systemd-creds --name="bar" decrypt /tmp/cred.enc /tmp/cred.dec)
105systemd-creds --name="" decrypt /tmp/cred.enc /tmp/cred.dec
106diff /tmp/cred.orig /tmp/cred.dec
107rm -f /tmp/cred.dec
108systemd-creds --name="foo" decrypt /tmp/cred.enc /tmp/cred.dec
109diff /tmp/cred.orig /tmp/cred.dec
110rm -f /tmp/cred.{enc,dec}
111# Credential validation: time
112systemd-creds --not-after="+1d" encrypt /tmp/cred.orig /tmp/cred.enc
113(! systemd-creds --timestamp="+2d" decrypt /tmp/cred.enc /tmp/cred.dec)
114systemd-creds decrypt /tmp/cred.enc /tmp/cred.dec
115diff /tmp/cred.orig /tmp/cred.dec
116rm -f /tmp/cred.{enc,dec}
117
118(! unshare -m bash -exc "mount -t tmpfs tmpfs /run/credentials && systemd-creds list")
119(! unshare -m bash -exc "mount -t tmpfs tmpfs /run/credentials && systemd-creds --system list")
120(! CREDENTIALS_DIRECTORY="" systemd-creds list)
121(! systemd-creds --system --foo)
122(! systemd-creds --system -@)
123(! systemd-creds --system --json=)
124(! systemd-creds --system --json="")
125(! systemd-creds --system --json=foo)
126(! systemd-creds --system cat)
127(! systemd-creds --system cat "")
128(! systemd-creds --system cat this-should-not-exist)
129(! systemd-run -p SetCredential=mycred:foo --wait --pipe -- systemd-creds --transcode= cat mycred)
130(! systemd-run -p SetCredential=mycred:foo --wait --pipe -- systemd-creds --transcode="" cat mycred)
131(! systemd-run -p SetCredential=mycred:foo --wait --pipe -- systemd-creds --transcode=foo cat mycred)
132(! systemd-run -p SetCredential=mycred:foo --wait --pipe -- systemd-creds --newline=foo cat mycred)
133(! systemd-run -p SetCredential=mycred:notbase64 --wait --pipe -- systemd-creds --transcode=unbase64 cat mycred)
134(! systemd-run -p SetCredential=mycred:nothex --wait --pipe -- systemd-creds --transcode=unhex cat mycred)
135(! systemd-run -p SetCredential=mycred:a --wait --pipe -- systemd-creds --transcode=unhex cat mycred)
136(! systemd-run -p SetCredential=mycred:notjson --wait --pipe -- systemd-creds --json=short cat mycred)
137(! systemd-run -p SetCredential=mycred:notjson --wait --pipe -- systemd-creds --json=pretty cat mycred)
138(! systemd-creds encrypt /foo/bar/baz -)
139(! systemd-creds decrypt /foo/bar/baz -)
140(! systemd-creds decrypt / -)
141(! systemd-creds encrypt / -)
142(! echo foo | systemd-creds --with-key=foo encrypt - -)
143(! echo {0..20} | systemd-creds decrypt - -)
144(! systemd-creds --not-after= encrypt /tmp/cred.orig /tmp/cred.enc)
145(! systemd-creds --not-after="" encrypt /tmp/cred.orig /tmp/cred.enc)
146(! systemd-creds --not-after="-1d" encrypt /tmp/cred.orig /tmp/cred.enc)
147(! systemd-creds --timestamp= encrypt /tmp/cred.orig /tmp/cred.enc)
148(! systemd-creds --timestamp="" encrypt /tmp/cred.orig /tmp/cred.enc)
149(! dd if=/dev/zero count=2M | systemd-creds --with-key=tpm2-absent encrypt - /dev/null)
150(! dd if=/dev/zero count=2M | systemd-creds --with-key=tpm2-absent decrypt - /dev/null)
151(! echo foo | systemd-creds encrypt - /tmp/full/foo)
152(! echo foo | systemd-creds encrypt - - | systemd-creds decrypt - /tmp/full/foo)
153
30dd9f73
LP
154# Verify that the creds are properly loaded and we can read them from the service's unpriv user
155systemd-run -p LoadCredential=passwd:/etc/passwd \
128db0aa
ZJS
156 -p LoadCredential=shadow:/etc/shadow \
157 -p SetCredential=dog:wuff \
158 -p DynamicUser=1 \
159 --unit=test-54-unpriv.service \
160 --wait \
161 --pipe \
162 cat '${CREDENTIALS_DIRECTORY}/passwd' '${CREDENTIALS_DIRECTORY}/shadow' '${CREDENTIALS_DIRECTORY}/dog' \
163 >/tmp/ts54-concat
164(cat /etc/passwd /etc/shadow && echo -n wuff) | cmp /tmp/ts54-concat
30dd9f73
LP
165rm /tmp/ts54-concat
166
6d085447 167# Test that SetCredential= acts as fallback for LoadCredential=
7a17e41d 168echo piff >/tmp/ts54-fallback
6d085447
LP
169[ "$(systemd-run -p LoadCredential=paff:/tmp/ts54-fallback -p SetCredential=paff:poff --pipe --wait systemd-creds cat paff)" = "piff" ]
170rm /tmp/ts54-fallback
171[ "$(systemd-run -p LoadCredential=paff:/tmp/ts54-fallback -p SetCredential=paff:poff --pipe --wait systemd-creds cat paff)" = "poff" ]
172
42a3f23c 173if systemd-detect-virt -q -c ; then
93a1f57d
LP
174 expected_credential=mynspawncredential
175 expected_value=strangevalue
176elif [ -d /sys/firmware/qemu_fw_cfg/by_name ]; then
177 # Verify that passing creds through kernel cmdline works
178 [ "$(systemd-creds --system cat kernelcmdlinecred)" = "uff" ]
179
8de7de46
LP
180 # And that it also works via SMBIOS
181 [ "$(systemd-creds --system cat smbioscredential)" = "magicdata" ]
182 [ "$(systemd-creds --system cat binarysmbioscredential)" = "magicbinarydata" ]
183
93a1f57d
LP
184 # If we aren't run in nspawn, we are run in qemu
185 systemd-detect-virt -q -v
186 expected_credential=myqemucredential
187 expected_value=othervalue
39f0d1d2
LP
188
189 # Verify that writing a sysctl via the kernel cmdline worked
190 [ "$(cat /proc/sys/kernel/domainname)" = "sysctltest" ]
3acb6ede
LP
191
192 # Verify that creating a user via sysusers via the kernel cmdline worked
193 grep -q ^credtestuser: /etc/passwd
1d77721f
LP
194
195 # Verify that writing a file via tmpfiles worked
196 [ "$(cat /tmp/sourcedfromcredential)" = "tmpfilessecret" ]
a0f4426d
LP
197 [ "$(cat /etc/motd.d/50-provision.conf)" = "hello" ]
198 [ "$(cat /etc/issue.d/50-provision.conf)" = "welcome" ]
93a1f57d
LP
199else
200 echo "qemu_fw_cfg support missing in kernel. Sniff!"
201 expected_credential=""
202 expected_value=""
203fi
204
205if [ "$expected_credential" != "" ] ; then
42a3f23c 206 # If this test is run in nspawn a credential should have been passed to us. See test/TEST-54-CREDS/test.sh
93a1f57d 207 [ "$(systemd-creds --system cat "$expected_credential")" = "$expected_value" ]
42a3f23c
LP
208
209 # Test that propagation from system credential to service credential works
93a1f57d 210 [ "$(systemd-run -p LoadCredential="$expected_credential" --pipe --wait systemd-creds cat "$expected_credential")" = "$expected_value" ]
42a3f23c
LP
211
212 # Check it also works, if we rename it while propagating it
93a1f57d 213 [ "$(systemd-run -p LoadCredential=miau:"$expected_credential" --pipe --wait systemd-creds cat miau)" = "$expected_value" ]
42a3f23c
LP
214
215 # Combine it with a fallback (which should have no effect, given the cred should be passed down)
93a1f57d 216 [ "$(systemd-run -p LoadCredential="$expected_credential" -p SetCredential="$expected_credential":zzz --pipe --wait systemd-creds cat "$expected_credential")" = "$expected_value" ]
4f80cfca
LP
217
218 # This should succeed
219 systemd-run -p AssertCredential="$expected_credential" -p Type=oneshot true
220
221 # And this should fail
128db0aa 222 (! systemd-run -p AssertCredential="undefinedcredential" -p Type=oneshot true)
42a3f23c
LP
223fi
224
30dd9f73 225# Verify that the creds are immutable
128db0aa
ZJS
226(! systemd-run -p LoadCredential=passwd:/etc/passwd \
227 -p DynamicUser=1 \
228 --unit=test-54-immutable-touch.service \
229 --wait \
230 touch '${CREDENTIALS_DIRECTORY}/passwd')
231(! systemd-run -p LoadCredential=passwd:/etc/passwd \
232 -p DynamicUser=1 \
233 --unit=test-54-immutable-rm.service \
234 --wait \
235 rm '${CREDENTIALS_DIRECTORY}/passwd')
30dd9f73 236
3989bdc1
AB
237# Check directory-based loading
238mkdir -p /tmp/ts54-creds/sub
239echo -n a >/tmp/ts54-creds/foo
240echo -n b >/tmp/ts54-creds/bar
241echo -n c >/tmp/ts54-creds/baz
242echo -n d >/tmp/ts54-creds/sub/qux
243systemd-run -p LoadCredential=cred:/tmp/ts54-creds \
128db0aa
ZJS
244 -p DynamicUser=1 \
245 --unit=test-54-dir.service \
246 --wait \
247 --pipe \
248 cat '${CREDENTIALS_DIRECTORY}/cred_foo' \
249 '${CREDENTIALS_DIRECTORY}/cred_bar' \
250 '${CREDENTIALS_DIRECTORY}/cred_baz' \
251 '${CREDENTIALS_DIRECTORY}/cred_sub_qux' >/tmp/ts54-concat
97b35c7c 252cmp /tmp/ts54-concat <(echo -n abcd)
3989bdc1
AB
253rm /tmp/ts54-concat
254rm -rf /tmp/ts54-creds
c69620ef 255
bbfb25f4
DDM
256# Check that globs work as expected
257mkdir -p /run/credstore
258echo -n a >/run/credstore/test.creds.first
259echo -n b >/run/credstore/test.creds.second
260mkdir -p /etc/credstore
261echo -n c >/etc/credstore/test.creds.third
262systemd-run -p "ImportCredential=test.creds.*" \
263 -p DynamicUser=1 \
264 --wait \
265 --pipe \
266 cat '${CREDENTIALS_DIRECTORY}/test.creds.first' \
267 '${CREDENTIALS_DIRECTORY}/test.creds.second' \
268 '${CREDENTIALS_DIRECTORY}/test.creds.third' >/tmp/ts54-concat
269cmp /tmp/ts54-concat <(echo -n abc)
270
3989bdc1 271# Now test encrypted credentials (only supported when built with OpenSSL though)
c69620ef
LP
272if systemctl --version | grep -q -- +OPENSSL ; then
273 echo -n $RANDOM >/tmp/test-54-plaintext
274 systemd-creds encrypt --name=test-54 /tmp/test-54-plaintext /tmp/test-54-ciphertext
275 systemd-creds decrypt --name=test-54 /tmp/test-54-ciphertext | cmp /tmp/test-54-plaintext
276
277 systemd-run -p LoadCredentialEncrypted=test-54:/tmp/test-54-ciphertext \
128db0aa
ZJS
278 --wait \
279 --pipe \
280 cat '${CREDENTIALS_DIRECTORY}/test-54' | cmp /tmp/test-54-plaintext
c69620ef
LP
281
282 echo -n $RANDOM >/tmp/test-54-plaintext
283 systemd-creds encrypt --name=test-54 /tmp/test-54-plaintext /tmp/test-54-ciphertext
284 systemd-creds decrypt --name=test-54 /tmp/test-54-ciphertext | cmp /tmp/test-54-plaintext
285
1c3f490f 286 systemd-run -p SetCredentialEncrypted=test-54:"$(cat /tmp/test-54-ciphertext)" \
128db0aa
ZJS
287 --wait \
288 --pipe \
289 cat '${CREDENTIALS_DIRECTORY}/test-54' | cmp /tmp/test-54-plaintext
c69620ef
LP
290
291 rm /tmp/test-54-plaintext /tmp/test-54-ciphertext
292fi
293
c9210b74
LB
294# https://github.com/systemd/systemd/issues/27275
295systemd-run -p DynamicUser=yes -p 'LoadCredential=os:/etc/os-release' \
296 -p 'ExecStartPre=true' \
297 -p 'ExecStartPre=systemd-creds cat os' \
574d09ba 298 --unit=test-54-exec-start.service \
c9210b74
LB
299 --wait \
300 --pipe \
301 true | cmp /etc/os-release
302
30dd9f73
LP
303systemd-analyze log-level info
304
0ee99483 305echo OK >/testok
30dd9f73
LP
306
307exit 0