Skip to contentSkip to conversation

Python in Domain Configurations

DISCUSSION Elliott Mitchell 4 participants 6 messages Jul 7, 2023 – Aug 1, 2023

lore
Share & tools
lore
#0 lore raw

There is a distinct weakness in Xen which ends up dragging in domain configuration limitations. I was thinking of using Python for parsing extra data out of domain configuration files. Only to find sample domain configuration files were in fact fully valid Python.

Having experimented, it appears entirely feasible to use libpython to parse current domain configuration files. Looking further it appears Python was in fact used in the past to handle domain configuration files.

Why was this removed?

The only context I could find was 54fbaf446b and
https://wiki.xenproject.org/wiki/PythonInXlConfig which don't explain the reasoning.

Would the maintainers be amenable to revisiting the decision to remove support for full Python in domain configuration files?

The obvious reason is having full Python would increase the size of xl. In most cases I'm unsure this would be too problematic. The case where it would matter is the daemon process. Issue here is plenty of other extra libraries are also bloating the runtime size of xl. I suspect the daemon process really needs to be split into a separate executable to reduce overhead anyway.

Signature · 4 lines
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#1 lore raw
Quoted 7 lines · Elliott Mitchell

The only context I could find was 54fbaf446b and
https://wiki.xenproject.org/wiki/PythonInXlConfig which don't explain the reasoning.

Would the maintainers be amenable to revisiting the decision to remove support for full Python in domain configuration files?

Any chance of this getting a response?

On examination it appears domain configuration files are a proper subset of Python. The interface to the parser is a bit interesting, but it looks fairly simple to replace the parser with libpython.

My goal is to create an init script for some automatically started domains. Issue is there can be ordering concerns with domain start/stop, and this seems best handled by adding an extra setting to the configuration files. If full Python syntax is available, I can use that for this extra data.

Signature · 4 lines
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#2 lore raw
Quoted 15 lines · Elliott Mitchell
Quoted 7 lines · Elliott Mitchell

The only context I could find was 54fbaf446b and
https://wiki.xenproject.org/wiki/PythonInXlConfig which don't explain the reasoning.

Would the maintainers be amenable to revisiting the decision to remove support for full Python in domain configuration files?

Any chance of this getting a response?

On examination it appears domain configuration files are a proper subset of Python. The interface to the parser is a bit interesting, but it looks fairly simple to replace the parser with libpython.

Quoted 5 lines

My goal is to create an init script for some automatically started domains. Issue is there can be ordering concerns with domain start/stop, and this seems best handled by adding an extra setting to the configuration files. If full Python syntax is available, I can use that for this extra data.

Hi,

I don't know full history here, but from my point of view, having a full-fledged script as a config file is undesirable for several reasons:
- it's easy to have unintended side effects of just loading a config file
- loading config file can no longer be assumed to be "cheap"
- dynamic config file means you can no long rely on file timestamp/hash to check if anything changed (I don't think it's an issue for the current xl/libxl, but could be for some higher level tools)
- leads to issues with various sandboxes - for example SELinux policy allowing scripted config file would be excessively permissive

So, IMHO reducing config file from a full python (like it used to be in xend times) into a static file with well defined syntax was an improvement. Lets not go backward.

As for your original problem, IIUC you would like to add some data that would _not_ be interpreted by libxl, right? For that you can use comments with some specific marker for your script. This approach used to work well for SysV init script, and in fact for a very similar use case (ordering and dependencies, among other things).

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#3 lore raw
Quoted 33 lines · Marek Marczykowski-Górecki
Quoted 15 lines · Elliott Mitchell
Quoted 7 lines · Elliott Mitchell

The only context I could find was 54fbaf446b and
https://wiki.xenproject.org/wiki/PythonInXlConfig which don't explain the reasoning.

Would the maintainers be amenable to revisiting the decision to remove support for full Python in domain configuration files?

Any chance of this getting a response?

On examination it appears domain configuration files are a proper subset of Python. The interface to the parser is a bit interesting, but it looks fairly simple to replace the parser with libpython.

Quoted 5 lines

My goal is to create an init script for some automatically started domains. Issue is there can be ordering concerns with domain start/stop, and this seems best handled by adding an extra setting to the configuration files. If full Python syntax is available, I can use that for this extra data.

I don't know full history here, but from my point of view, having a full-fledged script as a config file is undesirable for several reasons:
- it's easy to have unintended side effects of just loading a config file
- loading config file can no longer be assumed to be "cheap"
- dynamic config file means you can no long rely on file timestamp/hash to check if anything changed (I don't think it's an issue for the current xl/libxl, but could be for some higher level tools)
- leads to issues with various sandboxes - for example SELinux policy allowing scripted config file would be excessively permissive

Quoted 3 lines

So, IMHO reducing config file from a full python (like it used to be in xend times) into a static file with well defined syntax was an improvement. Lets not go backward.

