[BUG] XML iterfind error
@frogunder is already working on this.
Since Nov 3, 2021.
- Dominant language
- Python
- Stars
- 15.7k
- Forks
- 5.6k
- Avg merge
- 2d 44m
- Merged PRs (30d)
- 80
Description
Description
XML state and module can not iterate across multiple values it only finds the first value only.
Setup
(Please provide relevant configs and/or SLS files (be sure to remove sensitive info. There is no general set-up of Salt.)
Please be as specific as possible and give set-up details.
- on-prem machine
- [AWS Zen ] VM (Virtualbox, KVM, etc. please specify)
- VM running on a cloud service, please be explicit and add details
- container (Kubernetes, Docker, containerd, etc. please specify)
- or a combination, please be explicit
- jails if it is FreeBSD
Steps to Reproduce the behavior
(Include debug logs if possible and relevant)
Have an xml file (example log4j file) that contains multiple attributes with the same value. When trying to modify all the values it does not work
log4j example (only showing the logger information
<logger name="com.exmaple" additivity="false">
<level value="DEBUG" />
<appender-ref ref="LOG" />
</logger>
<logger name="org.example2" additivity="false">
<level value="DEBUG" />
<appender-ref ref="LOG" />
</logger>
<logger name="org.example" additivity="false">
<level value="DEBUG" />
<appender-ref ref="LOG" />
</logger>
<logger name="com.example2" additivity="false">
<level value="DEBUG" />
<appender-ref ref="LOG" />
</logger>
<logger name="org.apache.catalina" additivity="false">
<level value="DEBUG" />
<appender-ref ref="TOMCATLOG" />
</logger>
As show about if i wanted to change all the logger levels to INFO i am having problems when doing in salt because it only returns the first log level (for com.exmaple) not all of them
salt -v 'example minion' xml.get_attribute /etc/tomcat/log4j.xml "logger/level"
Executing job with jid 20211102210613240526
-------------------------------------------
example.minion:
----------
value:
INFO
when i change the xpath to include the @ salt throws an error
The minion function caused an exception: Traceback (most recent call last):
File "/usr/lib64/python3.6/xml/etree/ElementPath.py", line 263, in iterfind
selector = _cache[cache_key]
KeyError: ('.//logger/level/@value', None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/salt/minion.py", line 1905, in _thread_return
function_name, function_args, executors, opts, data
File "/usr/lib/python3.6/site-packages/salt/minion.py", line 1861, in _execute_job_function
return_data = self.executors[fname](opts, data, func, args, kwargs)
File "/usr/lib/python3.6/site-packages/salt/loader.py", line 1241, in __call__
return self.loader.run(run_func, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/salt/loader.py", line 2274, in run
return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/contextvars/__init__.py", line 38, in run
return callable(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/salt/loader.py", line 2289, in _run_as
return _func_or_method(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/salt/executors/direct_call.py", line 12, in execute
return func(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/salt/loader.py", line 1241, in __call__
return self.loader.run(run_func, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/salt/loader.py", line 2274, in run
return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/contextvars/__init__.py", line 38, in run
return callable(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/salt/loader.py", line 2289, in _run_as
return _func_or_method(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/salt/modules/xml.py", line 78, in get_attribute
element = root.find(element)
File "/usr/lib64/python3.6/xml/etree/ElementTree.py", line 652, in find
return self._root.find(path, namespaces)
File "/usr/lib64/python3.6/xml/etree/ElementPath.py", line 298, in find
return next(iterfind(elem, path, namespaces), None)
File "/usr/lib64/python3.6/xml/etree/ElementPath.py", line 277, in iterfind
selector.append(ops[token[0]](next, token))
KeyError: '@'
As per https://www.w3schools.com/xml/xml_xpath.asp you can use the xpath with an @ to grab all. Additionally, when i use an xml linter like xmlstarlet i can actually iterate through all the values
xmlstarlet view of all attributes
log4j:configuration/logger
log4j:configuration/logger/@name
log4j:configuration/logger/@additivity
log4j:configuration/logger/level
log4j:configuration/logger/level/@value
log4j:configuration/logger/appender-ref
log4j:configuration/logger/appender-ref/@ref
log4j:configuration/logger
log4j:configuration/logger/@name
log4j:configuration/logger/@additivity
log4j:configuration/logger/level
log4j:configuration/logger/level/@value
log4j:configuration/logger/appender-ref
log4j:configuration/logger/appender-ref/@ref
log4j:configuration/logger
log4j:configuration/logger/@name
log4j:configuration/logger/@additivity
log4j:configuration/logger/level
log4j:configuration/logger/level/@value
log4j:configuration/logger/appender-ref
log4j:configuration/logger/appender-ref/@ref
log4j:configuration/logger
log4j:configuration/logger/@name
log4j:configuration/logger/@additivity
log4j:configuration/logger/level
log4j:configuration/logger/level/@value
log4j:configuration/logger/appender-ref
log4j:configuration/logger/appender-ref/@ref
Expected behavior
The expected behavior would be to modify multiple attributes at once not just the first iteration it finds. Its looks like python findall might be the solution instead of iterfind. right now i am not able to take advantage of the salt module and stuck using cmd.run and running xmlstarlet locally to modify xml's
Screenshots
If applicable, add screenshots to help explain your problem.
Versions Report
salt --versions-report
(Provided by running salt --versions-report. Please also mention any differences in master/minion versions.)Salt Version:
Salt: 3003.1
Dependency Versions:
cffi: Not Installed
cherrypy: Not Installed
dateutil: Not Installed
docker-py: Not Installed
gitdb: 0.6.4
gitpython: 1.0.1
Jinja2: 2.11.1
libgit2: Not Installed
M2Crypto: 0.35.2
Mako: Not Installed
msgpack: 0.6.2
msgpack-pure: Not Installed
mysql-python: Not Installed
pycparser: Not Installed
pycrypto: Not Installed
pycryptodome: Not Installed
pygit2: Not Installed
Python: 3.6.8 (default, Nov 16 2020, 16:55:22)
python-gnupg: Not Installed
PyYAML: 3.13
PyZMQ: 17.0.0
smmap: 0.9.0
timelib: Not Installed
Tornado: 4.5.3
ZMQ: 4.1.4
System Versions:
dist: centos 7 Core
locale: UTF-8
machine: x86_64
release: 3.10.0-1160.45.1.el7.x86_64
system: Linux
version: CentOS Linux 7 Core
Additional context
Add any other context about the problem here.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.