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
35 changes: 28 additions & 7 deletions box-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
# and the target ships the image once the test has passed.
#
# boxes-setup seeds a new home once from /usr/local/share/boxes/home and does
# not refresh it. /home/agent in the image is an empty mount point. Thus a
# tool that must follow the image is installed outside the home.
# not refresh it. /home/agent in the image is a mount point, and its only
# entry is the link of the trust store. Thus a tool that must follow the
# image is installed outside the home.
#
# The build context is the repository root, because the file service is
# built from files/ and shared/.
Expand Down Expand Up @@ -242,9 +243,18 @@ RUN mkdir -p /usr/local/lib/boxes/setup.d

# The root filesystem is read-only at runtime, so every mount point must
# exist in the image. /workspace, /home/agent and /nix are writable binds, and
# /tmp is a tmpfs. The home stays empty, because the mount hides it in a box.
RUN mkdir -p /workspace /nix /home/agent \
&& chown agent:agent /workspace /nix /home/agent
# /tmp is a tmpfs. The mount hides the home in a box, so the only entry of
# the home is the link of the trust store.
#
# The trust store /etc/ssl/certs links to ~/.boxes/certs, and the image points
# that link at the system certificates in /etc/ssl/certs-system. So a build
# step and update-ca-certificates use the system certificates. In a box,
# boxes-setup points the link at a copy with the deployment CA added.
RUN mkdir -p /workspace /nix /home/agent/.boxes \
&& mv /etc/ssl/certs /etc/ssl/certs-system \
&& ln -s /etc/ssl/certs-system /home/agent/.boxes/certs \
&& ln -s /home/agent/.boxes/certs /etc/ssl/certs \
&& chown -h agent:agent /workspace /nix /home/agent /home/agent/.boxes /home/agent/.boxes/certs

# The files of a new home. boxes-setup copies them into a home that has no
# seed marker, and overwrites nothing. They belong to root and are readable
Expand Down Expand Up @@ -300,7 +310,9 @@ RUN test "$(id -u)" = "${AGENT_UID}" \
&& test "$(id -g)" = "${AGENT_GID}" \
&& test "$(id -un)" = "agent" \
&& test -w /workspace && test -w /home/agent && test -w /nix \
&& test -z "$(ls -A /home/agent)" \
&& test "$(ls -A /home/agent)" = .boxes && test "$(ls -A /home/agent/.boxes)" = certs \
&& test "$(readlink /home/agent/.boxes/certs)" = /etc/ssl/certs-system \
&& test -f /etc/ssl/certs/ca-certificates.crt \
&& test "$(command -v boxes-setup)" = "/usr/local/bin/boxes-setup" \
&& boxes-setup \
&& test -f /home/agent/.boxes/seeded \
Expand Down Expand Up @@ -329,6 +341,15 @@ RUN test "$(id -u)" = "${AGENT_UID}" \
case ":$PATH:" in *":$d:"*) ;; *) echo "a login shell lost $d" >&2; exit 1 ;; esac; \
done' "$PATH"

# The setup adds a proxy CA to the trust store. A client that reads only the
# bundle file and a client that reads only the hash links both trust it.
RUN openssl req -x509 -newkey rsa:2048 -nodes -subj '/CN=Boxes test CA' -days 1 \
-keyout /tmp/test-ca.key -out /home/agent/.boxes/proxy-ca.crt 2>/dev/null \
&& boxes-setup \
&& openssl verify -no-CApath -CAfile /etc/ssl/certs/ca-certificates.crt /home/agent/.boxes/proxy-ca.crt \
&& openssl verify -no-CAfile -CApath /etc/ssl/certs /home/agent/.boxes/proxy-ca.crt \
&& test "$(find /home/agent/.boxes -maxdepth 1 -name 'certs.*' | wc -l)" = 1

# Node has the requested major and comes from NodeSource. The vendor check
# catches apt preferring Ubuntu's nodejs.
RUN test "$(node -p 'process.versions.node.split(".")[0]')" = "${NODE_MAJOR}" \
Expand Down Expand Up @@ -562,7 +583,7 @@ ARG NIX_VERSION
RUN test "$(id -u)" = "${AGENT_UID}" \
&& test "$(id -un)" = "agent" \
&& test "$HOME" = "/home/agent" \
&& test -z "$(ls -A /home/agent)" \
&& test "$(ls -A /home/agent)" = .boxes && test "$(ls -A /home/agent/.boxes)" = certs \
&& test "$(command -v nix)" = "/usr/local/bin/nix" \
&& nix --version | grep -qF "${NIX_VERSION}" \
&& boxes-setup \
Expand Down
8 changes: 8 additions & 0 deletions box-image/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
#!/usr/bin/env bash
# Box container entrypoint. It holds the container open.

