Files
Codeman/test/agent-image-build-args-parity.test.ts
T
DevvynandClaude Opus 5.5 5cf5a45438 feat(docker): opt-in gh + az CLIs with git credential helpers for private repos
Add Case -> Clone Repo could only reach public repositories in the Docker
deployment. This lets a deployment opt in to the GitHub CLI and the Azure
CLI (+ azure-devops extension) as git credential helpers. Codeman itself
still collects no credentials.

- server.Dockerfile / agent.Dockerfile: CODEMAN_INSTALL_GH /
  CODEMAN_INSTALL_AZ build args (0 or 1, default 0; anything else stops the
  build). Off leaves no apt repository, package, extension, helper script
  or credential entry, so a default build is unchanged. On installs from
  the vendors' apt repositories and configures system gitconfig helpers:
  github.com / gist.github.com -> `gh auth git-credential`, dev.azure.com /
  *.visualstudio.com -> new docker/git-credential-azure-cli (an Entra ID
  token from `az account get-access-token`, or AZURE_DEVOPS_EXT_PAT).
  A helper whose CLI is not signed in prints nothing, so a private clone
  still fails fast.
- The extension lives in AZURE_EXTENSION_DIR outside HOME
  (/opt/codeman-az-extensions, runtime-owned; /opt/az-extensions, gid-0
  group-writable in the agent image).
- Hosts turn them on in docker-compose.override.yml: `build: args:` for the
  server image, `environment:` CODEMAN_AGENT_IMAGE_INSTALL_GH / _AZ for the
  agent image. build-agent-image.mjs and the in-app auto-build share one
  env -> ARG table (pinned by the parity test) and pass nothing when unset.
  docker-compose.yaml is untouched; .env.example only gains a comment, so
  the self-updater's environment gate sees no new keys.
- Docker cases seed the gh sign-in (~/.config/gh/hosts.yml, config.yml) and
  the az sign-in files from ~/.azure per file, read-only, like pi/grok.
- The Clone Repo AUTH_REQUIRED message says how to sign the server's git
  in instead of claiming private repositories cannot be cloned.
- Docs: docker/README.md "Private repositories", docker-compose.md,
  docker-cases.md, the Quick-Start / Core-Concepts / Docker-Cases wiki
  pages, security-architecture.md, architecture-invariants.md, changeset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0167CiuzLrmjYWxwKp3rMWjw
2026-09-23 08:57:09 +08:00

148 lines
6.8 KiB
TypeScript

