diff --git a/backend/test/scripts/container-user.test.ts b/backend/test/scripts/container-user.test.ts index 18f77504..76f5c789 100644 --- a/backend/test/scripts/container-user.test.ts +++ b/backend/test/scripts/container-user.test.ts @@ -107,43 +107,35 @@ describe('resolve_target_ids', () => { describe('align_container_user', () => { it('is a no-op when ids already match', () => { - const res = runScript( - 'align_container_user node; echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"', - { OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1000', PGID: '1000' }, - ) + const res = runScript('align_container_user node', { + OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1000', PGID: '1000', + }) expect(res.status).toBe(0) expect(stubCalls()).toEqual([]) - expect(res.stdout).toContain('uidChanged=0 gidChanged=0') }) - it('aligns both ids and records the change, group before user', () => { - const res = runScript( - 'align_container_user node; echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"', - { OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1001', PGID: '1002' }, - ) + it('aligns both ids, group before user', () => { + const res = runScript('align_container_user node', { + OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1001', PGID: '1002', + }) expect(res.status).toBe(0) expect(stubCalls()).toEqual(['groupmod -g 1002 node', 'usermod -u 1001 node']) - expect(res.stdout).toContain('uidChanged=1 gidChanged=1') }) it('aligns only the gid when the uid already matches', () => { - const res = runScript( - 'align_container_user node; echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"', - { OCM_STUB_NODE_UID: '1001', PUID: '1001', PGID: '1002' }, - ) + const res = runScript('align_container_user node', { + OCM_STUB_NODE_UID: '1001', PUID: '1001', PGID: '1002', + }) expect(res.status).toBe(0) expect(stubCalls()).toEqual(['groupmod -g 1002 node']) - expect(res.stdout).toContain('uidChanged=0 gidChanged=1') }) it('aligns only the uid when the gid already matches', () => { - const res = runScript( - 'align_container_user node; echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"', - { OCM_STUB_NODE_GID: '1002', PUID: '1001', PGID: '1002' }, - ) + const res = runScript('align_container_user node', { + OCM_STUB_NODE_GID: '1002', PUID: '1001', PGID: '1002', + }) expect(res.status).toBe(0) expect(stubCalls()).toEqual(['usermod -u 1001 node']) - expect(res.stdout).toContain('uidChanged=1 gidChanged=0') }) it('defaults the account name to node', () => { @@ -165,26 +157,24 @@ describe('align_container_user', () => { expect(res.stdout).toMatch(/Aligning node user to uid 1001/) }) - it('propagates groupmod failure without setting the gid change flag', () => { + it('propagates groupmod failure', () => { writeStub('groupmod', `echo "groupmod $*" >> "$OCM_STUB_LOG"\nexit 1`) - const res = runScript( - 'align_container_user node || echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"', - { OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1001', PGID: '1002' }, - ) + const res = runScript('align_container_user node || echo "failed"', { + OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1001', PGID: '1002', + }) expect(res.status).toBe(0) expect(stubCalls()).toEqual(['groupmod -g 1002 node']) - expect(res.stdout).toContain('uidChanged=0 gidChanged=0') + expect(res.stdout).toContain('failed') }) - it('propagates usermod failure without setting the uid change flag', () => { + it('propagates usermod failure', () => { writeStub('usermod', `echo "usermod $*" >> "$OCM_STUB_LOG"\nexit 1`) - const res = runScript( - 'align_container_user node || echo "uidChanged=$OCM_UID_CHANGED gidChanged=$OCM_GID_CHANGED"', - { OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1001', PGID: '1002' }, - ) + const res = runScript('align_container_user node || echo "failed"', { + OCM_STUB_NODE_UID: '1000', OCM_STUB_NODE_GID: '1000', PUID: '1001', PGID: '1002', + }) expect(res.status).toBe(0) expect(stubCalls()).toEqual(['groupmod -g 1002 node', 'usermod -u 1001 node']) - expect(res.stdout).toContain('uidChanged=0 gidChanged=1') + expect(res.stdout).toContain('failed') }) }) diff --git a/backend/test/scripts/docker-config.test.ts b/backend/test/scripts/docker-config.test.ts index c489694a..c0a8a29f 100644 --- a/backend/test/scripts/docker-config.test.ts +++ b/backend/test/scripts/docker-config.test.ts @@ -49,23 +49,14 @@ describe('entrypoint library wiring', () => { expect(warnIndex).toBeLessThan(chownIndex) }) - it('realigns /app only when ids changed', () => { + it('does not re-chown /app when ids change', () => { const entrypoint = read(entrypointPath) - expect(entrypoint).toContain('OCM_UID_CHANGED') - expect(entrypoint).toContain('OCM_GID_CHANGED') - - const conditionalBlockMatch = entrypoint.match( - /if \[ "\$OCM_UID_CHANGED" = "1" \] \|\| \[ "\$OCM_GID_CHANGED" = "1" \]; then[\s\S]*?chown -R node:node \/app\nfi/, - ) - expect(conditionalBlockMatch, 'conditional /app realign block must exist').not.toBeNull() - - const workspaceChownMatch = entrypoint.match(/chown -R node:node [^\n]*\/workspace/) - expect(workspaceChownMatch, 'pre-existing unconditional workspace chown must remain').not.toBeNull() - const conditionalChownIndex = entrypoint.indexOf('chown -R node:node /app\n', conditionalBlockMatch!.index!) - const workspaceChownIndex = entrypoint.indexOf(workspaceChownMatch![0]) - expect(conditionalChownIndex).toBeGreaterThan(workspaceChownIndex) + expect(entrypoint).not.toContain('OCM_UID_CHANGED') + expect(entrypoint).not.toContain('OCM_GID_CHANGED') + expect(entrypoint).not.toMatch(/chown -R node:node \/app(?:\s|\n|$)/) + expect(entrypoint).toContain('chown -R node:node /app/data /workspace /home/node') expect(entrypoint).toContain('mkdir -p /app/data /workspace /home/node/.cache /home/node/.opencode') }) diff --git a/docs/configuration/docker.md b/docs/configuration/docker.md index e64bf1a0..a7a504b3 100644 --- a/docs/configuration/docker.md +++ b/docs/configuration/docker.md @@ -243,7 +243,7 @@ The quoted `""` keeps paths containing spaces (for example `/Users/na Editing a repository from the host while an agent works in the same repository or worktree can collide on `index.lock` and branch state. !!! note "/app keeps the build-time uid" - `/app` and its `node_modules` are chowned to uid 1000 at build time. When `PUID` differs, the entrypoint re-chowns `/app` on startup, which costs one extra recursive walk per container start. + `/app` and its `node_modules` are chowned to uid 1000 at build time and are read-only at runtime. When `PUID` differs, the container runs the code as a different uid, but since `/app` is only read (never written) at runtime, it is left untouched and startup stays fast. ### Data diff --git a/scripts/docker-entrypoint.sh b/scripts/docker-entrypoint.sh index e54ff777..ce4d7ad2 100644 --- a/scripts/docker-entrypoint.sh +++ b/scripts/docker-entrypoint.sh @@ -97,9 +97,4 @@ warn_if_workspace_owner_differs /workspace "$OCM_TARGET_UID" "$OCM_TARGET_GID" mkdir -p /app/data /workspace /home/node/.cache /home/node/.opencode chown -R node:node /app/data /workspace /home/node -if [ "$OCM_UID_CHANGED" = "1" ] || [ "$OCM_GID_CHANGED" = "1" ]; then - echo "Realigning /app ownership after id change" - chown -R node:node /app -fi - exec runuser -u node -- "$@" diff --git a/scripts/lib/container-user.sh b/scripts/lib/container-user.sh index 1acf6293..1306f18f 100644 --- a/scripts/lib/container-user.sh +++ b/scripts/lib/container-user.sh @@ -2,8 +2,6 @@ OCM_TARGET_UID="" OCM_TARGET_GID="" -OCM_UID_CHANGED=0 -OCM_GID_CHANGED=0 resolve_target_ids() { OCM_TARGET_UID="${PUID:-1000}" @@ -49,7 +47,6 @@ align_group_id() { echo "Aligning $account group to gid $target_gid" groupmod -g "$target_gid" "$account" || return 1 - OCM_GID_CHANGED=1 } align_user_id() { @@ -69,7 +66,6 @@ align_user_id() { echo "Aligning $account user to uid $target_uid" usermod -u "$target_uid" "$account" || return 1 - OCM_UID_CHANGED=1 } align_container_user() {