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

docker: Default to non-root uid/gid in images #4295

Closed
anderseknert opened this issue Jan 27, 2022 · 8 comments · Fixed by #6373
Closed

docker: Default to non-root uid/gid in images #4295

anderseknert opened this issue Jan 27, 2022 · 8 comments · Fixed by #6373
Assignees

Comments

@anderseknert
Copy link
Member

OPA users have a couple of options to choose from when it comes to Docker images. For any given release, we'll have these image versions published:

openpolicyagent/opa:<version>
openpolicyagent/opa:<version>-debug
openpolicyagent/opa:<version>-rootless
openpolicyagent/opa:<version>-static
openpolicyagent/opa:<version>-envoy
openpolicyagent/opa:<version>-envoy-rootless

For various historic reasons, we've defaulted to uid/gid 0 (i.e. root) in the "default" image, providing a -rootless version for those that wanted to opt out of that. Some time later the -static version appeared to be able to run from base images without glibc such as Alpine. This wasn't made -rootless though. Rather than adding a -static-rootless as well, we should switch the default image run OPA as rootless by default, and allow users that need to run as root for some reason (like having to bind to port 443 without a service in front) to rather opt in to that. This should allow us to reduce the number of image variants, and provide better defaults.

openpolicyagent/opa:<version>
openpolicyagent/opa:<version>-debug
openpolicyagent/opa:<version>-static
openpolicyagent/opa:<version>-envoy

Users who still want or need to run OPA as the root user inside of the container, can easily do so via the docker run --user flag, or by setting the runAsUser, and other approriate attributes on the securityContext in a Kubernetes pod.

Suggested changes:

  1. Print warning in logs when OPA is run as root user/group, and information about coming change of defaults.
  2. 3-6 releases later, switch default to rootless.
  3. Stop publishing -rootless images.

Since we're going slowly with this change, I hope it will give members of the OPA community time to make their voices heard if there is something more we need to consider before making this change.

It should be noted that with the exception of the -debug image, OPA does not provide a shell, or other commands, in its images. The change suggested here is to align with best practices, not to prevent any known issue or attack vector.

@anderseknert
Copy link
Member Author

Alright, so starting from next release (v0.39.0):

  • The -rootless image now uses gid 1000 (same as uid)
  • Warning is printed when running OPA with uid/gid, including a notice where we recommend -rootless (if docker context) and stating that this will be made the default in future releases.

@ashutosh-narkar
Copy link
Member

@anderseknert the changes needed for this are already in afaict. Can we close this ?

@anderseknert
Copy link
Member Author

Not really. Rootless mode is not the default for the standard image. This ticket can be closed when:

  • The standard OPA images have switched to uid/gid which is not 0.
  • The -rootless images have been decomissioned.

What's been done so far is preparing for that by printing a warning from the next release and forward, plus made the -rootless image run with gid=1000 so that it won't print the same warning. As mentioned in the description, the plan is to move forward with that 3-6 releases from the next one. Until then, there's not a lot to do here.

@srenatus
Copy link
Contributor

srenatus commented Apr 1, 2022

Can we also update this quickstart to not need the image with privileges? 👉 https://github.com/open-policy-agent/opa-envoy-plugin/blob/8be224a995562e01c446f402285bbaabdbc68dc0/examples/istio/quick_start.yaml#L279

@anderseknert
Copy link
Member Author

Indeed. We should additionally bind to port 8443 (or whatever) in the OPA container manifest, and have the Service do the 443->8443 port mapping.

anderseknert added a commit to anderseknert/opa that referenced this issue Jan 4, 2023
For the changelog:

Breaking change: all OPA images now run with a non-root uid/gid.
This means there is no longer a need for the -rootless image variant,
and it has thus been decomissioned. If you were using the -rootless
images before, you'll need to change your configuration to use the
regular image (i.e. without the -rootless suffix).

While the OPA images contain no other software (like a shell), running
as root is still a bad practice. If you for some reason **must** run
OPA with root privileges, this can still be achieved by explicitly
setting the user, either with the `--user` argument for `docker run`,
or in the `securityContext` of your Kubernetes pod spec.

