Various fixes & improvments in the modern distro validation script (#12387)

Blue committed Dec 16, 2024 at 15:23 UTC e6545b415f3d79a577a4ead295fa9c19e791f79b
1 file changed +33 -10
distributions/validate-modern.py
+33 -10
@@ -155,7 +155,7 @@ def read_config_keys(config: configparser.ConfigParser) -> dict:
155
156 for section in config.sections():
157 for key in config[section].keys():
158 - keys[f'{section}.{key}'] = config[section][key]
158 + keys[f'{section}.{key.lower()}'] = config[section][key]
159
160 return keys
161
@@ -233,8 +233,31 @@ def read_systemd_enabled_units(flavor: str, name: str, tar) -> dict:
233
234 return units
235
236 +# Manually implemented because os.path.realpath tries to resolve local symlinks
237 +def linux_real_path(path: str):
238 + components = path.split('/')
239
237 -def get_tar_file(tar, path: str, follow_symlink=False):
240 + result = []
241 + for e in components:
242 + if e == '.' or not e:
243 + continue
244 + elif e == '..':
245 + if result:
246 + del result[-1]
247 + continue
248 +
249 + result.append(e)
250 +
251 + real_path = '/'.join(result)
252 + if path and path[0] == '/':
253 + return '/' + real_path
254 + else:
255 + return real_path
256 +
257 +def get_tar_file(tar, path: str, follow_symlink=False, symlink_depth=10):
258 + if symlink_depth < 0:
259 + print(f'Warning: Exceeded maximum symlink depth when reading: {path}')
260 + return None, None
261
262 # Tar members can be formated as /{path}, {path}, or ./{path}
263 if path.startswith('/'):
@@ -247,9 +270,9 @@ def get_tar_file(tar, path: str, follow_symlink=False):
270 def follow_if_symlink(info, path: str):
271 if follow_symlink and info.issym():
272 if info.linkpath.startswith('/'):
250 - return get_tar_file(tar, info.linkpath, follow_symlink=True)
273 + return get_tar_file(tar, info.linkpath, follow_symlink=True, symlink_depth=symlink_depth - 1)
274 else:
252 - return get_tar_file(tar, f'{os.path.dirname(path)}/{info.linkpath}', follow_symlink=True)
275 + return get_tar_file(tar, linux_real_path(os.path.dirname(path) + '/' + info.linkpath), follow_symlink=True, symlink_depth=symlink_depth -1)
276 else:
277 return info, path
278
@@ -268,9 +291,9 @@ def get_tar_file(tar, path: str, follow_symlink=False):
291 parent_path = os.path.dirname(path)
292 if parent_path != path:
293 try:
271 - parent_info, real_parent_path = get_tar_file(tar, parent_path, follow_symlink=True)
272 - if real_parent_path != parent_path:
273 - return get_tar_file(tar, f'{real_parent_path}/{os.path.basename(path)}', follow_symlink=True)
294 + parent_info, real_parent_path = get_tar_file(tar, parent_path, follow_symlink=True, symlink_depth=symlink_depth - 1)
295 + if real_parent_path is not None and real_parent_path != parent_path:
296 + return get_tar_file(tar, f'{real_parent_path}/{os.path.basename(path)}', follow_symlink=True, symlink_depth=symlink_depth -1)
297 except KeyError:
298 pass
299
@@ -327,7 +350,7 @@ def read_tar(flavor: str, name: str, file, elf_magic: str):
350
351 keys = read_config_keys(config)
352
330 - unexpected_keys = [e for e in keys if e not in valid_keys]
353 + unexpected_keys = [e for e in keys if e.lower() not in valid_keys]
354 if unexpected_keys:
355 error(flavor, name, f'Found unexpected_keys in "{path}": {unexpected_keys}')
356 else:
@@ -337,7 +360,7 @@ def read_tar(flavor: str, name: str, file, elf_magic: str):
360
361 defaultUid = None
362 if validate_mode('/etc/wsl-distribution.conf', [oct(0o664), oct(0o644)], 0, 0):
340 - config = validate_config('/etc/wsl-distribution.conf', ['oobe.command', 'oobe.defaultuid', 'shortcut.icon', 'oobe.defaultname', 'windowsterminal.profileTemplate'])
363 + config = validate_config('/etc/wsl-distribution.conf', ['oobe.command', 'oobe.defaultuid', 'shortcut.icon', 'oobe.defaultname', 'windowsterminal.profiletemplate'])
364
365 if oobe_command := config.get('oobe.command', None):
366 validate_mode(oobe_command, [oct(0o775), oct(0o755)], 0, 0)
@@ -432,7 +455,7 @@ def warning(flavor: str, distribution: str, message: str):
455 global warnings
456
457 message = f'{flavor}/{distribution}: {message}'
435 - click.secho(f'Warning: {message}', fg='red')
458 + click.secho(f'Warning: {message}', fg='yellow')
459
460 warnings.append(message)
461 if __name__ == "__main__":