# /etc/ssl/certs links to ~/.boxes/certs. The empty home of a new box or of a
# login has no such link, so the system certificates fill in until boxes-setup
# writes the link.
if [ ! -e /home/agent/.boxes/certs ]; then
mkdir -p /home/agent/.boxes && ln -sfn /etc/ssl/certs-system /home/agent/.boxes/certs
fi

exec sleep infinity
47 changes: 34 additions & 13 deletions box-image/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,43 @@ if [ -n "${TMPDIR:-}" ]; then
fi
fi

# --- the CA bundle -------------------------------------------------------------
# The system authorities and the deployment CA together. The orchestrator
# writes the CA to ~/.boxes/proxy-ca.crt and points the CA env vars at the
# bundle. Some tools, such as git-lfs, trust only the file they are pointed
# at, so it must hold every authority. Written even without a CA, because
# nix.conf of the full image names the file.
# --- the trust store -----------------------------------------------------------
# /etc/ssl/certs links to ~/.boxes/certs. The setup points that link at a new
# copy of the system certificates with the deployment CA, which the
# orchestrator writes to ~/.boxes/proxy-ca.crt. A new name for each copy lets
# one rename switch the link. ca-bundle.crt links to the bundle file, because
# the CA env vars and nix.conf of the full image name it.

# Adds the deployment CA to the bundle file and the hash links of a copy of
# the trust store, so clients that read either one trust it.
#
# $1 is the directory of the copy. Returns non-zero when a step fails.
add_proxy_ca() {
local hash
hash=$(openssl x509 -noout -subject_hash -in "$proxy_ca") \
&& cp "$proxy_ca" "$1/boxes-proxy-ca.pem" \
&& cat "$proxy_ca" >> "$1/ca-certificates.crt" \
&& ln -s boxes-proxy-ca.pem "$1/$hash.0"
}

proxy_ca=/home/agent/.boxes/proxy-ca.crt
bundle=/home/agent/.boxes/ca-bundle.crt
certs=/home/agent/.boxes/certs
copy=
if mkdir -p /home/agent/.boxes \
&& cat /etc/ssl/certs/ca-certificates.crt > "$bundle.tmp" \
&& { [ ! -f "$proxy_ca" ] || cat "$proxy_ca" >> "$bundle.tmp"; } \
&& mv "$bundle.tmp" "$bundle"; then
log "wrote the CA bundle to $bundle"
&& copy=$(mktemp -d "$certs.XXXXXX") \
&& chmod 0755 "$copy" \
&& cp -a /etc/ssl/certs-system/. "$copy/" \
&& { [ ! -f "$proxy_ca" ] || add_proxy_ca "$copy"; } \
&& ln -sfn certs/ca-certificates.crt "$certs.new" \
&& mv -T "$certs.new" /home/agent/.boxes/ca-bundle.crt \
&& ln -sfn "$(basename "$copy")" "$certs.new" \
&& mv -T "$certs.new" "$certs"; then
find /home/agent/.boxes -maxdepth 1 -name 'certs.*' ! -name "$(basename "$copy")" -exec rm -rf {} +
log "wrote the trust store to $copy"
else
rm -f "$bundle.tmp"
fail "could not write $bundle; TLS would fail for nix, git, curl and other tools"
[ -z "$copy" ] || rm -rf "$copy"
rm -f "$certs.new"
fail "could not write the trust store to $certs; TLS would fail for nix, git, curl and other tools"
fi

# --- directories an agent needs to find already there ------------------------
Expand Down
4 changes: 2 additions & 2 deletions doc/boxes.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ file operations wait for it. When a step fails, the box gets the status `error`
2. It runs `boxes-setup`, a shell script of the base image at `/usr/local/bin/boxes-setup` (source
`box-image/setup.sh`), with a limit of 60 seconds. The script first seeds a new home with the files of
`/usr/local/share/boxes/home`, once: the marker `~/.boxes/seeded` records the seed, and the copy overwrites no file.
Then it prepares the home for the tools of the base image: it empties `TMPDIR`, builds the CA bundle, and sets the
credential helpers of `gh` and `glab`. Then it runs
Then it prepares the home for the tools of the base image: it empties `TMPDIR`, writes the trust store of the box,
and sets the credential helpers of `gh` and `glab`. Then it runs
the [setup steps](custom-images.md#setup-steps) of the image in `/usr/local/lib/boxes/setup.d/`. The steps of the
full image (source `box-image/setup.d/`) check `/nix`, add the CA to the trust store of Chromium, and link the
browsers.
Expand Down
4 changes: 2 additions & 2 deletions doc/custom-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ executable file in `/usr/local/lib/boxes/setup.d/` in name order. Use a number p
- The setup and all steps together have a limit of 60 seconds.

When a step runs, the certificate of the egress proxy CA is at `~/.boxes/proxy-ca.crt` (only when the deployment has
one), and `~/.boxes/ca-bundle.crt` holds the certificates of the system authorities and of that CA. The environment has
the proxy variables (`HTTPS_PROXY`, `NO_PROXY`).
one). `/etc/ssl/certs` links to a copy of the system trust store `/etc/ssl/certs-system` with that CA added. The
environment has the proxy variables (`HTTPS_PROXY`, `NO_PROXY`).

