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

Run containers as a non-root user #2053

Merged
merged 11 commits into from
Jun 8, 2023
Merged

Run containers as a non-root user #2053

merged 11 commits into from
Jun 8, 2023

Conversation

alexintech
Copy link
Contributor

@alexintech alexintech commented May 30, 2023

What this PR does

Create a custom non-root user and use it to start an app. So uwsgi does not require to use setUid and setGid system calls.

It handles errors while starting in Kubernetes with runAsNonRoot: true check.

Which issue(s) this PR fixes

closes #445

Checklist

  • Unit, integration, and e2e (if applicable) tests updated
  • Documentation added (or pr:no public docs PR label added if not required)
  • CHANGELOG.md updated (or pr:no changelog PR label added if not required)

@alexintech alexintech requested a review from a team May 30, 2023 11:15
Copy link
Contributor

@joeyorlando joeyorlando left a comment

Choose a reason for hiding this comment

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

Overall looks great to me! Thanks for picking this up. Will approve once you've addressed the questions/concerned listed 😄

@iskhakov do you mind giving this a second set of 👀 ?

docker-compose-developer.yml Outdated Show resolved Hide resolved
docker-compose-developer.yml Outdated Show resolved Hide resolved
engine/Dockerfile Outdated Show resolved Hide resolved
engine/Dockerfile Outdated Show resolved Hide resolved
engine/Dockerfile Show resolved Hide resolved
docker-compose-developer.yml Show resolved Hide resolved
@joeyorlando joeyorlando added the pr:no public docs Added to a PR that does not require public documentation updates label Jun 1, 2023
Co-authored-by: Joey Orlando <joseph.t.orlando@gmail.com>
Copy link
Contributor

@iskhakov iskhakov left a comment

Choose a reason for hiding this comment

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

overall looks good to me

I would like to make a final look before merging, so I leave my review in "Request changes" state


# Create a group and user to run an app
ENV APP_USER=appuser
RUN groupadd --system --gid 1000 ${APP_USER} && \
Copy link
Contributor

Choose a reason for hiding this comment

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

can we keep gid=2000 to make sure it is backwards-compatible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can change to gid=2000.

I think it's a common practice to set the same uid and gid for a custom user. I suppose that nothing would break, as process that starts application have the same user permissions. Something can be broken, if another process with some other uid and in the same group wants to use files with group permissions. But I suppose it's not applicable to dockerized application.

I'll change to gid=2000 if you prefer that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @iskhakov @joeyorlando ! Change to gid=2000 or not ?

Copy link
Contributor

@joeyorlando joeyorlando Jun 8, 2023

Choose a reason for hiding this comment

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

I agree w/ @iskhakov , lets keep it to 2000 just to be safe. After that, I think we're safe to merge 😄

@joeyorlando joeyorlando enabled auto-merge June 8, 2023 06:53
@joeyorlando joeyorlando added this pull request to the merge queue Jun 8, 2023
Merged via the queue into grafana:dev with commit f67cfd0 Jun 8, 2023
RUN chown -R 1000:2000 /tmp/prometheus_django_metrics
ENV prometheus_multiproc_dir "/tmp/prometheus_django_metrics"
# Change to a non-root user (number is required by Kubernetes runAsNonRoot check)
USER 1000
Copy link
Contributor

Choose a reason for hiding this comment

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

@alexintech actually one thought came to mind. If we move USER 1000 higher up to line 13, I think we may not need any of the chown commands?

Copy link
Contributor Author

@alexintech alexintech Jun 8, 2023

Choose a reason for hiding this comment

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

If we move USER 1000 higher up to line 13

That will break RUN apt-get install -y sqlite3 default-mysql-client postgresql-client here in dev target as it requires root.

Copy link
Contributor

Choose a reason for hiding this comment

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

that makes sense 👍

I opened a PR to move USER to the end of the dev target so that it is present if a different build target is specified to docker build

@alexintech alexintech deleted the docker-non-root branch June 8, 2023 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:no public docs Added to a PR that does not require public documentation updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Helm - SecurityContext - cannot setgid() as non-root user
3 participants