Fixes open-policy-agent#4295

Signed-off-by: Anders Eknert <anders@eknert.com>
@ashutosh-narkar
Copy link
Member

In #5718, the standard OPA images have switched to uid/gid which is not 0. A note is added to the -rootless images that they will not be published post 0.50.0

@ashutosh-narkar ashutosh-narkar moved this from Backlog to In Progress in Open Policy Agent Mar 8, 2023
@ashutosh-narkar ashutosh-narkar moved this from In Progress to Planning - v0.52 in Open Policy Agent Mar 30, 2023
@ashutosh-narkar ashutosh-narkar self-assigned this Mar 30, 2023
@ashutosh-narkar ashutosh-narkar moved this from Planned - v0.52 to Planning - v0.53 in Open Policy Agent Apr 7, 2023
@ashutosh-narkar ashutosh-narkar moved this from Planning - v0.53 to Planning - v0.54 in Open Policy Agent May 19, 2023
@ashutosh-narkar ashutosh-narkar moved this from Planning - v0.54 to Planning - v0.55 in Open Policy Agent Jun 26, 2023
@charlieegan3
Copy link
Contributor

I think that once we have merged #6086, we can close this as the following items will then have been done:

The standard OPA images have switched to uid/gid which is not 0.

We are using non-root base images (and use the same user:group settings for clarity in #6086) also see #5540 #5787 #5718

The -rootless images have been decommissioned.

#6086 completes this.

I'm not really sure what if anything is still needed in opa-envoy #4295 (comment) but think we can move to an issue on that repo if still relevant?

@charlieegan3 charlieegan3 moved this from Planning - v0.55 to In Progress in Open Policy Agent Jul 11, 2023
@charlieegan3
Copy link
Contributor

Following some discussion, I have now implemented this: #6091 feedback welcome.

@ashutosh-narkar ashutosh-narkar moved this from In Progress to Planning - v0.56 in Open Policy Agent Jul 13, 2023
@ashutosh-narkar ashutosh-narkar moved this from Planning - v0.58 to Planning - v0.59 in Open Policy Agent Oct 2, 2023
@ashutosh-narkar ashutosh-narkar moved this from Planning - v0.59 to Planning - v0.58 in Open Policy Agent Oct 2, 2023
@ashutosh-narkar ashutosh-narkar moved this from Planning - v0.58 to Planning - v0.59 in Open Policy Agent Oct 2, 2023
@ashutosh-narkar ashutosh-narkar removed this from Planning - v0.59 (TBD) in Open Policy Agent Oct 6, 2023
@ashutosh-narkar ashutosh-narkar added this to Backlog in Open Policy Agent via automation Oct 26, 2023
@ashutosh-narkar ashutosh-narkar moved this from Backlog to Planning - v0.59 in Open Policy Agent Oct 26, 2023
ashutosh-narkar added a commit to ashutosh-narkar/opa that referenced this issue Nov 1, 2023
All published OPA images now run with a non-root uid/gid.
The uid:gid is set to 1000:1000 for all images. As a result
there is no longer a need for the --rootless image variant
hence it will not be published as part of future releases.
This change is in line with container security best practices.
OPA can still be run with root privileges by explicitly setting the user,
either with the --user argument for docker run, or by specifying
the securityContext in the Kubernetes Pod specification.

Fixes: open-policy-agent#4295

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
Open Policy Agent automation moved this from Planning - v0.59 to Done Nov 2, 2023
ashutosh-narkar added a commit that referenced this issue Nov 2, 2023
All published OPA images now run with a non-root uid/gid.
The uid:gid is set to 1000:1000 for all images. As a result
there is no longer a need for the --rootless image variant
hence it will not be published as part of future releases.
This change is in line with container security best practices.
OPA can still be run with root privileges by explicitly setting the user,
either with the --user argument for docker run, or by specifying
the securityContext in the Kubernetes Pod specification.

Fixes: #4295

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment