|
| 1 | +<!-- Copyright (c) Microsoft Corporation. Licensed under the MIT License. --> |
| 2 | + |
| 3 | +# code_transparency_client |
| 4 | + |
| 5 | +Rust REST client for the Azure Code Transparency Service. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +This crate provides a high-level HTTP client for interacting with the |
| 10 | +[Azure Code Transparency](https://learn.microsoft.com/en-us/azure/confidential-ledger/code-transparency-overview) |
| 11 | +service (formerly Microsoft Supply-chain Transparency, MST). It follows |
| 12 | +canonical Azure SDK patterns — pipeline policies, long-running operation |
| 13 | +polling, and structured error handling — to submit COSE_Sign1 messages for |
| 14 | +transparent registration and retrieve receipts. |
| 15 | + |
| 16 | +Key capabilities: |
| 17 | + |
| 18 | +- **Entry submission** — `create_entry()` submits COSE_Sign1 messages and |
| 19 | + returns a `Poller<OperationStatus>` for async tracking |
| 20 | +- **Convenience signing** — `make_transparent()` submits and polls to |
| 21 | + completion in a single call |
| 22 | +- **Entry retrieval** — `get_entry()` / `get_entry_statement()` fetch |
| 23 | + registered entries and their original statements |
| 24 | +- **Key management** — `get_public_keys()` / `resolve_signing_key()` fetch |
| 25 | + and resolve JWKS for receipt verification |
| 26 | +- **Pipeline policies** — `ApiKeyAuthPolicy` for Bearer-token injection, |
| 27 | + `TransactionNotCachedPolicy` for fast 503 retries |
| 28 | +- **CBOR error handling** — Parses RFC 9290 CBOR Problem Details from |
| 29 | + service error responses |
| 30 | + |
| 31 | +## Architecture |
| 32 | + |
| 33 | +``` |
| 34 | +┌──────────────────────────────────────────────────────┐ |
| 35 | +│ code_transparency_client │ |
| 36 | +├─────────────────────────┬────────────────────────────┤ |
| 37 | +│ client │ models │ |
| 38 | +│ ┌────────────────────┐ │ ┌────────────────────────┐ │ |
| 39 | +│ │CodeTransparency │ │ │JsonWebKey │ │ |
| 40 | +│ │ Client │ │ │JwksDocument │ │ |
| 41 | +│ │ │ │ └────────────────────────┘ │ |
| 42 | +│ │ • create_entry() │ │ │ |
| 43 | +│ │ • make_transparent │ │ operation_status │ |
| 44 | +│ │ • get_entry() │ │ ┌────────────────────────┐ │ |
| 45 | +│ │ • get_public_keys()│ │ │OperationStatus │ │ |
| 46 | +│ │ • resolve_signing │ │ │ (StatusMonitor) │ │ |
| 47 | +│ │ _key() │ │ └────────────────────────┘ │ |
| 48 | +│ └────────────────────┘ │ │ |
| 49 | +├─────────────────────────┼────────────────────────────┤ |
| 50 | +│ Pipeline Policies │ Error Handling │ |
| 51 | +│ ┌────────────────────┐ │ ┌────────────────────────┐ │ |
| 52 | +│ │ApiKeyAuthPolicy │ │ │CodeTransparencyError │ │ |
| 53 | +│ │TransactionNot │ │ │CborProblemDetails │ │ |
| 54 | +│ │ CachedPolicy │ │ └────────────────────────┘ │ |
| 55 | +│ └────────────────────┘ │ │ |
| 56 | +├─────────────────────────┴────────────────────────────┤ |
| 57 | +│ polling (DelayStrategy, MstPollingOptions) │ |
| 58 | +│ mock_transport (SequentialMockTransport) [test-utils] │ |
| 59 | +└──────────────────────────────────────────────────────┘ |
| 60 | + │ │ |
| 61 | + ▼ ▼ |
| 62 | + azure_core cbor_primitives |
| 63 | + (Pipeline, Poller, cose_sign1_primitives |
| 64 | + StatusMonitor) |
| 65 | +``` |
| 66 | + |
| 67 | +## Modules |
| 68 | + |
| 69 | +| Module | Description | |
| 70 | +|--------|-------------| |
| 71 | +| `client` | `CodeTransparencyClient` — main HTTP client with entry submission, retrieval, and key management | |
| 72 | +| `models` | `JsonWebKey`, `JwksDocument` — JWKS types for receipt verification key resolution | |
| 73 | +| `operation_status` | `OperationStatus` — `StatusMonitor` implementation for long-running operation polling | |
| 74 | +| `polling` | `DelayStrategy` (fixed / exponential), `MstPollingOptions` — configurable polling behavior | |
| 75 | +| `api_key_auth_policy` | `ApiKeyAuthPolicy` — pipeline policy injecting `Authorization: Bearer {key}` headers | |
| 76 | +| `transaction_not_cached_policy` | `TransactionNotCachedPolicy` — fast-retry policy (250 ms × 8) for `TransactionNotCached` 503 errors | |
| 77 | +| `cbor_problem_details` | `CborProblemDetails` — RFC 9290 CBOR Problem Details parser for structured error responses | |
| 78 | +| `error` | `CodeTransparencyError` — structured errors with HTTP status codes and service messages | |
| 79 | +| `mock_transport` | `SequentialMockTransport` — mock HTTP transport for unit tests (behind `test-utils` feature) | |
| 80 | + |
| 81 | +## Key Types |
| 82 | + |
| 83 | +### CodeTransparencyClient |
| 84 | + |
| 85 | +```rust |
| 86 | +use code_transparency_client::{CodeTransparencyClient, MstPollingOptions}; |
| 87 | + |
| 88 | +// Create a client with API key authentication |
| 89 | +let client = CodeTransparencyClient::new( |
| 90 | + "https://my-instance.confidential-ledger.azure.com", |
| 91 | + Some("my-api-key".into()), |
| 92 | + None, // default options |
| 93 | +)?; |
| 94 | + |
| 95 | +// Submit a COSE_Sign1 message and wait for receipt |
| 96 | +let transparent_bytes = client |
| 97 | + .make_transparent(&cose_sign1_bytes, None) |
| 98 | + .await?; |
| 99 | +``` |
| 100 | + |
| 101 | +### Entry Submission with Polling |
| 102 | + |
| 103 | +```rust |
| 104 | +use code_transparency_client::CodeTransparencyClient; |
| 105 | + |
| 106 | +let client = CodeTransparencyClient::new(endpoint, api_key, None)?; |
| 107 | + |
| 108 | +// Start the long-running operation |
| 109 | +let poller = client.create_entry(&cose_sign1_bytes).await?; |
| 110 | + |
| 111 | +// Poll until complete (uses default delay strategy) |
| 112 | +let status = poller.wait().await?; |
| 113 | +let entry_id = status.entry_id.expect("entry registered"); |
| 114 | + |
| 115 | +// Retrieve the transparent entry |
| 116 | +let entry_bytes = client.get_entry(&entry_id).await?; |
| 117 | +``` |
| 118 | + |
| 119 | +### Receipt Key Resolution |
| 120 | + |
| 121 | +```rust |
| 122 | +use code_transparency_client::CodeTransparencyClient; |
| 123 | + |
| 124 | +// Resolve a signing key by key ID (checks cache first, then fetches JWKS) |
| 125 | +let jwk = client.resolve_signing_key("key-id-123", &jwks_cache).await?; |
| 126 | +``` |
| 127 | + |
| 128 | +### Custom Polling Options |
| 129 | + |
| 130 | +```rust |
| 131 | +use code_transparency_client::{MstPollingOptions, DelayStrategy}; |
| 132 | +use std::time::Duration; |
| 133 | + |
| 134 | +let options = MstPollingOptions { |
| 135 | + delay_strategy: DelayStrategy::Exponential { |
| 136 | + initial: Duration::from_secs(1), |
| 137 | + max: Duration::from_secs(30), |
| 138 | + }, |
| 139 | + max_retries: Some(20), |
| 140 | +}; |
| 141 | + |
| 142 | +let transparent = client |
| 143 | + .make_transparent(&cose_bytes, Some(options)) |
| 144 | + .await?; |
| 145 | +``` |
| 146 | + |
| 147 | +## Error Handling |
| 148 | + |
| 149 | +All operations return `CodeTransparencyError`: |
| 150 | + |
| 151 | +```rust |
| 152 | +pub enum CodeTransparencyError { |
| 153 | + /// HTTP or network error from the Azure pipeline. |
| 154 | + HttpError(azure_core::Error), |
| 155 | + /// Service returned a structured CBOR Problem Details response. |
| 156 | + ServiceError { |
| 157 | + status: u16, |
| 158 | + details: Option<CborProblemDetails>, |
| 159 | + message: String, |
| 160 | + }, |
| 161 | + /// Operation timed out or exceeded max retries. |
| 162 | + PollingTimeout, |
| 163 | + /// CBOR/COSE deserialization failure. |
| 164 | + DeserializationError(String), |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +The `TransactionNotCachedPolicy` automatically retries 503 responses with |
| 169 | +a `TransactionNotCached` error code up to 8 times at 250 ms intervals before |
| 170 | +surfacing the error to the caller. |
| 171 | + |
| 172 | +## Memory Design |
| 173 | + |
| 174 | +- **Pipeline-based I/O**: HTTP requests flow through an `azure_core::http::Pipeline` |
| 175 | + with configurable policies. Response bodies are read once and owned by the caller. |
| 176 | +- **COSE bytes are borrowed**: `create_entry()` and `make_transparent()` accept |
| 177 | + `&[u8]`, avoiding copies of potentially large COSE_Sign1 messages. |
| 178 | +- **JWKS caching**: `resolve_signing_key()` checks an in-memory cache before |
| 179 | + making network requests, avoiding redundant fetches. |
| 180 | + |
| 181 | +## Dependencies |
| 182 | + |
| 183 | +- `azure_core` — HTTP pipeline, `Poller`, `StatusMonitor`, retry policies |
| 184 | +- `cbor_primitives` — CBOR decoding for problem details and configuration |
| 185 | +- `cose_sign1_primitives` — COSE types shared with the signing/validation stack |
| 186 | +- `serde` / `serde_json` — JSON deserialization for JWKS responses |
| 187 | +- `tokio` — Async runtime for HTTP operations |
| 188 | + |
| 189 | +## Testing |
| 190 | + |
| 191 | +Enable the `test-utils` feature to access `SequentialMockTransport` for |
| 192 | +unit tests without network access: |
| 193 | + |
| 194 | +```toml |
| 195 | +[dev-dependencies] |
| 196 | +code_transparency_client = { path = ".", features = ["test-utils"] } |
| 197 | +``` |
| 198 | + |
| 199 | +```rust |
| 200 | +use code_transparency_client::mock_transport::SequentialMockTransport; |
| 201 | + |
| 202 | +let transport = SequentialMockTransport::new(vec![ |
| 203 | + mock_response(200, cose_bytes), |
| 204 | + mock_response(200, receipt_bytes), |
| 205 | +]); |
| 206 | +``` |
| 207 | + |
| 208 | +## See Also |
| 209 | + |
| 210 | +- [extension_packs/mst/](../) — MST trust pack using this client for receipt validation |
| 211 | +- [extension_packs/certificates/](../../certificates/) — Certificate trust pack |
| 212 | +- [Azure Code Transparency docs](https://learn.microsoft.com/en-us/azure/confidential-ledger/code-transparency-overview) |
| 213 | + |
| 214 | +## License |
| 215 | + |
| 216 | +Licensed under the [MIT License](../../../../../LICENSE). |
0 commit comments