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

Enable sudo on agent.connect() when MUTAGEN_USE_SUDO environment var set to 1 #505

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mikeschinkel
Copy link

@mikeschinkel mikeschinkel commented Aug 3, 2024

While trying to fix Mutagen for my use-case, I specifically decided to do the least work possible so I did not consider any refactoring which I might have had a been a member of the Mutagen team, so please judge my PR accordingly.

I found one problem followed by another. First, I needed to make Mutagen run the agent as sudo, and then I needed to use visudo to make /var/lib/docker/volumes/*/_data available to me (note the wildcard in the path). The specific text I used in visudo was:

mikeschinkel ALL=(ALL) NOPASSWD: /home/mikeschinkel/.mutagen/agents/*/mutagen-agent

Of course sudo should ONLY be used when someone specifically requests it and this PR handles that.

To use sudo only when specifically requested and not when others who use Mutagen do not need or want sudo I considered adding a configuration item — and even did so and got it working — but then realized it was not synchronization vs. forwarding specific. I also decided I could do it with fewer code changes by just using an environment variableMUTAGEN_USE_SUDO — so that is what I did.

I modified ./pkg/agent/dial.go and adding the following code just before the command, composed with fmt.Sprintf():

// Add sudo if env var says to add sudo
var sudo string
if os.Getenv("MUTAGEN_USE_SUDO") == "1" {
  sudo = fmt.Sprintf("test ! -f '%s' && printf '%s' >&2 || sudo ", agentInvocationPath, notInstalledFlag)
}

The above code first tests to see if the agent exists and if it does not it prints the value of notInstalledFlag — or "Agent not installed" — to stdErr. This allows a test for that value later in the func.

If that is returned my code then causes agent.connect() to immediately return to its caller while setting TryInstall and CmdExe to true to cause the installer to try to install the agent. I had to add that last bit because there were numerous times during my testing where I found Mutagen would simply give up and never retry to install when the agent was not installed.

Hopefully you will find this useful and that it will meet the needs of others with similar scenarios to the one I experienced. But if you do not, I will at least have a custom Mutagen that meets my needs.

- Allow `MUTAGEN_USE_SUDO=1` to cause `agent.connect()` calls to be prefixed with `sudo` and a prior check to ensure the executable exists. This allows an admin to pre-configure `visudo` to support access to specific directories that otherwise require enhanced privledges, i.e. Docker volumes at `/var/lib/docker/volumes/*/_data`
- If the executable does not exist the value of the constant `notInstalledFlag` which is `"Agent not installed"` is written to the error log.
- If `"Agent not installed"` is returned in the error output then `connect()` returns with `TryInstall` set to `true` to cause the installer to install the agent.
@mikeschinkel
Copy link
Author

Sorry for the multiple force-pushes. This was the first time I had to create a signed commit and it took a few tries to get it all working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant