Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ansible/ci/roles/actions_runners/files/ci-runner/mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
Expand Down
5 changes: 3 additions & 2 deletions newborns/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down
9 changes: 5 additions & 4 deletions packages/cli-ci/src/commands/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof runJob>['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_'))
Expand Down Expand Up @@ -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/);
Expand Down Expand Up @@ -264,7 +264,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], [{}, {}]);
});
Expand Down
30 changes: 22 additions & 8 deletions packages/cli-ci/src/commands/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,21 @@
// forge's GITHUB_WORKFLOW) on a branch that deploys nowhere, or on a tag,
// as a run started by hand can be: the command fails in one line naming
// the branch and the branches that deploy.
// 2. Read from the vault `<owner>-ci` what the job needs, every read through
// 2. 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 6 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.
// 3. Read from the vault `<owner>-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
Expand All @@ -26,27 +40,27 @@
// 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.
// 3. deploy: mint a token of the tenant's GitHub App that may read the
// 4. 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.
// 4. Say how the vault was read.
// 5. repo: log the job in to the forge's image registry with its own token
// 5. Say how the vault was read.
// 6. 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.
// 6. On a forge runner, where step 2 read a setup key: join the overlay as
// 7. On a forge runner, where step 3 read a setup key: join the overlay as
// an ephemeral peer named `ci-<run id>-<job>`, 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.
// 7. repo: find the trusted cache server turbow would use for build, e2e and
// 8. repo: find the trusted cache server turbow would use for build, e2e and
// publish (PLX_TURBO_TRUSTED, else port 8586 of the cache host, which a
// forge runner reaches only where it joined) and ask it once, with the
// trusted token, as turbow does. Where none is named, the forge runner
Expand All @@ -57,13 +71,13 @@
// image is made from. Asked once, so that every step keeps one answer.
// Where the server answers, or is named without a token (which turbow
// fails the verb for), nothing is handed and turbow works as ever.
// 8. Hand later steps what they need. As outputs of the step:
// 9. 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 2 read them; SSH_AUTH_SOCK. No
// PLX_TURBO_TRUSTED_TOKEN where step 3 read them; SSH_AUTH_SOCK. No
// secret of the vault is a variable of the job but the two cache tokens.
//
// Under --dry-run it reads and masks, calls no API, and writes and starts
Expand Down
232 changes: 232 additions & 0 deletions packages/cli-ci/src/commands/setupOp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
// 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 { 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';
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<string, string>)[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', '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'));
});

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', '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(
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', 'bin', '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'));
});

/** 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), []);
}
});
Loading
Loading