netplan crashes when there is an empty origin hint file

Bug #1949884 reported by Michael Vogt
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Netplan
In Progress
Undecided
Lukas Märdian

Bug Description

When there is an empty netplan origin hint file and one tries to write it will crash netplan:

qemu:ubuntu-core-20-64 .../tests/core/netplan-cfg# echo "" > /etc/netplan/90-snapd-conf.yaml
qemu:ubuntu-core-20-64 .../tests/core/netplan-cfg# netplan set --origin-hint 90-snapd-conf network.bridges.br54.dhcp4=true
Traceback (most recent call last):
  File "/usr/sbin/netplan", line 23, in <module>
    netplan.main()
  File "/usr/share/netplan/netplan/cli/core.py", line 50, in main
    self.run_command()
  File "/usr/share/netplan/netplan/cli/utils.py", line 310, in run_command
    self.func()
  File "/usr/share/netplan/netplan/cli/commands/set.py", line 53, in run
    self.run_command()
  File "/usr/share/netplan/netplan/cli/utils.py", line 310, in run_command
    self.func()
  File "/usr/share/netplan/netplan/cli/commands/set.py", line 106, in command_set
    self.write_file(subtree, hint + '.yaml', self.root_dir)
  File "/usr/share/netplan/netplan/cli/commands/set.py", line 156, in write_file
    new_tree = self.merge(config, set_tree)
  File "/usr/share/netplan/netplan/cli/commands/set.py", line 133, in merge
    if key in a:
TypeError: argument of type 'NoneType' is not iterable

Revision history for this message
Michael Vogt (mvo) wrote :

Fwiw, the same seems to happen if the origin file does not exist and a subtree is unset (I can file a different bug for this). E.g.:

"""
# netplan set --origin-hint 90-some-random-thing-not-on-disk network=null
Traceback (most recent call last):
  File "/usr/sbin/netplan", line 23, in <module>
    netplan.main()
  File "/usr/share/netplan/netplan/cli/core.py", line 50, in main
    self.run_command()
  File "/usr/share/netplan/netplan/cli/utils.py", line 310, in run_command
    self.func()
  File "/usr/share/netplan/netplan/cli/commands/set.py", line 53, in run
    self.run_command()
  File "/usr/share/netplan/netplan/cli/utils.py", line 310, in run_command
    self.func()
  File "/usr/share/netplan/netplan/cli/commands/set.py", line 106, in command_set
    self.write_file(subtree, hint + '.yaml', self.root_dir)
  File "/usr/share/netplan/netplan/cli/commands/set.py", line 176, in write_file
    raise Exception('Invalid input: {}'.format(set_tree))
Exception: Invalid input: {'network': None}
"""

Revision history for this message
Lukas Märdian (slyon) wrote :

I can confirm these issues and prepared a few reproducer testcases (for test_cli_get_set.py), let's try to get those resolved!

    def test_set_empty_hint_file(self):
        empty_file = os.path.join(self.workdir.name, 'etc', 'netplan', '00-empty.yaml')
        open(empty_file, 'w').close() # touch empty file
        self._set(['ethernets.eth0.dhcp4=true', '--origin-hint=00-empty'])
        print(empty_file, flush=True)
        self.assertTrue(os.path.isfile(empty_file))
        with open(empty_file, 'r') as f:
            self.assertIn('network:\n ethernets:\n eth0:\n dhcp4: true', f.read())

    def test_set_network_null_hint(self):
        not_a_file = os.path.join(self.workdir.name, 'etc', 'netplan', '00-no-exist.yaml')
        self._set(['network=null', '--origin-hint=00-no-exist'])
        self.assertFalse(os.path.isfile(not_a_file))

    def test_set_network_null(self):
        not_a_file = os.path.join(self.workdir.name, 'etc', 'netplan', '00-no-exist.yaml')
        self._set(['network=null'])
        self.assertFalse(os.path.isfile(not_a_file))

Changed in netplan:
status: New → Triaged
Revision history for this message
Lukas Märdian (slyon) wrote :

FTR: This is being worked on here: https://github.com/canonical/netplan/pull/246

Changed in netplan:
status: Triaged → In Progress
assignee: nobody → Lukas Märdian (slyon)
Revision history for this message
Lukas Märdian (slyon) wrote :

I'm marking this a duplicate of LP: #1946957 as it is the same class of errors and fixed in the same upstream PR.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.