## Environment variables

Expand Down
39 changes: 35 additions & 4 deletions doc/egress.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ The orchestrator creates a certificate authority once and stores it in the [data
certificate and the key to the proxy with the rules. The proxy then creates a certificate for each host that it
intercepts.

The orchestrator also writes the certificate to `~/.boxes/proxy-ca.crt` in each box, and the
[box setup](boxes.md#the-setup) writes it together with the system certificates to `~/.boxes/ca-bundle.crt`. The
environment variables `NODE_EXTRA_CA_CERTS`, `SSL_CERT_FILE`, `GIT_SSL_CAINFO` and `CURL_CA_BUNDLE` contain the path
of that bundle. A tool that uses none of these variables fails on the intercepted hosts, and works on all other hosts.
The orchestrator also writes the certificate to `~/.boxes/proxy-ca.crt` in each box. The
[box setup](boxes.md#the-setup) writes a trust store with the system certificates and that certificate to the home,
and `/etc/ssl/certs` links to it. So every tool that uses `/etc/ssl/certs` accepts the certificates of the proxy. The
environment variables `NODE_EXTRA_CA_CERTS`, `SSL_CERT_FILE`, `GIT_SSL_CAINFO` and `CURL_CA_BUNDLE` contain the path of
the bundle file of the trust store, for tools such as Node that use their own list of certificate authorities. A tool
that uses only its own list fails on the intercepted hosts, and works on all other hosts.

## Settings

Expand Down Expand Up @@ -261,3 +263,32 @@ of every [user](users.md), so one host can have more than one credential. The pr
placeholder is in the request. In the policy, the id of a credential has its owner as a prefix, for example
`alice/claude`, because the ids must be unique. A credential of the empty user has no prefix. The credential of a secret
of an MCP server names only the header of the secret, and its id is `<owner>/mcp/<set>/<server>/<header>`.

### Trust store of a box

The root file system of a box is read-only, so the box image makes `/etc/ssl/certs` a link to `~/.boxes/certs`. The
system trust store of the image is in `/etc/ssl/certs-system`. In the image, `~/.boxes/certs` links back to the system
trust store, so a build step and `update-ca-certificates` use it. The mount of the home hides that link. So the
entrypoint of the image writes the same link into a home that has none, for example in a new box or a login container.

`boxes-setup` (`box-image/setup.sh`) copies the system trust store to a new directory `~/.boxes/certs.<random>`. It adds
the CA to the bundle file of the copy and as a hash link, because some tools read only one of them. Then it points
`~/.boxes/certs` at the copy and deletes the older copies. `~/.boxes/ca-bundle.crt` links to the bundle file of the
copy.

In the diagram, a solid arrow is a link, and a dotted arrow is a step of the setup.

```mermaid
flowchart TD
subgraph before["image, login container, new home"]
etc1["/etc/ssl/certs/"] --> home1["~/.boxes/certs/"]
home1 --> sys1["/etc/ssl/certs-system/"]
end

subgraph after["box after the setup"]
etc2["/etc/ssl/certs/"] --> home2["~/.boxes/certs/"]
home2 --> copy["~/.boxes/certs.&lt;random&gt;/"]
sys2["/etc/ssl/certs-system/"] -. "copy" .-> copy
ca["~/.boxes/proxy-ca.crt"] -. "add" .-> copy
end
```
9 changes: 4 additions & 5 deletions orchestrator/src/boxenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { HARNESSES } from './harness.ts';
import { HOME_DIR } from './runtime/runtime.ts';

/**
* The path of the CA bundle. The box setup writes the system authorities and
* the deployment CA to it, and the CA variables point to it.
* The path of the CA bundle. It links to the bundle file of the trust store
* that the box setup writes with the system certificates and the deployment CA.
*/
const CA_PATH = `${HOME_DIR}/.boxes/ca-bundle.crt`;

Expand Down Expand Up @@ -80,9 +80,8 @@ export function boxEnv(inputs: BoxEnvInputs): Record<string, string> {
env.no_proxy = 'localhost,127.0.0.1';

if (inputs.hasCa) {
// The box setup writes the system authorities and the CA to CA_PATH. The
// variables below point node, gh, git, git-lfs, curl and Codex to it. A tool
// that reads none of them fails TLS only for the intercepted hosts.
// The variables below point node, gh, git, git-lfs, curl and Codex to
// CA_PATH. A tool that ignores /etc/ssl/certs, such as node, needs them.
env.NODE_EXTRA_CA_CERTS = CA_PATH;
env.SSL_CERT_FILE = CA_PATH;
env.GIT_SSL_CAINFO = CA_PATH;
Expand Down
Loading