summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRodolfo Alonso Hernandez <ralonsoh@redhat.com>2020-02-21 15:28:23 +0000
committerRodolfo Alonso Hernandez <ralonsoh@redhat.com>2020-02-21 15:28:23 +0000
commit23a534b9f5f5aad2a95349f809e05cb9b6429f3c (patch)
tree48705164c8ca19de73fed2ac90cef7e342161dbb
parent07e464b78e6c58f6676fd6fc66fb7c210e9056cf (diff)
downloadoslo-rootwrap-23a534b9f5f5aad2a95349f809e05cb9b6429f3c.tar.gz
Implement "realpath" to retrieve the real absolute path6.0.1
This function will call "os.path.realpth" and this in turn could call "os.getcwd". If the current path has been deleted by other application, this method will raise FileNotFoundError exception. This new function catches and unsets this exception, returning an empty string. Change-Id: If70411d8d189de4a020e528cd54412a5678cfab9 Closes-Bug: #1864090
-rw-r--r--oslo_rootwrap/filters.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/oslo_rootwrap/filters.py b/oslo_rootwrap/filters.py
index ba017af..bcb03ac 100644
--- a/oslo_rootwrap/filters.py
+++ b/oslo_rootwrap/filters.py
@@ -33,6 +33,19 @@ def _getuid(user):
return pwd.getpwnam(user).pw_uid
+def realpath(path):
+ """Return the real absolute path.
+
+ If the execution directory does not exist, os.getcwd() raises a
+ FileNotFoundError exception. In this case, unset the exception and return
+ an empty string.
+ """
+ try:
+ return os.path.realpath(path)
+ except FileNotFoundError:
+ return ''
+
+
class CommandFilter(object):
"""Command filter only checking that the 1st argument matches exec_path."""
@@ -128,7 +141,7 @@ class PathFilter(CommandFilter):
if not os.path.isabs(arg) # arguments not specifying abs paths
)
paths_are_within_base_dirs = all(
- os.path.commonprefix([arg, os.path.realpath(value)]) == arg
+ os.path.commonprefix([arg, realpath(value)]) == arg
for arg, value in zip(self.args, arguments)
if os.path.isabs(arg) # arguments specifying abs paths
)
@@ -143,7 +156,7 @@ class PathFilter(CommandFilter):
command, arguments = userargs[0], userargs[1:]
# convert path values to canonical ones; copy other args as is
- args = [os.path.realpath(value) if os.path.isabs(arg) else value
+ args = [realpath(value) if os.path.isabs(arg) else value
for arg, value in zip(self.args, arguments)]
return super(PathFilter, self).get_command([command] + args,