Skip to content

Commit b020d0f

Browse files
committed
Check subsystem and host NQN validity before passing them to SPDK.
Fixes #364 Signed-off-by: Gil Bregman <gbregman@il.ibm.com>
1 parent c97b09b commit b020d0f

File tree

8 files changed

+283
-99
lines changed

8 files changed

+283
-99
lines changed

ceph-nvmeof.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ log_level=debug
3636
#prometheus_port = 10008
3737
#prometheus_bdev_pools = rbd
3838
#prometheus_stats_interval = 10
39+
#verify_nqns = True
3940

4041
[discovery]
4142
addr = 0.0.0.0

control/cli.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
from .proto import gateway_pb2_grpc as pb2_grpc
2424
from .proto import gateway_pb2 as pb2
2525
from .config import GatewayConfig
26-
from .config import GatewayEnumUtils
26+
from .utils import GatewayUtils
27+
from .utils import GatewayEnumUtils
2728

2829
BASE_GATEWAY_VERSION="0.0.7"
2930

@@ -187,7 +188,7 @@ def stub(self):
187188
def connect(self, host, port, client_key, client_cert, server_cert):
188189
"""Connects to server and sets stub."""
189190
# We need to enclose IPv6 addresses in brackets before concatenating a colon and port number to it
190-
host = GatewayConfig.escape_address_if_ipv6(host)
191+
host = GatewayUtils.escape_address_if_ipv6(host)
191192
server = f"{host}:{port}"
192193

193194
if client_key and client_cert:
@@ -550,15 +551,15 @@ def subsystem_add(self, args):
550551
out_func, err_func = self.get_output_functions(args)
551552
if args.max_namespaces == None:
552553
args.max_namespaces = 256
553-
if args.max_namespaces < 0:
554+
if args.max_namespaces <= 0:
554555
self.cli.parser.error("--max-namespaces value must be positive")
555556
if not args.subsystem:
556557
self.cli.parser.error("--subsystem argument is mandatory for add command")
557558
if args.force:
558559
self.cli.parser.error("--force argument is not allowed for add command")
559560
if args.enable_ha and not args.ana_reporting:
560561
self.cli.parser.error("ANA reporting must be enabled when HA is active")
561-
if args.subsystem == GatewayConfig.DISCOVERY_NQN:
562+
if args.subsystem == GatewayUtils.DISCOVERY_NQN:
562563
self.cli.parser.error("Can't add a discovery subsystem")
563564

