Compare commits

...

8 commits

Author SHA1 Message Date
Axionize
e903564dc7
Merge 6e7c537956 into b39ee97ccc 2025-12-08 14:12:50 +00:00
softwarefactory-project-zuul[bot]
b39ee97ccc
Merge pull request #677 from shenxianpeng/patch-1
docs: fix broken badge and restore coverage badge

SUMMARY
Replaced the outdated Shippable badge and active Codecov coverage badge, like other repos in ansible-collections org
ISSUE TYPE


Docs Pull Request

COMPONENT NAME

ADDITIONAL INFORMATION

Reviewed-by: Hideki Saito <saito@fgrep.org>
2025-11-28 07:14:56 +00:00
softwarefactory-project-zuul[bot]
72a6eb9729
Merge pull request #639 from Klaas-/Klaas-fix_authorized_key
Fixes #462 notice permission denied on authorized_key module

SUMMARY
As of right now the authorized_key module does not notice on an "absent" if a authorized_keys file is simply not readable to the executing user. I am trying to fix that
ISSUE TYPE


Bugfix Pull Request

COMPONENT NAME
authorized_key
ADDITIONAL INFORMATION


Execute as a user that does not have access to the root users authorized keys file

- name: Delete key from root user
  ansible.posix.authorized_key:
    state: absent
    user: root
    key: ssh-rsa xxxxxxxx

- name: Delete key from root user
  become: true
  ansible.posix.authorized_key:
    state: absent
    user: root
    key: ssh-rsa xxxxxxxx

The one without become will succeed before my change and will fail with a permission denied error after my change. The 2nd task will actually remove a key from root user if become privileges are available for the executing user

Reviewed-by: Brian Coca
Reviewed-by: Klaas Demter
Reviewed-by: Felix Fontein <felix@fontein.de>
Reviewed-by: Hideki Saito <saito@fgrep.org>
2025-11-28 03:25:21 +00:00
Klaas Demter
9651a19805
change result.failed==True to result is failed in check_permissions.yml
Co-authored-by: Felix Fontein <felix@fontein.de>
2025-10-22 08:29:46 +02:00
Klaas Demter
413ab782a8 Fixes #462 notice permission denied on authorized_key module 2025-10-21 10:00:12 +02:00
Xianpeng Shen
cda2e0657f
docs: fix broken badge and restore coverage badge 2025-08-14 14:33:30 +03:00
Axionize
6e7c537956 Add example to documentation 2023-12-25 00:58:33 -05:00
Axionize
d0ea1143ee Make synchronize work with multiple src paths 2023-12-25 00:54:16 -05:00
7 changed files with 83 additions and 20 deletions

View file

