From 9cb274114c844f9b1c13028f812926c987a7b4a7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 25 Jun 2019 13:37:27 +0200 Subject: [PATCH] bpo-37400: pythoninfo logs getpwuid and getgrouplist (GH-14373) test.pythoninfo now also logs pwd.getpwuid(os.getuid()) and os.getgrouplist(). Extract also os.getrandom() test to run it first. --- Lib/test/pythoninfo.py | 95 +++++++++++++++++++++++++++++------------- 1 file changed, 65 insertions(+), 30 deletions(-) diff --git a/Lib/test/pythoninfo.py b/Lib/test/pythoninfo.py index e9edf675b91..6eb9e875bd6 100644 --- a/Lib/test/pythoninfo.py +++ b/Lib/test/pythoninfo.py @@ -161,6 +161,25 @@ def collect_builtins(info_add): info_add('builtins.float.double_format', float.__getformat__("double")) +def collect_urandom(info_add): + import os + + if hasattr(os, 'getrandom'): + # PEP 524: Check if system urandom is initialized + try: + try: + os.getrandom(1, os.GRND_NONBLOCK) + state = 'ready (initialized)' + except BlockingIOError as exc: + state = 'not seeded yet (%s)' % exc + info_add('os.getrandom', state) + except OSError as exc: + # Python was compiled on a more recent Linux version + # than the current Linux kernel: ignore OSError(ENOSYS) + if exc.errno != errno.ENOSYS: + raise + + def collect_os(info_add): import os @@ -180,16 +199,16 @@ def collect_os(info_add): ) copy_attributes(info_add, os, 'os.%s', attributes, formatter=format_attr) - call_func(info_add, 'os.cwd', os, 'getcwd') + call_func(info_add, 'os.getcwd', os, 'getcwd') - call_func(info_add, 'os.uid', os, 'getuid') - call_func(info_add, 'os.gid', os, 'getgid') + call_func(info_add, 'os.getuid', os, 'getuid') + call_func(info_add, 'os.getgid', os, 'getgid') call_func(info_add, 'os.uname', os, 'uname') def format_groups(groups): return ', '.join(map(str, groups)) - call_func(info_add, 'os.groups', os, 'getgroups', formatter=format_groups) + call_func(info_add, 'os.getgroups', os, 'getgroups', formatter=format_groups) if hasattr(os, 'getlogin'): try: @@ -202,7 +221,7 @@ def collect_os(info_add): info_add("os.login", login) call_func(info_add, 'os.cpu_count', os, 'cpu_count') - call_func(info_add, 'os.loadavg', os, 'getloadavg') + call_func(info_add, 'os.getloadavg', os, 'getloadavg') # Environment variables used by the stdlib and tests. Don't log the full # environment: filter to list to not leak sensitive information. @@ -286,20 +305,32 @@ def collect_os(info_add): os.umask(mask) info_add("os.umask", '%03o' % mask) - if hasattr(os, 'getrandom'): - # PEP 524: Check if system urandom is initialized - try: - try: - os.getrandom(1, os.GRND_NONBLOCK) - state = 'ready (initialized)' - except BlockingIOError as exc: - state = 'not seeded yet (%s)' % exc - info_add('os.getrandom', state) - except OSError as exc: - # Python was compiled on a more recent Linux version - # than the current Linux kernel: ignore OSError(ENOSYS) - if exc.errno != errno.ENOSYS: - raise + +def collect_pwd(info_add): + try: + import pwd + except ImportError: + return + import os + + uid = os.getuid() + try: + entry = pwd.getpwuid(uid) + except KeyError: + entry = None + + info_add('pwd.getpwuid(%s)'% uid, + entry if entry is not None else '') + + if entry is None: + # there is nothing interesting to read if the current user identifier + # is not the password database + return + + if hasattr(os, 'getgrouplist'): + groups = os.getgrouplist(entry.pw_name, entry.pw_gid) + groups = ', '.join(map(str, groups)) + info_add('os.getgrouplist', groups) def collect_readline(info_add): @@ -625,31 +656,35 @@ def collect_info(info): info_add = info.add for collect_func in ( - # collect_os() should be the first, to check the getrandom() status - collect_os, + # collect_urandom() must be the first, to check the getrandom() status. + # Other functions may block on os.urandom() indirectly and so change + # its state. + collect_urandom, collect_builtins, + collect_cc, + collect_datetime, + collect_decimal, + collect_expat, collect_gdb, + collect_gdbm, + collect_get_config, collect_locale, + collect_os, collect_platform, + collect_pwd, collect_readline, + collect_resource, collect_socket, collect_sqlite, collect_ssl, + collect_subprocess, collect_sys, collect_sysconfig, + collect_testcapi, collect_time, - collect_datetime, collect_tkinter, collect_zlib, - collect_expat, - collect_decimal, - collect_testcapi, - collect_resource, - collect_cc, - collect_gdbm, - collect_get_config, - collect_subprocess, # Collecting from tests should be last as they have side effects. collect_test_socket,