11# Dstack Crate
22
3- This crate provides a rust client for communicating with the dstack server, which is available inside dstack.
3+ This crate provides rust clients for communicating with both the current dstack server and the legacy tappd service , which are available inside dstack.
44
55## Installation
66
@@ -11,8 +11,10 @@ dstack-rust = { git = "https://github.com/Dstack-TEE/dstack.git", package = "dst
1111
1212## Basic Usage
1313
14+ ### DstackClient (Current API)
15+
1416``` rust
15- use dstack_sdk :: DstackClient ;
17+ use dstack_sdk :: dstack_client :: DstackClient ;
1618
1719#[tokio:: main]
1820async fn main () -> Result <(), Box <dyn std :: error :: Error >> {
@@ -40,46 +42,104 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4042}
4143```
4244
45+ ### TappdClient (Legacy API)
46+
47+ ``` rust
48+ use dstack_sdk :: tappd_client :: TappdClient ;
49+
50+ #[tokio:: main]
51+ async fn main () -> Result <(), Box <dyn std :: error :: Error >> {
52+ let client = TappdClient :: new (None ); // Uses env var or default to Unix socket
53+
54+ // Get system info
55+ let info = client . info (). await ? ;
56+ println! (" Instance ID: {}" , info . instance_id);
57+ println! (" App Name: {}" , info . app_name);
58+
59+ // Derive a key
60+ let key_resp = client . derive_key (" my-app" ). await ? ;
61+ println! (" Key: {}" , key_resp . key);
62+ println! (" Certificate Chain: {:?}" , key_resp . certificate_chain);
63+
64+ // Decode the key to bytes (extracts raw ECDSA P-256 private key - 32 bytes)
65+ let key_bytes = key_resp . to_bytes ()? ;
66+ println! (" ECDSA P-256 private key bytes (32 bytes): {:?}" , key_bytes . len ());
67+
68+ // Generate quote (exactly 64 bytes of report data required)
69+ let mut report_data = b " test-data" . to_vec ();
70+ report_data . resize (64 , 0 ); // Pad to 64 bytes
71+ let quote_resp = client . get_quote (report_data ). await ? ;
72+ println! (" Quote: {}" , quote_resp . quote);
73+ let rtmrs = quote_resp . replay_rtmrs ()? ;
74+ println! (" Replayed RTMRs: {:?}" , rtmrs );
75+
76+ Ok (())
77+ }
78+ ```
79+
4380## Features
44- ### Initialization
81+
82+ ### DstackClient Initialization
4583
4684``` rust
4785let client = DstackClient :: new (Some (" http://localhost:8000" ));
4886```
4987- ` endpoint ` : Optional HTTP URL or Unix socket path (` /var/run/dstack.sock ` by default)
50-
5188- Will use the ` DSTACK_SIMULATOR_ENDPOINT ` environment variable if set
5289
53- ## Methods
90+ ### TappdClient Initialization (Legacy API)
5491
55- ### ` info(): InfoResponse `
92+ ``` rust
93+ let client = TappdClient :: new (Some (" /var/run/tappd.sock" ));
94+ ```
95+ - ` endpoint ` : Optional HTTP URL or Unix socket path (` /var/run/tappd.sock ` by default)
96+ - Will use the ` TAPPD_SIMULATOR_ENDPOINT ` environment variable if set
97+ - Supports the legacy tappd.sock API for backwards compatibility
5698
57- Fetches metadata and measurements about the CVM instance.
99+ ## API Methods
58100
59- ### ` get_key(path: Option<String>, purpose: Option<String>) -> GetKeyResponse `
101+ ### DstackClient Methods
60102
61- Derives a key for a specified path and optional purpose.
103+ #### ` info(): InfoResponse `
104+ Fetches metadata and measurements about the CVM instance.
62105
106+ #### ` get_key(path: Option<String>, purpose: Option<String>) -> GetKeyResponse `
107+ Derives a key for a specified path and optional purpose.
63108- ` key ` : Private key in hex format
64-
65109- ` signature_chain ` : Vec of X.509 certificate chain entries
66110
67- ### ` get_quote(report_data: Vec<u8>) -> GetQuoteResponse `
68-
111+ #### ` get_quote(report_data: Vec<u8>) -> GetQuoteResponse `
69112Generates a TDX quote with a custom 64-byte payload.
70-
71113- ` quote ` : Hex-encoded quote
72-
73114- ` event_log ` : Serialized list of events
74-
75115- ` replay_rtmrs() ` : Reconstructs RTMR values from the event log
76116
77- ### ` emit_event(event: String, payload: Vec<u8>) `
117+ #### ` emit_event(event: String, payload: Vec<u8>) `
78118Sends an event log with associated binary payload to the runtime.
79119
80- ### ` get_tls_key(...) -> GetTlsKeyResponse `
120+ #### ` get_tls_key(...) -> GetTlsKeyResponse `
81121Requests a key and X.509 certificate chain for RA-TLS or server/client authentication.
82122
123+ ### TappdClient Methods (Legacy API)
124+
125+ #### ` info(): TappdInfoResponse `
126+ Fetches metadata and measurements about the CVM instance.
127+
128+ #### ` derive_key(path: &str) -> DeriveKeyResponse `
129+ Derives a key for a specified path.
130+ - ` key ` : ECDSA P-256 private key in PEM format
131+ - ` certificate_chain ` : Vec of X.509 certificate chain entries
132+ - ` to_bytes() ` : Extracts and returns the raw ECDSA P-256 private key bytes (32 bytes)
133+
134+ #### ` derive_key_with_subject(path: &str, subject: &str) -> DeriveKeyResponse `
135+ Derives a key with a custom certificate subject.
136+
137+ #### ` derive_key_with_subject_and_alt_names(path: &str, subject: Option<&str>, alt_names: Option<Vec<String>>) -> DeriveKeyResponse `
138+ Derives a key with full certificate customization.
139+
140+ #### ` get_quote(report_data: Vec<u8>) -> TdxQuoteResponse `
141+ Generates a TDX quote with exactly 64 bytes of raw report data.
142+
83143### Structures
84144- ` GetKeyResponse ` : Holds derived key and signature chain
85145
@@ -104,7 +164,20 @@ cd dstack/sdk/simulator
104164Set the endpoint in your environment:
105165
106166```
107- export DSTACK_SIMULATOR_ENDPOINT=http://localhost:8000
167+ export DSTACK_SIMULATOR_ENDPOINT=/path/to/dstack-simulator/dstack.sock
168+ ```
169+
170+ ## Examples
171+
172+ See the ` examples/ ` directory for comprehensive usage examples:
173+
174+ - ` examples/dstack_client_usage.rs ` - Complete example using the current DstackClient API
175+ - ` examples/tappd_client_usage.rs ` - Complete example using the legacy TappdClient API
176+
177+ Run examples with:
178+ ``` bash
179+ cargo run --example dstack_client_usage
180+ cargo run --example tappd_client_usage
108181```
109182
110183## License
0 commit comments