Skip to content

Commit 0eb532d

Browse files
authored
Merge pull request #366 from Dstack-TEE/sdk-sync
2 parents 20befe3 + ac61a5e commit 0eb532d

File tree

9 files changed

+3508
-2
lines changed

9 files changed

+3508
-2
lines changed

.claude/agents/sdk-sync-checker.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
name: protobuf-sdk-validator
3+
description: Validates that SDK implementations are synchronized with Protocol Buffer schema definitions. Use when protobuf files change, SDKs need verification, or you suspect schema drift.
4+
tools: Bash, Glob, Grep, Read, TodoWrite
5+
model: sonnet
6+
color: yellow
7+
---
8+
9+
You validate SDK implementations against protobuf schemas to ensure synchronization.
10+
11+
## Process
12+
13+
### 1. Discovery
14+
- Find all `.proto` files in `guest-agent/rpc/proto/`
15+
- Identify SDK implementations in `sdk/` (python, go, rust, js, curl docs)
16+
- Extract services, RPCs, and message types from proto files
17+
18+
### 2. Extract Schema
19+
For each message type, extract:
20+
- Field names, types, and numbers
21+
- Required/optional/repeated modifiers
22+
- Nested types and enums
23+
- Service method signatures
24+
25+
### 3. Compare SDKs
26+
For each SDK, verify message/response types contain:
27+
- All proto fields (accounting for naming conventions)
28+
- Correct type mappings (bytes→hex string, string→string, repeated→array)
29+
- Proper optionality markers
30+
31+
### 4. Report
32+
33+
```markdown
34+
# SDK Sync Report
35+
36+
## Summary
37+
Status: ✅/❌ | Protos: X | SDKs: Y | Issues: Z
38+
39+
## Findings
40+
41+
### [SDK Name] (path/to/file.ext)
42+
| Proto Message | Status | Missing Fields |
43+
|---------------|--------|----------------|
44+
| MessageName | ❌ | field1, field2 |
45+
46+
Details:
47+
- ❌ MessageName.field1: missing (proto line X, expected in SDK)
48+
- ❌ MessageName.field2: missing (proto line Y, expected in SDK)
49+
50+
## Action Items
51+
1. [SDK]: Add field X to MessageY (file.ext:lineN)
52+
2. [SDK]: Fix type mismatch for field Z
53+
```
54+
55+
## Type Mappings
56+
- `bytes` → hex `string` (Python/Go/JS/Rust), `string` JSON (cURL docs)
57+
- `string``string` (all)
58+
- `repeated X` → array/list/vec (language-specific)
59+
- `int32/uint32` → number/int types
60+
61+
## Naming Conventions
62+
- Python: `snake_case`
63+
- Go: `PascalCase` (exported fields)
64+
- Rust: `snake_case`
65+
- JavaScript: `camelCase`
66+
- cURL docs: `snake_case` (JSON wire format)
67+
68+
## Locations
69+
- Protos: `guest-agent/rpc/proto/*.proto`
70+
- Python: `sdk/python/src/dstack_sdk/dstack_client.py`
71+
- Go: `sdk/go/dstack/client.go`
72+
- Rust: `sdk/rust/types/src/dstack.rs`
73+
- JS: `sdk/js/src/index.ts`
74+
- Docs: `sdk/curl/api.md`, `sdk/curl/api-tappd.md`
75+
76+
Focus on API surface differences. Provide specific file paths and line numbers.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ node_modules/
77
/.cargo
88
.venv
99
/tmp
10-
.claude
10+
.claude/settings.local.json

CLAUDE.md

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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.

sdk/curl/api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ curl --unix-socket /var/run/dstack.sock http://dstack/Info
156156
"tcb_info": "<tcb-info-string>",
157157
"app_name": "my-app",
158158
"device_id": "<hex-encoded-device-id>",
159+
"mr_aggregated": "<hex-encoded-mr-aggregated>",
159160
"os_image_hash": "<hex-encoded-os-image-hash>",
160161
"key_provider_info": "<key-provider-info-string>",
161-
"compose_hash": "<hex-encoded-compose-hash>"
162+
"compose_hash": "<hex-encoded-compose-hash>",
163+
"vm_config": "<json-vm-config-string>"
162164
}
163165
```
164166

sdk/go/dstack/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type GetQuoteResponse struct {
3838
Quote []byte `json:"quote"`
3939
EventLog string `json:"event_log"`
4040
ReportData []byte `json:"report_data"`
41+
VmConfig string `json:"vm_config"`
4142
}
4243

4344
// Represents an event log entry in the TCB info
@@ -72,10 +73,12 @@ type InfoResponse struct {
7273
TcbInfo string `json:"tcb_info"`
7374
AppName string `json:"app_name"`
7475
DeviceID string `json:"device_id"`
76+
MrAggregated string `json:"mr_aggregated,omitempty"`
7577
KeyProviderInfo string `json:"key_provider_info"`
7678
// Optional: empty if OS image is not measured by KMS
7779
OsImageHash string `json:"os_image_hash,omitempty"`
7880
ComposeHash string `json:"compose_hash"`
81+
VmConfig string `json:"vm_config,omitempty"`
7982
}
8083

8184
// DecodeTcbInfo decodes the TcbInfo string into a TcbInfo struct

0 commit comments

Comments
 (0)