Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

16.0 KB
Raw Blame
/*
 * The boot script, which is the one piece of BoxLite that has to be right on a
 * machine nobody will log into.
 *
 * A runner is created once and its script is then ignored for the life of the
 * host, so a mistake here is not repaired by the next deploy. What is checked
 * is the shape a person cannot see by reading the string: that the checksum
 * gate is fatal, that the KVM check runs before anything installs, that the
 * unit's settings land somewhere systemd will read them, and that no secret is
 * written into a value the instance metadata exposes.
 */

import assert from 'node:assert/strict'
import test from 'node:test'
import {
  RUNNER_ENV_FILE,
  RUNNER_HOME,
  renderRunnerBoot,
  runnerApiUrl,
  type BootPlatform,
} from '../stack/runner-boot.ts'
import { renderUnitEnvironmentPolicyScripts } from '../stack/runner-upgrade.ts'

const platform = (overrides: Partial<BootPlatform> = {}): BootPlatform => ({
  hostAddress: 'HOST_IP=$(curl -s http://metadata/ip)',
  installVolumeMount: 'apt-get install -y some-volume-mount',
  prepareKvm: '',
  prepareHome: '',
  startWrapper: null,
  unitEnvironment: {},
  ...overrides,
})

const render = (overrides: Partial<Parameters<typeof renderRunnerBoot>[0]> = {}): string =>
  Buffer.from(
    renderRunnerBoot({
      apiUrl: 'https://api.dev.boxlite.ai/',
      otlpUrl: 'http://collector:4318',
      binary: {
        identity: '0.10.0',
        tarballUrl: 'https://example.invalid/runner-v0.10.0.tar.gz',
        checksumUrl: 'https://example.invalid/runner-v0.10.0.tar.gz.sha256',
        tarballName: 'runner-v0.10.0.tar.gz',
        transport: 'https',
      },
      port: 3003,
      environment: { BOXLITE_RUNNER_NAME: 'default' },
      platform: platform(),
      ...overrides,
    }),
    'base64',
  ).toString('utf8')

test('cloud boot installs the standalone kernel before starting the runner', () => {
  const script = render()
  const install = script.indexOf('\ninstall_runner_kernel\n')
  assert.ok(install >= 0, 'cloud boot did not install a kernel')
  assert.ok(install < script.indexOf('systemctl start boxlite-runner'))
  assert.match(script, /libkrunfw-kernel-dind-x86_64\.bin/)
})

test('cloud boot installs the NFS client filestore volumes mount with', () => {
  // filestore volumes run `mount -t nfs -o vers=4.1,hard` as root. nfs-common is
  // not in the Ubuntu cloud image's default package set, so the boot carries it:
  // a host without it fails the first filestore box with "unknown filesystem
  // type nfs", on an instance whose script nobody will read again.
  const script = render()
  const installs = script.search(/^apt-get install -y .*nfs-common.*$/m)
  assert.notEqual(installs, -1, 'the NFS client package is not installed')
  assert.ok(installs < script.indexOf('systemctl start boxlite-runner'))
})

