Skip to content

build: run the image as a non-root user - #89

Merged
uniqueg merged 6 commits into
devfrom
build/non-root-image
Nov 7, 2026
Merged

uniqueg merged 6 commits into
devfrom
build/non-root-image

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Summary

The image ran as root. It now runs as the user tesk (UID 1000, group 0, home /home/tesk), as the images of foca and cloud-registry do. Group 0 owns the home directory, so arbitrary UIDs in group 0 (OpenShift) work too. The Ubuntu base image's user ubuntu, which has UID 1000, is removed first.

  • Dockerfile: user, HOME, WORKDIR, USER 1000:0.
  • Smoke test (ci.yml, job smoke): checks that the image user is 1000:0; starts the image with an arbitrary UID (1000750000:0) and a read-only root file system (--tmpfs /tmp), and checks that such a UID gets HOME=/home/tesk and may write to it.
  • Docs: docs/deployment.md (user, home, kubeconfig location, read-only root file system, rootless Docker), the docker run examples in the README and docs/deployment.md (kubeconfig mounted to /home/tesk/.kube/config, run with --user "$(id -u):0" so that a 0600 kubeconfig can be read), and an upgrade note in docs/upgrading.md.

The TESK chart already runs the API as UID 1000 when tesk.securityContext.enabled is true (the default), and mounts only a ConfigMap (0644), so the chart needs no change. Testing on OpenShift is left for a deployment (see the issue).

Closes #83 (part of #65)

Verification

  • docker build and the smoke script of the smoke job, run locally: before the change it failed with "Image user is '', expected '1000:0'"; after it, it passes (both profiles, arbitrary UID with read-only root file system, home directory check, Swagger UI setting).
  • docker run --rm --entrypoint id tesk-api:smoke: uid=1000(tesk) gid=0(root).
  • actionlint 1.7.7: no findings.
  • CI: all checks passed (gate CI passed, Smoke test with the new checks, Test, Scan image, Docs, Lint, CodeQL, Codecov), also after merging dev with the coverage check (ci: add coverage targets #88).

Lane

Full review: adversarial-reviewer and quality-reviewer, two rounds. Fixed: the docs' docker run examples failed for a 0600 kubeconfig of a host UID other than 1000 (medium; now --user "$(id -u):0"), the unverified chart claim in the upgrade note (medium; now names tesk.securityContext), the userdel ... || true (now guarded by id ubuntu), no check of the home directory for arbitrary UIDs (now in the smoke test), a note for rootless Docker.

Declined review findings

  • Small differences from foca's Dockerfile (USER 1000:0 instead of USER 1000, no /app directory): equivalent, and the explicit group documents the design.

Open questions

None.

🤖 Generated with Claude Code

Summary by Sourcery

Run the TESK API image as a non-root user while preserving compatibility with arbitrary group-0 UIDs and existing Kubernetes deployments.

Enhancements:

  • Run the container image as non-root user tesk (UID 1000, group 0) with a shared home directory that supports arbitrary group-0 UIDs.
  • Document non-root execution, kubeconfig mounting, read-only filesystems, and rootless Docker usage.

Build:

  • Configure the image home directory, working directory, and default user for non-root execution.

CI:

  • Extend smoke tests to verify the configured image user and arbitrary-UID operation with a read-only root filesystem and writable home directory.

Documentation:

  • Add upgrade guidance for the image user change and update Docker usage examples for the new kubeconfig location and user mapping.

uniqueg and others added 4 commits November 7, 2026 09:01
The image ran as root. It now runs as the user tesk (UID 1000, group
0, home /home/tesk), replacing the base image's user ubuntu with the
same UID, as the images of foca and cloud-registry do. Group 0 owns
the home directory, so that arbitrary UIDs in group 0 (OpenShift)
work too. The smoke test checks the image's user and starts it with
an arbitrary UID and a read-only root file system.

Closes #83

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Check in the smoke test that an arbitrary UID in group 0 gets the home
directory and may write to it. Run the documented local examples with
the host's UID in group 0, so that a kubeconfig readable only by the
host user can be read. Guard the removal of the user ubuntu.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Configuration

Configure the image to run as tesk (UID 1000, group 0), with a group-writable home directory and that directory as its working directory.

  • Dockerfile L19-31

Documentation

Update the local run examples and deployment and upgrade guidance to explain the non-root identity, kubeconfig location and permissions, read-only filesystem support, and rootless Docker option.

  • README.md L40-46
  • docs/deployment.md L27-34, L76-77
  • docs/upgrading.md L11-19

Tests

Extend the smoke test to verify the configured image identity and check arbitrary group-0 UID operation, including writable home-directory behavior.

  • .github/workflows/ci.yml L198-203, L221-232

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 56.39%. Comparing base (a72db27) to head (0ffceaf).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev      #89   +/-   ##
=======================================
  Coverage   56.39%   56.39%           
=======================================
  Files          66       66           
  Lines        2167     2167           
  Branches      236      236           
=======================================
  Hits         1222     1222           
  Misses        804      804           
  Partials      141      141           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review November 7, 2026 08:24

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="Dockerfile" line_range="24" />
<code_context>
+# writes only to `/tmp`; the Kubernetes client reads `~/.kube/config` (or
+# `KUBECONFIG`) when it runs outside a cluster. The user `ubuntu` of the
+# Ubuntu base image is removed, as it has UID 1000
+ENV HOME=/home/tesk
+RUN if id ubuntu > /dev/null 2>&1; then userdel --remove ubuntu; fi && \
+    useradd --uid 1000 --gid 0 --create-home --home-dir "${HOME}" tesk && \
</code_context>
<issue_to_address>
**Mounted kubeconfig is ignored**

When the image runs outside a cluster as a UID with no passwd entry and `KUBECONFIG` is unset, `Config.defaultClient()` uses Java’s `user.home`, which `HOME` does not set for that UID, so it looks somewhere other than `/home/tesk/.kube/config`; the mounted kubeconfig is not loaded and Kubernetes requests fail.

Set Java’s `user.home` to `/home/tesk` or otherwise ensure it resolves to that directory.

Also at `docs/deployment.md:32`, `docs/deployment.md:34`, `README.md:43`.
</issue_to_address>

### Comment 2
<location path="README.md" line_range="43" />
<code_context>
 ```bash
-docker run --rm --publish 127.0.0.1:8080:8080 \
-  --volume "${KUBECONFIG:-${HOME}/.kube/config}:/root/.kube/config:ro" \
+docker run --rm --publish 127.0.0.1:8080:8080 --user "$(id -u):0" \
+  --volume "${KUBECONFIG:-${HOME}/.kube/config}:/home/tesk/.kube/config:ro" \
   elixircloud/tesk-api:latest
</code_context>
<issue_to_address>
**Rootless Docker cannot read kubeconfig**

When a user follows the README quick start with rootless Docker and a kubeconfig readable only by the host user, rootless Docker maps host-owned files to container UID 0, but this command runs the process as the host UID inside the container. A kubeconfig readable only by its owner is therefore unreadable, so Kubernetes requests fail for rootless Docker users following the quick start.

Add the rootless Docker exception to the README example, or direct users to the deployment instructions for the appropriate `--user` value.
</issue_to_address>

Sourcery assessment

Approval pending. 2 findings to address first.

Blocking findings: Dockerfile:24, README.md:43


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread Dockerfile Resolved
Comment thread README.md Resolved
@uniqueg
uniqueg merged commit 2ab2ecb into dev Nov 7, 2026
25 checks passed
@uniqueg
uniqueg deleted the build/non-root-image branch November 7, 2026 08:43
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.

build: run the image as a non-root user

1 participant