Skip to content

Commit afa69cd

Browse files
authored
[Bugfix] [zos_fetch] Fix permission issue when using become and become_user (#2079)
* Use become_user in _transfer_remote_content * Fix become validation * Fix cleanup when escalating privileges * Add changelog fragment * Update 2079-become-use-zos_fetch.yml
1 parent 3af291a commit afa69cd

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bugfixes:
2+
- zos_fetch - Previously, the use of `become` would result in a permissions error
3+
while trying to fetch a data set or a member. Fix now allows a user to escalate
4+
privileges when fetching resources.
5+
(https://github.com/ansible-collections/ibm_zos_core/pull/2079)

plugins/action/zos_fetch.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ def _transfer_remote_content(
363363
sftp_transfer_method = "sftp"
364364
user_ssh_transfer_method = None
365365
is_ssh_transfer_method_updated = False
366+
was_user_updated = False
366367

367368
try:
368369
if version_major == 2 and version_minor >= 11:
@@ -383,7 +384,16 @@ def _transfer_remote_content(
383384
display.vvv(u"ibm_zos_fetch SSH transfer method updated from {0} to {1}.".format(user_ssh_transfer_method,
384385
sftp_transfer_method), host=self._play_context.remote_addr)
385386

387+
if self._connection.become:
388+
was_user_updated = True
389+
self._connection.set_option('remote_user', self._play_context._become_user)
390+
display.vvv(
391+
u"ibm_zos_fetch SSH transfer user updated to {0}".format(self._play_context._become_user),
392+
host=self._play_context.remote_addr
393+
)
394+
386395
display.vvv(u"{0} {1} TO {2}".format(_sftp_action, remote_path, dest), host=self._play_context.remote_addr)
396+
display.vvv(u"{0}, {1}".format(vars(self._connection), vars(self._play_context)))
387397
(returncode, stdout, stderr) = self._connection._file_transport_command(remote_path, dest, _sftp_action)
388398

389399
display.vvv(u"ibm_zos_fetch return code: {0}".format(returncode), host=self._play_context.remote_addr)
@@ -424,6 +434,13 @@ def _transfer_remote_content(
424434
result["failed"] = True
425435

426436
finally:
437+
if was_user_updated:
438+
self._connection.set_option('remote_user', self._play_context._remote_user)
439+
display.vvv(
440+
u"ibm_zos_fetch SSH transfer user restored to {0}".format(self._play_context._remote_user),
441+
host=self._play_context.remote_addr
442+
)
443+
427444
# Restore the users defined option `ssh_transfer_method` if it was overridden
428445

429446
if is_ssh_transfer_method_updated:
@@ -446,4 +463,21 @@ def _remote_cleanup(self, remote_path, src_type, encoding):
446463
rm_cmd = "rm -r {0}".format(remote_path)
447464
if src_type != "PO" and src_type != "GDG":
448465
rm_cmd = rm_cmd.replace(" -r", "")
466+
467+
# If another user created the temporary files, we'll need to run rm
468+
# with it too, lest we get a permissions issue.
469+
if self._connection.become:
470+
self._connection.set_option('remote_user', self._play_context._become_user)
471+
display.vvv(
472+
u"ibm_zos_fetch SSH cleanup user updated to {0}".format(self._play_context._become_user),
473+
host=self._play_context.remote_addr
474+
)
475+
449476
self._connection.exec_command(rm_cmd)
477+
478+
if self._connection.become:
479+
self._connection.set_option('remote_user', self._play_context._remote_user)
480+
display.vvv(
481+
u"ibm_zos_fetch SSH cleanup user restored to {0}".format(self._play_context._remote_user),
482+
host=self._play_context.remote_addr
483+
)

0 commit comments

Comments
 (0)