mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-23 04:24:00 -07:00
gem: fix soundness issue when uninstalling default gems on Ubuntu (#10689)
Some checks failed
EOL CI / EOL Sanity (Ⓐ2.16) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.16+py2.7) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.16+py3.11) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.16+py3.6) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/3/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/3/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/3/) (push) Has been cancelled
nox / Run extra sanity tests (push) Has been cancelled
Some checks failed
EOL CI / EOL Sanity (Ⓐ2.16) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.16+py2.7) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.16+py3.11) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.16+py3.6) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/3/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/3/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/3/) (push) Has been cancelled
nox / Run extra sanity tests (push) Has been cancelled
* Attempt to fix gem soundness issue * Return command execution * Fix value error * Attempt to fix failling tests * Fix minor issues * Update changelog * Update tests/integration/targets/gem/tasks/main.yml Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * Update changelogs/fragments/10689-gem-prevent-soundness-issue.yml Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * Remove state and name from gem error message * Improve gem uninstall check * Make unit tests pass * Fix linting issues * gem: Remove length chenck and adapt unit tests * Adapt gem unit tests * gem: improve error msg * Fix sanity error * Fix linting issue --------- Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
750adb431a
commit
cc41d9da60
4 changed files with 38 additions and 8 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- "gem - fix soundness issue when uninstalling default gems on Ubuntu (https://github.com/ansible-collections/community.general/issues/10451, https://github.com/ansible-collections/community.general/pull/10689)."
|
|
@ -243,7 +243,7 @@ def uninstall(module):
|
||||||
if module.params['force']:
|
if module.params['force']:
|
||||||
cmd.append('--force')
|
cmd.append('--force')
|
||||||
cmd.append(module.params['name'])
|
cmd.append(module.params['name'])
|
||||||
module.run_command(cmd, environ_update=environ, check_rc=True)
|
return module.run_command(cmd, environ_update=environ, check_rc=True)
|
||||||
|
|
||||||
|
|
||||||
def install(module):
|
def install(module):
|
||||||
|
@ -334,9 +334,21 @@ def main():
|
||||||
changed = True
|
changed = True
|
||||||
elif module.params['state'] == 'absent':
|
elif module.params['state'] == 'absent':
|
||||||
if exists(module):
|
if exists(module):
|
||||||
uninstall(module)
|
command_output = uninstall(module)
|
||||||
changed = True
|
if command_output is not None and exists(module):
|
||||||
|
rc, out, err = command_output
|
||||||
|
module.fail_json(
|
||||||
|
msg=(
|
||||||
|
"Failed to uninstall gem '%s': it is still present after 'gem uninstall'. "
|
||||||
|
"This usually happens with default or system gems provided by the OS, "
|
||||||
|
"which cannot be removed with the gem command."
|
||||||
|
) % module.params['name'],
|
||||||
|
rc=rc,
|
||||||
|
stdout=out,
|
||||||
|
stderr=err
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
changed = True
|
||||||
result = {}
|
result = {}
|
||||||
result['name'] = module.params['name']
|
result['name'] = module.params['name']
|
||||||
result['state'] = module.params['state']
|
result['state'] = module.params['state']
|
||||||
|
|
|
@ -212,3 +212,18 @@
|
||||||
that:
|
that:
|
||||||
- install_gem_result is changed
|
- install_gem_result is changed
|
||||||
- not gem_bindir_stat.stat.exists
|
- not gem_bindir_stat.stat.exists
|
||||||
|
|
||||||
|
- name: Attempt to uninstall default gem 'json'
|
||||||
|
community.general.gem:
|
||||||
|
name: json
|
||||||
|
state: absent
|
||||||
|
when: ansible_distribution == "Ubuntu"
|
||||||
|
register: gem_result
|
||||||
|
ignore_errors: true
|
||||||
|
|
||||||
|
- name: Assert gem uninstall failed as expected
|
||||||
|
when: ansible_distribution == "Ubuntu"
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- gem_result is failed
|
||||||
|
- gem_result.msg.startswith("Failed to uninstall gem 'json'")
|
||||||
|
|
|
@ -52,7 +52,9 @@ class TestGem(ModuleTestCase):
|
||||||
|
|
||||||
def patch_run_command(self):
|
def patch_run_command(self):
|
||||||
target = 'ansible.module_utils.basic.AnsibleModule.run_command'
|
target = 'ansible.module_utils.basic.AnsibleModule.run_command'
|
||||||
return self.mocker.patch(target)
|
mock = self.mocker.patch(target)
|
||||||
|
mock.return_value = (0, '', '')
|
||||||
|
return mock
|
||||||
|
|
||||||
def test_fails_when_user_install_and_install_dir_are_combined(self):
|
def test_fails_when_user_install_and_install_dir_are_combined(self):
|
||||||
with set_module_args({
|
with set_module_args({
|
||||||
|
@ -107,12 +109,11 @@ class TestGem(ModuleTestCase):
|
||||||
|
|
||||||
run_command = self.patch_run_command()
|
run_command = self.patch_run_command()
|
||||||
|
|
||||||
with pytest.raises(AnsibleExitJson) as exc:
|
with pytest.raises(AnsibleFailJson) as exc:
|
||||||
gem.main()
|
gem.main()
|
||||||
|
|
||||||
result = exc.value.args[0]
|
result = exc.value.args[0]
|
||||||
|
assert result['failed']
|
||||||
assert result['changed']
|
|
||||||
assert run_command.called
|
assert run_command.called
|
||||||
|
|
||||||
assert '--install-dir /opt/dummy' in get_command(run_command)
|
assert '--install-dir /opt/dummy' in get_command(run_command)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue