Page MenuHomeVyOS Platform

Use a compliant TOML parser
Closed, ResolvedPublic

Description

The module from python3-toml is not a correct TOML implementation (at the moment at least) — it cannot handle triple-quoted strings.

That is a big problem for any includes in flavors that contain quotes, since trying to use triple quotes to avoid having to watch the quote type leads to broken data.

For example:

vyos_bld@17c979e9fa91:/vyos$ python3
Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import toml
>>> 
>>> with open('data/build-flavors/iso-test.toml', 'r') as f:
...   s = f.read()
... 
>>> print(s)
# Generic (aka "universal") ISO image

image_format = "iso"

# Include these packages in the image regardless of the architecture
packages = [
  # QEMU and Xen guest tools exist for multiple architectures
  "qemu-guest-agent",
  "vyos-xe-guest-utilities",
]

[[includes_chroot]]
path = "opt/vyatta/etc/config/config.test"
data = '''
system {
    host-name totally-not-vyos
    login {
        user vyos {
            authentication {
                encrypted-password $6$QxPS.uk6mfo$9QBSo8u1FkH16gMyAVhus6fU3LOzvLR9Z9.82m3tiHFAxTtIkhaZSWssSgzt4v4dGAL8rhVQxTg0oAG9/q11h/
                plaintext-password ""
            }
        }
    }
    syslog {
        global {
            facility all {
                level info
            }
            facility protocols {
                level debug
            }
        }
    }
    ntp {
        server "time1.vyos.net"
        server "time2.vyos.net"
        server "time3.vyos.net"
    }
    console {
        device ttyS0 {
            speed 115200
        }
    }
    config-management {
        commit-revisions 150
    }
}

interfaces {
    loopback lo {
    }
}
'''

[architectures.amd64]
  # Hyper-V and VMware guest tools are x86-only
  packages = ["hyperv-daemons", "vyos-1x-vmware"]

>>> t = toml.loads(s)
>>> print(t["includes_chroot"][0]["data"]
... )
system {
    host-name totally-not-vyos
    login {
        user vyos {
            authentication {
                encrypted-password $6$QxPS.uk6mfo$9QBSo8u1FkH16gMyAVhus6fU3LOzvLR9Z9.82m3tiHFAxTtIkhaZSWssSgzt4v4dGAL8rhVQxTg0oAG9/q11h/
                plaintext-password """            }
        }
    }
    syslog {
        global {
            facility all {
                level info
            }
            facility protocols {
                level debug
            }
        }
    }
    ntp {
        server "time1.vyos.net"
        server "time2.vyos.net"
        server "time3.vyos.net"
    }
    console {
        device ttyS0 {
            speed 115200
        }
    }
    config-management {
        commit-revisions 150
    }
}

interfaces {
    loopback lo {
    }
}

As you can see, python3-toml thinks that the "" quote pair was supposed contains a stray quote and mangles the string:

login {
    user vyos {
        authentication {
            encrypted-password $6$QxPS.uk6mfo$9QBSo8u1FkH16gMyAVhus6fU3LOzvLR9Z9.82m3tiHFAxTtIkhaZSWssSgzt4v4dGAL8rhVQxTg0oAG9/q11h/
            plaintext-password """            }
    }
}

The good news is that python3-tomli works correctly and it's a more active project overall, and it has the same loads() interface.

Details

Difficulty level
Unknown (require assessment)
Version
-
Why the issue appeared?
Will be filled on close
Is it a breaking change?
Perfectly compatible
Issue type
Bug (incorrect behavior)

Event Timeline

dmbaturin created this task.