From 29ab6cc0ed4bc909c7e457ea6e8476fabfc49371 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Mon, 3 Jun 2024 15:47:17 +0200 Subject: disk: improve disk setup command validation Checks: --type is gpt or dos. --num must have numeric argument. --part defines 4 coma-separated values. --part number must be an integer. --part number is not repeated between partitions. --part partition type is a known type. --part filesystem type is a known type. --part size has a valid format. --- cli/objects/disks.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/cli/objects/disks.py b/cli/objects/disks.py index f38f420..b832b56 100644 --- a/cli/objects/disks.py +++ b/cli/objects/disks.py @@ -37,8 +37,7 @@ class OgDisk(): if len(match.groups()) == 2: number, unit = match.groups() return str(int(float(number)*units[unit])) - print(f'Error parsing size {size}. Aborting.') - return None + raise ValueError(f'error parsing size {size}. Examples of valid sizes: 200M, 2G, 1T') disk_type_map = {'dos': 'MSDOS', 'gpt': 'GPT'} part_types = ['LINUX', 'EFI', 'WINDOWS', 'CACHE'] @@ -96,21 +95,45 @@ class OgDisk(): print("Missing --client-ip, or --room-id/--center-id. No clients provided.") return None + if not parsed_args.num.isdigit(): + print(f'Invalid disk number: must be an integer value') + return + payload = {'clients': parsed_args.client_ip, 'type': disk_type_map[parsed_args.type], 'disk': str(parsed_args.num), 'cache': '0', 'cache_size': '0', 'partition_setup': []} + + defined_part_indices = set() for i, p in enumerate(parsed_args.part, start=1): + p = p[0] + + if len(p) != 4: + print(f'Invalid partition: requires "num,part_scheme,fs,size", "{','.join(p)}" provided') + return part_num, code, fs, size = p[0], p[1].upper(), p[2].upper(), p[3] + if not part_num.isdigit(): + print(f'Invalid partition: the first parameter must be a number, "{part_num}" provided') + return + + if part_num in defined_part_indices: + print(f'Invalid partition: partition number "{part_num}" has multiple definitions') + return + + defined_part_indices.add(part_num) + if code not in part_types: - print( - f'Specified partition type {code} is not supported. Aborting...') + print(f'Invalid partition {i}: specified partition type {code} is not supported. The supported formats are {part_types}') return + if fs not in fs_types: - print( - f'Specified filesystem {code} is not supported. Aborting...') + print(f'Invalid partition {i}: specified filesystem {fs} is not supported. The supported formats are {fs_types}') + return + try: + size = parse_size(size) + except ValueError as error: + print(f'Invalid partition {i}: {str(error)}') return - size = parse_size(size) for j in range(i, int(part_num)): part = {'partition': str(j), 'code': 'EMPTY', -- cgit v1.2.3-18-g5258