canonical / canonical/cloud-init

schema validation fails in --annotation

Open
#5,656 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
3.8k
Forks
1.1k
Avg merge
3d 8h
Merged PRs (30d)
22

Description

# Bug report
I see a traceback while annotating an invalid configuration.

```python
# cloud-init schema --annotate -c ./tmp.yml
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/cloudinit/config/schema.py", line 1178, in validate_cloudconfig_file
if not validate_cloudconfig_schema(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/config/schema.py", line 825, in validate_cloudconfig_schema
raise SchemaValidationError(errors, deprecations + info_deprecations)
cloudinit.config.schema.SchemaValidationError: Cloud config schema errors: : Additional properties are not allowed ('lvm', 'mount' were unexpected)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/bin/cloud-init", line 33, in
sys.exit(load_entry_point('cloud-init==24.2', 'console_scripts', 'cloud-init')())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/cmd/main.py", line 1183, in main
retval = util.log_time(
^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/util.py", line 2810, in log_time
ret = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/config/schema.py", line 1893, in handle_schema_args
performed_schema_validation = validate_cloudconfig_file(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/config/schema.py", line 1195, in validate_cloudconfig_file
annotated_cloudconfig_file(
File "/usr/lib/python3/dist-packages/cloudinit/config/schema.py", line 969, in annotated_cloudconfig_file
return _Annotator(cloudconfig, original_content, schemamarks).annotate(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/config/schema.py", line 942, in annotate
errors_by_line = self._build_errors_by_line(schema_errors)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/config/schema.py", line 868, in _build_errors_by_line
errors_by_line[self._schemamarks[path]].append(msg)
~~~~~~~~~~~~~~~~~^^^^^^
KeyError: ''
```

The failure appears to happen when 2 or more invalid top level keys exist.

## Steps to reproduce the problem

`cloud-init schema --annotate -c ./tmp.yml`

where `tmp.yml` contains:

```yaml
#cloud-config
lvm: tmp
mount: tmp
```

Contributor guide

Open the contributing guide

Research direction

The traceback points to cloudinit/config/schema.py, especially annotated_cloudconfig_file and _build_errors_by_line. Reproduce it with cloud-init schema --annotate -c ./tmp.yml using the two invalid top-level keys, then trace how the empty schema-error path is mapped to schema marks. Done means annotation reports the invalid keys without raising a traceback.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.