564565
req = pb2.create_subsystem_req(subsystem_nqn=args.subsystem,
@@ -608,7 +609,7 @@ def subsystem_del(self, args):
608609
self.cli.parser.error("--ana-reporting argument is not allowed for del command")
609610
if args.enable_ha:
610611
self.cli.parser.error("--enable-ha argument is not allowed for del command")
611-
if args.subsystem == GatewayConfig.DISCOVERY_NQN:
612+
if args.subsystem == Utils.DISCOVERY_NQN:
612613
self.cli.parser.error("Can't delete a discovery subsystem")
613614

614615
req = pb2.delete_subsystem_req(subsystem_nqn=args.subsystem, force=args.force)
@@ -743,16 +744,16 @@ def listener_add(self, args):
743744
if not args.traddr:
744745
self.cli.parser.error("--traddr argument is mandatory for add command")
745746

746-
if not args.trsvcid:
747+
if args.trsvcid == None:
747748
args.trsvcid = 4420
748-
elif args.trsvcid < 0:
749+
elif args.trsvcid <= 0:
749750
self.cli.parser.error("trsvcid value must be positive")
750751
if not args.trtype:
751752
args.trtype = "TCP"
752753
if not args.adrfam:
753754
args.adrfam = "IPV4"
754755

755-
traddr = GatewayConfig.escape_address_if_ipv6(args.traddr)
756+
traddr = GatewayUtils.escape_address_if_ipv6(args.traddr)
756757
trtype = None
757758
adrfam = None
758759
if args.trtype:
@@ -807,16 +808,16 @@ def listener_del(self, args):
807808
self.cli.parser.error("--gateway-name argument is mandatory for del command")
808809
if not args.traddr:
809810
self.cli.parser.error("--traddr argument is mandatory for del command")
810-
if not args.trsvcid:
811+
if args.trsvcid == None:
811812
self.cli.parser.error("--trsvcid argument is mandatory for del command")
812-
if args.trsvcid < 0:
813+
if args.trsvcid <= 0:
813814
self.cli.parser.error("trsvcid value must be positive")
814815
if not args.trtype:
815816
args.trtype = "TCP"
816817
if not args.adrfam:
817818
args.adrfam = "IPV4"
818819

819-
traddr = GatewayConfig.escape_address_if_ipv6(args.traddr)
820+
traddr = GatewayUtils.escape_address_if_ipv6(args.traddr)
820821
trtype = None
821822
adrfam = None
822823
if args.trtype:
@@ -874,7 +875,7 @@ def listener_list(self, args):
874875
self.cli.parser.error("--trtype argument is not allowed for list command")
875876
if args.adrfam:
876877
self.cli.parser.error("--adrfam argument is not allowed for list command")
877-
if args.trsvcid:
878+
if args.trsvcid != None:
878879
self.cli.parser.error("--trsvcid argument is not allowed for list command")
879880

880881
listeners_info = None
@@ -1161,9 +1162,9 @@ def ns_add(self, args):
11611162
args.block_size = 512
11621163
if args.load_balancing_group == None:
11631164
args.load_balancing_group = 1
1164-
if args.load_balancing_group and args.load_balancing_group < 0:
1165+
if args.load_balancing_group <= 0:
11651166
self.cli.parser.error("load-balancing-group value must be positive")
1166-
if args.nsid and args.nsid <= 0:
1167+
if args.nsid != None and args.nsid <= 0:
11671168
self.cli.parser.error("nsid value must be positive")
11681169
if args.size != None:
11691170
self.cli.parser.error("--size argument is not allowed for add command")
@@ -1173,8 +1174,6 @@ def ns_add(self, args):
11731174
self.cli.parser.error("--rbd-image argument is mandatory for add command")
11741175
if args.block_size <= 0:
11751176
self.cli.parser.error("block-size value must be positive")
1176-
if args.load_balancing_group <= 0:
1177-
self.cli.parser.error("load-balancing-group value must be positive")
11781177
if args.rw_ios_per_second != None:
11791178
self.cli.parser.error("--rw-ios-per-second argument is not allowed for add command")
11801179
if args.rw_megabytes_per_second != None:
@@ -1227,9 +1226,9 @@ def ns_del(self, args):
12271226
"""Deletes a namespace from a subsystem."""
12281227

12291228
out_func, err_func = self.get_output_functions(args)
1230-
if not args.nsid and not args.uuid:
1229+
if args.nsid == None and args.uuid == None:
12311230
self.cli.parser.error("At least one of --nsid or --uuid arguments is mandatory for del command")
1232-
if args.nsid and args.nsid < 0:
1231+
if args.nsid != None and args.nsid <= 0:
12331232
self.cli.parser.error("nsid value must be positive")
12341233
if args.size != None:
12351234
self.cli.parser.error("--size argument is not allowed for del command")
@@ -1239,7 +1238,7 @@ def ns_del(self, args):
12391238
self.cli.parser.error("--rbd-pool argument is not allowed for del command")
12401239
if args.rbd_image != None:
12411240
self.cli.parser.error("--rbd-image argument is not allowed for del command")
1242-
if args.load_balancing_group:
1241+
if args.load_balancing_group != None:
12431242
self.cli.parser.error("--load-balancing-group argument is not allowed for del command")
12441243
if args.rw_ios_per_second != None:
12451244
self.cli.parser.error("--rw-ios-per-second argument is not allowed for del command")
@@ -1288,13 +1287,13 @@ def ns_resize(self, args):
12881287
"""Resizes a namespace."""
12891288

12901289
out_func, err_func = self.get_output_functions(args)
1291-
if not args.nsid and not args.uuid:
1290+
if args.nsid == None and args.uuid == None:
12921291
self.cli.parser.error("At least one of --nsid or --uuid arguments is mandatory for resize command")
1293-
if args.nsid and args.nsid < 0:
1292+
if args.nsid != None and args.nsid <= 0:
12941293
self.cli.parser.error("nsid value must be positive")
1295-
if not args.size:
1294+
if args.size == None:
12961295
self.cli.parser.error("--size argument is mandatory for resize command")
1297-
if args.size < 0:
1296+
if args.size <= 0:
12981297
self.cli.parser.error("size value must be positive")
12991298
if args.block_size != None:
13001299
self.cli.parser.error("--block-size argument is not allowed for resize command")
@@ -1379,7 +1378,7 @@ def ns_list(self, args):
13791378
"""Lists namespaces on a subsystem."""
13801379

13811380
out_func, err_func = self.get_output_functions(args)
1382-
if args.nsid and args.nsid < 0:
1381+
if args.nsid != None and args.nsid <= 0:
13831382
self.cli.parser.error("nsid value must be positive")
13841383
if args.size != None:
13851384
self.cli.parser.error("--size argument is not allowed for list command")
@@ -1486,9 +1485,9 @@ def ns_get_io_stats(self, args):
14861485
"""Get namespace IO statistics."""
14871486

14881487
out_func, err_func = self.get_output_functions(args)
1489-
if not args.nsid and not args.uuid:
1488+
if args.nsid == None and args.uuid == None:
14901489
self.cli.parser.error("At least one of --nsid or --uuid arguments is mandatory for get_io_stats command")
1491-
if args.nsid and args.nsid < 0:
1490+
if args.nsid != None and args.nsid <= 0:
14921491
self.cli.parser.error("nsid value must be positive")
14931492
if args.size != None:
14941493
self.cli.parser.error("--size argument is not allowed for get_io_stats command")
@@ -1587,9 +1586,9 @@ def ns_change_load_balancing_group(self, args):
15871586
"""Change namespace load balancing group."""
15881587

15891588
out_func, err_func = self.get_output_functions(args)
1590-
if not args.nsid and not args.uuid:
1589+
if args.nsid == None and args.uuid == None:
15911590
self.cli.parser.error("At least one of --nsid or --uuid arguments is mandatory for change_load_balancing_group command")
1592-
if args.nsid and args.nsid < 0:
1591+
if args.nsid != None and args.nsid <= 0:
15931592
self.cli.parser.error("nsid value must be positive")
15941593
if args.load_balancing_group == None:
15951594
self.cli.parser.error("--load-balancing-group argument is mandatory for change_load_balancing_group command")
@@ -1659,9 +1658,9 @@ def ns_set_qos(self, args):
16591658
"""Set namespace QOS limits."""
16601659

16611660
out_func, err_func = self.get_output_functions(args)
1662-
if not args.nsid and not args.uuid:
1661+
if args.nsid == None and args.uuid == None:
16631662
self.cli.parser.error("At least one of --nsid or --uuid arguments is mandatory for set_qos command")
1664-
if args.nsid and args.nsid < 0:
1663+
if args.nsid != None and args.nsid <= 0:
16651664
self.cli.parser.error("nsid value must be positive")
16661665
if args.load_balancing_group != None:
16671666
self.cli.parser.error("--load-balancing-group argument is not allowed for set_qos command")

control/config.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,13 @@
1515
import gzip
1616
import shutil
1717

18-
class GatewayEnumUtils:
19-
def get_value_from_key(e_type, keyval, ignore_case = False):
20-
val = None
21-
try:
22-
key_index = e_type.keys().index(keyval)
23-
val = e_type.values()[key_index]
24-
except ValueError:
25-
pass
26-
except IndexError:
27-
pass
28-
29-
if ignore_case and val == None and type(keyval) == str:
30-
val = get_value_from_key(e_type, keyval.lower(), False)
31-
if ignore_case and val == None and type(keyval) == str:
32-
val = get_value_from_key(e_type, keyval.upper(), False)
33-
34-
return val
35-
36-
def get_key_from_value(e_type, val):
37-
keyval = None
38-
try:
39-
val_index = e_type.values().index(val)
40-
keyval = e_type.keys()[val_index]
41-
except ValueError:
42-
pass
43-
except IndexError:
44-
pass
45-
return keyval
46-
4718
class GatewayConfig:
4819
"""Loads and returns config file settings.
4920
5021
Instance attributes:
5122
config: Config parser object
5223
"""
5324

54-
DISCOVERY_NQN = "nqn.2014-08.org.nvmexpress.discovery"
5525
CEPH_RUN_DIRECTORY = "/var/run/ceph/"
5626

5727
def __init__(self, conffile):
@@ -103,13 +73,6 @@ def dump_config_file(self, logger):
10373
except Exception:
10474
pass
10575

106-
# We need to enclose IPv6 addresses in brackets before concatenating a colon and port number to it
107-
def escape_address_if_ipv6(addr) -> str:
108-
ret_addr = addr
109-
if ":" in addr and not addr.strip().startswith("["):
110-
ret_addr = f"[{addr}]"
111-
return ret_addr
112-
11376
class GatewayLogger:
11477
CEPH_LOG_DIRECTORY = "/var/log/ceph/"
11578
MAX_LOG_FILE_SIZE_DEFAULT = 10

control/discovery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import logging
1414
from .config import GatewayConfig
1515
from .state import GatewayState, LocalGatewayState, OmapGatewayState, GatewayStateHandler
16-
from .config import GatewayEnumUtils
16+
from .utils import GatewayEnumUtils
1717
from .config import GatewayLogger
1818
from .proto import gateway_pb2 as pb2
1919

0 commit comments

Comments
 (0)