I wouldn't really call the existing format "well defined". While there are patterns which are followed, some of those have rather a lot of wiggle room.

I'm still looking, but I suspect libpython can be told to only accept trivial operations (assignments to variables) and reject anything which includes a jump or conditional.

Quoted 5 lines

As for your original problem, IIUC you would like to add some data that would _not_ be interpreted by libxl, right? For that you can use comments with some specific marker for your script. This approach used to work well for SysV init script, and in fact for a very similar use case (ordering and dependencies, among other things).

That is /not/ the issue. xl simply ignores any variables which it doesn't interpret (this is in fact a Bad Thing). I need to know what the limits to the syntax are.

Notice how many init scripts do . /etc/default/<somefile> to load configuration? I'm thinking it would be very handy to use a similar technique to load domain.cfg files, with Python being the interpreter.

I also think some portions of the domain.cfg format might work better with full Python syntax. For example might it be handier to allow:

disk = [

	{
		'vdev': 'xvda',
		'format': 'raw',
		'access': 'rw',
		'target': '/dev/disk/by-path/foo-bar-baz',
	},

]

It looks pretty feasible to replace the low-level parser with libpython. Now to examining the "ast" module and finding out whether a file can be loaded while rejecting conditionals.

Signature · 4 lines
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#4 lore raw

Elliott Mitchell writes ("Re: Python in Domain Configurations"):

Quoted 4 lines · Marek Marczykowski-Górecki
Quoted 3 lines

So, IMHO reducing config file from a full python (like it used to be in xend times) into a static file with well defined syntax was an improvement. Lets not go backward.

I'm no longer working on this codebase, but since I've been CC'd:

I was one of the people who replaced the Python-based config parsing with the current arrangements. We didn't just do this because we were replacing xend (whose use of Python as implementation language made it appear convenient to just read and execute the configs as Python code).

We did it for the reasons Marek gives. It's true that the existing format is not as well specified as it could be. It was intended as a plausible subset of Python literal syntax. We chose that syntax to preserve compatibility with the vast majority of existing config files and to provide something familiar. (And it seems we did achieve those goals.)

The disk configuration syntax is particularly warty, but we inherited much of that from the Python version.

If we had a free choice today, I might advocate for TOML. But I don't see any value in changing the concrete syntax now.

Quoted 8 lines
Quoted 5 lines

As for your original problem, IIUC you would like to add some data that would _not_ be interpreted by libxl, right? For that you can use comments with some specific marker for your script. This approach used to work well for SysV init script, and in fact for a very similar use case (ordering and dependencies, among other things).

That is /not/ the issue. xl simply ignores any variables which it doesn't interpret (this is in fact a Bad Thing).

I forget, but isn't there some kind of scheme for warning about unrecognised configuration options ?

Quoted 1 line

I need to know what the limits to the syntax are.

I agree that it's not great that the syntax is not 100% documented. The parser is in
tools/libs/util/libxlu_cfg_y.y tools/libs/util/libxlu_cfg_l.l
I'm sure patches to improve the docs would be welcome.

Note that it is still a *subset* of Python, so if you wish to use a Python interpreter to parse it in your own tooling, you're very welcome to do so.

Quoted 3 lines

Notice how many init scripts do . /etc/default/<somefile> to load configuration? I'm thinking it would be very handy to use a similar technique to load domain.cfg files, with Python being the interpreter.

I don't think this is a good idea. Both because I don't think the functionality available in a Python interpreter should be available in the libxl configuration, and because Python is a large and complex dependency which we don't want to pull in here.

Quoted 11 lines

I also think some portions of the domain.cfg format might work better with full Python syntax. For example might it be handier to allow:

disk = [

	{
		'vdev': 'xvda',
		'format': 'raw',
		'access': 'rw',
		'target': '/dev/disk/by-path/foo-bar-baz',
	},

]

I agree that something like this would be nice. I don't think it should be done by importing Python. These two files - the main part of the existing parser - is only 183 loc including comments. Extending it (and the support code in libxlu_cfg.c) to do dictionaries as well as lists doesn't seem like it would make it too much bigger.

Thanks,
Ian.

Signature · 4 lines
Ian Jackson <[email protected]>   These opinions are my own.  

Pronouns: they/he.  If I emailed you from @fyvzl.net or @evade.org.uk,
that is a private address which bypasses my fierce spamfilter.
#5 lore raw
Quoted 21 lines · Ian Jackson

Elliott Mitchell writes ("Re: Python in Domain Configurations"):

Quoted 4 lines · Marek Marczykowski-Górecki
Quoted 3 lines

So, IMHO reducing config file from a full python (like it used to be in xend times) into a static file with well defined syntax was an improvement. Lets not go backward.

I'm no longer working on this codebase, but since I've been CC'd:

I was one of the people who replaced the Python-based config parsing with the current arrangements. We didn't just do this because we were replacing xend (whose use of Python as implementation language made it appear convenient to just read and execute the configs as Python code).

