-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add git_helpers.py w/ function for safe directory handling #35
Conversation
As for a unit test, there appears to be no way to change the configuration directories that are read by git. The only thing I can think of is a test only on HEAD which temporarily moves (in a context manager) the user config |
an alternative is to change the |
Regarding "As for a unit test, there appears to be no way to change the configuration directories that are read by git", I thought the GIT_CONFIG_SYSTEM and GIT_CONFIG_something (GLOBAL) could be used, but I didn't explore that after we decided not to use use them for a solution. |
I think this makes sense, so the boxes just need to get done and remove from WIP status and then ask for another review. |
The following test works for me (using the match string from the previous comment). It works only on HEAD, so the test stills needs some changes. import pytest
import tempfile
from ska_helpers import git_helpers
import git
import os
# @pytest.mark.filterwarnings("ignore:Updating git config")
def test_make_git_repo_safe():
with tempfile.TemporaryDirectory() as d, pytest.warns(
UserWarning, match="Updating git config"
):
os.environ["HOME"] = d
repo = git.Repo("/proj/sot/ska/data/chandra_models")
with pytest.raises(git.exc.GitCommandError):
repo.is_dirty()
git_helpers.make_git_repo_safe("/proj/sot/ska/data/chandra_models")
repo.is_dirty() |
Thanks for the updates @javierggt this is looking pretty close! Can you document your unit testing? I'll try to run the tests as well. |
cfa31b2
to
d6703f8
Compare
9aa30b3
to
7a9c572
Compare
Note to @javierggt - |
This didn't work for me on OSX but maybe we don't care. I set my chandra_models directory to be owned by the 'nx' user (because it was a user on my machine).
and then tried to run the drift model test from chandra_aca with this version of ska_helpers in my path. And my git config hasn't been modified and the test failed.
But trying to do this on OSX was a pretty contrived test anyway. |
Oh wait... does this PR just add the helper function? If so, seems like we need another PR to actually do the handling. |
I was coming to this realization. I think in all the discussion this final connection to actually use the new functionality got lost. Do'oh! But maybe that is best left as a separate PR. |
Well, it didn't get lost ... it just sounds like we didn't get around to defining requirements in its evolution from WIP. But that's fine. A new PR to use it is also fine and then functional testing like I tried above should work. |
Being honest in my mind it really did get lost because 2 hours ago (before your comment) I asked myself the question "where is this used now"? There are so many PR's open that I thought it might be in one of the others but finally came to the conclusion that it was nowhere. |
@jeanconn @javierggt - I believe this is ready for final review and testing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
See https://chandramission.slack.com/archives/G01LN40AXPG/p1688129800711439 for context. See also this blog for more info. Long story short is that git made a security update which requires setting a git config
safe.directory
in order to do any git operations on a git repo that is not owned by the user running the git command. This would break or greatly slow down the current infrastructure for accessingchandra_models
data.Instead this PR provides a convenient mechanism to restore shared access for select git repos in CXC operations. In practice this is expected to apply only to the
chandra_models
shared repo. This implies that CXC users trust that the owners of thechandra_models
repo to not be placing malicious content in the repo.If the user config does not permit safe access to the requested chandra_models repo then the user
~/.gitconfig
file will be updated (viagit config --global
) to mark that directory (and only that directory) as safe for access as a non-owner. A warning message to that effect is issued.On HEAD linux this typically results in adding the following two lines to
~/.gitconfig
:Bonus function
utils.get_owner(path)
I needed a platform-independent way to get the user name, which landed in
ska_helpers.utils
. l'm not sure if this is the only or best way, but it appears to work. Interestingly, if I ask for the owner of a VM-shared directory on Windows it returns"Everyone"
.Interface impacts
This may update the user's
~/.gitconfig
. See above.Testing
Unit and functional tests
aldcroft
with ska3-flight 2023.1 activated, otherwise vanilla) (at 35991b8)Mac
Linux HEAD
The relevant unit test ran to completion and passed (indicating it found a chandra_models with a different owner, verified the not safe condition and then fixed it):
Note that four tests of
ska_helpers.chandra_models
failed due the git safe problem. To test correcting this I did:This produced the expected warning and updated my
~/.gitconfig
to add these two lines:After doing this then the original four failing tests now pass.
Windows
Independent check of unit tests by Jean
Functional tests
Ran this on Windows where every VM-shared repo is considered unsafe. For example:
OSX - JC - I can confirm that after changing the permission on /Users/jean/git/chandra_models that this worked as expected
And that the correct safe directory entry was added to my .gitconfig.