/**
* @fileoverview The two producers of the agent-image `docker build` command line must agree.
*
* There are two, and there have to be: `scripts/build-agent-image.mjs` is what a human runs
* and is a `.mjs`, so it cannot import the TypeScript registry and reads the generated
* `config/clis.stock.json` instead; `src/docker-hosts.ts` builds the same command for the
* in-app auto-build on the first Docker case, from `STOCK_CLIS` directly.
*
* Two independent producers of one command line is exactly the shape that drifts, and the
* failure would be quiet and confusing: an image built by hand and an image built by the app
* would hold different CLIs under the SAME `codeman/agent:base` tag, so which CLIs a container
* has would depend on who built it.
*
* Port: none (pure).
*/
import { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import {
agentImageBuildArgPairs as mjsPairs,
agentImageNpmPackages as mjsPackages,
GIT_HOST_CLI_BUILD_ARGS as mjsGitHostArgs,
gitHostCliBuildArgPairs as mjsGitHostPairs,
} from '../scripts/lib/cli-catalog.mjs';
import {
agentImageBuildArgPairs as tsPairs,
agentImageBuildArgs,
agentImageNpmPackages as tsPackages,
GIT_HOST_CLI_BUILD_ARGS as tsGitHostArgs,
gitHostCliBuildArgPairs as tsGitHostPairs,
} from '../src/docker-hosts.js';
const CATALOG = JSON.parse(readFileSync(fileURLToPath(new URL('../config/clis.stock.json', import.meta.url)), 'utf-8'));
describe('agent-image build args: the .mjs and the TS mirror agree', () => {
it('resolve the same npm package list, in the same order', () => {
// Order matters as well as membership: a different order is a different RUN string, hence
// a different layer hash, hence a cache miss between the two build paths.
expect(tsPackages()).toEqual(mjsPackages(CATALOG));
});
it('produce the same --build-arg pairs', () => {
expect(tsPairs()).toEqual(mjsPairs(CATALOG));
});
it('render the same argv', () => {
// What the .mjs assembles by hand around its pairs, spelled out here so a change to
// either side's argv SHAPE (not just its values) fails too.
const pairs = tsPairs();
const expected = [
'build',
'-f',
'/repo/docker/agent.Dockerfile',
'-t',
'codeman/agent:base',
'--no-cache',
...pairs.flatMap(([name, value]) => ['--build-arg', `${name}=${value}`]),
'/repo',
];
expect(agentImageBuildArgs('/repo/docker/agent.Dockerfile', 'codeman/agent:base', '/repo', true, pairs)).toEqual(
expected
);
});
it('keeps --build-arg out of the argv when nothing is passed', () => {
// The parameter defaults to empty, so an existing caller that has not been updated still
// produces exactly the command it produced before.
expect(agentImageBuildArgs('/d', 'i', '/c')).toEqual(['build', '-f', '/d', '-t', 'i', '/c']);
});
it('resolves a non-empty list (anti-vacuity)', () => {
// Two empty lists compare equal very happily.
expect(tsPackages().length).toBeGreaterThan(3);
expect(tsPairs()[0][1].length).toBeGreaterThan(20);
});
it('matches the Dockerfile ARG default, so a bare `docker build` is cache-identical', () => {
const dockerfile = readFileSync(fileURLToPath(new URL('../docker/agent.Dockerfile', import.meta.url)), 'utf-8');
const declared = /^ARG CLI_NPM_PACKAGES="([^"]*)"$/m.exec(dockerfile)?.[1];
expect(declared, 'the Dockerfile no longer declares CLI_NPM_PACKAGES').toBeDefined();
expect(declared).toBe(tsPackages().join(' '));
});
it('validates an unsafe package name with the SAME regex on both sides', () => {
// Equal OUTPUT on today's catalogue (asserted above) does not prove equal VALIDATION — a
// looser regex on one side would only show up the day someone ships a hostile package name.
// The regex is duplicated rather than shared (the .mjs side cannot import the .ts side, the
// whole reason this file exists), so pin the literal PATTERN text is identical between the
// two source files rather than trusting the comment that says so.
const tsSource = readFileSync(fileURLToPath(new URL('../src/docker-hosts.ts', import.meta.url)), 'utf-8');
const mjsSource = readFileSync(fileURLToPath(new URL('../scripts/lib/cli-catalog.mjs', import.meta.url)), 'utf-8');
const extract = (source: string, file: string): string => {
// Non-greedy to `/;` deliberately: the pattern itself contains a `/` (inside the
// character class), so a naive `[^/]+` stops at the wrong slash.
const m = /const SAFE_PACKAGE = (\/.+?\/);/.exec(source);
expect(m, `could not find the SAFE_PACKAGE regex literal in ${file}`).toBeDefined();
return m![1];
};
expect(extract(tsSource, 'docker-hosts.ts')).toBe(extract(mjsSource, 'cli-catalog.mjs'));
});
});
describe('optional gh / az in the agent image: both producers pass the same switches', () => {
const ENV_GH = 'CODEMAN_AGENT_IMAGE_INSTALL_GH';
const ENV_AZ = 'CODEMAN_AGENT_IMAGE_INSTALL_AZ';
it('map the same environment variables to the same Dockerfile ARGs', () => {
expect(tsGitHostArgs).toEqual(mjsGitHostArgs);
expect(tsGitHostArgs.map(([, arg]) => arg)).toEqual(['CODEMAN_INSTALL_GH', 'CODEMAN_INSTALL_AZ']);
});
it('agree for every combination, and an unset or empty variable adds nothing', () => {
for (const gh of [undefined, '', '0', '1']) {
for (const az of [undefined, '', '0', '1']) {
const env: NodeJS.ProcessEnv = {};
if (gh !== undefined) env[ENV_GH] = gh;
if (az !== undefined) env[ENV_AZ] = az;
const expected: Array<[string, string]> = [];
if (gh) expected.push(['CODEMAN_INSTALL_GH', gh]);
if (az) expected.push(['CODEMAN_INSTALL_AZ', az]);
expect(tsGitHostPairs(env)).toEqual(expected);
expect(mjsGitHostPairs(env)).toEqual(expected);
expect(tsPairs(env)).toEqual(mjsPairs(CATALOG, env));
}
}
});
it('keeps the default argv unchanged when neither variable is set', () => {
expect(tsPairs({})).toEqual([['CLI_NPM_PACKAGES', tsPackages().join(' ')]]);
});
it('refuses anything but 0 or 1 on both sides, naming the variable', () => {
for (const bad of ['yes', 'true', '2', ' 1', '0 && echo']) {
expect(() => tsGitHostPairs({ [ENV_AZ]: bad })).toThrow(new RegExp(ENV_AZ));
expect(() => mjsGitHostPairs({ [ENV_AZ]: bad })).toThrow(new RegExp(ENV_AZ));
}
});
it('both Dockerfiles declare the switches, defaulting to OFF (opt-in)', () => {
for (const file of ['../docker/agent.Dockerfile', '../docker/server.Dockerfile']) {
const dockerfile = readFileSync(fileURLToPath(new URL(file, import.meta.url)), 'utf-8');
expect(dockerfile, file).toMatch(/^ARG CODEMAN_INSTALL_GH=0$/m);
expect(dockerfile, file).toMatch(/^ARG CODEMAN_INSTALL_AZ=0$/m);
}
});
});