Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Unified Diff: tools/telemetry/telemetry/core/platform/posix_platform_backend.py

Issue 750093007: [Telemetry] Fix sudoers file parsing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CL to land Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/telemetry/telemetry/core/platform/posix_platform_backend_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/telemetry/telemetry/core/platform/posix_platform_backend.py
diff --git a/tools/telemetry/telemetry/core/platform/posix_platform_backend.py b/tools/telemetry/telemetry/core/platform/posix_platform_backend.py
index 512923f6f857382d34ee5d00495b28bc363d4908..af84ec183b3f3e68af4f2c765e8d51555c1e89b0 100644
--- a/tools/telemetry/telemetry/core/platform/posix_platform_backend.py
+++ b/tools/telemetry/telemetry/core/platform/posix_platform_backend.py
@@ -13,6 +13,24 @@ from telemetry.core.platform import desktop_platform_backend
from telemetry.core.platform import ps_util
+def _BinaryExistsInSudoersFiles(path, sudoers_file_contents):
+ """Returns True if the binary in |path| features in the sudoers file.
+ """
+ for line in sudoers_file_contents.splitlines():
+ if re.match(r'\s*\(.+\) NOPASSWD: %s(\s\S+)*$' % re.escape(path), line):
+ return True
+ return False
+
+
+def _CanRunElevatedWithSudo(path):
+ """Returns True if the binary at |path| appears in the sudoers file.
+ If this function returns true then the binary at |path| can be run via sudo
+ without prompting for a password.
+ """
+ sudoers = subprocess.check_output(['/usr/bin/sudo', '-l'])
+ return _BinaryExistsInSudoersFiles(path, sudoers)
+
+
class PosixPlatformBackend(desktop_platform_backend.DesktopPlatformBackend):
# This is an abstract class. It is OK to have abstract methods.
@@ -109,18 +127,9 @@ class PosixPlatformBackend(desktop_platform_backend.DesktopPlatformBackend):
"""Returns True if the binary at |path| has the setuid bit set."""
return (os.stat(path).st_mode & stat.S_ISUID) == stat.S_ISUID
- def CanRunElevatedWithSudo(path):
- """Returns True if the binary at |path| appears explicitly in the sudoers
- file and can be run without prompting for a password."""
- sudoers = subprocess.check_output(['/usr/bin/sudo', '-l'])
- for line in sudoers.splitlines():
- if re.match(r'\s*\(.+\) NOPASSWD: %s$' % path, line):
- return True
- return False
-
if elevate_privilege and not IsSetUID(application):
args = ['/usr/bin/sudo'] + args
- if not CanRunElevatedWithSudo(application) and not IsElevated():
+ if not _CanRunElevatedWithSudo(application) and not IsElevated():
print ('Telemetry needs to run %s under sudo. Please authenticate.' %
application)
# Synchronously authenticate.
« no previous file with comments | « no previous file | tools/telemetry/telemetry/core/platform/posix_platform_backend_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698