Vonng / pigsty

Battery-Included PostgreSQL Distro as a Free RDS Alternative
https://pigsty.io
GNU Affero General Public License v3.0
2.97k stars 241 forks source link

Use a more robust way to get current user in pg-role #416

Closed Vonng closed 1 month ago

Vonng commented 1 month ago

We are using this snippet in /pg/bin/pg-role to get the current user:

def get_user():
    return os.environ["USER"]

While it may not function if a non-interactive shell is used.

We can use geteuid and getpwuid instead, which does not rely on tty or environment variable.

def get_user():
    try:
        return pwd.getpwuid(os.geteuid()).pw_name
    except KeyError:
        return ""
Vonng commented 1 month ago

Thanks @ paul for bring this issue, resolve by https://github.com/Vonng/pigsty/commit/dc2dfa8a48b8107b40f6860abcb6f281fbc272f1