This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Suggestion: make getpass.getuser() also look at SUDO_USER environment variable
Type: enhancement Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: gregory.p.smith Nosy List: amoss, gregory.p.smith, steven.daprano, xtreak, zach.ware
Priority: normal Keywords: patch

Created on 2018-09-17 04:57 by amoss, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9493 closed amoss, 2018-09-22 12:31
Messages (5)
msg325511 - (view) Author: Amos S (amoss) * Date: 2018-09-17 04:57
When doing "sudo python-script.py", the output of "getpass.getuser()" is pretty much useless for what it's used (I assume mainly logging and tracking purposes, that's what we use it for ourselves).

I worked around this limitation by using it in expression likes:

  username = os.environ.get("SUDO_USER") or getpass.getuser()

I think it'll be useful to many other users if getpass.getuser() could integrate this behavior.

I'd love to provide the code change if this is approved.
msg326558 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-09-27 12:40
Versions 3.7 and below are all in feature-freeze, so this change could only apply to 3.8 and above.

I don't know if this feature is desirable or not.

If it is (sometimes?) desirable, my guess is that it would be undesirable to use SUDO_USER *unless* the effective user ID was 0. (Don't check for the name "root", that's only a convention.) In pseudocode:

    names = ('LOGNAME', 'USER', 'LNAME', 'USERNAME')
    if effective user ID == 0:
        names = ('SUDO_USER',) + names
    for name in names:
        ...


Also needs documentation and tests.
msg326595 - (view) Author: Amos S (amoss) * Date: 2018-09-27 21:08
The use of environment variables like USER and LOGNAME instead of getuid() etc is done in order to try to get "who really initiated this process?" rather than "who this process belongs to?". This is hidden today when SUDO_USER is ignored.

This also fits my interpretation of the function's documentation: 'Return the "login name" of the user.'

For instance, in a system I use I have to sudo to a system user other than root in order to execute certain scripts and this patch would reveal the original user who executed the sudo command.

I updated the test case already in the existing PR.

I update to the documentation to list SUDO_USER.
msg351980 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2019-09-11 16:51
I agree with Steven in that I'm not quite sure this is a good change, but I also see that it would be useful in some cases.  Perhaps either a `check_sudo_user=False` keyword-only parameter, or a `vars_to_check=(<existing options>)` parameter would be better?

Adding Gregory P. Smith as the last person to have added their name to getpass.py :)
msg352201 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2019-09-12 15:33
Adding a check for SUDO_USER would break existing uses as it would no longer be reporting the user the process is running as.
History
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 78890
2019-09-12 15:33:49gregory.p.smithsetstatus: open -> closed
title: Suggestion: make getuser.getpass() also look at SUDO_USER environment variable -> Suggestion: make getpass.getuser() also look at SUDO_USER environment variable
messages: + msg352201

assignee: gregory.p.smith
resolution: rejected
stage: patch review -> resolved
2019-09-11 16:51:58zach.waresetnosy: + gregory.p.smith, zach.ware
messages: + msg351980
2018-09-27 21:08:35amosssetmessages: + msg326595
2018-09-27 12:40:20steven.dapranosetnosy: + steven.daprano

messages: + msg326558
versions: - Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7
2018-09-22 15:38:54xtreaksetnosy: + xtreak
2018-09-22 12:31:40amosssetkeywords: + patch
stage: patch review
pull_requests: + pull_request8903
2018-09-17 04:57:38amosscreate