Skip to content

Commit 79c83ad

Browse files
committed
nvme/068: add test for authentication with dhchap keys
With the patchset 'nvme-auth: switch to use kernel keyring' the authentication code will store the authentication codes in the kernel keyring, which allows to use pre-populated keys for nvme authentication. So add a testcase for that. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent 7baa454 commit 79c83ad

2 files changed

Lines changed: 157 additions & 0 deletions

File tree

tests/nvme/068

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-3.0+
3+
# Copyright (C) 2022 Hannes Reinecke, SUSE Labs
4+
#
5+
# Test re-authentication with dhchap keys
6+
7+
. tests/nvme/rc
8+
9+
DESCRIPTION="Test re-authentication with dhchap keys"
10+
QUICK=1
11+
12+
requires() {
13+
_nvme_requires
14+
_have_fio
15+
_have_loop
16+
_have_kernel_option NVME_AUTH
17+
_have_kernel_option NVME_TARGET_AUTH
18+
_require_kernel_nvme_fabrics_feature dhchap_ctrl_secret
19+
_require_nvme_trtype_is_fabrics
20+
_require_nvme_cli_auth
21+
_have_driver dh_generic
22+
}
23+
24+
set_conditions() {
25+
_set_nvme_trtype "$@"
26+
}
27+
28+
test() {
29+
echo "Running ${TEST_NAME}"
30+
31+
_setup_nvmet
32+
33+
local hostkey
34+
local new_hostkey
35+
local ctrlkey
36+
local new_ctrlkey
37+
local ctrldev
38+
local rand_io_size
39+
local ns
40+
41+
keyctl link %:.nvme @u
42+
43+
hostkey="$(nvme gen-dhchap-key -n "${def_subsysnqn}" --hmac=1 2> /dev/null)"
44+
if [ -z "$hostkey" ] ; then
45+
echo "failed to generate host key"
46+
keyctl unlink %:.nvme
47+
return 1
48+
fi
49+
hostkeydesc="$(uuidgen)"
50+
if ! keyctl add dhchap ${hostkeydesc} ${hostkey} %:.nvme > /dev/null; then
51+
echo "failed to add host key"
52+
keyctl unlink %:.nvme
53+
return 1
54+
fi
55+
56+
ctrlkey="$(nvme gen-dhchap-key -n "${def_subsysnqn}" --hmac=1 2> /dev/null)"
57+
if [ -z "$ctrlkey" ] ; then
58+
echo "failed to generate ctrl key"
59+
keyctl unlink %:.nvme
60+
return 1
61+
fi
62+
ctrlkeydesc="$(uuidgen)"
63+
if ! keyctl add dhchap ${ctrlkeydesc} ${ctrlkey} %:.nvme > /dev/null; then
64+
keyctl revoke "%dhchap:${hostkeydesc}"
65+
echo "failed to add ctrl key"
66+
keyctl unlink %:.nvme
67+
return 1
68+
fi
69+
70+
_nvmet_target_setup --blkdev file --ctrlkey "${ctrlkeydesc}" \
71+
--hostkey "${hostkeydesc}"
72+
73+
_set_nvmet_dhgroup "${def_hostnqn}" "ffdhe2048"
74+
75+
_nvme_connect_subsys --dhchap-secret "${hostkeydesc}" \
76+
--dhchap-ctrl-secret "${ctrlkeydesc}"
77+
78+
echo "Re-authenticate with original host key"
79+
80+
ctrldev=$(_find_nvme_dev "${def_subsysnqn}")
81+
if [ -z "$ctrldev" ] ; then
82+
echo "nvme controller not found"
83+
fi
84+
hostkey_file="/sys/class/nvme/${ctrldev}/dhchap_secret"
85+
echo -n "${hostkey}" > "${hostkey_file}"
86+
87+
echo "Renew host key on the controller"
88+
89+
new_hostkey="$(nvme gen-dhchap-key --nqn "${def_subsysnqn}" 2> /dev/null)"
90+
if [ -z "$new_hostkey" ] ; then
91+
echo "failed to generate new host key"
92+
keyctl revoke "%dhchap:${ctrlkeydesc}"
93+
keyctl revoke "%dhchap:${hostkeydesc}"
94+
keyctl unlink %:.nvme
95+
return 1
96+
fi
97+
new_hostkeydesc="$(uuidgen)"
98+
if ! keyctl add dhchap ${new_hostkeydesc} ${new_hostkey} %:.nvme > /dev/null; then
99+
echo "failed to add new host key"
100+
keyctl revoke "%dhchap:${ctrlkeydesc}"
101+
keyctl revoke "%dhchap:${hostkeydesc}"
102+
keyctl unlink %:.nvme
103+
return 1
104+
fi
105+
_set_nvmet_hostkey "${def_hostnqn}" "${new_hostkeydesc}"
106+
107+
echo "Re-authenticate with new host key"
108+
109+
echo -n "${new_hostkeydesc}" > "${hostkey_file}"
110+
keyctl revoke "%dhchap:${hostkeydesc}"
111+
112+
echo "Renew ctrl key on the controller"
113+
114+
new_ctrlkey="$(nvme gen-dhchap-key --nqn "${def_subsysnqn}" 2> /dev/null)"
115+
if [ -z "$new_ctrlkey" ]; then
116+
echo "failed to generate new controller key"
117+
keyctl revoke "%dhchap:${ctrlkeydesc}"
118+
keyctl revoke "%dhchap:${new_hostkeydesc}"
119+
keyctl unlink %:.nvme
120+
return 1
121+
fi
122+
new_ctrlkeydesc="$(uuidgen)"
123+
if ! keyctl add dhchap ${new_ctrlkeydesc} ${new_ctrlkey} %:.nvme > /dev/null; then
124+
echo "failed to add new controller key"
125+
keyctl revoke "%dhchap:${ctrlkeydesc}"
126+
keyctl revoke "%dhchap:${new_hostkeydesc}"
127+
keyctl unlink %:.nvme
128+
return 1
129+
fi
130+
_set_nvmet_ctrlkey "${def_hostnqn}" "${new_ctrlkeydesc}"
131+
132+
echo "Re-authenticate with new ctrl key"
133+
134+
ctrlkey_file="/sys/class/nvme/${ctrldev}/dhchap_ctrl_secret"
135+
echo -n "${new_ctrlkeydesc}" > "${ctrlkey_file}"
136+
keyctl revoke "%dhchap:${ctrlkeydesc}"
137+
138+
rand_io_size="$(_nvme_calc_rand_io_size 4m)"
139+
_run_fio_rand_io --size="${rand_io_size}" --filename="/dev/${ns}"
140+
141+
_nvme_disconnect_subsys
142+
_nvmet_target_cleanup
143+
144+
keyctl revoke "%dhchap:${new_ctrlkeydesc}"
145+
keyctl revoke "%dhchap:${new_hostkeydesc}"
146+
keyctl unlink %:.nvme > /dev/null
147+
148+
echo "Test complete"
149+
}

tests/nvme/068.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Running nvme/068
2+
Re-authenticate with original host key
3+
Renew host key on the controller
4+
Re-authenticate with new host key
5+
Renew ctrl key on the controller
6+
Re-authenticate with new ctrl key
7+
disconnected 1 controller(s)
8+
Test complete

0 commit comments

Comments
 (0)