Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Look up live user from PKEXEC_UID #5074

Merged
merged 1 commit into from
Sep 6, 2023

Conversation

halfline
Copy link
Contributor

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.

@github-actions github-actions bot added the f39 label Aug 23, 2023
@halfline
Copy link
Contributor Author

note this is untested. it's just fixing a problem I saw when looking at recent commit history.

@halfline
Copy link
Contributor Author

cc @jkonecny12

@jkonecny12
Copy link
Member

Thanks, I'll take a look on this.

@jkonecny12
Copy link
Member

Tested and seems to be fix for https://bugzilla.redhat.com/show_bug.cgi?id=2235728 .

@jkonecny12
Copy link
Member

@halfline thanks for the PR! Do you agree that I'll take over this PR and finish it?

@halfline
Copy link
Contributor Author

halfline commented Sep 1, 2023

@halfline thanks for the PR! Do you agree that I'll take over this PR and finish it?

yes please

@jkonecny12
Copy link
Member

jkonecny12 commented Sep 1, 2023

Verified even in the Live environment and it seems to be working correctly everywhere, great work!

We just need to fix the tests and link the bug.

@jkonecny12
Copy link
Member

@VladimirSlavik, @M4rtinK, @rvykydal could you please tell me if you agree with the second commit (should be squashed if you agree). I tried to do this in way of pytest because they have great monkeypatching for environment vars, however, it was a bit bigger overhaul.

What I wonder is if we want to go this way or rather stick with the current UnitTests approach. I definitely can change the tests to the old solution if we prefer that.

Copy link
Contributor

@VladimirSlavik VladimirSlavik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test also really possible values for the environment variable:

  • negative number
  • zero
  • huge number out of range
  • non-numeric string such as "hello world"
  • empty string
  • etc...

The actual code as written now does not try to do anything beyond surviving missing variable.

@jkonecny12
Copy link
Member

UPDATED:

  • Remove pytest migration - this is not the best PR for that test
  • Updated the tests to have fixed tests

@jkonecny12
Copy link
Member

I definitely can add that but I wonder what we want to cover here? If the PKEXEC_UID will be wrong then policy kit doesn't work as expected and we have much more problems then broken installation. Do we want to be that paranoid?

@jkonecny12
Copy link
Member

I guess you can't be paranoid enough 😆

Anyway, updated to add more tests for the input environment variable.

@jkonecny12
Copy link
Member

/kickstart-test --testtype smoke

@VladimirSlavik
Copy link
Contributor

I guess you can't be paranoid enough 😆

Ah, sorry. The point was that there are values that will clearly make the code crash, and that the code is always executed, so you want to make it not-crashy.

Copy link
Contributor

@VladimirSlavik VladimirSlavik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, looks good to me in terms of ensuring it works.

Please consider merging the "fail" tests and adding doc texts.

Comment on lines 53 to 61
@patch("pyanaconda.core.live_user.conf")
@patch("pyanaconda.core.live_user.getpwuid")
@patch.dict("pyanaconda.core.live_user.os.environ", {"PKEXEC_UID": "1024"})
def test_get_live_user_with_failed_getpwuid(self, getpwuid_mock, conf_mock):
# supposedly live but missing user
conf_mock.system.provides_liveuser = True
getpwuid_mock.side_effect = KeyError
assert get_live_user() is None
getpwuid_mock.assert_called_once_with(1024)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, now I understand why you thought this is verbose compared to pytest! You can actually collapse all of the "fail" tests into one by using patch.dict within a loop:

    @patch("pyanaconda.core.live_user.conf")
    @patch("pyanaconda.core.live_user.getpwuid")
    def test_get_live_user_with_wrong_pkexec_env(self, getpwuid_mock, conf_mock):
        """Test getting live user with wrong environment values"""
        # supposedly live but missing user
        conf_mock.system.provides_liveuser = True
        
        # no value
        with patch.dict("pyanaconda.core.live_user.os.environ", dict()):
            assert get_live_user() is None
            getpwuid_mock.assert_not_called()
        
        # all the rest
        for value in ("", "0", "-1", "999999999999999999999999", "hello world"):
            with patch.dict("pyanaconda.core.live_user.os.environ", {"PKEXEC_ENV": value}):
                assert get_live_user() == None
                getpwuid_mock.assert_not_called()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, your suggested solution is probably better one. I was balancing between cleanliness and correctness.

Having each of these tests as separate test seems to me like more correct approach - one test / one test-case. However, yeah it's pretty verbose.

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.

Resolves: rhbz#2235728
@jkonecny12
Copy link
Member

UPDATED

@jkonecny12
Copy link
Member

/kickstart-test --testtype smoke

@jkonecny12 jkonecny12 merged commit 77c084a into rhinstaller:master Sep 6, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

3 participants