-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add POST /v1/enclave-assignment and an attestation-verifying client #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ea21a9a
feat(api): replace enclave keys route with POST /v1/enclave-assignment
kilianglas 97bbfc9
feat(client): add Nitro attestation verifier ported from bedrock
kilianglas 8e3fbca
feat(client): add the assignment HTTP client and CLI
kilianglas 1e3230d
feat(e2e): verify attestations instead of parsing them
kilianglas 112970f
refactor: tighten comments and drop redundant tests
kilianglas a0e0d9d
refactor(client): drop the verifier-client binary
kilianglas cecf4bd
refactor(client): introduce ClientConfig and a reusable request path
kilianglas ee05046
refactor(client): configure the client the way world-id-protocol does
kilianglas 405db17
refactor: simplify the client and dedup the route error handling
kilianglas a71b7fa
refactor: cleanup
kilianglas 37d6074
fix(client): reject empty PCR configurations instead of matching them
kilianglas 39d29f3
fix(api): restore the doc comment missing_docs requires
kilianglas c747baf
docs(client): add the MIT notice the ported code requires
kilianglas 3135e9c
refactor(api): centralize error mapping and test routes through the r…
kilianglas 8da712f
refactor(client): rename Client to FaceVerifierClient
kilianglas af932d6
docs: state invariants rather than what changed
kilianglas b7065c8
Merge branch 'main' into kilianglas/enclave-assignment
kilianglas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,4 +11,6 @@ | |
| pub mod enclave; | ||
| pub mod routes; | ||
| pub mod server; | ||
| #[cfg(test)] | ||
| mod test_support; | ||
| pub mod types; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| use axum::{Json, extract::State, http::StatusCode}; | ||
| use base64::{Engine as _, engine::general_purpose::STANDARD}; | ||
| use enclave_types::EnclaveError; | ||
| use serde::Serialize; | ||
|
|
||
| use crate::enclave::EnclaveClientError; | ||
| use crate::types::AppState; | ||
|
|
||
| /// The enclave assigned to a client, as an attestation document. | ||
| /// | ||
| /// The document already carries the enclave's identity and expiry, and the client verifies it | ||
| /// before trusting either, so the host relays opaque bytes and adds no fields of its own. | ||
| #[derive(Debug, Serialize)] | ||
| pub struct EnclaveAssignmentResponse { | ||
| attestation: String, | ||
| } | ||
|
|
||
| /// Assigns this host's enclave by returning its encryption-key attestation. | ||
| pub async fn handler( | ||
| State(state): State<AppState>, | ||
| ) -> Result<Json<EnclaveAssignmentResponse>, StatusCode> { | ||
| // TODO: Cache the attestation document, invalidating on enclave reconnect, and bound the | ||
| // entry's lifetime by the document certificate's validity. Until then every request costs | ||
| // an NSM attestation, so this route must not carry production traffic uncapped. | ||
| let response = state | ||
| .enclave_client() | ||
| .get_enclave_keys() | ||
| .await | ||
| .map_err(|error| { | ||
| let status = status_for(&error); | ||
| tracing::error!( | ||
| ?error, | ||
| %status, | ||
| dependency = "secure-enclave", | ||
| failure_class = error.failure_class(), | ||
| "enclave assignment failed" | ||
| ); | ||
| status | ||
| })?; | ||
|
|
||
| Ok(Json(EnclaveAssignmentResponse { | ||
| attestation: STANDARD.encode(response.encryption_key_attestation), | ||
| })) | ||
| } | ||
|
|
||
| /// Maps an enclave-client failure to an HTTP status. | ||
| const fn status_for(error: &EnclaveClientError) -> StatusCode { | ||
|
kilianglas marked this conversation as resolved.
Outdated
|
||
| match error { | ||
| EnclaveClientError::Timeout => StatusCode::GATEWAY_TIMEOUT, | ||
| EnclaveClientError::Transport(_) => StatusCode::SERVICE_UNAVAILABLE, | ||
| EnclaveClientError::Operation(operation) => match operation { | ||
| EnclaveError::NotReady | ||
| | EnclaveError::SecureModuleNotInitialized | ||
| | EnclaveError::AttestationFailed => StatusCode::SERVICE_UNAVAILABLE, | ||
| // Match-path errors cannot arise from an attestation request. Reaching one means | ||
| // the enclave answered a request it was not asked, so surface a host bug rather | ||
| // than retryable unavailability. | ||
| EnclaveError::DecryptFailed | ||
| | EnclaveError::MalformedMatchPayload | ||
| | EnclaveError::InvalidHashesJson | ||
| | EnclaveError::ThumbnailHashMismatch | ||
| | EnclaveError::MatchBelowThreshold | ||
| | EnclaveError::InvalidImage | ||
| | EnclaveError::EmbeddingGenerationFailed | ||
| | EnclaveError::EmbeddingComparisonFailed => StatusCode::INTERNAL_SERVER_ERROR, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use axum::{extract::State, http::StatusCode}; | ||
| use enclave_types::{EnclaveError, GetEnclaveKeysResponse}; | ||
|
|
||
| use super::{handler, status_for}; | ||
| use crate::enclave::EnclaveClientError; | ||
| use crate::test_support::{StubEnclaveClient, state_with}; | ||
|
|
||
| #[tokio::test] | ||
| async fn returns_the_encryption_key_attestation_and_nothing_else() { | ||
| let state = state_with(StubEnclaveClient { | ||
| keys: Some(Ok(GetEnclaveKeysResponse { | ||
| encryption_key_attestation: vec![1, 2, 3], | ||
| signing_key_attestation: vec![4, 5, 6], | ||
| })), | ||
| ..StubEnclaveClient::default() | ||
| }); | ||
|
|
||
| let response = handler(State(state)) | ||
| .await | ||
| .expect("a reachable enclave should yield an assignment") | ||
| .0; | ||
|
|
||
| assert_eq!(response.attestation, "AQID"); | ||
|
|
||
| let json = serde_json::to_value(&response).expect("response should serialize"); | ||
| assert_eq!(json.as_object().map(serde_json::Map::len), Some(1)); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn maps_enclave_failure_to_its_status() { | ||
| let state = state_with(StubEnclaveClient { | ||
| keys: Some(Err(EnclaveClientError::Timeout)), | ||
| ..StubEnclaveClient::default() | ||
| }); | ||
|
|
||
| let status = handler(State(state)) | ||
| .await | ||
| .expect_err("a timed-out enclave should not yield an assignment"); | ||
|
|
||
| assert_eq!(status, StatusCode::GATEWAY_TIMEOUT); | ||
| } | ||
|
|
||
| #[test] | ||
| fn status_mapping_is_exhaustive_and_classified() { | ||
| assert_eq!( | ||
| status_for(&EnclaveClientError::Timeout), | ||
| StatusCode::GATEWAY_TIMEOUT | ||
| ); | ||
| assert_eq!( | ||
| status_for(&EnclaveClientError::Transport("boom".to_string())), | ||
| StatusCode::SERVICE_UNAVAILABLE | ||
| ); | ||
|
|
||
| for operation in [ | ||
| EnclaveError::NotReady, | ||
| EnclaveError::SecureModuleNotInitialized, | ||
| EnclaveError::AttestationFailed, | ||
| ] { | ||
| assert_eq!( | ||
| status_for(&EnclaveClientError::Operation(operation)), | ||
| StatusCode::SERVICE_UNAVAILABLE, | ||
| "{operation:?} should read as retryable unavailability" | ||
| ); | ||
| } | ||
|
|
||
| assert_eq!( | ||
| status_for(&EnclaveClientError::Operation(EnclaveError::DecryptFailed)), | ||
| StatusCode::INTERNAL_SERVER_ERROR | ||
| ); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.