test('the script fails fast, and logs where a person can find it', () => {
  const script = render()
  assert.match(script, /^#!\/bin\/bash/)
  assert.match(script, /exec > \/var\/log\/runner-setup\.log 2>&1/)
  assert.match(script, /set -euo pipefail/)
  const fails = script.indexOf('set -euo pipefail')
  const installs = script.indexOf('apt-get update')
  assert.ok(fails < installs, 'a half-finished bootstrap must not survive its first failure')
})

test('a checksum mismatch is fatal, and is checked before the binary is installed', () => {
  const script = render()
  const verifies = script.indexOf('runner checksum mismatch')
  const installs = script.indexOf('tar -xzf')
  assert.notEqual(verifies, -1, 'the digest has to be compared, not merely fetched')
  assert.ok(verifies < installs, 'it runs as root; verifying after installing verifies nothing')
  assert.match(script, /exit 1/)
  assert.match(script, /\[ "\$EXPECTED" = "\$ACTUAL" \]/, 'the manifest digest is compared to the bytes')
})

test('the manifest this host verifies against is the one it fetched beside the tarball', () => {
  // That the verification *works* is proved by running it — see
  // `runner-payload.test.ts`, which also covers the wrong-name and
  // uppercase-digest cases. What matters here is that a boot script renders it
  // at all, and against the file it just downloaded rather than some other one.
  const script = render()
  assert.match(script, /EXPECTED=\$\(awk '[^']*' < \/tmp\/boxlite-runner\.sha256\)/)
  assert.match(script, /ACTUAL=\$\(sha256sum < \/tmp\/boxlite-runner\.tar\.gz \|/)
})

test('the manifest is fetched beside the tarball, not derived on the host', () => {
  const script = render()
  assert.match(script, /runner-v0\.10\.0\.tar\.gz\.sha256/)
  const fetches = script.indexOf('runner-v0.10.0.tar.gz.sha256')
  const verifies = script.indexOf('EXPECTED=$(awk')
  assert.ok(fetches < verifies)
})

test('an s3 address is read with the host’s own role, and needs a region', () => {
  // A build-mode binary is staged rather than published, so nothing here is
  // fetched anonymously — and the region reaches a shell, so it is checked.
  const staged = {
    identity: '0.10.0',
    tarballUrl: 's3://boxlite-app-dev-artifacts/runner/abc/runner.tar.gz',
    checksumUrl: 's3://boxlite-app-dev-artifacts/runner/abc/runner.tar.gz.sha256',
    tarballName: 'runner.tar.gz',
    transport: 's3' as const,
  }
  assert.match(render({ binary: staged, region: 'ap-southeast-1' }), /aws .*s3 cp --region ap-southeast-1/)
  assert.throws(() => render({ binary: staged }), /needs the region that bucket lives in/)
})

test('a host without /dev/kvm refuses to finish rather than registering', () => {
  // A box is a microVM. A host that registers and cannot start one is a silent
  // failure; one that never registers is a visible one.
  const script = render()
  const checks = script.indexOf('/dev/kvm is absent')
  const starts = script.indexOf('systemctl start boxlite-runner')
  assert.notEqual(checks, -1)
  assert.ok(checks < starts)
})

test('the KVM hook runs before the check that depends on it', () => {
  // On GCP the device exists but is not readable by the account the runner runs
  // as, and preparing it after the check would fail a host that was fine.
  const script = render({ platform: platform({ prepareKvm: 'usermod -aG kvm root' }) })
  assert.ok(script.indexOf('usermod -aG kvm root') < script.indexOf('/dev/kvm is absent'))
})

test('the home is put in place before the unit is enabled', () => {
  // A unit started onto an empty directory writes box state to the boot disk,
  // where a mount made afterwards would hide it.
  const script = render({ platform: platform({ prepareHome: '# put the home in place' }) })
  const hook = script.indexOf('# put the home in place')
  assert.notEqual(hook, -1, 'the hook is not in the script')
  assert.ok(hook < script.indexOf('systemctl enable boxlite-runner'), 'the unit is enabled before its home is in place')
})

test('the unit will not start without the mount its home is on', () => {
  // A failed mount must stop the runner rather than leave it writing box state
  // to the disk beneath; where the home is on the root disk this names only `/`.
  const script = render()
  const unitStart = script.indexOf('[Unit]')
  const unit = script.slice(unitStart, script.indexOf('[Service]', unitStart))
  assert.match(unit, new RegExp(`^RequiresMountsFor=${RUNNER_HOME}$`, 'm'))
  assert.match(script, new RegExp(`^BOXLITE_HOME_DIR=${RUNNER_HOME}$`, 'm'))
})

test('the unit reads its settings from a file, not from lines appended after [Install]', () => {
  // The host's own address is not known until the script runs. Appending it to
  // the unit would land it inside `[Install]`, where systemd reads it as part
  // of that section and the runner never sees it.
  const script = render()
  assert.match(script, /EnvironmentFile=\/etc\/boxlite\/runner\.env/)
  assert.match(script, /printf 'RUNNER_DOMAIN=%s\\n' "\$HOST_IP" >> \/etc\/boxlite\/runner\.env/)
  const install = script.indexOf('[Install]')
  const appends = script.indexOf("printf 'RUNNER_DOMAIN")
  assert.ok(appends > install, 'the append is outside the unit file entirely, which is the point')
  assert.doesNotMatch(script, /Environment=RUNNER_DOMAIN/, 'never as a unit line')
})

test('the API URL is normalised once, here, rather than at every caller', () => {
  const script = render()
  assert.match(script, /BOXLITE_API_URL=https:\/\/api\.dev\.boxlite\.ai\/api$/m, 'one slash, not two')
})

test('a start wrapper replaces the binary as ExecStart, and only when there is one', () => {
  const without = render()
  assert.match(without, /ExecStart=\/usr\/local\/bin\/boxlite-runner$/m)

  const wrapped = render({
    platform: platform({
      startWrapper: { path: '/usr/local/bin/boxlite-runner-start.sh', script: '# fetches secrets' },
    }),
  })
  assert.match(wrapped, /ExecStart=\/usr\/local\/bin\/boxlite-runner-start\.sh$/m)
  assert.match(wrapped, /# fetches secrets/)
})

test('a secret delivered as an address stays an address, and is fetched rather than written', () => {
  // What runs on a runner is untrusted code by design, and instance metadata is
  // readable by all of it. Everything the store marks as an address therefore
  // reaches the unit through the wrapper's own fetch.
  //
  // The registration token is the one deliberate exception and is asserted
  // separately below — `stack/runner-boot.ts` records why it was accepted. This
  // test guards everything else, which is the part that must not drift.
  const script = render({
    environment: { BOXLITE_RUNNER_NAME: 'default', GHCR_TOKEN_ARN: 'arn:aws:secretsmanager:::secret:ghcr' },
    platform: platform({
      startWrapper: {
        path: '/usr/local/bin/boxlite-runner-start.sh',
        script: 'fetch GHCR_TOKEN "$GHCR_TOKEN_ARN"',
      },
    }),
  })
  assert.match(script, /GHCR_TOKEN_ARN=arn:aws:secretsmanager:::secret:ghcr/, 'the address may be written')
  assert.doesNotMatch(script, /^GHCR_TOKEN=/m, 'the value may not')
})

test('the registration token is written into the unit file, which is the accepted trade', () => {
  // Deliberate, and asserted so the decision is visible rather than implied by
  // its absence: pairing is token-based and the value has to be the one the API
  // seeded the row from, so it is delivered as a value. The cost — metadata is
  // readable on the host and off the API — is recorded in `runner-boot.ts`.
  //
  // Asserted here because a provider that quietly stopped delivering it is the
  // failure that crash-loops a host, and the one nothing else would catch.
  const script = render({ environment: { BOXLITE_RUNNER_NAME: 'default', BOXLITE_RUNNER_TOKEN: 'host-token' } })
  assert.match(script, /^BOXLITE_RUNNER_TOKEN=host-token$/m)
  // Inside the heredoc that writes the EnvironmentFile, which is the only place
  // systemd will read it from. Anchored on the heredoc itself rather than on
  // `[Install]`: the script mentions that section in a comment long before the
  // unit exists, so an index comparison against it proves nothing.
  const opens = script.indexOf("runner.env << 'RUNNERENV'")
  const closes = script.indexOf('RUNNERENV', opens + "runner.env << 'RUNNERENV'".length)
  const wrote = script.indexOf('BOXLITE_RUNNER_TOKEN=host-token')
  assert.ok(opens !== -1 && closes !== -1)
  assert.ok(wrote > opens && wrote < closes, 'it lands inside the EnvironmentFile, not loose in the script')
})

test('the platform’s own unit settings win over what the caller passed', () => {
  // A cloud names things only it can name — a region, a project — and a caller
  // that happened to set the same key must not silently retarget the host.
  const script = render({
    environment: { AWS_REGION: 'us-east-1' },
    platform: platform({ unitEnvironment: { AWS_REGION: 'ap-southeast-1' } }),
  })
  assert.match(script, /^AWS_REGION=ap-southeast-1$/m)
  assert.doesNotMatch(script, /^AWS_REGION=us-east-1$/m)
})

test('the policy compares against the exact line the boot script wrote', () => {
  /*
   * The pairing that keeps a converged fleet from reading as non-compliant
   * forever. `renderUnitEnvironmentPolicyScripts` greps the unit environment with
   * `grep -qxF` — a whole-line, fixed-string match — so a boot script that
   * writes the address even slightly differently (a trailing slash kept, a
   * quote added) makes every host fail `validate`, enforce on every cycle, and
   * restart its boxes each time while nothing ever converges.
   *
   * Both strings come from production code and neither is spelled here, which
   * is the point: the test would not survive the two deriving it separately.
   */
  const apiUrl = 'https://api.boxlite.ai/'
  // Carried verbatim where the address is not, so the two sides have one more
  // way to disagree: a normalization added to either alone breaks the pairing.
  const otlpUrl = 'http://collector:4318'
  const volumeBackend = 'gcs'
  const script = render({
    apiUrl,
    otlpUrl,
    platform: platform({ unitEnvironment: { VOLUME_STORAGE_BACKEND: volumeBackend } }),
  })
  const { validate } = renderUnitEnvironmentPolicyScripts({ apiUrl, otlpUrl, volumeBackend })

  // The array the policy compares against, and only it: the block around it
  // carries a `printf '%s\\n'` that a looser read would pick up as a pinned line.
  const declared = /UNIT_ENV_EXPECTED=\(([^)]*)\)/.exec(validate)?.[1] ?? ''
  const pinned = [...declared.matchAll(/'([^']+)'/g)].map((match) => match[1])
  assert.deepEqual(
    pinned,
    [
      `BOXLITE_API_URL=${runnerApiUrl(apiUrl)}`,
      `OTEL_EXPORTER_OTLP_ENDPOINT=${otlpUrl}`,
      `VOLUME_STORAGE_BACKEND=${volumeBackend}`,
    ],
    'the policy pins lines the boot script does not write',
  )

  // The boot script writes each as its own line, which is what `grep -qxF` needs.
  const lines = script.split('\n')
  for (const line of pinned) {
    assert.ok(lines.includes(line), `the boot script writes no line equal to ${JSON.stringify(line)}`)
  }
  // And both name the same file, or the policy converges something else. The
  // policy reaches it through an override that nothing sets outside a test, so
  // what has to match is the default behind it.
  assert.ok(validate.includes(`UNIT_ENV_FILE="\${BOXLITE_RUNNER_ENV_FILE:-${RUNNER_ENV_FILE}}"`))
  assert.ok(script.includes(RUNNER_ENV_FILE))
})

test('boot expands the runner map as quoted environment, never the wrapper itself', () => {
  const script = render({ environment: { BOXLITE_RUNNER_ENV: '{"BOXLITE_EXPERIMENTAL":"nested-virtualization"}' } })
  assert.match(script, /^BOXLITE_EXPERIMENTAL="nested-virtualization"$/m)
  assert.doesNotMatch(script, /^KRUN_USE_THP=/m)
  assert.doesNotMatch(script, /^BOXLITE_RUNNER_ENV=/m)
})

test('boot omits THP setup unless prepareThp is supplied, and runs prepareThp before starting the runner', () => {
  const without = render()
  assert.doesNotMatch(without, /install_runner_thp/)
  assert.doesNotMatch(without, /boxlite-thp\.conf/)

  const withThp = render({
    platform: platform({ prepareThp: '# configure host thp\ninstall_runner_thp' }),
  })
  const installThp = withThp.indexOf('\ninstall_runner_thp\n')
  assert.ok(installThp >= 0, 'boot did not include prepareThp hook')
  assert.ok(installThp < withThp.indexOf('systemctl start boxlite-runner'))
})

test('boot writes KRUN_USE_THP="1" only when BOXLITE_RUNNER_ENV sets it', () => {
  const script = render({ environment: { BOXLITE_RUNNER_ENV: '{"KRUN_USE_THP":"1"}' } })
  assert.match(script, /^KRUN_USE_THP="1"$/m)
})

test('invalid maps and deployment-owned keys fail before rendering boot or policy', () => {
  const invalid = ['null', '[]', '{', '{"X":false}', '{"BAD-KEY":"v"}', '{"X":"line\\nfeed"}', '{"X":"a\\u0000b"}']
  for (const key of [
    'BOXLITE_RUNNER_TOKEN',
    'BOXLITE_API_URL',
    'OTEL_LOGGING_ENABLED',
    'OTEL_TRACING_ENABLED',
    'BOXLITE_KERNEL_PATH',
    'PATH',
    'BOXLITE_RUNNER_ENV',
  ]) {
    invalid.push(JSON.stringify({ [key]: 'override' }))
  }
  for (const runnerEnv of invalid) {
    assert.throws(() => render({ environment: { BOXLITE_RUNNER_ENV: runnerEnv } }), /BOXLITE_RUNNER_ENV/)
    assert.throws(
      () => renderUnitEnvironmentPolicyScripts({ apiUrl: 'https://api.example.test', ...{ runnerEnv } }),
      /BOXLITE_RUNNER_ENV/,
    )
  }
  assert.throws(
    () => render({ environment: { DIRECT: 'existing', BOXLITE_RUNNER_ENV: '{"DIRECT":"override"}' } }),
    /BOXLITE_RUNNER_ENV/,
  )
})