Skip to content

Commit

Permalink
ansible_mitogen: Fix errant ModuleNotFoundError blacklist exceptions
Browse files Browse the repository at this point in the history
Current module whitelist/blacklist behaviour is to reject any module not on
the whitelist if the whitelist is populated. Adding `ansible` and
`ansible_mitogen` to the whitelist effectively blocklisted every other Python
module/package, negating much of the benefit of Mitogen.

Fixes #1011
  • Loading branch information
moreati committed Jul 28, 2023
1 parent ec212a1 commit fb4ae24
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 30 deletions.
8 changes: 2 additions & 6 deletions .ci/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ jobs:

# NOTE: this hangs when ran in Ubuntu 18.04
Van_27_210:
tox.env: py27-mode_localhost-ansible2.10
STRATEGY: linear
ANSIBLE_SKIP_TAGS: resource_intensive
tox.env: py27-mode_localhost-ansible2.10-strategy_linear
Van_27_4:
tox.env: py27-mode_localhost-ansible4
STRATEGY: linear
ANSIBLE_SKIP_TAGS: resource_intensive
tox.env: py27-mode_localhost-ansible4-strategy_linear

- job: Linux
pool:
Expand Down
24 changes: 0 additions & 24 deletions ansible_mitogen/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,29 +180,6 @@ def setup_pool(pool):
LOG.debug('Service pool configured: size=%d', pool.size)


def _setup_responder(responder):
"""
Configure :class:`mitogen.master.ModuleResponder` to only permit
certain packages, and to generate custom responses for certain modules.
"""
responder.whitelist_prefix('ansible')
responder.whitelist_prefix('ansible_mitogen')

# Ansible 2.3 is compatible with Python 2.4 targets, however
# ansible/__init__.py is not. Instead, executor/module_common.py writes
# out a 2.4-compatible namespace package for unknown reasons. So we
# copy it here.
responder.add_source_override(
fullname='ansible',
path=ansible.__file__,
source=(ANSIBLE_PKG_OVERRIDE % (
ansible.__version__,
ansible.__author__,
)).encode(),
is_pkg=True,
)


def increase_open_file_limit():
"""
#549: in order to reduce the possibility of hitting an open files limit,
Expand Down Expand Up @@ -669,7 +646,6 @@ def _setup_master(self):
broker=self.broker,
max_message_size=MAX_MESSAGE_SIZE,
)
_setup_responder(self.router.responder)
mitogen.core.listen(self.broker, 'shutdown', self._on_broker_shutdown)
mitogen.core.listen(self.broker, 'exit', self._on_broker_exit)
self.listener = mitogen.unix.Listener.build_stream(
Expand Down
2 changes: 2 additions & 0 deletions tests/ansible/integration/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
tags: playbook_semantics
- import_playbook: process/all.yml
tags: process
- import_playbook: responder/all.yml
tags: responder
- import_playbook: runner/all.yml
tags: runner
- import_playbook: ssh/all.yml
Expand Down
1 change: 1 addition & 0 deletions tests/ansible/integration/responder/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- import_playbook: imports.yml
23 changes: 23 additions & 0 deletions tests/ansible/integration/responder/imports.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
- name: integration/responder/imports.yml
hosts: test-targets
tasks:
- meta: end_play
when: not is_mitogen

- name: Import pure Python module via Ansible module
mitogen_plain_old_add:
x: 2
y: 2
register: mitogen_plain_old_add_result

- name: Import binary module via Responder
custom_python_run_script:
script: |
import lxml
register: binary_module_result

- name: Import missing module via Responder
custom_python_run_script:
script: |
import does_not_exist
register: missing_module_result
36 changes: 36 additions & 0 deletions tests/ansible/lib/modules/mitogen_plain_old_add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python

DOCUMENTATION = '''
module: mitogen_plain_old_add
options:
x: {type: int, required: true}
y: {type: int, required: true}
author:
- Alex Willmer (@moreati)
'''

RETURN = '''
total: {type: int, returned: always, sample: 42}
'''

from ansible.module_utils.basic import AnsibleModule

import plain_old_module

def main():
module = AnsibleModule(
argument_spec={
'x': {'type': int, 'required': True},
'x': {'type': int, 'required': True},
},
supports_check_mode=True,
)
result = {
'changed': False,
'total': plain_old_module.add(module.params['x'], module.params['y']),
}
module.exit_json(**result)


if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions tests/ansible/run_ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
os.environ['PATH'],
)

os.environ['PYTHONPATH'] = os.path.join(GIT_BASEDIR, 'tests', 'data')

extra = {
'is_mitogen': os.environ.get('ANSIBLE_STRATEGY', '').startswith('mitogen'),
'git_basedir': GIT_BASEDIR,
Expand Down

0 comments on commit fb4ae24

Please sign in to comment.