Skip to content

Commit 98e2a47

Browse files
authored
Merge pull request #870 from gbregman/devel
Allow only one host NQN when adding host with PSK keys
2 parents 69cb827 + 097d040 commit 98e2a47

File tree

2 files changed

+25
-51
lines changed

2 files changed

+25
-51
lines changed

control/cli.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,25 +1054,16 @@ def host_add(self, args):
10541054
out_func, err_func = self.get_output_functions(args)
10551055

10561056
if args.psk:
1057-
if len(args.psk) > len(args.host_nqn):
1058-
err_func("There are more PSK values than hosts, will ignore redundant values")
1059-
elif len(args.psk) < len(args.host_nqn):
1060-
err_func("There are more hosts than PSK values, will assume empty PSK values")
1057+
if len(args.host_nqn) > 1:
1058+
self.cli.parser.error(f"Can't have more than one host NQN when PSK keys are used")
10611059

10621060
for i in range(len(args.host_nqn)):
10631061
one_host_nqn = args.host_nqn[i]
1064-
one_host_psk = None
1065-
if args.psk:
1066-
try:
1067-
one_host_psk = args.psk[i]
1068-
except IndexError:
1069-
pass
10701062

1071-
if one_host_nqn == "*" and one_host_psk:
1072-
err_func(f"PSK is only allowed for specific hosts, ignoring PSK value \"{one_host_psk}\"")
1073-
one_host_psk = None
1063+
if one_host_nqn == "*" and args.psk:
1064+
self.cli.parser.error(f"PSK is only allowed for specific hosts")
10741065