We did it for the reasons Marek gives. It's true that the existing format is not as well specified as it could be. It was intended as a plausible subset of Python literal syntax. We chose that syntax to preserve compatibility with the vast majority of existing config files and to provide something familiar. (And it seems we did achieve those goals.)

Quoted 2 lines

The disk configuration syntax is particularly warty, but we inherited much of that from the Python version.

Okay. I do note allowing full Python does make domain creation by script easier. While I have one use for re-adding the functionality, I'm sure someone else would come up with other high-value ones.

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines

As for your original problem, IIUC you would like to add some data that would _not_ be interpreted by libxl, right? For that you can use comments with some specific marker for your script. This approach used to work well for SysV init script, and in fact for a very similar use case (ordering and dependencies, among other things).

That is /not/ the issue. xl simply ignores any variables which it doesn't interpret (this is in fact a Bad Thing).

I forget, but isn't there some kind of scheme for warning about unrecognised configuration options ?

There certainly should be and there may have been one in the past, but no there isn't one now. One advantage for using Python is processed data could be removed when creating the domain and anything left over could be warned about. Though such could be added to the existing parser too.

Quoted 7 lines
Quoted 1 line

I need to know what the limits to the syntax are.

I agree that it's not great that the syntax is not 100% documented. The parser is in
tools/libs/util/libxlu_cfg_y.y tools/libs/util/libxlu_cfg_l.l
I'm sure patches to improve the docs would be welcome.

That is merely the DFA for the grammar. There is a bunch of extra for interfacing with the parser, then there is the layer above.

Quoted 3 lines

Note that it is still a *subset* of Python, so if you wish to use a Python interpreter to parse it in your own tooling, you're very welcome to do so.

Yup, and this will make that simpler. Having Python dictionaries would make some things even easier though.

Quoted 8 lines
Quoted 3 lines

Notice how many init scripts do . /etc/default/<somefile> to load configuration? I'm thinking it would be very handy to use a similar technique to load domain.cfg files, with Python being the interpreter.

I don't think this is a good idea. Both because I don't think the functionality available in a Python interpreter should be available in the libxl configuration, and because Python is a large and complex dependency which we don't want to pull in here.

While PvGRUB and Tianocore seem likely to displace PyGRUB, PyGRUB still functions on ARM (which PvGRUB isn't ported to yet). As such there is still a dependency.

Too me the greater concern is the daemon process. Issue is, for the daemon process the large amount of functionality built into xl is a similar liability. Even the temporary data seems a large liability for the daemon. Additionally if that was a separate executable, then the process name could be changed via execve() and make it clearer that process was expected to remain behind.

Quoted 12 lines
Quoted 11 lines

I also think some portions of the domain.cfg format might work better with full Python syntax. For example might it be handier to allow:

disk = [

	{
		'vdev': 'xvda',
		'format': 'raw',
		'access': 'rw',
		'target': '/dev/disk/by-path/foo-bar-baz',
	},

]

Quoted 5 lines

I agree that something like this would be nice. I don't think it should be done by importing Python. These two files - the main part of the existing parser - is only 183 loc including comments. Extending it (and the support code in libxlu_cfg.c) to do dictionaries as well as lists doesn't seem like it would make it too much bigger.

That ignores all the interfacing code. Add in the interfacing and it is closer to 2000 lines. If you merely count the file parser that is only 1000 lines.

I suspect I could interface to libpython with 200-300 lines. Those would allow far more functionality than what is currently implemented.

Quoted 5 lines · Marek Marczykowski-Górecki

I don't know full history here, but from my point of view, having a full-fledged script as a config file is undesirable for several reasons:
- it's easy to have unintended side effects of just loading a config file
- loading config file can no longer be assumed to be "cheap"

If you use full Python functionality in the domain.cfg files, it could indeed do interesting things during config load. This though is good reason not to do those things.

Include in documentation, "please don't use complicated Python functionality in domain configurations"? Guarding against this seems to be trying to guard against shooting oneself in the foot. Users will find ways to cause odd situations no matter what you do.

Quoted 3 lines

- dynamic config file means you can no long rely on file timestamp/hash to check if anything changed (I don't think it's an issue for the current xl/libxl, but could be for some higher level tools)

I'm not looking for dynamic configurations. I'm mostly thinking of allowing better configuration constructs. Both disks and network could use Python dictionaries for a better structure.

Quoted 2 lines

- leads to issues with various sandboxes - for example SELinux policy allowing scripted config file would be excessively permissive

I suspect someone not using complex Python constructs could have simpler SE Linux policy.

Having done a bit of experimentation, it appears libpython can be made to disable some functionality during loading. Notably it appears one could restrict Python to mostly dumb assignments during loading.

Further, if this got to the stage of a PoC it should be possible for it to live alongside the existing parser for a time.

Signature · 4 lines
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445