From bd423cc0553dbf8d11d977517e5b0dfc08c53f03 Mon Sep 17 00:00:00 2001 From: Niklas Wagner Date: Sun, 1 Nov 2026 16:44:21 +0100 Subject: [PATCH 1/4] feat!: validate runtime users for every container platform --- README.md | 17 +++++ internal/containerartifact/artifact.go | 39 +++++++++- internal/containerartifact/artifact_test.go | 84 ++++++++++++++++++++- 3 files changed, 137 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 42f3dec..25afec8 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,20 @@ Shared container build and release workflows for Tuxcrate. [Documentation](https://github.com/tuxcrate/docs/blob/main/docs/maintainers/container-releases.md) + +## Shared runtime-user validation + +Before running a repository's `tests/smoke.sh`, `container-artifact test` checks +every platform image with its default user, no network, and `/bin/sh` as the +entrypoint. Images must provide `/bin/sh` and `id`. The default requirement is +user and group `tuxcrate`, both with numeric ID 1000. The check also runs when +there is no repository smoke hook. + +Images that require root must explicitly declare +`LABEL com.tuxcrate.runtime-user="root"` and document why. This checks for +`0:0:root:root`; it does not skip user validation. The optional value `tuxcrate` +declares the default explicitly; all other values fail validation. + +Keep directory permissions, application startup, and API checks in repository +smoke hooks. Deploy root-user declarations before enabling the shared validator, +and deploy the validator before removing duplicate checks from callers. diff --git a/internal/containerartifact/artifact.go b/internal/containerartifact/artifact.go index 1c0901f..10de444 100644 --- a/internal/containerartifact/artifact.go +++ b/internal/containerartifact/artifact.go @@ -214,14 +214,17 @@ func (a app) test(ctx context.Context, archive, requested, version string) (err if strings.TrimSpace(string(actual)) != p.OS+"/"+p.Architecture { return fmt.Errorf("Loaded %s instead of %s", strings.TrimSpace(string(actual)), names[i]) } + testIDs = append(testIDs, id) + if err := a.checkRuntimeUser(ctx, image, names[i], id); err != nil { + return err + } if hook != nil { - testIDs = append(testIDs, id) fmt.Fprintf(a.stdout, "Running tests/smoke.sh for %s (%s).\n", names[i], version) if err := a.runHook(ctx, image, version, names[i], id, 5*time.Minute, 30*time.Second); err != nil { return err } } else { - fmt.Fprintln(a.stdout, "No tests/smoke.sh; retaining Containerfile assertions and platform validation only.") + fmt.Fprintln(a.stdout, "No tests/smoke.sh; shared platform and runtime-user validation still apply.") } } current, err := archiveHash(archive) @@ -252,6 +255,38 @@ func (a app) test(ctx context.Context, archive, requested, version string) (err return nil } +func (a app) checkRuntimeUser(ctx context.Context, image, platform, id string) error { + ctx, cancel := context.WithTimeout(ctx, time.Minute) + defer cancel() + raw, err := a.command(ctx, true, "docker", "image", "inspect", "--format", "{{json .Config.Labels}}", image) + if err != nil { + return err + } + var labels map[string]string + if err := json.Unmarshal(raw, &labels); err != nil { + return fmt.Errorf("decode image labels for %s: %w", platform, err) + } + expected := "1000:1000:tuxcrate:tuxcrate" + switch policy := labels["com.tuxcrate.runtime-user"]; policy { + case "", "tuxcrate": + case "root": + expected = "0:0:root:root" + default: + return fmt.Errorf("unsupported com.tuxcrate.runtime-user label %q for %s; expected tuxcrate or root", policy, platform) + } + fmt.Fprintf(a.stdout, "Checking default runtime user for %s (expected %s).\n", platform, expected) + actual, err := a.command(ctx, true, "docker", "run", "--rm", "--pull=never", "--platform", platform, + "--network", "none", "--label", "io.tuxcrate.smoke-test="+id, "--entrypoint", "/bin/sh", image, + "-eu", "-c", `printf '%s:%s:%s:%s\n' "$(id -u)" "$(id -g)" "$(id -un)" "$(id -gn)"`) + if err != nil { + return fmt.Errorf("runtime-user check for %s: %w", platform, err) + } + if actual := strings.TrimSpace(string(actual)); actual != expected { + return fmt.Errorf("runtime-user check for %s: expected %s, got %q", platform, expected, actual) + } + return nil +} + func (a app) cleanupCommand(ctx context.Context, args ...string) { if _, err := a.command(ctx, false, "docker", args...); err != nil { fmt.Fprintln(a.stderr, err) diff --git a/internal/containerartifact/artifact_test.go b/internal/containerartifact/artifact_test.go index f3962a2..55ad376 100644 --- a/internal/containerartifact/artifact_test.go +++ b/internal/containerartifact/artifact_test.go @@ -70,7 +70,9 @@ fi set -eu case "$1 $2" in 'image inspect') - if [[ ${WRONG_PLATFORM:-} == true ]]; then + if [[ $4 == '{{json .Config.Labels}}' ]]; then + printf '%s\n' "${IMAGE_LABELS:-null}" + elif [[ ${WRONG_PLATFORM:-} == true ]]; then echo linux/386 elif [[ ${!#} == *-1 ]]; then echo linux/arm64 @@ -78,6 +80,22 @@ case "$1 $2" in echo linux/amd64 fi ;; + 'run --rm') + test "$3" = --pull=never + test "$4" = --platform + test "$6" = --network + test "$7" = none + test "$8" = --label + [[ $9 == io.tuxcrate.smoke-test=* ]] + test "${10}" = --entrypoint + test "${11}" = /bin/sh + [[ ${12} == tuxcrate-test:* ]] + test "${13}" = -eu + test "${14}" = -c + echo "user:$5" >> "$FIXTURE/events" + if [[ ${FAIL_USER_PLATFORM:-} == "$5" ]]; then exit 24; fi + printf '%s\n' "${RUNTIME_IDENTITY:-1000:1000:tuxcrate:tuxcrate}" + ;; 'image rm') echo "remove:${!#}" >> "$FIXTURE/events" ;; 'ps -aq') echo leaked-test-container ;; 'rm --force') echo "cleanup:$*" >> "$FIXTURE/events" ;; @@ -137,6 +155,12 @@ func TestBuildTestPublishArtifact(t *testing.T) { if first < 0 || second <= first || strings.Contains(events, "publish:") { t.Fatalf("unexpected test order: %s", events) } + for _, platform := range []string{"linux/amd64", "linux/arm64"} { + user, hook := strings.Index(events, "user:"+platform), strings.Index(events, "hook:"+platform) + if user < 0 || hook <= user { + t.Fatalf("runtime user must be checked before each platform hook: %s", events) + } + } if !strings.Contains(events, "cleanup:rm --force --volumes leaked-test-container") { t.Fatalf("missing container cleanup: %s", events) } @@ -162,6 +186,9 @@ func TestFailedTestsNeverAuthorizeUpload(t *testing.T) { {"first platform", "FAIL_PLATFORM=linux/amd64", "exit status 23"}, {"second platform", "FAIL_PLATFORM=linux/arm64", "exit status 23"}, {"wrong loaded architecture", "WRONG_PLATFORM=true", "Loaded linux/386"}, + {"first platform user command", "FAIL_USER_PLATFORM=linux/amd64", "exit status 24"}, + {"second platform user command", "FAIL_USER_PLATFORM=linux/arm64", "exit status 24"}, + {"wrong runtime user", "RUNTIME_IDENTITY=1000:1000:tuxcreate:tuxcreate", "expected 1000:1000:tuxcrate:tuxcrate"}, {"modified during tests", "MUTATE_ARCHIVE=true", "changed during testing"}, } { t.Run(tt.name, func(t *testing.T) { @@ -237,6 +264,61 @@ func TestMissingHookCompatibility(t *testing.T) { if output, err := f.run(t, "test", f.archive, "linux/amd64,linux/arm64", "1.2.3"); err != nil || strings.Count(output, "No tests/smoke.sh") != 2 { t.Fatalf("%v\n%s", err, output) } + if events := f.events(t); strings.Count(events, "user:") != 2 || strings.Contains(events, "hook:") { + t.Fatalf("missing hooks must not skip runtime-user validation: %s", events) + } +} + +func TestRuntimeUserPolicy(t *testing.T) { + for _, tt := range []struct { + name, labels, identity string + wantError bool + }{ + {"default", "null", "1000:1000:tuxcrate:tuxcrate", false}, + {"explicit tuxcrate", `{"com.tuxcrate.runtime-user":"tuxcrate"}`, "1000:1000:tuxcrate:tuxcrate", false}, + {"declared root", `{"com.tuxcrate.runtime-user":"root"}`, "0:0:root:root", false}, + {"undeclared root", "null", "0:0:root:root", true}, + {"wrong uid", "null", "1001:1000:tuxcrate:tuxcrate", true}, + {"wrong gid", "null", "1000:1001:tuxcrate:tuxcrate", true}, + {"wrong username", "null", "1000:1000:app:tuxcrate", true}, + {"wrong group name", "null", "1000:1000:tuxcrate:app", true}, + {"root label with nonroot user", `{"com.tuxcrate.runtime-user":"root"}`, "1000:1000:tuxcrate:tuxcrate", true}, + {"unknown policy", `{"com.tuxcrate.runtime-user":"skip"}`, "0:0:root:root", true}, + {"invalid labels", "invalid JSON", "1000:1000:tuxcrate:tuxcrate", true}, + } { + t.Run(tt.name, func(t *testing.T) { + f := newFixture(t) + f.env = append(f.env, "IMAGE_LABELS="+tt.labels, "RUNTIME_IDENTITY="+tt.identity) + f.write(t, "image.tar.tested", "stale receipt") + output, err := f.run(t, "test", f.archive, "linux/amd64,linux/arm64", "1.2.3") + if (err != nil) != tt.wantError { + t.Fatalf("unexpected result: %v\n%s", err, output) + } + if tt.wantError { + if _, err := os.Stat(f.archive + ".tested"); !os.IsNotExist(err) { + t.Fatal("invalid runtime user retained a publication receipt") + } + if strings.Contains(f.events(t), "hook:") { + t.Fatal("invalid runtime user reached the application hook") + } + } + }) + } +} + +// Opt in with locally loaded images to exercise the real Docker runtime check. +func TestRuntimeUserDockerIntegration(t *testing.T) { + images := os.Getenv("TUXCRATE_RUNTIME_USER_TEST_IMAGES") + if images == "" { + t.Skip("set TUXCRATE_RUNTIME_USER_TEST_IMAGES to comma-separated local image references") + } + for _, image := range strings.Split(images, ",") { + t.Run(image, func(t *testing.T) { + if err := (app{os.Stdout, os.Stderr}).checkRuntimeUser(context.Background(), image, "linux/amd64", "runtime-user-integration"); err != nil { + t.Fatal(err) + } + }) + } } func TestWorkflowCannotUploadBeforeTestsAndMetadata(t *testing.T) { From 74cca1c55a30b6788e444c341775a990060d800c Mon Sep 17 00:00:00 2001 From: Niklas Wagner Date: Sun, 1 Nov 2026 16:47:57 +0100 Subject: [PATCH 2/4] refactor: use com.tuxcrate labels for test containers --- README.md | 3 +++ internal/containerartifact/artifact.go | 25 ++++++++++++++++----- internal/containerartifact/artifact_test.go | 15 +++++++++++-- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 25afec8..ecd6f45 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,9 @@ Images that require root must explicitly declare `0:0:root:root`; it does not skip user validation. The optional value `tuxcrate` declares the default explicitly; all other values fail validation. +Shared test containers use `com.tuxcrate.smoke-test`. Cleanup also recognizes +the previous `io.tuxcrate.smoke-test` prefix used by existing repository hooks. + Keep directory permissions, application startup, and API checks in repository smoke hooks. Deploy root-user declarations before enabling the shared validator, and deploy the validator before removing duplicate checks from callers. diff --git a/internal/containerartifact/artifact.go b/internal/containerartifact/artifact.go index 10de444..30de3a0 100644 --- a/internal/containerartifact/artifact.go +++ b/internal/containerartifact/artifact.go @@ -148,11 +148,24 @@ func (a app) test(ctx context.Context, archive, requested, version string) (err cleanup, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() for _, id := range testIDs { - containers, e := a.command(cleanup, true, "docker", "ps", "-aq", "--filter", "label=io.tuxcrate.smoke-test="+id) - if e != nil { - fmt.Fprintln(a.stderr, e) - } else if ids := strings.Fields(string(containers)); len(ids) > 0 { - a.cleanupCommand(cleanup, append([]string{"rm", "--force", "--volumes"}, ids...)...) + var containers []string + seen := make(map[string]bool) + // Existing repository hooks still use the previous label prefix. + for _, label := range []string{"com.tuxcrate.smoke-test", "io.tuxcrate.smoke-test"} { + output, e := a.command(cleanup, true, "docker", "ps", "-aq", "--filter", "label="+label+"="+id) + if e != nil { + fmt.Fprintln(a.stderr, e) + continue + } + for _, container := range strings.Fields(string(output)) { + if !seen[container] { + containers = append(containers, container) + seen[container] = true + } + } + } + if len(containers) > 0 { + a.cleanupCommand(cleanup, append([]string{"rm", "--force", "--volumes"}, containers...)...) } } for i, image := range images { @@ -276,7 +289,7 @@ func (a app) checkRuntimeUser(ctx context.Context, image, platform, id string) e } fmt.Fprintf(a.stdout, "Checking default runtime user for %s (expected %s).\n", platform, expected) actual, err := a.command(ctx, true, "docker", "run", "--rm", "--pull=never", "--platform", platform, - "--network", "none", "--label", "io.tuxcrate.smoke-test="+id, "--entrypoint", "/bin/sh", image, + "--network", "none", "--label", "com.tuxcrate.smoke-test="+id, "--entrypoint", "/bin/sh", image, "-eu", "-c", `printf '%s:%s:%s:%s\n' "$(id -u)" "$(id -g)" "$(id -un)" "$(id -gn)"`) if err != nil { return fmt.Errorf("runtime-user check for %s: %w", platform, err) diff --git a/internal/containerartifact/artifact_test.go b/internal/containerartifact/artifact_test.go index 55ad376..282ee82 100644 --- a/internal/containerartifact/artifact_test.go +++ b/internal/containerartifact/artifact_test.go @@ -86,7 +86,7 @@ case "$1 $2" in test "$6" = --network test "$7" = none test "$8" = --label - [[ $9 == io.tuxcrate.smoke-test=* ]] + [[ $9 == com.tuxcrate.smoke-test=* ]] test "${10}" = --entrypoint test "${11}" = /bin/sh [[ ${12} == tuxcrate-test:* ]] @@ -97,7 +97,10 @@ case "$1 $2" in printf '%s\n' "${RUNTIME_IDENTITY:-1000:1000:tuxcrate:tuxcrate}" ;; 'image rm') echo "remove:${!#}" >> "$FIXTURE/events" ;; - 'ps -aq') echo leaked-test-container ;; + 'ps -aq') + echo "query:$*" >> "$FIXTURE/events" + echo leaked-test-container + ;; 'rm --force') echo "cleanup:$*" >> "$FIXTURE/events" ;; *) echo "Unexpected docker command: $*" >&2; exit 1 ;; esac @@ -164,6 +167,14 @@ func TestBuildTestPublishArtifact(t *testing.T) { if !strings.Contains(events, "cleanup:rm --force --volumes leaked-test-container") { t.Fatalf("missing container cleanup: %s", events) } + for _, prefix := range []string{"com.tuxcrate.smoke-test", "io.tuxcrate.smoke-test"} { + if !strings.Contains(events, "query:ps -aq --filter label="+prefix+"=") { + t.Fatalf("missing current or legacy label cleanup: %s", events) + } + } + if strings.Contains(events, "leaked-test-container leaked-test-container") { + t.Fatalf("cleanup must deduplicate containers found under both prefixes: %s", events) + } output, err := os.ReadFile(filepath.Join(f.dir, "output")) if err != nil { t.Fatal(err) From f6ab072ae65509c695c0eef9f10725465fe12995 Mon Sep 17 00:00:00 2001 From: Niklas Wagner Date: Sun, 1 Nov 2026 16:52:23 +0100 Subject: [PATCH 3/4] refactor: use only com.tuxcrate smoke-test labels --- README.md | 4 ++-- internal/containerartifact/artifact.go | 23 +++++---------------- internal/containerartifact/artifact_test.go | 10 +++------ 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index ecd6f45..6755593 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ Images that require root must explicitly declare `0:0:root:root`; it does not skip user validation. The optional value `tuxcrate` declares the default explicitly; all other values fail validation. -Shared test containers use `com.tuxcrate.smoke-test`. Cleanup also recognizes -the previous `io.tuxcrate.smoke-test` prefix used by existing repository hooks. +Shared test containers and repository hooks use `com.tuxcrate.smoke-test`. +Cleanup uses only this label; there is no legacy-prefix fallback. Keep directory permissions, application startup, and API checks in repository smoke hooks. Deploy root-user declarations before enabling the shared validator, diff --git a/internal/containerartifact/artifact.go b/internal/containerartifact/artifact.go index 30de3a0..8dce9cd 100644 --- a/internal/containerartifact/artifact.go +++ b/internal/containerartifact/artifact.go @@ -148,24 +148,11 @@ func (a app) test(ctx context.Context, archive, requested, version string) (err cleanup, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() for _, id := range testIDs { - var containers []string - seen := make(map[string]bool) - // Existing repository hooks still use the previous label prefix. - for _, label := range []string{"com.tuxcrate.smoke-test", "io.tuxcrate.smoke-test"} { - output, e := a.command(cleanup, true, "docker", "ps", "-aq", "--filter", "label="+label+"="+id) - if e != nil { - fmt.Fprintln(a.stderr, e) - continue - } - for _, container := range strings.Fields(string(output)) { - if !seen[container] { - containers = append(containers, container) - seen[container] = true - } - } - } - if len(containers) > 0 { - a.cleanupCommand(cleanup, append([]string{"rm", "--force", "--volumes"}, containers...)...) + containers, e := a.command(cleanup, true, "docker", "ps", "-aq", "--filter", "label=com.tuxcrate.smoke-test="+id) + if e != nil { + fmt.Fprintln(a.stderr, e) + } else if ids := strings.Fields(string(containers)); len(ids) > 0 { + a.cleanupCommand(cleanup, append([]string{"rm", "--force", "--volumes"}, ids...)...) } } for i, image := range images { diff --git a/internal/containerartifact/artifact_test.go b/internal/containerartifact/artifact_test.go index 282ee82..b0fdccc 100644 --- a/internal/containerartifact/artifact_test.go +++ b/internal/containerartifact/artifact_test.go @@ -98,6 +98,7 @@ case "$1 $2" in ;; 'image rm') echo "remove:${!#}" >> "$FIXTURE/events" ;; 'ps -aq') + [[ $4 == label=com.tuxcrate.smoke-test=* ]] echo "query:$*" >> "$FIXTURE/events" echo leaked-test-container ;; @@ -167,13 +168,8 @@ func TestBuildTestPublishArtifact(t *testing.T) { if !strings.Contains(events, "cleanup:rm --force --volumes leaked-test-container") { t.Fatalf("missing container cleanup: %s", events) } - for _, prefix := range []string{"com.tuxcrate.smoke-test", "io.tuxcrate.smoke-test"} { - if !strings.Contains(events, "query:ps -aq --filter label="+prefix+"=") { - t.Fatalf("missing current or legacy label cleanup: %s", events) - } - } - if strings.Contains(events, "leaked-test-container leaked-test-container") { - t.Fatalf("cleanup must deduplicate containers found under both prefixes: %s", events) + if strings.Count(events, "query:ps -aq --filter label=com.tuxcrate.smoke-test=") != 2 { + t.Fatalf("expected one com.tuxcrate cleanup query per platform: %s", events) } output, err := os.ReadFile(filepath.Join(f.dir, "output")) if err != nil { From 2cd902255a2221fa1e64ce78cbb7991109be56b2 Mon Sep 17 00:00:00 2001 From: Niklas Wagner Date: Sun, 1 Nov 2026 17:00:08 +0100 Subject: [PATCH 4/4] docs: keep runtime-user documentation in the docs repository --- README.md | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/README.md b/README.md index 6755593..42f3dec 100644 --- a/README.md +++ b/README.md @@ -3,23 +3,3 @@ Shared container build and release workflows for Tuxcrate. [Documentation](https://github.com/tuxcrate/docs/blob/main/docs/maintainers/container-releases.md) - -## Shared runtime-user validation - -Before running a repository's `tests/smoke.sh`, `container-artifact test` checks -every platform image with its default user, no network, and `/bin/sh` as the -entrypoint. Images must provide `/bin/sh` and `id`. The default requirement is -user and group `tuxcrate`, both with numeric ID 1000. The check also runs when -there is no repository smoke hook. - -Images that require root must explicitly declare -`LABEL com.tuxcrate.runtime-user="root"` and document why. This checks for -`0:0:root:root`; it does not skip user validation. The optional value `tuxcrate` -declares the default explicitly; all other values fail validation. - -Shared test containers and repository hooks use `com.tuxcrate.smoke-test`. -Cleanup uses only this label; there is no legacy-prefix fallback. - -Keep directory permissions, application startup, and API checks in repository -smoke hooks. Deploy root-user declarations before enabling the shared validator, -and deploy the validator before removing duplicate checks from callers.