test-runner: Don't require SUDO_GID to be set for logs

Base the root user check on os.getuid() instead of SUDO_GID so as not to
implicitly require sudo.  SUDO_GID being set doesn't guarantee that the
effective user is root either since you can sudo to non-root accounts.
This commit is contained in:
Andrew Zaborowski 2022-03-30 19:33:59 +02:00 committed by Denis Kenzior
parent 0201cde7ce
commit 83299ef6aa
1 changed files with 26 additions and 21 deletions

View File

@ -276,13 +276,16 @@ class Process(subprocess.Popen):
return True return True
def _append_outfile(self, file, append=True): def _append_outfile(self, file, append=True):
gid = int(self.ctx.args.log_gid)
uid = int(self.ctx.args.log_uid)
dir = os.path.dirname(file) dir = os.path.dirname(file)
if self.ctx.args.log_gid:
gid = int(self.ctx.args.log_gid)
uid = int(self.ctx.args.log_uid)
if not path_exists(dir): if not path_exists(dir):
os.mkdir(dir) os.mkdir(dir)
os.chown(dir, uid, gid) if self.ctx.args.log_gid:
os.chown(dir, uid, gid)
file = os.path.join(dir,file) file = os.path.join(dir,file)
@ -299,7 +302,8 @@ class Process(subprocess.Popen):
traceback.print_exc() traceback.print_exc()
exit(0) exit(0)
os.fchown(f.fileno(), uid, gid) if self.ctx.args.log_gid:
os.fchown(f.fileno(), uid, gid)
self.write_fds.append(f) self.write_fds.append(f)
@ -1833,46 +1837,47 @@ class Main:
if self.args.sub_tests: if self.args.sub_tests:
options += ' --sub_tests %s' % ','.join(self.args.sub_tests) options += ' --sub_tests %s' % ','.join(self.args.sub_tests)
gid = None
if os.environ.get('SUDO_GID', None):
uid = int(os.environ['SUDO_UID'])
gid = int(os.environ['SUDO_GID'])
append_gid_uid = False
if self.args.log: if self.args.log:
if os.environ.get('SUDO_GID', None) is None: if os.getuid() != 0:
print("--log can only be used as root user") print("--log can only be used as root user")
quit() quit()
self.args.log = os.path.abspath(self.args.log) self.args.log = os.path.abspath(self.args.log)
uid = int(os.environ['SUDO_UID']) append_gid_uid = True
gid = int(os.environ['SUDO_GID'])
if not path_exists(self.args.log): if not path_exists(self.args.log):
os.mkdir(self.args.log) os.mkdir(self.args.log)
os.chown(self.args.log, uid, gid) if gid:
os.chown(self.args.log, uid, gid)
options += ' --log-gid %u' % gid
options += ' --log-uid %u' % uid
if self.args.monitor: if self.args.monitor:
if os.environ.get('SUDO_GID', None) is None: if os.getuid() != 0:
print("--monitor can only be used as root user") print("--monitor can only be used as root user")
quit() quit()
self.args.monitor = os.path.abspath(self.args.monitor) self.args.monitor = os.path.abspath(self.args.monitor)
mon_parent_dir = os.path.abspath(os.path.join(self.args.monitor, os.pardir)) mon_parent_dir = os.path.abspath(os.path.join(self.args.monitor, os.pardir))
append_gid_uid = True
options += ' --log-gid %u' % int(os.environ['SUDO_GID'])
options += ' --log-uid %u' % int(os.environ['SUDO_UID'])
if self.args.result: if self.args.result:
if os.environ.get('SUDO_GID', None) is None: if os.getuid() != 0:
print("--result can only be used as root user") print("--result can only be used as root user")
quit() quit()
self.args.result = os.path.abspath(self.args.result) self.args.result = os.path.abspath(self.args.result)
result_parent_dir = os.path.abspath(os.path.join(self.args.result, os.pardir)) result_parent_dir = os.path.abspath(os.path.join(self.args.result, os.pardir))
append_gid_uid = True
if '--log-gid' not in options: if append_gid_uid and gid:
options += ' --log-gid %u' % int(os.environ['SUDO_GID']) options += ' --log-gid %u' % (gid,)
options += ' --log-uid %u' % (uid,)
if '--log-uid' not in options:
options += ' --log-uid %u' % int(os.environ['SUDO_UID'])
denylist = [ denylist = [
'auto_tests', 'auto_tests',