Prevent failures due to unsafe plugin name (#82759) (#82791)

(cherry picked from commit 56f31126ad)
pull/82837/head
Martin Krizek 9 months ago committed by GitHub
parent cb82ddec02
commit 3d263f8330
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
bugfixes:
- "Fix an issue when setting a plugin name from an unsafe source resulted in ``ValueError: unmarshallable object`` (https://github.com/ansible/ansible/issues/82708)"

@ -34,6 +34,7 @@ from ansible.utils.collection_loader import AnsibleCollectionConfig, AnsibleColl
from ansible.utils.collection_loader._collection_finder import _AnsibleCollectionFinder, _get_collection_metadata
from ansible.utils.display import Display
from ansible.utils.plugin_docs import add_fragments, find_plugin_docfile
from ansible.utils.unsafe_proxy import _is_unsafe
# TODO: take the packaging dep, or vendor SpecifierSet?
@ -858,6 +859,17 @@ class PluginLoader:
def get_with_context(self, name, *args, **kwargs):
''' instantiates a plugin of the given name using arguments '''
if _is_unsafe(name):
# Objects constructed using the name wrapped as unsafe remain
# (correctly) unsafe. Using such unsafe objects in places
# where underlying types (builtin string in this case) are
# expected can cause problems.
# One such case is importlib.abc.Loader.exec_module failing
# with "ValueError: unmarshallable object" because the module
# object is created with the __path__ attribute being wrapped
# as unsafe which isn't marshallable.
# Manually removing the unsafe wrapper prevents such issues.
name = name._strip_unsafe()
found_in_cache = True
class_only = kwargs.pop('class_only', False)

@ -0,0 +1,6 @@
from ansible.plugins.action import ActionBase
class ActionModule(ActionBase):
def run(self, tmp=None, task_vars=None):
return {"nca_executed": True}

@ -34,3 +34,5 @@ done
# test config loading
ansible-playbook use_coll_name.yml -i ../../inventory -e 'ansible_connection=ansible.builtin.ssh' "$@"
ANSIBLE_COLLECTIONS_PATH=$PWD/collections ansible-playbook unsafe_plugin_name.yml "$@"

@ -0,0 +1,9 @@
- hosts: localhost
gather_facts: false
tasks:
- action: !unsafe n.c.a
register: r
- assert:
that:
- r.nca_executed
Loading…
Cancel
Save