# Debug a Running Enclave

Diagnose and fix issues with your running enclave deployments.

## Before you start

Make sure:

- Your app is deployed.
- You can redeploy after enabling the `debug` block or adding `ssh_keys`.
- You know the public IP address of the EC2 instance running the enclave. `caution apps get -d <deployment>` shows it for each deployment.
- SSH access is to the **host** EC2 instance, not the enclave itself.

> [!TIP]
>
> **Using an AI coding agent?**
>
> Install the [`caution-platform` skill](<https://docs.caution.co/guides/build-with-an-ai-agent/index.md>) so Claude Code or Codex can help debug local QEMU runs and Nitro deployments.

## Enable debug access

Debugging usually requires two separate settings in the enclave's `debug` block: `enabled = true` to read enclave console output, and `ssh_keys` to access the host EC2 instance.

> [!WARNING]
>
> **Do not use debug access in production**
>
> While debug access is enabled, do not send production traffic to the deployment, do not rely on PCR values from that enclave, and do not leave port 22 open longer than needed.

### Enable console output

Add a `debug` block with `enabled = true` to your `caution.hcl` to make the enclave console readable:

```hcl
enclave "main" {
  debug {
    enabled = true
  }
  unit "default" {
    command = "/app/server"
  }
}
```

Debug mode passes `--debug-mode` to `nitro-cli run-enclave`, which allows you to read the enclave's console output. The tradeoff is that AWS zeros out all PCR values in debug mode, so `caution verify` will refuse to attest the enclave. **Do not use debug mode in production.**

Redeploy after changing the `debug` block. On the `deployments` lifecycle, a debug candidate does not alter an existing live deployment. If nothing is live, however, successful startup can activate the debug deployment automatically. Use a separate test app or otherwise isolate it from production traffic.

### Add SSH access to the host

To SSH into the EC2 instance running the enclave, add your public key to the `ssh_keys` list:

```hcl
debug {
  enabled  = true
  ssh_keys = ["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExample... user@host"]
}
```

Each key must be a full OpenSSH public key string starting with `ssh-ed25519`, `ssh-rsa`, `ecdsa-sha2-nistp256`, or similar. Add multiple keys as list entries:

```hcl
ssh_keys = [
  "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFirst... user1@host",
  "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAISecond... user2@host",
]
```

Adding `ssh_keys` automatically opens port 22 in the instance's security group.

> [!CAUTION]
>
> **Do not leave SSH enabled in production**
>
> SSH access and debug mode should only be used during development and testing. Remove the `debug` block for new production deployments and explicitly destroy retained debug deployments afterward. Editing the file does not close SSH or disable debug mode on existing instances. Leaving SSH open exposes port 22 to the internet, and debug mode disables attestation verification.

## Connect to the host

After redeploying with `ssh_keys`, connect to the EC2 instance:

```bash
ssh ec2-user@<instance-ip>
```

> [!NOTE]
>
> **Host access only**
>
> SSH access is to the **host** EC2 instance, not the enclave itself. The enclave runs in an isolated VM. From the host you can inspect the enclave's state using `nitro-cli` commands.

## Inspect enclave startup

From the SSH session, check whether the enclave is running and inspect startup logs:

```bash
# Find the enclave ID
ENCLAVE_ID=$(sudo nitro-cli describe-enclaves | grep -o '"EnclaveID": "[^"]*"' | cut -d'"' -f4 | head -1)

# Is the enclave running?
sudo nitro-cli describe-enclaves

# Enclave service logs
sudo journalctl -u nitro-enclave.service --no-pager -n 100

# Direct-mode app-port proxy (intentionally absent for STEVE and Attested TLS)
sudo systemctl status vsock-proxy-3000.service

# Outbound network proxy (only when outbound egress is enabled)
sudo systemctl status vsock-network.service

# TLS termination
sudo systemctl status caddy.service
sudo journalctl -u caddy.service --no-pager -n 50

# Nitro CLI logs (enclave startup errors, resource allocation failures)
sudo cat /var/log/nitro_enclaves/nitro_enclaves.log

# Full boot log (instance provisioning, EIF download, service setup)
sudo cat /var/log/user-data.log
```

> [!NOTE]
>
> **AWS Nitro service names**
>
> These service names apply to Caution deployments on AWS Nitro Enclaves.
>
> For a STEVE deployment, the application HTTP port is intentionally enclave-local and has no `vsock-proxy-<app-port>.service`. Check `vsock-proxy-49500.service` and Caddy instead. Attested TLS similarly keeps the application port inside the enclave and forwards port `443`.
>
> With Attested TLS, host Caddy handles HTTP routing, not TLS certificate issuance. Certificate issuance runs inside the enclave. Enclave console output is available only in debug mode; do not weaken a production deployment merely to collect logs.

## Read enclave console output

If debug mode is enabled, stream the enclave's stdout/stderr:

```bash
sudo nitro-cli console --enclave-id "$ENCLAVE_ID"
```

This only works when `debug { enabled = true }` is set in `caution.hcl`. In production mode, the console is not accessible.

## Inspect network and proxy services

Check the host-side services that connect traffic to the enclave:

```bash
# Direct-mode app-port proxy (absent for STEVE and Attested TLS)
sudo systemctl status vsock-proxy-3000.service

# Outbound network proxy (only when outbound egress is enabled)
sudo systemctl status vsock-network.service

# TLS termination
sudo systemctl status caddy.service
sudo journalctl -u caddy.service --no-pager -n 50
```

These examples use port `3000`. For direct ingress, replace it with the configured application port. For STEVE, check `vsock-proxy-49500.service`; absence of an application-port proxy is expected.

For HCL deployments, the outbound proxy is installed only when `network` declares at least one `egress` rule. If egress is omitted, `vsock-network.service` is intentionally absent; a missing unit alone is not an enclave startup failure.

## Clean up debug access

Before returning the app to production, remove the `debug` block from `caution.hcl`:

```hcl
# Remove the entire debug { } block, including ssh_keys
```

Commit the production-mode configuration and deploy it. On the `deployments` lifecycle, verify the new deployment, release any required secret shares, and activate it if the old deployment is still live:

```bash
caution verify -d <production-deployment>
# Only if encrypted secrets are configured:
caution secret send-shard -d <production-deployment>
# Only if the new deployment is not already live:
caution apps switch -d <production-deployment>
```

Check production behavior, then explicitly remove each retained debug deployment:

```bash
caution apps destroy -d <debug-deployment>
```

Confirm cleanup completed. Removing the `debug` block and pushing alone leaves existing debug/SSH-enabled instances unchanged. Legacy apps replace the deployment; verify the replacement and confirm the old debug instance is gone there too. Verification must target a production-mode enclave: debug mode zeros out PCR values.

## Test locally with QEMU

For a faster debugging loop, you can boot the same kernel and initramfs inputs locally with QEMU instead of deploying to AWS each time. First, build the enclave image:

```bash
caution apps build
```

When testing uncommitted application or Containerfile changes in a Git checkout, use `caution apps build --no-cache`. With caching enabled, the CLI can reuse an application image tagged from the current Git `HEAD` without checking working-tree changes, so a plain rebuild can keep testing an older image.

This outputs the EIF path and a build directory you can inspect. The build directory contains an `eif-stage/output/` directory with:

```text
enclave.eif
enclave.pcrs
rootfs.cpio.gz
```

Use `rootfs.cpio.gz` as the QEMU initrd. Do not pass the EIF itself to `-initrd`; the EIF is the Nitro package produced by `eif_build`, while QEMU expects a raw initramfs.

Two kernel options are available depending on what you need to test.

### Logs only (Nitro kernel)

The Nitro kernel comes from the pinned StageX `linux-nitro` image used by the generated `Containerfile.eif`:

```dockerfile
FROM stagex/user-linux-nitro@sha256:aa1006d91a7265b33b86160031daad2fdf54ec2663ed5ccbd312567cc9beff2c AS linux-nitro
COPY --from=linux-nitro /bzImage /build/kernel/bzImage
COPY --from=linux-nitro /linux.config /build/kernel/linux.config
```

Extract it:

```bash
mkdir -p /tmp/caution-linux-nitro
docker create --name caution-linux-nitro \
  stagex/user-linux-nitro@sha256:aa1006d91a7265b33b86160031daad2fdf54ec2663ed5ccbd312567cc9beff2c

docker cp caution-linux-nitro:/bzImage /tmp/caution-linux-nitro/bzImage
docker cp caution-linux-nitro:/linux.config /tmp/caution-linux-nitro/linux.config
docker rm caution-linux-nitro
```

Boot:

```bash
qemu-system-x86_64 \
  -m 512M \
  -nographic \
  -kernel /tmp/caution-linux-nitro/bzImage \
  -initrd /path/to/eif-stage/output/rootfs.cpio.gz \
  -append "console=ttyS0 reboot=k panic=1 nomodules nit.target=/run.sh"
```

This shows boot output and application logs. Networking will not work — the Nitro kernel has no virtio-net or vsock drivers.

### With networking (standard kernel)

To expose ports and test HTTP endpoints, use a standard x86\_64 kernel:

```bash
docker run --rm --platform linux/amd64 \
  -v /tmp/caution-linux-nitro:/out ubuntu:24.04 \
  bash -c "apt-get update -q && apt-get install -y linux-image-generic \
    && chmod 644 /boot/vmlinuz-* \
    && cp /boot/vmlinuz-*-generic /out/vmlinuz-amd64"
```

Boot with port forwarding — adjust ports to match your `caution.hcl`:

```bash
qemu-system-x86_64 \
  -m 512M \
  -nographic \
  -kernel /tmp/caution-linux-nitro/vmlinuz-amd64 \
  -initrd /path/to/eif-stage/output/rootfs.cpio.gz \
  -append "console=ttyS0 reboot=k panic=1 nomodules nit.target=/run.sh" \
  -netdev user,id=net0,hostfwd=tcp:0.0.0.0:8083-:8083,hostfwd=tcp:0.0.0.0:49502-:49502 \
  -device virtio-net-pci,netdev=net0
```

```bash
# App
curl http://localhost:8083/

# Attestation — nonce is base64-encoded 32 bytes
curl -X POST http://localhost:49502/attestation \
  -H "Content-Type: application/json" \
  -d '{"nonce": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="}'
```

Expected attestation response (correct — NSM not available in QEMU):

```json
{"errors":["unable to get nonced attestation: AttestationGeneration (...)","could not initialize the NSM driver"]}
```

Expected warnings that are harmless:

- `socat: TUNSETIFF {"eth0"}: Invalid argument` — vsock TUN creation failed, but virtio-net eth0 still comes up
- `open("/dev/vsock"): No such file or directory` — standard kernel has no vsock driver
- `WARNING: NSM module not found at /nsm.ko` — expected, no hardware

### Local limitations

| Feature | Local QEMU | Production (Nitro) |
| --- | --- | --- |
| App starts, logs visible | Yes | Yes |
| Networking / port access | Standard kernel only | Yes (vsock tunnel) |
| NSM / attestation document | No | Yes |
| VSock proxies | No | Yes |
| PCR measurements | No | Yes |

## Common issues

### Enclave won't start

Symptoms include the app never becoming reachable, `nitro-cli describe-enclaves` showing no running enclave, or `nitro-enclave.service` restarting repeatedly.

Inspect enclave startup and check the allocator service:

```bash
sudo systemctl status nitro-enclaves-allocator.service
sudo journalctl -u nitro-enclave.service --no-pager
```

Common causes include insufficient memory or CPU allocation, or the EIF failing to download from S3.

### Application is unreachable

Symptoms include connection timeouts, failed health checks, or Caddy returning an upstream or proxy error.

Inspect the services for the configured encryption mode. Direct ingress requires the application-port VSOCK proxy. STEVE intentionally omits that proxy and instead requires the STEVE proxy on port `49500` and host Caddy:

```bash
# Direct ingress only
sudo systemctl status vsock-proxy-3000.service

# STEVE ingress
sudo systemctl status vsock-proxy-49500.service
sudo systemctl status caddy.service
```

If the mode-appropriate proxies are running but the app is not responding, use `sudo nitro-cli console` with debug mode to check whether the application and STEVE started correctly inside the enclave.

### Attestation fails after debugging

Symptoms include `caution verify` failing with:

> Cannot verify attestation: enclave is in debug mode

Debug mode zeros out PCR values, so attestation cannot be verified while debug mode is enabled.

Follow [Clean up debug access](<#clean-up-debug-access>): verify the production-mode replacement, activate it, and explicitly remove retained debug instances.