1075-
req = pb2.add_host_req(subsystem_nqn=args.subsystem, host_nqn=one_host_nqn, psk=one_host_psk)
1066+
req = pb2.add_host_req(subsystem_nqn=args.subsystem, host_nqn=one_host_nqn, psk=args.psk)
10761067
try:
10771068
ret = self.stub.add_host(req)
10781069
except Exception as ex:
@@ -1219,7 +1210,7 @@ def host_list(self, args):
12191210
]
12201211
host_add_args = host_common_args + [
12211212
argument("--host-nqn", "-t", help="Host NQN list", nargs="+", required=True),
1222-
argument("--psk", help="Hosts PSK key list", nargs="+", required=False),
1213+
argument("--psk", help="Hosts PSK key list", required=False),
12231214
]
12241215
host_del_args = host_common_args + [
12251216
argument("--host-nqn", "-t", help="Host NQN list", nargs="+", required=True),

tests/test_psk.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
hostnqn8 = "nqn.2014-08.org.nvmexpress:uuid:22207d09-d8af-4ed2-84ec-a6d80b0cf7f2"
2626
hostnqn9 = "nqn.2014-08.org.nvmexpress:uuid:22207d09-d8af-4ed2-84ec-a6d80b0cf7f3"
2727
hostnqn10 = "nqn.2014-08.org.nvmexpress:uuid:22207d09-d8af-4ed2-84ec-a6d80b0cf7f4"
28-
hostnqn11 = "nqn.2014-08.org.nvmexpress:uuid:22207d09-d8af-4ed2-84ec-a6d80b0cf7f5"
29-
hostnqn12 = "nqn.2014-08.org.nvmexpress:uuid:22207d09-d8af-4ed2-84ec-a6d80b0cf7f6"
3028

3129
hostpsk = "NVMeTLSkey-1:01:YzrPElk4OYy1uUERriPwiiyEJE/+J5ckYpLB+5NHMsR2iBuT:"
3230
hostpsk2 = "NVMeTLSkey-1:02:FTFds4vH4utVcfrOforxbrWIgv+Qq4GQHgMdWwzDdDxE1bAqK2mOoyXxmbJxGeueEVVa/Q==:"
@@ -123,18 +121,14 @@ def test_create_not_secure(caplog, gateway):
123121

124122
def test_create_secure_list(caplog, gateway):
125123
caplog.clear()
126-
cli(["host", "add", "--subsystem", subsystem, "--host-nqn", hostnqn8, hostnqn9, hostnqn10, "--psk", hostpsk5, hostpsk6, hostpsk7, hostpsk])
127-
assert f"There are more PSK values than hosts, will ignore redundant values" in caplog.text
128-
assert f"Adding host {hostnqn8} to {subsystem}: Successful" in caplog.text
129-
assert f"Adding host {hostnqn9} to {subsystem}: Successful" in caplog.text
130-
assert f"Adding host {hostnqn10} to {subsystem}: Successful" in caplog.text
131-
132-
def test_create_secure_list_missing_psk(caplog, gateway):
133-
caplog.clear()
134-
cli(["host", "add", "--subsystem", subsystem, "--host-nqn", hostnqn11, hostnqn12, "--psk", hostpsk8])
135-
assert f"Adding host {hostnqn11} to {subsystem}: Successful" in caplog.text
136-
assert f"Adding host {hostnqn12} to {subsystem}: Successful" in caplog.text
137-
assert f"There are more hosts than PSK values, will assume empty PSK values" in caplog.text
124+
rc = 0
125+
try:
126+
cli(["host", "add", "--subsystem", subsystem, "--host-nqn", hostnqn8, hostnqn9, hostnqn10, "--psk", hostpsk])
127+
except SystemExit as sysex:
128+
rc = int(str(sysex))
129+
pass
130+
assert rc == 2
131+
assert f"error: Can't have more than one host NQN when PSK keys are used" in caplog.text
138132

139133
def test_create_secure_junk_key(caplog, gateway):
140134
caplog.clear()
@@ -150,16 +144,14 @@ def test_create_secure_no_key(caplog, gateway):
150144
rc = int(str(sysex))
151145
pass
152146
assert rc == 2
153-
assert f"error: argument --psk: expected at least one argument" in caplog.text
147+
assert f"error: argument --psk: expected one argument" in caplog.text
154148

155149
def test_list_psk_hosts(caplog, gateway):
156150
caplog.clear()
157151
hosts = cli_test(["host", "list", "--subsystem", subsystem])
158152
found = 0
159-
assert len(hosts.hosts) == 10
153+
assert len(hosts.hosts) == 5
160154
for h in hosts.hosts:
161-
assert h.nqn != hostnqn3
162-
assert h.nqn != hostnqn5
163155
if h.nqn == hostnqn:
164156
found += 1
165157
assert h.use_psk
@@ -175,29 +167,20 @@ def test_list_psk_hosts(caplog, gateway):
175167
elif h.nqn == hostnqn7:
176168
found += 1
177169
assert not h.use_psk
178-
elif h.nqn == hostnqn8:
179-
found += 1
180-
assert h.use_psk
181-
elif h.nqn == hostnqn9:
182-
found += 1
183-
assert h.use_psk
184-
elif h.nqn == hostnqn10:
185-
found += 1
186-
assert h.use_psk
187-
elif h.nqn == hostnqn11:
188-
found += 1
189-
assert h.use_psk
190-
elif h.nqn == hostnqn12:
191-
found += 1
192-
assert not h.use_psk
193170
else:
194171
assert False
195-
assert found == 10
172+
assert found == 5
196173

197174
def test_allow_any_host_with_psk(caplog, gateway):
198175
caplog.clear()
199-
cli(["host", "add", "--subsystem", subsystem, "--host-nqn", "*", "--psk", hostpsk])
200-
assert f"PSK is only allowed for specific hosts, ignoring PSK value \"{hostpsk}\"" in caplog.text
176+
rc = 0
177+
try:
178+
cli(["host", "add", "--subsystem", subsystem, "--host-nqn", "*", "--psk", hostpsk])
179+
except SystemExit as sysex:
180+
rc = int(str(sysex))
181+
pass
182+
assert rc == 2
183+
assert f"error: PSK is only allowed for specific hosts" in caplog.text
201184

202185
def test_list_listeners(caplog, gateway):
203186
caplog.clear()

0 commit comments

Comments
 (0)