@ -2,7 +2,7 @@
<!-- Add CI and code coverage badges here. Samples included below. --> <!-- Add CI and code coverage badges here. Samples included below. -->
[![Build Status]( [![Build Status](
https://dev.azure.com/ansible/ansible.posix/_apis/build/status/CI?branchName=main)](https://dev.azure.com/ansible/ansible.posix/_build?definitionId=26) https://dev.azure.com/ansible/ansible.posix/_apis/build/status/CI?branchName=main)](https://dev.azure.com/ansible/ansible.posix/_build?definitionId=26)
[![Run Status](https://api.shippable.com/projects/5e669aaf8b17a60007e4d18d/badge?branch=main)]() <!--[![Codecov](https://img.shields.io/codecov/c/github/ansible-collections/ansible.posix)](https://codecov.io/gh/ansible-collections/ansible.posix)--> [![Codecov](https://img.shields.io/codecov/c/github/ansible-collections/ansible.posix)](https://codecov.io/gh/ansible-collections/ansible.posix)
## Communication ## Communication

View file

@ -0,0 +1,3 @@
---
bugfixes:
- ansible.posix.authorized_key - fixes error on permission denied in authorized_key module (https://github.com/ansible-collections/ansible.posix/issues/462).

View file

@ -339,6 +339,8 @@ class ActionModule(ActionBase):
dest = _tmp_args.get('dest', None) dest = _tmp_args.get('dest', None)
if src is None or dest is None: if src is None or dest is None:
return dict(failed=True, msg="synchronize requires both src and dest parameters are set") return dict(failed=True, msg="synchronize requires both src and dest parameters are set")
if isinstance(src, str):
src = [src]
# Determine if we need a user@ and a password # Determine if we need a user@ and a password
user = None user = None
@ -365,11 +367,11 @@ class ActionModule(ActionBase):
# use the mode to define src and dest's url # use the mode to define src and dest's url
if _tmp_args.get('mode', 'push') == 'pull': if _tmp_args.get('mode', 'push') == 'pull':
# src is a remote path: <user>@<host>, dest is a local path # src is a remote path: <user>@<host>, dest is a local path
src = self._process_remote(_tmp_args, src_host, src, user, inv_port in localhost_ports) src = [self._process_remote(_tmp_args, src_host, e, user, inv_port in localhost_ports) for e in src]
dest = self._process_origin(dest_host, dest, user) dest = self._process_origin(dest_host, dest, user)
else: else:
# src is a local path, dest is a remote path: <user>@<host> # src is a local path, dest is a remote path: <user>@<host>
src = self._process_origin(src_host, src, user) src = [self._process_origin(src_host, e, user) for e in src]
dest = self._process_remote(_tmp_args, dest_host, dest, user, inv_port in localhost_ports) dest = self._process_remote(_tmp_args, dest_host, dest, user, inv_port in localhost_ports)
password = dest_host_inventory_vars.get('ansible_ssh_pass', None) or dest_host_inventory_vars.get('ansible_password', None) password = dest_host_inventory_vars.get('ansible_ssh_pass', None) or dest_host_inventory_vars.get('ansible_password', None)
@ -378,7 +380,7 @@ class ActionModule(ActionBase):
else: else:
# Still need to munge paths (to account for roles) even if we aren't # Still need to munge paths (to account for roles) even if we aren't
# copying files between hosts # copying files between hosts
src = self._get_absolute_path(path=src) src = [self._get_absolute_path(path=e) for e in src]
dest = self._get_absolute_path(path=dest) dest = self._get_absolute_path(path=dest)
_tmp_args['_local_rsync_password'] = password _tmp_args['_local_rsync_password'] = password

View file

@ -225,6 +225,8 @@ import os.path
import tempfile import tempfile
import re import re
import shlex import shlex
import errno
import traceback
from operator import itemgetter from operator import itemgetter
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
@ -475,16 +477,18 @@ def parsekey(module, raw_key, rank=None):
return (key, key_type, options, comment, rank) return (key, key_type, options, comment, rank)
def readfile(filename): def readfile(module, filename):
if not os.path.isfile(filename):
return ''
f = open(filename)
try: try:
with open(filename, 'r') as f:
return f.read() return f.read()
finally: except IOError as e:
f.close() if e.errno == errno.EACCES:
module.fail_json(msg="Permission denied on file or path for authorized keys file: %s" % filename,
exception=traceback.format_exc())
elif e.errno == errno.ENOENT:
return ''
else:
raise
def parsekeys(module, lines): def parsekeys(module, lines):
@ -597,7 +601,7 @@ def enforce_state(module, params):
# check current state -- just get the filename, don't create file # check current state -- just get the filename, don't create file
do_write = False do_write = False
params["keyfile"] = keyfile(module, user, do_write, path, manage_dir) params["keyfile"] = keyfile(module, user, do_write, path, manage_dir)
existing_content = readfile(params["keyfile"]) existing_content = readfile(module, params["keyfile"])
existing_keys = parsekeys(module, existing_content) existing_keys = parsekeys(module, existing_content)
# Add a place holder for keys that should exist in the state=present and # Add a place holder for keys that should exist in the state=present and

View file

@ -361,6 +361,17 @@ EXAMPLES = r'''
src: /tmp/localpath/ src: /tmp/localpath/
dest: /tmp/remotepath dest: /tmp/remotepath
rsync_path: /usr/gnu/bin/rsync rsync_path: /usr/gnu/bin/rsync
# Source files from multiple folders and merge them on the remote
# Files of the same name in /tmp/path_c/ will take precedence over those in /tmp/path_b/, and same for path_b to path_a
- name: Copy files from multiple folders and merge them into dest
ansible.posix.synchronize:
src:
- /tmp/path_a/
- /tmp/path_b/
- /tmp/path_c/
dest: /tmp/dest/
recursive: True
''' '''
@ -396,9 +407,9 @@ def substitute_controller(path):
def is_rsh_needed(source, dest): def is_rsh_needed(source, dest):
if source.startswith('rsync://') or dest.startswith('rsync://'): if all(e.startswith('rsync://') for e in source) or dest.startswith('rsync://'):
return False return False
if ':' in source or ':' in dest: if any(':' in e for e in source) or ':' in dest:
return True return True
return False return False
@ -406,7 +417,7 @@ def is_rsh_needed(source, dest):
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
src=dict(type='path', required=True), src=dict(type='list', required=True),
dest=dict(type='path', required=True), dest=dict(type='path', required=True),
dest_port=dict(type='int'), dest_port=dict(type='int'),
delete=dict(type='bool', default=False), delete=dict(type='bool', default=False),
@ -540,11 +551,10 @@ def main():
if dirs: if dirs:
cmd.append('--dirs') cmd.append('--dirs')
if source.startswith('rsync://') and dest.startswith('rsync://'): if all(e.startswith('rsync://') for e in source) and dest.startswith('rsync://'):
module.fail_json(msg='either src or dest must be a localhost', rc=1) module.fail_json(msg='either src or dest must be a localhost', rc=1)
if is_rsh_needed(source, dest): if is_rsh_needed(source, dest):
# https://github.com/ansible/ansible/issues/15907 # https://github.com/ansible/ansible/issues/15907
has_rsh = False has_rsh = False
for rsync_opt in rsync_opts: for rsync_opt in rsync_opts:
@ -600,7 +610,7 @@ def main():
changed_marker = '<<CHANGED>>' changed_marker = '<<CHANGED>>'
cmd.append('--out-format=%s' % shlex_quote(changed_marker + '%i %n%L')) cmd.append('--out-format=%s' % shlex_quote(changed_marker + '%i %n%L'))
cmd.append(shlex_quote(source)) [cmd.append(shlex_quote(e)) for e in source]
cmd.append(shlex_quote(dest)) cmd.append(shlex_quote(dest))
cmdstr = ' '.join(cmd) cmdstr = ' '.join(cmd)

View file

@ -0,0 +1,41 @@
---
# -------------------------------------------------------------
# check permissions
- name: Create a file that is not accessible
ansible.builtin.file:
state: touch
path: "{{ output_dir | expanduser }}/file_permissions"
owner: root
mode: '0000'
- name: Create unprivileged user
ansible.builtin.user:
name: nopriv
create_home: true
- name: Try to delete a key from an unreadable file
become: true
become_user: nopriv
ansible.posix.authorized_key:
user: root
key: "{{ dss_key_basic }}"
state: absent
path: "{{ output_dir | expanduser }}/file_permissions"
register: result
ignore_errors: true
- name: Assert that the key deletion has failed
ansible.builtin.assert:
that:
- result is failed
- name: Remove the file
ansible.builtin.file:
state: absent
path: "{{ output_dir | expanduser }}/file_permissions"
- name: Remove the user
ansible.builtin.user:
name: nopriv
state: absent

View file

@ -34,3 +34,6 @@
- name: Test for specifying key as a path - name: Test for specifying key as a path
ansible.builtin.import_tasks: check_path.yml ansible.builtin.import_tasks: check_path.yml
- name: Test for permission denied files
ansible.builtin.import_tasks: check_permissions.yml