Skip to content

Commit

Permalink
core: Look up live user from PKEXEC_UID
Browse files Browse the repository at this point in the history
anaconda can be run as gnome-initial-setup or liveuser on live
images, depending on how its started by the use.

It currently assumes it will be started as liveuser always.

This commit makes it look up the username and home directory by
querying PKEXEC_UID environment variable.
  • Loading branch information
halfline committed Aug 26, 2023
1 parent 4d435b5 commit 645dbb3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
12 changes: 8 additions & 4 deletions pyanaconda/core/live_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#

from collections import namedtuple
from pwd import getpwnam
from pwd import getpwuid

from pyanaconda.core.configuration.anaconda import conf

Expand All @@ -38,13 +38,17 @@ def get_live_user():
return None

try:
username = "liveuser"
uid = getpwnam(username).pw_uid
if not "PKEXEC_UID" in os.environ:
return None
uid = int(os.environ.get("PKEXEC_UID"))
passwd_entry = getpwuid(uid)
username = passwd_entry.pw_name
home_dir = passwd_entry.pw_dir
env_prune = ("GDK_BACKEND",)
env_add = {
"XDG_RUNTIME_DIR": "/run/user/{}".format(uid),
"USER": username,
"HOME": "/home/{}".format(username),
"HOME": home_dir,
}
return User(name=username,
uid=uid,
Expand Down
18 changes: 10 additions & 8 deletions tests/unit_tests/pyanaconda_tests/core/test_live_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# Red Hat, Inc.
#

import os
from unittest import TestCase
from unittest.mock import patch, Mock
from pyanaconda.core.live_user import get_live_user, User
Expand All @@ -25,16 +26,17 @@
class GetLiveUserTests(TestCase):

@patch("pyanaconda.core.live_user.conf")
@patch("pyanaconda.core.live_user.getpwnam")
def test_get_live_user(self, getpwnam_mock, conf_mock):
@patch("pyanaconda.core.live_user.getpwuid")
def test_get_live_user(self, getpwuid_mock, conf_mock):
# not live = early exit
conf_mock.system.provides_liveuser = False
assert get_live_user() is None
getpwnam_mock.assert_not_called()
getpwuid_mock.assert_not_called()

# live and has user
conf_mock.system.provides_liveuser = True
getpwnam_mock.return_value = Mock(pw_uid=1024)
os.environ['PKEXEC_UID']=1024
getpwuid_mock.return_value = Mock(pw_uid=1024, pw_dir='/home/liveuser', pw_name='liveuser')
assert get_live_user() == User(name="liveuser",
uid=1024,
env_prune=("GDK_BACKEND",),
Expand All @@ -43,10 +45,10 @@ def test_get_live_user(self, getpwnam_mock, conf_mock):
"USER": "liveuser",
"HOME": "/home/liveuser",
})
getpwnam_mock.assert_called_once_with("liveuser")
getpwnam_mock.reset_mock()
getpwuid_mock.assert_called_once_with("liveuser")
getpwuid_mock.reset_mock()

# supposedly live but missing user
getpwnam_mock.side_effect = KeyError
getpwuid_mock.side_effect = KeyError
assert get_live_user() is None
getpwnam_mock.assert_called_once_with("liveuser")
getpwuid_mock.assert_called_once_with("liveuser")

0 comments on commit 645dbb3

Please sign in to comment.