Hide binary contents in pillar.{get,item,items} by default
Given https://github.com/saltstack/salt/pull/30268, more people are likely to start using the file_tree external pillar to store binary data.
Given the fact that echoing binary data to the terminal can have weird side-effects, we should hide binary data by default in pillar.get, pillar.item, and pillar.items.
My proposal would be for binary data to be replaced by something like this:
Binary data (12345 bytes, md5:ba7160b551cde281c9ed09852dba538a)
What kind of checksum is performed would be determined by the hash_type minion option.
Implementing this would involve adding a new keyword argument hide_binary, which would default to True.
To get the actual binary data, all references to pillar.get and config.get would need to be updated with hide_binary=False. For this reason, I think it's better to target the next feature release cycle rather than make the change in a point release. There may be people already using custom grains/pillar to store binary data, and a point release probably isn't the best place to force people to update their SLS. I'm open to being convinced to make this change in 2015.8 though.
Also, I think it might be a good idea to apply this same change to grains.get, grains.item, and grains.items.
@cachedout @basepi @thatch45 Thoughts?
We may not need to change every reference to config.get, pillar.get, etc... I'm willing to bet that in the majority of cases binary data is not desired.
The .get functions should not hide binary by default. If you're asking for a pillar value, you want the value.
However, pillar.items and grains.items should hide by default. Those functions are generally not used programmatically but rather to just browse the available pillar/grains data, and so the actual binary data isn't useful.
How will you detect binary data?
Binary pillar data is stored as a string, we check that string for a null character (\0).
So, I've got some thoughts, on the handling of pillar contents, which can now be either
- Text strings (presumably, Unicode or ASCII), or
- Arbitrary binary data (e.g., read from a file).
Thoughts on Detecting Binary vs. Text Data
As far as detecting whether data is 'text' or 'binary',
- There's no guarantee that a binary file will contain a NUL.
- There's no guarantee that a legitimately binary file won't also be a valid UTF-8 string by coincidence.
- There's no guarantee that a legitimately binary file won't also be a valid ASCII string by coincidence.
- There's no guarantee that a UTF-8 string won't have lots of bytes with values >127 (one might be tempted to detect them like this, but shouldn't.)
Some of those things are more likely to happen than others.
An alternative, perhaps, would be to attempt to decode the bytes as UTF-8, and assume it's text data if that succeeds, and binary data if that fails? Would that be better than looking for NUL bytes?
(Of course, that test will fail to detect NUL bytes as binary data, since they're valid UTF-8.)
I'm sure we don't want to restrict Salt's handling of data as text to only ASCII characters...
A Modest Proposal?
Note: I will say bytes() below for clarity, but I am actually referring to Python2 str():
It may be best to simply not attempt to deduce whether pillar contents are unicode() or bytes(), and instead force the user to make that choice explicitly (e.g., file_tree would create pillar values of type bytes(), file.managed pillar_contents would write bytes(), and Jinja templating would cause a unicode(bytes, 'utf-8') operation to occur (and it would be at this point, that it would be appropriate to consider stripping a trailing EOL by default; not before.)
Regardless, I think there'd be value in standardizing how Salt is supposed to handle text/binary data and final EOL in general; ideally, in a way that is consistent, logical, and generally does what you want by default.
Thoughts on Python3 Compatibility
Currently, Salt uses Python2 str type to store pillar contents, and that works fine since Python2 str's are just sequences of bytes. This might be a pain-point for Python3, though, where not every binary file will be valid contents for a str.
Possible resolutions for Python3 include:
- Store pillar contents as bytes() until an operation (e.g., Jinja templating) requires a str() instead. File.managed would write bytes() if contents_pillar is used.
- Make a class/type for pillar data, that knows whether it's holding a str() or a bytes(), and how to convert between them (which is either tricky, or may fail; take your pick.)
or, alternately:
- Make use of and PEP383 and
errors="surrogateescape"... I think this might allow you to feed pillar contents to e.g. Jinja, and have it correctly output the original bytes that were read, even if the contents are not valid unicode string data.
Thoughts on Text Files and their Final EOL
This is not exactly the same topic, but I'd like to mentino that in *nix / under POSIX, '\n' is specified as the line terminator for text files, rather than a line separator; all non-zero-length text-format files should end with a '\n'. I'd like to see Salt making that the default case except where the user directs otherwise.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.