From 0bee9df5f6e0579db564701574341536a5dbce0f Mon Sep 17 00:00:00 2001 From: David Julian Albers Date: Thu, 3 Dec 2026 03:07:51 +0100 Subject: [PATCH 1/8] PLX-248: tests for plx-ci setup in a job handed no token for the vault Red: a repo job with no token fails today with "the job cannot read the vault" where it should read nothing, say so in one line, and go on. Co-Authored-By: Claude Sonnet --- packages/cli-ci/src/commands/setup.test.ts | 4 +- .../src/commands/setupWithoutToken.test.ts | 72 +++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 packages/cli-ci/src/commands/setupWithoutToken.test.ts diff --git a/packages/cli-ci/src/commands/setup.test.ts b/packages/cli-ci/src/commands/setup.test.ts index eb5b2a3c..610923f7 100644 --- a/packages/cli-ci/src/commands/setup.test.ts +++ b/packages/cli-ci/src/commands/setup.test.ts @@ -53,8 +53,8 @@ test('a Connect server’s address without its token is no Connect server', () = assert.deepEqual(credentialsOf(job.calls), [['OP_SERVICE_ACCOUNT_TOKEN']]); }); -test('with neither, the job fails in one line that names both, and nothing is read', () => { - const job = runJob(['repo'], { vault: VAULT }); +test('with neither, a deploy job fails in one line that names both, and nothing is read', () => { + const job = runJob(['deploy'], { vault: VAULT }); assert.equal(job.status, 1, job.log); assert.deepEqual(credentialsOf(job.calls), []); assert.match(job.log, /OP_CONNECT_HOST and OP_CONNECT_TOKEN/); diff --git a/packages/cli-ci/src/commands/setupWithoutToken.test.ts b/packages/cli-ci/src/commands/setupWithoutToken.test.ts new file mode 100644 index 00000000..cea01e74 --- /dev/null +++ b/packages/cli-ci/src/commands/setupWithoutToken.test.ts @@ -0,0 +1,72 @@ +// plx-ci setup in a job that is handed no token for the vault, as the job of a pull request from a fork is (the +// forge gives it no secrets), and neither a Connect server on the runner: it reads nothing from the vault, says so +// in one line, and goes on with what needs none (§ 8.4 of the project's decisions). +import assert from 'node:assert/strict'; +import { join } from 'node:path'; +import { test } from 'node:test'; +import { runJob } from '../testing/job.ts'; + +const FORGE_RUNNER = { RUNNER_ENVIRONMENT: 'github-hosted' }; +const CACHE_HOST = { PLX_TURBO_CACHE_HOST: 'cache.acme.internal' }; +const VAULT = { 'op://acme-ci/repo_web/server-actions-key': 'a-key' }; +/** A pull request from a fork: a forge runner, the repo's cache host, the overlay, and no secret. */ +const ON_PUSH = { GITHUB_WORKFLOW: 'on_push', GITHUB_REF_NAME: 'main', GITHUB_EVENT_NAME: 'push' }; +const FORK = { ...FORGE_RUNNER, ...CACHE_HOST, GITHUB_WORKFLOW: 'on_pr', GITHUB_EVENT_NAME: 'pull_request' }; + +/** The one line the step says about it. */ +const SAYS = /^no token for the vault: /m; + +test('a repo job with no token reads nothing, and says so in one line', () => { + const job = runJob(['repo', '--overlay', 'netbird'], { env: FORK, vault: VAULT }); + assert.equal(job.status, 0, job.log); + assert.deepEqual( + job.calls.filter((c) => c.tool === 'op'), + [], + ); + assert.equal(job.log.match(new RegExp(SAYS, 'gm'))?.length, 1, job.log); + assert.match(job.log, /without the server-actions key and without the remote cache/); + assert.ok(!/with the service account|Connect server/.test(job.log), 'it does not claim to have read the vault'); +}); + +test('a job with no token hands no key and no cache token, and joins no overlay', () => { + const job = runJob(['repo', '--overlay', 'netbird'], { env: FORK, vault: VAULT }); + assert.equal(job.status, 0, job.log); + assert.deepEqual(job.output, {}); + assert.ok(!job.log.includes('::add-mask::'), 'there is nothing to mask'); + assert.ok(!job.calls.some((c) => ['netbird', 'apt-get', 'curl'].includes(c.tool)), 'no overlay, no download'); + for (const name of Object.keys(job.env)) assert.ok(!/TOKEN/.test(name), name); +}); + +test('a job with no token is still logged in to the forge’s registries with the job’s own token, which needs no vault', () => { + const job = runJob(['repo', '--private-packages'], { env: FORK, vault: VAULT }); + assert.equal(job.status, 0, job.log); + assert.equal(job.env.DOCKER_CONFIG, join(job.temp, 'docker')); + assert.equal(job.env.NPM_CONFIG_USERCONFIG, join(job.temp, 'npm', 'npmrc')); +}); + +test('a repo job of the workflow on_commit or on_pr reads nothing where it has no token, and one of on_push fails', () => { + for (const GITHUB_WORKFLOW of ['on_commit', 'on_pr']) { + const job = runJob(['repo'], { env: { GITHUB_WORKFLOW }, vault: VAULT }); + assert.equal(job.status, 0, `${GITHUB_WORKFLOW}\n${job.log}`); + } + const job = runJob(['repo'], { env: ON_PUSH, vault: VAULT }); + assert.equal(job.status, 1, job.log); + assert.match(job.log, /on_push .*vault/); + assert.match(job.log, /OP_SERVICE_ACCOUNT_TOKEN/); + assert.deepEqual(job.output, {}); +}); + +test('a deploy job with no token fails, since it cannot deploy', () => { + const job = runJob(['deploy'], { env: { ...FORGE_RUNNER, ...ON_PUSH }, vault: VAULT }); + assert.equal(job.status, 1, job.log); + assert.match(job.log, /OP_CONNECT_HOST and OP_CONNECT_TOKEN/); + assert.match(job.log, /OP_SERVICE_ACCOUNT_TOKEN/); + assert.ok(!SAYS.test(job.log), 'it does not go on as a job that deploys nothing'); +}); + +test('a job with a token reads as before and says nothing of the kind', () => { + const job = runJob(['repo'], { env: { OP_SERVICE_ACCOUNT_TOKEN: 'ops_x', GITHUB_WORKFLOW: 'on_pr' }, vault: VAULT }); + assert.equal(job.status, 0, job.log); + assert.ok(!SAYS.test(job.log), job.log); + assert.equal(job.output['server-actions-key'], 'a-key'); +}); From 06cb8d62cb7b5383176f16557df8a58f223a16e3 Mon Sep 17 00:00:00 2001 From: David Julian Albers Date: Thu, 3 Dec 2026 03:09:04 +0100 Subject: [PATCH 2/8] PLX-248: plx-ci setup reads nothing in a job handed no token for the vault A repo job with no credential for the vault, as a pull request from a fork is, says so in one line and goes on with the logins, which need none. A deploy job and a run of on_push still fail, naming the credentials missing. Co-Authored-By: Claude Sonnet --- .../src/commands/setupWithoutToken.test.ts | 2 +- packages/cli-ci/src/core/setup.ts | 47 +++++++++++++++---- packages/cli-ci/src/core/vault.ts | 14 ++++-- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/packages/cli-ci/src/commands/setupWithoutToken.test.ts b/packages/cli-ci/src/commands/setupWithoutToken.test.ts index cea01e74..747bcfa6 100644 --- a/packages/cli-ci/src/commands/setupWithoutToken.test.ts +++ b/packages/cli-ci/src/commands/setupWithoutToken.test.ts @@ -32,7 +32,7 @@ test('a job with no token hands no key and no cache token, and joins no overlay' const job = runJob(['repo', '--overlay', 'netbird'], { env: FORK, vault: VAULT }); assert.equal(job.status, 0, job.log); assert.deepEqual(job.output, {}); - assert.ok(!job.log.includes('::add-mask::'), 'there is nothing to mask'); + assert.equal(job.log.match(/::add-mask::/g)?.length, 1, 'the registry login’s alone is masked'); assert.ok(!job.calls.some((c) => ['netbird', 'apt-get', 'curl'].includes(c.tool)), 'no overlay, no download'); for (const name of Object.keys(job.env)) assert.ok(!/TOKEN/.test(name), name); }); diff --git a/packages/cli-ci/src/core/setup.ts b/packages/cli-ci/src/core/setup.ts index 855a72ab..f99cba12 100644 --- a/packages/cli-ci/src/core/setup.ts +++ b/packages/cli-ci/src/core/setup.ts @@ -13,7 +13,7 @@ import { write, } from '@plexus-ms/lib'; import { need, ownerOf } from '../commands/shared.ts'; -import { mask, vaultOf } from './vault.ts'; +import { mask, type Vault, vaultAccess, vaultOf } from './vault.ts'; export const JOBS = ['repo', 'deploy'] as const; export type Job = (typeof JOBS)[number]; @@ -61,6 +61,31 @@ function refuseOnPushWithoutEnvironment(env: NodeJS.ProcessEnv): void { ); } +/** + * The vault this job reads, or undefined where it reads none: a repo job handed + * no credential for it, as the job of a pull request from a fork is, since the + * forge gives such a job no secrets. It says so in one line and goes on with + * what needs none: the logins are the job's own token's, and without the key + * and the cache tokens the verbs run with a key of Next's own making and + * without the remote cache. A deploy job cannot deploy without the vault, and + * a run of on_push publishes images of the repo's one key (§ 3.8): both fail + * here, in a line that names the credentials missing. + */ +function vaultFor(ctx: Context, options: SetupOptions, env: NodeJS.ProcessEnv): Vault | undefined { + if (vaultAccess(env) || options.job === 'deploy') return vaultOf(ctx, env); + if (env.GITHUB_WORKFLOW === 'on_push') { + try { + vaultOf(ctx, env); + } catch (error) { + throw new Error(`on_push publishes the repo’s images and cannot run without the vault; ${detailOf(error)}`); + } + } + ctx.say( + 'no token for the vault: reading nothing from it; the job runs without the server-actions key and without the remote cache', + ); + return undefined; +} + export async function setup(ctx: Context, options: SetupOptions, env: NodeJS.ProcessEnv = process.env): Promise { refuseOnPushWithoutEnvironment(env); const repository = need('GITHUB_REPOSITORY', env); @@ -68,7 +93,7 @@ export async function setup(ctx: Context, options: SetupOptions, env: NodeJS.Pro const repo = repository.split('/')[1] ?? repository; const vaultName = `${owner}-ci`; const temp = need('RUNNER_TEMP', env); - const vault = vaultOf(ctx, env); + const vault = vaultFor(ctx, options, env); const outputs: Record = {}; const variables: Record = {}; const plan: Plan = []; @@ -79,11 +104,11 @@ export async function setup(ctx: Context, options: SetupOptions, env: NodeJS.Pro env.RUNNER_ENVIRONMENT === 'github-hosted' && options.overlay === 'netbird' && (options.job === 'deploy' || cacheHost !== undefined); - const setupKey = overlay ? await vault.read(`op://${vaultName}/netbird/setup-key`) : undefined; + const setupKey = overlay && vault ? await vault.read(`op://${vaultName}/netbird/setup-key`) : undefined; if (options.job === 'repo') { - outputs['server-actions-key'] = await vault.read(`op://${vaultName}/repo_${repo}/server-actions-key`); - if (overlay) { + if (vault) outputs['server-actions-key'] = await vault.read(`op://${vaultName}/repo_${repo}/server-actions-key`); + if (overlay && vault) { variables.PLX_TURBO_SHARED_TOKEN = await vault.read(`op://${vaultName}/turbo-cache/shared-token`); variables.PLX_TURBO_TRUSTED_TOKEN = await vault.read(`op://${vaultName}/turbo-cache/trusted-token`); } @@ -100,6 +125,7 @@ export async function setup(ctx: Context, options: SetupOptions, env: NodeJS.Pro variables.NPM_CONFIG_USERCONFIG = npmrc; } } else { + if (!vault) throw new Error('a deploy job reads the vault'); const sshKey = keyLines(ctx, await vault.read(`op://${vaultName}/deploy-ssh/private key?ssh-format=openssh`)); const clientId = await vault.read(`op://${vaultName}/github-app/client-id`, { secret: false }); const appKey = keyLines(ctx, await vault.read(`op://${vaultName}/github-app/private-key`)); @@ -121,11 +147,12 @@ export async function setup(ctx: Context, options: SetupOptions, env: NodeJS.Pro variables.SSH_AUTH_SOCK = socket; } - ctx.say( - vault.access === 'connect' - ? `the vault ${vaultName}, through the runner’s Connect server` - : `the vault ${vaultName}, with the service account`, - ); + if (vault) + ctx.say( + vault.access === 'connect' + ? `the vault ${vaultName}, through the runner’s Connect server` + : `the vault ${vaultName}, with the service account`, + ); await ctx.apply(plan); if (setupKey) { try { diff --git a/packages/cli-ci/src/core/vault.ts b/packages/cli-ci/src/core/vault.ts index 7560985d..6197708e 100644 --- a/packages/cli-ci/src/core/vault.ts +++ b/packages/cli-ci/src/core/vault.ts @@ -33,18 +33,24 @@ export function mask(ctx: Context, value: string): void { for (const line of value.split('\n').map((l) => l.trim())) if (line) ctx.io.stdout.write(`::add-mask::${line}\n`); } +/** How this runner reaches the vault, or undefined where it carries no credential for it, which only a job without secrets does. */ +export function vaultAccess(env: NodeJS.ProcessEnv = process.env): VaultAccess | undefined { + if (CONNECT.every((name) => env[name])) return 'connect'; + return env.OP_SERVICE_ACCOUNT_TOKEN ? 'service account' : undefined; +} + /** The vault as this runner reaches it; an error naming both ways where it reaches it neither way. */ export function vaultOf(ctx: Context, env: NodeJS.ProcessEnv = process.env): Vault { - const connect = CONNECT.every((name) => env[name]); - if (!connect && !env.OP_SERVICE_ACCOUNT_TOKEN) + const access = vaultAccess(env); + if (!access) throw new Error( 'the job cannot read the vault: the runner carries no Connect server (OP_CONNECT_HOST and OP_CONNECT_TOKEN), and the step was handed no OP_SERVICE_ACCOUNT_TOKEN', ); const opEnv = { ...env }; - if (connect) delete opEnv.OP_SERVICE_ACCOUNT_TOKEN; + if (access === 'connect') delete opEnv.OP_SERVICE_ACCOUNT_TOKEN; else for (const name of CONNECT) delete opEnv[name]; return { - access: connect ? 'connect' : 'service account', + access, read: async (ref, { secret = true } = {}) => { const value = (await ctx.read('op', ['read', ref], { env: opEnv })).replace(/\n$/, ''); if (secret) mask(ctx, value); From 432a151f061629c3eea2e55a5f1b5cc69fe43be6 Mon Sep 17 00:00:00 2001 From: David Julian Albers Date: Thu, 3 Dec 2026 03:10:17 +0100 Subject: [PATCH 3/8] PLX-248: tests for plx-ci setup on a runner without op Red: with a token and no op on the PATH, the first read of the vault fails with exit 127 ("op: command not found") where the step should install the pinned version, verify it, and read with it. core/op.ts is a stub that installs nothing; the fakes learn the download, unzip and gpg. Co-Authored-By: Claude Sonnet --- packages/cli-ci/src/commands/setupOp.test.ts | 144 +++++++++++++++++++ packages/cli-ci/src/core/op.ts | 9 ++ packages/cli-ci/src/testing/job.ts | 61 +++++++- 3 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 packages/cli-ci/src/commands/setupOp.test.ts create mode 100644 packages/cli-ci/src/core/op.ts diff --git a/packages/cli-ci/src/commands/setupOp.test.ts b/packages/cli-ci/src/commands/setupOp.test.ts new file mode 100644 index 00000000..7fe240b0 --- /dev/null +++ b/packages/cli-ci/src/commands/setupOp.test.ts @@ -0,0 +1,144 @@ +// plx-ci setup on a runner that has no `op`: the forge's own runners carry none. A job with a token installs the +// pinned version from 1Password's download, verified against its signature, into the job's own temporary directory, +// and reads as before; a runner that has `op` is left as it is (§ 8.4 of the project's decisions). The fakes of +// testing/job.ts stand in for the download, gpg and `op`. +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { test } from 'node:test'; +import { OP_SIGNING_KEY, OP_VERSION } from '../core/op.ts'; +import { type Call, runJob } from '../testing/job.ts'; + +const KEY_REF = 'op://acme-ci/repo_web/server-actions-key'; +const VAULT = { [KEY_REF]: 'a-key' }; +const SERVICE_ACCOUNT = { OP_SERVICE_ACCOUNT_TOKEN: 'ops_service-account' }; +const ARCH = ({ x64: 'amd64', arm64: 'arm64' } as Record)[process.arch]; +const ZIP = `https://cache.agilebits.com/dist/1P/op2/pkg/v${OP_VERSION}/op_linux_${ARCH}_v${OP_VERSION}.zip`; +const KEY = 'https://downloads.1password.com/linux/keys/1password.asc'; + +const tool = (calls: Call[], name: string) => calls.filter((c) => c.tool === name); +const downloads = (calls: Call[]) => tool(calls, 'curl').filter((c) => c.args.includes('--output')); +/** The vault reads and which `op` made them. */ +const reads = (calls: Call[]) => tool(calls, 'op').filter((c) => c.args[0] === 'read'); + +test('a job with a token on a runner without op downloads the pinned version, and reads the vault with it', () => { + const job = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true }); + assert.equal(job.status, 0, job.log); + assert.deepEqual( + downloads(job.calls).map((c) => c.args.at(-1)), + [ZIP, KEY], + ); + assert.equal(job.output['server-actions-key'], 'a-key'); + const [read, ...more] = reads(job.calls); + assert.equal(more.length, 0); + assert.equal(read?.path, join(job.temp, 'op', 'op'), 'the vault is read by the installed op'); + assert.deepEqual(read?.seen, { OP_SERVICE_ACCOUNT_TOKEN: 'ops_service-account' }); + assert.match(job.log, new RegExp(`^installed op ${OP_VERSION.replaceAll('.', '\\.')}`, 'm')); +}); + +test('everything of the install is in the job’s own temporary directory, never in the checkout', () => { + const job = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true }); + assert.equal(job.status, 0, job.log); + const paths = [ + ...downloads(job.calls).map((c) => c.args[c.args.indexOf('--output') + 1] ?? ''), + ...tool(job.calls, 'unzip').map((c) => c.args[c.args.indexOf('-d') + 1] ?? ''), + ...tool(job.calls, 'gpg').map((c) => c.args[c.args.indexOf('--homedir') + 1] ?? ''), + ]; + assert.equal(paths.length, 5); + for (const path of paths) assert.ok(path.startsWith(`${job.temp}/`), path); +}); + +test('the download is verified before op is run: the signature against the pinned key, in a keyring of its own', () => { + const job = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true }); + assert.equal(job.status, 0, job.log); + const gpg = tool(job.calls, 'gpg'); + assert.deepEqual( + gpg.map((c) => c.args.find((a) => a === '--import' || a === '--verify')), + ['--import', '--verify'], + ); + const homes = new Set(gpg.map((c) => c.args[c.args.indexOf('--homedir') + 1])); + assert.equal(homes.size, 1, 'one keyring, the job’s, not the runner’s'); + const verify = gpg[1]?.args ?? []; + assert.deepEqual(verify.slice(-2), [join(job.temp, 'op', 'op.sig'), join(job.temp, 'op', 'op')]); + const order = job.calls.map((c) => `${c.tool}${c.args[0] === 'read' ? ' read' : ''}`); + assert.ok(order.indexOf('gpg') < order.indexOf('op read'), order.join(', ')); + assert.ok( + job.calls.findIndex((c) => c.tool === 'gpg' && c.args.includes('--verify')) < + job.calls.findIndex((c) => c.path.startsWith(job.temp)), + 'nothing the download holds is run before it is verified', + ); +}); + +test('a signature that does not verify fails the job in one line, and the vault is never read', () => { + const job = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true, gpgSigner: false }); + assert.equal(job.status, 1, job.log); + assert.match(job.log, new RegExp(`op ${OP_VERSION.replaceAll('.', '\\.')} .*not .*signature`)); + assert.deepEqual(reads(job.calls), []); + assert.deepEqual(job.output, {}); +}); + +test('a signature made by another key than 1Password’s fails the job, whichever key the download offers', () => { + const job = runJob(['repo'], { + env: SERVICE_ACCOUNT, + vault: VAULT, + noOp: true, + gpgSigner: 'ABCDEF0123456789ABCDEF0123456789ABCDEF01', + }); + assert.equal(job.status, 1, job.log); + assert.match(job.log, /ABCDEF0123456789ABCDEF0123456789ABCDEF01/); + assert.ok(job.log.includes(OP_SIGNING_KEY), 'it names the key it expected'); + assert.deepEqual(reads(job.calls), []); +}); + +test('an op that is not the version it was downloaded as fails the job', () => { + const job = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true, opVersion: '2.0.1' }); + assert.equal(job.status, 1, job.log); + assert.match( + job.log, + new RegExp(`2\\.0\\.1.*${OP_VERSION.replaceAll('.', '\\.')}|${OP_VERSION.replaceAll('.', '\\.')}.*2\\.0\\.1`), + ); + assert.deepEqual(reads(job.calls), []); +}); + +test('a runner that has op is left as it is: nothing is downloaded, and the vault is read with its op', () => { + const job = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT }); + assert.equal(job.status, 0, job.log); + for (const name of ['curl', 'unzip', 'gpg']) assert.deepEqual(tool(job.calls, name), [], name); + assert.ok(!/installed op/.test(job.log)); + const [read] = reads(job.calls); + assert.equal(dirname(read?.path ?? ''), dirname(tool(job.calls, 'op')[0]?.path ?? '')); + assert.ok(!(read?.path ?? '').startsWith(job.temp)); +}); + +test('a deploy job installs op the same way', () => { + const job = runJob(['deploy'], { env: SERVICE_ACCOUNT, vault: {}, noOp: true }); + assert.equal(job.status, 1, 'the vault holds no deploy key, which is the fake’s to refuse'); + assert.deepEqual( + downloads(job.calls).map((c) => c.args.at(-1)), + [ZIP, KEY], + ); + assert.ok(reads(job.calls).every((c) => c.path === join(job.temp, 'op', 'op'))); +}); + +test('a job without a token installs nothing, whether or not the runner has op', () => { + const job = runJob(['repo'], { env: { GITHUB_WORKFLOW: 'on_pr' }, vault: VAULT, noOp: true }); + assert.equal(job.status, 0, job.log); + assert.deepEqual(downloads(job.calls), []); + assert.deepEqual(tool(job.calls, 'op'), []); +}); + +test('under --dry-run nothing is downloaded or written', () => { + const job = runJob(['--dry-run', 'repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true }); + assert.deepEqual(downloads(job.calls), []); + assert.deepEqual(tool(job.calls, 'unzip'), []); + assert.deepEqual(tool(job.calls, 'gpg'), []); + assert.match(job.log, /\[dry-run\].*op/); +}); + +test('the version is stated once in the code, and the environment image’s pin is the same', () => { + const image = readFileSync( + join(import.meta.dirname, '../../../../ansible/ci/roles/actions_runners/files/ci-runner/mise.toml'), + 'utf8', + ); + assert.match(image, new RegExp(`^"aqua:1password/cli" = "${OP_VERSION.replaceAll('.', '\\.')}"$`, 'm')); +}); diff --git a/packages/cli-ci/src/core/op.ts b/packages/cli-ci/src/core/op.ts new file mode 100644 index 00000000..7384ef9b --- /dev/null +++ b/packages/cli-ci/src/core/op.ts @@ -0,0 +1,9 @@ +// The vault's CLI, `op`, for a runner that has none. +import type { Context } from '@plexus-ms/lib'; + +export const OP_VERSION = '2.42.0'; +export const OP_SIGNING_KEY = '3FEF9748469ADBE15DA7CA80AC2D62742012EA22'; + +export async function opOf(_ctx: Context, _env: NodeJS.ProcessEnv, _temp: string): Promise { + return 'op'; +} diff --git a/packages/cli-ci/src/testing/job.ts b/packages/cli-ci/src/testing/job.ts index 84887037..1a904394 100644 --- a/packages/cli-ci/src/testing/job.ts +++ b/packages/cli-ci/src/testing/job.ts @@ -6,10 +6,17 @@ // vault's and the overlay's variables it saw. Not shipped (excluded in // tsconfig). // -// op `op read ` answers from `vault`, else fails as op does +// op `op read ` answers from `vault`, else fails as op does; `op --version` answers +// `opVersion` (default the pinned one); with `noOp`, the op on the PATH is not found, +// as on a runner without it, and only one that was unzipped works // curl the forge's API, as curl answers with the config's `fail-with-body` // or `write-out`: a repository's installation, and a token for it, // or for every call the status `apiStatus`; any other download, a key +// and a download to a file (`--output`), which `fake download of ` +// fills; `noOp` leaves the runner without the vault's CLI +// unzip puts a working `op` (the fake itself) and its `op.sig` into the `-d` directory +// gpg `--import` succeeds; `--verify` reports the signature valid for the pinned key, +// `gpgSigner` for another key, or `gpgBad` fails as gpg does // sudo runs the rest of its arguments, its own options dropped // netbird `up` and `down` succeed unless `netbirdFails`; `status` is connected // getent every name resolves @@ -27,6 +34,7 @@ import { } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; +import { OP_SIGNING_KEY, OP_VERSION } from '../core/op.ts'; const BIN = join(import.meta.dirname, '..', 'bin.ts'); @@ -34,16 +42,24 @@ const BIN = join(import.meta.dirname, '..', 'bin.ts'); const SEEN = ['OP_CONNECT_HOST', 'OP_CONNECT_TOKEN', 'OP_SERVICE_ACCOUNT_TOKEN', 'NB_SETUP_KEY', 'SSH_AUTH_SOCK']; const FAKE = `#!/usr/bin/env node -const { appendFileSync, readFileSync } = require('node:fs'); +const { appendFileSync, chmodSync, copyFileSync, readFileSync, writeFileSync } = require('node:fs'); const { basename } = require('node:path'); const tool = basename(process.argv[1]); const args = process.argv.slice(2); -const input = ['ssh-add', 'gpg', 'tee'].includes(tool) || args.includes('-K') ? readFileSync(0, 'utf8') : undefined; +const input = ['ssh-add', 'tee'].includes(tool) || (tool === 'gpg' && args.includes('--dearmor')) || args.includes('-K') ? readFileSync(0, 'utf8') : undefined; const seen = Object.fromEntries(${JSON.stringify(SEEN)}.filter((k) => process.env[k] !== undefined).map((k) => [k, process.env[k]])); -appendFileSync(process.env.FAKE_LOG, JSON.stringify({ tool, args, input, seen }) + '\\n'); +appendFileSync(process.env.FAKE_LOG, JSON.stringify({ tool, args, input, seen, path: process.argv[1] }) + '\\n'); const fail = (message) => { process.stderr.write(message + '\\n'); process.exit(1); }; switch (tool) { case 'op': { + if (process.env.FAKE_NO_OP && require('node:path').dirname(process.argv[1]) === process.env.FAKE_BIN) { + process.stderr.write('op: command not found\\n'); + process.exit(127); + } + if (args[0] === '--version') { + process.stdout.write((process.env.FAKE_OP_VERSION || ${JSON.stringify(OP_VERSION)}) + '\\n'); + break; + } const vault = JSON.parse(readFileSync(process.env.FAKE_VAULT, 'utf8')); const ref = args[1]; if (args[0] !== 'read' || !(ref in vault)) fail('[ERROR] could not read secret ' + ref); @@ -51,6 +67,10 @@ switch (tool) { break; } case 'curl': { + if (args.includes('--output')) { + writeFileSync(args[args.indexOf('--output') + 1], 'fake download of ' + args.at(-1)); + break; + } if (!input) { process.stdout.write('-----BEGIN PGP PUBLIC KEY BLOCK-----\\nfake\\n-----END PGP PUBLIC KEY BLOCK-----\\n'); break; @@ -82,13 +102,28 @@ switch (tool) { if (process.env.FAKE_NETBIRD_FAILS && args[0] === 'up') fail('netbird: the management server refused the setup key'); if (args[0] === 'status') process.stdout.write('Management: Connected\\nSignal: Connected\\nPeers count: 0/3 Connected\\n'); break; + case 'unzip': { + const dir = args[args.indexOf('-d') + 1]; + copyFileSync(process.argv[1], dir + '/op'); + chmodSync(dir + '/op', 0o755); + writeFileSync(dir + '/op.sig', 'fake signature'); + break; + } + case 'gpg': { + if (args.includes('--verify')) { + if (process.env.FAKE_GPG_BAD) fail('gpg: BAD signature from "Code signing for 1Password"'); + const key = process.env.FAKE_GPG_SIGNER || ${JSON.stringify(OP_SIGNING_KEY)}; + process.stdout.write('[GNUPG:] GOODSIG AC2D62742012EA22 Code signing\\n[GNUPG:] VALIDSIG ' + key + ' 2026-11-04 1793825235 0 4 0 1 10 00 ' + key + '\\n'); + } + break; + } case 'getent': process.stdout.write('100.64.0.9 ' + args[1] + '\\n'); break; } `; -const TOOLS = ['op', 'curl', 'sudo', 'netbird', 'getent', 'ssh-agent', 'ssh-add', 'gpg', 'tee', 'apt-get']; +const TOOLS = ['op', 'curl', 'sudo', 'netbird', 'getent', 'ssh-agent', 'ssh-add', 'gpg', 'tee', 'apt-get', 'unzip']; /** One call a fake recorded. */ export interface Call { @@ -96,6 +131,8 @@ export interface Call { args: string[]; input?: string; seen: Record; + /** Where the tool that ran is: the fake on the PATH, or a copy of it that was unzipped. */ + path: string; } export interface Job { @@ -132,6 +169,12 @@ export function runJob( env?: Record; vault?: Record; netbirdFails?: boolean; + /** The runner has no vault CLI of its own: the one on its PATH is not found. */ + noOp?: boolean; + /** What the unzipped `op` says its version is. */ + opVersion?: string; + /** What gpg says of the signature: `false` for a bad one, a fingerprint for one of another key. */ + gpgSigner?: string | false; apiStatus?: number; } = {}, ): Job { @@ -165,6 +208,14 @@ export function runJob( RUNNER_TEMP: temp, FAKE_LOG: join(dir, 'calls'), FAKE_VAULT: vault, + FAKE_BIN: bin, + ...(options.noOp ? { FAKE_NO_OP: '1' } : {}), + ...(options.opVersion ? { FAKE_OP_VERSION: options.opVersion } : {}), + ...(options.gpgSigner === false + ? { FAKE_GPG_BAD: '1' } + : options.gpgSigner + ? { FAKE_GPG_SIGNER: options.gpgSigner } + : {}), ...(options.netbirdFails ? { FAKE_NETBIRD_FAILS: '1' } : {}), ...(options.apiStatus ? { FAKE_API_STATUS: String(options.apiStatus) } : {}), ...options.env, From 9e4f7cea41a550958f70ffc426e2432fe0ad0fa4 Mon Sep 17 00:00:00 2001 From: David Julian Albers Date: Thu, 3 Dec 2026 03:12:26 +0100 Subject: [PATCH 4/8] PLX-248: plx-ci setup installs op, verified, where the runner has none With a token and no op on the PATH, the step downloads the pinned version from 1Password into RUNNER_TEMP/op, checks the signature of op against the pinned signing key in a keyring of the job's own, checks its version, and reads the vault with it. A runner that has op is left as it is. The pin of the environment image is held equal to the constant by a test. Co-Authored-By: Claude Sonnet --- .../actions_runners/files/ci-runner/mise.toml | 4 +- packages/cli-ci/src/commands/setup.test.ts | 5 +- packages/cli-ci/src/commands/setup.ts | 13 +++ packages/cli-ci/src/core/op.ts | 85 ++++++++++++++++++- packages/cli-ci/src/core/setup.ts | 17 +++- packages/cli-ci/src/core/vault.ts | 9 +- 6 files changed, 119 insertions(+), 14 deletions(-) diff --git a/ansible/ci/roles/actions_runners/files/ci-runner/mise.toml b/ansible/ci/roles/actions_runners/files/ci-runner/mise.toml index e7664287..f1906fb0 100644 --- a/ansible/ci/roles/actions_runners/files/ci-runner/mise.toml +++ b/ansible/ci/roles/actions_runners/files/ci-runner/mise.toml @@ -10,7 +10,9 @@ # the update bot (§ 1 PLX-Updates); allow_low_downloads only waives npm's # young-package heuristics. It runs on the base image's node. "npm:@plexus-ms/cli-ci" = { version = "0.135.0", allow_low_downloads = true } -# The vault's CLI, which load-secrets-action uses where it finds one. +# The vault's CLI, which plx-ci setup reads the vault with. A runner without it, the forge's own, gets the same +# version installed by plx-ci setup (packages/cli-ci/src/core/op.ts, which holds the version in code; a test of +# cli-ci holds the two equal, so the update bot's pull request for a new one stays red until that moves too). "aqua:1password/cli" = "2.42.0" # What jobs installed for themselves: gh for plx-update. exo stays for the # transition, for the sweep of the instances an earlier version launched; the diff --git a/packages/cli-ci/src/commands/setup.test.ts b/packages/cli-ci/src/commands/setup.test.ts index 610923f7..d886a309 100644 --- a/packages/cli-ci/src/commands/setup.test.ts +++ b/packages/cli-ci/src/commands/setup.test.ts @@ -20,7 +20,7 @@ const SERVICE_ACCOUNT = { OP_SERVICE_ACCOUNT_TOKEN: 'ops_service-account' }; /** The vault's credentials each `op` call saw. */ const credentialsOf = (calls: ReturnType['calls']) => calls - .filter((c) => c.tool === 'op') + .filter((c) => c.tool === 'op' && c.args[0] === 'read') .map((c) => Object.keys(c.seen) .filter((k) => k.startsWith('OP_')) @@ -260,7 +260,8 @@ test('under --dry-run a deploy job reads and masks, and calls nothing: no API, n assert.equal(job.status, 0, job.log); assert.deepEqual( job.calls.map((c) => c.tool), - ['op', 'op', 'op'], + ['op', 'op', 'op', 'op'], + 'the probe for op, then the three reads', ); assert.deepEqual([job.output, job.env], [{}, {}]); }); diff --git a/packages/cli-ci/src/commands/setup.ts b/packages/cli-ci/src/commands/setup.ts index b220a433..b3912b06 100644 --- a/packages/cli-ci/src/commands/setup.ts +++ b/packages/cli-ci/src/commands/setup.ts @@ -58,6 +58,19 @@ // Under --dry-run it reads and masks, calls no API, and writes and starts // nothing. Exit code 1 where that refusal, a read, the token or a deploy job's join fails. // +// Where the job is handed no credential for the vault, as the job of a pull +// request from a fork is (the forge gives it no secrets), a repo job reads +// nothing from it, says so in one line, and goes on with the logins, which are +// its own token's: it gets no server-actions key and joins no overlay, so the +// verbs run without the remote cache. A deploy job, and a repo job of the +// workflow on_push, which publishes images that carry the repo's one key, +// fail there in a line that names the credentials missing. +// Where the job has a credential and the runner no `op`, as the forge's own +// runners have none, the command installs the pinned version into +// RUNNER_TEMP/op and reads with it, after verifying its signature; a runner +// that has `op` is left as it is (core/op.ts says how, and where the version +// is stated). +// // plx-ci setup repo --overlay netbird --private-packages // plx-ci setup deploy --overlay netbird import type { Command } from 'commander'; diff --git a/packages/cli-ci/src/core/op.ts b/packages/cli-ci/src/core/op.ts index 7384ef9b..fce7f994 100644 --- a/packages/cli-ci/src/core/op.ts +++ b/packages/cli-ci/src/core/op.ts @@ -1,9 +1,86 @@ -// The vault's CLI, `op`, for a runner that has none. -import type { Context } from '@plexus-ms/lib'; +// The vault's CLI, `op`, on a runner that has none: the forge's own runners +// carry no `op`, and a job with a token needs it to read the vault +// (core/vault.ts). A runner that has one, as the environment image of a runner +// host does, is left as it is. Otherwise `opOf` installs OP_VERSION into the +// job's own temporary directory (RUNNER_TEMP/op, outside the checkout), as +// 1Password documents its CLI for Linux (developer.1password.com, "Get +// started with 1Password CLI", the manual install): the zip from +// cache.agilebits.com, which holds `op` and its detached signature `op.sig`, +// and `gpg --verify op.sig op` against 1Password's signing key. The key is +// fetched from 1Password's own site (the apt repository's key file, the same +// key) into a keyring made for the job, and it is the signature's primary key +// that must be the pinned fingerprint, so that a key the download served +// instead is no key of 1Password's. Nothing is run before it verifies; after +// it, `op --version` must say the version asked for. +// +// The version is stated here and nowhere else in the code. The environment +// image pins the same version in its mise.toml (aqua:1password/cli), where +// the update bot can propose a new one; a test holds the two equal, so that +// such a pull request stays red until this constant moves with it. +import { join } from 'node:path'; +import { type Context, run } from '@plexus-ms/lib'; export const OP_VERSION = '2.42.0'; +/** 1Password's code-signing key (fingerprint 3FEF 9748 469A DBE1 5DA7 CA80 AC2D 6274 2012 EA22). */ export const OP_SIGNING_KEY = '3FEF9748469ADBE15DA7CA80AC2D62742012EA22'; -export async function opOf(_ctx: Context, _env: NodeJS.ProcessEnv, _temp: string): Promise { - return 'op'; +const KEY_URL = 'https://downloads.1password.com/linux/keys/1password.asc'; +/** 1Password's names for the CPU architectures of a Linux runner. */ +const ARCHITECTURES: Record = { x64: 'amd64', arm64: 'arm64', arm: 'arm', ia32: '386' }; + +/** + * The command that runs the vault's CLI on this runner: `op` where the runner + * has it, else the verified install of OP_VERSION in `temp`, whose path. + */ +export async function opOf(ctx: Context, env: NodeJS.ProcessEnv, temp: string): Promise { + // Asked without the vault's credentials, which the question needs none of. + const asked = Object.fromEntries(Object.entries(env).filter(([name]) => !name.startsWith('OP_'))); + if ( + await ctx.read('op', ['--version'], { env: asked }).then( + () => true, + () => false, + ) + ) + return 'op'; + const architecture = ARCHITECTURES[process.arch]; + if (!architecture) throw new Error(`no op for the architecture ${process.arch}`); + const dir = join(temp, 'op'); + const [gnupg, zip, key, bin, sig] = ['gnupg', 'op.zip', 'key.asc', 'op', 'op.sig'].map((name) => join(dir, name)) as [ + string, + string, + string, + string, + string, + ]; + ctx.say(`${ctx.dryRun ? '[dry-run] ' : ''}this runner has no op: installing ${OP_VERSION}, verified, into ${dir}`); + await ctx.apply([ + run('mkdir', ['-p', '-m', '700', dir]), + run('mkdir', ['-m', '700', gnupg]), + run('curl', [ + '-fsSL', + '--output', + zip, + `https://cache.agilebits.com/dist/1P/op2/pkg/v${OP_VERSION}/op_linux_${architecture}_v${OP_VERSION}.zip`, + ]), + run('curl', ['-fsSL', '--output', key, KEY_URL]), + run('unzip', ['-qo', zip, '-d', dir]), + ]); + if (ctx.dryRun) return 'op'; + const what = `op ${OP_VERSION} from 1Password’s download`; + await ctx.read('gpg', ['--homedir', gnupg, '--batch', '--quiet', '--import', key]); + const status = await ctx + .read('gpg', ['--homedir', gnupg, '--batch', '--status-fd', '1', '--verify', sig, bin]) + .catch(() => { + throw new Error(`${what} did not pass its signature check, and is not run`); + }); + const signer = /^\[GNUPG:\] VALIDSIG .* ([0-9A-F]{40})$/m.exec(status)?.[1]; + if (signer !== OP_SIGNING_KEY) + throw new Error( + `${what} is not signed by 1Password’s key ${OP_SIGNING_KEY} but by ${signer ?? 'no key'}, and is not run`, + ); + await ctx.apply([run('chmod', ['700', bin])]); + const said = (await ctx.read(bin, ['--version'])).trim(); + if (said !== OP_VERSION) throw new Error(`${what} says it is ${said}, not ${OP_VERSION}`); + ctx.say(`installed op ${OP_VERSION}, signature verified`); + return bin; } diff --git a/packages/cli-ci/src/core/setup.ts b/packages/cli-ci/src/core/setup.ts index f99cba12..09c0f8e2 100644 --- a/packages/cli-ci/src/core/setup.ts +++ b/packages/cli-ci/src/core/setup.ts @@ -13,6 +13,7 @@ import { write, } from '@plexus-ms/lib'; import { need, ownerOf } from '../commands/shared.ts'; +import { opOf } from './op.ts'; import { mask, type Vault, vaultAccess, vaultOf } from './vault.ts'; export const JOBS = ['repo', 'deploy'] as const; @@ -67,12 +68,20 @@ function refuseOnPushWithoutEnvironment(env: NodeJS.ProcessEnv): void { * forge gives such a job no secrets. It says so in one line and goes on with * what needs none: the logins are the job's own token's, and without the key * and the cache tokens the verbs run with a key of Next's own making and - * without the remote cache. A deploy job cannot deploy without the vault, and + * without the remote cache. A job with a credential reads with the runner's + * `op`, or with one installed for the job where the runner has none + * (core/op.ts). A deploy job cannot deploy without the vault, and * a run of on_push publishes images of the repo's one key (§ 3.8): both fail * here, in a line that names the credentials missing. */ -function vaultFor(ctx: Context, options: SetupOptions, env: NodeJS.ProcessEnv): Vault | undefined { - if (vaultAccess(env) || options.job === 'deploy') return vaultOf(ctx, env); +async function vaultFor( + ctx: Context, + options: SetupOptions, + env: NodeJS.ProcessEnv, + temp: string, +): Promise { + if (vaultAccess(env)) return vaultOf(ctx, env, await opOf(ctx, env, temp)); + if (options.job === 'deploy') return vaultOf(ctx, env); if (env.GITHUB_WORKFLOW === 'on_push') { try { vaultOf(ctx, env); @@ -93,7 +102,7 @@ export async function setup(ctx: Context, options: SetupOptions, env: NodeJS.Pro const repo = repository.split('/')[1] ?? repository; const vaultName = `${owner}-ci`; const temp = need('RUNNER_TEMP', env); - const vault = vaultFor(ctx, options, env); + const vault = await vaultFor(ctx, options, env, temp); const outputs: Record = {}; const variables: Record = {}; const plan: Plan = []; diff --git a/packages/cli-ci/src/core/vault.ts b/packages/cli-ci/src/core/vault.ts index 6197708e..3c5dedb8 100644 --- a/packages/cli-ci/src/core/vault.ts +++ b/packages/cli-ci/src/core/vault.ts @@ -39,8 +39,11 @@ export function vaultAccess(env: NodeJS.ProcessEnv = process.env): VaultAccess | return env.OP_SERVICE_ACCOUNT_TOKEN ? 'service account' : undefined; } -/** The vault as this runner reaches it; an error naming both ways where it reaches it neither way. */ -export function vaultOf(ctx: Context, env: NodeJS.ProcessEnv = process.env): Vault { +/** + * The vault as this runner reaches it, read with the CLI at `op` (the runner's own, unless core/op.ts installed + * one); an error naming both ways where it reaches it neither way. + */ +export function vaultOf(ctx: Context, env: NodeJS.ProcessEnv = process.env, op = 'op'): Vault { const access = vaultAccess(env); if (!access) throw new Error( @@ -52,7 +55,7 @@ export function vaultOf(ctx: Context, env: NodeJS.ProcessEnv = process.env): Vau return { access, read: async (ref, { secret = true } = {}) => { - const value = (await ctx.read('op', ['read', ref], { env: opEnv })).replace(/\n$/, ''); + const value = (await ctx.read(op, ['read', ref], { env: opEnv })).replace(/\n$/, ''); if (secret) mask(ctx, value); return value; }, From ffc46f40af4f1010d54969175a4f1804fd068d5c Mon Sep 17 00:00:00 2001 From: David Julian Albers Date: Thu, 3 Dec 2026 03:13:29 +0100 Subject: [PATCH 5/8] PLX-248: the header of the job fakes says what the new fakes do Co-Authored-By: Claude Sonnet --- packages/cli-ci/src/testing/job.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/cli-ci/src/testing/job.ts b/packages/cli-ci/src/testing/job.ts index 1a904394..aad8adb0 100644 --- a/packages/cli-ci/src/testing/job.ts +++ b/packages/cli-ci/src/testing/job.ts @@ -11,12 +11,12 @@ // as on a runner without it, and only one that was unzipped works // curl the forge's API, as curl answers with the config's `fail-with-body` // or `write-out`: a repository's installation, and a token for it, -// or for every call the status `apiStatus`; any other download, a key -// and a download to a file (`--output`), which `fake download of ` -// fills; `noOp` leaves the runner without the vault's CLI +// or for every call the status `apiStatus`; a download to a file +// (`--output`), which `fake download of ` fills; any other +// download, a key // unzip puts a working `op` (the fake itself) and its `op.sig` into the `-d` directory // gpg `--import` succeeds; `--verify` reports the signature valid for the pinned key, -// `gpgSigner` for another key, or `gpgBad` fails as gpg does +// `gpgSigner` for another key's, or fails as gpg does for `gpgSigner: false` // sudo runs the rest of its arguments, its own options dropped // netbird `up` and `down` succeed unless `netbirdFails`; `status` is connected // getent every name resolves From f83bad7568cfe684bd7f49e28298c464fd6f9613 Mon Sep 17 00:00:00 2001 From: David Julian Albers Date: Thu, 3 Dec 2026 03:29:24 +0100 Subject: [PATCH 6/8] PLX-248: tests for what the install may run, and for the no-token line on each kind of runner Red: unzip extracts every member into the directory of the keyring, gpg is started with its configuration and agent, curl and the installed op see the vault's credentials, a missing tool ends as a bare spawn error, and the no-token line is no annotation and claims no cache on a slot that has its host's. Green at once, as guards: the primary key being the one compared, a verification naming no signature, a slot with Connect. Co-Authored-By: Claude Sonnet --- packages/cli-ci/src/commands/setupOp.test.ts | 96 ++++++++++++++++++- .../src/commands/setupWithoutToken.test.ts | 46 ++++++++- packages/cli-ci/src/testing/job.ts | 42 ++++++-- 3 files changed, 172 insertions(+), 12 deletions(-) diff --git a/packages/cli-ci/src/commands/setupOp.test.ts b/packages/cli-ci/src/commands/setupOp.test.ts index 7fe240b0..40830e06 100644 --- a/packages/cli-ci/src/commands/setupOp.test.ts +++ b/packages/cli-ci/src/commands/setupOp.test.ts @@ -3,7 +3,7 @@ // and reads as before; a runner that has `op` is left as it is (§ 8.4 of the project's decisions). The fakes of // testing/job.ts stand in for the download, gpg and `op`. import assert from 'node:assert/strict'; -import { readFileSync } from 'node:fs'; +import { existsSync, readdirSync, readFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { test } from 'node:test'; import { OP_SIGNING_KEY, OP_VERSION } from '../core/op.ts'; @@ -31,7 +31,7 @@ test('a job with a token on a runner without op downloads the pinned version, an assert.equal(job.output['server-actions-key'], 'a-key'); const [read, ...more] = reads(job.calls); assert.equal(more.length, 0); - assert.equal(read?.path, join(job.temp, 'op', 'op'), 'the vault is read by the installed op'); + assert.equal(read?.path, join(job.temp, 'op', 'bin', 'op'), 'the vault is read by the installed op'); assert.deepEqual(read?.seen, { OP_SERVICE_ACCOUNT_TOKEN: 'ops_service-account' }); assert.match(job.log, new RegExp(`^installed op ${OP_VERSION.replaceAll('.', '\\.')}`, 'm')); }); @@ -59,7 +59,7 @@ test('the download is verified before op is run: the signature against the pinne const homes = new Set(gpg.map((c) => c.args[c.args.indexOf('--homedir') + 1])); assert.equal(homes.size, 1, 'one keyring, the job’s, not the runner’s'); const verify = gpg[1]?.args ?? []; - assert.deepEqual(verify.slice(-2), [join(job.temp, 'op', 'op.sig'), join(job.temp, 'op', 'op')]); + assert.deepEqual(verify.slice(-2), [join(job.temp, 'op', 'bin', 'op.sig'), join(job.temp, 'op', 'bin', 'op')]); const order = job.calls.map((c) => `${c.tool}${c.args[0] === 'read' ? ' read' : ''}`); assert.ok(order.indexOf('gpg') < order.indexOf('op read'), order.join(', ')); assert.ok( @@ -117,7 +117,7 @@ test('a deploy job installs op the same way', () => { downloads(job.calls).map((c) => c.args.at(-1)), [ZIP, KEY], ); - assert.ok(reads(job.calls).every((c) => c.path === join(job.temp, 'op', 'op'))); + assert.ok(reads(job.calls).every((c) => c.path === join(job.temp, 'op', 'bin', 'op'))); }); test('a job without a token installs nothing, whether or not the runner has op', () => { @@ -142,3 +142,91 @@ test('the version is stated once in the code, and the environment image’s pin ); assert.match(image, new RegExp(`^"aqua:1password/cli" = "${OP_VERSION.replaceAll('.', '\\.')}"$`, 'm')); }); + +/** What a tampered download can hold besides `op` and `op.sig`: a gpg configuration naming a program for the agent, and a file that climbs out. */ +const PLANTED = ['gnupg/gpg.conf', 'gpg.conf', '../../evil', 'agent']; + +test('only op and op.sig are taken from the zip, into a directory that holds nothing else', () => { + const job = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true, zipExtra: PLANTED }); + assert.equal(job.status, 0, job.log); + const [unzip] = tool(job.calls, 'unzip'); + const into = unzip?.args[unzip.args.indexOf('-d') + 1] ?? ''; + assert.deepEqual(unzip?.args.slice(unzip.args.indexOf('-d') - 2, unzip.args.indexOf('-d')), ['op', 'op.sig']); + assert.deepEqual(readdirSync(into).sort(), ['op', 'op.sig']); + const keyring = tool(job.calls, 'gpg')[0]?.args[tool(job.calls, 'gpg')[0]?.args.indexOf('--homedir') + 1] ?? ''; + assert.ok( + !keyring.startsWith(`${into}/`) && !into.startsWith(`${keyring}/`) && keyring !== into, + 'the keyring is elsewhere', + ); + assert.deepEqual(readdirSync(keyring), [], 'nothing of the zip is in the keyring'); + for (const planted of ['gpg.conf', 'agent', 'evil']) + assert.ok(!existsSync(join(job.temp, 'op', planted)) && !existsSync(join(job.temp, planted)), planted); +}); + +test('the key is imported before anything is unzipped, and gpg takes no configuration and starts no agent of its own', () => { + const job = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true }); + assert.equal(job.status, 0, job.log); + const order = job.calls.map((c) => + c.tool === 'gpg' ? `gpg ${c.args.find((a) => a === '--import' || a === '--verify')}` : c.tool, + ); + assert.ok(order.indexOf('gpg --import') < order.indexOf('unzip'), order.join(', ')); + assert.ok(order.indexOf('unzip') < order.indexOf('gpg --verify'), order.join(', ')); + for (const gpg of tool(job.calls, 'gpg')) { + assert.equal(gpg.args[0], '--no-options', 'which gpg reads before it reads any file'); + assert.ok(gpg.args.includes('--no-autostart'), gpg.args.join(' ')); + } +}); + +test('the downloads are over https alone, and tried again', () => { + const job = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true }); + assert.equal(job.status, 0, job.log); + for (const curl of downloads(job.calls)) { + assert.ok(curl.args.join(' ').includes('--proto =https'), curl.args.join(' ')); + assert.ok(curl.args.includes('--retry'), curl.args.join(' ')); + } +}); + +test('nothing the install starts, and nothing the installed op is asked, sees the vault’s credentials', () => { + const job = runJob(['repo'], { env: { ...SERVICE_ACCOUNT, OP_CONNECT_HOST: 'h' }, vault: VAULT, noOp: true }); + assert.equal(job.status, 0, job.log); + const started = job.calls.filter( + (c) => ['curl', 'unzip', 'gpg'].includes(c.tool) || (c.tool === 'op' && c.args[0] === '--version'), + ); + assert.ok(started.length >= 7, 'the probe, the downloads, gpg, unzip and the installed op'); + for (const call of started) + assert.deepEqual( + Object.keys(call.seen).filter((k) => k.startsWith('OP_')), + [], + `${call.tool} ${call.args.join(' ')}`, + ); +}); + +test('the primary key must be the pinned one, whichever subkey signs', () => { + const OTHER = 'ABCDEF0123456789ABCDEF0123456789ABCDEF01'; + const wrongPrimary = runJob(['repo'], { + env: SERVICE_ACCOUNT, + vault: VAULT, + noOp: true, + gpgSigner: OTHER, + gpgSubkey: OP_SIGNING_KEY, + }); + assert.equal(wrongPrimary.status, 1, wrongPrimary.log); + assert.deepEqual(reads(wrongPrimary.calls), []); + const subkey = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true, gpgSubkey: OTHER }); + assert.equal(subkey.status, 0, subkey.log); +}); + +test('a verification that exits 0 and names no signature fails the job', () => { + const job = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true, gpgSilent: true }); + assert.equal(job.status, 1, job.log); + assert.deepEqual(reads(job.calls), []); +}); + +test('a runner without unzip or gpg or curl fails the job in one line that names the tool', () => { + for (const missing of ['unzip', 'gpg', 'curl']) { + const job = runJob(['repo'], { env: SERVICE_ACCOUNT, vault: VAULT, noOp: true, without: [missing] }); + assert.equal(job.status, 1, `${missing}\n${job.log}`); + assert.match(job.log, new RegExp(`cannot install op: .*${missing}.* not on this runner`), missing); + assert.deepEqual(reads(job.calls), []); + } +}); diff --git a/packages/cli-ci/src/commands/setupWithoutToken.test.ts b/packages/cli-ci/src/commands/setupWithoutToken.test.ts index 747bcfa6..500a8b66 100644 --- a/packages/cli-ci/src/commands/setupWithoutToken.test.ts +++ b/packages/cli-ci/src/commands/setupWithoutToken.test.ts @@ -14,7 +14,7 @@ const ON_PUSH = { GITHUB_WORKFLOW: 'on_push', GITHUB_REF_NAME: 'main', GITHUB_EV const FORK = { ...FORGE_RUNNER, ...CACHE_HOST, GITHUB_WORKFLOW: 'on_pr', GITHUB_EVENT_NAME: 'pull_request' }; /** The one line the step says about it. */ -const SAYS = /^no token for the vault: /m; +const SAYS = /^::warning::no token for the vault: /m; test('a repo job with no token reads nothing, and says so in one line', () => { const job = runJob(['repo', '--overlay', 'netbird'], { env: FORK, vault: VAULT }); @@ -70,3 +70,47 @@ test('a job with a token reads as before and says nothing of the kind', () => { assert.ok(!SAYS.test(job.log), job.log); assert.equal(job.output['server-actions-key'], 'a-key'); }); + +/** A slot of a runner host: its host's cache servers on loopback and their tokens, which no workflow hands it. */ +const SLOT_CACHES = { + RUNNER_ENVIRONMENT: 'self-hosted', + PLX_TURBO_SHARED: 'http://127.0.0.1:8585', + PLX_TURBO_SHARED_TOKEN: 'slot-shared-token', + PLX_TURBO_TRUSTED: 'http://127.0.0.1:8586', + PLX_TURBO_TRUSTED_TOKEN: 'slot-trusted-token', +}; + +test('on a forge runner the line says the job has no key and no remote cache, as annotation of the run', () => { + const job = runJob(['repo'], { env: FORK, vault: VAULT }); + assert.equal(job.status, 0, job.log); + assert.match( + job.log, + /^::warning::no token for the vault: reading nothing from it; .*without the server-actions key and without the remote cache$/m, + ); +}); + +test('on a slot without a Connect server the line claims no lack of cache: the slot hands its host’s cache tokens', () => { + const job = runJob(['repo'], { env: { ...SLOT_CACHES, GITHUB_WORKFLOW: 'on_pr' }, vault: VAULT }); + assert.equal(job.status, 0, job.log); + assert.match(job.log, SAYS); + assert.match(job.log, /without the server-actions key/); + assert.ok(!/without the remote cache/.test(job.log), 'the slot’s cache servers stay with the job'); + assert.match(job.log, /the cache servers of this runner’s host/); + assert.deepEqual(job.output, {}); +}); + +test('on a slot with a Connect server the job has a credential: it reads the key, and says nothing of having no token', () => { + const job = runJob(['repo'], { + env: { + ...SLOT_CACHES, + OP_CONNECT_HOST: 'http://localhost:8080', + OP_CONNECT_TOKEN: 'connect-token', + GITHUB_WORKFLOW: 'on_pr', + }, + vault: VAULT, + }); + assert.equal(job.status, 0, job.log); + assert.ok(!SAYS.test(job.log), job.log); + assert.match(job.log, /through the runner’s Connect server/); + assert.equal(job.output['server-actions-key'], 'a-key'); +}); diff --git a/packages/cli-ci/src/testing/job.ts b/packages/cli-ci/src/testing/job.ts index aad8adb0..8d606a40 100644 --- a/packages/cli-ci/src/testing/job.ts +++ b/packages/cli-ci/src/testing/job.ts @@ -30,10 +30,11 @@ import { mkdtempSync, openSync, readFileSync, + symlinkSync, writeFileSync, } from 'node:fs'; import { tmpdir } from 'node:os'; -import { join } from 'node:path'; +import { basename, dirname, join } from 'node:path'; import { OP_SIGNING_KEY, OP_VERSION } from '../core/op.ts'; const BIN = join(import.meta.dirname, '..', 'bin.ts'); @@ -103,17 +104,27 @@ switch (tool) { if (args[0] === 'status') process.stdout.write('Management: Connected\\nSignal: Connected\\nPeers count: 0/3 Connected\\n'); break; case 'unzip': { + // As unzip does: the members named after the zip, or every member where none is; a name loses its leading ../ const dir = args[args.indexOf('-d') + 1]; - copyFileSync(process.argv[1], dir + '/op'); - chmodSync(dir + '/op', 0o755); - writeFileSync(dir + '/op.sig', 'fake signature'); + const named = args.slice(args.findIndex((a) => !a.startsWith('-')) + 1).filter((a, i, all) => a !== '-d' && all[i - 1] !== '-d'); + const members = ['op', 'op.sig', ...JSON.parse(process.env.FAKE_ZIP_EXTRA || '[]')]; + for (const member of named.length ? members.filter((m) => named.includes(m)) : members) { + const file = require('node:path').join(dir, member.replace(/^(\\.\\.\\/)+/, '')); + require('node:fs').mkdirSync(require('node:path').dirname(file), { recursive: true }); + if (member === 'op') { + copyFileSync(process.argv[1], file); + chmodSync(file, 0o755); + } else writeFileSync(file, member === 'op.sig' ? 'fake signature' : 'planted by the zip'); + } break; } case 'gpg': { if (args.includes('--verify')) { if (process.env.FAKE_GPG_BAD) fail('gpg: BAD signature from "Code signing for 1Password"'); + if (process.env.FAKE_GPG_SILENT) break; const key = process.env.FAKE_GPG_SIGNER || ${JSON.stringify(OP_SIGNING_KEY)}; - process.stdout.write('[GNUPG:] GOODSIG AC2D62742012EA22 Code signing\\n[GNUPG:] VALIDSIG ' + key + ' 2026-11-04 1793825235 0 4 0 1 10 00 ' + key + '\\n'); + const subkey = process.env.FAKE_GPG_SUBKEY || key; + process.stdout.write('[GNUPG:] GOODSIG AC2D62742012EA22 Code signing\\n[GNUPG:] VALIDSIG ' + subkey + ' 2026-11-04 1793825235 0 4 0 1 10 00 ' + key + '\\n'); } break; } @@ -173,6 +184,14 @@ export function runJob( noOp?: boolean; /** What the unzipped `op` says its version is. */ opVersion?: string; + /** Members the downloaded zip holds besides `op` and `op.sig`, by name. */ + zipExtra?: string[]; + /** The fingerprint of the signing subkey gpg reports, where the primary key signs by a subkey. */ + gpgSubkey?: string; + /** gpg exits 0 and says nothing of the signature. */ + gpgSilent?: boolean; + /** Tools the runner lacks: not on its PATH at all. */ + without?: string[]; /** What gpg says of the signature: `false` for a bad one, a fingerprint for one of another key. */ gpgSigner?: string | false; apiStatus?: number; @@ -183,10 +202,15 @@ export function runJob( const temp = join(dir, 'temp'); mkdirSync(bin); mkdirSync(temp); - for (const tool of TOOLS) { + for (const tool of TOOLS.filter((t) => !options.without?.includes(t))) { writeFileSync(join(bin, tool), FAKE); chmodSync(join(bin, tool), 0o755); } + if (options.without) + for (const tool of [dirname(process.execPath), '/usr/bin'].flatMap((d) => + ['node', 'mkdir', 'chmod'].map((t) => join(d, t)), + )) + if (existsSync(tool) && !existsSync(join(bin, basename(tool)))) symlinkSync(tool, join(bin, basename(tool))); const vault = join(dir, 'vault.json'); writeFileSync(vault, JSON.stringify(options.vault ?? {})); const logPath = join(dir, 'log'); @@ -195,7 +219,8 @@ export function runJob( stdio: ['ignore', fd, fd], timeout: 60_000, env: { - PATH: `${bin}:${process.env.PATH}`, + // A runner lacking tools has the fakes, node, and the plain tools the command runs itself, nothing else. + PATH: options.without ? bin : `${bin}:${process.env.PATH}`, HOME: dir, GITHUB_ACTIONS: 'true', GITHUB_REPOSITORY: 'acme/web', @@ -209,6 +234,9 @@ export function runJob( FAKE_LOG: join(dir, 'calls'), FAKE_VAULT: vault, FAKE_BIN: bin, + ...(options.zipExtra ? { FAKE_ZIP_EXTRA: JSON.stringify(options.zipExtra) } : {}), + ...(options.gpgSubkey ? { FAKE_GPG_SUBKEY: options.gpgSubkey } : {}), + ...(options.gpgSilent ? { FAKE_GPG_SILENT: '1' } : {}), ...(options.noOp ? { FAKE_NO_OP: '1' } : {}), ...(options.opVersion ? { FAKE_OP_VERSION: options.opVersion } : {}), ...(options.gpgSigner === false From 7768ac9ddf326ae53eb2a8f1ce8bcfcb3ff9d6b0 Mon Sep 17 00:00:00 2001 From: David Julian Albers Date: Thu, 3 Dec 2026 03:31:55 +0100 Subject: [PATCH 7/8] PLX-248: the install runs nothing of the zip before it verifies; the no-token line is a warning and true on a slot unzip takes op and op.sig by name into a directory of their own, the keyring is made and the key imported before the zip is opened, gpg runs with --no-options and --no-autostart, curl is https-only with retries, none of them sees OP_*, and a missing tool is named. The pin's comment no longer promises an update bot pull request; a person moves both values. Co-Authored-By: Claude Sonnet --- .../actions_runners/files/ci-runner/mise.toml | 4 +- packages/cli-ci/src/commands/setup.ts | 41 +++---- packages/cli-ci/src/core/op.ts | 109 +++++++++++------- packages/cli-ci/src/core/setup.ts | 19 ++- 4 files changed, 102 insertions(+), 71 deletions(-) diff --git a/ansible/ci/roles/actions_runners/files/ci-runner/mise.toml b/ansible/ci/roles/actions_runners/files/ci-runner/mise.toml index f1906fb0..471f8c81 100644 --- a/ansible/ci/roles/actions_runners/files/ci-runner/mise.toml +++ b/ansible/ci/roles/actions_runners/files/ci-runner/mise.toml @@ -11,8 +11,8 @@ # young-package heuristics. It runs on the base image's node. "npm:@plexus-ms/cli-ci" = { version = "0.135.0", allow_low_downloads = true } # The vault's CLI, which plx-ci setup reads the vault with. A runner without it, the forge's own, gets the same -# version installed by plx-ci setup (packages/cli-ci/src/core/op.ts, which holds the version in code; a test of -# cli-ci holds the two equal, so the update bot's pull request for a new one stays red until that moves too). +# version installed by plx-ci setup (packages/cli-ci/src/core/op.ts holds it in code). The update bot cannot look +# this package up, so a person moves the two together, and a test of cli-ci holds them equal. "aqua:1password/cli" = "2.42.0" # What jobs installed for themselves: gh for plx-update. exo stays for the # transition, for the sweep of the instances an earlier version launched; the diff --git a/packages/cli-ci/src/commands/setup.ts b/packages/cli-ci/src/commands/setup.ts index b3912b06..65a20812 100644 --- a/packages/cli-ci/src/commands/setup.ts +++ b/packages/cli-ci/src/commands/setup.ts @@ -7,7 +7,21 @@ // which is on the overlay already and whose repo slots carry their host's // cache servers. In order: // -// 1. Read from the vault `-ci` what the job needs, every read through +// 1. Where the job has a credential for the vault (a Connect server or the +// service account's token) and the runner no `op`, as the forge's own +// runners have none, install the pinned version into RUNNER_TEMP/op, +// verified by its signature (core/op.ts says how, and where the version +// is stated); a runner that has `op` is left as it is. Where the job has +// no credential, as the job of a pull request from a fork has none, a +// repo job reads nothing from the vault, says so in one line, a warning +// annotation of the run, and goes on at step 5 with the logins, which are +// its own token's: no server-actions key and no overlay join, so that the +// verbs run without the remote cache, except where the runner itself +// names cache servers and tokens, as a slot of a runner host does. A +// deploy job, and a repo job of the workflow on_push, which publishes +// images that carry the repo's one key, fail there in a line that names +// the credentials missing. +// 2. Read from the vault `-ci` what the job needs, every read through // the one function of core/vault.ts, which reaches the vault through the // runner's Connect server where the runner carries one and with the // service account otherwise, and masks each secret in the log as it @@ -22,33 +36,33 @@ // netbird`, also netbird/setup-key // A key pasted into a one-line field gets its lines back, and each of // them is masked as it is restored. -// 2. deploy: mint a token of the tenant's GitHub App that may read the +// 3. deploy: mint a token of the tenant's GitHub App that may read the // operations repo (`--operations-repo`, default `operations`) and // nothing else, and mask it. The App's key never leaves this process. // Where GitHub refuses the App (401) or finds it not installed on the // operations repo (404), the job fails in one line naming the App's // client ID and the repo. -// 3. Say how the vault was read. -// 4. repo: log the job in to the forge's image registry with its own token +// 4. Say how the vault was read. +// 5. repo: log the job in to the forge's image registry with its own token // (GITHUB_TOKEN as GITHUB_ACTOR), in a Docker config of the job's own // under RUNNER_TEMP, which buildx reads; with // `--private-packages`, to the forge's package registry too, in a // user-level npmrc there. Each file is 0600 in a directory 0700. // deploy: start an SSH agent on a socket under RUNNER_TEMP and add the // deploy key. -// 5. On a forge runner, where step 1 read a setup key: join the overlay as +// 6. On a forge runner, where step 2 read a setup key: join the overlay as // an ephemeral peer named `ci--`, and wait until the cache // host resolves (repo) or the network map names a peer (deploy, whose // key reaches the app hosts and not the cache host). A repo job that // cannot join goes on without the remote cache and warns on the run; a // deploy job fails. -// 6. Hand later steps what they need. As outputs of the step: +// 7. Hand later steps what they need. As outputs of the step: // repo server-actions-key, for the build and publish steps alone // deploy operations-token, for the checkout of the operations repo // both joined=true, where the job joined the overlay // As variables of the job (GITHUB_ENV): DOCKER_CONFIG and, with private // packages, NPM_CONFIG_USERCONFIG; PLX_TURBO_SHARED_TOKEN and -// PLX_TURBO_TRUSTED_TOKEN where step 1 read them; SSH_AUTH_SOCK. No +// PLX_TURBO_TRUSTED_TOKEN where step 2 read them; SSH_AUTH_SOCK. No // secret of the vault is a variable of the job but the two cache tokens. // // First of all, in the workflow on_push (the forge's GITHUB_WORKFLOW) on a @@ -58,19 +72,6 @@ // Under --dry-run it reads and masks, calls no API, and writes and starts // nothing. Exit code 1 where that refusal, a read, the token or a deploy job's join fails. // -// Where the job is handed no credential for the vault, as the job of a pull -// request from a fork is (the forge gives it no secrets), a repo job reads -// nothing from it, says so in one line, and goes on with the logins, which are -// its own token's: it gets no server-actions key and joins no overlay, so the -// verbs run without the remote cache. A deploy job, and a repo job of the -// workflow on_push, which publishes images that carry the repo's one key, -// fail there in a line that names the credentials missing. -// Where the job has a credential and the runner no `op`, as the forge's own -// runners have none, the command installs the pinned version into -// RUNNER_TEMP/op and reads with it, after verifying its signature; a runner -// that has `op` is left as it is (core/op.ts says how, and where the version -// is stated). -// // plx-ci setup repo --overlay netbird --private-packages // plx-ci setup deploy --overlay netbird import type { Command } from 'commander'; diff --git a/packages/cli-ci/src/core/op.ts b/packages/cli-ci/src/core/op.ts index fce7f994..4966ce73 100644 --- a/packages/cli-ci/src/core/op.ts +++ b/packages/cli-ci/src/core/op.ts @@ -1,5 +1,5 @@ // The vault's CLI, `op`, on a runner that has none: the forge's own runners -// carry no `op`, and a job with a token needs it to read the vault +// carry no `op`, and a job with a credential needs it to read the vault // (core/vault.ts). A runner that has one, as the environment image of a runner // host does, is left as it is. Otherwise `opOf` installs OP_VERSION into the // job's own temporary directory (RUNNER_TEMP/op, outside the checkout), as @@ -8,15 +8,23 @@ // cache.agilebits.com, which holds `op` and its detached signature `op.sig`, // and `gpg --verify op.sig op` against 1Password's signing key. The key is // fetched from 1Password's own site (the apt repository's key file, the same -// key) into a keyring made for the job, and it is the signature's primary key -// that must be the pinned fingerprint, so that a key the download served -// instead is no key of 1Password's. Nothing is run before it verifies; after -// it, `op --version` must say the version asked for. +// key), and it is the signature's primary key that must be the pinned +// fingerprint, so that a key the download served instead is no key of +// 1Password's. +// +// What is run before the signature verifies is only curl, unzip and gpg, and +// none of them can be made to run what the zip holds: the keyring is made and +// the key imported before the zip is opened; unzip takes the two members `op` +// and `op.sig` by name and nothing else, into a directory of their own; gpg +// runs with `--no-options`, which reads no configuration file, and +// `--no-autostart`, which starts no gpg-agent or dirmngr (neither is needed +// to import a public key or verify a signature); and none of the four, nor +// the installed op asked for its version, sees the vault's credentials +// (OP_*). `op` itself is run only after it verifies. // // The version is stated here and nowhere else in the code. The environment -// image pins the same version in its mise.toml (aqua:1password/cli), where -// the update bot can propose a new one; a test holds the two equal, so that -// such a pull request stays red until this constant moves with it. +// image pins the same version in its mise.toml (aqua:1password/cli); a person +// moves the two together, and a test holds them equal. import { join } from 'node:path'; import { type Context, run } from '@plexus-ms/lib'; @@ -30,13 +38,19 @@ const ARCHITECTURES: Record = { x64: 'amd64', arm64: 'arm64', ar /** * The command that runs the vault's CLI on this runner: `op` where the runner - * has it, else the verified install of OP_VERSION in `temp`, whose path. + * has it, else the verified install of OP_VERSION under `temp`, whose path. */ export async function opOf(ctx: Context, env: NodeJS.ProcessEnv, temp: string): Promise { - // Asked without the vault's credentials, which the question needs none of. - const asked = Object.fromEntries(Object.entries(env).filter(([name]) => !name.startsWith('OP_'))); + // Everything below is started without the vault's credentials, which none of it needs. + const clean = Object.fromEntries(Object.entries(env).filter(([name]) => !name.startsWith('OP_'))); + // A plan step's environment is laid over the process's, so there the credentials are blanked; a read's replaces it. + const blank = Object.fromEntries( + Object.keys(env) + .filter((name) => name.startsWith('OP_')) + .map((name) => [name, undefined]), + ); if ( - await ctx.read('op', ['--version'], { env: asked }).then( + await ctx.read('op', ['--version'], { env: clean }).then( () => true, () => false, ) @@ -45,42 +59,51 @@ export async function opOf(ctx: Context, env: NodeJS.ProcessEnv, temp: string): const architecture = ARCHITECTURES[process.arch]; if (!architecture) throw new Error(`no op for the architecture ${process.arch}`); const dir = join(temp, 'op'); - const [gnupg, zip, key, bin, sig] = ['gnupg', 'op.zip', 'key.asc', 'op', 'op.sig'].map((name) => join(dir, name)) as [ - string, - string, + const [gnupg, downloads, unpacked] = ['gnupg', 'download', 'bin'].map((name) => join(dir, name)) as [ string, string, string, ]; - ctx.say(`${ctx.dryRun ? '[dry-run] ' : ''}this runner has no op: installing ${OP_VERSION}, verified, into ${dir}`); - await ctx.apply([ - run('mkdir', ['-p', '-m', '700', dir]), - run('mkdir', ['-m', '700', gnupg]), - run('curl', [ - '-fsSL', - '--output', - zip, - `https://cache.agilebits.com/dist/1P/op2/pkg/v${OP_VERSION}/op_linux_${architecture}_v${OP_VERSION}.zip`, - ]), - run('curl', ['-fsSL', '--output', key, KEY_URL]), - run('unzip', ['-qo', zip, '-d', dir]), - ]); - if (ctx.dryRun) return 'op'; + const [zip, key] = [join(downloads, 'op.zip'), join(downloads, 'key.asc')]; + const [bin, sig] = [join(unpacked, 'op'), join(unpacked, 'op.sig')]; const what = `op ${OP_VERSION} from 1Password’s download`; - await ctx.read('gpg', ['--homedir', gnupg, '--batch', '--quiet', '--import', key]); - const status = await ctx - .read('gpg', ['--homedir', gnupg, '--batch', '--status-fd', '1', '--verify', sig, bin]) - .catch(() => { - throw new Error(`${what} did not pass its signature check, and is not run`); - }); - const signer = /^\[GNUPG:\] VALIDSIG .* ([0-9A-F]{40})$/m.exec(status)?.[1]; - if (signer !== OP_SIGNING_KEY) - throw new Error( - `${what} is not signed by 1Password’s key ${OP_SIGNING_KEY} but by ${signer ?? 'no key'}, and is not run`, - ); - await ctx.apply([run('chmod', ['700', bin])]); - const said = (await ctx.read(bin, ['--version'])).trim(); - if (said !== OP_VERSION) throw new Error(`${what} says it is ${said}, not ${OP_VERSION}`); + const gpg = ['--no-options', '--no-autostart', '--homedir', gnupg, '--batch']; + const download = (to: string, url: string) => + run('curl', ['-fsSL', '--proto', '=https', '--retry', '3', '--output', to, url], { env: blank }); + ctx.say(`${ctx.dryRun ? '[dry-run] ' : ''}this runner has no op: installing ${OP_VERSION}, verified, into ${dir}`); + try { + await ctx.apply([ + run('mkdir', ['-p', '-m', '700', dir], { env: blank }), + run('mkdir', ['-m', '700', gnupg, downloads, unpacked], { env: blank }), + download( + zip, + `https://cache.agilebits.com/dist/1P/op2/pkg/v${OP_VERSION}/op_linux_${architecture}_v${OP_VERSION}.zip`, + ), + download(key, KEY_URL), + ]); + if (ctx.dryRun) return 'op'; + await ctx.read('gpg', [...gpg, '--quiet', '--import', key], { env: clean }); + // Only the two members, whatever else the zip holds, into a directory that holds nothing else. + await ctx.apply([run('unzip', ['-qo', zip, 'op', 'op.sig', '-d', unpacked], { env: blank })]); + const status = await ctx + .read('gpg', [...gpg, '--status-fd', '1', '--verify', sig, bin], { env: clean }) + .catch(() => { + throw new Error(`${what} did not pass its signature check, and is not run`); + }); + const signer = /^\[GNUPG:\] VALIDSIG .* ([0-9A-F]{40})$/m.exec(status)?.[1]; + if (signer !== OP_SIGNING_KEY) + throw new Error( + `${what} is not signed by 1Password’s key ${OP_SIGNING_KEY} but by ${signer ?? 'no key'}, and is not run`, + ); + await ctx.apply([run('chmod', ['700', bin], { env: blank })]); + const said = (await ctx.read(bin, ['--version'], { env: clean })).trim(); + if (said !== OP_VERSION) throw new Error(`${what} says it is ${said}, not ${OP_VERSION}`); + } catch (error) { + const missing = error as { code?: string; path?: string }; + if (missing.code === 'ENOENT') + throw new Error(`cannot install op: ${missing.path ?? 'a tool'} is not on this runner`); + throw error; + } ctx.say(`installed op ${OP_VERSION}, signature verified`); return bin; } diff --git a/packages/cli-ci/src/core/setup.ts b/packages/cli-ci/src/core/setup.ts index 09c0f8e2..3fe96a87 100644 --- a/packages/cli-ci/src/core/setup.ts +++ b/packages/cli-ci/src/core/setup.ts @@ -65,10 +65,13 @@ function refuseOnPushWithoutEnvironment(env: NodeJS.ProcessEnv): void { /** * The vault this job reads, or undefined where it reads none: a repo job handed * no credential for it, as the job of a pull request from a fork is, since the - * forge gives such a job no secrets. It says so in one line and goes on with - * what needs none: the logins are the job's own token's, and without the key - * and the cache tokens the verbs run with a key of Next's own making and - * without the remote cache. A job with a credential reads with the runner's + * forge gives such a job no secrets. It says so in one line, a warning of the + * run so that a secret unset or misnamed in a pull request of the repo itself + * is seen, and goes on with what needs none: the logins are the job's own + * token's, and without the key the build runs with a key of Next's own + * making. Without the cache tokens it runs without the remote cache, but a + * slot hands its host's cache servers and tokens whatever the job has, and the + * line says so there. A job with a credential reads with the runner's * `op`, or with one installed for the job where the runner has none * (core/op.ts). A deploy job cannot deploy without the vault, and * a run of on_push publishes images of the repo's one key (§ 3.8): both fail @@ -89,8 +92,12 @@ async function vaultFor( throw new Error(`on_push publishes the repo’s images and cannot run without the vault; ${detailOf(error)}`); } } - ctx.say( - 'no token for the vault: reading nothing from it; the job runs without the server-actions key and without the remote cache', + // A slot hands its host's cache servers and their tokens whether or not the job has a credential for the vault. + const slotCaches = Boolean(env.PLX_TURBO_SHARED_TOKEN || env.PLX_TURBO_TRUSTED_TOKEN); + ctx.io.stdout.write( + `::warning::no token for the vault: reading nothing from it; the job runs without the server-actions key and ${ + slotCaches ? 'with the cache servers of this runner’s host' : 'without the remote cache' + }\n`, ); return undefined; } From 309af41a238dbbf42548f384b3984256495c0143 Mon Sep 17 00:00:00 2001 From: David Julian Albers Date: Thu, 3 Dec 2026 04:20:33 +0100 Subject: [PATCH 8/8] PLX-248: the pipeline newborn's stand-in op says its version plx-ci setup asks op --version before it installs one; the stand-in answered every call but read with an error, so setup took the runner for one without op and downloaded the vendor's. Co-Authored-By: Claude Sonnet --- newborns/pipeline.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/newborns/pipeline.test.ts b/newborns/pipeline.test.ts index 97523da9..b8ca53c3 100644 --- a/newborns/pipeline.test.ts +++ b/newborns/pipeline.test.ts @@ -7,7 +7,8 @@ // actions/checkout a fresh clone of the repo's origin at the pushed commit // jdx/mise-action mise install, and every later command through mise's environment // run the command, split on its spaces: plx-ci setup itself, with an `op` -// that reads the vault's items from this file, and the verbs +// that says its version, as plx-ci setup asks before it installs one, and +// reads the vault's items from this file, and the verbs // actions/upload-artifact the files its path finds in the checkout, as the artifact holds them // // A step's `if` and the expressions of its environment are the few the @@ -78,7 +79,7 @@ before(async () => { mkdirSync(bin); writeFileSync( join(bin, 'op'), - `#!/bin/sh\n[ "$1 $2" = "read op://acme-ci/repo_web/server-actions-key" ] && echo '${KEY}' && exit 0\necho "op: no item $2" >&2\nexit 1\n`, + `#!/bin/sh\n[ "$1" = "--version" ] && echo '2.42.0' && exit 0\n[ "$1 $2" = "read op://acme-ci/repo_web/server-actions-key" ] && echo '${KEY}' && exit 0\necho "op: no item $2" >&2\nexit 1\n`, ); chmodSync(join(bin, 'op'), 0o755); env = {