|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +dstack is a developer-friendly, security-first framwwork for deploying containerized applications into Intel TDX (Trust Domain Extensions) Trusted Execution Environments (TEEs). The system provides end-to-end security through hardware-rooted attestation, automated key management, and zero-trust networking. |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +dstack consists of several core components that interact to provide TEE-based container deployment: |
| 12 | + |
| 13 | +### Core Components |
| 14 | + |
| 15 | +- **`dstack-vmm`** (`vmm/`): Virtual Machine Manager that runs on bare-metal TDX hosts. Orchestrates CVM lifecycle, manages QEMU processes, allocates resources, parses docker-compose files, and provides a web UI (port 9080) for deployment. |
| 16 | + |
| 17 | +- **`dstack-kms`** (`kms/`): Key Management System that handles cryptographic key provisioning after TDX quote verification. Derives keys deterministically per application identity and enforces authorization policies defined in smart contracts on Ethereum. |
| 18 | + |
| 19 | +- **`dstack-gateway`** (`gateway/`): Reverse proxy providing zero-trust network access. Handles TLS termination, automated ACME certificate provisioning, and traffic routing via ingress mapping rules. |
| 20 | + |
| 21 | +- **`dstack-guest-agent`** (`guest-agent/`): Runs inside each CVM to provide runtime services including Docker Compose lifecycle management, TDX quote generation, key provisioning from KMS, and log aggregation. Exposes API via Unix socket at `/var/run/dstack.sock`. |
| 22 | + |
| 23 | +### Communication Protocols |
| 24 | + |
| 25 | +- **RA-TLS**: Remote Attestation TLS used for all inter-CVM communication, embedding TDX quotes in X.509 certificates for mutual authentication |
| 26 | +- **`prpc`**: Protocol Buffers-based RPC framework used across all service APIs |
| 27 | +- **`vsock`**: Host-guest communication channel for metadata and configuration |
| 28 | +- **Unix Domain Sockets**: Used for local management (e.g., `vmm.sock`) |
| 29 | + |
| 30 | +### Additional Components |
| 31 | + |
| 32 | +- **`certbot`** (`certbot/`): Automated ACME DNS-01 certificate management |
| 33 | +- **`ct_monitor`** (`ct_monitor/`): Certificate Transparency log monitoring |
| 34 | +- **`verifier`** (`verifier/`): TDX quote verification service using `dcap-qvl` |
| 35 | +- **`supervisor`** (`supervisor/`): Process supervision inside CVMs |
| 36 | +- **SDKs** (`sdk/`): Client SDKs in Rust, Python, Go, and JavaScript for interacting with guest-agent APIs |
| 37 | + |
| 38 | +## Build Commands |
| 39 | + |
| 40 | +### Rust Components |
| 41 | + |
| 42 | +```bash |
| 43 | +# Build all components |
| 44 | +cargo build --release |
| 45 | + |
| 46 | +# Build specific components |
| 47 | +cargo build --release -p dstack-vmm |
| 48 | +cargo build --release -p dstack-kms |
| 49 | +cargo build --release -p dstack-gateway |
| 50 | +cargo build --release -p dstack-guest-agent |
| 51 | + |
| 52 | +# Check code |
| 53 | +cargo check --all-features |
| 54 | + |
| 55 | +# Format code |
| 56 | +cargo fmt --all |
| 57 | + |
| 58 | +# Lint with Clippy |
| 59 | +cargo clippy -- -D warnings --allow unused_variables |
| 60 | +``` |
| 61 | + |
| 62 | +### Ethereum Smart Contracts (KMS Auth) |
| 63 | + |
| 64 | +```bash |
| 65 | +cd kms/auth-eth |
| 66 | +npm install |
| 67 | +npm run build # Compile TypeScript |
| 68 | +npm test # Run tests |
| 69 | +npm run test:coverage # Run tests with coverage |
| 70 | + |
| 71 | +# Hardhat commands |
| 72 | +npx hardhat compile |
| 73 | +npx hardhat test |
| 74 | +npx hardhat node # Start local node |
| 75 | +``` |
| 76 | + |
| 77 | +### Python SDK |
| 78 | + |
| 79 | +```bash |
| 80 | +cd sdk/python |
| 81 | +make install # Install dependencies |
| 82 | +make test # Run tests |
| 83 | +``` |
| 84 | + |
| 85 | +## Test Commands |
| 86 | + |
| 87 | +### Running All Tests |
| 88 | + |
| 89 | +```bash |
| 90 | +# Run all Rust tests (requires simulator) |
| 91 | +./run-tests.sh |
| 92 | +``` |
| 93 | + |
| 94 | +This script: |
| 95 | +1. Builds the SDK simulator (`sdk/simulator/`) |
| 96 | +2. Starts the simulator in background |
| 97 | +3. Sets `DSTACK_SIMULATOR_ENDPOINT` and `TAPPD_SIMULATOR_ENDPOINT` |
| 98 | +4. Runs `cargo test --all-features -- --show-output` |
| 99 | + |
| 100 | +### Running Specific Tests |
| 101 | + |
| 102 | +```bash |
| 103 | +# Run tests for a specific package |
| 104 | +cargo test -p dstack-kms --all-features |
| 105 | + |
| 106 | +# Run a specific test |
| 107 | +cargo test --all-features test_name |
| 108 | + |
| 109 | +# Run tests with output |
| 110 | +cargo test --all-features -- --show-output --test-threads=1 |
| 111 | +``` |
| 112 | + |
| 113 | +### Foundry Tests (Ethereum Contracts) |
| 114 | + |
| 115 | +```bash |
| 116 | +cd kms/auth-eth |
| 117 | + |
| 118 | +# Run all Foundry tests |
| 119 | +forge test |
| 120 | + |
| 121 | +# Run with verbosity |
| 122 | +forge test -vv |
| 123 | + |
| 124 | +# Run specific test contract |
| 125 | +forge test --match-contract UpgradesWithPluginTest -vv |
| 126 | + |
| 127 | +# Clean build artifacts |
| 128 | +forge clean |
| 129 | +``` |
| 130 | + |
| 131 | +## Code Style Guidelines |
| 132 | + |
| 133 | +### Logging and Error Messages |
| 134 | + |
| 135 | +- **Never capitalize** the first letter of log messages and error messages |
| 136 | +- Example: `log::info!("starting server on port {}", port);` |
| 137 | +- Example: `anyhow::bail!("failed to connect to server");` |
| 138 | + |
| 139 | +This rule is enforced in `.cursorrules`. |
| 140 | + |
| 141 | +## Key Security Concepts |
| 142 | + |
| 143 | +### Attestation Flow |
| 144 | + |
| 145 | +1. **Quote Generation**: Applications request TDX quotes via `getQuote()` with reportData (up to 64 bytes) |
| 146 | +2. **Quote Verification**: `dstack-verifier` validates quotes using `dcap-qvl`, verifies OS image hash, and replays RTMRs from event logs |
| 147 | +3. **RTMR Replay**: Compute Runtime Measurement Register values by applying SHA384 hashing to event log entries |
| 148 | + |
| 149 | +### Key Management |
| 150 | + |
| 151 | +- **Deterministic Keys**: `getKey(path, purpose)` derives secp256k1 keys using HKDF, with signature chains proving TEE origin |
| 152 | +- **TLS Keys**: `getTlsKey()` generates fresh X.509 certificates with optional RA-TLS support |
| 153 | +- **Environment Encryption**: Client-side encryption using X25519 ECDH + AES-256-GCM, decrypted only in TEE |
| 154 | + |
| 155 | +### Smart Contract Integration |
| 156 | + |
| 157 | +- **DstackKms**: Main KMS contract managing OS image whitelist and app registration |
| 158 | +- **DstackApp**: Per-app authorization contract controlling device IDs and compose hash whitelist |
| 159 | +- Deployed on Ethereum-compatible networks (Phala Network) |
| 160 | + |
| 161 | +## Development Workflow |
| 162 | + |
| 163 | +### Local Development Setup |
| 164 | + |
| 165 | +1. Build meta-dstack artifacts (see README.md section "Build and Run") |
| 166 | +2. Download or build guest OS image |
| 167 | +3. Run components in separate terminals: |
| 168 | + - KMS: `./dstack-kms -c kms.toml` |
| 169 | + - Gateway: `sudo ./dstack-gateway -c gateway.toml` |
| 170 | + - VMM: `./dstack-vmm -c vmm.toml` |
| 171 | + |
| 172 | +### Deploying Apps |
| 173 | + |
| 174 | +- Via Web UI: `http://localhost:9080` (or configured port) |
| 175 | +- Via CLI: `./vmm-cli.py` (see `docs/vmm-cli-user-guide.md`) |
| 176 | +- Requires: |
| 177 | + 1. On-chain app registration (`npx hardhat kms:create-app`) |
| 178 | + 2. Adding compose hash to whitelist (`npx hardhat app:add-hash`) |
| 179 | + 3. Deploying via VMM with App ID |
| 180 | + |
| 181 | +### Accessing Deployed Apps |
| 182 | + |
| 183 | +Ingress mapping pattern: `<id>[-[<port>][s|g]].<base_domain>` |
| 184 | +- Default: TLS termination to TCP |
| 185 | +- `s` suffix: TLS passthrough |
| 186 | +- `g` suffix: HTTP/2 with TLS termination (gRPC) |
| 187 | + |
| 188 | +## Important Files |
| 189 | + |
| 190 | +- `Cargo.toml`: Workspace configuration with all Rust crates |
| 191 | +- `vmm.toml`: VMM configuration (CID pool, port mapping, KMS/gateway URLs) |
| 192 | +- `kms.toml`: KMS configuration (contract addresses, RPC endpoints) |
| 193 | +- `gateway.toml`: Gateway configuration (domain, certificates, WireGuard) |
| 194 | +- `docker-compose.yaml`: App deployment format (normalized to `.app-compose.json`) |
| 195 | + |
| 196 | +## Common Tasks |
| 197 | + |
| 198 | +### Adding a New Rust Crate |
| 199 | + |
| 200 | +1. Create crate directory and `Cargo.toml` |
| 201 | +2. Add to workspace members in root `Cargo.toml` |
| 202 | +3. Add workspace dependency if it will be used by other crates |
| 203 | + |
| 204 | +### Modifying RPC APIs |
| 205 | + |
| 206 | +RPC definitions use `prpc` framework with Protocol Buffers: |
| 207 | +- Define `.proto` files in `*/rpc/proto/` |
| 208 | +- Use `prpc-build` in `build.rs` to generate Rust code |
| 209 | +- Implement service traits in main crate |
| 210 | + |
| 211 | +### Working with TDX Quotes |
| 212 | + |
| 213 | +- Low-level bindings: `tdx-attest-sys/` (FFI to libtdx-attest) |
| 214 | +- High-level API: `tdx-attest/` |
| 215 | +- Verification: `verifier/` using `dcap-qvl` |
| 216 | +- Event log parsing: `cc-eventlog/` |
| 217 | + |
| 218 | +## Documentation |
| 219 | + |
| 220 | +- Main README: `README.md` |
| 221 | +- Deployment guide: `docs/deployment.md` |
| 222 | +- VMM CLI guide: `docs/vmm-cli-user-guide.md` |
| 223 | +- Security guide: `docs/security-guide/security-guide.md` |
| 224 | +- Design decisions: `docs/design-and-hardening-decisions.md` |
| 225 | + |
| 226 | +When need more detailed info, try to use deepwiki mcp. |
0 commit comments