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

[enhancement]: Add option to skip GrantContainerUserSUDOPrivilege in job container init #4332

Open
joshchngs opened this issue Jun 25, 2023 · 5 comments
Labels
Area: Agent Containers Issues related to Docker, containerd, etc. enhancement

Comments

@joshchngs
Copy link

joshchngs commented Jun 25, 2023

Describe your feature request here

I would like to be able to skip this section, per container definition:

executionContext.Output(StringUtil.Loc("GrantContainerUserSUDOPrivilege", containerUserName));
// Create a new group for giving sudo permission
string sudoGroupName = "azure_pipelines_sudo";
await DockerExec(executionContext, container.ContainerId, $"groupadd {sudoGroupName}");
// Add the new created user to the new created sudo group.
await DockerExec(executionContext, container.ContainerId, $"usermod -a -G {sudoGroupName} {containerUserName}");
// Allow the new sudo group run any sudo command without providing password.
await DockerExec(executionContext, container.ContainerId, $"su -c \"echo '%{sudoGroupName} ALL=(ALL:ALL) NOPASSWD:ALL' >> /etc/sudoers\"");

If my image user already has sudo privilege, these steps are unnecessary. If I define the container with --user <something> they can't work. I want to be able to tell the agent that I've already handled setting up sudo for the job user.

Why?

The documentation says that the container user must be able to run these commands. In reality, only root can do this, as su will prompt for a password otherwise. Therefore, it is only possible to use images with USER=root.

Side note, I don't understand the /etc/sudoers write. This file should be read-only (mode 0440), and AFAIK it is in most base images. I don't understand how this command ever succeeds.

There are a few use cases where USER=root is not possible, including mine. The highlighted docker execs are the root cause, and are mentioned in a few places.

Workarounds

The links above mention a few workarounds, such as passing --user 0:0 as a docker create option. This doesn't work for all use cases.

I'm currently doing:

Dockerfile

RUN chmod u+s /usr/sbin/groupadd /usr/sbin/usermod && \
    chmod u+w /etc/sudoers
COPY su_hack.sh /bin/su

su_hack.sh

#!/usr/bin/env bash
echo "'su' has been disabled in this container"

Caveats

The use case enabled by adding the option is only viable if the image already has a correctly configured user with matching UID/GID for the container init step to find. This means that either the agent host user UID/GID needs to be controlled, or the image needs to be rebuilt on each agent before it's used.

@vmapetr
Copy link
Contributor

vmapetr commented Jun 26, 2023

Hi, @joshchngs thanks for reporting!
We are currently working on more prioritized issues but will get back to this one soon.

@pixdrift
Copy link

Thanks for raising this issue, I think this whole block of code that interacts with and modifies the container needs discussion, it causes us no end of issues when running generic container images.
https://github.com/microsoft/azure-pipelines-agent/blob/master/src/Agent.Worker/ContainerOperationProvider.cs#L544-L764

I think more 'configuration knobs' to turn off the agent behaviour around creation, initialisation, and execution of containers, including this entire block of code would be a useful addition.

The agent always mounting in the docker.socket (or all the bind mounts for that matter) should probably be optional too!

Copy link

github-actions bot commented Mar 2, 2024

This issue has had no activity in 180 days. Please comment if it is not actually stale

@github-actions github-actions bot added the stale label Mar 2, 2024
@joshchngs
Copy link
Author

@vmapetr I think your bot is throwing shade. Any progress?

@github-actions github-actions bot removed the stale label Mar 4, 2024
@asad26
Copy link

asad26 commented Aug 22, 2024

Any update on this. When can we have such feature?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Agent Containers Issues related to Docker, containerd, etc. enhancement
Projects
None yet
Development

No branches or pull requests

5 participants